Adding variables through modules

Hi --

Hi!

I'm trying to enhance a set of controllers which can set the level of security. [code] 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 [/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