mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #34609 from kamipo/delete_all_on_collection_proxy
Ensure that `delete_all` on collection proxy returns affected count
This commit is contained in:
commit
609c58bfa6
5 changed files with 23 additions and 6 deletions
|
@ -1,3 +1,7 @@
|
|||
* Ensure that `delete_all` on collection proxy returns affected count.
|
||||
|
||||
*Ryuta Kamizono*
|
||||
|
||||
* Reset scope after delete on collection association to clear stale offsets of removed records.
|
||||
|
||||
*Gannon McGibbon*
|
||||
|
|
|
@ -99,6 +99,7 @@ module ActiveRecord
|
|||
def delete_or_nullify_all_records(method)
|
||||
count = delete_count(method, scope)
|
||||
update_counter(-count)
|
||||
count
|
||||
end
|
||||
|
||||
# Deletes the records according to the <tt>:dependent</tt> option.
|
||||
|
|
|
@ -161,6 +161,8 @@ module ActiveRecord
|
|||
else
|
||||
update_counter(-count)
|
||||
end
|
||||
|
||||
count
|
||||
end
|
||||
|
||||
def difference(a, b)
|
||||
|
|
|
@ -264,7 +264,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
|
|||
car = Car.create(name: "honda")
|
||||
car.funky_bulbs.create!
|
||||
assert_equal 1, car.funky_bulbs.count
|
||||
assert_nothing_raised { car.reload.funky_bulbs.delete_all }
|
||||
assert_equal 1, car.reload.funky_bulbs.delete_all
|
||||
assert_equal 0, car.funky_bulbs.count, "bulbs should have been deleted using :delete_all strategy"
|
||||
end
|
||||
|
||||
|
@ -1413,7 +1413,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
|
|||
assert_equal 3, clients.count
|
||||
|
||||
assert_difference "Client.count", -(clients.count) do
|
||||
companies(:first_firm).dependent_clients_of_firm.delete_all
|
||||
assert_equal clients.count, companies(:first_firm).dependent_clients_of_firm.delete_all
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -1510,10 +1510,20 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
|
|||
def test_delete_all_with_option_delete_all
|
||||
firm = companies(:first_firm)
|
||||
client_id = firm.dependent_clients_of_firm.first.id
|
||||
firm.dependent_clients_of_firm.delete_all(:delete_all)
|
||||
count = firm.dependent_clients_of_firm.count
|
||||
assert_equal count, firm.dependent_clients_of_firm.delete_all(:delete_all)
|
||||
assert_nil Client.find_by_id(client_id)
|
||||
end
|
||||
|
||||
def test_delete_all_with_option_nullify
|
||||
firm = companies(:first_firm)
|
||||
client_id = firm.dependent_clients_of_firm.first.id
|
||||
count = firm.dependent_clients_of_firm.count
|
||||
assert_equal firm, Client.find(client_id).firm
|
||||
assert_equal count, firm.dependent_clients_of_firm.delete_all(:nullify)
|
||||
assert_nil Client.find(client_id).firm
|
||||
end
|
||||
|
||||
def test_delete_all_accepts_limited_parameters
|
||||
firm = companies(:first_firm)
|
||||
assert_raise(ArgumentError) do
|
||||
|
|
|
@ -200,7 +200,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
|
|||
|
||||
assert_no_difference "Job.count" do
|
||||
assert_difference "Reference.count", -1 do
|
||||
person.reload.jobs_with_dependent_destroy.delete_all
|
||||
assert_equal 1, person.reload.jobs_with_dependent_destroy.delete_all
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -211,7 +211,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
|
|||
|
||||
assert_no_difference "Job.count" do
|
||||
assert_no_difference "Reference.count" do
|
||||
person.reload.jobs_with_dependent_nullify.delete_all
|
||||
assert_equal 1, person.reload.jobs_with_dependent_nullify.delete_all
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -222,7 +222,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
|
|||
|
||||
assert_no_difference "Job.count" do
|
||||
assert_difference "Reference.count", -1 do
|
||||
person.reload.jobs_with_dependent_delete_all.delete_all
|
||||
assert_equal 1, person.reload.jobs_with_dependent_delete_all.delete_all
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue