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?
Top comments (1)
Very interesting work, thank you