aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2018-09-11 18:26:59 -0400
committerGitHub <noreply@github.com>2018-09-11 18:26:59 -0400
commit21b32bb2a8001240e3e2c42811e2777d0411837d (patch)
tree1a7df57490790d8cc2e9de7d3bbf3d1b9138c5b1
parentf589e20b0a865a7efd93e65b2887f0e4ab9a556f (diff)
parentb167d521ab873112ff1b98520f184d8088feb754 (diff)
downloadrails-21b32bb2a8001240e3e2c42811e2777d0411837d.tar.gz
rails-21b32bb2a8001240e3e2c42811e2777d0411837d.tar.bz2
rails-21b32bb2a8001240e3e2c42811e2777d0411837d.zip
Merge pull request #33815 from mberlanda/mberlanda/enhance-config-for
Use ActiveSupport::InheritableOptions and deep_symbolize_keys in config_for
-rw-r--r--railties/lib/rails/application.rb5
-rw-r--r--railties/test/application/configuration_test.rb51
2 files changed, 53 insertions, 3 deletions
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb
index 99e42ebefb..26ed195dcc 100644
--- a/railties/lib/rails/application.rb
+++ b/railties/lib/rails/application.rb
@@ -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
diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb
index f5119b1931..83192edb39 100644
--- a/railties/test/application/configuration_test.rb
+++ b/railties/test/application/configuration_test.rb
@@ -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