Forum: Rails Engines Filters order in Controllers

Posted by Antonio Tapiador del Dujo (Guest)
on 2008-08-28 14:09
(Received via mailing list)
Hi!
I'm moving my CMSplugin to Rails Engines, as it's a very nice framework 
for
certain features. Thank you!

However, I've a problem with filters in Controllers.

In my plugin I have the following code:

class PostsController < ActionController::Base
  before_filter :get_post, :only => [..]

  [..]
end

and in an application:

class PostsController < ActionController::Base
  before_filter :get_post, :except => [...]
  before_filter :post_has_media, :only => [...]

  [...]
end


The problem is that :get_post should be passed before :post_has_media, 
and
that doesn't work.

If I delete the "before_filter :get_post" statement from the plugin, 
things
work

What is the behaviour of filters in Controllers? Is the filter deleted 
when
redefined in the application or there are changes in the order?

Thank you again.
Posted by James Adam (Guest)
on 2008-08-28 14:13
(Received via mailing list)
The order of filters will be determined by the order that the
controller files are loaded, and this is currently plugins first, then
application. So filters defined in the application will be invoked
after those defined in plugins, by default.

To get around this, I believe you can call
append_before_filter/prepend_before_filter to explicitly control where
your filters are inserted into the filter chain.

HTH,

James

On Thu, Aug 28, 2008 at 1:08 PM, Antonio Tapiador del Dujo
Posted by Antonio Tapiador del Dujo (Guest)
on 2008-08-29 12:28
(Received via mailing list)
El Thursday 28 August 2008 14:13:04 James Adam 
escribió:> The order of filters will be determined by the order that the
> controller files are loaded, and this is currently plugins first, then
> application. So filters defined in the application will be invoked
> after those defined in plugins, by default.

Uhmm, that doesn't explain the behaviour of my app.
:-?

What happens if a filter is defined in the plugin and redefined in the
application?

> To get around this, I believe you can call
> append_before_filter/prepend_before_filter to explicitly control where
> your filters are inserted into the filter chain.

Do you know how can I see the contents of the filter chain?

Thank you.
Posted by James Adam (Guest)
on 2008-08-29 12:50
(Received via mailing list)
On Fri, Aug 29, 2008 at 11:28 AM, Antonio Tapiador del Dujo
<atapiador@dit.upm.es> wrote:
> El Thursday 28 August 2008 14:13:04 James Adam escribió:
>> The order of filters will be determined by the order that the
>> controller files are loaded, and this is currently plugins first, then
>> application. So filters defined in the application will be invoked
>> after those defined in plugins, by default.
>
> Uhmm, that doesn't explain the behaviour of my app.
> :-?
>
> What happens if a filter is defined in the plugin and redefined in the
> application?

Ah - good question. Sorry if I missed it in my earlier response!

I believe the behaviour would be the same as if you added the filter
twice in the same controller - it will get called twice. My advice
would be not define the filter in both your application AND the plugin
controllers - only define it in one.

> Do you know how can I see the contents of the filter chain?

I'm not sure about this off-hand, but the Rails docs should be helpful.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.