diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-07-10 16:40:07 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-07-15 15:35:13 -0300 |
commit | 9629dea4fb15a948ab4394590fdd946bd9dd4f91 (patch) | |
tree | ed2425641a15e167ad78e1b4251323d7f3ccf6cd /railties/lib | |
parent | 101cf688987ec39c05859061677f99b43facc2a2 (diff) | |
download | rails-9629dea4fb15a948ab4394590fdd946bd9dd4f91.tar.gz rails-9629dea4fb15a948ab4394590fdd946bd9dd4f91.tar.bz2 rails-9629dea4fb15a948ab4394590fdd946bd9dd4f91.zip |
Add Rails::Application#config_for
This is a convenience for loading configuration for the current Rails
environment.
Diffstat (limited to 'railties/lib')
-rw-r--r-- | railties/lib/rails/application.rb | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 362713eb75..c5fd08e743 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -187,6 +187,38 @@ module Rails end end + # Convenience for loading config/foo.yml for the current Rails env. + # + # Example: + # + # # config/exception_notification.yml: + # production: + # url: http://127.0.0.1:8080 + # namespace: my_app_production + # development: + # url: http://localhost:3001 + # namespace: my_app_development + # + # # config/production.rb + # MyApp::Application.configure do + # config.middleware.use ExceptionNotifier, config_for(:exception_notification) + # end + def config_for(name) + yaml = Pathname.new("#{paths["config"].existent.first}/#{name}.yml") + + if yaml.exist? + require "yaml" + require "erb" + (YAML.load(ERB.new(yaml.read).result) || {})[Rails.env] || {} + else + raise "Could not load configuration. No such file - #{yaml}" + end + rescue Psych::SyntaxError => e + raise "YAML syntax error occurred while parsing #{yaml}. " \ + "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \ + "Error: #{e.message}" + end + # Stores some of the Rails initial environment parameters which # will be used by middlewares and engines to configure themselves. def env_config |