Class versus instance method - how?

I have a file call authentication.rb in rails root/lib directory which
has the following method defined amongst others:

module Authentication

def authorize_admin?
logged_in? && has_role?(‘admin’)
end

end

This module is included into application.rb as follows:

class ApplicationController < ActionController::Base

Ryan’s stripped down Authentication module from

Restful_Authentication
include Authentication

Tim H.'s RoleRequirement module from his plugin

include RoleRequirementSystem

end

I am also using Ryan Heath’s excellent navigation_helper plugin which
has a “current_tab” class method. You set the current_tab at the
controller level as follows:

class UsersController < ApplicationController

before_filter :login_required
current_tab(self.authorize_admin? ? :clients : :basic_data)

end

I was trying the conditional form for setting the current tab based on
whether the logged_in? user was an “admin” or a plain “user”. That did
not work. I got the following exception:

NoMethodError in UsersController#edit

undefined method `authorize_admin?’ for UsersController:Class in line 4
which is the current_tab method above. I do not fully understand why.
The current_tab is a class method why can’t it take an argument
containing an instance method?

I got around the above issue by doing this:

class UsersController < ApplicationController

before_filter :login_required
current_tab :clients

def edit
self.class.current_tab :basic_data unless authorize_admin?
end

end

So at the method level, I override the current_tab based on desired
condition using the authorize_admin? method. This works.

So my question in the above context is: how do you call an instance
method as an argument to the class method?

Bharat

end

On Apr 2, 10:33 pm, Bharat R. [email protected]
wrote:

end
end

If you don’t understand why this doesn’t work, then you probably don’t
understand when this code runs. This code runs when the class is
loaded from users_controller.rb. In particular self is the class
UsersController, so you can’t call authorize_admin? on it, since that
is an instance method and you don’t have an instance of
UsersController.

In production this code only executes once (when the app starts), ie
there is no request or current user - clearly you can’t be doing user
specific things at this point.

Fred

Thank you Fred. You are right, I did not understand it. Now I do. I
do get a bit “mixed-up” with all these “mix-ins” sometimes :slight_smile: