Forum: Ruby on Rails Mixin variables

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
437ee3e09fe9539cdec6ba44153186cd?d=identicon&s=25 Will Emigh (Guest)
on 2006-04-19 00:05
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?
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-04-19 01:01
(Received via mailing list)
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 (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" PDF now on sale!  http://www.manning.com/black
Paper version coming in early May!
437ee3e09fe9539cdec6ba44153186cd?d=identicon&s=25 Will Emigh (Guest)
on 2006-04-19 01:32
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.
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-04-19 04:04
(Received via mailing list)
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
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" PDF now on sale!  http://www.manning.com/black
Paper version coming in early May!
437ee3e09fe9539cdec6ba44153186cd?d=identicon&s=25 Will Emigh (Guest)
on 2006-04-20 00:21
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
This topic is locked and can not be replied to.