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. 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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> ... 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Jun-25 11:35 UTC
Re: Adding variables through modules
Hi -- On Sun, 24 Jun 2007, Matt Sir wrote:> > 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 -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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? > > > DavidHi! 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---