Created
June 18, 2012 20:23
-
-
Save ziadoz/2950495 to your computer and use it in GitHub Desktop.
Rails 3 YAML Configuration Loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/config.yml | |
defaults: &defaults | |
key: value | |
regexp: !ruby/regexp /pattern/ | |
development: | |
<<: *defaults | |
test: | |
<<: *defaults | |
production: | |
<<: *defaults | |
key: override value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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