Sat 16 Jul 2011
Adding an application config file to a rails project
Posted by nola under Ruby, Ruby On Rails
[2] Comments
Recently I worked on a project where I needed certain config variables for staging, production, test etc and I wanted to store all these in a yaml file. I could set them in the different environments files but I prefer them all in one file.
Create a file in the config/initializers .. I called it 00_load_app_config.rb so I can be sure it gets loaded first. In it I have only a single line
-
APP_CONFIG = YAML.load_file("#{Rails.root}/config/app_config.yml")[Rails.env]
Then I create a yaml config file at config/app_config.yml
-
base: &common
-
company_name: 'The Awesomest Company Evar!'
-
api_username: 'api_user'
-
api_password: 'bob123'
-
-
development:
-
<<: *common
-
api_hostname: 'http://api-development.myawesomesite.com'
-
twitter_app_key: 'fff'
-
twitter_app_secret: 'fff'
-
-
staging:
-
<<: *common
-
api_hostname: 'http://staging-api.myawesomesite.com'
-
twitter_app_key: 'sss'
-
twitter_app_secret: 'sss'
-
-
test:
-
<<: *common
-
api_hostname: 'http://test-api.myawesomesite.com'
-
twitter_app_key: 'eee'
-
twitter_app_secret: 'eee'
-
-
production:
-
<<: *common
-
api_username: 'api_user_prod'
-
api_password: 'bobprod123'
-
api_hostname: 'http://production-api.myawesomesite.com'
-
twitter_app_key: 'yyy'
-
twitter_app_secret: 'yyy'
The variables in the common section are applied to every environment and overridden like in the case of production, which overrides the api_username and api_password. In each environment they pull the company_name from the common block.
To use a value in your rails app, use the APP_CONFIG array, like
APP_CONFIG['website_name'] or APP_CONFIG['twitter_app_key'] and it will always return the value for the environment currently in Rails.env
Thanks to my friend Rath who showed me this, I changed the name from config to app_config because I liked it to be more specific, but use whatever name you like!

September 20th, 2011 at 10:26 am
I recommend looking at how whiskey_disk handles configuration information across varying deployment targets.
Best,
Rick
September 20th, 2011 at 7:40 pm
Thanks, I think that is a little much for what I needed but I will keep that in mind!