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 money motivation

Opscomplete powered by makandra brand

Save money by migrating from AWS to our fully managed hosting in Germany.

  • Trusted by over 100 customers
  • Ready to use with Ruby, Node.js, PHP
  • Proactive management by operations experts
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)