Aaron Smith 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.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---