I’ve just released small plugin rails3_before_render. It adds a hook like before_filter to your controllers that gets executed between when your action is completed and the template is rendered. It can really DRY up loading some data.
Please be carefull using that. Regular before/after/around filters is enough for most of the tasks that you can imagine. If not you always call method directly, that will increase readability of your code. Use before_render in exceptional cases.
Here is good presentation by David Chelimsky about code duplication and DRY concept.
Mostly I’ve needed that plugin for my mugen gem.
There is error check after each api request that populate Mugen.errors variable with messages. Every action of controller after all requests has been sent need to check that variable and populate flash messages with that. Without having plugin I used wrapper function like this:
After 5-th action code duplication seems unreasonable. Regular filters like before_filter and after_filter didn’t fit here. Errors parsing clearly needs to be executed right before rendering.
With that I could remove mygengo_request method along with wrapping each action in controller.
Before writing this plugin I have found before_render plugin by Todd Willey. Sadly it worked for rails 2.x only. Rails 3 brought us completely new architecture of ActionPack and as I have found even ActiveSupport had major changes. Callbacks module is now more efficient that is was before.
New ActiveSupport::Callbacks methods were used, in analogy with ActionDispatch. That allowed me to escape reimplementing callback filter chains.
Comments