I really like this pattern for providing accessors to the models used in a controller. Alternatively, you usually see a before_filter
to set the model from the params hash only on the actions that need it. IMO this is abusing a before_filter
because the filter chain is for pre-processing the request. This does sort of make sense if you want to redirect away from the page gracefully if the record can't be found, but you can do that by just calling the accessor in a filter to make sure it's there.
This means that views are more like partials with locals, which is nice. It also means the accessor is as easy to test as a named filter. And as usual it pulls the method out of the action to share, which is also nice.
Also, it achieves the same ends as gems that try to OO the view, but without a gem and with terse code. The model can be swapped for a presenter if necessary.
I've been trying this style out recently and really like it. I was using the decent_exposure gem before but don't like the magic of autoloading models by inference.
I've put this in my ApplicationController: