Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert hashes into parameters #33076

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 8 additions & 10 deletions actionpack/lib/action_controller/metal/strong_parameters.rb
Expand Up @@ -639,20 +639,18 @@ def extract!(*keys)
# 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

Expand Down
21 changes: 21 additions & 0 deletions actionpack/test/controller/parameters/accessors_test.rb
Expand Up @@ -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")
Expand Down