Before filter in applicatino_controller

If I do something like this:

class ApplicationController < ActionController::Base
protect_from_forgery
before_filter RubyCAS::Filter
before_filter :fetch_operator
include SessionsHelper

private

def fetch_operator
@current_operator ||= session[:cas_user] &&
Operator.find_by_uid(session[:cas_user])
rescue ActiveRecord::RecordNotFound
redirect_to("/404.html")
end

end

The fetch_operator action is always executed, before every action in
every controller?
I’d want to fetch the operator only at start and not before every
action but I need to have it available on every view and controller.

Hi,

The fetch_operator action is always executed, before every action in every
controller?
Yes, all kind of filters implemented inside ApplicationController
class are always executed in every controller (whose parent is
ApplicationController) unless you change the behavior explicitly.

class ApplicationController < ActionController::Base

before_filter :fetch_operator

end

class anotherController < ApplicationController
before_filter :fetch_operator, :only => [:login]
end

For further information, see:
http://rails.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html

Exequiel.

On 24 February 2011 08:21, Mauro [email protected] wrote:

If I do something like this:

[snip controller code]

The fetch_operator action is always executed, before every action in
every controller?
I’d want to fetch the operator only at start and not before every
action but I need to have it available on every view and controller.

Are you then using the @current_operator variable in places in your
controller? (like “if @current_operator.name == ‘fred’…”?)

If so, instead use a helper method to return the operator (and
populate it if necessary), rather than the filter method:

class ApplicationController < ActionController::Base
protect_from_forgery
before_filter RubyCAS::Filter
include SessionsHelper
helper_method :current_operator

def current_operator
@current_operator ||= fetch_operator
end

private
def fetch_operator
session[:cas_user] &&
Operator.find_by_uid(session[:cas_user])
rescue ActiveRecord::RecordNotFound
redirect_to(“/404.html”)
end

end

But if I want fetch the operator only once at application startup?

I guess you can set up a session variable to make sure to call only
once.

if !session[:operator_called]

session[:operator_called] = true
end

On 24 February 2011 17:48, exequiel [email protected] wrote:

But if I want fetch the operator only once at application startup?

I guess you can set up a session variable to make sure to call only
once.

if !session[:operator_called]

session[:operator_called] = true
end

In a method of application controller?

On 24 February 2011 13:37, Michael P. [email protected] wrote:

Are you then using the @current_operator variable in places in your
helper_method :current_operator
redirect_to(“/404.html”)
end

end

But if I want fetch the operator only once at application startup?

On Thu, Feb 24, 2011 at 4:29 PM, Mauro [email protected] wrote:

In a method of application controller?

It’s just an example, you can add that piece of code in any method, for
example:

class ApplicationController < ActionController::Base

before_filter :call_operator_once

def call_operator_once
if !session[:operator_called]
fetch_operator()
!session[:operator_called] = true
end
end
end

Typo error, it should be:

def call_operator_once
if !session[:operator_called]
fetch_operator()
session[:operator_called] = true
end
end

Right, it will called before each action of each controller in your
application, but only one time the fetch_operator method will be
executed. I thought you want to make sure that fetch_operator method
is executed at least one time independent of controller. Now, if you
want to use a filter method and avoid to call it in other controllers
you can use “skip_before_filter :my_method” in each controller. And if
you definitely don’t use this method in other controllers, I guess you
don’t need define a filter method, just call it as usually in each
action that you need it, because any filter that you defined inside
the ActionController class will be inherited by other controllers.

On 24 February 2011 19:40, Exequiel F. [email protected] wrote:



defcall_operator_once
if!session[:operator_called]
fetch_operator()
!session[:operator_called]= true
end
end
end

Yes but it executes the call_operator before each action of each
controller in my application.