Alex Rivera | Logout

How to append a before_filter in gem after all other controller before_filters in a Rails3 app?

Asked 2011-09-16T15:54:29.580
9

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.

Edit
Report

1 Answer

0

Try this:

class DesktopsController < ApplicationController
  skip_before_filter :set_locale
  before_filter      :set_language_in_session
  before_filter      :set_locale
end 

Reference

skip_before_filter doc

answered 2011-09-16T16:51:56.950

Your Answer