Skip to content

Commit e97101d

Browse files
[READY FOR REVIEW] MONGOID-5571 Fix options arg in #destroy! being ignored + specs (#5552)
* Fix options args in #destroy! being ignored. * Add specs for #delete, #destroy, and #destroy :persist and :suppress options * Update code docs * rspec expect
1 parent 0a1d647 commit e97101d

File tree

4 files changed

+316
-32
lines changed

4 files changed

+316
-32
lines changed

lib/mongoid/persistable/deletable.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ module Deletable
1212
# @example Remove the document.
1313
# document.remove
1414
#
15-
# @param [ Hash ] options Options to pass to remove.
15+
# @param [ Hash ] options The options.
16+
# @option options [ true | false ] :persist Whether to persist
17+
# the delete action.
18+
# @option options [ true | false ] :suppress Whether to update
19+
# the parent document in-memory when deleting an embedded document.
1620
#
1721
# @return [ TrueClass ] True.
1822
def delete(options = {})

lib/mongoid/persistable/destroyable.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ module Destroyable
1212
# @example Destroy a document.
1313
# document.destroy
1414
#
15-
# @param [ Hash ] options Options to pass to destroy.
15+
# @param [ Hash ] options The options.
16+
# @option options [ true | false ] :persist Whether to persist
17+
# the delete action. Callbacks will still be run even if false.
18+
# @option options [ true | false ] :suppress Whether to update
19+
# the parent document in-memory when deleting an embedded document.
1620
#
1721
# @return [ true | false ] True if successful, false if not.
1822
def destroy(options = nil)
@@ -35,8 +39,21 @@ def destroy(options = nil)
3539
result
3640
end
3741

42+
# Remove the document from the database with callbacks. Raises
43+
# an error if the document is not destroyed.
44+
#
45+
# @example Destroy a document.
46+
# document.destroy!
47+
#
48+
# @param [ Hash ] options The options.
49+
# @option options [ true | false ] :persist Whether to persist
50+
# the delete action. Callbacks will still be run even if false.
51+
# @option options [ true | false ] :suppress Whether to update
52+
# the parent document in-memory when deleting an embedded document.
53+
#
54+
# @return [ true ] Always true.
3855
def destroy!(options = {})
39-
destroy || raise(Errors::DocumentNotDestroyed.new(_id, self.class))
56+
destroy(options) || raise(Errors::DocumentNotDestroyed.new(_id, self.class))
4057
end
4158

4259
module ClassMethods

spec/mongoid/persistable/deletable_spec.rb

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
end
5858
end
5959

60-
context "when removing a root document" do
60+
context "when deleting a root document" do
6161

6262
let!(:deleted) do
6363
person.delete
@@ -76,9 +76,28 @@
7676
it "resets the flagged for destroy flag" do
7777
expect(person).to_not be_flagged_for_destroy
7878
end
79+
80+
context 'when :persist option false' do
81+
82+
let!(:deleted) do
83+
person.delete(persist: false)
84+
end
85+
86+
it "does not delete the document from the collection" do
87+
expect(Person.find(person.id)).to eq person
88+
end
89+
90+
it "returns true" do
91+
expect(deleted).to be true
92+
end
93+
94+
it "does not set the flagged for destroy flag" do
95+
expect(person).to_not be_flagged_for_destroy
96+
end
97+
end
7998
end
8099

81-
context "when removing an embedded document" do
100+
context "when deleting an embedded document" do
82101

83102
let(:address) do
84103
person.addresses.build(street: "Bond Street")
@@ -114,13 +133,60 @@
114133
Person.find(person.id)
115134
end
116135

117-
it "removes the object from the parent and database" do
136+
it "removes the object from the parent" do
137+
expect(person.addresses).to be_empty
138+
end
139+
140+
it "removes the object from the database" do
141+
expect(from_db.addresses).to be_empty
142+
end
143+
end
144+
145+
context 'when :persist option false' do
146+
147+
before do
148+
address.save!
149+
address.delete(persist: false)
150+
end
151+
152+
let(:from_db) do
153+
Person.find(person.id)
154+
end
155+
156+
it "does not remove the object from the parent" do
157+
expect(person.addresses).to eq [address]
158+
expect(person.addresses.first).to_not be_flagged_for_destroy
159+
end
160+
161+
it "does not remove the object from the database" do
162+
expect(from_db.addresses).to eq [address]
163+
expect(from_db.addresses.first).to_not be_flagged_for_destroy
164+
end
165+
end
166+
167+
context 'when :suppress option true' do
168+
169+
before do
170+
address.save!
171+
address.delete(suppress: true)
172+
end
173+
174+
let(:from_db) do
175+
Person.find(person.id)
176+
end
177+
178+
it "does not remove the object from the parent" do
179+
expect(person.addresses).to eq [address]
180+
expect(person.addresses.first).to_not be_flagged_for_destroy
181+
end
182+
183+
it "removes the object from the database" do
118184
expect(from_db.addresses).to be_empty
119185
end
120186
end
121187
end
122188

123-
context "when removing deeply embedded documents" do
189+
context "when deleting deeply embedded documents" do
124190

125191
context "when the document has been saved" do
126192

0 commit comments

Comments
 (0)