How to name Rails Concerns?

Rails concerns are a handy technique to organize code / logic within your classes. But how do we name these Rails Concerns?

We usually follow these two conventions:

able suffix

We suffix Concerns with able when the object we are including …


This content originally appeared on DEV Community and was authored by Manuel Ortega

Rails concerns are a handy technique to organize code / logic within your classes. But how do we name these Rails Concerns?

We usually follow these two conventions:

able suffix

We suffix Concerns with able when the object we are including the concern to is able to act or behave as the concern describes.

module Avatarable
  extend ActiveSupport::Concern
  ...

  included do
    ...
  end
  ...

end

The Concern above tells us that the class we include this concern to is able to act / behave as we would expect from an avatar.

We also follow this convention when we can execute the action that the concern describes to the object we are including the concern to.

module Subscribable
  extend ActiveSupport::Concern
  ...
end

In this case we are making clear we are able to subscribe to objects from the class we include this Subscribable Concern to.

has prefix

We prefix our Concerns with has when the logic we are trying to group within our Concern is not describing a behavior but a has relation between the class where we will include the Concern to and a second entity. In the following scenario we may have a User entity that has mentions within a blogging platform. At some point we may come up with a Concern that groups all the logic tied to a user having mentions.

module HasMentions
  extend ActiveSupport::Concern

  included do
    ...
  end
end

In case none of the conventions described above makes sense within a specific scenario we don't follow them and goes with whatever better describes what the concern groups together.

How do you name your Rails Concerns? What conventions do you follow?


This content originally appeared on DEV Community and was authored by Manuel Ortega


Print Share Comment Cite Upload Translate Updates
APA

Manuel Ortega | Sciencx (2021-04-15T18:28:57+00:00) How to name Rails Concerns?. Retrieved from https://www.scien.cx/2021/04/15/how-to-name-rails-concerns/

MLA
" » How to name Rails Concerns?." Manuel Ortega | Sciencx - Thursday April 15, 2021, https://www.scien.cx/2021/04/15/how-to-name-rails-concerns/
HARVARD
Manuel Ortega | Sciencx Thursday April 15, 2021 » How to name Rails Concerns?., viewed ,<https://www.scien.cx/2021/04/15/how-to-name-rails-concerns/>
VANCOUVER
Manuel Ortega | Sciencx - » How to name Rails Concerns?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/15/how-to-name-rails-concerns/
CHICAGO
" » How to name Rails Concerns?." Manuel Ortega | Sciencx - Accessed . https://www.scien.cx/2021/04/15/how-to-name-rails-concerns/
IEEE
" » How to name Rails Concerns?." Manuel Ortega | Sciencx [Online]. Available: https://www.scien.cx/2021/04/15/how-to-name-rails-concerns/. [Accessed: ]
rf:citation
» How to name Rails Concerns? | Manuel Ortega | Sciencx | https://www.scien.cx/2021/04/15/how-to-name-rails-concerns/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.