RubyFlow The Ruby and Rails community linklog

Stop Worrying and Start Being Concerned: ActiveSupport Concerns

ActiveSupport is a pretty massive component within Rails, and it’s responsible for a ton of different functionality, including language extensions and utilities. The ActiveSupport::Concern wrapper is a particularly interesting utility for encapsulating shared logic, which can help keep your models skinny and DRY. This blog post delves into the basics of when, how, and why to use a concern.

Comments

I agree that Concerns help keep things DRY, but I don’t think they help with fat models. Certainly it slims down the model file, but the model itself still has just as many methods and responsibilities, so fully understanding the model is still rather difficult.

So I think the real benefits of Concerns, as you rightly point out, are 1) shared code getting bundled together instead of repeated; and 2)clarifying responsibilities. The “Skip Authorized” concern example you give is particularly useful there for clarifying - before, we had two methods we are skipping, and being able to simplify “skip_after_action :verify_authorized” and “skip_after_action :verify_policy_scoped” to just “Skip Authorized” helps me to see why we are skipping those actions. It also helps me to see when I might want to skip those actions in future controllers.

All that being said: it seems like concerns are most helpful when bundling up Rails magic, since it’s not so easy to see how you could instead use a PORO. So I can at least see now what the motivation would be for using them. Thanks!

Post a comment

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in