I started working through the tutorial at railstutorial.org
to get exposure to the framework. My controllers aren’t monstrous yet, but I can
see that Single Responsibility Principal(SRP) isn’t being applied throughout the tutorial, as it extends beyond the scope of the tutorial.
I’ve got this relatively simple controller here. I can already see different concerns (e.g. authentication and authorization) leaking into this controller, which contains too many actions to begin with. This assigns too many actions to one controller.
I stumbled upon rails focused controllers which solves one of these issues
and looks quite interesting.
Is this a common solution? Or are better solutions available?
In the .net world, we tend to use aspect oriented programming(AOP) to
achieve a cleaner Separation of Concerns (SoC). However, recently a few guys have written a new front controller framework called Fubu Behaviours. It captures the idea of a request pipeline nicely. Something that makes more and more sense to me.
In order to handle a request we tend to go through a few steps before (and sometimes
after) the action is executed. In some cases, conditionally ending the request. It seems natural to use something like behaviors, a pipeline or a russian dolls pattern.
So that each link in the chain is responsible for either continuation or cessation. Inheritance doesn’t seem to be the best solution.
Is there anything similar to that in rails? Would it make sense in rails?
Recommended readings will be just as well welcome!
I agree with you that having those authorization functions like
is_adminandcorrect_userare a bit of a code smell. I would remove them and handle it a bit better with a gem I often use called CanCan.It allows you to move all authorization rules out of your controllers into a manifest (i.e. the Ability model), only requiring your controllers to initiate the authorization check through
authorize_resourcecall in your controller. Then you can handle simple redirects in yourApplicationController:Other than that, I would move all the
@user = User.find(params[:id])calls into a before_filter, and clean up your indenting and your action ordering (should beindex,new,create,show,edit,update,destroy) and I think your controller would be nice and skinny.