I've been told that the helpers are just for functions that are needed by the views.
Where should I put in functions that are used commonly by models? What about controllers?
What's the convention to place commonly used functions that will be used in:
1) models
2) views
3) controllers
Problem: Creating a module in lib to hold the functions and including the module in a class would create a boat-load of instance methods for the class.
Problem: What about functions that are common and needed in all three?
Problem: Creating a module in lib to hold the functions and including the module in a class would create a boat-load of instance methods for the class.
First organize, then optimize
Problem: What about functions that are common and needed in all three? Do you really have methods that are needed in all the three and not exist yet ? If yes, may be you can give an exemple
I think the question should be where to put logic in general. You should first think what your method does before to think about where to put it. But whatever you create, when it's getting big and or valuable, you should really think about exporting it as a gem/plugin.
Inner navigation logic (what to display and where to go after an action) : Controllers
Data logic (How to manipulate, process data) : Models
Data presentation logic (How to display data) : Helper, Partial and Decorators
Layout language logic (layout language help) : Helper
Layout logic (should i display this menu ?) : ?
I think if you are not in this configuration you are creating kind of backend system. So it should go in lib, then in a gem later.
This organization is just an exemple. The most important thing is to organize your code and split different logic layers and don't hesitate to refactor/export code to make it generic after adding new features...
application_controller.rb
application_helper.rb
ActiveRecord::Base
to include common methods OR write a module with common model methods and include it in the models that need it OR do it in OOP way by subclassing ActiveRecord::Base with your abstract class, then inheriting all your models from this class. To use common methods in both Model and Controller, do one of the following:
require
it when you need to use its methods.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With