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:
parent
cb848c8dd7
commit
e58c96766f
3 changed files with 22 additions and 2 deletions
|
@ -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*
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue