Skip to content

Commit 7afc626

Browse files
authored
MONGOID-5079 clear has_many association locally after destroy_all (#5278)
* MONGOID-5079 has_many association is not cleared after destroy_all * MONGOID-5079 fix tests * MONGOID-5079 add HABTM tests
1 parent 5c1c60f commit 7afc626

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

lib/mongoid/association/referenced/has_many/proxy.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,8 @@ def remove_all(conditions = nil, method = :delete_all)
472472
selector = conditions || {}
473473
removed = klass.send(method, selector.merge!(criteria.selector))
474474
_target.delete_if do |doc|
475-
if doc._matches?(selector)
476-
unbind_one(doc) and true
475+
doc._matches?(selector).tap do |b|
476+
unbind_one(doc) if b
477477
end
478478
end
479479
removed

spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2395,6 +2395,10 @@
23952395
it "removes the ids from the foreign key" do
23962396
expect(person.preference_ids).to eq([ preference_two.id ])
23972397
end
2398+
2399+
it "sets the association locally" do
2400+
expect(person.preferences).to eq([preference_two])
2401+
end
23982402
end
23992403

24002404
context "when conditions are not provided" do
@@ -2421,6 +2425,10 @@
24212425
it "returns the number of documents deleted" do
24222426
expect(deleted).to eq(2)
24232427
end
2428+
2429+
it "sets the association locally" do
2430+
expect(person.preferences).to eq([])
2431+
end
24242432
end
24252433
end
24262434
end

spec/mongoid/association/referenced/has_many/proxy_spec.rb

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,10 +2239,8 @@
22392239
Person.create!(username: 'durran')
22402240
end
22412241

2242-
before do
2243-
person.posts.create!(title: "Testing")
2244-
person.posts.create!(title: "Test")
2245-
end
2242+
let!(:post1) { person.posts.create!(title: "Testing") }
2243+
let!(:post2) { person.posts.create!(title: "Test") }
22462244

22472245
it "removes the correct posts" do
22482246
person.posts.send(method, { title: "Testing" })
@@ -2258,6 +2256,11 @@
22582256
it "returns the number of documents deleted" do
22592257
expect(person.posts.send(method, { title: "Testing" })).to eq(1)
22602258
end
2259+
2260+
it "sets the association locally" do
2261+
person.posts.send(method, { title: "Testing" })
2262+
expect(person.posts).to eq([post2])
2263+
end
22612264
end
22622265

22632266
context "when conditions are not provided" do
@@ -2284,6 +2287,11 @@
22842287
it "returns the number of documents deleted" do
22852288
expect(person.posts.send(method)).to eq(2)
22862289
end
2290+
2291+
it "sets the association locally" do
2292+
person.posts.send(method)
2293+
expect(person.posts).to eq([])
2294+
end
22872295
end
22882296
end
22892297

@@ -2295,10 +2303,8 @@
22952303
Movie.create!(title: "Bladerunner")
22962304
end
22972305

2298-
before do
2299-
movie.ratings.create!(value: 1)
2300-
movie.ratings.create!(value: 2)
2301-
end
2306+
let!(:rating1) { movie.ratings.create!(value: 1) }
2307+
let!(:rating2) { movie.ratings.create!(value: 2) }
23022308

23032309
it "removes the correct ratings" do
23042310
movie.ratings.send(method, { value: 1 })
@@ -2313,6 +2319,11 @@
23132319
it "returns the number of documents deleted" do
23142320
expect(movie.ratings.send(method, { value: 1 })).to eq(1)
23152321
end
2322+
2323+
it "sets the association locally" do
2324+
movie.ratings.send(method, { value: 1 })
2325+
expect(movie.ratings).to eq([rating2])
2326+
end
23162327
end
23172328

23182329
context "when conditions are not provided" do
@@ -2339,6 +2350,11 @@
23392350
it "returns the number of documents deleted" do
23402351
expect(movie.ratings.send(method)).to eq(2)
23412352
end
2353+
2354+
it "sets the association locally" do
2355+
movie.ratings.send(method)
2356+
expect(movie.ratings).to eq([])
2357+
end
23422358
end
23432359
end
23442360
end

0 commit comments

Comments
 (0)