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

refacto: config_for with ActiveSupport::InheritableOptions and symbolized keys

This commit is contained in:
Mauro Berlanda 2018-09-11 23:47:41 +02:00
parent 2a470d73a7
commit b167d521ab
No known key found for this signature in database
GPG key ID: 0C87569D7215D095
2 changed files with 53 additions and 3 deletions

View file

@ -232,7 +232,10 @@ module Rails
if yaml.exist?
require "erb"
(YAML.load(ERB.new(yaml.read).result) || {})[env] || {}
require "active_support/ordered_options"
config = (YAML.load(ERB.new(yaml.read).result) || {})[env] || {}
ActiveSupport::InheritableOptions.new(config.deep_symbolize_keys)
else
raise "Could not load configuration. No such file - #{yaml}"
end

View file

@ -1671,7 +1671,7 @@ module ApplicationTests
test "config_for loads custom configuration from yaml files" do
app_file "config/custom.yml", <<-RUBY
development:
key: 'custom key'
foo: 'bar'
RUBY
add_to_config <<-RUBY
@ -1680,7 +1680,54 @@ module ApplicationTests
app "development"
assert_equal "custom key", Rails.application.config.my_custom_config["key"]
assert_equal "bar", Rails.application.config.my_custom_config["foo"]
end
test "config_for loads custom configuration from yaml accessible as symbol" do
app_file "config/custom.yml", <<-RUBY
development:
foo: 'bar'
RUBY
add_to_config <<-RUBY
config.my_custom_config = config_for('custom')
RUBY
app "development"
assert_equal "bar", Rails.application.config.my_custom_config[:foo]
end
test "config_for loads custom configuration from yaml accessible as method" do
app_file "config/custom.yml", <<-RUBY
development:
foo: 'bar'
RUBY
add_to_config <<-RUBY
config.my_custom_config = config_for('custom')
RUBY
app "development"
assert_equal "bar", Rails.application.config.my_custom_config.foo
end
test "config_for loads nested custom configuration from yaml as symbol keys" do
app_file "config/custom.yml", <<-RUBY
development:
foo:
bar:
baz: 1
RUBY
add_to_config <<-RUBY
config.my_custom_config = config_for('custom')
RUBY
app "development"
assert_equal 1, Rails.application.config.my_custom_config.foo[:bar][:baz]
end
test "config_for uses the Pathname object if it is provided" do