From e58c96766f5264be7fb8a4bdfd9a40ef177fb057 Mon Sep 17 00:00:00 2001 From: Simon Eskildsen Date: Fri, 30 Oct 2015 18:46:15 +0000 Subject: rails/application: allow passing an env to config_for --- railties/CHANGELOG.md | 4 ++++ railties/lib/rails/application.rb | 4 ++-- railties/test/application/configuration_test.rb | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) (limited to 'railties') diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index f43b73cb9d..967f850ea0 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,3 +1,7 @@ +* Allow passing an environment to `config_for`. + + *Simon Eskildsen* + * Allow rake:stats to account for rake tasks in lib/tasks *Kevin Deisz* diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index e81ec62a1d..77efe3248d 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -218,12 +218,12 @@ module Rails # Rails.application.configure do # config.middleware.use ExceptionNotifier, config_for(:exception_notification) # end - def config_for(name) + def config_for(name, env: Rails.env) yaml = Pathname.new("#{paths["config"].existent.first}/#{name}.yml") if yaml.exist? require "erb" - (YAML.load(ERB.new(yaml.read).result) || {})[Rails.env] || {} + (YAML.load(ERB.new(yaml.read).result) || {})[env] || {} else raise "Could not load configuration. No such file - #{yaml}" end diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index ebcfcb1c3a..22b615023d 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -1367,5 +1367,21 @@ module ApplicationTests assert_match 'YAML syntax error occurred while parsing', exception.message end + + test "config_for allows overriding the environment" do + app_file 'config/custom.yml', <<-RUBY + test: + key: 'walrus' + production: + key: 'unicorn' + RUBY + + add_to_config <<-RUBY + config.my_custom_config = config_for('custom', env: 'production') + RUBY + require "#{app_path}/config/environment" + + assert_equal 'unicorn', Rails.application.config.my_custom_config['key'] + end end end -- cgit v1.2.3