DEV Community

loading...
Main Street

How to name Rails Concerns?

ortegacmanuel profile image Manuel Ortega Updated on ・2 min read

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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?

Discussion (1)

pic
Editor guide
Collapse
filatovv profile image
Yuri Filatov

Very interesting work, thank you