Most of the Rails libraries use the following pattern:

class Something
  @@config1 = "change_me"
  cattr_accessor :config1
end

Something.config1 = "secret code"

On Sep 7, 10:38 am, javinto <[EMAIL PROTECTED]> wrote:
> Thanks!!
>
> You made me dive into this matter deep this time. I get the picture
> now. That's partially then. As I'm still looking for a way to
> configure things in a way, well like Rails does, e.g. in the
> initializers: ActiveSupport.escape_html_entities_in_json = false
>
> But then, I don't know wether ActiveSupport here is one big
> Singleton?! That might be a way to solve it as this functionality
> would perfectly fit in a Singleton. I will give it a thought....
>
> Jan
>
> On 6 sep, 20:38, Andrew Bloom <[EMAIL PROTECTED]> wrote:
>
> > What I forgot to mention is that if you do:
>
> > module AccountSystem
> >   class << self
> >      attr_accessor  :account_system_type
> >   end
> > end
>
> > You are actually putting the attr_accessor on the class Module, not on
> > the class that AccountSystem will be included in. You need to use the
> > included method, or use the extend method and refactor your module a
> > bit.
>
> > On Sep 6, 1:34 pm, Andrew Bloom <[EMAIL PROTECTED]> wrote:
>
> > > if you are checking AccountSystem.account_system_type in your
> > > controller what is the point of including it in ApplicationController?
> > > If you plan to use it as such, maybe you are better off making it a
> > > Singleton Class.
>
> > > If you actually want account_system_type to be a class accessor on
> > > ApplicationController you must do something like this:
>
> > > Module AccountSystem
> > >   SINGLE = 1
> > >   MULTIPLE = 2
>
> > >   self.included(klass)
> > >     klass.send(:cattr_accessor, :account_system_id)
> > >   end
> > > end
>
> > > class ApplicationController < ActionController::Base
> > >   include AccountSystem
>
> > >   # this is how you would use it
> > >   def random_method
> > >     self.class.account_system_id == AccountSystem::SINGLE
> > >   end
> > > end
>
> > > That should do what you seem to want, but if your goal was something
> > > different let me know and I can try to help.
>
> > > Of note, if you do use the above solution you could do:
>
> > > Module AccountSystem
> > >   ...
>
> > >   def single?
> > >     self.class.account_system_id == SINGLE
> > >   end
>
> > >   def multiple?
> > >     self.class.account_system_id == MULTIPLE
> > >   end
>
> > >   ...
> > > end
>
> > > class ApplicationController < ActionController::Base
> > >   include AccountSystem
>
> > >   # this is how you would use it
> > >   def random_method
> > >     if single?
> > >       puts "single"
> > >     elsif multiple?
> > >       puts "multiple"
> > >     else
> > >       raise "please set the AccountSystem.acoun_type_id"
> > >     end
> > >   end
> > > end
>
> > > On Sep 6, 12:08 pm, javinto <[EMAIL PROTECTED]> wrote:
>
> > > > Hi,
>
> > > > I've added a custom library called lib\AccountSystem like so:
>
> > > > "module AccountSystem
> > > >   SINGLE = 1
> > > >   MULTIPLE = 2
>
> > > >   class << self
> > > >      attr_accessor  :account_system_type
> > > >   end
> > > > end"
>
> > > > Now I wanna configure
> > > > AccountSystem.account_system_type=AccountSystem::SINGLE in one app. I
> > > > used an initializer: config/initializers/account_initialization.rb
> > > > where I put this line in.
>
> > > > I included my AccountSystem in the ApplicationController.
>
> > > > So now I'd like to check within my controllers the value of
> > > > AccountSystem.account_system_type
>
> > > > But there it is empty!
> > > > However if I run "Ruby script\console" and type
> > > > AccountSystem.account_system_type I get the value of 1 as I would
> > > > expect.
>
> > > > How can I achieve the same result within my controllers?
>
> > > > I'm on rails 2.1.0/2.1.1
>
> > > > Thanks
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to