diff options
author | Mauro Berlanda <mauro.berlanda@gmail.com> | 2018-09-11 23:47:41 +0200 |
---|---|---|
committer | Mauro Berlanda <mauro.berlanda@gmail.com> | 2018-09-11 23:47:41 +0200 |
commit | b167d521ab873112ff1b98520f184d8088feb754 (patch) | |
tree | b0bd9df4be86e9bae55d329a00a48598da56931e /railties | |
parent | 2a470d73a75ebf8cd7975e469bd82586d9234442 (diff) | |
download | rails-b167d521ab873112ff1b98520f184d8088feb754.tar.gz rails-b167d521ab873112ff1b98520f184d8088feb754.tar.bz2 rails-b167d521ab873112ff1b98520f184d8088feb754.zip |
refacto: config_for with ActiveSupport::InheritableOptions and symbolized keys
Diffstat (limited to 'railties')
-rw-r--r-- | railties/lib/rails/application.rb | 5 | ||||
-rw-r--r-- | railties/test/application/configuration_test.rb | 51 |
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 |