Skip to content

Instantly share code, notes, and snippets.

@coreyti
Created September 26, 2012 21:33
Show Gist options
  • Save coreyti/3790731 to your computer and use it in GitHub Desktop.
Save coreyti/3790731 to your computer and use it in GitHub Desktop.
Load settings for Rails from YAML files
require File.expand_path('../boot', __FILE__) # etc.
module Example
class Application < Rails::Application
# Helper for loading deeply nested environment config.
def load_env(context, key, value)
if value.is_a?(Hash)
options = begin
context.send(key) || ActiveSupport::OrderedOptions.new
rescue
ActiveSupport::OrderedOptions.new
end
context.send("#{key}=", options)
value.each do |k, v|
load_env(options, k, v)
end
else
context.send("#{key}=", value)
end
end
# Load environment settings from yaml files.
["config/environment.yml", "config/environments/#{Rails.env}.yml"].each do |path|
yml = YAML.load_file(Rails.root.join(path))
yml && yml.each do |key, value|
load_env(config, key, value)
end
end
# more config...
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment