soft_deletion 0.1.9 → 0.1.10
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +17 -10
- data/gemfiles/rails2.gemfile.lock +1 -1
- data/gemfiles/rails3.gemfile.lock +1 -1
- data/lib/soft_deletion/version.rb +1 -1
- data/lib/soft_deletion.rb +5 -5
- data/test/soft_deletion_test.rb +66 -26
- data/test/test_helper.rb +7 -0
- metadata +22 -35
data/Gemfile.lock
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
soft_deletion (0.1.
|
4
|
+
soft_deletion (0.1.10)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: http://rubygems.org/
|
8
8
|
specs:
|
9
|
-
activemodel (3.2.
|
10
|
-
activesupport (= 3.2.
|
9
|
+
activemodel (3.2.7)
|
10
|
+
activesupport (= 3.2.7)
|
11
11
|
builder (~> 3.0.0)
|
12
|
-
activerecord (3.2.
|
13
|
-
activemodel (= 3.2.
|
14
|
-
activesupport (= 3.2.
|
12
|
+
activerecord (3.2.7)
|
13
|
+
activemodel (= 3.2.7)
|
14
|
+
activesupport (= 3.2.7)
|
15
15
|
arel (~> 3.0.2)
|
16
16
|
tzinfo (~> 0.3.29)
|
17
|
-
activesupport (3.2.
|
17
|
+
activesupport (3.2.7)
|
18
18
|
i18n (~> 0.6)
|
19
19
|
multi_json (~> 1.0)
|
20
20
|
appraisal (0.4.1)
|
@@ -23,10 +23,17 @@ GEM
|
|
23
23
|
arel (3.0.2)
|
24
24
|
builder (3.0.0)
|
25
25
|
i18n (0.6.0)
|
26
|
-
|
27
|
-
|
26
|
+
metaclass (0.0.1)
|
27
|
+
mocha (0.12.3)
|
28
|
+
metaclass (~> 0.0.1)
|
29
|
+
multi_json (1.3.6)
|
28
30
|
rake (0.9.2.2)
|
29
|
-
shoulda (
|
31
|
+
shoulda (3.1.1)
|
32
|
+
shoulda-context (~> 1.0)
|
33
|
+
shoulda-matchers (~> 1.2)
|
34
|
+
shoulda-context (1.0.0)
|
35
|
+
shoulda-matchers (1.2.0)
|
36
|
+
activesupport (>= 3.0.0)
|
30
37
|
sqlite3 (1.3.6)
|
31
38
|
test-unit (2.2.0)
|
32
39
|
tzinfo (0.3.33)
|
data/lib/soft_deletion.rb
CHANGED
@@ -90,9 +90,7 @@ module SoftDeletion
|
|
90
90
|
end
|
91
91
|
|
92
92
|
def soft_delete(*args)
|
93
|
-
_run_soft_delete
|
94
|
-
return false unless save(*args)
|
95
|
-
end
|
93
|
+
_run_soft_delete{ save(*args) }
|
96
94
|
end
|
97
95
|
|
98
96
|
def soft_undelete!
|
@@ -111,19 +109,21 @@ module SoftDeletion
|
|
111
109
|
|
112
110
|
def _run_soft_delete(&block)
|
113
111
|
self.class.transaction do
|
112
|
+
result = nil
|
114
113
|
if ActiveRecord::VERSION::MAJOR > 2
|
115
114
|
run_callbacks :soft_delete do
|
116
115
|
mark_as_deleted
|
117
116
|
soft_delete_dependencies.each(&:soft_delete!)
|
118
|
-
block.call
|
117
|
+
result = block.call
|
119
118
|
end
|
120
119
|
else
|
121
120
|
run_callbacks :before_soft_delete
|
122
121
|
mark_as_deleted
|
123
122
|
soft_delete_dependencies.each(&:soft_delete!)
|
124
|
-
block.call
|
123
|
+
result = block.call
|
125
124
|
run_callbacks :after_soft_delete
|
126
125
|
end
|
126
|
+
result
|
127
127
|
end
|
128
128
|
end
|
129
129
|
end
|
data/test/soft_deletion_test.rb
CHANGED
@@ -14,32 +14,32 @@ class SoftDeletionTest < ActiveSupport::TestCase
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def self.successfully_soft_deletes
|
17
|
-
context
|
17
|
+
context "successfully soft deleted" do
|
18
18
|
setup do
|
19
19
|
@category.soft_delete!
|
20
20
|
end
|
21
21
|
|
22
|
-
should
|
22
|
+
should "mark itself as deleted" do
|
23
23
|
assert_deleted @category
|
24
24
|
end
|
25
25
|
|
26
|
-
should
|
26
|
+
should "soft delete its dependent associations" do
|
27
27
|
assert_deleted @forum
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
32
|
def self.successfully_bulk_soft_deletes
|
33
|
-
context
|
33
|
+
context "successfully bulk soft deleted" do
|
34
34
|
setup do
|
35
35
|
Category.soft_delete_all!(@category)
|
36
36
|
end
|
37
37
|
|
38
|
-
should
|
38
|
+
should "mark itself as deleted" do
|
39
39
|
assert_deleted @category
|
40
40
|
end
|
41
41
|
|
42
|
-
should
|
42
|
+
should "soft delete its dependent associations" do
|
43
43
|
assert_deleted @forum
|
44
44
|
end
|
45
45
|
end
|
@@ -89,16 +89,16 @@ class SoftDeletionTest < ActiveSupport::TestCase
|
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
92
|
-
context
|
93
|
-
should
|
92
|
+
context "without dependent associations" do
|
93
|
+
should "only soft-delete itself" do
|
94
94
|
category = NACategory.create!
|
95
95
|
category.soft_delete!
|
96
96
|
assert_deleted category
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
100
|
-
context
|
101
|
-
should
|
100
|
+
context "with independent associations" do
|
101
|
+
should "not delete associations" do
|
102
102
|
category = IDACategory.create!
|
103
103
|
forum = category.forums.create!
|
104
104
|
category.soft_delete!
|
@@ -106,7 +106,7 @@ class SoftDeletionTest < ActiveSupport::TestCase
|
|
106
106
|
end
|
107
107
|
end
|
108
108
|
|
109
|
-
context
|
109
|
+
context "with dependent has_one association" do
|
110
110
|
setup do
|
111
111
|
@category = HOACategory.create!
|
112
112
|
@forum = @category.create_forum
|
@@ -122,38 +122,38 @@ class SoftDeletionTest < ActiveSupport::TestCase
|
|
122
122
|
@forum = @category.destroyable_forums.create!
|
123
123
|
end
|
124
124
|
|
125
|
-
context
|
125
|
+
context "successfully soft deleted" do
|
126
126
|
setup do
|
127
127
|
@category.soft_delete!
|
128
128
|
end
|
129
129
|
|
130
|
-
should
|
130
|
+
should "mark itself as deleted" do
|
131
131
|
assert_deleted @category
|
132
132
|
end
|
133
133
|
|
134
|
-
should
|
134
|
+
should "not destroy dependent association" do
|
135
135
|
assert DestroyableForum.exists?(@forum.id)
|
136
136
|
end
|
137
137
|
end
|
138
138
|
end
|
139
139
|
|
140
|
-
context
|
140
|
+
context "with dependent has_many associations" do
|
141
141
|
setup do
|
142
142
|
@category = Category.create!
|
143
143
|
@forum = @category.forums.create!
|
144
144
|
end
|
145
145
|
|
146
|
-
context
|
146
|
+
context "failing to soft delete" do
|
147
147
|
setup do
|
148
148
|
@category.stubs(:valid?).returns(false)
|
149
149
|
assert_raise(ActiveRecord::RecordInvalid) { @category.soft_delete! }
|
150
150
|
end
|
151
151
|
|
152
|
-
should
|
152
|
+
should "not mark itself as deleted" do
|
153
153
|
assert_not_deleted @category
|
154
154
|
end
|
155
155
|
|
156
|
-
should
|
156
|
+
should "not soft delete its dependent associations" do
|
157
157
|
assert_not_deleted @forum
|
158
158
|
end
|
159
159
|
end
|
@@ -161,7 +161,7 @@ class SoftDeletionTest < ActiveSupport::TestCase
|
|
161
161
|
successfully_soft_deletes
|
162
162
|
successfully_bulk_soft_deletes
|
163
163
|
|
164
|
-
context
|
164
|
+
context "being restored from soft deletion" do
|
165
165
|
setup do
|
166
166
|
@category.soft_delete!
|
167
167
|
Category.with_deleted do
|
@@ -171,18 +171,18 @@ class SoftDeletionTest < ActiveSupport::TestCase
|
|
171
171
|
end
|
172
172
|
end
|
173
173
|
|
174
|
-
should
|
174
|
+
should "not mark itself as deleted" do
|
175
175
|
assert_not_deleted @category
|
176
176
|
end
|
177
177
|
|
178
|
-
should
|
178
|
+
should "restore its dependent associations" do
|
179
179
|
assert_not_deleted @forum
|
180
180
|
end
|
181
181
|
end
|
182
182
|
end
|
183
183
|
|
184
|
-
context
|
185
|
-
should
|
184
|
+
context "a soft-deleted has-many category that nullifies forum references on delete" do
|
185
|
+
should "nullify those references" do
|
186
186
|
category = NDACategory.create!
|
187
187
|
forum = category.forums.create!
|
188
188
|
category.soft_delete!
|
@@ -191,15 +191,15 @@ class SoftDeletionTest < ActiveSupport::TestCase
|
|
191
191
|
end
|
192
192
|
end
|
193
193
|
|
194
|
-
context
|
195
|
-
should
|
194
|
+
context "without deleted_at column" do
|
195
|
+
should "default scope should not provoke an error" do
|
196
196
|
assert_nothing_raised do
|
197
197
|
OriginalCategory.create!
|
198
198
|
end
|
199
199
|
end
|
200
200
|
end
|
201
201
|
|
202
|
-
context "
|
202
|
+
context ".soft_delete_all!" do
|
203
203
|
setup do
|
204
204
|
@categories = 2.times.map { Category.create! }
|
205
205
|
end
|
@@ -243,4 +243,44 @@ class SoftDeletionTest < ActiveSupport::TestCase
|
|
243
243
|
assert_nil Cat1Forum.find_by_id(forum.id)
|
244
244
|
end
|
245
245
|
end
|
246
|
+
|
247
|
+
context "validations" do
|
248
|
+
should "fail when validations fail" do
|
249
|
+
forum = ValidatedForum.create!(:category_id => 1)
|
250
|
+
forum.category_id = nil
|
251
|
+
assert_raise ActiveRecord::RecordInvalid do
|
252
|
+
forum.soft_delete!
|
253
|
+
end
|
254
|
+
assert_not_deleted forum
|
255
|
+
end
|
256
|
+
|
257
|
+
should "pass when validations pass" do
|
258
|
+
forum = ValidatedForum.create!(:category_id => 1)
|
259
|
+
forum.soft_delete!
|
260
|
+
assert_deleted forum
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
context "#soft_delete" do
|
265
|
+
should "return true if it succeeds" do
|
266
|
+
forum = ValidatedForum.create!(:category_id => 1)
|
267
|
+
assert_equal true, forum.soft_delete
|
268
|
+
assert_deleted forum
|
269
|
+
end
|
270
|
+
|
271
|
+
should "return false if validations fail" do
|
272
|
+
forum = ValidatedForum.create!(:category_id => 1)
|
273
|
+
forum.category_id = nil
|
274
|
+
assert_equal false, forum.soft_delete
|
275
|
+
assert_not_deleted forum
|
276
|
+
end
|
277
|
+
|
278
|
+
should "return true if validations are prevented and it succeeds" do
|
279
|
+
forum = ValidatedForum.create!(:category_id => 1)
|
280
|
+
forum.category_id = nil
|
281
|
+
skip_validations = (ActiveRecord::VERSION::MAJOR == 2 ? false : {:validate => false})
|
282
|
+
assert_equal true, forum.soft_delete(skip_validations)
|
283
|
+
assert_deleted forum
|
284
|
+
end
|
285
|
+
end
|
246
286
|
end
|
data/test/test_helper.rb
CHANGED
@@ -54,6 +54,13 @@ class Forum < ActiveRecord::Base
|
|
54
54
|
belongs_to :category
|
55
55
|
end
|
56
56
|
|
57
|
+
class ValidatedForum < ActiveRecord::Base
|
58
|
+
silent_set_table_name 'forums'
|
59
|
+
include SoftDeletion
|
60
|
+
belongs_to :category
|
61
|
+
validates_presence_of :category_id
|
62
|
+
end
|
63
|
+
|
57
64
|
class Category < ActiveRecord::Base
|
58
65
|
include SoftDeletion
|
59
66
|
has_many :forums, :dependent => :destroy
|
metadata
CHANGED
@@ -1,32 +1,22 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: soft_deletion
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.10
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 9
|
10
|
-
version: 0.1.9
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Zendesk
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
date: 2012-06-29 00:00:00 Z
|
12
|
+
date: 2012-08-07 00:00:00.000000000 Z
|
19
13
|
dependencies: []
|
20
|
-
|
21
14
|
description:
|
22
15
|
email: michael@grosser.it
|
23
16
|
executables: []
|
24
|
-
|
25
17
|
extensions: []
|
26
|
-
|
27
18
|
extra_rdoc_files: []
|
28
|
-
|
29
|
-
files:
|
19
|
+
files:
|
30
20
|
- .travis.yml
|
31
21
|
- Appraisals
|
32
22
|
- Gemfile
|
@@ -45,37 +35,34 @@ files:
|
|
45
35
|
- test/soft_deletion_test.rb
|
46
36
|
- test/test_helper.rb
|
47
37
|
homepage: http://github.com/grosser/soft_deletion
|
48
|
-
licenses:
|
38
|
+
licenses:
|
49
39
|
- MIT
|
50
40
|
post_install_message:
|
51
41
|
rdoc_options: []
|
52
|
-
|
53
|
-
require_paths:
|
42
|
+
require_paths:
|
54
43
|
- lib
|
55
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
45
|
none: false
|
57
|
-
requirements:
|
58
|
-
- -
|
59
|
-
- !ruby/object:Gem::Version
|
60
|
-
|
61
|
-
segments:
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
segments:
|
62
51
|
- 0
|
63
|
-
|
64
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
hash: -1755831677353429354
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
54
|
none: false
|
66
|
-
requirements:
|
67
|
-
- -
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
|
70
|
-
segments:
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
segments:
|
71
60
|
- 0
|
72
|
-
|
61
|
+
hash: -1755831677353429354
|
73
62
|
requirements: []
|
74
|
-
|
75
63
|
rubyforge_project:
|
76
64
|
rubygems_version: 1.8.24
|
77
65
|
signing_key:
|
78
66
|
specification_version: 3
|
79
67
|
summary: Explicit soft deletion for ActiveRecord via deleted_at and default scope.
|
80
68
|
test_files: []
|
81
|
-
|