Adding variables through modules

Hi!

I’m trying to enhance a set of controllers which can set the level of
security.

Class MenuControllder < ApplicationController
  include BasicTest
end

module BasicTest # in lib/basic_test.rb
  @test = nil
  def self.included controller
    controller.before_filter(:basic_filter)
    @test = controller.name
  end

  def basic_filter
     raise @test
  end
end

This doesn’t work at all. @test is nil by the time it calls
basic_filter.
If I use @@test then @@test results in the last controller that was
built by the server.

I need a new instance of BasicTest for each controller, and I need it to
not be accessed by other controllers that are inheriting BasicTest.

I might not be understanding Modules correctly, but I thought that this
is the types of things they did since multi inheritance is not
supported!

Any guidance on where to look to to accomplish this sort of thing would
be appreciated!

module BasicTest # in lib/basic_test.rb
@test = nil
def self.included controller
controller.before_filter(:basic_filter)
@test = controller.name
end

def basic_filter
raise @test
end
end
[/code]

This doesn’t work at all. @test is nil by the time it calls
basic_filter.

… what happens if you put @test = controller.name, before the
controller.before_filter(:basic_filter) call in the self.included
controller ?
the @test = nil assignment in the begining of the module kinda ruins the
assignment afterwords, no?

… what happens if you put @test = controller.name, before the
controller.before_filter(:basic_filter) call in the self.included
controller ?
the @test = nil assignment in the begining of the module kinda ruins the
assignment afterwords, no?

if you move the code around like you suggested, @test is still nil in
the basic_filter both with and without the @test = nil at the beginning
of the module.

But from what you said the @ be available in all the methods of a class
and the @@ act as a singleton variable? That’s what I always thought
but have been foiled here.

Thanks for the suggestion any more would be greatly appreciated!

Matt

Hi –

On Sun, 24 Jun 2007, Matt Sir wrote:

module BasicTest # in lib/basic_test.rb
[/code]

This doesn’t work at all. @test is nil by the time it calls
basic_filter.

Actually you’ve got two totally unrelated @test variables here: the
one belonging to the module object BasicTest, and one in an instance
method which will belong to whatever object calls the method
(basic_filter). Instance variables always belong to “self”, which can
be an object of any class.

If I use @@test then @@test results in the last controller that was
built by the server.

I need a new instance of BasicTest for each controller, and I need it to
not be accessed by other controllers that are inheriting BasicTest.

Modules don’t have instances, and they don’t get inherited. So I’m
not totally sure what you’re aiming for here. But maybe
instance_variable_set would help? I’m thinking of something like
(untested):

def self.included(controller)
controller.before_filter do |c| # c is an instance of controller
c.instance_variable_set("@test", controller.name)
end
end

though since you can always get from a controller instance to its
class’s name easily (self.class.name), you don’t really need to store
this information.

Can you explain a little more what you want to happen?

David

def self.included(controller)
controller.before_filter do |c| # c is an instance of controller
c.instance_variable_set("@test", controller.name)
end
end

though since you can always get from a controller instance to its
class’s name easily (self.class.name), you don’t really need to store
this information.

Can you explain a little more what you want to happen?

David

Hi!

So I gave that a try and didn’t quite work but came closer!

In the meantime… We have a database guy and a programmer, that’s me!
The database guy wants it to be ‘normalized’, HA right! Well to do that
we have the same information for controllers in different tables which
mean different models. So we have MenuWidget and ForumWidget (there are
several types of widgets)

But they all do the same thing in the before filter so I need the class
name so i can dynamically call the appropriate tables. The @test was a
simpler test i was using what I really have is something like.

class BasicTest
self.included
@problem_here = Security.new(controller.name)
end

def basic_filter
if @problem_here.verify?(params[:action])
blablbalba
else
redirect…
end
end

class Security
@@name = nil
def initialize controller
@@name = controller.name
end
def verify?(action)
do_something_snazy_with_the_name
end
end
end

so the Security class works dandy, it is getting the instance of the
security class to stick around. I got it to work fine by storing the
variable inside the controller itself, but I really need to get it into
the module eventually.

the problem is I also need to use the variable in the controller as well

class MenuController < …
include BasicTest

@problem_here.set_level(:index,4)
def index
end
end

Anyway thank you very much I will continue to fool around with
instance_variable_set! Hope the new information helped and did not
confuse!

Matt

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