acts_as_paranoid 0.6.3 → 0.7.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +80 -16
- data/README.md +130 -68
- data/lib/acts_as_paranoid/associations.rb +21 -17
- data/lib/acts_as_paranoid/core.rb +90 -78
- data/lib/acts_as_paranoid/relation.rb +2 -0
- data/lib/acts_as_paranoid/validations.rb +8 -74
- data/lib/acts_as_paranoid/version.rb +3 -1
- data/lib/acts_as_paranoid.rb +31 -30
- data/test/test_associations.rb +119 -43
- data/test/test_core.rb +119 -67
- data/test/test_default_scopes.rb +7 -5
- data/test/test_helper.rb +111 -65
- data/test/test_inheritance.rb +3 -1
- data/test/test_relations.rb +18 -10
- data/test/test_validations.rb +9 -7
- metadata +71 -32
- data/lib/acts_as_paranoid/preloader_association.rb +0 -16
- data/test/test_preloader_association.rb +0 -27
data/test/test_associations.rb
CHANGED
|
@@ -1,27 +1,34 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
2
4
|
|
|
3
5
|
class AssociationsTest < ParanoidBaseTest
|
|
4
|
-
def
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
paranoid_company_1.paranoid_products.create! :name => "ParanoidProduct #1"
|
|
8
|
-
paranoid_company_2.paranoid_products.create! :name => "ParanoidProduct #2"
|
|
6
|
+
def test_removal_with_destroy_associations
|
|
7
|
+
paranoid_company = ParanoidDestroyCompany.create! name: "ParanoidDestroyCompany #1"
|
|
8
|
+
paranoid_company.paranoid_products.create! name: "ParanoidProduct #1"
|
|
9
9
|
|
|
10
10
|
assert_equal 1, ParanoidDestroyCompany.count
|
|
11
|
-
assert_equal 1,
|
|
12
|
-
assert_equal 2, ParanoidProduct.count
|
|
11
|
+
assert_equal 1, ParanoidProduct.count
|
|
13
12
|
|
|
14
13
|
ParanoidDestroyCompany.first.destroy
|
|
15
14
|
assert_equal 0, ParanoidDestroyCompany.count
|
|
16
|
-
assert_equal
|
|
15
|
+
assert_equal 0, ParanoidProduct.count
|
|
17
16
|
assert_equal 1, ParanoidDestroyCompany.with_deleted.count
|
|
18
|
-
assert_equal
|
|
17
|
+
assert_equal 1, ParanoidProduct.with_deleted.count
|
|
19
18
|
|
|
20
19
|
ParanoidDestroyCompany.with_deleted.first.destroy
|
|
21
20
|
assert_equal 0, ParanoidDestroyCompany.count
|
|
22
|
-
assert_equal
|
|
21
|
+
assert_equal 0, ParanoidProduct.count
|
|
23
22
|
assert_equal 0, ParanoidDestroyCompany.with_deleted.count
|
|
24
|
-
assert_equal
|
|
23
|
+
assert_equal 0, ParanoidProduct.with_deleted.count
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def test_removal_with_delete_all_associations
|
|
27
|
+
paranoid_company = ParanoidDeleteCompany.create! name: "ParanoidDestroyCompany #1"
|
|
28
|
+
paranoid_company.paranoid_products.create! name: "ParanoidProduct #2"
|
|
29
|
+
|
|
30
|
+
assert_equal 1, ParanoidDeleteCompany.count
|
|
31
|
+
assert_equal 1, ParanoidProduct.count
|
|
25
32
|
|
|
26
33
|
ParanoidDeleteCompany.first.destroy
|
|
27
34
|
assert_equal 0, ParanoidDeleteCompany.count
|
|
@@ -38,17 +45,20 @@ class AssociationsTest < ParanoidBaseTest
|
|
|
38
45
|
|
|
39
46
|
def test_belongs_to_with_scope_option
|
|
40
47
|
paranoid_has_many_dependant = ParanoidHasManyDependant.new
|
|
41
|
-
includes_values = ParanoidTime.includes(:not_paranoid).includes_values
|
|
42
48
|
|
|
43
|
-
|
|
49
|
+
expected_includes_values = ParanoidTime.includes(:not_paranoid).includes_values
|
|
50
|
+
includes_values = paranoid_has_many_dependant
|
|
51
|
+
.association(:paranoid_time_with_scope).scope.includes_values
|
|
44
52
|
|
|
45
|
-
|
|
53
|
+
assert_equal expected_includes_values, includes_values
|
|
54
|
+
|
|
55
|
+
paranoid_time = ParanoidTime.create(name: "not-hello")
|
|
46
56
|
paranoid_has_many_dependant.paranoid_time = paranoid_time
|
|
47
57
|
paranoid_has_many_dependant.save!
|
|
48
58
|
|
|
49
59
|
assert_nil paranoid_has_many_dependant.paranoid_time_with_scope
|
|
50
60
|
|
|
51
|
-
paranoid_time.update(name:
|
|
61
|
+
paranoid_time.update(name: "hello")
|
|
52
62
|
|
|
53
63
|
paranoid_has_many_dependant.reload
|
|
54
64
|
|
|
@@ -68,26 +78,29 @@ class AssociationsTest < ParanoidBaseTest
|
|
|
68
78
|
assert_equal includes_values, paranoid_has_many_dependant
|
|
69
79
|
.association(:paranoid_time_with_scope_with_deleted).scope.includes_values
|
|
70
80
|
|
|
71
|
-
paranoid_time = ParanoidTime.create(name:
|
|
81
|
+
paranoid_time = ParanoidTime.create(name: "not-hello")
|
|
72
82
|
paranoid_has_many_dependant.paranoid_time = paranoid_time
|
|
73
83
|
paranoid_has_many_dependant.save!
|
|
74
84
|
|
|
75
85
|
assert_nil paranoid_has_many_dependant.paranoid_time_with_scope_with_deleted
|
|
76
86
|
|
|
77
|
-
paranoid_time.update(name:
|
|
87
|
+
paranoid_time.update(name: "hello")
|
|
78
88
|
paranoid_has_many_dependant.reload
|
|
79
89
|
|
|
80
|
-
assert_equal paranoid_time, paranoid_has_many_dependant
|
|
90
|
+
assert_equal paranoid_time, paranoid_has_many_dependant
|
|
91
|
+
.paranoid_time_with_scope_with_deleted
|
|
81
92
|
|
|
82
93
|
paranoid_time.destroy
|
|
83
94
|
paranoid_has_many_dependant.reload
|
|
84
95
|
|
|
85
|
-
assert_equal paranoid_time, paranoid_has_many_dependant
|
|
96
|
+
assert_equal paranoid_time, paranoid_has_many_dependant
|
|
97
|
+
.paranoid_time_with_scope_with_deleted
|
|
86
98
|
end
|
|
87
99
|
|
|
88
100
|
def test_belongs_to_with_deleted
|
|
89
101
|
paranoid_time = ParanoidTime.first
|
|
90
|
-
paranoid_has_many_dependant = paranoid_time.paranoid_has_many_dependants
|
|
102
|
+
paranoid_has_many_dependant = paranoid_time.paranoid_has_many_dependants
|
|
103
|
+
.create(name: "dependant!")
|
|
91
104
|
|
|
92
105
|
assert_equal paranoid_time, paranoid_has_many_dependant.paranoid_time
|
|
93
106
|
assert_equal paranoid_time, paranoid_has_many_dependant.paranoid_time_with_deleted
|
|
@@ -101,20 +114,25 @@ class AssociationsTest < ParanoidBaseTest
|
|
|
101
114
|
|
|
102
115
|
def test_belongs_to_polymorphic_with_deleted
|
|
103
116
|
paranoid_time = ParanoidTime.first
|
|
104
|
-
paranoid_has_many_dependant = ParanoidHasManyDependant
|
|
117
|
+
paranoid_has_many_dependant = ParanoidHasManyDependant
|
|
118
|
+
.create!(name: "dependant!", paranoid_time_polymorphic_with_deleted: paranoid_time)
|
|
105
119
|
|
|
106
120
|
assert_equal paranoid_time, paranoid_has_many_dependant.paranoid_time
|
|
107
|
-
assert_equal paranoid_time, paranoid_has_many_dependant
|
|
121
|
+
assert_equal paranoid_time, paranoid_has_many_dependant
|
|
122
|
+
.paranoid_time_polymorphic_with_deleted
|
|
108
123
|
|
|
109
124
|
paranoid_time.destroy
|
|
110
125
|
|
|
111
126
|
assert_nil paranoid_has_many_dependant.reload.paranoid_time
|
|
112
|
-
assert_equal paranoid_time, paranoid_has_many_dependant
|
|
127
|
+
assert_equal paranoid_time, paranoid_has_many_dependant
|
|
128
|
+
.reload.paranoid_time_polymorphic_with_deleted
|
|
113
129
|
end
|
|
114
130
|
|
|
115
131
|
def test_belongs_to_nil_polymorphic_with_deleted
|
|
116
132
|
paranoid_time = ParanoidTime.first
|
|
117
|
-
paranoid_has_many_dependant =
|
|
133
|
+
paranoid_has_many_dependant =
|
|
134
|
+
ParanoidHasManyDependant.create!(name: "dependant!",
|
|
135
|
+
paranoid_time_polymorphic_with_deleted: nil)
|
|
118
136
|
|
|
119
137
|
assert_nil paranoid_has_many_dependant.paranoid_time
|
|
120
138
|
assert_nil paranoid_has_many_dependant.paranoid_time_polymorphic_with_deleted
|
|
@@ -126,19 +144,23 @@ class AssociationsTest < ParanoidBaseTest
|
|
|
126
144
|
end
|
|
127
145
|
|
|
128
146
|
def test_belongs_to_options
|
|
129
|
-
paranoid_time = ParanoidHasManyDependant.reflections
|
|
147
|
+
paranoid_time = ParanoidHasManyDependant.reflections
|
|
148
|
+
.with_indifferent_access[:paranoid_time]
|
|
130
149
|
assert_equal :belongs_to, paranoid_time.macro
|
|
131
150
|
assert_nil paranoid_time.options[:with_deleted]
|
|
132
151
|
end
|
|
133
152
|
|
|
134
153
|
def test_belongs_to_with_deleted_options
|
|
135
|
-
paranoid_time_with_deleted =
|
|
154
|
+
paranoid_time_with_deleted =
|
|
155
|
+
ParanoidHasManyDependant.reflections
|
|
156
|
+
.with_indifferent_access[:paranoid_time_with_deleted]
|
|
136
157
|
assert_equal :belongs_to, paranoid_time_with_deleted.macro
|
|
137
158
|
assert paranoid_time_with_deleted.options[:with_deleted]
|
|
138
159
|
end
|
|
139
160
|
|
|
140
161
|
def test_belongs_to_polymorphic_with_deleted_options
|
|
141
|
-
paranoid_time_polymorphic_with_deleted = ParanoidHasManyDependant.reflections
|
|
162
|
+
paranoid_time_polymorphic_with_deleted = ParanoidHasManyDependant.reflections
|
|
163
|
+
.with_indifferent_access[:paranoid_time_polymorphic_with_deleted]
|
|
142
164
|
assert_equal :belongs_to, paranoid_time_polymorphic_with_deleted.macro
|
|
143
165
|
assert paranoid_time_polymorphic_with_deleted.options[:with_deleted]
|
|
144
166
|
end
|
|
@@ -161,6 +183,50 @@ class AssociationsTest < ParanoidBaseTest
|
|
|
161
183
|
assert_equal [child], parent.paranoid_has_many_dependants.with_deleted.to_a
|
|
162
184
|
end
|
|
163
185
|
|
|
186
|
+
def test_join_with_model_with_deleted
|
|
187
|
+
obj = ParanoidHasManyDependant.create(paranoid_time: ParanoidTime.create)
|
|
188
|
+
assert_not_nil obj.paranoid_time
|
|
189
|
+
assert_not_nil obj.paranoid_time_with_deleted
|
|
190
|
+
|
|
191
|
+
obj.paranoid_time.destroy
|
|
192
|
+
obj.reload
|
|
193
|
+
|
|
194
|
+
assert_nil obj.paranoid_time
|
|
195
|
+
assert_not_nil obj.paranoid_time_with_deleted
|
|
196
|
+
|
|
197
|
+
# Note that obj is destroyed because of dependent: :destroy in ParanoidTime
|
|
198
|
+
assert obj.destroyed?
|
|
199
|
+
|
|
200
|
+
assert_empty ParanoidHasManyDependant.with_deleted.joins(:paranoid_time)
|
|
201
|
+
assert_equal [obj],
|
|
202
|
+
ParanoidHasManyDependant.with_deleted.joins(:paranoid_time_with_deleted)
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def test_includes_with_deleted
|
|
206
|
+
paranoid_time = ParanoidTime.first
|
|
207
|
+
paranoid_time.paranoid_has_many_dependants.create(name: "dependant!")
|
|
208
|
+
|
|
209
|
+
paranoid_time.destroy
|
|
210
|
+
|
|
211
|
+
ParanoidHasManyDependant.with_deleted
|
|
212
|
+
.includes(:paranoid_time_with_deleted).each do |hasmany|
|
|
213
|
+
assert_not_nil hasmany.paranoid_time_with_deleted
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def test_includes_with_deleted_with_polymorphic_parent
|
|
218
|
+
not_paranoid_parent = NotParanoidHasManyAsParent.create(name: "not paranoid parent")
|
|
219
|
+
paranoid_parent = ParanoidHasManyAsParent.create(name: "paranoid parent")
|
|
220
|
+
ParanoidBelongsToPolymorphic.create(name: "belongs_to", parent: not_paranoid_parent)
|
|
221
|
+
ParanoidBelongsToPolymorphic.create(name: "belongs_to", parent: paranoid_parent)
|
|
222
|
+
|
|
223
|
+
paranoid_parent.destroy
|
|
224
|
+
|
|
225
|
+
ParanoidBelongsToPolymorphic.with_deleted.includes(:parent).each do |hasmany|
|
|
226
|
+
assert_not_nil hasmany.parent
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
|
|
164
230
|
def test_cannot_find_a_paranoid_deleted_many_many_association
|
|
165
231
|
left = ParanoidManyManyParentLeft.create
|
|
166
232
|
right = ParanoidManyManyParentRight.create
|
|
@@ -171,8 +237,10 @@ class AssociationsTest < ParanoidBaseTest
|
|
|
171
237
|
left.reload
|
|
172
238
|
|
|
173
239
|
assert_equal [], left.paranoid_many_many_children, "Linking objects not deleted"
|
|
174
|
-
assert_equal [], left.paranoid_many_many_parent_rights,
|
|
175
|
-
|
|
240
|
+
assert_equal [], left.paranoid_many_many_parent_rights,
|
|
241
|
+
"Associated objects not unlinked"
|
|
242
|
+
assert_equal right, ParanoidManyManyParentRight.find(right.id),
|
|
243
|
+
"Associated object deleted"
|
|
176
244
|
end
|
|
177
245
|
|
|
178
246
|
def test_cannot_find_a_paranoid_destroyed_many_many_association
|
|
@@ -185,11 +253,13 @@ class AssociationsTest < ParanoidBaseTest
|
|
|
185
253
|
left.reload
|
|
186
254
|
|
|
187
255
|
assert_equal [], left.paranoid_many_many_children, "Linking objects not deleted"
|
|
188
|
-
assert_equal [], left.paranoid_many_many_parent_rights,
|
|
189
|
-
|
|
256
|
+
assert_equal [], left.paranoid_many_many_parent_rights,
|
|
257
|
+
"Associated objects not unlinked"
|
|
258
|
+
assert_equal right, ParanoidManyManyParentRight.find(right.id),
|
|
259
|
+
"Associated object deleted"
|
|
190
260
|
end
|
|
191
261
|
|
|
192
|
-
def
|
|
262
|
+
def test_cannot_find_a_has_many_through_object_when_its_linking_object_is_soft_destroyed
|
|
193
263
|
left = ParanoidManyManyParentLeft.create
|
|
194
264
|
right = ParanoidManyManyParentRight.create
|
|
195
265
|
left.paranoid_many_many_parent_rights << right
|
|
@@ -218,8 +288,10 @@ class AssociationsTest < ParanoidBaseTest
|
|
|
218
288
|
left.paranoid_many_many_parent_rights << right
|
|
219
289
|
|
|
220
290
|
child = left.paranoid_many_many_children.first
|
|
221
|
-
assert_equal left, child.paranoid_many_many_parent_left,
|
|
222
|
-
|
|
291
|
+
assert_equal left, child.paranoid_many_many_parent_left,
|
|
292
|
+
"Child's left parent is incorrect"
|
|
293
|
+
assert_equal right, child.paranoid_many_many_parent_right,
|
|
294
|
+
"Child's right parent is incorrect"
|
|
223
295
|
|
|
224
296
|
left.paranoid_many_many_parent_rights.clear
|
|
225
297
|
|
|
@@ -232,8 +304,10 @@ class AssociationsTest < ParanoidBaseTest
|
|
|
232
304
|
left.paranoid_many_many_parent_rights << right
|
|
233
305
|
|
|
234
306
|
child = left.paranoid_many_many_children.first
|
|
235
|
-
assert_equal left, child.paranoid_many_many_parent_left,
|
|
236
|
-
|
|
307
|
+
assert_equal left, child.paranoid_many_many_parent_left,
|
|
308
|
+
"Child's left parent is incorrect"
|
|
309
|
+
assert_equal right, child.paranoid_many_many_parent_right,
|
|
310
|
+
"Child's right parent is incorrect"
|
|
237
311
|
|
|
238
312
|
left.paranoid_many_many_parent_rights.destroy(right)
|
|
239
313
|
|
|
@@ -246,8 +320,10 @@ class AssociationsTest < ParanoidBaseTest
|
|
|
246
320
|
left.paranoid_many_many_parent_rights << right
|
|
247
321
|
|
|
248
322
|
child = left.paranoid_many_many_children.first
|
|
249
|
-
assert_equal left, child.paranoid_many_many_parent_left,
|
|
250
|
-
|
|
323
|
+
assert_equal left, child.paranoid_many_many_parent_left,
|
|
324
|
+
"Child's left parent is incorrect"
|
|
325
|
+
assert_equal right, child.paranoid_many_many_parent_right,
|
|
326
|
+
"Child's right parent is incorrect"
|
|
251
327
|
|
|
252
328
|
left.paranoid_many_many_parent_rights.delete(right)
|
|
253
329
|
|
|
@@ -271,12 +347,12 @@ class AssociationsTest < ParanoidBaseTest
|
|
|
271
347
|
end
|
|
272
348
|
|
|
273
349
|
def test_mass_assignment_of_paranoid_column_enabled
|
|
274
|
-
if Gem.loaded_specs[
|
|
275
|
-
skip
|
|
350
|
+
if Gem.loaded_specs["activerecord"].version >= Gem::Version.new("5.2.0")
|
|
351
|
+
skip "Creation as deleted is not supported with Rails >= 5.2"
|
|
276
352
|
end
|
|
277
353
|
now = Time.now
|
|
278
|
-
record = ParanoidTime.create! :
|
|
279
|
-
assert_equal
|
|
354
|
+
record = ParanoidTime.create! name: "Foo", deleted_at: now
|
|
355
|
+
assert_equal "Foo", record.name
|
|
280
356
|
assert_equal now, record.deleted_at
|
|
281
357
|
end
|
|
282
358
|
end
|
data/test/test_core.rb
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
2
4
|
|
|
3
5
|
class ParanoidTest < ParanoidBaseTest
|
|
4
6
|
def test_paranoid?
|
|
@@ -103,19 +105,32 @@ class ParanoidTest < ParanoidBaseTest
|
|
|
103
105
|
|
|
104
106
|
def test_recovery!
|
|
105
107
|
ParanoidBoolean.first.destroy
|
|
106
|
-
ParanoidBoolean.create(:
|
|
108
|
+
ParanoidBoolean.create(name: "paranoid")
|
|
107
109
|
|
|
108
110
|
assert_raise do
|
|
109
111
|
ParanoidBoolean.only_deleted.first.recover!
|
|
110
112
|
end
|
|
111
113
|
end
|
|
112
114
|
|
|
115
|
+
# Rails does not allow saving deleted records
|
|
116
|
+
def test_no_save_after_destroy
|
|
117
|
+
paranoid = ParanoidString.first
|
|
118
|
+
paranoid.destroy
|
|
119
|
+
paranoid.name = "Let's update!"
|
|
120
|
+
|
|
121
|
+
assert_not paranoid.save
|
|
122
|
+
assert_raises ActiveRecord::RecordNotSaved do
|
|
123
|
+
paranoid.save!
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
113
127
|
def setup_recursive_tests
|
|
114
128
|
@paranoid_time_object = ParanoidTime.first
|
|
115
129
|
|
|
116
130
|
# Create one extra ParanoidHasManyDependant record so that we can validate
|
|
117
131
|
# the correct dependants are recovered.
|
|
118
|
-
ParanoidTime.where(
|
|
132
|
+
ParanoidTime.where("id <> ?", @paranoid_time_object.id).first
|
|
133
|
+
.paranoid_has_many_dependants.create(name: "should not be recovered").destroy
|
|
119
134
|
|
|
120
135
|
@paranoid_boolean_count = ParanoidBoolean.count
|
|
121
136
|
|
|
@@ -124,20 +139,21 @@ class ParanoidTest < ParanoidBaseTest
|
|
|
124
139
|
assert_equal 1, NotParanoid.count
|
|
125
140
|
|
|
126
141
|
(1..3).each do |i|
|
|
127
|
-
has_many_object = @paranoid_time_object.paranoid_has_many_dependants
|
|
128
|
-
|
|
142
|
+
has_many_object = @paranoid_time_object.paranoid_has_many_dependants
|
|
143
|
+
.create(name: "has_many_#{i}")
|
|
144
|
+
has_many_object.create_paranoid_belongs_dependant(name: "belongs_to_#{i}")
|
|
129
145
|
has_many_object.save
|
|
130
146
|
|
|
131
|
-
paranoid_boolean = @paranoid_time_object.paranoid_booleans
|
|
132
|
-
|
|
147
|
+
paranoid_boolean = @paranoid_time_object.paranoid_booleans
|
|
148
|
+
.create(name: "boolean_#{i}")
|
|
149
|
+
paranoid_boolean.create_paranoid_has_one_dependant(name: "has_one_#{i}")
|
|
133
150
|
paranoid_boolean.save
|
|
134
151
|
|
|
135
|
-
@paranoid_time_object.not_paranoids.create(:
|
|
136
|
-
|
|
152
|
+
@paranoid_time_object.not_paranoids.create(name: "not_paranoid_a#{i}")
|
|
137
153
|
end
|
|
138
154
|
|
|
139
|
-
@paranoid_time_object.create_not_paranoid(:
|
|
140
|
-
@paranoid_time_object.create_has_one_not_paranoid(:
|
|
155
|
+
@paranoid_time_object.create_not_paranoid(name: "not_paranoid_belongs_to")
|
|
156
|
+
@paranoid_time_object.create_has_one_not_paranoid(name: "has_one_not_paranoid")
|
|
141
157
|
|
|
142
158
|
assert_equal 3, ParanoidTime.count
|
|
143
159
|
assert_equal 3, ParanoidHasManyDependant.count
|
|
@@ -188,7 +204,7 @@ class ParanoidTest < ParanoidBaseTest
|
|
|
188
204
|
@paranoid_time_object.destroy
|
|
189
205
|
@paranoid_time_object.reload
|
|
190
206
|
|
|
191
|
-
@paranoid_time_object.recover(:
|
|
207
|
+
@paranoid_time_object.recover(recursive: true)
|
|
192
208
|
|
|
193
209
|
assert_equal 3, ParanoidTime.count
|
|
194
210
|
assert_equal 3, ParanoidHasManyDependant.count
|
|
@@ -209,14 +225,14 @@ class ParanoidTest < ParanoidBaseTest
|
|
|
209
225
|
@paranoid_time_object.paranoid_has_many_dependants.first.destroy
|
|
210
226
|
end
|
|
211
227
|
Time.stub :now, 1.hour.ago do
|
|
212
|
-
@paranoid_time_object.paranoid_has_many_dependants
|
|
213
|
-
last.paranoid_belongs_dependant
|
|
214
|
-
destroy
|
|
228
|
+
@paranoid_time_object.paranoid_has_many_dependants
|
|
229
|
+
.last.paranoid_belongs_dependant
|
|
230
|
+
.destroy
|
|
215
231
|
end
|
|
216
232
|
@paranoid_time_object.destroy
|
|
217
233
|
@paranoid_time_object.reload
|
|
218
234
|
|
|
219
|
-
@paranoid_time_object.recover(:
|
|
235
|
+
@paranoid_time_object.recover(recursive: true)
|
|
220
236
|
|
|
221
237
|
assert_equal 3, ParanoidTime.count
|
|
222
238
|
assert_equal 2, ParanoidHasManyDependant.count
|
|
@@ -229,17 +245,17 @@ class ParanoidTest < ParanoidBaseTest
|
|
|
229
245
|
|
|
230
246
|
def test_recursive_recovery_for_belongs_to_polymorphic
|
|
231
247
|
child_1 = ParanoidAndroid.create
|
|
232
|
-
section_1 = ParanoidSection.create(:
|
|
248
|
+
section_1 = ParanoidSection.create(paranoid_thing: child_1)
|
|
233
249
|
|
|
234
|
-
child_2 = ParanoidPolygon.create(:
|
|
235
|
-
section_2 = ParanoidSection.create(:
|
|
250
|
+
child_2 = ParanoidPolygon.create(sides: 3)
|
|
251
|
+
section_2 = ParanoidSection.create(paranoid_thing: child_2)
|
|
236
252
|
|
|
237
253
|
assert_equal section_1.paranoid_thing, child_1
|
|
238
254
|
assert_equal section_1.paranoid_thing.class, ParanoidAndroid
|
|
239
255
|
assert_equal section_2.paranoid_thing, child_2
|
|
240
256
|
assert_equal section_2.paranoid_thing.class, ParanoidPolygon
|
|
241
257
|
|
|
242
|
-
parent = ParanoidTime.create(:
|
|
258
|
+
parent = ParanoidTime.create(name: "paranoid_parent")
|
|
243
259
|
parent.paranoid_sections << section_1
|
|
244
260
|
parent.paranoid_sections << section_2
|
|
245
261
|
|
|
@@ -270,7 +286,7 @@ class ParanoidTest < ParanoidBaseTest
|
|
|
270
286
|
@paranoid_time_object.destroy
|
|
271
287
|
@paranoid_time_object.reload
|
|
272
288
|
|
|
273
|
-
@paranoid_time_object.recover(:
|
|
289
|
+
@paranoid_time_object.recover(recursive: false)
|
|
274
290
|
|
|
275
291
|
assert_equal 3, ParanoidTime.count
|
|
276
292
|
assert_equal 0, ParanoidHasManyDependant.count
|
|
@@ -439,104 +455,108 @@ class ParanoidTest < ParanoidBaseTest
|
|
|
439
455
|
|
|
440
456
|
# Test string type columns that don't have a nil value when not deleted (Y/N for example)
|
|
441
457
|
def test_string_type_with_no_nil_value_before_destroy
|
|
442
|
-
ps = ParanoidString.create!(:
|
|
443
|
-
assert_equal 1, ParanoidString.where(:
|
|
458
|
+
ps = ParanoidString.create!(deleted: "not dead")
|
|
459
|
+
assert_equal 1, ParanoidString.where(id: ps).count
|
|
444
460
|
end
|
|
445
461
|
|
|
446
462
|
def test_string_type_with_no_nil_value_after_destroy
|
|
447
|
-
ps = ParanoidString.create!(:
|
|
463
|
+
ps = ParanoidString.create!(deleted: "not dead")
|
|
448
464
|
ps.destroy
|
|
449
|
-
assert_equal 0, ParanoidString.where(:
|
|
465
|
+
assert_equal 0, ParanoidString.where(id: ps).count
|
|
450
466
|
end
|
|
451
467
|
|
|
452
468
|
def test_string_type_with_no_nil_value_before_destroy_with_deleted
|
|
453
|
-
ps = ParanoidString.create!(:
|
|
454
|
-
assert_equal 1, ParanoidString.with_deleted.where(:
|
|
469
|
+
ps = ParanoidString.create!(deleted: "not dead")
|
|
470
|
+
assert_equal 1, ParanoidString.with_deleted.where(id: ps).count
|
|
455
471
|
end
|
|
456
472
|
|
|
457
473
|
def test_string_type_with_no_nil_value_after_destroy_with_deleted
|
|
458
|
-
ps = ParanoidString.create!(:
|
|
474
|
+
ps = ParanoidString.create!(deleted: "not dead")
|
|
459
475
|
ps.destroy
|
|
460
|
-
assert_equal 1, ParanoidString.with_deleted.where(:
|
|
476
|
+
assert_equal 1, ParanoidString.with_deleted.where(id: ps).count
|
|
461
477
|
end
|
|
462
478
|
|
|
463
479
|
def test_string_type_with_no_nil_value_before_destroy_only_deleted
|
|
464
|
-
ps = ParanoidString.create!(:
|
|
465
|
-
assert_equal 0, ParanoidString.only_deleted.where(:
|
|
480
|
+
ps = ParanoidString.create!(deleted: "not dead")
|
|
481
|
+
assert_equal 0, ParanoidString.only_deleted.where(id: ps).count
|
|
466
482
|
end
|
|
467
483
|
|
|
468
484
|
def test_string_type_with_no_nil_value_after_destroy_only_deleted
|
|
469
|
-
ps = ParanoidString.create!(:
|
|
485
|
+
ps = ParanoidString.create!(deleted: "not dead")
|
|
470
486
|
ps.destroy
|
|
471
|
-
assert_equal 1, ParanoidString.only_deleted.where(:
|
|
487
|
+
assert_equal 1, ParanoidString.only_deleted.where(id: ps).count
|
|
472
488
|
end
|
|
473
489
|
|
|
474
490
|
def test_string_type_with_no_nil_value_after_destroyed_twice
|
|
475
|
-
ps = ParanoidString.create!(:
|
|
491
|
+
ps = ParanoidString.create!(deleted: "not dead")
|
|
476
492
|
2.times { ps.destroy }
|
|
477
|
-
assert_equal 0, ParanoidString.with_deleted.where(:
|
|
493
|
+
assert_equal 0, ParanoidString.with_deleted.where(id: ps).count
|
|
478
494
|
end
|
|
479
495
|
|
|
480
496
|
# Test boolean type columns, that are not nullable
|
|
481
497
|
def test_boolean_type_with_no_nil_value_before_destroy
|
|
482
|
-
ps = ParanoidBooleanNotNullable.create!
|
|
483
|
-
assert_equal 1, ParanoidBooleanNotNullable.where(:
|
|
498
|
+
ps = ParanoidBooleanNotNullable.create!
|
|
499
|
+
assert_equal 1, ParanoidBooleanNotNullable.where(id: ps).count
|
|
484
500
|
end
|
|
485
501
|
|
|
486
502
|
def test_boolean_type_with_no_nil_value_after_destroy
|
|
487
|
-
ps = ParanoidBooleanNotNullable.create!
|
|
503
|
+
ps = ParanoidBooleanNotNullable.create!
|
|
488
504
|
ps.destroy
|
|
489
|
-
assert_equal 0, ParanoidBooleanNotNullable.where(:
|
|
505
|
+
assert_equal 0, ParanoidBooleanNotNullable.where(id: ps).count
|
|
490
506
|
end
|
|
491
507
|
|
|
492
508
|
def test_boolean_type_with_no_nil_value_before_destroy_with_deleted
|
|
493
|
-
ps = ParanoidBooleanNotNullable.create!
|
|
494
|
-
assert_equal 1, ParanoidBooleanNotNullable.with_deleted.where(:
|
|
509
|
+
ps = ParanoidBooleanNotNullable.create!
|
|
510
|
+
assert_equal 1, ParanoidBooleanNotNullable.with_deleted.where(id: ps).count
|
|
495
511
|
end
|
|
496
512
|
|
|
497
513
|
def test_boolean_type_with_no_nil_value_after_destroy_with_deleted
|
|
498
|
-
ps = ParanoidBooleanNotNullable.create!
|
|
514
|
+
ps = ParanoidBooleanNotNullable.create!
|
|
499
515
|
ps.destroy
|
|
500
|
-
assert_equal 1, ParanoidBooleanNotNullable.with_deleted.where(:
|
|
516
|
+
assert_equal 1, ParanoidBooleanNotNullable.with_deleted.where(id: ps).count
|
|
501
517
|
end
|
|
502
518
|
|
|
503
519
|
def test_boolean_type_with_no_nil_value_before_destroy_only_deleted
|
|
504
|
-
ps = ParanoidBooleanNotNullable.create!
|
|
505
|
-
assert_equal 0, ParanoidBooleanNotNullable.only_deleted.where(:
|
|
520
|
+
ps = ParanoidBooleanNotNullable.create!
|
|
521
|
+
assert_equal 0, ParanoidBooleanNotNullable.only_deleted.where(id: ps).count
|
|
506
522
|
end
|
|
507
523
|
|
|
508
524
|
def test_boolean_type_with_no_nil_value_after_destroy_only_deleted
|
|
509
|
-
ps = ParanoidBooleanNotNullable.create!
|
|
525
|
+
ps = ParanoidBooleanNotNullable.create!
|
|
510
526
|
ps.destroy
|
|
511
|
-
assert_equal 1, ParanoidBooleanNotNullable.only_deleted.where(:
|
|
527
|
+
assert_equal 1, ParanoidBooleanNotNullable.only_deleted.where(id: ps).count
|
|
512
528
|
end
|
|
513
529
|
|
|
514
530
|
def test_boolean_type_with_no_nil_value_after_destroyed_twice
|
|
515
|
-
ps = ParanoidBooleanNotNullable.create!
|
|
531
|
+
ps = ParanoidBooleanNotNullable.create!
|
|
516
532
|
2.times { ps.destroy }
|
|
517
|
-
assert_equal 0, ParanoidBooleanNotNullable.with_deleted.where(:
|
|
533
|
+
assert_equal 0, ParanoidBooleanNotNullable.with_deleted.where(id: ps).count
|
|
518
534
|
end
|
|
519
535
|
|
|
520
536
|
def test_no_double_tap_destroys_fully
|
|
521
|
-
ps = ParanoidNoDoubleTapDestroysFully.create!
|
|
537
|
+
ps = ParanoidNoDoubleTapDestroysFully.create!
|
|
522
538
|
2.times { ps.destroy }
|
|
523
|
-
assert_equal 1, ParanoidNoDoubleTapDestroysFully.with_deleted.where(:
|
|
539
|
+
assert_equal 1, ParanoidNoDoubleTapDestroysFully.with_deleted.where(id: ps).count
|
|
524
540
|
end
|
|
525
541
|
|
|
526
|
-
def
|
|
527
|
-
paranoid_boolean = ParanoidBoolean.create!
|
|
528
|
-
paranoid_with_counter_cache = ParanoidWithCounterCache
|
|
542
|
+
def test_decrement_counters_without_touch
|
|
543
|
+
paranoid_boolean = ParanoidBoolean.create!
|
|
544
|
+
paranoid_with_counter_cache = ParanoidWithCounterCache
|
|
545
|
+
.create!(paranoid_boolean: paranoid_boolean)
|
|
529
546
|
|
|
530
547
|
assert_equal 1, paranoid_boolean.paranoid_with_counter_caches_count
|
|
548
|
+
updated_at = paranoid_boolean.reload.updated_at
|
|
531
549
|
|
|
532
550
|
paranoid_with_counter_cache.destroy
|
|
533
551
|
|
|
534
552
|
assert_equal 0, paranoid_boolean.reload.paranoid_with_counter_caches_count
|
|
553
|
+
assert_equal updated_at, paranoid_boolean.reload.updated_at
|
|
535
554
|
end
|
|
536
555
|
|
|
537
556
|
def test_decrement_custom_counters
|
|
538
|
-
paranoid_boolean = ParanoidBoolean.create!
|
|
539
|
-
paranoid_with_custom_counter_cache = ParanoidWithCustomCounterCache
|
|
557
|
+
paranoid_boolean = ParanoidBoolean.create!
|
|
558
|
+
paranoid_with_custom_counter_cache = ParanoidWithCustomCounterCache
|
|
559
|
+
.create!(paranoid_boolean: paranoid_boolean)
|
|
540
560
|
|
|
541
561
|
assert_equal 1, paranoid_boolean.custom_counter_cache
|
|
542
562
|
|
|
@@ -545,15 +565,43 @@ class ParanoidTest < ParanoidBaseTest
|
|
|
545
565
|
assert_equal 0, paranoid_boolean.reload.custom_counter_cache
|
|
546
566
|
end
|
|
547
567
|
|
|
568
|
+
def test_decrement_counters_with_touch
|
|
569
|
+
paranoid_boolean = ParanoidBoolean.create!
|
|
570
|
+
paranoid_with_counter_cache = ParanoidWithTouchAndCounterCache
|
|
571
|
+
.create!(paranoid_boolean: paranoid_boolean)
|
|
572
|
+
|
|
573
|
+
assert_equal 1, paranoid_boolean.paranoid_with_touch_and_counter_caches_count
|
|
574
|
+
updated_at = paranoid_boolean.reload.updated_at
|
|
575
|
+
|
|
576
|
+
paranoid_with_counter_cache.destroy
|
|
577
|
+
|
|
578
|
+
assert_equal 0, paranoid_boolean.reload.paranoid_with_touch_and_counter_caches_count
|
|
579
|
+
assert_not_equal updated_at, paranoid_boolean.reload.updated_at
|
|
580
|
+
end
|
|
581
|
+
|
|
582
|
+
def test_touch_belongs_to
|
|
583
|
+
paranoid_boolean = ParanoidBoolean.create!
|
|
584
|
+
paranoid_with_counter_cache = ParanoidWithTouch
|
|
585
|
+
.create!(paranoid_boolean: paranoid_boolean)
|
|
586
|
+
|
|
587
|
+
updated_at = paranoid_boolean.reload.updated_at
|
|
588
|
+
|
|
589
|
+
paranoid_with_counter_cache.destroy
|
|
590
|
+
|
|
591
|
+
assert_not_equal updated_at, paranoid_boolean.reload.updated_at
|
|
592
|
+
end
|
|
593
|
+
|
|
548
594
|
def test_destroy_with_optional_belongs_to_and_counter_cache
|
|
549
|
-
ps = ParanoidWithCounterCacheOnOptionalBelognsTo.create!
|
|
595
|
+
ps = ParanoidWithCounterCacheOnOptionalBelognsTo.create!
|
|
550
596
|
ps.destroy
|
|
551
|
-
assert_equal 1, ParanoidWithCounterCacheOnOptionalBelognsTo.only_deleted
|
|
597
|
+
assert_equal 1, ParanoidWithCounterCacheOnOptionalBelognsTo.only_deleted
|
|
598
|
+
.where(id: ps).count
|
|
552
599
|
end
|
|
553
600
|
|
|
554
601
|
def test_hard_destroy_decrement_counters
|
|
555
|
-
paranoid_boolean = ParanoidBoolean.create!
|
|
556
|
-
paranoid_with_counter_cache = ParanoidWithCounterCache
|
|
602
|
+
paranoid_boolean = ParanoidBoolean.create!
|
|
603
|
+
paranoid_with_counter_cache = ParanoidWithCounterCache
|
|
604
|
+
.create!(paranoid_boolean: paranoid_boolean)
|
|
557
605
|
|
|
558
606
|
assert_equal 1, paranoid_boolean.paranoid_with_counter_caches_count
|
|
559
607
|
|
|
@@ -563,8 +611,9 @@ class ParanoidTest < ParanoidBaseTest
|
|
|
563
611
|
end
|
|
564
612
|
|
|
565
613
|
def test_hard_destroy_decrement_custom_counters
|
|
566
|
-
paranoid_boolean = ParanoidBoolean.create!
|
|
567
|
-
paranoid_with_custom_counter_cache = ParanoidWithCustomCounterCache
|
|
614
|
+
paranoid_boolean = ParanoidBoolean.create!
|
|
615
|
+
paranoid_with_custom_counter_cache = ParanoidWithCustomCounterCache
|
|
616
|
+
.create!(paranoid_boolean: paranoid_boolean)
|
|
568
617
|
|
|
569
618
|
assert_equal 1, paranoid_boolean.custom_counter_cache
|
|
570
619
|
|
|
@@ -574,8 +623,9 @@ class ParanoidTest < ParanoidBaseTest
|
|
|
574
623
|
end
|
|
575
624
|
|
|
576
625
|
def test_increment_counters
|
|
577
|
-
paranoid_boolean = ParanoidBoolean.create!
|
|
578
|
-
paranoid_with_counter_cache = ParanoidWithCounterCache
|
|
626
|
+
paranoid_boolean = ParanoidBoolean.create!
|
|
627
|
+
paranoid_with_counter_cache = ParanoidWithCounterCache
|
|
628
|
+
.create!(paranoid_boolean: paranoid_boolean)
|
|
579
629
|
|
|
580
630
|
assert_equal 1, paranoid_boolean.paranoid_with_counter_caches_count
|
|
581
631
|
|
|
@@ -589,8 +639,9 @@ class ParanoidTest < ParanoidBaseTest
|
|
|
589
639
|
end
|
|
590
640
|
|
|
591
641
|
def test_increment_custom_counters
|
|
592
|
-
paranoid_boolean = ParanoidBoolean.create!
|
|
593
|
-
paranoid_with_custom_counter_cache = ParanoidWithCustomCounterCache
|
|
642
|
+
paranoid_boolean = ParanoidBoolean.create!
|
|
643
|
+
paranoid_with_custom_counter_cache = ParanoidWithCustomCounterCache
|
|
644
|
+
.create!(paranoid_boolean: paranoid_boolean)
|
|
594
645
|
|
|
595
646
|
assert_equal 1, paranoid_boolean.custom_counter_cache
|
|
596
647
|
|
|
@@ -604,6 +655,7 @@ class ParanoidTest < ParanoidBaseTest
|
|
|
604
655
|
end
|
|
605
656
|
|
|
606
657
|
def test_explicitly_setting_table_name_after_acts_as_paranoid_macro
|
|
607
|
-
assert_equal "explicit_table.deleted_at", ParanoidWithExplicitTableNameAfterMacro
|
|
658
|
+
assert_equal "explicit_table.deleted_at", ParanoidWithExplicitTableNameAfterMacro
|
|
659
|
+
.paranoid_column_reference
|
|
608
660
|
end
|
|
609
661
|
end
|