mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #35249 from Edouard-chin/ec-config-for-hash-in-arrau
Fix the `config_for` to always return a NonSymbolAccessDeprecatedHash:
This commit is contained in:
commit
79b8b62621
2 changed files with 40 additions and 4 deletions
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in a new issue