RobL Vs

Background

Skip all filters

I found some code recently for a client that skips (or should at least) skip all filters in a controller. I haven’t seen this done before and a quick google clarified someone else has put it to use somewhere, as I wasn’t sure if this should ever have worked.

1class FooController < ApplicationController
2  skip_filter filter_chain
3end

http://markmcb.com/2008/10/14/skip-all-rails-filters/

It was actually implemented with a little more functionality to set options for all the filters that are being skipped. e.g only for particular actions in a controller.

1class ApplicationController
2
3  def self.skip_all_filters(options = {})
4    skip_filter filter_chain, options
5  end
6
7end

However, this doesn’t appear to work in post Rails 2.1 applications. So I found myself using the following instead.

 1class ApplicationController
 2 
 3  def self.skip_all_filters(options = {})
 4    filter_chain.map(&:method).each do |filter|
 5      skip_filter filter, options
 6    end
 7  end
 8
 9end
1class FooController < ApplicationController
2  
3  before_filter :do_something
4  skip_all_filters :only => [:blah, :blah2]
5
6end