There’s a gem, which appends a before_filter to a Rails app:
class Railtie < Rails::Railtie
initializer "..." do
ActiveSupport.on_load(:action_controller) do
ActionController::Base.send(:include, Filter)
...
module Filter
extend ActiveSupport::Concern
included do
append_before_filter :set_locale
end
def set_locale
....
And here’s some controller in the app:
class DesktopsController < ApplicationController
before_filter :set_language_in_session
Now the problem with this is that the before_filter from the gem is being put in the filter chain before the before_filter from the DesktopsController:
DesktopsController._process_action_callbacks.select { |c| c.kind == :before }.collect { |filter| filter.filter }
=> [
[0] :set_locale,
[1] :set_language_in_session
]
How can I make the before_filter from the gem (set_locale) be put after all other filters? The secret probably lies in this line:
ActiveSupport.on_load(:action_controller) do
But I’ve tried different libraries without any luck…
Btw. Here’s the full code of the gem. Ruby 1.9.2, Rails 3.0.5.
Adapting the gem will never work. The gem is initialised as one of the first things in the rails process, so before any actual controller is ever launched. So this means, that whatever you do, most likely the filter from the gem will still remain the first gem.
So the only solutions I see are:
prepend_before_filterin your own controllers for those actions that need to come before theset_locale.skip_before_filterand explicitbefore_filter.In a rails 2 application, we devised a hack to make sure that some filter was run as last filter, by overriding the
perform_action_without_filters(see Is there a way to force a before_filter to always execute last?) but we removed that when upgrading to rails 3.