未验证 提交 2a08f79e 编写于 作者: R Rafael França 提交者: GitHub

Merge pull request #36294 from gustavogr/add-deep-transform-keys-to-strong-params

Added deep_transform_keys to StrongParameters
* Added `deep_transform_keys` and `deep_transform_keys!` methods to ActionController::Parameters.
*Gustavo Gutierrez*
* Calling `ActionController::Parameters#transform_keys/!` without a block now returns
an enumerator for the parameters instead of the underlying hash.
......
......@@ -693,6 +693,23 @@ def transform_keys!(&block)
self
end
# Returns a new <tt>ActionController::Parameters</tt> instance with the
# results of running +block+ once for every key. This includes the keys
# from the root hash and from all nested hashes and arrays. The values are unchanged.
def deep_transform_keys(&block)
new_instance_with_inherited_permitted_status(
@parameters.deep_transform_keys(&block)
)
end
# Returns the <tt>ActionController::Parameters</tt> instance changing its keys.
# This includes the keys from the root hash and from all nested hashes and arrays.
# The values are unchanged.
def deep_transform_keys!(&block)
@parameters.deep_transform_keys!(&block)
self
end
# Deletes a key-value pair from +Parameters+ and returns the value. If
# +key+ is not found, returns +nil+ (or, with optional code block, yields
# +key+ and returns the result). Cf. +#extract!+, which returns the
......
......@@ -213,6 +213,15 @@ class ParametersAccessorsTest < ActiveSupport::TestCase
assert_kind_of ActionController::Parameters, @params.transform_keys!.each { |k| k }
end
test "deep_transform_keys retains permitted status" do
@params.permit!
assert_predicate @params.deep_transform_keys { |k| k }, :permitted?
end
test "deep_transform_keys retains unpermitted status" do
assert_not_predicate @params.deep_transform_keys { |k| k }, :permitted?
end
test "transform_values retains permitted status" do
@params.permit!
assert_predicate @params.transform_values { |v| v }, :permitted?
......
......@@ -118,4 +118,13 @@ class ParametersMutatorsTest < ActiveSupport::TestCase
test "transform_values! retains unpermitted status" do
assert_not_predicate @params.transform_values! { |v| v }, :permitted?
end
test "deep_transform_keys! retains permitted status" do
@params.permit!
assert_predicate @params.deep_transform_keys! { |k| k }, :permitted?
end
test "deep_transform_keys! retains unpermitted status" do
assert_not_predicate @params.deep_transform_keys! { |k| k }, :permitted?
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册