diff options
author | Edouard CHIN <edouard.chin@shopify.com> | 2019-02-13 13:00:47 +0100 |
---|---|---|
committer | Edouard CHIN <edouard.chin@shopify.com> | 2019-02-14 14:11:05 +0100 |
commit | 4f7231da7017a198e5cff4735ffee6c7e5bb36a6 (patch) | |
tree | df70894c85f813b8948f5d9d17dc1f4c8fae910c /railties/lib/rails/application.rb | |
parent | 8d9d46fdae4502b8881eca641c51a64ee7804194 (diff) | |
download | rails-4f7231da7017a198e5cff4735ffee6c7e5bb36a6.tar.gz rails-4f7231da7017a198e5cff4735ffee6c7e5bb36a6.tar.bz2 rails-4f7231da7017a198e5cff4735ffee6c7e5bb36a6.zip |
Fix the `config_for` to always return a NonSymbolAccessDeprecatedHash:
- If you have hashes inside array, the hashes were getting initialized
as regular HWIA wereas we want them to be
NonSymbolAccessDeprecatedHash in order to trigger a deprecation
warning when keys are accessed with string.
This patch fixes that by overwriting the `[]=` to to the same
as what HWIA does (with the difference that we don't call
`convert_key` to not trigger a deprecation when setting value).
I also took the liberty to extract `hash.nested_under_indifferent_access`,
into a separate method to allow subclasses to return whatever
they want.
Inheriting HWIA is not common, but I think it's useful for cases
like this one where we want to preprocess reading and writing values
in the hash (for deprecation purposes or other reasons).
Diffstat (limited to 'railties/lib/rails/application.rb')
-rw-r--r-- | railties/lib/rails/application.rb | 22 |
1 files changed, 18 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 |