acts_as_paranoid 0.6.1 → 0.7.2

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.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActsAsParanoid
2
4
  module Relation
3
5
  def self.included(base)
@@ -1,4 +1,6 @@
1
- require 'active_support/core_ext/array/wrap'
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
- def self.[](version)
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
- class V6 < V5
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[ActiveRecord::VERSION::MAJOR], _merge_attributes(attr_names)
21
+ validates_with UniquenessWithoutDeletedValidator, _merge_attributes(attr_names)
88
22
  end
89
23
  end
90
24
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActsAsParanoid
2
- VERSION = "0.6.1"
4
+ VERSION = "0.7.2"
3
5
  end
@@ -1,27 +1,34 @@
1
- require 'test_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
2
4
 
3
5
  class AssociationsTest < ParanoidBaseTest
4
- def test_removal_with_associations
5
- paranoid_company_1 = ParanoidDestroyCompany.create! :name => "ParanoidDestroyCompany #1"
6
- paranoid_company_2 = ParanoidDeleteCompany.create! :name => "ParanoidDestroyCompany #1"
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, ParanoidDeleteCompany.count
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 1, ParanoidProduct.count
15
+ assert_equal 0, ParanoidProduct.count
17
16
  assert_equal 1, ParanoidDestroyCompany.with_deleted.count
18
- assert_equal 2, ParanoidProduct.with_deleted.count
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 1, ParanoidProduct.count
21
+ assert_equal 0, ParanoidProduct.count
23
22
  assert_equal 0, ParanoidDestroyCompany.with_deleted.count
24
- assert_equal 1, ParanoidProduct.with_deleted.count
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
- assert_equal includes_values, paranoid_has_many_dependant.association(:paranoid_time_with_scope).scope.includes_values
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
- paranoid_time = ParanoidTime.create(name: 'not-hello')
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: 'hello')
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: 'not-hello')
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: 'hello')
87
+ paranoid_time.update(name: "hello")
78
88
  paranoid_has_many_dependant.reload
79
89
 
