From 1c4a7a0da3945d2230e1a94f487781268f2134cd Mon Sep 17 00:00:00 2001 From: Eugene Kenny Date: Tue, 12 May 2020 12:02:31 +0100 Subject: [PATCH] Return enumerator from each_pair and each_value This matches Hash's behaviour for those methods. --- actionpack/CHANGELOG.md | 5 +++++ .../metal/strong_parameters.rb | 2 ++ .../controller/parameters/accessors_test.rb | 20 +++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 73fdad1822..4086697f4b 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,3 +1,8 @@ +* Calling `each_pair` or `each_value` on an `ActionController::Parameters` + without passing a block now returns an enumerator. + + *Eugene Kenny* + * `fixture_file_upload` now uses path relative to `file_fixture_path` Previously the path had to be relative to `fixture_path`. diff --git a/actionpack/lib/action_controller/metal/strong_parameters.rb b/actionpack/lib/action_controller/metal/strong_parameters.rb index 879e82416f..7389aef51e 100644 --- a/actionpack/lib/action_controller/metal/strong_parameters.rb +++ b/actionpack/lib/action_controller/metal/strong_parameters.rb @@ -360,6 +360,7 @@ module ActionController # Convert all hashes in values into parameters, then yield each pair in # the same way as Hash#each_pair. def each_pair(&block) + return to_enum(__callee__) unless block_given? @parameters.each_pair do |key, value| yield [key, convert_hashes_to_parameters(key, value)] end @@ -369,6 +370,7 @@ module ActionController # Convert all hashes in values into parameters, then yield each value in # the same way as Hash#each_value. def each_value(&block) + return to_enum(:each_value) unless block_given? @parameters.each_pair do |key, value| yield convert_hashes_to_parameters(key, value) end diff --git a/actionpack/test/controller/parameters/accessors_test.rb b/actionpack/test/controller/parameters/accessors_test.rb index 0001e8f70a..764b2cc292 100644 --- a/actionpack/test/controller/parameters/accessors_test.rb +++ b/actionpack/test/controller/parameters/accessors_test.rb @@ -58,6 +58,11 @@ class ParametersAccessorsTest < ActiveSupport::TestCase end end + test "each without a block returns an enumerator" do + assert_kind_of Enumerator, @params.each + assert_equal @params, @params.each.to_h + end + test "each_pair carries permitted status" do @params.permit! @params.each_pair { |key, value| assert(value.permitted?) if key == "person" } @@ -75,6 +80,11 @@ class ParametersAccessorsTest < ActiveSupport::TestCase end end + test "each_pair without a block returns an enumerator" do + assert_kind_of Enumerator, @params.each_pair + assert_equal @params, @params.each_pair.to_h + end + test "each_value carries permitted status" do @params.permit! @params.each_value do |value| @@ -88,6 +98,11 @@ class ParametersAccessorsTest < ActiveSupport::TestCase end end + test "each_value without a block returns an enumerator" do + assert_kind_of Enumerator, @params.each_value + assert_equal @params.values, @params.each_value.to_a + end + test "each_key converts to hash for permitted" do @params.permit! @params.each_key { |key| assert_kind_of(String, key) if key == "person" } @@ -97,6 +112,11 @@ class ParametersAccessorsTest < ActiveSupport::TestCase @params.each_key { |key| assert_kind_of(String, key) if key == "person" } end + test "each_key without a block returns an enumerator" do + assert_kind_of Enumerator, @params.each_key + assert_equal @params.keys, @params.each_key.to_a + end + test "empty? returns true when params contains no key/value pairs" do params = ActionController::Parameters.new assert_empty params