paranoia 2.2.1 → 2.6.0

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