Calling a class method from an extended object

Hi all !

I’m on Rails, and I have a general Ruby question. Here’s a part of my
code:

class MockControllerBase; end

module AuthenticatedSystem
def self.included(base)
base.send :include, AuthenticatedSystem::InstanceMethods

base.send :extend, AuthenticatedSystem::ClassMethods

end

module ClassMethods
def required_permissions(options={})
logger.debug
{“AuthenticatedSystem#required_permissions(#{options.inspect})”}
end
end

module InstanceMethods
def authorized?
false
end
end
end

class AuthenticatedSystemAuthorizationTest < Test::Unit::TestCase
def setup
class << @controller = MockControllerBase.new
include AuthenticatedSystem
end
end

def test_wide_open_lets_any_action_go_through
@controller.class.required_permissions true

@params[:action] = "create"
assert @controller.authorized?

end
end

I cannot seem to call #required_permissions after it was included.
How should I access it ?

Both
@controller.class.required_permissions true

and
@controller.required_permissions true

raise a NoMethodError, with a different object.

Should I refactor, or do things differently ?

Thanks !

Hi –

On Sat, 3 Feb 2007, Francois B. wrote:

base.send :extend, AuthenticatedSystem::ClassMethods
def authorized?
end
How should I access it ?

Both
@controller.class.required_permissions true

and
@controller.required_permissions true

raise a NoMethodError, with a different object.

You’ve included the module in the singleton class of @controller,
which is neither @controller nor @controller.class. So
required_permissions is a method of what would be
@controller.singleton_class if this method existed:

class Object
def singleton_class
class << self
self
end
end
end

:slight_smile:

Should I refactor, or do things differently ?

I would do it differently, because I’ve never been a fan of the
InstanceMethods/ClassMethods thing. I prefer just to include or
extend separate modules as needed and where needed. But I know it’s
very popular.

David

Hi !

2007/2/2, [email protected] [email protected]:

I would do it differently, because I’ve never been a fan of the
InstanceMethods/ClassMethods thing. I prefer just to include or
extend separate modules as needed and where needed. But I know it’s
very popular.

Here’s what I did instead:
class AuthenticatedSystemAuthorizationTest < Test::Unit::TestCase
class Controller < MockControllerBase
include AuthenticatedSystem
end

def setup
@controller = Controller.new
end
end

Thank you, David, for your help !

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs