5. Concern : Dependency
Methods of a concern are not invoked until the concern is included in something that is not a
concern.
6. Concerns in Models : DRYing
Example : Consider a Article model, a Event model and a Comment Model. An article or an event
has_many comments. A comment belongs_to either article or event.
9. Concerns in Models : Modularization
Example : Consider a Event model. A event has many attenders and comments. Typically,
the event model might look like this:
11. Concerns In Controllers :
Modularization and DRYing
Concerns can be used in Controllers in a similar way as in Models.
Example:
• An Users controller, an Articles Controller, an Events Controller.
• A User has to login to view all the articles and events .
• Typically: Both Articles and Events controller will have some logic to check
that the user has logged in etc.
• Concerns : Shift all the common logic related to user in Article and Event
controller into a concern module called Authorizable. Then Include this
Authorizable module in Articles and Events Controller.
12. Concerns: Placement and Logic
Placement
• If only one concern per model/controller or One concern is being included by
multiple models/controllers: Place them directly under the concerns folder.
app/controller/concern/authorizable.rb
app/model/concern/commentable.rb
• If many concerns per model/controller: Create a folder inside the concerns
with the name of the model/controller and place them inside it.
app/model/event/attendable.rb
Logic:
Concerns should be domain based rather than technical based. For Example :
For a model, domain based concerns will be like ‘Commentable’, ‘Taggable’,
While technical based concerns will be ‘FinderMethod’, ‘ValidationMethod’
13. Concerns: Pros and Cons
Pros
• Easy to Use for refactoring.
• No external dependency
Cons
• Modules Problem
• Debugging is tougher
• No actual reduction in code. Its just better organized.
16. Problems With this Approach
• Multiple Instance Variables in Controllers :
1) Violation Of Sandi Metz Rule : One Controller action may pass only one
instance variable to its corresponding view.
2) Seperation of Concern in Controllers
• Logic creeping In the view files
Solutions : Include Logic as Models Methods
Include Logic in Helpers
18. Presenters : A Design Pattern
Create a folder in your app directory with name ‘presenters’. Create a .rb file
specific to the model we are working with (user in our case)
20. Presenters Pros and Cons
Pros
• Introduces Object-Oriented-ness in the View Layer
• Clean Controllers and Views
• Easy Testing.
Cons
• Generating this additional layer is a overhead.