paranoia 2.4.1 → 2.5.1

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