diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2017-04-11 21:56:55 -0400 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2017-04-18 17:33:24 -0400 |
commit | 9f4c2632ef28b9622ffa0eca5d02beea8ec809c0 (patch) | |
tree | 18b38a2187e01d9f02f24d96c67a4e60af8e5827 | |
parent | fd88ccc905549c61e0e4525fcb68b91d20b9afe9 (diff) | |
download | rails-9f4c2632ef28b9622ffa0eca5d02beea8ec809c0.tar.gz rails-9f4c2632ef28b9622ffa0eca5d02beea8ec809c0.tar.bz2 rails-9f4c2632ef28b9622ffa0eca5d02beea8ec809c0.zip |
Add ActionController::Parameters#to_hash to implict conversion
Now methods that implicit convert objects to a hash will be able to work
without requiring the users to change their implementation.
This method will return a Hash instead of a HashWithIndefirentAccess
to mimic the same implementation of HashWithIndefirentAccess#to_hash.
-rw-r--r-- | actionpack/lib/action_controller/metal/strong_parameters.rb | 16 | ||||
-rw-r--r-- | actionpack/test/controller/parameters/parameters_permit_test.rb | 26 |
2 files changed, 42 insertions, 0 deletions
diff --git a/actionpack/lib/action_controller/metal/strong_parameters.rb b/actionpack/lib/action_controller/metal/strong_parameters.rb index b20ad940ea..62c654da03 100644 --- a/actionpack/lib/action_controller/metal/strong_parameters.rb +++ b/actionpack/lib/action_controller/metal/strong_parameters.rb @@ -259,6 +259,22 @@ module ActionController end end + # Returns a safe <tt>Hash</tt> representation of this parameter + # with all unpermitted keys removed. + # + # params = ActionController::Parameters.new({ + # name: 'Senjougahara Hitagi', + # oddity: 'Heavy stone crab' + # }) + # params.to_hash + # # => ActionController::UnfilteredParameters: unable to convert unfiltered parameters to hash + # + # safe_params = params.permit(:name) + # safe_params.to_hash # => {"name"=>"Senjougahara Hitagi"} + def to_hash + to_h.to_hash + end + # Returns an unsafe, unfiltered # <tt>ActiveSupport::HashWithIndifferentAccess</tt> representation of this # parameter. diff --git a/actionpack/test/controller/parameters/parameters_permit_test.rb b/actionpack/test/controller/parameters/parameters_permit_test.rb index 2616b040d1..12555c3136 100644 --- a/actionpack/test/controller/parameters/parameters_permit_test.rb +++ b/actionpack/test/controller/parameters/parameters_permit_test.rb @@ -403,6 +403,32 @@ class ParametersPermitTest < ActiveSupport::TestCase end end + test "to_hash raises UnfilteredParameters on unfiltered params" do + assert_raises(ActionController::UnfilteredParameters) do + @params.to_hash + end + end + + test "to_hash returns converted hash on permitted params" do + @params.permit! + + assert_instance_of Hash, @params.to_hash + assert_not_kind_of ActionController::Parameters, @params.to_hash + end + + test "to_hash returns converted hash when .permit_all_parameters is set" do + begin + ActionController::Parameters.permit_all_parameters = true + params = ActionController::Parameters.new(crab: "Senjougahara Hitagi") + + assert_instance_of Hash, params.to_hash + assert_not_kind_of ActionController::Parameters, params.to_hash + assert_equal({ "crab" => "Senjougahara Hitagi" }, params.to_hash) + assert_equal({ "crab" => "Senjougahara Hitagi" }, params) + ensure + ActionController::Parameters.permit_all_parameters = false + end + end test "to_unsafe_h returns unfiltered params" do assert @params.to_unsafe_h.is_a? ActiveSupport::HashWithIndifferentAccess |