Plugin : adding filters in ApplicationController

Hi everybody !

I’m trying to extend the class ApplicationController with a plugin to
add filters to follow some executions. I don’t know why, but I observed
a weird behaviour and I don’t know why. Here is my code.

init.rb

require ‘myplugin’

myplugin.rb

ApplicationController.class_eval do
before_filter do
puts “I’m in the before_filter”
end
end

The things is this filter happens just the first time (the first action
after Webrick is loaded). Well, I tried to do the same thing, extending
ActionController::Base, and it works without problems. But I’d like to
extend ApplicationController and not the Base.

Any ideas ?

On Mar 4, 10:30 am, onion wushu [email protected] wrote:

myplugin.rb
extend ApplicationController and not the Base.

In development mode your code gets reloaded between requests - the
reloaded application controller class doesn’t have this filter added
to it.

One way of working around this is for your plugin to add a method to
base (eg add_my_filter) and call that from whichever controllers use
it.
There is also a hook ( config.to_prepare ) that it called before each
request in development mode

Fred