Skip to content
This repository has been archived by the owner on Aug 17, 2017. It is now read-only.

Bug fix : nested ActiveSupport::HashWithIndifferentAccess were not converted to ActionController::Params #143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/action_controller/parameters.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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) })
Expand Down
12 changes: 12 additions & 0 deletions 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