Mixin variables

I’ve created an access control module that I’m including in several
different controllers. Everything works fine, but I want to allow each
controller to override the default login page with something like this:

set_login_pages :secure => ‘my_login’

In my module, I’ve set up the set_login_pages method like this:

def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods
def set_login_pages(*login_page_list)
@login_pages = login_page_list
end
end

But then when I try to access @login pages in the require_user method,
it’s blank.

What am I doing wrong?

Hi –

On Wed, 19 Apr 2006, Will Emigh wrote:

end
What am I doing wrong?
The @login_pages instance variable you’re creating belongs to the
class object that’s getting extended. That has no effect on the
@login_pages belonging to any instance of that class.

Here’s a little demo that shows you the difference:

module M
def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods
def m
puts “I am #{self}”
@var = 1
end
end
end

class C
include M
m
puts “I too am #{self}”
puts “My @var is #{@var}”

def inst
puts “I am an instance of C”
puts “My @var is #{@var}”
end
end

C.new.inst

David


David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” PDF now on sale! Ruby for Rails
Paper version coming in early May!

I tried the same thing with a class variable instead and had the same
problem. Is there a better way to do this?

– Will

unknown wrote:

The @login_pages instance variable you’re creating belongs to the
class object that’s getting extended. That has no effect on the
@login_pages belonging to any instance of that class.

Hi –

On Wed, 19 Apr 2006, Will Emigh wrote:

I tried the same thing with a class variable instead and had the same
problem. Is there a better way to do this?

Can you post a mock-up of how you’d like to use it in application
code?

David

Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” PDF now on sale! Ruby for Rails
Paper version coming in early May!

I want my controller to look like this:

class MyController < ApplicationController
include AccessControl

before_filter :require_login
set_login_pages :index => ‘my_login’

def index
end

def normal
end

end

require_login and set_login pages are defined in AccessControl.
require_login checks to see if they’re logged in and redirects to a
login page if not. When redirecting, it checks @@login_pages to see if
the requested page has an alternate login. However, @@login_pages is
blank when require_login checks it (even if set_login_pages has stored
something in it).

– Will

unknown wrote:

On Wed, 19 Apr 2006, Will Emigh wrote:

I tried the same thing with a class variable instead and had the same
problem. Is there a better way to do this?

Can you post a mock-up of how you’d like to use it in application
code?

David