Best place for storing configuration information?


#1

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


#2

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’]


#3

Sean H. 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.


#4

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