# Mixin variables

**URL:** <https://www.ruby-forum.com/t/mixin-variables/58151>\
**Category:** Rails\
**Created:** [April 19, 2006, 12:05am UTC](https://www.ruby-forum.com/t/mixin-variables/58151 "2006-04-19T00:05:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Will\_Emigh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/977dab/32.png) [@Will\_Emigh](https://www.ruby-forum.com/u/Will_Emigh)\
**Post date:** [April 19, 2006, 12:05am UTC](https://www.ruby-forum.com/t/mixin-variables/58151/1 "2006-04-19T00:05:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Will\_Emigh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/977dab/32.png) [@Will\_Emigh](https://www.ruby-forum.com/u/Will_Emigh)\
**Post date:** [April 19, 2006, 1:01am UTC](https://www.ruby-forum.com/t/mixin-variables/58151/2 "2006-04-19T01:01:03Z")

</div>

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 (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” PDF now on sale! [Ruby for Rails](http://www.manning.com/black)  
Paper version coming in early May!

---

<div class="post-metadata">

**Author:** ![Will\_Emigh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/977dab/32.png) [@Will\_Emigh](https://www.ruby-forum.com/u/Will_Emigh)\
**Post date:** [April 19, 2006, 1:32am UTC](https://www.ruby-forum.com/t/mixin-variables/58151/3 "2006-04-19T01:32:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Will\_Emigh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/977dab/32.png) [@Will\_Emigh](https://www.ruby-forum.com/u/Will_Emigh)\
**Post date:** [April 19, 2006, 4:04am UTC](https://www.ruby-forum.com/t/mixin-variables/58151/4 "2006-04-19T04:04:05Z")

</div>

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/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” PDF now on sale! [Ruby for Rails](http://www.manning.com/black)  
Paper version coming in early May!

---

<div class="post-metadata">

**Author:** ![Will\_Emigh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/977dab/32.png) [@Will\_Emigh](https://www.ruby-forum.com/u/Will_Emigh)\
**Post date:** [April 20, 2006, 12:21am UTC](https://www.ruby-forum.com/t/mixin-variables/58151/5 "2006-04-20T00:21:54Z")

</div>

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
