提交 1c4a7a0d 编写于 作者: E Eugene Kenny

Return enumerator from each_pair and each_value

This matches Hash's behaviour for those methods.
上级 25bc1c01
* 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 @@ def to_unsafe_h
# 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 @@ def each_pair(&block)
# 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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册