# ActionController filter chain question

**URL:** <https://www.ruby-forum.com/t/actioncontroller-filter-chain-question/108249>\
**Category:** Rails\
**Created:** [July 1, 2007, 1:01am UTC](https://www.ruby-forum.com/t/actioncontroller-filter-chain-question/108249 "2007-07-01T01:01:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [July 1, 2007, 1:01am UTC](https://www.ruby-forum.com/t/actioncontroller-filter-chain-question/108249/1 "2007-07-01T01:01:29Z")

</div>

Hey all, wondering about ActionController filter chains. EX:

require ‘rubygems’  
require ‘action\_controller’

class Tray \< ActionController::Base  
before\_filter :aud

def aud  
puts “AUD”  
end

def run  
puts “RUN”  
end  
end

f = Tray.new  
f.run

Should actioncontroller alone implement failter chains? This example  
only spits out “RUN” but i’m trying to find a way to execute the full  
filter\_chain by itself, not depending on Rails at all…

Any ideas?  
Thanks

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [July 1, 2007, 1:59am UTC](https://www.ruby-forum.com/t/actioncontroller-filter-chain-question/108249/2 "2007-07-01T01:59:39Z")

</div>

Aaron S. wrote:

> Hey all, wondering about ActionController filter chains. EX:
> 
> require ‘rubygems’  
> require ‘action\_controller’
> 
> class Tray \< ActionController::Base  
> before\_filter :aud
> 
> def aud  
> puts “AUD”  
> end
> 
> def run  
> puts “RUN”  
> end  
> end
> 
> f = Tray.new  
> f.run
> 
> Should actioncontroller alone implement failter chains? This example  
> only spits out “RUN” but i’m trying to find a way to execute the full  
> filter\_chain by itself, not depending on Rails at all…
> 
> Any ideas?  
> Thanks

Got it::

require ‘rubygems’  
require ‘action\_controller’

class ActionTest \< ActionController::Base

before\_filter :login  
before\_filter { |controller| puts “BEFORE PROC FILTER” }  
prepend\_before\_filter :aud  
after\_filter { |controller| puts “PROC AFTER FILTER” }  
append\_after\_filter :aft  
after\_filter :run

around\_filter do |controller, action|  
logger.debug “before #{controller.action\_name}”  
action.call  
logger.debug “after #{controller.action\_name}”  
end

def login  
puts “LOGIN”  
end

def aud  
puts “AUD”  
end

def run  
puts “RUN”  
end

def aft  
puts “AFT”  
end

def target  
puts “TARGET BIOTCH”  
end  
end

class ActionController::Base

def run\_target\_with\_filters(method,controller)  
afters = []  
befores = []

#populate the before and after arrays  
controller.filter\_chain.each do |fl|  
if(fl.inspect.to\_s.match(/BeforeFilter/))  
befores \<\< fl  
elsif(fl.class.to\_s.match(/AfterFilter/))  
afters.unshift(fl)  
end  
end

befores.each do |fl|  
begin  
if(fl.inspect.to\_s.match(/Proc/) != nil)  
fl.filter.call(controller)  
#elsif(fl.inspect.to\_s.match(/ProcWithCallFilter/))  
# fl.filter.call(controller, ?action?)  
else  
controller.send(fl.filter.to\_s)  
end  
rescue LocalJumpError =\> e  
next  
end  
end

#Call target Method  
controller.send(method)

#Call afters  
afters.each do |fl|  
begin  
if(fl.inspect.to\_s.match(/Proc/) != nil)  
fl.filter.call(controller)  
#elsif(fl.inspect.to\_s.match(/ProcWithCallFilter/))  
# fl.filter.call(controller,?action?)  
else  
controller.send(fl.filter.to\_s)  
end  
rescue LocalJumpError =\> e  
next  
end  
end  
end  
end

f = ActionTest.new  
f.run\_target\_with\_filters(:target, f)

One thing it doesn’t handle is around\_filters… looking into that still.