80
- assert_equal paranoid_time, paranoid_has_many_dependant.paranoid_time_with_scope_with_deleted
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.paranoid_time_with_scope_with_deleted
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.create(:name => 'dependant!')
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.create!(:name => 'dependant!', :paranoid_time_polymorphic_with_deleted => paranoid_time)
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.paranoid_time_polymorphic_with_deleted
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.reload.paranoid_time_polymorphic_with_deleted
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 = ParanoidHasManyDependant.create!(:name => 'dependant!', :paranoid_time_polymorphic_with_deleted => nil)
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.with_indifferent_access[:paranoid_time]
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 = ParanoidHasManyDependant.reflections.with_indifferent_access[: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.with_indifferent_access[:paranoid_time_polymorphic_with_deleted]
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, "Associated objects not unlinked"
175
- assert_equal right, ParanoidManyManyParentRight.find(right.id), "Associated object deleted"
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, "Associated objects not unlinked"
189
- assert_equal right, ParanoidManyManyParentRight.find(right.id), "Associated object deleted"
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 test_cannot_find_a_has_many_through_object_when_its_linking_object_is_paranoid_destroyed
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, "Child's left parent is incorrect"
222
- assert_equal right, child.paranoid_many_many_parent_right, "Child's right parent is incorrect"
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, "Child's left parent is incorrect"
236
- assert_equal right, child.paranoid_many_many_parent_right, "Child's right parent is incorrect"
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, "Child's left parent is incorrect"
250
- assert_equal right, child.paranoid_many_many_parent_right, "Child's right parent is incorrect"
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['activerecord'].version >= Gem::Version.new('5.2.0')
275
- skip 'Creation as deleted is not supported with Rails >= 5.2'
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! :name => 'Foo', :deleted_at => now
279
- assert_equal 'Foo', record.name
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
- require 'test_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
2
4
 
3
5
  class ParanoidTest < ParanoidBaseTest
4
6
  def test_paranoid?
@@ -69,6 +71,13 @@ class ParanoidTest < ParanoidBaseTest
69
71
  assert_not_nil pt.paranoid_value
70
72
  end
71
73
 
74
+ def test_non_persisted_delete
75
+ pt = ParanoidTime.new
76
+ assert_nil pt.paranoid_value
77
+ pt.delete
78
+ assert_not_nil pt.paranoid_value
79
+ end
80
+
72
81
  def test_non_persisted_destroy!
73
82
  pt = ParanoidTime.new
74
83
  assert_nil pt.paranoid_value
@@ -94,12 +103,34 @@ class ParanoidTest < ParanoidBaseTest
94
103
  assert_equal 1, ParanoidString.count
95
104
  end
96
105
 
106
+ def test_recovery!
107
+ ParanoidBoolean.first.destroy
108
+ ParanoidBoolean.create(name: "paranoid")
109
+
110
+ assert_raise do
111
+ ParanoidBoolean.only_deleted.first.recover!
112
+ end
113
+ end
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
+
97
127
  def setup_recursive_tests
98
128
  @paranoid_time_object = ParanoidTime.first
99
129
 
100
130
  # Create one extra ParanoidHasManyDependant record so that we can validate
101
131
  # the correct dependants are recovered.
102
- ParanoidTime.where('id <> ?', @paranoid_time_object.id).first.paranoid_has_many_dependants.create(:name => "should not be recovered").destroy
132
+ ParanoidTime.where("id <> ?", @paranoid_time_object.id).first
133
+ .paranoid_has_many_dependants.create(name: "should not be recovered").destroy
103
134
 
104
135
  @paranoid_boolean_count = ParanoidBoolean.count
105
136
 
@@ -108,20 +139,21 @@ class ParanoidTest < ParanoidBaseTest
108
139
  assert_equal 1, NotParanoid.count
109
140
 
110
141
  (1..3).each do |i|
111
- has_many_object = @paranoid_time_object.paranoid_has_many_dependants.create(:name => "has_many_#{i}")
112
- has_many_object.create_paranoid_belongs_dependant(:name => "belongs_to_#{i}")
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}")
113
145
  has_many_object.save
114
146
 
115
- paranoid_boolean = @paranoid_time_object.paranoid_booleans.create(:name => "boolean_#{i}")
116
- paranoid_boolean.create_paranoid_has_one_dependant(:name => "has_one_#{i}")
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}")
117
150
  paranoid_boolean.save
118
151
 
119
- @paranoid_time_object.not_paranoids.create(:name => "not_paranoid_a#{i}")
120
-
152
+ @paranoid_time_object.not_paranoids.create(name: "not_paranoid_a#{i}")
121
153
  end
122
154
 
123
- @paranoid_time_object.create_not_paranoid(:name => "not_paranoid_belongs_to")
124
- @paranoid_time_object.create_has_one_not_paranoid(:name => "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")
125
157
 
126
158
  assert_equal 3, ParanoidTime.count
127
159
  assert_equal 3, ParanoidHasManyDependant.count
@@ -172,7 +204,7 @@ class ParanoidTest < ParanoidBaseTest
172
204
  @paranoid_time_object.destroy
173
205
  @paranoid_time_object.reload
174
206
 
175
- @paranoid_time_object.recover(:recursive => true)
207
+ @paranoid_time_object.recover(recursive: true)
176
208
 
177
209
  assert_equal 3, ParanoidTime.count
178
210
  assert_equal 3, ParanoidHasManyDependant.count
@@ -193,14 +225,14 @@ class ParanoidTest < ParanoidBaseTest
193
225
  @paranoid_time_object.paranoid_has_many_dependants.first.destroy
194
226
  end
195
227
  Time.stub :now, 1.hour.ago do
196
- @paranoid_time_object.paranoid_has_many_dependants.
197
- last.paranoid_belongs_dependant.
198
- destroy
228
+ @paranoid_time_object.paranoid_has_many_dependants
229
+ .last.paranoid_belongs_dependant
230
+ .destroy
199
231
  end
200
232
  @paranoid_time_object.destroy
201
233
  @paranoid_time_object.reload
202
234
 
203
- @paranoid_time_object.recover(:recursive => true)
235
+ @paranoid_time_object.recover(recursive: true)
204
236
 
205
237
  assert_equal 3, ParanoidTime.count
206
238
  assert_equal 2, ParanoidHasManyDependant.count
@@ -213,17 +245,17 @@ class ParanoidTest < ParanoidBaseTest
213
245
 
214
246
  def test_recursive_recovery_for_belongs_to_polymorphic
215
247
  child_1 = ParanoidAndroid.create
216
- section_1 = ParanoidSection.create(:paranoid_thing => child_1)
248
+ section_1 = ParanoidSection.create(paranoid_thing: child_1)
217
249
 
218
- child_2 = ParanoidPolygon.create(:sides => 3)
219
- section_2 = ParanoidSection.create(:paranoid_thing => child_2)
250
+ child_2 = ParanoidPolygon.create(sides: 3)
251
+ section_2 = ParanoidSection.create(paranoid_thing: child_2)
220
252
 
221
253
  assert_equal section_1.paranoid_thing, child_1
222
254
  assert_equal section_1.paranoid_thing.class, ParanoidAndroid
223
255
  assert_equal section_2.paranoid_thing, child_2
224
256
  assert_equal section_2.paranoid_thing.class, ParanoidPolygon
225
257
 
226
- parent = ParanoidTime.create(:name => "paranoid_parent")
258
+ parent = ParanoidTime.create(name: "paranoid_parent")
227
259
  parent.paranoid_sections << section_1
228
260
  parent.paranoid_sections << section_2
229
261
 
@@ -254,7 +286,7 @@ class ParanoidTest < ParanoidBaseTest
254
286
  @paranoid_time_object.destroy
255
287
  @paranoid_time_object.reload
256
288
 
257
- @paranoid_time_object.recover(:recursive => false)
289
+ @paranoid_time_object.recover(recursive: false)
258
290
 
259
291
  assert_equal 3, ParanoidTime.count
260
292
  assert_equal 0, ParanoidHasManyDependant.count
@@ -265,6 +297,24 @@ class ParanoidTest < ParanoidBaseTest
265
297
  assert_equal 0, HasOneNotParanoid.count
266
298
  end
267
299
 
300
+ def test_dirty
301
+ pt = ParanoidTime.create
302
+ pt.destroy
303
+ assert_not pt.changed?
304
+ end
305
+
306
+ def test_delete_dirty
307
+ pt = ParanoidTime.create
308
+ pt.delete
309
+ assert_not pt.changed?
310
+ end
311
+
312
+ def test_destroy_fully_dirty
313
+ pt = ParanoidTime.create
314
+ pt.destroy_fully!
315
+ assert_not pt.changed?
316
+ end
317
+
268
318
  def test_deleted?
269
319
  ParanoidTime.first.destroy
270
320
  assert ParanoidTime.with_deleted.first.deleted?
@@ -273,6 +323,43 @@ class ParanoidTest < ParanoidBaseTest
273
323
  assert ParanoidString.with_deleted.first.deleted?
274
324
  end
275
325
 
