acts_as_paranoid 0.6.3 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +69 -16
- data/README.md +130 -68
- data/lib/acts_as_paranoid.rb +31 -30
- data/lib/acts_as_paranoid/associations.rb +21 -17
- data/lib/acts_as_paranoid/core.rb +87 -77
- 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/test/test_associations.rb +119 -43
- data/test/test_core.rb +90 -66
- data/test/test_default_scopes.rb +7 -5
- data/test/test_helper.rb +87 -65
- data/test/test_inheritance.rb +3 -1
- data/test/test_relations.rb +18 -10
- data/test/test_validations.rb +9 -7
- metadata +57 -26
- data/lib/acts_as_paranoid/preloader_association.rb +0 -16
- data/test/test_preloader_association.rb +0 -27
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/array/wrap"
|
2
4
|
|
3
5
|
module ActsAsParanoid
|
4
6
|
module Validations
|
@@ -6,85 +8,17 @@ module ActsAsParanoid
|
|
6
8
|
base.extend ClassMethods
|
7
9
|
end
|
8
10
|
|
9
|
-
class UniquenessWithoutDeletedValidator
|
10
|
-
|
11
|
-
name = "V#{version.to_s.tr('.', '_')}"
|
12
|
-
unless constants.include? name.to_sym
|
13
|
-
raise "Unknown validator version #{name.inspect}; expected one of #{constants.sort.join(', ')}"
|
14
|
-
end
|
15
|
-
const_get name
|
16
|
-
end
|
17
|
-
|
18
|
-
class V5 < ActiveRecord::Validations::UniquenessValidator
|
19
|
-
def validate_each(record, attribute, value)
|
20
|
-
finder_class = find_finder_class_for(record)
|
21
|
-
table = finder_class.arel_table
|
22
|
-
|
23
|
-
relation = build_relation(finder_class, attribute, value)
|
24
|
-
[Array(finder_class.primary_key), Array(record.send(:id))].transpose.each do |pk_key, pk_value|
|
25
|
-
relation = relation.where(table[pk_key.to_sym].not_eq(pk_value))
|
26
|
-
end if record.persisted?
|
27
|
-
|
28
|
-
Array.wrap(options[:scope]).each do |scope_item|
|
29
|
-
relation = relation.where(table[scope_item].eq(record.public_send(scope_item)))
|
30
|
-
end
|
31
|
-
|
32
|
-
if relation.where(finder_class.paranoid_default_scope).exists?(relation)
|
33
|
-
record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
protected
|
38
|
-
|
39
|
-
def build_relation(klass, attribute, value)
|
40
|
-
if ActiveRecord::VERSION::MINOR == 0 && ActiveRecord::VERSION::MAJOR == 5
|
41
|
-
return super(klass, klass.arel_table, attribute, value)
|
42
|
-
else
|
43
|
-
super
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
class V4 < ActiveRecord::Validations::UniquenessValidator
|
49
|
-
def validate_each(record, attribute, value)
|
50
|
-
finder_class = find_finder_class_for(record)
|
51
|
-
table = finder_class.arel_table
|
52
|
-
|
53
|
-
# TODO: Use record.class.column_types[attribute.to_s].coder ?
|
54
|
-
coder = record.class.column_types[attribute.to_s]
|
55
|
-
|
56
|
-
if value && coder
|
57
|
-
value = if coder.respond_to? :type_cast_for_database
|
58
|
-
coder.type_cast_for_database value
|
59
|
-
else
|
60
|
-
coder.type_cast_for_write value
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
relation = build_relation(finder_class, table, attribute, value)
|
65
|
-
[Array(finder_class.primary_key), Array(record.send(:id))].transpose.each do |pk_key, pk_value|
|
66
|
-
relation = relation.and(table[pk_key.to_sym].not_eq(pk_value))
|
67
|
-
end if record.persisted?
|
68
|
-
|
69
|
-
Array.wrap(options[:scope]).each do |scope_item|
|
70
|
-
scope_value = record.send(scope_item)
|
71
|
-
relation = relation.and(table[scope_item].eq(scope_value))
|
72
|
-
end
|
73
|
-
|
74
|
-
# Re-add ActsAsParanoid default scope conditions manually.
|
75
|
-
if finder_class.unscoped.where(finder_class.paranoid_default_scope).where(relation).exists?
|
76
|
-
record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
11
|
+
class UniquenessWithoutDeletedValidator < ActiveRecord::Validations::UniquenessValidator
|
12
|
+
private
|
80
13
|
|
81
|
-
|
14
|
+
def build_relation(klass, attribute, value)
|
15
|
+
super.where(klass.paranoid_default_scope)
|
82
16
|
end
|
83
17
|
end
|
84
18
|
|
85
19
|
module ClassMethods
|
86
20
|
def validates_uniqueness_of_without_deleted(*attr_names)
|
87
|
-
validates_with UniquenessWithoutDeletedValidator
|
21
|
+
validates_with UniquenessWithoutDeletedValidator, _merge_attributes(attr_names)
|
88
22
|
end
|
89
23
|
end
|
90
24
|
end
|
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,93 +455,94 @@ 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
542
|
def test_decrement_counters
|
527
|
-
paranoid_boolean = ParanoidBoolean.create!
|
528
|
-
paranoid_with_counter_cache = ParanoidWithCounterCache
|
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
|
531
548
|
|
@@ -535,8 +552,9 @@ class ParanoidTest < ParanoidBaseTest
|
|
535
552
|
end
|
536
553
|
|
537
554
|
def test_decrement_custom_counters
|
538
|
-
paranoid_boolean = ParanoidBoolean.create!
|
539
|
-
paranoid_with_custom_counter_cache = ParanoidWithCustomCounterCache
|
555
|
+
paranoid_boolean = ParanoidBoolean.create!
|
556
|
+
paranoid_with_custom_counter_cache = ParanoidWithCustomCounterCache
|
557
|
+
.create!(paranoid_boolean: paranoid_boolean)
|
540
558
|
|
541
559
|
assert_equal 1, paranoid_boolean.custom_counter_cache
|
542
560
|
|
@@ -546,14 +564,16 @@ class ParanoidTest < ParanoidBaseTest
|
|
546
564
|
end
|
547
565
|
|
548
566
|
def test_destroy_with_optional_belongs_to_and_counter_cache
|
549
|
-
ps = ParanoidWithCounterCacheOnOptionalBelognsTo.create!
|
567
|
+
ps = ParanoidWithCounterCacheOnOptionalBelognsTo.create!
|
550
568
|
ps.destroy
|
551
|
-
assert_equal 1, ParanoidWithCounterCacheOnOptionalBelognsTo.only_deleted
|
569
|
+
assert_equal 1, ParanoidWithCounterCacheOnOptionalBelognsTo.only_deleted
|
570
|
+
.where(id: ps).count
|
552
571
|
end
|
553
572
|
|
554
573
|
def test_hard_destroy_decrement_counters
|
555
|
-
paranoid_boolean = ParanoidBoolean.create!
|
556
|
-
paranoid_with_counter_cache = ParanoidWithCounterCache
|
574
|
+
paranoid_boolean = ParanoidBoolean.create!
|
575
|
+
paranoid_with_counter_cache = ParanoidWithCounterCache
|
576
|
+
.create!(paranoid_boolean: paranoid_boolean)
|
557
577
|
|
558
578
|
assert_equal 1, paranoid_boolean.paranoid_with_counter_caches_count
|
559
579
|
|
@@ -563,8 +583,9 @@ class ParanoidTest < ParanoidBaseTest
|
|
563
583
|
end
|
564
584
|
|
565
585
|
def test_hard_destroy_decrement_custom_counters
|
566
|
-
paranoid_boolean = ParanoidBoolean.create!
|
567
|
-
paranoid_with_custom_counter_cache = ParanoidWithCustomCounterCache
|
586
|
+
paranoid_boolean = ParanoidBoolean.create!
|
587
|
+
paranoid_with_custom_counter_cache = ParanoidWithCustomCounterCache
|
588
|
+
.create!(paranoid_boolean: paranoid_boolean)
|
568
589
|
|
569
590
|
assert_equal 1, paranoid_boolean.custom_counter_cache
|
570
591
|
|
@@ -574,8 +595,9 @@ class ParanoidTest < ParanoidBaseTest
|
|
574
595
|
end
|
575
596
|
|
576
597
|
def test_increment_counters
|
577
|
-
paranoid_boolean = ParanoidBoolean.create!
|
578
|
-
paranoid_with_counter_cache = ParanoidWithCounterCache
|
598
|
+
paranoid_boolean = ParanoidBoolean.create!
|
599
|
+
paranoid_with_counter_cache = ParanoidWithCounterCache
|
600
|
+
.create!(paranoid_boolean: paranoid_boolean)
|
579
601
|
|
580
602
|
assert_equal 1, paranoid_boolean.paranoid_with_counter_caches_count
|
581
603
|
|
@@ -589,8 +611,9 @@ class ParanoidTest < ParanoidBaseTest
|
|
589
611
|
end
|
590
612
|
|
591
613
|
def test_increment_custom_counters
|
592
|
-
paranoid_boolean = ParanoidBoolean.create!
|
593
|
-
paranoid_with_custom_counter_cache = ParanoidWithCustomCounterCache
|
614
|
+
paranoid_boolean = ParanoidBoolean.create!
|
615
|
+
paranoid_with_custom_counter_cache = ParanoidWithCustomCounterCache
|
616
|
+
.create!(paranoid_boolean: paranoid_boolean)
|
594
617
|
|
595
618
|
assert_equal 1, paranoid_boolean.custom_counter_cache
|
596
619
|
|
@@ -604,6 +627,7 @@ class ParanoidTest < ParanoidBaseTest
|
|
604
627
|
end
|
605
628
|
|
606
629
|
def test_explicitly_setting_table_name_after_acts_as_paranoid_macro
|
607
|
-
assert_equal "explicit_table.deleted_at", ParanoidWithExplicitTableNameAfterMacro
|
630
|
+
assert_equal "explicit_table.deleted_at", ParanoidWithExplicitTableNameAfterMacro
|
631
|
+
.paranoid_column_reference
|
608
632
|
end
|
609
633
|
end
|