Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have been hearing that the controller should be as concise as possible. So, I try to keep most of the processing work in the helper for the corresponding controller. But, I am little confused regarding whether I should instead use private controller methods or helper methods.
I am using helper methods only for processing and returning values. They have no other usage for now. They are not called from views.
That's a bad idea in general. Helper methods shouldn't be used except for specific convert-something-into-string stuff (basically, should be used only in views).
For anything else, you should use service objects or something similar, basically, PORO (plain old ruby objects)
In controllers you want to handle authentication and render the right thing, you don't want to deal with anything else otherwise they become too complex.
You might want to check these books to improve knowledge about this topic:
Growing Rails applications which is a really good book on how to avoid putting code in wrong places and how to keep codebase maintenable on the long term
POODR which is a Ruby guide on how to write good Object-Oriented code, it's a must for any developer and will help you understand why using an helper it's a bad idea
Usually, a Helper is called in a template or a view.
If you want to keep your controller DRY, use functions in your models or in services (read this good article about services by Ben Lewis)
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Can someone please explain why it is that in most of the documentation I’ve read everyone drums home the theory that it’s bad to include code in a Grails GSP yet, even in the same doc you find varying degrees of logic in a GSP? Is there some unwritten rule as to how much code a GSP should have or is it a judgement call as to what you think is safe?
TIA
There are several reasons. The main reason is because Grails roughly follows the MVC pattern, with a service layer added in. Most of your business logic should be in services or data services. Your controllers should only be concerned with taking in parameters (preferably in command objects), calling services, routing, and rendering, the views should just render the model sent from the controller. The controllers should just have templating logic, loops, and conditionals, but nothing that requires a transactions or bean references. Organizing you logic this way gives you a separation of concerns, and like the previous comment mentioned it makes everything easier to test.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Personally I prefer callback over delegate in Swift for simple logical correlations, because it's pretty straight-forward and easy to understand. At the same time, some prefers delegate, since delegation is a popular pattern in other languages, such as C#.
There are some discussions I found online:
1. "Why you shouldn't use delegates in Swift?"
https://medium.cobeisfresh.com/why-you-shouldn-t-use-delegates-in-swift-7ef808a7f16b#.hqb7zrc1v
2. Apple is shifting its focus more on the callback pattern
https://www.reddit.com/r/swift/comments/2ces1q/closures_vs_delegates/
3. blocks or delegates?
http://blog.stablekernel.com/blocks-or-delegates/
After reading these discussions, I am still undecided on the preference.
I would like to know when is better to use closures and when is better to use delegates? and reasons?
Thanks!
(Opinion based answer for an opinion based question)
The questions shouldn't be which is better, it should be what's the best solution for the problem I'm trying to solve.
My simple rule: if something requires one function as it's interface, a callback is usually a good solution. If more than one function is required, especially when they're required for the basic function of an object, a Delegate is probably a better solution.
As always it depends on the specific situation, but absolute statements rarely work out in real-world usage.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
So, what most comfortable and useful for developer? A similar question was already 4 years ago, and many things could change.
http://activeadmin.info/
https://github.com/sferik/rails_admin
or maybe Typus https://github.com/typus/typus???
Disclaimer: this is only opinion. This kind of question can have no 'correct' answer.
I use Rails Admin a lot on my current main project. It has advantages and disadvantages.
Advantages:
It handles things like nested forms for ActiveRecord relationships out of the box.
Disadvantages:
Adding features or custom behaviour is quite difficult and documentation is over-complex and fragmented.
Putting the DSL for CMS behaviour in the models is not great for code separation.
The default style is a little outdated.
The DSL itself is over-complex, badly documented and prone to code bloat and duplication.
The use of PJAX for page updates can complicate any JS you wish to add to the page. (If you're not careful your code will be run multiple times, or not at all, for a single page.)
The default mechanism for saving content items is incompatible with a database that uses referential integrity. This is a very bad thing. (I ended up patching this code.)
I haven't used ActiveAdmin as much, but when I have I noticed the following things:
Out-of-the-box, you have to roll more of your own features when it comes to things like nested forms for relationships. (This may have changed over time.)
It looks lovely.
The DSL is neatly separated from the model code and feels more logical.
Adding your own features is a lot easier with less code.
Adding JS is pretty trivial.
I don't know if Active Admin is better for referential integrity, but if this interests you let me know in comments and I'll set up a couple of trial projects to show you what I mean.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
When I first started with ruby/rails and noticed that you don't need to put parenthesis, I recall thinking "no way I'll do that!".
But now I kind of like it.
So the question is, is that good style? Or should I stick to using parenthesis?
Thanks.
The downvotes are probably because SO is technically not a good forum for this kind of question - apparently SO community likes clear, answerable, discrete, objective questions, not more general style or technique questions like this. But I love questions like these, so here goes.
Since working with Ruby various little style questions like this drive me absolutely crazy, which is stupid because they're not that big of a deal. But the Ruby community seems obsessed with style questions, maybe it just comes with the territory of having an extremely expressive language.
My answer:
I try to orient everything around making my code as intelligible as possible, without making it unreasonably long. If I'm struggling between two styles, I'll almost always come down on the style that I think will be easier for a more novice developer to understand in the future.
I omit parentheses when either there's no arguments, or when the argument "flows naturally" after the method name. So if the method is describing an action and the first parameter is the object of that method, it's beautiful to omit the parentheses. Like this:
create :user, name: "Topher"
raise "Problem!" unless #thingy.is_a? String
render partial: "filters"
But often the first parameter of the method is a hash of options or is an incidental value or something other than "the object of the action being taken". In these cases, I usually find it looks wrong / jarring to leave out the parentheses. Like in these cases:
#user.disable!(force: true)
#entries.paginate(page: params[:page])
And of course, if you're chaining methods together (like building up an ActiveRecord call), you need to include the parentheses if any params are present, so the question becomes moot.
A more general answer:
If you haven't already, read the Ruby style guide, from start to finish, a couple times. Some rules will appeal to you, some will repulse you, but exposing yourself to it will get you familiar with the styles and patterns used by many many other Rubyists (who debate about those styles ad nauseum) and when you're confronted with similar situations, you'll be more conscious of the different ways to approach it and what tradeoffs of those different styles might be.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm playing with the ASP.NET MVC Framework, trying to create a simple site.
My model is essentially:
-Questions
-Answers
-Categories
Since each Question must belong to a Category and each Answer must belong to a question - is it correct to split 'Category' into it's own controller?
What would be incorrect about creating a controller path of /Question/Category/List?
Also, if I wanted to create a search - do I then create a controller called 'Search' and use like so: /Search/Question/, /Search/Answer/? Or do I use '/Question/Search/'?
Thank you in advance for any insight.
Below is a read regarding grouping controllers
Grouping Conrollers
The above is not directly related to your question but may be useful.
Now regarding your question, I'll take it this way.. (This is just one way)..
My initial take on this...
-for Questions and Category
/Question/Ask
/Question/Edit/{id}
/Question/List
/Question/Search/{criteria}
/Category/Create
/Category/Edit/{id}
/Category/List
-for answer
/Question/Answer/{questionid}
/Question/Answer/Search/
/Question/Answer/List/Group/Question
The other approach is to separate the controller with respect to Admin and Visitor functionality. Put all the admin logic like create/update/ in one controller and split the remaining logic into other controllers. There may be +/- argument to this. But's that's an individuated opinion.
Note:
Category can exist independent of the question
A question is associated with one or more category
An answer belongs to a question and cannot exist independently.
*** The example came out of top of my head and may not be the best practice but may just give some points to ponder. :). Feel free to apply your ideas.