diff options
author | Prem Sichanugrist <s@sikac.hu> | 2014-07-25 16:23:38 -0400 |
---|---|---|
committer | Prem Sichanugrist <s@sikac.hu> | 2014-08-18 20:42:45 -0400 |
commit | 9704379c780db81f0aea7038cab596c3d19909a8 (patch) | |
tree | 0a7591fe61ee8a2daa33073f406acd988d09990f /actionpack | |
parent | bd7f47190e942d1e514f702eafd1083909dd2884 (diff) | |
download | rails-9704379c780db81f0aea7038cab596c3d19909a8.tar.gz rails-9704379c780db81f0aea7038cab596c3d19909a8.tar.bz2 rails-9704379c780db81f0aea7038cab596c3d19909a8.zip |
Refactor code to reduce duplicate `self.class.new`
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_controller/metal/strong_parameters.rb | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/actionpack/lib/action_controller/metal/strong_parameters.rb b/actionpack/lib/action_controller/metal/strong_parameters.rb index 02652a84b3..beffe9172b 100644 --- a/actionpack/lib/action_controller/metal/strong_parameters.rb +++ b/actionpack/lib/action_controller/metal/strong_parameters.rb @@ -350,9 +350,7 @@ module ActionController # params.slice(:a, :b) # => {"a"=>1, "b"=>2} # params.slice(:d) # => {} def slice(*keys) - self.class.new(super).tap do |new_instance| - new_instance.permitted = @permitted - end + new_instance_with_inherited_permitted_status(super) end # Removes and returns the key/value pairs matching the given keys. @@ -361,9 +359,7 @@ module ActionController # 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 + new_instance_with_inherited_permitted_status(super) end # Returns a new <tt>ActionController::Parameters</tt> with the results of @@ -374,9 +370,7 @@ module ActionController # # => {"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 + new_instance_with_inherited_permitted_status(super) else super end @@ -387,9 +381,7 @@ module ActionController # this object is +HashWithIndifferentAccess+ def transform_keys # :nodoc: if block_given? - self.class.new(super).tap do |new_instance| - new_instance.permitted = @permitted - end + new_instance_with_inherited_permitted_status(super) else super end @@ -415,6 +407,12 @@ module ActionController end private + def new_instance_with_inherited_permitted_status(hash) + self.class.new(hash).tap do |new_instance| + new_instance.permitted = @permitted + end + end + def convert_hashes_to_parameters(key, value, assign_if_converted=true) converted = convert_value_to_parameters(value) self[key] = converted if assign_if_converted && !converted.equal?(value) |