This project is archived and is in readonly mode.
before_filters overwriting each other
-
Alexey Ilyichev
It seems to me that this code from active_support/callbacks.rb (set_callback method) is what causes that behaviour. I wonder why it was made this way.
filters.each do |filter| chain.delete_if {|c| c.matches?(type, filter) } end -
Oriol Gual
Why don't you just use this:
class UsersController < ApplicationController before_filter :log_filter_call, :only => [:index, :show] # ... end -
Alexey Ilyichev
Originally we wanted to split some of our controllers into mixins, containing sets of actions, because in our app we have many namespaces that share alot of common actions, but not totally same. And some actions that should belong to different mixins might have the same before_filters, so it would be nice if we could declare the same filter in these mixins and not get a conflict like this.
-
2kan
+1 I expected it being called once for both #index and #show too.
-
Alexey Ilyichev
Seems it was intended this way. See commit ef692162.
-
Neeraj Singh
- Importance changed from to Low
This is how callbacks are working right now.
class A < ApplicationController before_filter :ensure_logged_in, :only => [:index] def index end end class B < A before_filter :ensure_logged_in, :except => [:index] # index will be called without calling ensure_logged_in def index end endIf the behavior of the callback is changed to not to remove duplicate callbacks then a subclass always has to worry about unwanted changes propagating to it. That would mean any subclass will have to guard itself by using skip_before_filter then use its own filter.
On the flip side I do think inheritance means subclass probably wants the behavior given to parent class.
This change would potentially break existing apps if they are relying in the feature I illustrated above.