326
+ def test_delete_deleted?
327
+ ParanoidTime.first.delete
328
+ assert ParanoidTime.with_deleted.first.deleted?
329
+
330
+ ParanoidString.first.delete
331
+ assert ParanoidString.with_deleted.first.deleted?
332
+ end
333
+
334
+ def test_destroy_fully_deleted?
335
+ object = ParanoidTime.first
336
+ object.destroy_fully!
337
+ assert object.deleted?
338
+
339
+ object = ParanoidString.first
340
+ object.destroy_fully!
341
+ assert object.deleted?
342
+ end
343
+
344
+ def test_deleted_fully?
345
+ ParanoidTime.first.destroy
346
+ assert_not ParanoidTime.with_deleted.first.deleted_fully?
347
+
348
+ ParanoidString.first.destroy
349
+ assert ParanoidString.with_deleted.first.deleted?
350
+ end
351
+
352
+ def test_delete_deleted_fully?
353
+ ParanoidTime.first.delete
354
+ assert_not ParanoidTime.with_deleted.first.deleted_fully?
355
+ end
356
+
357
+ def test_destroy_fully_deleted_fully?
358
+ object = ParanoidTime.first
359
+ object.destroy_fully!
360
+ assert object.deleted_fully?
361
+ end
362
+
276
363
  def test_paranoid_destroy_callbacks
277
364
  @paranoid_with_callback = ParanoidWithCallback.first
278
365
  ParanoidWithCallback.transaction do
@@ -312,6 +399,14 @@ class ParanoidTest < ParanoidBaseTest
312
399
  assert @paranoid_with_callback.called_after_recover
313
400
  end
314
401
 
402
+ def test_recovery_callbacks_without_destroy
403
+ @paranoid_with_callback = ParanoidWithCallback.first
404
+ @paranoid_with_callback.recover
405
+
406
+ assert_nil @paranoid_with_callback.called_before_recover
407
+ assert_nil @paranoid_with_callback.called_after_recover
408
+ end
409
+
315
410
  def test_delete_by_multiple_id_is_paranoid
316
411
  model_a = ParanoidBelongsDependant.create
317
412
  model_b = ParanoidBelongsDependant.create
@@ -360,104 +455,171 @@ class ParanoidTest < ParanoidBaseTest
360
455
 
361
456
  # Test string type columns that don't have a nil value when not deleted (Y/N for example)
362
457
  def test_string_type_with_no_nil_value_before_destroy
363
- ps = ParanoidString.create!(:deleted => 'not dead')
364
- assert_equal 1, ParanoidString.where(:id => ps).count
458
+ ps = ParanoidString.create!(deleted: "not dead")
459
+ assert_equal 1, ParanoidString.where(id: ps).count
365
460
  end
366
461
 
367
462
  def test_string_type_with_no_nil_value_after_destroy
368
- ps = ParanoidString.create!(:deleted => 'not dead')
463
+ ps = ParanoidString.create!(deleted: "not dead")
369
464
  ps.destroy
370
- assert_equal 0, ParanoidString.where(:id => ps).count
465
+ assert_equal 0, ParanoidString.where(id: ps).count
371
466
  end
372
467
 
373
468
  def test_string_type_with_no_nil_value_before_destroy_with_deleted
374
- ps = ParanoidString.create!(:deleted => 'not dead')
375
- assert_equal 1, ParanoidString.with_deleted.where(:id => ps).count
469
+ ps = ParanoidString.create!(deleted: "not dead")
470
+ assert_equal 1, ParanoidString.with_deleted.where(id: ps).count
376
471
  end
377
472
 
378
473
  def test_string_type_with_no_nil_value_after_destroy_with_deleted
379
- ps = ParanoidString.create!(:deleted => 'not dead')
474
+ ps = ParanoidString.create!(deleted: "not dead")
380
475
  ps.destroy
381
- assert_equal 1, ParanoidString.with_deleted.where(:id => ps).count
476
+ assert_equal 1, ParanoidString.with_deleted.where(id: ps).count
382
477
  end
383
478
 
384
479
  def test_string_type_with_no_nil_value_before_destroy_only_deleted
385
- ps = ParanoidString.create!(:deleted => 'not dead')
386
- assert_equal 0, ParanoidString.only_deleted.where(:id => ps).count
480
+ ps = ParanoidString.create!(deleted: "not dead")
481
+ assert_equal 0, ParanoidString.only_deleted.where(id: ps).count
387
482
  end
388
483
 
