Skip to content

Commit b0a8fc7

Browse files
* Support only Mongoid 7.3 and later. (#68)
* In conformity with Mongoid 7.3+ behavior, dependencies are not destroyed when calling delete/delete! Co-authored-by: shields <shields@tablecheck.com>
1 parent 6e7bc3d commit b0a8fc7

6 files changed

Lines changed: 35 additions & 40 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.5.0 (2021/07/24)
2+
3+
* Support only Mongoid 7.3 and later.
4+
* In conformity with Mongoid 7.3+ behavior, dependencies are not destroyed when calling delete/delete!
5+
16
## 0.4.1 (2021/07/24)
27

38
* Do not allow Mongoid 7.3 and later, due to breaking changes

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
source 'https://rubygems.org'
44
gemspec name: 'mongoid_paranoia'
55

6-
gem 'mongoid', '~> 7.0'
6+
gem 'mongoid', '~> 7.3'
77

88
group :test do
99
gem 'rspec', '~> 3.8'

lib/mongoid/paranoia.rb

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,6 @@ def configure
5050
define_model_callbacks :remove
5151
end
5252

53-
# Delete the paranoid +Document+ from the database completely. This will
54-
# run the destroy callbacks.
55-
#
56-
# @example Hard destroy the document.
57-
# document.destroy!
58-
#
59-
# @return [ true, false ] If the operation succeeded.
60-
#
61-
# @since 1.0.0
62-
def destroy!
63-
run_callbacks(:destroy) do
64-
run_callbacks(:remove) do
65-
delete!
66-
end
67-
end
68-
end
69-
7053
# Override the persisted method to allow for the paranoia gem.
7154
# If a paranoid record is selected, then we only want to check
7255
# if it's a new record, not if it is "destroyed"
@@ -92,34 +75,41 @@ def persisted?
9275
# @return [ true ] True.
9376
#
9477
# @since 1.0.0
95-
alias orig_remove :remove
78+
alias :orig_delete :delete
9679

9780
def remove(_ = {})
98-
return false unless catch(:abort) do
99-
if respond_to?(:apply_destroy_dependencies!)
100-
apply_destroy_dependencies!
101-
else
102-
apply_delete_dependencies!
103-
end
104-
end
10581
time = self.deleted_at = Time.now
10682
_paranoia_update('$set' => { paranoid_field => time })
10783
@destroyed = true
10884
true
10985
end
11086

111-
alias delete :remove
87+
alias :delete :remove
88+
alias :delete! :orig_delete
11289

113-
# Delete the paranoid +Document+ from the database completely.
90+
# Delete the paranoid +Document+ from the database completely. This will
91+
# run the destroy and remove callbacks.
11492
#
115-
# @example Hard delete the document.
116-
# document.delete!
93+
# @example Hard destroy the document.
94+
# document.destroy!
11795
#
11896
# @return [ true, false ] If the operation succeeded.
11997
#
12098
# @since 1.0.0
121-
def delete!
122-
orig_remove
99+
def destroy!(options = {})
100+
raise Errors::ReadonlyDocument.new(self.class) if readonly?
101+
self.flagged_for_destroy = true
102+
result = run_callbacks(:destroy) do
103+
run_callbacks(:remove) do
104+
if catch(:abort) { apply_destroy_dependencies! }
105+
delete!(options || {})
106+
else
107+
false
108+
end
109+
end
110+
end
111+
self.flagged_for_destroy = false
112+
result
123113
end
124114

125115
# Determines if this document is destroyed.

lib/mongoid/paranoia/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module Mongoid
44
module Paranoia
5-
VERSION = '0.4.1'
5+
VERSION = '0.5.0'
66
end
77
end

mongoid_paranoia.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Gem::Specification.new do |gem|
1717
gem.test_files = Dir.glob('{perf,spec}/**/*')
1818
gem.require_paths = ['lib']
1919

20-
gem.add_dependency 'mongoid', '>= 7.0', '< 7.3'
20+
gem.add_dependency 'mongoid', '~> 7.3'
2121

2222
gem.add_development_dependency 'rubocop', '>= 1.8.1'
2323
end

spec/mongoid/paranoia_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -531,10 +531,10 @@ class ParanoidConfiguredReset
531531
post.delete!
532532
end
533533

534-
it "cascades the dependent option" do
534+
it "does not cascade the dependent option" do
535535
expect {
536536
author.reload
537-
}.to raise_error(Mongoid::Errors::DocumentNotFound)
537+
}.to_not raise_error(Mongoid::Errors::DocumentNotFound)
538538
end
539539
end
540540
end
@@ -613,10 +613,10 @@ class ParanoidConfiguredReset
613613
post.delete
614614
end
615615

616-
it "cascades the dependent option" do
616+
it "does not cascade the dependent option" do
617617
expect {
618618
author.reload
619-
}.to raise_error(Mongoid::Errors::DocumentNotFound)
619+
}.to_not raise_error(Mongoid::Errors::DocumentNotFound)
620620
end
621621
end
622622

@@ -637,8 +637,8 @@ class ParanoidConfiguredReset
637637
end
638638
end
639639

640-
it "does not destroy the document" do
641-
expect(post).not_to be_destroyed
640+
it "ignores restrict and destroys the document" do
641+
expect(post).to be_destroyed
642642
end
643643
end
644644
end

0 commit comments

Comments
 (0)