Ruby, etc

Learn all the things

He who enjoys doing and enjoys what he has done is happy. - Fortune Cookie

Adding an Application Config File to a Rails Project

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

1
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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

1
APP_CONFIG['website_name'] or APP_CONFIG['twitter_app_key']

It will 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!