1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

rails/application: allow passing an env to config_for

This commit is contained in:
Simon Eskildsen 2015-10-30 18:46:15 +00:00
parent cb848c8dd7
commit e58c96766f
3 changed files with 22 additions and 2 deletions

View file

@ -1,3 +1,7 @@
* Allow passing an environment to `config_for`.
*Simon Eskildsen*
* Allow rake:stats to account for rake tasks in lib/tasks * Allow rake:stats to account for rake tasks in lib/tasks
*Kevin Deisz* *Kevin Deisz*

View file

@ -218,12 +218,12 @@ module Rails
# Rails.application.configure do # Rails.application.configure do
# config.middleware.use ExceptionNotifier, config_for(:exception_notification) # config.middleware.use ExceptionNotifier, config_for(:exception_notification)
# end # end
def config_for(name) def config_for(name, env: Rails.env)
yaml = Pathname.new("#{paths["config"].existent.first}/#{name}.yml") yaml = Pathname.new("#{paths["config"].existent.first}/#{name}.yml")
if yaml.exist? if yaml.exist?
require "erb" require "erb"
(YAML.load(ERB.new(yaml.read).result) || {})[Rails.env] || {} (YAML.load(ERB.new(yaml.read).result) || {})[env] || {}
else else
raise "Could not load configuration. No such file - #{yaml}" raise "Could not load configuration. No such file - #{yaml}"
end end

View file

@ -1367,5 +1367,21 @@ module ApplicationTests
assert_match 'YAML syntax error occurred while parsing', exception.message assert_match 'YAML syntax error occurred while parsing', exception.message
end 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
end end