diff options
author | yuuji.yaginuma <yuuji.yaginuma@gmail.com> | 2016-10-18 08:17:54 +0900 |
---|---|---|
committer | yuuji.yaginuma <yuuji.yaginuma@gmail.com> | 2016-10-18 18:04:40 +0900 |
commit | 6d7f35ecb1ab6d58bb81ecbf5ff18009fb002425 (patch) | |
tree | 8e88a9cd29faa5e1595cf9ee6575614c160ab1e4 | |
parent | 8e76f6959efc44c8575b721fdf53d06ea9430fed (diff) | |
download | rails-6d7f35ecb1ab6d58bb81ecbf5ff18009fb002425.tar.gz rails-6d7f35ecb1ab6d58bb81ecbf5ff18009fb002425.tar.bz2 rails-6d7f35ecb1ab6d58bb81ecbf5ff18009fb002425.zip |
add `ActionController::Parameters#merge!`
This method has the same behavior as `Hash#merge!`, returns current
`ActionController::Parameters`.
-rw-r--r-- | actionpack/CHANGELOG.md | 4 | ||||
-rw-r--r-- | actionpack/lib/action_controller/metal/strong_parameters.rb | 7 | ||||
-rw-r--r-- | actionpack/test/controller/parameters/parameters_permit_test.rb | 17 |
3 files changed, 28 insertions, 0 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index c9c347ea26..d4b8d9b456 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,3 +1,7 @@ +* Add `ActionController::Parameters#merge!`, which behaves the same as `Hash#merge!`. + + *Yuji Yaginuma* + * Allow keys not found in RACK_KEY_TRANSLATION for setting the environment when rendering arbitrary templates. diff --git a/actionpack/lib/action_controller/metal/strong_parameters.rb b/actionpack/lib/action_controller/metal/strong_parameters.rb index b64617d084..9df67d4be1 100644 --- a/actionpack/lib/action_controller/metal/strong_parameters.rb +++ b/actionpack/lib/action_controller/metal/strong_parameters.rb @@ -573,6 +573,13 @@ module ActionController ) 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. diff --git a/actionpack/test/controller/parameters/parameters_permit_test.rb b/actionpack/test/controller/parameters/parameters_permit_test.rb index 728d8e1279..e66c2807c3 100644 --- a/actionpack/test/controller/parameters/parameters_permit_test.rb +++ b/actionpack/test/controller/parameters/parameters_permit_test.rb @@ -244,6 +244,23 @@ class ParametersPermitTest < ActiveSupport::TestCase 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" } |