From b03b54fba1c7f284b872231d5dc68a67515b9f36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Andr=C3=A9s=20=C3=81lvarez?= Date: Fri, 7 Mar 2014 11:29:51 -0600 Subject: [PATCH] Permit ActionController::AnyParam on hashes --- lib/action_controller/parameters.rb | 7 +++++ test/parameters_permit_test.rb | 44 +++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/lib/action_controller/parameters.rb b/lib/action_controller/parameters.rb index 8f43723..0aa2f63 100644 --- a/lib/action_controller/parameters.rb +++ b/lib/action_controller/parameters.rb @@ -27,6 +27,8 @@ def initialize(params) end end + class AnyParam; end + class Parameters < ActiveSupport::HashWithIndifferentAccess attr_accessor :permitted alias :permitted? :permitted @@ -191,6 +193,11 @@ def hash_filter(params, filter) slice(*filter.keys).each do |key, value| next unless value + if filter[key] == ActionController::AnyParam + params[key] = value + return + end + if filter[key] == [] # Declaration {:comment_ids => []}. array_of_permitted_scalars_filter(params, key) diff --git a/test/parameters_permit_test.rb b/test/parameters_permit_test.rb index b1905a9..d59973f 100644 --- a/test/parameters_permit_test.rb +++ b/test/parameters_permit_test.rb @@ -346,4 +346,48 @@ def assert_filtered_out(params, key) assert !hash.permitted? end end + + test 'any param permitted in the root' do + initial_hash = { + :book => { + :title => "Romeo and Juliet", + :authors => [{ + :name => "William Shakespeare", + :born => "1564-04-26" + }] + } + } + + params = ActionController::Parameters.new(initial_hash) + + permitted = params.permit({ :book => ActionController::AnyParam }) + assert_equal initial_hash.with_indifferent_access, permitted + end + + test 'any param permitted in a nested hash' do + initial_hash = { + :book => { + :title => "Romeo and Juliet", + :authors => [{ + :name => "William Shakespeare", + :born => "1564-04-26" + }] + } + } + + params = ActionController::Parameters.new(initial_hash) + expected_hash = { + "book" => { + "authors" => [ + { + "name" => "William Shakespeare", + "born" => "1564-04-26" + } + ] + } + } + + permitted = params.permit({ :book => [ authors: ActionController::AnyParam ] }) + assert_equal expected_hash, permitted + end end