paranoia 2.3.1 → 2.5.3

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,1361 +0,0 @@
1
- require 'bundler/setup'
2
- require 'active_record'
3
- require 'minitest/autorun'
4
- require 'paranoia'
5
-
6
- test_framework = defined?(MiniTest::Test) ? MiniTest::Test : MiniTest::Unit::TestCase
7
-
8
- def connect!
9
- ActiveRecord::Base.establish_connection :adapter => 'sqlite3', database: ':memory:'
10
- end
11
-
12
- def setup!
13
- connect!
14
- {
15
- 'parent_model_with_counter_cache_columns' => 'related_models_count INTEGER DEFAULT 0',
16
- 'parent_models' => 'deleted_at DATETIME',
17
- 'paranoid_models' => 'parent_model_id INTEGER, deleted_at DATETIME',
18
- 'paranoid_model_with_belongs' => 'parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_id INTEGER',
19
- 'paranoid_model_with_build_belongs' => 'parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_and_build_id INTEGER, name VARCHAR(32)',
20
- 'paranoid_model_with_anthor_class_name_belongs' => 'parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_id INTEGER',
21
- 'paranoid_model_with_foreign_key_belongs' => 'parent_model_id INTEGER, deleted_at DATETIME, has_one_foreign_key_id INTEGER',
22
- 'paranoid_model_with_timestamps' => 'parent_model_id INTEGER, created_at DATETIME, updated_at DATETIME, deleted_at DATETIME',
23
- 'not_paranoid_model_with_belongs' => 'parent_model_id INTEGER, paranoid_model_with_has_one_id INTEGER',
24
- 'not_paranoid_model_with_belongs_and_assocation_not_soft_destroyed_validator' => 'parent_model_id INTEGER, paranoid_model_with_has_one_id INTEGER',
25
- 'paranoid_model_with_has_one_and_builds' => 'parent_model_id INTEGER, color VARCHAR(32), deleted_at DATETIME, has_one_foreign_key_id INTEGER',
26
- 'featureful_models' => 'deleted_at DATETIME, name VARCHAR(32)',
27
- 'plain_models' => 'deleted_at DATETIME',
28
- 'callback_models' => 'deleted_at DATETIME',
29
- 'fail_callback_models' => 'deleted_at DATETIME',
30
- 'related_models' => 'parent_model_id INTEGER, parent_model_with_counter_cache_column_id INTEGER, deleted_at DATETIME',
31
- 'asplode_models' => 'parent_model_id INTEGER, deleted_at DATETIME',
32
- 'employers' => 'name VARCHAR(32), deleted_at DATETIME',
33
- 'employees' => 'deleted_at DATETIME',
34
- 'jobs' => 'employer_id INTEGER NOT NULL, employee_id INTEGER NOT NULL, deleted_at DATETIME',
35
- 'custom_column_models' => 'destroyed_at DATETIME',
36
- 'custom_sentinel_models' => 'deleted_at DATETIME NOT NULL',
37
- 'non_paranoid_models' => 'parent_model_id INTEGER',
38
- 'polymorphic_models' => 'parent_id INTEGER, parent_type STRING, deleted_at DATETIME',
39
- 'namespaced_paranoid_has_ones' => 'deleted_at DATETIME, paranoid_belongs_tos_id INTEGER',
40
- 'namespaced_paranoid_belongs_tos' => 'deleted_at DATETIME, paranoid_has_one_id INTEGER',
41
- 'unparanoid_unique_models' => 'name VARCHAR(32), paranoid_with_unparanoids_id INTEGER',
42
- 'active_column_models' => 'deleted_at DATETIME, active BOOLEAN',
43
- 'active_column_model_with_uniqueness_validations' => 'name VARCHAR(32), deleted_at DATETIME, active BOOLEAN',
44
- 'paranoid_model_with_belongs_to_active_column_model_with_has_many_relationships' => 'name VARCHAR(32), deleted_at DATETIME, active BOOLEAN, active_column_model_with_has_many_relationship_id INTEGER',
45
- 'active_column_model_with_has_many_relationships' => 'name VARCHAR(32), deleted_at DATETIME, active BOOLEAN',
46
- 'without_default_scope_models' => 'deleted_at DATETIME'
47
- }.each do |table_name, columns_as_sql_string|
48
- ActiveRecord::Base.connection.execute "CREATE TABLE #{table_name} (id INTEGER NOT NULL PRIMARY KEY, #{columns_as_sql_string})"
49
- end
50
- end
51
-
52
- class WithDifferentConnection < ActiveRecord::Base
53
- establish_connection adapter: 'sqlite3', database: ':memory:'
54
- connection.execute 'CREATE TABLE with_different_connections (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
55
- acts_as_paranoid
56
- end
57
-
58
- setup!
59
-
60
- class ParanoiaTest < test_framework
61
- def setup
62
- connection = ActiveRecord::Base.connection
63
- cleaner = ->(source) {
64
- ActiveRecord::Base.connection.execute "DELETE FROM #{source}"
65
- }
66
-
67
- if ActiveRecord::VERSION::MAJOR < 5
68
- connection.tables.each(&cleaner)
69
- else
70
- connection.data_sources.each(&cleaner)
71
- end
72
- end
73
-
74
- def test_plain_model_class_is_not_paranoid
75
- assert_equal false, PlainModel.paranoid?
76
- end
77
-
78
- def test_paranoid_model_class_is_paranoid
79
- assert_equal true, ParanoidModel.paranoid?
80
- end
81
-
82
- def test_plain_models_are_not_paranoid
83
- assert_equal false, PlainModel.new.paranoid?
84
- end
85
-
86
- def test_paranoid_models_are_paranoid
87
- assert_equal true, ParanoidModel.new.paranoid?
88
- end
89
-
90
- def test_paranoid_models_to_param
91
- model = ParanoidModel.new
92
- model.save
93
- to_param = model.to_param
94
-
95
- model.destroy
96
-
97
- assert model.to_param
98
- assert_equal to_param, model.to_param
99
- end
100
-
101
- def test_destroy_behavior_for_plain_models
102
- model = PlainModel.new
103
- assert_equal 0, model.class.count
104
- model.save!
105
- assert_equal 1, model.class.count
106
- model.destroy
107
-
108
- assert_equal true, model.deleted_at.nil?
109
-
110
- assert_equal 0, model.class.count
111
- assert_equal 0, model.class.unscoped.count
112
- end
113
-
114
- # Anti-regression test for #81, which would've introduced a bug to break this test.
115
- def test_destroy_behavior_for_plain_models_callbacks
116
- model = CallbackModel.new
117
- model.save
118
- model.remove_called_variables # clear called callback flags
119
- model.destroy
120
-
121
- assert_nil model.instance_variable_get(:@update_callback_called)
122
- assert_nil model.instance_variable_get(:@save_callback_called)
123
- assert_nil model.instance_variable_get(:@validate_called)
124
-
125
- assert model.instance_variable_get(:@destroy_callback_called)
126
- assert model.instance_variable_get(:@after_destroy_callback_called)
127
- assert model.instance_variable_get(:@after_commit_callback_called)
128
- end
129
-
130
-
131
- def test_delete_behavior_for_plain_models_callbacks
132
- model = CallbackModel.new
133
- model.save
134
- model.remove_called_variables # clear called callback flags
135
- model.delete
136
-
137
- assert_nil model.instance_variable_get(:@update_callback_called)
138
- assert_nil model.instance_variable_get(:@save_callback_called)
139
- assert_nil model.instance_variable_get(:@validate_called)
140
- assert_nil model.instance_variable_get(:@destroy_callback_called)
141
- assert_nil model.instance_variable_get(:@after_destroy_callback_called)
142
- assert_nil model.instance_variable_get(:@after_commit_callback_called)
143
- end
144
-
145
- def test_delete_in_transaction_behavior_for_plain_models_callbacks
146
- model = CallbackModel.new
147
- model.save
148
- model.remove_called_variables # clear called callback flags
149
- CallbackModel.transaction do
150
- model.delete
151
- end
152
-
153
- assert_nil model.instance_variable_get(:@update_callback_called)
154
- assert_nil model.instance_variable_get(:@save_callback_called)
155
- assert_nil model.instance_variable_get(:@validate_called)
156
- assert_nil model.instance_variable_get(:@destroy_callback_called)
157
- assert_nil model.instance_variable_get(:@after_destroy_callback_called)
158
- assert model.instance_variable_get(:@after_commit_callback_called)
159
- end
160
-
161
- def test_destroy_behavior_for_paranoid_models
162
- model = ParanoidModel.new
163
- assert_equal 0, model.class.count
164
- model.save!
165
- assert_equal 1, model.class.count
166
- model.destroy
167
-
168
- assert_equal false, model.deleted_at.nil?
169
-
170
- assert_equal 0, model.class.count
171
- assert_equal 1, model.class.unscoped.count
172
- end
173
-
174
- def test_update_columns_on_paranoia_destroyed
175
- record = ParentModel.create
176
- record.destroy
177
-
178
- assert record.update_columns deleted_at: Time.now
179
- end
180
-
181
- def test_scoping_behavior_for_paranoid_models
182
- parent1 = ParentModel.create
183
- parent2 = ParentModel.create
184
- p1 = ParanoidModel.create(:parent_model => parent1)
185
- p2 = ParanoidModel.create(:parent_model => parent2)
186
- p1.destroy
187
- p2.destroy
188
-
189
- assert_equal 0, parent1.paranoid_models.count
190
- assert_equal 1, parent1.paranoid_models.only_deleted.count
191
-
192
- assert_equal 2, ParanoidModel.only_deleted.joins(:parent_model).count
193
- assert_equal 1, parent1.paranoid_models.deleted.count
194
- assert_equal 0, parent1.paranoid_models.without_deleted.count
195
- p3 = ParanoidModel.create(:parent_model => parent1)
196
- assert_equal 2, parent1.paranoid_models.with_deleted.count
197
- assert_equal 1, parent1.paranoid_models.without_deleted.count
198
- assert_equal [p1,p3], parent1.paranoid_models.with_deleted
199
- end
200
-
201
- def test_only_deleted_with_joins
202
- c1 = ActiveColumnModelWithHasManyRelationship.create(name: 'Jacky')
203
- c2 = ActiveColumnModelWithHasManyRelationship.create(name: 'Thomas')
204
- p1 = ParanoidModelWithBelongsToActiveColumnModelWithHasManyRelationship.create(name: 'Hello', active_column_model_with_has_many_relationship: c1)
205
-
206
- c1.destroy
207
- assert_equal 1, ActiveColumnModelWithHasManyRelationship.count
208
- assert_equal 1, ActiveColumnModelWithHasManyRelationship.only_deleted.count
209
- assert_equal 1, ActiveColumnModelWithHasManyRelationship.only_deleted.joins(:paranoid_model_with_belongs_to_active_column_model_with_has_many_relationships).count
210
- end
211
-
212
- def test_destroy_behavior_for_custom_column_models
213
- model = CustomColumnModel.new
214
- assert_equal 0, model.class.count
215
- model.save!
216
- assert_nil model.destroyed_at
217
- assert_equal 1, model.class.count
218
- model.destroy
219
-
220
- assert_equal false, model.destroyed_at.nil?
221
- assert model.paranoia_destroyed?
222
-
223
- assert_equal 0, model.class.count
224
- assert_equal 1, model.class.unscoped.count
225
- assert_equal 1, model.class.only_deleted.count
226
- assert_equal 1, model.class.deleted.count
227
- end
228
-
229
- def test_default_sentinel_value
230
- assert_nil ParanoidModel.paranoia_sentinel_value
231
- end
232
-
233
- def test_without_default_scope_option
234
- model = WithoutDefaultScopeModel.create
235
- model.destroy
236
- assert_equal 1, model.class.count
237
- assert_equal 1, model.class.only_deleted.count
238
- assert_equal 0, model.class.where(deleted_at: nil).count
239
- end
240
-
241
- def test_active_column_model
242
- model = ActiveColumnModel.new
243
- assert_equal 0, model.class.count
244
- model.save!
245
- assert_nil model.deleted_at
246
- assert_equal true, model.active
247
- assert_equal 1, model.class.count
248
- model.destroy
249
-
250
- assert_equal false, model.deleted_at.nil?
251
- assert_nil model.active
252
- assert model.paranoia_destroyed?
253
-
254
- assert_equal 0, model.class.count
255
- assert_equal 1, model.class.unscoped.count
256
- assert_equal 1, model.class.only_deleted.count
257
- assert_equal 1, model.class.deleted.count
258
- end
259
-
260
- def test_active_column_model_with_uniqueness_validation_only_checks_non_deleted_records
261
- a = ActiveColumnModelWithUniquenessValidation.create!(name: "A")
262
- a.destroy
263
- b = ActiveColumnModelWithUniquenessValidation.new(name: "A")
264
- assert b.valid?
265
- end
266
-
267
- def test_active_column_model_with_uniqueness_validation_still_works_on_non_deleted_records
268
- a = ActiveColumnModelWithUniquenessValidation.create!(name: "A")
269
- b = ActiveColumnModelWithUniquenessValidation.new(name: "A")
270
- refute b.valid?
271
- end
272
-
273
- def test_sentinel_value_for_custom_sentinel_models
274
- model = CustomSentinelModel.new
275
- assert_equal 0, model.class.count
276
- model.save!
277
- assert_equal DateTime.new(0), model.deleted_at
278
- assert_equal 1, model.class.count
279
- model.destroy
280
-
281
- assert DateTime.new(0) != model.deleted_at
282
- assert model.paranoia_destroyed?
283
-
284
- assert_equal 0, model.class.count
285
- assert_equal 1, model.class.unscoped.count
286
- assert_equal 1, model.class.only_deleted.count
287
- assert_equal 1, model.class.deleted.count
288
-
289
- model.restore
290
- assert_equal DateTime.new(0), model.deleted_at
291
- assert !model.destroyed?
292
-
293
- assert_equal 1, model.class.count
294
- assert_equal 1, model.class.unscoped.count
295
- assert_equal 0, model.class.only_deleted.count
296
- assert_equal 0, model.class.deleted.count
297
- end
298
-
299
- def test_destroy_behavior_for_featureful_paranoid_models
300
- model = get_featureful_model
301
- assert_equal 0, model.class.count
302
- model.save!
303
- assert_equal 1, model.class.count
304
- model.destroy
305
-
306
- assert_equal false, model.deleted_at.nil?
307
-
308
- assert_equal 0, model.class.count
309
- assert_equal 1, model.class.unscoped.count
310
- end
311
-
312
- def test_destroy_behavior_for_has_one_with_build_and_validation_error
313
- model = ParanoidModelWithHasOneAndBuild.create
314
- model.destroy
315
- end
316
-
317
- # Regression test for #24
318
- def test_chaining_for_paranoid_models
319
- scope = FeaturefulModel.where(:name => "foo").only_deleted
320
- assert_equal({'name' => "foo"}, scope.where_values_hash)
321
- end
322
-
323
- def test_only_destroyed_scope_for_paranoid_models
324
- model = ParanoidModel.new
325
- model.save
326
- model.destroy
327
- model2 = ParanoidModel.new
328
- model2.save
329
-
330
- assert_equal model, ParanoidModel.only_deleted.last
331
- assert_equal false, ParanoidModel.only_deleted.include?(model2)
332
- end
333
-
334
- def test_default_scope_for_has_many_relationships
335
- parent = ParentModel.create
336
- assert_equal 0, parent.related_models.count
337
-
338
- child = parent.related_models.create
339
- assert_equal 1, parent.related_models.count
340
-
341
- child.destroy
342
- assert_equal false, child.deleted_at.nil?
343
-
344
- assert_equal 0, parent.related_models.count
345
- assert_equal 1, parent.related_models.unscoped.count
346
- end
347
-
348
- def test_default_scope_for_has_many_through_relationships
349
- employer = Employer.create
350
- employee = Employee.create
351
- assert_equal 0, employer.jobs.count
352
- assert_equal 0, employer.employees.count
353
- assert_equal 0, employee.jobs.count
354
- assert_equal 0, employee.employers.count
355
-
356
- job = Job.create :employer => employer, :employee => employee
357
- assert_equal 1, employer.jobs.count
358
- assert_equal 1, employer.employees.count
359
- assert_equal 1, employee.jobs.count
360
- assert_equal 1, employee.employers.count
361
-
362
- employee2 = Employee.create
363
- job2 = Job.create :employer => employer, :employee => employee2
364
- employee2.destroy
365
- assert_equal 2, employer.jobs.count
366
- assert_equal 1, employer.employees.count
367
-
368
- job.destroy
369
- assert_equal 1, employer.jobs.count
370
- assert_equal 0, employer.employees.count
371
- assert_equal 0, employee.jobs.count
372
- assert_equal 0, employee.employers.count
373
- end
374
-
375
- def test_delete_behavior_for_callbacks
376
- model = CallbackModel.new
377
- model.save
378
- model.delete
379
- assert_nil model.instance_variable_get(:@destroy_callback_called)
380
- end
381
-
382
- def test_destroy_behavior_for_callbacks
383
- model = CallbackModel.new
384
- model.save
385
- model.destroy
386
- assert model.instance_variable_get(:@destroy_callback_called)
387
- end
388
-
389
- def test_destroy_on_readonly_record
390
- # Just to demonstrate the AR behaviour
391
- model = NonParanoidModel.create!
392
- model.readonly!
393
- assert_raises ActiveRecord::ReadOnlyRecord do
394
- model.destroy
395
- end
396
-
397
- # Mirrors behaviour above
398
- model = ParanoidModel.create!
399
- model.readonly!
400
- assert_raises ActiveRecord::ReadOnlyRecord do
401
- model.destroy
402
- end
403
- end
404
-
405
- def test_destroy_on_really_destroyed_record
406
- model = ParanoidModel.create!
407
- model.really_destroy!
408
- assert model.really_destroyed?
409
- assert model.paranoia_destroyed?
410
- model.destroy
411
- assert model.really_destroyed?
412
- assert model.paranoia_destroyed?
413
- end
414
-
415
- def test_destroy_on_unsaved_record
416
- # Just to demonstrate the AR behaviour
417
- model = NonParanoidModel.new
418
- model.destroy!
419
- assert model.destroyed?
420
- model.destroy!
421
- assert model.destroyed?
422
-
423
- # Mirrors behaviour above
424
- model = ParanoidModel.new
425
- model.destroy!
426
- assert model.paranoia_destroyed?
427
- model.destroy!
428
- assert model.paranoia_destroyed?
429
- end
430
-
431
- def test_restore
432
- model = ParanoidModel.new
433
- model.save
434
- id = model.id
435
- model.destroy
436
-
437
- assert model.paranoia_destroyed?
438
-
439
- model = ParanoidModel.only_deleted.find(id)
440
- model.restore!
441
- model.reload
442
-
443
- assert_equal false, model.paranoia_destroyed?
444
- end
445
-
446
- def test_restore_on_object_return_self
447
- model = ParanoidModel.create
448
- model.destroy
449
-
450
- assert_equal model.class, model.restore.class
451
- end
452
-
453
- # Regression test for #92
454
- def test_destroy_twice
455
- model = ParanoidModel.new
456
- model.save
457
- model.destroy
458
- model.destroy
459
-
460
- assert_equal 1, ParanoidModel.unscoped.where(id: model.id).count
461
- end
462
-
463
- # Regression test for #92
464
- def test_destroy_bang_twice
465
- model = ParanoidModel.new
466
- model.save!
467
- model.destroy!
468
- model.destroy!
469
-
470
- assert_equal 1, ParanoidModel.unscoped.where(id: model.id).count
471
- end
472
-
473
- def test_destroy_return_value_on_success
474
- model = ParanoidModel.create
475
- return_value = model.destroy
476
-
477
- assert_equal(return_value, model)
478
- end
479
-
480
- def test_destroy_return_value_on_failure
481
- model = FailCallbackModel.create
482
- return_value = model.destroy
483
-
484
- assert_equal(return_value, false)
485
- end
486
-
487
- def test_restore_behavior_for_callbacks
488
- model = CallbackModel.new
489
- model.save
490
- id = model.id
491
- model.destroy
492
-
493
- assert model.paranoia_destroyed?
494
-
495
- model = CallbackModel.only_deleted.find(id)
496
- model.restore!
497
- model.reload
498
-
499
- assert model.instance_variable_get(:@restore_callback_called)
500
- end
501
-
502
- def test_really_destroy
503
- model = ParanoidModel.new
504
- model.save
505
- model.really_destroy!
506
- refute ParanoidModel.unscoped.exists?(model.id)
507
- end
508
-
509
- def test_real_destroy_dependent_destroy
510
- parent = ParentModel.create
511
- child1 = parent.very_related_models.create
512
- child2 = parent.non_paranoid_models.create
513
- child3 = parent.create_non_paranoid_model
514
-
515
- parent.really_destroy!
516
-
517
- refute RelatedModel.unscoped.exists?(child1.id)
518
- refute NonParanoidModel.unscoped.exists?(child2.id)
519
- refute NonParanoidModel.unscoped.exists?(child3.id)
520
- end
521
-
522
- def test_real_destroy_dependent_destroy_after_normal_destroy
523
- parent = ParentModel.create
524
- child = parent.very_related_models.create
525
- parent.destroy
526
- parent.really_destroy!
527
- refute RelatedModel.unscoped.exists?(child.id)
528
- end
529
-
530
- def test_real_destroy_dependent_destroy_after_normal_destroy_does_not_delete_other_children
531
- parent_1 = ParentModel.create
532
- child_1 = parent_1.very_related_models.create
533
-
534
- parent_2 = ParentModel.create
535
- child_2 = parent_2.very_related_models.create
536
- parent_1.destroy
537
- parent_1.really_destroy!
538
- assert RelatedModel.unscoped.exists?(child_2.id)
539
- end
540
-
541
- def test_really_destroy_behavior_for_callbacks
542
- model = CallbackModel.new
543
- model.save
544
- model.really_destroy!
545
-
546
- assert model.instance_variable_get(:@real_destroy_callback_called)
547
- end
548
-
549
- def test_really_delete
550
- model = ParanoidModel.new
551
- model.save
552
- model.really_delete
553
-
554
- refute ParanoidModel.unscoped.exists?(model.id)
555
- end
556
-
557
- def test_multiple_restore
558
- a = ParanoidModel.new
559
- a.save
560
- a_id = a.id
561
- a.destroy
562
-
563
- b = ParanoidModel.new
564
- b.save
565
- b_id = b.id
566
- b.destroy
567
-
568
- c = ParanoidModel.new
569
- c.save
570
- c_id = c.id
571
- c.destroy
572
-
573
- ParanoidModel.restore([a_id, c_id])
574
-
575
- a.reload
576
- b.reload
577
- c.reload
578
-
579
- refute a.paranoia_destroyed?
580
- assert b.paranoia_destroyed?
581
- refute c.paranoia_destroyed?
582
- end
583
-
584
- def test_restore_with_associations_using_recovery_window
585
- parent = ParentModel.create
586
- first_child = parent.very_related_models.create
587
- second_child = parent.very_related_models.create
588
-
589
- parent.destroy
590
- second_child.update(deleted_at: parent.deleted_at + 11.minutes)
591
-
592
- parent.restore!(:recursive => true)
593
- assert_equal true, parent.deleted_at.nil?
594
- assert_equal true, first_child.reload.deleted_at.nil?
595
- assert_equal true, second_child.reload.deleted_at.nil?
596
-
597
- parent.destroy
598
- second_child.update(deleted_at: parent.deleted_at + 11.minutes)
599
-
600
- parent.restore(:recursive => true, :recovery_window => 10.minutes)
601
- assert_equal true, parent.deleted_at.nil?
602
- assert_equal true, first_child.reload.deleted_at.nil?
603
- assert_equal false, second_child.reload.deleted_at.nil?
604
-
605
- second_child.restore
606
- parent.destroy
607
- first_child.update(deleted_at: parent.deleted_at - 11.minutes)
608
- second_child.update(deleted_at: parent.deleted_at - 9.minutes)
609
-
610
- ParentModel.restore(parent.id, :recursive => true, :recovery_window => 10.minutes)
611
- assert_equal true, parent.reload.deleted_at.nil?
612
- assert_equal false, first_child.reload.deleted_at.nil?
613
- assert_equal true, second_child.reload.deleted_at.nil?
614
- end
615
-
616
- def test_restore_with_associations
617
- parent = ParentModel.create
618
- first_child = parent.very_related_models.create
619
- second_child = parent.non_paranoid_models.create
620
-
621
- parent.destroy
622
- assert_equal false, parent.deleted_at.nil?
623
- assert_equal false, first_child.reload.deleted_at.nil?
624
- assert_equal true, second_child.destroyed?
625
-
626
- parent.restore!
627
- assert_equal true, parent.deleted_at.nil?
628
- assert_equal false, first_child.reload.deleted_at.nil?
629
- assert_equal true, second_child.destroyed?
630
-
631
- parent.destroy
632
- parent.restore(:recursive => true)
633
- assert_equal true, parent.deleted_at.nil?
634
- assert_equal true, first_child.reload.deleted_at.nil?
635
- assert_equal true, second_child.destroyed?
636
-
637
- parent.destroy
638
- ParentModel.restore(parent.id, :recursive => true)
639
- assert_equal true, parent.reload.deleted_at.nil?
640
- assert_equal true, first_child.reload.deleted_at.nil?
641
- assert_equal true, second_child.destroyed?
642
- end
643
-
644
- # regression tests for #118
645
- def test_restore_with_has_one_association
646
- # setup and destroy test objects
647
- hasOne = ParanoidModelWithHasOne.create
648
- belongsTo = ParanoidModelWithBelong.create
649
- anthorClassName = ParanoidModelWithAnthorClassNameBelong.create
650
- foreignKey = ParanoidModelWithForeignKeyBelong.create
651
- notParanoidModel = NotParanoidModelWithBelong.create
652
-
653
- hasOne.paranoid_model_with_belong = belongsTo
654
- hasOne.class_name_belong = anthorClassName
655
- hasOne.paranoid_model_with_foreign_key_belong = foreignKey
656
- hasOne.not_paranoid_model_with_belong = notParanoidModel
657
- hasOne.save!
658
-
659
- hasOne.destroy
660
- assert_equal false, hasOne.deleted_at.nil?
661
- assert_equal false, belongsTo.deleted_at.nil?
662
-
663
- # Does it restore has_one associations?
664
- hasOne.restore(:recursive => true)
665
- hasOne.save!
666
-
667
- assert_equal true, hasOne.reload.deleted_at.nil?
668
- assert_equal true, belongsTo.reload.deleted_at.nil?, "#{belongsTo.deleted_at}"
669
- assert_equal true, notParanoidModel.destroyed?
670
- assert ParanoidModelWithBelong.with_deleted.reload.count != 0, "There should be a record"
671
- assert ParanoidModelWithAnthorClassNameBelong.with_deleted.reload.count != 0, "There should be an other record"
672
- assert ParanoidModelWithForeignKeyBelong.with_deleted.reload.count != 0, "There should be a foreign_key record"
673
- end
674
-
675
- def test_new_restore_with_has_one_association
676
- # setup and destroy test objects
677
- hasOne = ParanoidModelWithHasOne.create
678
- belongsTo = ParanoidModelWithBelong.create
679
- anthorClassName = ParanoidModelWithAnthorClassNameBelong.create
680
- foreignKey = ParanoidModelWithForeignKeyBelong.create
681
- notParanoidModel = NotParanoidModelWithBelong.create
682
-
683
- hasOne.paranoid_model_with_belong = belongsTo
684
- hasOne.class_name_belong = anthorClassName
685
- hasOne.paranoid_model_with_foreign_key_belong = foreignKey
686
- hasOne.not_paranoid_model_with_belong = notParanoidModel
687
- hasOne.save!
688
-
689
- hasOne.destroy
690
- assert_equal false, hasOne.deleted_at.nil?
691
- assert_equal false, belongsTo.deleted_at.nil?
692
-
693
- # Does it restore has_one associations?
694
- newHasOne = ParanoidModelWithHasOne.with_deleted.find(hasOne.id)
695
- newHasOne.restore(:recursive => true)
696
- newHasOne.save!
697
-
698
- assert_equal true, hasOne.reload.deleted_at.nil?
699
- assert_equal true, belongsTo.reload.deleted_at.nil?, "#{belongsTo.deleted_at}"
700
- assert_equal true, notParanoidModel.destroyed?
701
- assert ParanoidModelWithBelong.with_deleted.reload.count != 0, "There should be a record"
702
- assert ParanoidModelWithAnthorClassNameBelong.with_deleted.reload.count != 0, "There should be an other record"
703
- assert ParanoidModelWithForeignKeyBelong.with_deleted.reload.count != 0, "There should be a foreign_key record"
704
- end
705
-
706
- def test_model_restore_with_has_one_association
707
- # setup and destroy test objects
708
- hasOne = ParanoidModelWithHasOne.create
709
- belongsTo = ParanoidModelWithBelong.create
710
- anthorClassName = ParanoidModelWithAnthorClassNameBelong.create
711
- foreignKey = ParanoidModelWithForeignKeyBelong.create
712
- notParanoidModel = NotParanoidModelWithBelong.create
713
-
714
- hasOne.paranoid_model_with_belong = belongsTo
715
- hasOne.class_name_belong = anthorClassName
716
- hasOne.paranoid_model_with_foreign_key_belong = foreignKey
717
- hasOne.not_paranoid_model_with_belong = notParanoidModel
718
- hasOne.save!
719
-
720
- hasOne.destroy
721
- assert_equal false, hasOne.deleted_at.nil?
722
- assert_equal false, belongsTo.deleted_at.nil?
723
-
724
- # Does it restore has_one associations?
725
- ParanoidModelWithHasOne.restore(hasOne.id, :recursive => true)
726
- hasOne.save!
727
-
728
- assert_equal true, hasOne.reload.deleted_at.nil?
729
- assert_equal true, belongsTo.reload.deleted_at.nil?, "#{belongsTo.deleted_at}"
730
- assert_equal true, notParanoidModel.destroyed?
731
- assert ParanoidModelWithBelong.with_deleted.reload.count != 0, "There should be a record"
732
- assert ParanoidModelWithAnthorClassNameBelong.with_deleted.reload.count != 0, "There should be an other record"
733
- assert ParanoidModelWithForeignKeyBelong.with_deleted.reload.count != 0, "There should be a foreign_key record"
734
- end
735
-
736
- def test_restore_with_nil_has_one_association
737
- # setup and destroy test object
738
- hasOne = ParanoidModelWithHasOne.create
739
- hasOne.destroy
740
- assert_equal false, hasOne.reload.deleted_at.nil?
741
-
742
- # Does it raise NoMethodException on restore of nil
743
- hasOne.restore(:recursive => true)
744
-
745
- assert hasOne.reload.deleted_at.nil?
746
- end
747
-
748
- def test_restore_with_module_scoped_has_one_association
749
- # setup and destroy test object
750
- hasOne = Namespaced::ParanoidHasOne.create
751
- hasOne.destroy
752
- assert_equal false, hasOne.reload.deleted_at.nil?
753
-
754
- # Does it raise "uninitialized constant ParanoidBelongsTo"
755
- # on restore of ParanoidHasOne?
756
- hasOne.restore(:recursive => true)
757
-
758
- assert hasOne.reload.deleted_at.nil?
759
- end
760
-
761
- # covers #185
762
- def test_restoring_recursive_has_one_restores_correct_object
763
- hasOnes = 2.times.map { ParanoidModelWithHasOne.create }
764
- belongsTos = 2.times.map { ParanoidModelWithBelong.create }
765
-
766
- hasOnes[0].update paranoid_model_with_belong: belongsTos[0]
767
- hasOnes[1].update paranoid_model_with_belong: belongsTos[1]
768
-
769
- hasOnes.each(&:destroy)
770
-
771
- ParanoidModelWithHasOne.restore(hasOnes[1].id, :recursive => true)
772
- hasOnes.each(&:reload)
773
- belongsTos.each(&:reload)
774
-
775
- # without #185, belongsTos[0] will be restored instead of belongsTos[1]
776
- refute_nil hasOnes[0].deleted_at
777
- refute_nil belongsTos[0].deleted_at
778
- assert_nil hasOnes[1].deleted_at
779
- assert_nil belongsTos[1].deleted_at
780
- end
781
-
782
- # covers #131
783
- def test_has_one_really_destroy_with_nil
784
- model = ParanoidModelWithHasOne.create
785
- model.really_destroy!
786
-
787
- refute ParanoidModelWithBelong.unscoped.exists?(model.id)
788
- end
789
-
790
- def test_has_one_really_destroy_with_record
791
- model = ParanoidModelWithHasOne.create { |record| record.build_paranoid_model_with_belong }
792
- model.really_destroy!
793
-
794
- refute ParanoidModelWithBelong.unscoped.exists?(model.id)
795
- end
796
-
797
- def test_observers_notified
798
- a = ParanoidModelWithObservers.create
799
- a.destroy
800
- a.restore!
801
-
802
- assert a.observers_notified.select {|args| args == [:before_restore, a]}
803
- assert a.observers_notified.select {|args| args == [:after_restore, a]}
804
- end
805
-
806
- def test_observers_not_notified_if_not_supported
807
- a = ParanoidModelWithObservers.create
808
- a.destroy
809
- a.restore!
810
- # essentially, we're just ensuring that this doesn't crash
811
- end
812
-
813
- def test_validates_uniqueness_only_checks_non_deleted_records
814
- a = Employer.create!(name: "A")
815
- a.destroy
816
- b = Employer.new(name: "A")
817
- assert b.valid?
818
- end
819
-
820
- def test_validates_uniqueness_still_works_on_non_deleted_records
821
- a = Employer.create!(name: "A")
822
- b = Employer.new(name: "A")
823
- refute b.valid?
824
- end
825
-
826
- def test_updated_at_modification_on_destroy
827
- paranoid_model = ParanoidModelWithTimestamp.create(:parent_model => ParentModel.create, :updated_at => 1.day.ago)
828
- assert paranoid_model.updated_at < 10.minutes.ago
829
- paranoid_model.destroy
830
- assert paranoid_model.updated_at > 10.minutes.ago
831
- end
832
-
833
- def test_updated_at_modification_on_restore
834
- parent1 = ParentModel.create
835
- pt1 = ParanoidModelWithTimestamp.create(:parent_model => parent1)
836
- ParanoidModelWithTimestamp.record_timestamps = false
837
- pt1.update_columns(created_at: 20.years.ago, updated_at: 10.years.ago, deleted_at: 10.years.ago)
838
- ParanoidModelWithTimestamp.record_timestamps = true
839
- assert pt1.updated_at < 10.minutes.ago
840
- refute pt1.deleted_at.nil?
841
- pt1.restore!
842
- assert pt1.deleted_at.nil?
843
- assert pt1.updated_at > 10.minutes.ago
844
- end
845
-
846
- def test_i_am_the_destroyer
847
- expected = %Q{
848
- Sharon: "There should be a method called I_AM_THE_DESTROYER!"
849
- Ryan: "What should this method do?"
850
- Sharon: "It should fix all the spelling errors on the page!"
851
- }
852
- assert_output expected do
853
- ParanoidModel.I_AM_THE_DESTROYER!
854
- end
855
- end
856
-
857
- def test_destroy_fails_if_callback_raises_exception
858
- parent = AsplodeModel.create
859
-
860
- assert_raises(StandardError) { parent.destroy }
861
-
862
- #transaction should be rolled back, so parent NOT deleted
863
- refute parent.destroyed?, 'Parent record was destroyed, even though AR callback threw exception'
864
- end
865
-
866
- def test_destroy_fails_if_association_callback_raises_exception
867
- parent = ParentModel.create
868
- children = []
869
- 3.times { children << parent.asplode_models.create }
870
-
871
- assert_raises(StandardError) { parent.destroy }
872
-
873
- #transaction should be rolled back, so parent and children NOT deleted
874
- refute parent.destroyed?, 'Parent record was destroyed, even though AR callback threw exception'
875
- refute children.any?(&:destroyed?), 'Child record was destroyed, even though AR callback threw exception'
876
- end
877
-
878
- def test_restore_model_with_different_connection
879
- ActiveRecord::Base.remove_connection # Disconnect the main connection
880
- a = WithDifferentConnection.create
881
- a.destroy!
882
- a.restore!
883
- # This test passes if no exception is raised
884
- ensure
885
- setup! # Reconnect the main connection
886
- end
887
-
888
- def test_restore_clear_association_cache_if_associations_present
889
- parent = ParentModel.create
890
- 3.times { parent.very_related_models.create }
891
-
892
- parent.destroy
893
-
894
- assert_equal 0, parent.very_related_models.count
895
- assert_equal 0, parent.very_related_models.size
896
-
897
- parent.restore(recursive: true)
898
-
899
- assert_equal 3, parent.very_related_models.count
900
- assert_equal 3, parent.very_related_models.size
901
- end
902
-
903
- def test_model_without_db_connection
904
- ActiveRecord::Base.remove_connection
905
-
906
- NoConnectionModel.class_eval{ acts_as_paranoid }
907
- ensure
908
- setup!
909
- end
910
-
911
- def test_restore_recursive_on_polymorphic_has_one_association
912
- parent = ParentModel.create
913
- polymorphic = PolymorphicModel.create(parent: parent)
914
-
915
- parent.destroy
916
-
917
- assert_equal 0, polymorphic.class.count
918
-
919
- parent.restore(recursive: true)
920
-
921
- assert_equal 1, polymorphic.class.count
922
- end
923
-
924
- # Ensure that we're checking parent_type when restoring
925
- def test_missing_restore_recursive_on_polymorphic_has_one_association
926
- parent = ParentModel.create
927
- polymorphic = PolymorphicModel.create(parent_id: parent.id, parent_type: 'ParanoidModel')
928
-
929
- parent.destroy
930
- polymorphic.destroy
931
-
932
- assert_equal 0, polymorphic.class.count
933
-
934
- parent.restore(recursive: true)
935
-
936
- assert_equal 0, polymorphic.class.count
937
- end
938
-
939
- def test_counter_cache_column_update_on_destroy#_and_restore_and_really_destroy
940
- parent_model_with_counter_cache_column = ParentModelWithCounterCacheColumn.create
941
- related_model = parent_model_with_counter_cache_column.related_models.create
942
-
943
- assert_equal 1, parent_model_with_counter_cache_column.reload.related_models_count
944
- related_model.destroy
945
- assert_equal 0, parent_model_with_counter_cache_column.reload.related_models_count
946
- end
947
-
948
- def test_callbacks_for_counter_cache_column_update_on_destroy
949
- parent_model_with_counter_cache_column = ParentModelWithCounterCacheColumn.create
950
- related_model = parent_model_with_counter_cache_column.related_models.create
951
-
952
- assert_nil related_model.instance_variable_get(:@after_destroy_callback_called)
953
- assert_nil related_model.instance_variable_get(:@after_commit_on_destroy_callback_called)
954
-
955
- related_model.destroy
956
-
957
- assert related_model.instance_variable_get(:@after_destroy_callback_called)
958
- # assert related_model.instance_variable_get(:@after_commit_on_destroy_callback_called)
959
- end
960
-
961
- def test_uniqueness_for_unparanoid_associated
962
- parent_model = ParanoidWithUnparanoids.create
963
- related = parent_model.unparanoid_unique_models.create
964
- # will raise exception if model is not checked for paranoia
965
- related.valid?
966
- end
967
-
968
- def test_assocation_not_soft_destroyed_validator
969
- notParanoidModel = NotParanoidModelWithBelongsAndAssocationNotSoftDestroyedValidator.create
970
- parentModel = ParentModel.create
971
- assert notParanoidModel.valid?
972
-
973
- notParanoidModel.parent_model = parentModel
974
- assert notParanoidModel.valid?
975
- parentModel.destroy
976
- assert !notParanoidModel.valid?
977
- assert notParanoidModel.errors.full_messages.include? "Parent model has been soft-deleted"
978
- end
979
-
980
- # TODO: find a fix for Rails 4.1
981
- if ActiveRecord::VERSION::STRING !~ /\A4\.1/
982
- def test_counter_cache_column_update_on_really_destroy
983
- parent_model_with_counter_cache_column = ParentModelWithCounterCacheColumn.create
984
- related_model = parent_model_with_counter_cache_column.related_models.create
985
-
986
- assert_equal 1, parent_model_with_counter_cache_column.reload.related_models_count
987
- related_model.really_destroy!
988
- assert_equal 0, parent_model_with_counter_cache_column.reload.related_models_count
989
- end
990
- end
991
-
992
- # TODO: find a fix for Rails 4.0 and 4.1
993
- if ActiveRecord::VERSION::STRING >= '4.2'
994
- def test_callbacks_for_counter_cache_column_update_on_really_destroy!
995
- parent_model_with_counter_cache_column = ParentModelWithCounterCacheColumn.create
996
- related_model = parent_model_with_counter_cache_column.related_models.create
997
-
998
- assert_nil related_model.instance_variable_get(:@after_destroy_callback_called)
999
- assert_nil related_model.instance_variable_get(:@after_commit_on_destroy_callback_called)
1000
-
1001
- related_model.really_destroy!
1002
-
1003
- assert related_model.instance_variable_get(:@after_destroy_callback_called)
1004
- assert related_model.instance_variable_get(:@after_commit_on_destroy_callback_called)
1005
- end
1006
-
1007
- def test_counter_cache_column_on_double_destroy
1008
- parent_model_with_counter_cache_column = ParentModelWithCounterCacheColumn.create
1009
- related_model = parent_model_with_counter_cache_column.related_models.create
1010
-
1011
- related_model.destroy
1012
- related_model.destroy
1013
- assert_equal 0, parent_model_with_counter_cache_column.reload.related_models_count
1014
- end
1015
-
1016
- def test_counter_cache_column_on_double_restore
1017
- parent_model_with_counter_cache_column = ParentModelWithCounterCacheColumn.create
1018
- related_model = parent_model_with_counter_cache_column.related_models.create
1019
-
1020
- related_model.destroy
1021
- related_model.restore
1022
- related_model.restore
1023
- assert_equal 1, parent_model_with_counter_cache_column.reload.related_models_count
1024
- end
1025
-
1026
- def test_counter_cache_column_on_destroy_and_really_destroy
1027
- parent_model_with_counter_cache_column = ParentModelWithCounterCacheColumn.create
1028
- related_model = parent_model_with_counter_cache_column.related_models.create
1029
-
1030
- related_model.destroy
1031
- related_model.really_destroy!
1032
- assert_equal 0, parent_model_with_counter_cache_column.reload.related_models_count
1033
- end
1034
-
1035
- def test_counter_cache_column_on_restore
1036
- parent_model_with_counter_cache_column = ParentModelWithCounterCacheColumn.create
1037
- related_model = parent_model_with_counter_cache_column.related_models.create
1038
-
1039
- related_model.destroy
1040
- assert_equal 0, parent_model_with_counter_cache_column.reload.related_models_count
1041
- related_model.restore
1042
- assert_equal 1, parent_model_with_counter_cache_column.reload.related_models_count
1043
- end
1044
- end
1045
-
1046
- private
1047
- def get_featureful_model
1048
- FeaturefulModel.new(:name => "not empty")
1049
- end
1050
- end
1051
-
1052
- # Helper classes
1053
-
1054
- class ParanoidModel < ActiveRecord::Base
1055
- belongs_to :parent_model
1056
- acts_as_paranoid
1057
- end
1058
-
1059
- class ParanoidWithUnparanoids < ActiveRecord::Base
1060
- self.table_name = 'plain_models'
1061
- has_many :unparanoid_unique_models
1062
- end
1063
-
1064
- class UnparanoidUniqueModel < ActiveRecord::Base
1065
- belongs_to :paranoid_with_unparanoids
1066
- validates :name, :uniqueness => true
1067
- end
1068
-
1069
- class FailCallbackModel < ActiveRecord::Base
1070
- belongs_to :parent_model
1071
- acts_as_paranoid
1072
-
1073
- before_destroy { |_|
1074
- if ActiveRecord::VERSION::MAJOR < 5
1075
- false
1076
- else
1077
- throw :abort
1078
- end
1079
- }
1080
- end
1081
-
1082
- class FeaturefulModel < ActiveRecord::Base
1083
- acts_as_paranoid
1084
- validates :name, :presence => true, :uniqueness => true
1085
- end
1086
-
1087
- class NonParanoidChildModel < ActiveRecord::Base
1088
- validates :name, :presence => true, :uniqueness => true
1089
- end
1090
-
1091
- class PlainModel < ActiveRecord::Base
1092
- end
1093
-
1094
- class CallbackModel < ActiveRecord::Base
1095
- acts_as_paranoid
1096
- before_destroy { |model| model.instance_variable_set :@destroy_callback_called, true }
1097
- before_restore { |model| model.instance_variable_set :@restore_callback_called, true }
1098
- before_update { |model| model.instance_variable_set :@update_callback_called, true }
1099
- before_save { |model| model.instance_variable_set :@save_callback_called, true}
1100
- before_real_destroy { |model| model.instance_variable_set :@real_destroy_callback_called, true }
1101
-
1102
- after_destroy { |model| model.instance_variable_set :@after_destroy_callback_called, true }
1103
- after_commit { |model| model.instance_variable_set :@after_commit_callback_called, true }
1104
-
1105
- validate { |model| model.instance_variable_set :@validate_called, true }
1106
-
1107
- def remove_called_variables
1108
- instance_variables.each {|name| (name.to_s.end_with?('_called')) ? remove_instance_variable(name) : nil}
1109
- end
1110
- end
1111
-
1112
- class ParentModel < ActiveRecord::Base
1113
- acts_as_paranoid
1114
- has_many :paranoid_models
1115
- has_many :related_models
1116
- has_many :very_related_models, :class_name => 'RelatedModel', dependent: :destroy
1117
- has_many :non_paranoid_models, dependent: :destroy
1118
- has_one :non_paranoid_model, dependent: :destroy
1119
- has_many :asplode_models, dependent: :destroy
1120
- has_one :polymorphic_model, as: :parent, dependent: :destroy
1121
- end
1122
-
1123
- class ParentModelWithCounterCacheColumn < ActiveRecord::Base
1124
- has_many :related_models
1125
- end
1126
-
1127
- class RelatedModel < ActiveRecord::Base
1128
- acts_as_paranoid
1129
- belongs_to :parent_model
1130
- belongs_to :parent_model_with_counter_cache_column, counter_cache: true
1131
-
1132
- after_destroy do |model|
1133
- if parent_model_with_counter_cache_column && parent_model_with_counter_cache_column.reload.related_models_count == 0
1134
- model.instance_variable_set :@after_destroy_callback_called, true
1135
- end
1136
- end
1137
- after_commit :set_after_commit_on_destroy_callback_called, on: :destroy
1138
-
1139
- def set_after_commit_on_destroy_callback_called
1140
- if parent_model_with_counter_cache_column && parent_model_with_counter_cache_column.reload.related_models_count == 0
1141
- self.instance_variable_set :@after_commit_on_destroy_callback_called, true
1142
- end
1143
- end
1144
- end
1145
-
1146
- class Employer < ActiveRecord::Base
1147
- acts_as_paranoid
1148
- validates_uniqueness_of :name
1149
- has_many :jobs
1150
- has_many :employees, :through => :jobs
1151
- end
1152
-
1153
- class Employee < ActiveRecord::Base
1154
- acts_as_paranoid
1155
- has_many :jobs
1156
- has_many :employers, :through => :jobs
1157
- end
1158
-
1159
- class Job < ActiveRecord::Base
1160
- acts_as_paranoid
1161
- belongs_to :employer
1162
- belongs_to :employee
1163
- end
1164
-
1165
- class CustomColumnModel < ActiveRecord::Base
1166
- acts_as_paranoid column: :destroyed_at
1167
- end
1168
-
1169
- class CustomSentinelModel < ActiveRecord::Base
1170
- acts_as_paranoid sentinel_value: DateTime.new(0)
1171
- end
1172
-
1173
- class WithoutDefaultScopeModel < ActiveRecord::Base
1174
- acts_as_paranoid without_default_scope: true
1175
- end
1176
-
1177
- class ActiveColumnModel < ActiveRecord::Base
1178
- acts_as_paranoid column: :active, sentinel_value: true
1179
-
1180
- def paranoia_restore_attributes
1181
- {
1182
- deleted_at: nil,
1183
- active: true
1184
- }
1185
- end
1186
-
1187
- def paranoia_destroy_attributes
1188
- {
1189
- deleted_at: current_time_from_proper_timezone,
1190
- active: nil
1191
- }
1192
- end
1193
- end
1194
-
1195
- class ActiveColumnModelWithUniquenessValidation < ActiveRecord::Base
1196
- validates :name, :uniqueness => true
1197
- acts_as_paranoid column: :active, sentinel_value: true
1198
-
1199
- def paranoia_restore_attributes
1200
- {
1201
- deleted_at: nil,
1202
- active: true
1203
- }
1204
- end
1205
-
1206
- def paranoia_destroy_attributes
1207
- {
1208
- deleted_at: current_time_from_proper_timezone,
1209
- active: nil
1210
- }
1211
- end
1212
- end
1213
-
1214
- class ActiveColumnModelWithHasManyRelationship < ActiveRecord::Base
1215
- has_many :paranoid_model_with_belongs_to_active_column_model_with_has_many_relationships
1216
- acts_as_paranoid column: :active, sentinel_value: true
1217
-
1218
- def paranoia_restore_attributes
1219
- {
1220
- deleted_at: nil,
1221
- active: true
1222
- }
1223
- end
1224
-
1225
- def paranoia_destroy_attributes
1226
- {
1227
- deleted_at: current_time_from_proper_timezone,
1228
- active: nil
1229
- }
1230
- end
1231
- end
1232
-
1233
- class ParanoidModelWithBelongsToActiveColumnModelWithHasManyRelationship < ActiveRecord::Base
1234
- belongs_to :active_column_model_with_has_many_relationship
1235
-
1236
- acts_as_paranoid column: :active, sentinel_value: true
1237
-
1238
- def paranoia_restore_attributes
1239
- {
1240
- deleted_at: nil,
1241
- active: true
1242
- }
1243
- end
1244
-
1245
- def paranoia_destroy_attributes
1246
- {
1247
- deleted_at: current_time_from_proper_timezone,
1248
- active: nil
1249
- }
1250
- end
1251
- end
1252
-
1253
- class NonParanoidModel < ActiveRecord::Base
1254
- end
1255
-
1256
- class ParanoidModelWithObservers < ParanoidModel
1257
- def observers_notified
1258
- @observers_notified ||= []
1259
- end
1260
-
1261
- def self.notify_observer(*args)
1262
- observers_notified << args
1263
- end
1264
- end
1265
-
1266
- class ParanoidModelWithoutObservers < ParanoidModel
1267
- self.class.send(remove_method :notify_observers) if method_defined?(:notify_observers)
1268
- end
1269
-
1270
- # refer back to regression test for #118
1271
- class ParanoidModelWithHasOne < ParanoidModel
1272
- has_one :paranoid_model_with_belong, :dependent => :destroy
1273
- has_one :class_name_belong, :dependent => :destroy, :class_name => "ParanoidModelWithAnthorClassNameBelong"
1274
- has_one :paranoid_model_with_foreign_key_belong, :dependent => :destroy, :foreign_key => "has_one_foreign_key_id"
1275
- has_one :not_paranoid_model_with_belong, :dependent => :destroy
1276
- end
1277
-
1278
- class ParanoidModelWithHasOneAndBuild < ActiveRecord::Base
1279
- has_one :paranoid_model_with_build_belong, :dependent => :destroy
1280
- validates :color, :presence => true
1281
- after_validation :build_paranoid_model_with_build_belong, on: :create
1282
-
1283
- private
1284
- def build_paranoid_model_with_build_belong
1285
- super.tap { |child| child.name = "foo" }
1286
- end
1287
- end
1288
-
1289
- class ParanoidModelWithBuildBelong < ActiveRecord::Base
1290
- acts_as_paranoid
1291
- validates :name, :presence => true
1292
- belongs_to :paranoid_model_with_has_one_and_build
1293
- end
1294
-
1295
- class ParanoidModelWithBelong < ActiveRecord::Base
1296
- acts_as_paranoid
1297
- belongs_to :paranoid_model_with_has_one
1298
- end
1299
-
1300
- class ParanoidModelWithAnthorClassNameBelong < ActiveRecord::Base
1301
- acts_as_paranoid
1302
- belongs_to :paranoid_model_with_has_one
1303
- end
1304
-
1305
- class ParanoidModelWithForeignKeyBelong < ActiveRecord::Base
1306
- acts_as_paranoid
1307
- belongs_to :paranoid_model_with_has_one
1308
- end
1309
-
1310
- class ParanoidModelWithTimestamp < ActiveRecord::Base
1311
- belongs_to :parent_model
1312
- acts_as_paranoid
1313
- end
1314
-
1315
- class NotParanoidModelWithBelong < ActiveRecord::Base
1316
- belongs_to :paranoid_model_with_has_one
1317
- end
1318
-
1319
- class NotParanoidModelWithBelongsAndAssocationNotSoftDestroyedValidator < NotParanoidModelWithBelong
1320
- belongs_to :parent_model
1321
- validates :parent_model, association_not_soft_destroyed: true
1322
- end
1323
-
1324
- class FlaggedModel < PlainModel
1325
- acts_as_paranoid :flag_column => :is_deleted
1326
- end
1327
-
1328
- class FlaggedModelWithCustomIndex < PlainModel
1329
- acts_as_paranoid :flag_column => :is_deleted, :indexed_column => :is_deleted
1330
- end
1331
-
1332
- class AsplodeModel < ActiveRecord::Base
1333
- acts_as_paranoid
1334
- before_destroy do |r|
1335
- raise StandardError, 'ASPLODE!'
1336
- end
1337
- end
1338
-
1339
- class NoConnectionModel < ActiveRecord::Base
1340
- end
1341
-
1342
- class PolymorphicModel < ActiveRecord::Base
1343
- acts_as_paranoid
1344
- belongs_to :parent, polymorphic: true
1345
- end
1346
-
1347
- module Namespaced
1348
- def self.table_name_prefix
1349
- "namespaced_"
1350
- end
1351
-
1352
- class ParanoidHasOne < ActiveRecord::Base
1353
- acts_as_paranoid
1354
- has_one :paranoid_belongs_to, dependent: :destroy
1355
- end
1356
-
1357
- class ParanoidBelongsTo < ActiveRecord::Base
1358
- acts_as_paranoid
1359
- belongs_to :paranoid_has_one
1360
- end
1361
- end