Have you ever wanted to write a controller before or after filter in Ruby on Rails? It sure would be nice to be able to do it by passing a append a parameter array to the filter declaration, as you would do, for example, to the ssl_required declaration when using the Ssl_requirement plugin.
Unfortunately such a convenient syntax is missing in Rails, but there are ways to overcome that. Parameterize_filter is a minimalist plugin that addresses this missing feature. It adds two methods to controllers:
- before_filter_with_parameters
- after_filter_with_parameters
Now you can declare filters that require arguments like so:
class MyController < ApplicationController
before_filter_with_parameters :login_required, Role::ADMIN
after_filter_with_parameters :log_access, LogLevel::INFO
# more code...
Parameterize_filter is based on blog comment posted by Cassiano d'Andrea at http://blog.aclarke.eu/passing-a-parameter-to-a-before-filter. I merely wrapped it up in a plugin. Parameterize_filter is an unpretentious plugin but can become very handy when it comes to simplify your controller code.
Installation
Parameterize_filter is hosted at GoogleCode. To install run:
script/plugin install http://parameterize-filter.googlecode.com/svn/trunk/parameterize_filter
And you are set to go. Hope you find it useful.