Moving config mess from environment files to config.yml

If your environments/*.rb look like a repetitive mess, its time to get a config.yml!

  • overview of your configuration
  • dry code
  • (optional) not check in all the passwords/keys of your app (only check in config.example.yml), great for open-source apps
  • Can be loaded without loading the environment (e.g. small rake tasks)
# lib/cfg.rb
CFG = YAML.load(ERB.new(
  File.read("config/config.yml")
).result)[Rails.env].with_indifferent_access

# config/application.rb
require "cfg"

# config/config.yml
common: &common
  api:
    airbrake_key: xxx
    google_analytics_key: yyy
  admin_email: admin@example.com

test:
  <<: *common
  host: 'localhost'

development:
  <<: *common
  host: 'localhost'

production:
  <<: *common
  host: 'fuu.bar'

# config/application.rb
config.action_mailer.default_url_options = {:host => CFG[:host]}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s