Our controller here either creates or destroys a Like, but before that we require a feature flag with Flipper.enabled? in both actions. In codebases i

Clear up your Rails controllers with before_action wrappers

submited by
Style Pass
2024-10-21 03:00:06

Our controller here either creates or destroys a Like, but before that we require a feature flag with Flipper.enabled? in both actions.

In codebases it can be tough to know if what you're doing is a good approach, but having a wrapping abstraction helps enable us to we're doing ok.

This is just me writing different versions out and comparing the different naming and seeing which I like better. It's not a science.

Typically, for these before_action wrappers I tend to use requires_, require_, ensures_ or ensure_ as the prefix, but it's good to try alternatives.

I'd like something else than throw :abort to be our response. In case a user accesses a feature they shouldn't I'd lean more towards head :bad_request.

Pro-tip: since this only adds a class method, I prefer to just use extend and omit the self.. It also means we don't have to extend ActiveSupport::Concern.

Leave a Comment