389
484
  def test_string_type_with_no_nil_value_after_destroy_only_deleted
390
- ps = ParanoidString.create!(:deleted => 'not dead')
485
+ ps = ParanoidString.create!(deleted: "not dead")
391
486
  ps.destroy
392
- assert_equal 1, ParanoidString.only_deleted.where(:id => ps).count
487
+ assert_equal 1, ParanoidString.only_deleted.where(id: ps).count
393
488
  end
394
489
 
395
490
  def test_string_type_with_no_nil_value_after_destroyed_twice
396
- ps = ParanoidString.create!(:deleted => 'not dead')
491
+ ps = ParanoidString.create!(deleted: "not dead")
397
492
  2.times { ps.destroy }
398
- assert_equal 0, ParanoidString.with_deleted.where(:id => ps).count
493
+ assert_equal 0, ParanoidString.with_deleted.where(id: ps).count
399
494
  end
400
495
 
401
496
  # Test boolean type columns, that are not nullable
402
497
  def test_boolean_type_with_no_nil_value_before_destroy
403
- ps = ParanoidBooleanNotNullable.create!()
404
- assert_equal 1, ParanoidBooleanNotNullable.where(:id => ps).count
498
+ ps = ParanoidBooleanNotNullable.create!
499
+ assert_equal 1, ParanoidBooleanNotNullable.where(id: ps).count
405
500
  end
406
501
 
407
502
  def test_boolean_type_with_no_nil_value_after_destroy
408
- ps = ParanoidBooleanNotNullable.create!()
503
+ ps = ParanoidBooleanNotNullable.create!
409
504
  ps.destroy
410
- assert_equal 0, ParanoidBooleanNotNullable.where(:id => ps).count
505
+ assert_equal 0, ParanoidBooleanNotNullable.where(id: ps).count
411
506
  end
412
507
 
413
508
  def test_boolean_type_with_no_nil_value_before_destroy_with_deleted
414
- ps = ParanoidBooleanNotNullable.create!()
415
- assert_equal 1, ParanoidBooleanNotNullable.with_deleted.where(:id => ps).count
509
+ ps = ParanoidBooleanNotNullable.create!
510
+ assert_equal 1, ParanoidBooleanNotNullable.with_deleted.where(id: ps).count
416
511
  end
417
512
 
418
513
  def test_boolean_type_with_no_nil_value_after_destroy_with_deleted
419
- ps = ParanoidBooleanNotNullable.create!()
514
+ ps = ParanoidBooleanNotNullable.create!
420
515
  ps.destroy
421
- assert_equal 1, ParanoidBooleanNotNullable.with_deleted.where(:id => ps).count
516
+ assert_equal 1, ParanoidBooleanNotNullable.with_deleted.where(id: ps).count
422
517
  end
423
518
 
424
519
  def test_boolean_type_with_no_nil_value_before_destroy_only_deleted
425
- ps = ParanoidBooleanNotNullable.create!()
426
- assert_equal 0, ParanoidBooleanNotNullable.only_deleted.where(:id => ps).count
520
+ ps = ParanoidBooleanNotNullable.create!
521
+ assert_equal 0, ParanoidBooleanNotNullable.only_deleted.where(id: ps).count
427
522
  end
428
523
 
429
524
  def test_boolean_type_with_no_nil_value_after_destroy_only_deleted
430
- ps = ParanoidBooleanNotNullable.create!()
525
+ ps = ParanoidBooleanNotNullable.create!
431
526
  ps.destroy
432
- assert_equal 1, ParanoidBooleanNotNullable.only_deleted.where(:id => ps).count
527
+ assert_equal 1, ParanoidBooleanNotNullable.only_deleted.where(id: ps).count
433
528
  end
434
529
 
435
530
  def test_boolean_type_with_no_nil_value_after_destroyed_twice
436
- ps = ParanoidBooleanNotNullable.create!()
531
+ ps = ParanoidBooleanNotNullable.create!
437
532
  2.times { ps.destroy }
