Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 15, 2021 06:28 pm GMT

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 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    ...  endend

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?


Original Link: https://dev.to/mainstreet/how-to-name-rails-concerns-3m86

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To