提交 9704379c 编写于 作者: P Prem Sichanugrist

Refactor code to reduce duplicate `self.class.new`

上级 bd7f4719
......@@ -350,9 +350,7 @@ def fetch(key, *args)
# 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 @@ def slice(*keys)
# 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 @@ def extract!(*keys)
# # => {"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 @@ def transform_values
# 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 @@ def permitted=(new_permitted)
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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册