mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Convert hashes into parameters (#33076)
* Convert hashes into parameters Ensure `ActionController::Parameters#transform_values` and `ActionController::Parameters#transform_values!` converts hashes into parameters. * fixup! Convert hashes into parameters [Rafael Mendonça França + Kevin Sjöberg]
This commit is contained in:
parent
897946f214
commit
716f2e09c2
2 changed files with 29 additions and 10 deletions
|
@ -639,20 +639,18 @@ module ActionController
|
|||
# params = ActionController::Parameters.new(a: 1, b: 2, c: 3)
|
||||
# params.transform_values { |x| x * 2 }
|
||||
# # => <ActionController::Parameters {"a"=>2, "b"=>4, "c"=>6} permitted: false>
|
||||
def transform_values(&block)
|
||||
if block
|
||||
new_instance_with_inherited_permitted_status(
|
||||
@parameters.transform_values(&block)
|
||||
)
|
||||
else
|
||||
@parameters.transform_values
|
||||
end
|
||||
def transform_values
|
||||
return to_enum(:transform_values) unless block_given?
|
||||
new_instance_with_inherited_permitted_status(
|
||||
@parameters.transform_values { |v| yield convert_value_to_parameters(v) }
|
||||
)
|
||||
end
|
||||
|
||||
# Performs values transformation and returns the altered
|
||||
# <tt>ActionController::Parameters</tt> instance.
|
||||
def transform_values!(&block)
|
||||
@parameters.transform_values!(&block)
|
||||
def transform_values!
|
||||
return to_enum(:transform_values!) unless block_given?
|
||||
@parameters.transform_values! { |v| yield convert_value_to_parameters(v) }
|
||||
self
|
||||
end
|
||||
|
||||
|
|
|
@ -190,6 +190,27 @@ class ParametersAccessorsTest < ActiveSupport::TestCase
|
|||
assert_not_predicate @params.transform_values { |v| v }, :permitted?
|
||||
end
|
||||
|
||||
test "transform_values converts hashes to parameters" do
|
||||
@params.transform_values do |value|
|
||||
assert_kind_of ActionController::Parameters, value
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
test "transform_values without block yieds an enumerator" do
|
||||
assert_kind_of Enumerator, @params.transform_values
|
||||
end
|
||||
|
||||
test "transform_values! converts hashes to parameters" do
|
||||
@params.transform_values! do |value|
|
||||
assert_kind_of ActionController::Parameters, value
|
||||
end
|
||||
end
|
||||
|
||||
test "transform_values! without block yields an enumerator" do
|
||||
assert_kind_of Enumerator, @params.transform_values!
|
||||
end
|
||||
|
||||
test "value? returns true if the given value is present in the params" do
|
||||
params = ActionController::Parameters.new(city: "Chicago", state: "Illinois")
|
||||
assert params.value?("Chicago")
|
||||
|
|
Loading…
Reference in a new issue