438
- assert_equal 0, ParanoidBooleanNotNullable.with_deleted.where(:id => ps).count
533
+ assert_equal 0, ParanoidBooleanNotNullable.with_deleted.where(id: ps).count
534
+ end
535
+
536
+ def test_boolean_type_with_no_nil_value_after_recover
537
+ ps = ParanoidBooleanNotNullable.create!
538
+ ps.destroy
539
+ assert_equal 1, ParanoidBooleanNotNullable.only_deleted.where(id: ps).count
540
+
541
+ ps.recover
542
+ assert_equal 1, ParanoidBooleanNotNullable.where(id: ps).count
543
+ end
544
+
545
+ def test_boolean_type_with_no_nil_value_after_recover!
546
+ ps = ParanoidBooleanNotNullable.create!
547
+ ps.destroy
548
+ assert_equal 1, ParanoidBooleanNotNullable.only_deleted.where(id: ps).count
549
+
550
+ ps.recover!
551
+ assert_equal 1, ParanoidBooleanNotNullable.where(id: ps).count
439
552
  end
440
553
 
441
554
  def test_no_double_tap_destroys_fully
442
- ps = ParanoidNoDoubleTapDestroysFully.create!()
555
+ ps = ParanoidNoDoubleTapDestroysFully.create!
443
556
  2.times { ps.destroy }
444
- assert_equal 1, ParanoidNoDoubleTapDestroysFully.with_deleted.where(:id => ps).count
557
+ assert_equal 1, ParanoidNoDoubleTapDestroysFully.with_deleted.where(id: ps).count
445
558
  end
446
559
 
447
- def test_decrement_counters
448
- paranoid_boolean = ParanoidBoolean.create!()
449
- paranoid_with_counter_cache = ParanoidWithCounterCache.create!(paranoid_boolean: paranoid_boolean)
560
+ def test_decrement_counters_without_touch
561
+ paranoid_boolean = ParanoidBoolean.create!
562
+ paranoid_with_counter_cache = ParanoidWithCounterCache
563
+ .create!(paranoid_boolean: paranoid_boolean)
450
564
 
451
565
  assert_equal 1, paranoid_boolean.paranoid_with_counter_caches_count
566
+ updated_at = paranoid_boolean.reload.updated_at
452
567
 
453
568
  paranoid_with_counter_cache.destroy
454
569
 
455
570
  assert_equal 0, paranoid_boolean.reload.paranoid_with_counter_caches_count
571
+ assert_equal updated_at, paranoid_boolean.reload.updated_at
572
+ end
573
+
574
+ def test_decrement_custom_counters
575
+ paranoid_boolean = ParanoidBoolean.create!
576
+ paranoid_with_custom_counter_cache = ParanoidWithCustomCounterCache
577
+ .create!(paranoid_boolean: paranoid_boolean)
578
+
579
+ assert_equal 1, paranoid_boolean.custom_counter_cache
580
+
581
+ paranoid_with_custom_counter_cache.destroy
582
+
583
+ assert_equal 0, paranoid_boolean.reload.custom_counter_cache
584
+ end
585
+
586
+ def test_decrement_counters_with_touch
587
+ paranoid_boolean = ParanoidBoolean.create!
588
+ paranoid_with_counter_cache = ParanoidWithTouchAndCounterCache
589
+ .create!(paranoid_boolean: paranoid_boolean)
590
+
591
+ assert_equal 1, paranoid_boolean.paranoid_with_touch_and_counter_caches_count
592
+ updated_at = paranoid_boolean.reload.updated_at
593
+
594
+ paranoid_with_counter_cache.destroy
595
+
596
+ assert_equal 0, paranoid_boolean.reload.paranoid_with_touch_and_counter_caches_count
597
+ assert_not_equal updated_at, paranoid_boolean.reload.updated_at
598
+ end
599
+
600
+ def test_touch_belongs_to
601
+ paranoid_boolean = ParanoidBoolean.create!
602
+ paranoid_with_counter_cache = ParanoidWithTouch
603
+ .create!(paranoid_boolean: paranoid_boolean)
604
+
605
+ updated_at = paranoid_boolean.reload.updated_at
606
+
607
+ paranoid_with_counter_cache.destroy
608
+
609
+ assert_not_equal updated_at, paranoid_boolean.reload.updated_at
610
+ end
611
+
612
+ def test_destroy_with_optional_belongs_to_and_counter_cache
613
+ ps = ParanoidWithCounterCacheOnOptionalBelognsTo.create!
614
+ ps.destroy
615
+ assert_equal 1, ParanoidWithCounterCacheOnOptionalBelognsTo.only_deleted
616
+ .where(id: ps).count
456
617
  end
