1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Return enumerator from each_pair and each_value

This matches Hash's behaviour for those methods.
This commit is contained in:
Eugene Kenny 2020-05-12 12:02:31 +01:00
parent 25bc1c0134
commit 1c4a7a0da3
3 changed files with 27 additions and 0 deletions

View file

@ -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`.

View file

@ -360,6 +360,7 @@ module ActionController
# Convert all hashes in values into parameters, then yield each pair in
# the same way as <tt>Hash#each_pair</tt>.
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 <tt>Hash#each_value</tt>.
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

View file

@ -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