17

There are few things in Rails:

## Concerns
## Decorators
## Presenters 
## Service Objects
## Helpers

Can anybody give me real problem example in what type of situation what should I follow, So I can have better understanding of these features.

Examples will be better to understand the concept

Thanks in advance and thanks for looking

stevec
  • 41,291
  • 27
  • 223
  • 311
user3118220
  • 1,438
  • 12
  • 16
  • I think you'd find everything you need just googling. There are tons of articles about those things, many of them really discreet and thorough. – Almaron Mar 19 '15 at 10:27
  • If you setup a Rails 4 app, you will notice the app/models/concerns and app/controllers/concerns directories. Concerns are modules that can be mixed into your models and controllers to share code between them. Actually it is a pretty simple and powerful concept. It has to do with code reuse. Basically, the idea is to extract common and / or context specific chunks of code in order to clean up the models/controllers and avoid them getting too fat and messy. https://signalvnoise.com/posts/3372-put-chubby-models-on-a-diet-with-concerns – ROR Mar 24 '15 at 05:52
  • There's some great explanations on decorators vs presenters here: https://stackoverflow.com/questions/7860301/ruby-on-rails-patterns-decorator-vs-presenter – stevec Aug 24 '20 at 00:37

2 Answers2

16

Well, as I said in the comment, you'll be better of with simple google searches.

For example, this is a nice article about most of them.

I'll just walk you through the basics.

  1. Concerns are mainly for DRYing up your models and controllers. If you have a very fat controller/model that has lots of functionality in it (violating the SRP), it's much better to break it into several independant concerns and just include them back in. That way you can also share functionality between similar controllers/models. Here's a nice article.

  2. Decorators are used for separating models' business logic with their user appearance. E.g. for storing methods only used in the views and for other displaying. They are also used to extend the logic of an object. Here is a nice thoughbot post.

  3. Presenters are practically the same, but used only for displaying purposes.

  4. Service Objects are mainly used for sophisticated logic that does not necesserally belong in a specific model/controller and/or deals with several models for example.

  5. Helpers are solidly for moving logic out of the view and thus simplifying the view patterns and DRYing up the views. Usually used for simple things ('cause otherwise it's better to use a decorator or a presenter).

Almaron
  • 4,127
  • 6
  • 26
  • 48
2

Concerns

Are used to share functionality between files in one 'type' ('model', 'controller', ...). Thus you have

  /app
    /controllers
       /concerns

    /models
       /concerns

In concerns you put modules which will be included inside classes. It is god practice to put some behaviour code. For example

/app
  /models
    /concerns
       messageable.rb
    project.rb
    ..
    proposal.rb

Project model

class Project < ActiveRecord::Base
  include Messageable
end

Proposal model

class Proposal < ActiveRecord::Base
  include Messageable
end

inside app/models/concerns/messageable.rb

require 'active_record/concern'
module Messageable
  extend ActiveSupport::Concern

  # implement behaviour
  module ClassMethods
     # class methods for the behaviour
  end
end
Nermin
  • 6,118
  • 13
  • 23