457
618
 
458
619
  def test_hard_destroy_decrement_counters
459
- paranoid_boolean = ParanoidBoolean.create!()
460
- paranoid_with_counter_cache = ParanoidWithCounterCache.create!(paranoid_boolean: paranoid_boolean)
620
+ paranoid_boolean = ParanoidBoolean.create!
621
+ paranoid_with_counter_cache = ParanoidWithCounterCache
622
+ .create!(paranoid_boolean: paranoid_boolean)
461
623
 
462
624
  assert_equal 1, paranoid_boolean.paranoid_with_counter_caches_count
463
625
 
@@ -466,9 +628,22 @@ class ParanoidTest < ParanoidBaseTest
466
628
  assert_equal 0, paranoid_boolean.reload.paranoid_with_counter_caches_count
467
629
  end
468
630
 
631
+ def test_hard_destroy_decrement_custom_counters
632
+ paranoid_boolean = ParanoidBoolean.create!
633
+ paranoid_with_custom_counter_cache = ParanoidWithCustomCounterCache
634
+ .create!(paranoid_boolean: paranoid_boolean)
635
+
636
+ assert_equal 1, paranoid_boolean.custom_counter_cache
637
+
638
+ paranoid_with_custom_counter_cache.destroy_fully!
639
+
640
+ assert_equal 0, paranoid_boolean.reload.custom_counter_cache
641
+ end
642
+
469
643
  def test_increment_counters
470
- paranoid_boolean = ParanoidBoolean.create!()
471
- paranoid_with_counter_cache = ParanoidWithCounterCache.create!(paranoid_boolean: paranoid_boolean)
644
+ paranoid_boolean = ParanoidBoolean.create!
645
+ paranoid_with_counter_cache = ParanoidWithCounterCache
646
+ .create!(paranoid_boolean: paranoid_boolean)
472
647
 
473
648
  assert_equal 1, paranoid_boolean.paranoid_with_counter_caches_count
474
649
 
@@ -480,4 +655,25 @@ class ParanoidTest < ParanoidBaseTest
480
655
 
481
656
  assert_equal 1, paranoid_boolean.reload.paranoid_with_counter_caches_count
482
657
  end
658
+
659
+ def test_increment_custom_counters
660
+ paranoid_boolean = ParanoidBoolean.create!
661
+ paranoid_with_custom_counter_cache = ParanoidWithCustomCounterCache
662
+ .create!(paranoid_boolean: paranoid_boolean)
663
+
664
+ assert_equal 1, paranoid_boolean.custom_counter_cache
665
+
666
+ paranoid_with_custom_counter_cache.destroy
667
+
668
+ assert_equal 0, paranoid_boolean.reload.custom_counter_cache
669
+
670
+ paranoid_with_custom_counter_cache.recover
671
+
672
+ assert_equal 1, paranoid_boolean.reload.custom_counter_cache
673
+ end
674
+
675
+ def test_explicitly_setting_table_name_after_acts_as_paranoid_macro
676
+ assert_equal "explicit_table.deleted_at", ParanoidWithExplicitTableNameAfterMacro
677
+ .paranoid_column_reference
678
+ end
483
679
  end