Read more

How to inspect controller filter chains in specs

Arne Hartherz
August 28, 2012Software engineer at makandra GmbH

Sometimes you need to look at the filter chain in specs. You can do it like that on Rails 2:

controller.class.filter_chain.map(&:method)
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

Note that we need to look at the controller's class since before_filter and after_filter stuff happens on the class level.

Also mind that the above code will give you all filters, both those run before and after an action. You can query before? and after? on the filter objects to scope down to only some of them:

controller.class.filter_chain.select(&:before?).map(&:method)

For Rails 3, do it like this (warning, ugly!):

controller._process_action_callbacks.select { |c| c.kind == :before }.map(&:filter)

That all being said, it may make more sense for you to use a Cucumber feature in the first place.

Posted by Arne Hartherz to makandra dev (2012-08-28 13:32)