From aa3c97dfab61a535e8146a27b8db9c7bec482ae3 Mon Sep 17 00:00:00 2001 From: Thomas Guillory Date: Mon, 20 May 2013 12:23:53 +0200 Subject: [PATCH] Fix nested conversion of ActiveSupport::HashWithIndifferentAccess to ActionController::Parameters --- lib/action_controller/parameters.rb | 17 ++++++++++++++++- test/parameters_conversion_test.rb | 12 ++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 test/parameters_conversion_test.rb diff --git a/lib/action_controller/parameters.rb b/lib/action_controller/parameters.rb index 644f5e6..714487f 100644 --- a/lib/action_controller/parameters.rb +++ b/lib/action_controller/parameters.rb @@ -41,6 +41,21 @@ def initialize(attributes = nil) @permitted = false end + def self.new_from_hash_copying_default(hash) + new(hash).tap do |new_hash| + new_hash.default = hash.default + end + end + + def update(other_hash) + if other_hash.is_a? Parameters + super(other_hash) + else + other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) } + self + end + end + def permit! each_pair do |key, value| convert_hashes_to_parameters(key, value) @@ -99,7 +114,7 @@ def dup protected def convert_value(value) - if value.class == Hash + if value.is_a?(Hash) self.class.new_from_hash_copying_default(value) elsif value.is_a?(Array) value.dup.replace(value.map { |e| convert_value(e) }) diff --git a/test/parameters_conversion_test.rb b/test/parameters_conversion_test.rb new file mode 100644 index 0000000..7296f21 --- /dev/null +++ b/test/parameters_conversion_test.rb @@ -0,0 +1,12 @@ +require 'test_helper' +require 'action_controller/parameters' + +class ParametersConversionTest < ActiveSupport::TestCase + + test "nested ActiveSupport::HashWithIndifferentAccess are converted to ActionController::Parameters" do + parameters = ActionController::Parameters.new({:people => [{:a => 'b'}]}.with_indifferent_access) + assert_instance_of( ActionController::Parameters, + parameters[:people].first) + end + +end