soft_deletion 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/lib/soft_deletion/core.rb +25 -20
- data/lib/soft_deletion/dependency.rb +2 -2
- data/lib/soft_deletion/version.rb +1 -1
- data/spec/soft_deletion_spec.rb +24 -12
- metadata +3 -3
data/Gemfile.lock
CHANGED
data/lib/soft_deletion/core.rb
CHANGED
@@ -98,43 +98,48 @@ module SoftDeletion
|
|
98
98
|
protected
|
99
99
|
|
100
100
|
def _run_soft_delete(&block)
|
101
|
+
result = false
|
102
|
+
internal = lambda do
|
103
|
+
mark_as_deleted
|
104
|
+
soft_delete_dependencies.each(&:soft_delete!)
|
105
|
+
result = block.call
|
106
|
+
end
|
107
|
+
|
101
108
|
self.class.transaction do
|
102
|
-
result = false
|
103
109
|
if ActiveRecord::VERSION::MAJOR > 2
|
104
|
-
run_callbacks :soft_delete
|
105
|
-
mark_as_deleted
|
106
|
-
soft_delete_dependencies.each(&:soft_delete!)
|
107
|
-
result = block.call
|
108
|
-
end
|
110
|
+
run_callbacks :soft_delete, &internal
|
109
111
|
else
|
110
112
|
return false if !run_callbacks(:before_soft_delete) { |result, object| result == false }
|
111
|
-
|
112
|
-
soft_delete_dependencies.each(&:soft_delete!)
|
113
|
-
result = block.call
|
113
|
+
internal.call
|
114
114
|
run_callbacks :after_soft_delete
|
115
115
|
end
|
116
|
-
result
|
117
116
|
end
|
117
|
+
|
118
|
+
result
|
118
119
|
end
|
119
120
|
|
120
121
|
def _run_soft_undelete(&block)
|
122
|
+
raise "#{self.class} is not deleted" unless deleted_at
|
123
|
+
|
124
|
+
result = false
|
125
|
+
limit = deleted_at - 1.hour
|
126
|
+
internal = lambda do
|
127
|
+
mark_as_undeleted
|
128
|
+
soft_delete_dependencies.each { |m| m.soft_undelete!(limit)}
|
129
|
+
result = block.call
|
130
|
+
end
|
131
|
+
|
121
132
|
self.class.transaction do
|
122
|
-
result = false
|
123
133
|
if ActiveRecord::VERSION::MAJOR > 2
|
124
|
-
run_callbacks :soft_undelete
|
125
|
-
mark_as_undeleted
|
126
|
-
soft_delete_dependencies.each(&:soft_undelete!)
|
127
|
-
result = block.call
|
128
|
-
end
|
134
|
+
run_callbacks :soft_undelete, &internal
|
129
135
|
else
|
130
136
|
return false if !run_callbacks(:before_soft_undelete) { |result, object| result == false }
|
131
|
-
|
132
|
-
soft_delete_dependencies.each(&:soft_undelete!)
|
133
|
-
result = block.call
|
137
|
+
internal.call
|
134
138
|
run_callbacks :after_soft_undelete
|
135
139
|
end
|
136
|
-
result
|
137
140
|
end
|
141
|
+
|
142
|
+
result
|
138
143
|
end
|
139
144
|
end
|
140
145
|
end
|
@@ -17,11 +17,11 @@ module SoftDeletion
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
def soft_undelete!
|
20
|
+
def soft_undelete!(limit)
|
21
21
|
return unless can_soft_delete?
|
22
22
|
|
23
23
|
klass.with_deleted do
|
24
|
-
dependencies.each(&:soft_undelete!)
|
24
|
+
dependencies.reject { |m| m.deleted_at < limit }.each(&:soft_undelete!)
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
data/spec/soft_deletion_spec.rb
CHANGED
@@ -133,7 +133,7 @@ describe SoftDeletion do
|
|
133
133
|
describe ".before_soft_undelete" do
|
134
134
|
it "is called on soft-undeletion" do
|
135
135
|
Category.before_soft_undelete :foo
|
136
|
-
category = Category.create!
|
136
|
+
category = Category.create!(:deleted_at => Time.now)
|
137
137
|
|
138
138
|
category.should_receive(:foo)
|
139
139
|
|
@@ -142,21 +142,21 @@ describe SoftDeletion do
|
|
142
142
|
|
143
143
|
it "stops execution chain if false is returned" do
|
144
144
|
Category.before_soft_undelete :foo, :bar
|
145
|
-
category = Category.create!
|
145
|
+
category = Category.create!(:deleted_at => Time.now)
|
146
146
|
|
147
147
|
category.should_receive(:foo).and_return(false)
|
148
148
|
category.should_not_receive(:bar)
|
149
149
|
|
150
150
|
category.soft_undelete!.should == false
|
151
151
|
category.reload
|
152
|
-
category.
|
152
|
+
category.should be_deleted
|
153
153
|
end
|
154
154
|
end
|
155
155
|
|
156
156
|
describe ".after_soft_undelete" do
|
157
157
|
it "is called after soft-undeletion" do
|
158
158
|
Category.after_soft_undelete :foo
|
159
|
-
category = Category.create!
|
159
|
+
category = Category.create!(:deleted_at => Time.now)
|
160
160
|
|
161
161
|
category.should_receive(:foo)
|
162
162
|
|
@@ -165,7 +165,7 @@ describe SoftDeletion do
|
|
165
165
|
|
166
166
|
it "is called with a block" do
|
167
167
|
Category.after_soft_undelete{|c| c.foo }
|
168
|
-
category = Category.create!
|
168
|
+
category = Category.create!(:deleted_at => Time.now)
|
169
169
|
|
170
170
|
category.should_receive(:foo)
|
171
171
|
|
@@ -174,7 +174,7 @@ describe SoftDeletion do
|
|
174
174
|
|
175
175
|
it "calls multiple after soft-undeletion" do
|
176
176
|
Category.after_soft_undelete :foo, :bar
|
177
|
-
category = Category.create!
|
177
|
+
category = Category.create!(:deleted_at => Time.now)
|
178
178
|
|
179
179
|
category.should_receive(:foo)
|
180
180
|
category.should_receive(:bar)
|
@@ -184,7 +184,7 @@ describe SoftDeletion do
|
|
184
184
|
|
185
185
|
it "does not stop undeletion when returning false" do
|
186
186
|
Category.after_soft_undelete :foo
|
187
|
-
category = Category.create!
|
187
|
+
category = Category.create!(:deleted_at => Time.now)
|
188
188
|
|
189
189
|
category.should_receive(:foo).and_return false
|
190
190
|
|
@@ -196,7 +196,7 @@ describe SoftDeletion do
|
|
196
196
|
|
197
197
|
it "is not called after normal destroy" do
|
198
198
|
Category.after_soft_undelete :foo
|
199
|
-
category = Category.create!
|
199
|
+
category = Category.create!(:deleted_at => Time.now)
|
200
200
|
|
201
201
|
category.should_not_receive(:foo)
|
202
202
|
|
@@ -286,23 +286,35 @@ describe SoftDeletion do
|
|
286
286
|
successfully_bulk_soft_deletes
|
287
287
|
|
288
288
|
context "being restored from soft deletion" do
|
289
|
-
|
290
|
-
@category.soft_delete!
|
289
|
+
def undelete!
|
291
290
|
Category.with_deleted do
|
292
291
|
@category.reload
|
293
292
|
@category.soft_undelete!
|
294
293
|
end
|
295
294
|
end
|
296
295
|
|
297
|
-
|
296
|
+
before do
|
297
|
+
@category.soft_delete!
|
298
|
+
Category.with_deleted { @category = Category.find(@category.id) }
|
299
|
+
end
|
300
|
+
|
301
|
+
it "does not mark itself as deleted" do
|
302
|
+
undelete!
|
298
303
|
@category.reload
|
299
304
|
@category.should_not be_deleted
|
300
305
|
end
|
301
306
|
|
302
|
-
it "
|
307
|
+
it "restores its dependent associations" do
|
308
|
+
undelete!
|
303
309
|
@forum.reload
|
304
310
|
@forum.should_not be_deleted
|
305
311
|
end
|
312
|
+
|
313
|
+
it "does not restore far previous deletions" do
|
314
|
+
@forum.update_attributes(:deleted_at => 1.year.ago)
|
315
|
+
undelete!
|
316
|
+
@forum.reload.should be_deleted
|
317
|
+
end
|
306
318
|
end
|
307
319
|
end
|
308
320
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: soft_deletion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -54,7 +54,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
54
54
|
version: '0'
|
55
55
|
segments:
|
56
56
|
- 0
|
57
|
-
hash:
|
57
|
+
hash: -2369399626209139476
|
58
58
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
59
|
none: false
|
60
60
|
requirements:
|
@@ -63,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
63
63
|
version: '0'
|
64
64
|
segments:
|
65
65
|
- 0
|
66
|
-
hash:
|
66
|
+
hash: -2369399626209139476
|
67
67
|
requirements: []
|
68
68
|
rubyforge_project:
|
69
69
|
rubygems_version: 1.8.25
|