Skip to content

Commit

Permalink
Merge pull request #26820 from y-yagi/add_bang_merge_to_parameters
Browse files Browse the repository at this point in the history
add `ActionController::Parameters#merge!`
  • Loading branch information
rafaelfranca committed Nov 10, 2016
1 parent cfad06c commit dff89e9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Add `ActionController::Parameters#merge!`, which behaves the same as `Hash#merge!`.

*Yuji Yaginuma*

* Make `fixture_file_upload` work in integration tests.

*Yuji Yaginuma*
Expand Down
7 changes: 7 additions & 0 deletions actionpack/lib/action_controller/metal/strong_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,13 @@ def merge(other_hash)
)
end

# Returns current <tt>ActionController::Parameters</tt> instance which
# +other_hash+ merges into current hash.
def merge!(other_hash)
@parameters.merge!(other_hash.to_h)
self
end

# This is required by ActiveModel attribute assignment, so that user can
# pass +Parameters+ to a mass assignment methods in a model. It should not
# matter as we are using +HashWithIndifferentAccess+ internally.
Expand Down
17 changes: 17 additions & 0 deletions actionpack/test/controller/parameters/parameters_permit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,23 @@ def walk_permitted params
assert merged_params[:id]
end

test "not permitted is sticky beyond merge!" do
assert_not @params.merge!(a: "b").permitted?
end

test "permitted is sticky beyond merge!" do
@params.permit!
assert @params.merge!(a: "b").permitted?
end

test "merge! with parameters" do
other_params = ActionController::Parameters.new(id: "1234").permit!
@params.merge!(other_params)

assert_equal "1234", @params[:id]
assert_equal "32", @params[:person][:age]
end

test "modifying the parameters" do
@params[:person][:hometown] = "Chicago"
@params[:person][:family] = { brother: "Jonas" }
Expand Down

0 comments on commit dff89e9

Please sign in to comment.