Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

order of before_filters in Rails controller and concerns

I have a Rails concern defined as follows:

module MyConcern
  extend ActiveSupport::Concern

  included do
    before_filter :filter_inside_concern
  end

  def filter_inside_concern
    # ...
  end
end

and I have a before_filter also on the controller layer:

class MyController < ApplicationController
  before_filter :filter_inside_controller
end

If I include MyConcern inside MyController, does the order in which the before filters are called dependent on how the code is arranged? For example, if we have

class MyController < ApplicationController
  include MyConcern

  before_filter :filter_inside_controller
end

Does filter_inside_concern gets called before filter_inside_controller (or vice versa)?

Thank you!

like image 925
Adler Santos Avatar asked Oct 28 '25 14:10

Adler Santos


1 Answers

I have recreated your situation and find out sequence of execution depends on sequence in which you write both filters.

if you write

  include MyConcern
  before_filter :filter_inside_controller

concern filter will execute first

or if you write filters in this sequence

  before_filter :filter_inside_controller
  include MyConcern

controller filter will execute first

like image 51
Asad Ali Avatar answered Oct 30 '25 07:10

Asad Ali