soft_deletion 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile.lock +1 -1
- data/MIT-LICENSE +20 -0
- data/Rakefile +1 -1
- data/Readme.md +9 -2
- data/gemfiles/rails2.gemfile.lock +1 -1
- data/gemfiles/rails3.gemfile.lock +1 -1
- data/lib/soft_deletion.rb +25 -4
- data/lib/soft_deletion/version.rb +1 -1
- data/test/soft_deletion_test.rb +55 -0
- metadata +5 -4
data/Gemfile.lock
CHANGED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) NOW-PRESENT Zendesk
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
CHANGED
data/Readme.md
CHANGED
@@ -38,15 +38,22 @@ Usage
|
|
38
38
|
user.soft_undelete!
|
39
39
|
user.products.count == 10
|
40
40
|
|
41
|
+
# soft delete many
|
42
|
+
User.soft_delete_all!(1,2,3,4)
|
43
|
+
|
41
44
|
|
42
45
|
TODO
|
43
46
|
====
|
44
47
|
- Rails 3 from with inspiration from https://github.com/JackDanger/permanent_records/blob/master/lib/permanent_records.rb
|
45
48
|
- maybe stuff from https://github.com/smoku/soft_delete
|
46
49
|
|
50
|
+
Authors
|
51
|
+
=======
|
52
|
+
|
53
|
+
### [Contributors](https://github.com/grosser/soft_deletion/contributors)
|
54
|
+
- [Michel Pigassou](https://github.com/Dagnan)
|
55
|
+
- [Steven Davidovitz](https://github.com/steved555)
|
47
56
|
|
48
|
-
Author
|
49
|
-
======
|
50
57
|
[Zendesk](http://zendesk.com)<br/>
|
51
58
|
michael@grosser.it<br/>
|
52
59
|
License: MIT<br/>
|
data/lib/soft_deletion.rb
CHANGED
@@ -44,6 +44,27 @@ module SoftDeletion
|
|
44
44
|
yield self
|
45
45
|
end
|
46
46
|
end
|
47
|
+
|
48
|
+
def soft_delete_all!(ids_or_models)
|
49
|
+
ids_or_models = Array.wrap(ids_or_models)
|
50
|
+
|
51
|
+
if ids_or_models.first.is_a?(ActiveRecord::Base)
|
52
|
+
ids = ids_or_models.map(&:id)
|
53
|
+
models = ids_or_models
|
54
|
+
else
|
55
|
+
ids = ids_or_models
|
56
|
+
models = all(:conditions => { :id => ids })
|
57
|
+
end
|
58
|
+
|
59
|
+
transaction do
|
60
|
+
update_all(["deleted_at = ?", Time.now], :id => ids)
|
61
|
+
|
62
|
+
models.each do |model|
|
63
|
+
model.soft_delete_dependencies.each(&:soft_delete!)
|
64
|
+
model.run_callbacks ActiveRecord::VERSION::MAJOR > 2 ? :soft_delete : :after_soft_delete
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
47
68
|
end
|
48
69
|
|
49
70
|
def deleted?
|
@@ -79,6 +100,10 @@ module SoftDeletion
|
|
79
100
|
end
|
80
101
|
end
|
81
102
|
|
103
|
+
def soft_delete_dependencies
|
104
|
+
self.class.soft_delete_dependents.map { |dependent| Dependency.new(self, dependent) }
|
105
|
+
end
|
106
|
+
|
82
107
|
protected
|
83
108
|
|
84
109
|
def _soft_delete!
|
@@ -86,8 +111,4 @@ module SoftDeletion
|
|
86
111
|
soft_delete_dependencies.each(&:soft_delete!)
|
87
112
|
save!
|
88
113
|
end
|
89
|
-
|
90
|
-
def soft_delete_dependencies
|
91
|
-
self.class.soft_delete_dependents.map { |dependent| Dependency.new(self, dependent) }
|
92
|
-
end
|
93
114
|
end
|
data/test/soft_deletion_test.rb
CHANGED
@@ -115,6 +115,22 @@ class SoftDeletionTest < ActiveSupport::TestCase
|
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
118
|
+
def self.successfully_bulk_soft_deletes
|
119
|
+
context 'successfully bulk soft deleted' do
|
120
|
+
setup do
|
121
|
+
Category.soft_delete_all!(@category)
|
122
|
+
end
|
123
|
+
|
124
|
+
should 'mark itself as deleted' do
|
125
|
+
assert_deleted @category
|
126
|
+
end
|
127
|
+
|
128
|
+
should 'soft delete its dependent associations' do
|
129
|
+
assert_deleted @forum
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
118
134
|
setup do
|
119
135
|
clear_callbacks Category, :soft_delete
|
120
136
|
end
|
@@ -127,6 +143,13 @@ class SoftDeletionTest < ActiveSupport::TestCase
|
|
127
143
|
category.soft_delete!
|
128
144
|
end
|
129
145
|
|
146
|
+
should "be called after bulk soft-deletion" do
|
147
|
+
Category.after_soft_delete :foo
|
148
|
+
category = Category.create!
|
149
|
+
category.expects(:foo)
|
150
|
+
Category.soft_delete_all!(category)
|
151
|
+
end
|
152
|
+
|
130
153
|
should "be call multiple after soft-deletion" do
|
131
154
|
Category.after_soft_delete :foo, :bar
|
132
155
|
category = Category.create!
|
@@ -167,6 +190,7 @@ class SoftDeletionTest < ActiveSupport::TestCase
|
|
167
190
|
end
|
168
191
|
|
169
192
|
successfully_soft_deletes
|
193
|
+
successfully_bulk_soft_deletes
|
170
194
|
end
|
171
195
|
|
172
196
|
context 'with dependent has_many associations' do
|
@@ -191,6 +215,7 @@ class SoftDeletionTest < ActiveSupport::TestCase
|
|
191
215
|
end
|
192
216
|
|
193
217
|
successfully_soft_deletes
|
218
|
+
successfully_bulk_soft_deletes
|
194
219
|
|
195
220
|
context 'being restored from soft deletion' do
|
196
221
|
setup do
|
@@ -229,4 +254,34 @@ class SoftDeletionTest < ActiveSupport::TestCase
|
|
229
254
|
end
|
230
255
|
end
|
231
256
|
end
|
257
|
+
|
258
|
+
context "bulk soft deletion" do
|
259
|
+
setup do
|
260
|
+
@categories = 2.times.map { Category.create! }
|
261
|
+
end
|
262
|
+
|
263
|
+
context "by id" do
|
264
|
+
setup do
|
265
|
+
Category.soft_delete_all!(@categories.map(&:id))
|
266
|
+
end
|
267
|
+
|
268
|
+
should "delete all models" do
|
269
|
+
@categories.each do |category|
|
270
|
+
assert_deleted category
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
context "by model" do
|
276
|
+
setup do
|
277
|
+
Category.soft_delete_all!(@categories)
|
278
|
+
end
|
279
|
+
|
280
|
+
should "delete all models" do
|
281
|
+
@categories.each do |category|
|
282
|
+
assert_deleted category
|
283
|
+
end
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|
232
287
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: soft_deletion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 5
|
10
|
+
version: 0.1.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Zendesk
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-06-
|
18
|
+
date: 2012-06-26 00:00:00 Z
|
19
19
|
dependencies: []
|
20
20
|
|
21
21
|
description:
|
@@ -31,6 +31,7 @@ files:
|
|
31
31
|
- Appraisals
|
32
32
|
- Gemfile
|
33
33
|
- Gemfile.lock
|
34
|
+
- MIT-LICENSE
|
34
35
|
- Rakefile
|
35
36
|
- Readme.md
|
36
37
|
- gemfiles/rails2.gemfile
|