diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2019-02-14 21:21:30 -0500 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2019-02-14 21:21:47 -0500 |
commit | 79b8b626216b4f8f24b5606f3edd43d5b14cd2a6 (patch) | |
tree | 06c9451efd691b76d616d8fe3e713a7ba5b96b09 | |
parent | c43c83984792ac546e7610c898cf9099cc1a2ee6 (diff) | |
parent | 4f7231da7017a198e5cff4735ffee6c7e5bb36a6 (diff) | |
download | rails-79b8b626216b4f8f24b5606f3edd43d5b14cd2a6.tar.gz rails-79b8b626216b4f8f24b5606f3edd43d5b14cd2a6.tar.bz2 rails-79b8b626216b4f8f24b5606f3edd43d5b14cd2a6.zip |
Merge pull request #35249 from Edouard-chin/ec-config-for-hash-in-arrau
Fix the `config_for` to always return a NonSymbolAccessDeprecatedHash:
-rw-r--r-- | railties/lib/rails/application.rb | 22 | ||||
-rw-r--r-- | railties/test/application/configuration_test.rb | 22 |
2 files changed, 40 insertions, 4 deletions
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index d0417f8a49..fbad3e5db3 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -602,10 +602,7 @@ module Rails end def []=(key, value) - if value.is_a?(Hash) - value = self.class.new(value) - end - super(key.to_sym, value) + regular_writer(key.to_sym, convert_value(value, for: :assignment)) end private @@ -623,6 +620,23 @@ module Rails key end + + def convert_value(value, options = {}) # :doc: + if value.is_a? Hash + if options[:for] == :to_hash + value.to_hash + else + self.class.new(value) + end + elsif value.is_a?(Array) + if options[:for] != :assignment || value.frozen? + value = value.dup + end + value.map! { |e| convert_value(e, options) } + else + value + end + end end end end diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 960f708bdf..f3161ad2a8 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -1787,6 +1787,28 @@ module ApplicationTests end end + test "config_for loads nested custom configuration inside array from yaml with deprecated non-symbol access" 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" + + config = Rails.application.config.my_custom_config + assert_instance_of Rails::Application::NonSymbolAccessDeprecatedHash, config[:foo][:bar].first + + assert_deprecated do + assert_equal 1, config[:foo][:bar].first["baz"] + end + end + test "config_for makes all hash methods available" do app_file "config/custom.yml", <<-RUBY development: |