Skip to content

Instantly share code, notes, and snippets.

@ziadoz
Created June 18, 2012 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ziadoz/2950495 to your computer and use it in GitHub Desktop.
Save ziadoz/2950495 to your computer and use it in GitHub Desktop.
Rails 3 YAML Configuration Loader
# config/config.yml
defaults: &defaults
key: value
regexp: !ruby/regexp /pattern/
development:
<<: *defaults
test:
<<: *defaults
production:
<<: *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