mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #39249 from eugeneius/parameters_each_pair_each_value_enumerator
Return enumerator from ActionController::Parameters' each_pair and each_value methods
This commit is contained in:
commit
23bf8e3bd4
3 changed files with 27 additions and 0 deletions
|
@ -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`.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue