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

@ -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 @@ def message_verifier(verifier_name)
# 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 @@ def index
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