“...I've been working since 2008 with Ruby / Ruby on Rails, love a bit of Elixir / Phoenix and learning Rust. I also poke through other people's code and make PRs for OpenSource Ruby projects that sometimes make it. Currently working for InPay...”

Rob Lacey
Senior Software Engineer, UK

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.

class FooController < ApplicationController
  skip_filter filter_chain
end

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.

class ApplicationController

  def self.skip_all_filters(options = {})
    skip_filter filter_chain, options
  end

end

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

class ApplicationController
 
  def self.skip_all_filters(options = {})
    filter_chain.map(&:method).each do |filter|
      skip_filter filter, options
    end
  end

end

class FooController < ApplicationController
  
  before_filter :do_something
  skip_all_filters :only => [:blah, :blah2]

end
GPK of the Day Wrappin' RUTH