Decrufting your Rails filters

Introducing FilterDecrufter

Suppose you have a Rails controller with a before_filter:

1
before_filter :load_widget, :only => [:show, :frobnicate]

If you’ve deleted the frobnicate action, Rails won’t complain; that symbol will just sit around in your controller adding to the file noise. Furthermore, if you removed both methods specified in the only block then the load_widget method would be unused and gathering dust as well. FilterDecrufter to the rescue!

How to use it

To use it, add FilterDecrufter to your Gemfile in the development group:

1
gem 'filter_decrufter'

and then run the task:

1
2
3
$ bundle exec rake filter_decrufter:check
Api::V1::WidgetsController before_filter 'find_widget' has an :only constraint with a non-existent action name 'show'
EmployeesController after_filter 'set_name' has an :only constraint with a non-existent action name 'frobnicate'

How does it work?

FilterDecrufter hooks before_filter, after_filter, and around_filter by defining a singleton method on ApplicationController for each filter type. As it proxies those method invocations, it also records the only and except arguments for future analysis.

Once the metaprogramming is done, FilterDecrufter loads up all the controllers (with Rails.application.eager_load!) which triggers calls to the filter method proxies. The proxies gather the data, the reporting mechanism compares the filter arguments to the output of AbstractController::UrlFor::ClassMethods.action_methods, and the old action names are printed out so you can get them squared away.

Happy decrufting!

Comments