Forum: Ruby on Rails Best place for storing configuration information?

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.
2b2c2a705ed12f8fb327c7b4c56456c6?d=identicon&s=25 Sean Hussey (seanhussey)
on 2006-05-05 23:32
(Received via mailing list)
Hi all,

Where do you store your non-Rails-specific configuration info?  For
instance, I want to be able to login to LDAP as a particular admin
user.  I don't want the u/p combo in any of the models...does it go in
environment.rb?  database.yml?  Set a constant in a lib file?

When you've got it in a good location, how do you access it?

Thanks!

Sean
2f9a03aa0fcfe945229cb6126eda2cb2?d=identicon&s=25 Philip Hallstrom (Guest)
on 2006-05-05 23:44
(Received via mailing list)
> Where do you store your non-Rails-specific configuration info?  For
> instance, I want to be able to login to LDAP as a particular admin
> user.  I don't want the u/p combo in any of the models...does it go in
> environment.rb?  database.yml?  Set a constant in a lib file?
>
> When you've got it in a good location, how do you access it?

This is what I've done...

% cat config/site.yml
defaults: &defaults
   memcache_servers    : ['127.0.0.1:11211', '127.0.0.1:11212']
   use_static_banners  : true

development:
   <<: *defaults
   use_static_banners  : true


test:
   use_static_banners  : false
   <<: *defaults

production:
   use_static_banners  : false
   <<: *defaults


Then, in environment.rb do:

site_yaml = YAML::load_file("#{RAILS_ROOT}/config/site.yml")
...
... = site_yaml[RAILS_ENV]['use_static_banners']
... = site_yaml[RAILS_ENV]['memcache_servers']
...
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-05-05 23:52
Sean Hussey wrote:
> Hi all,
>
> Where do you store your non-Rails-specific configuration info?  For
> instance, I want to be able to login to LDAP as a particular admin
> user.  I don't want the u/p combo in any of the models...does it go in
> environment.rb?  database.yml?  Set a constant in a lib file?
>
> When you've got it in a good location, how do you access it?
>
> Thanks!
>
> Sean

I used the Settings plugin
http://wiki.rubyonrails.com/rails/pages/SettingsPlugin

works very well for tracking global variables, and you can easily set
different values for different environments, since they are in different
databases.
0091f92762685860109bbcb02edfdf27?d=identicon&s=25 Alain Ravet (Guest)
on 2006-05-06 17:49
(Received via mailing list)
Sean,


I added this code at the bottom of my environment.rb file:

class FooConfig
     @@property = {
         :ldap_authenticator => {
                   :port         => 389,
                   :host         => 'yahoo.google.be',
                   :sac_dn       => '???',
                   :sac_passw    => '???',
                   :base_dn      => '???'
                   }
     }
     cattr_accessor :property
     def self.method_missing(method, *arguments)
         FooConfig.property[method.to_sym]
     end
end


To read the values, you'd just write

         port = FooConfig.ldap_authenticator[:port]
         host = FooConfig.ldap_authenticator[:host]


though I couldn't resist Rubyfying it a little further:

#add this in the heavy LDAP settings' user code:

     # implements: host(), port(), sac_dn(), sac_passw() and base_dn()
     def self.method_missing(method, *arguments)
         FooConfig.ldap_authenticator[method.to_sym]
     end

, so you can just type
     port = port()
     host = host()


Alain
This topic is locked and can not be replied to.