Skip to content

Commit

Permalink
Merge pull request #29040 from eugeneius/parameters_delete_block
Browse files Browse the repository at this point in the history
Pass block in ActionController::Parameters#delete
  • Loading branch information
tenderlove committed May 11, 2017
2 parents 358280d + fb0fae9 commit 4b969ea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions actionpack/lib/action_controller/metal/strong_parameters.rb
Expand Up @@ -666,8 +666,8 @@ def transform_keys!(&block)
# 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
Expand Down
21 changes: 21 additions & 0 deletions actionpack/test/controller/parameters/mutators_test.rb
Expand Up @@ -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?
Expand Down

0 comments on commit 4b969ea

Please sign in to comment.