Skip to content

Instantly share code, notes, and snippets.

Created Jun 18, 2012
What would you like to do?
Rails 3 YAML Configuration Loader
# config/config.yml
defaults: &defaults
key: value
regexp: !ruby/regexp /pattern/
<<: *defaults
<<: *defaults
<<: *defaults
key: override value
# Access via symbol or string
puts APP_CONFIG[:key]
puts APP_CONFIG['key']
# Symbolize all keys when using to config other Rails components
ActionMailer::Base.smtp_settings = APP_CONFIG[:mailer][:smtp_settings].symbolize_keys
ActionMailer::Base.default APP_CONFIG[:mailer][:defaults].symbolize_keys
# config/initializers/load_config.rb
config_file = "#{Rails.root}/config/config.yml"
APP_CONFIG = YAML.load_file(config_file)[Rails.env].with_indifferent_access if File.exists?(config_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment