From bd7f47190e942d1e514f702eafd1083909dd2884 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Fri, 25 Jul 2014 16:16:58 -0400 Subject: Add missing `Hash` methods to `AC::Parameters` This is to make sure that `permitted` status is maintained on the resulting object. I found these methods that needs to be redefined by looking for `self.class.new` in the code. * extract! * transform_keys * transform_values --- .../action_controller/metal/strong_parameters.rb | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/metal/strong_parameters.rb b/actionpack/lib/action_controller/metal/strong_parameters.rb index 764474ad4e..02652a84b3 100644 --- a/actionpack/lib/action_controller/metal/strong_parameters.rb +++ b/actionpack/lib/action_controller/metal/strong_parameters.rb @@ -355,6 +355,46 @@ module ActionController end end + # Removes and returns the key/value pairs matching the given keys. + # + # params = ActionController::Parameters.new(a: 1, b: 2, c: 3) + # params.extract!(:a, :b) # => {"a"=>1, "b"=>2} + # params # => {"c"=>3} + def extract!(*keys) + self.class.new(super).tap do |new_instance| + new_instance.permitted = @permitted + end + end + + # Returns a new ActionController::Parameters with the results of + # running +block+ once for every value. The keys are unchanged. + # + # params = ActionController::Parameters.new(a: 1, b: 2, c: 3) + # params.transform_values { |x| x * 2 } + # # => {"a"=>2, "b"=>4, "c"=>6} + def transform_values + if block_given? + self.class.new(super).tap do |new_instance| + new_instance.permitted = @permitted + end + else + super + end + end + + # This method is here only to make sure that the returned object has the + # correct +permitted+ status. It should not matter since the parent of + # this object is +HashWithIndifferentAccess+ + def transform_keys # :nodoc: + if block_given? + self.class.new(super).tap do |new_instance| + new_instance.permitted = @permitted + end + else + super + end + end + # Returns an exact copy of the ActionController::Parameters # instance. +permitted+ state is kept on the duped object. # -- cgit v1.2.3