1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Pass block in ActionController::Parameters#delete

In order to fully support the same interface as `Hash#delete`, we need
to pass the block through to the underlying method, not just the key.

This used to work correctly, but it regressed when
`ActionController::Parameters` stopped inheriting from `Hash` in 5.0.
This commit is contained in:
Eugene Kenny 2017-05-10 23:48:46 +01:00
parent 97bd56e674
commit fb0fae9747
2 changed files with 23 additions and 2 deletions

View file

@ -666,8 +666,8 @@ module ActionController
# to key. If the key is not found, returns the default value. If the
# optional code block is given and the key is not found, pass in the key
# and return the result of block.
def delete(key)
convert_value_to_parameters(@parameters.delete(key))
def delete(key, &block)
convert_value_to_parameters(@parameters.delete(key, &block))
end
# Returns a new instance of <tt>ActionController::Parameters</tt> with only

View file

@ -25,6 +25,27 @@ class ParametersMutatorsTest < ActiveSupport::TestCase
assert_not @params.delete(:person).permitted?
end
test "delete returns the value when the key is present" do
assert_equal "32", @params[:person].delete(:age)
end
test "delete removes the entry when the key present" do
@params[:person].delete(:age)
assert_not @params[:person].key?(:age)
end
test "delete returns nil when the key is not present" do
assert_equal nil, @params[:person].delete(:first_name)
end
test "delete returns the value of the given block when the key is not present" do
assert_equal "David", @params[:person].delete(:first_name) { "David" }
end
test "delete yields the key to the given block when the key is not present" do
assert_equal "first_name: David", @params[:person].delete(:first_name) { |k| "#{k}: David" }
end
test "delete_if retains permitted status" do
@params.permit!
assert @params.delete_if { |k| k == "person" }.permitted?