mongoid_paranoia 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,751 +1,887 @@
1
- require "spec_helper"
2
-
3
- describe Mongoid::Paranoia do
4
-
5
- describe ".scoped" do
6
-
7
- it "returns a scoped criteria" do
8
- expect(ParanoidPost.scoped.selector).to eq({ "deleted_at" => nil })
9
- end
10
- end
11
-
12
- describe ".deleted" do
13
-
14
- context "when called on a root document" do
15
-
16
- let(:post) do
17
- ParanoidPost.create(title: "testing")
18
- end
19
-
20
- before do
21
- post.destroy
22
- end
23
-
24
- let(:deleted) do
25
- ParanoidPost.deleted
26
- end
27
-
28
- it "returns the deleted documents" do
29
- expect(deleted).to eq([ post ])
30
- end
31
- end
32
-
33
- context "when called on an embedded document" do
34
-
35
- let(:person) do
36
- Person.create
37
- end
38
-
39
- let(:phone) do
40
- person.paranoid_phones.create
41
- end
42
-
43
- before do
44
- phone.destroy
45
- person.reload
46
- end
47
-
48
- it "returns the deleted documents" do
49
- expect(person.paranoid_phones.deleted.to_a).to eq([ phone ])
50
- end
51
-
52
- it "returns the correct count" do
53
- expect(person.paranoid_phones.deleted.count).to eq(1)
54
- end
55
- end
56
- end
57
-
58
- describe "#destroy!" do
59
-
60
- context "when the document is a root" do
61
-
62
- let(:post) do
63
- ParanoidPost.create(title: "testing")
64
- end
65
-
66
- before do
67
- post.destroy!
68
- end
69
-
70
- let(:raw) do
71
- ParanoidPost.collection.find(_id: post.id).first
72
- end
73
-
74
- it "hard deletes the document" do
75
- expect(raw).to be_nil
76
- end
77
-
78
- it "executes the before destroy callbacks" do
79
- expect(post.before_destroy_called).to be_truthy
80
- end
81
-
82
- it "executes the after destroy callbacks" do
83
- expect(post.after_destroy_called).to be_truthy
84
- end
85
- end
86
-
87
- context "when the document is embedded" do
88
-
89
- let(:person) do
90
- Person.create
91
- end
92
-
93
- let(:phone) do
94
- person.paranoid_phones.create(number: "911")
95
- end
96
-
97
- before do
98
- phone.destroy!
99
- end
100
-
101
- let(:raw) do
102
- Person.collection.find(_id: person.id).first
103
- end
104
-
105
- it "hard deletes the document" do
106
- expect(raw["paranoid_phones"]).to be_empty
107
- end
108
-
109
- it "executes the before destroy callbacks" do
110
- expect(phone.before_destroy_called).to be_truthy
111
- end
112
-
113
- it "executes the after destroy callbacks" do
114
- expect(phone.after_destroy_called).to be_truthy
115
- end
116
- end
117
-
118
- context "when the document has a dependent relation" do
119
-
120
- let(:post) do
121
- ParanoidPost.create(title: "test")
122
- end
123
-
124
- let!(:author) do
125
- post.authors.create(name: "poe")
126
- end
127
-
128
- before do
129
- post.destroy!
130
- end
131
-
132
- it "cascades the dependent option" do
133
- expect {
134
- author.reload
135
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
136
- end
137
- end
138
- end
139
-
140
- describe "#destroy" do
141
-
142
- context "when the document is a root" do
143
-
144
- let(:post) do
145
- ParanoidPost.create(title: "testing")
146
- end
147
-
148
- before do
149
- post.destroy
150
- end
151
-
152
- let(:raw) do
153
- ParanoidPost.collection.find(_id: post.id).first
154
- end
155
-
156
- it "soft deletes the document" do
157
- expect(raw["deleted_at"]).to be_within(1).of(Time.now)
158
- end
159
-
160
- it "is still marked as persisted" do
161
- expect(post.persisted?).to eq(true)
162
- end
163
-
164
- it "does not return the document in a find" do
165
- expect {
166
- ParanoidPost.find(post.id)
167
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
168
- end
169
-
170
- it "executes the before destroy callbacks" do
171
- expect(post.before_destroy_called).to be_truthy
172
- end
173
-
174
- it "executes the after destroy callbacks" do
175
- expect(post.after_destroy_called).to be_truthy
176
- end
177
- end
178
-
179
- context "when the document is embedded" do
180
-
181
- let(:person) do
182
- Person.create
183
- end
184
-
185
- let(:phone) do
186
- person.paranoid_phones.create(number: "911")
187
- end
188
-
189
- before do
190
- phone.destroy
191
- end
192
-
193
- let(:raw) do
194
- Person.collection.find(_id: person.id).first
195
- end
196
-
197
- it "soft deletes the document" do
198
- expect(raw["paranoid_phones"].first["deleted_at"]).to be_within(1).of(Time.now)
199
- end
200
-
201
- it "does not return the document in a find" do
202
- expect {
203
- person.paranoid_phones.find(phone.id)
204
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
205
- end
206
-
207
- it "does not include the document in the relation" do
208
- expect(person.paranoid_phones.scoped).to be_empty
209
- end
210
-
211
- it "executes the before destroy callbacks" do
212
- expect(phone.before_destroy_called).to be_truthy
213
- end
214
-
215
- it "executes the after destroy callbacks" do
216
- expect(phone.after_destroy_called).to be_truthy
217
- end
218
- end
219
-
220
- context "when the document has a dependent: :delete relation" do
221
-
222
- let(:post) do
223
- ParanoidPost.create(title: "test")
224
- end
225
-
226
- let!(:author) do
227
- post.authors.create(name: "poe")
228
- end
229
-
230
- before do
231
- post.destroy
232
- end
233
-
234
- it "cascades the dependent option" do
235
- expect {
236
- author.reload
237
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
238
- end
239
- end
240
-
241
- context "when the document has a dependent: :restrict relation" do
242
-
243
- let(:post) do
244
- ParanoidPost.create(title: "test")
245
- end
246
-
247
- let!(:title) do
248
- post.titles.create
249
- end
250
-
251
- before do
252
- begin
253
- post.destroy
254
- rescue Mongoid::Errors::DeleteRestriction
255
- end
256
- end
257
-
258
- it "does not destroy the document" do
259
- expect(post).not_to be_destroyed
260
- end
261
- end
262
- end
263
-
264
- describe "#destroyed?" do
265
-
266
- context "when the document is a root" do
267
-
268
- let(:post) do
269
- ParanoidPost.create(title: "testing")
270
- end
271
-
272
- context "when the document is hard deleted" do
273
-
274
- before do
275
- post.destroy!
276
- end
277
-
278
- it "returns true" do
279
- expect(post).to be_destroyed
280
- end
281
- end
282
-
283
- context "when the document is soft deleted" do
284
-
285
- before do
286
- post.destroy
287
- end
288
-
289
- it "returns true" do
290
- expect(post).to be_destroyed
291
- end
292
-
293
- it "returns true for deleted scope document" do
294
- expect(ParanoidPost.deleted.last).to be_destroyed
295
- end
296
- end
297
- end
298
-
299
- context "when the document is embedded" do
300
-
301
- let(:person) do
302
- Person.create
303
- end
304
-
305
- let(:phone) do
306
- person.paranoid_phones.create(number: "911")
307
- end
308
-
309
- context "when the document is hard deleted" do
310
-
311
- before do
312
- phone.destroy!
313
- end
314
-
315
- it "returns true" do
316
- expect(phone).to be_destroyed
317
- end
318
- end
319
-
320
- context "when the document is soft deleted" do
321
-
322
- before do
323
- phone.destroy
324
- end
325
-
326
- it "returns true" do
327
- expect(phone).to be_destroyed
328
- end
329
- end
330
- end
331
- end
332
-
333
- describe "#deleted?" do
334
-
335
- context "when the document is a root" do
336
-
337
- let(:post) do
338
- ParanoidPost.create(title: "testing")
339
- end
340
-
341
- context "when the document is hard deleted" do
342
-
343
- before do
344
- post.destroy!
345
- end
346
-
347
- it "returns true" do
348
- expect(post).to be_deleted
349
- end
350
- end
351
-
352
- context "when the document is soft deleted" do
353
-
354
- before do
355
- post.destroy
356
- end
357
-
358
- it "returns true" do
359
- expect(post).to be_deleted
360
- end
361
- end
362
- end
363
-
364
- context "when the document is embedded" do
365
-
366
- let(:person) do
367
- Person.create
368
- end
369
-
370
- let(:phone) do
371
- person.paranoid_phones.create(number: "911")
372
- end
373
-
374
- context "when the document is hard deleted" do
375
-
376
- before do
377
- phone.destroy!
378
- end
379
-
380
- it "returns true" do
381
- expect(phone).to be_deleted
382
- end
383
- end
384
-
385
- context "when the document is soft deleted" do
386
-
387
- before do
388
- phone.destroy
389
- end
390
-
391
- it "returns true" do
392
- expect(phone).to be_deleted
393
- end
394
- end
395
-
396
- context "when the document has non-dependent relation" do
397
- let(:post) do
398
- ParanoidPost.create(title: "test")
399
- end
400
-
401
- let!(:tag) do
402
- post.tags.create(text: "tagie")
403
- end
404
-
405
- before do
406
- post.delete
407
- end
408
-
409
- it "doesn't cascades the dependent option" do
410
- expect(tag.reload).to eq(tag)
411
- end
412
-
413
- end
414
- end
415
- end
416
-
417
- describe "#delete!" do
418
-
419
- context "when the document is a root" do
420
-
421
- let(:post) do
422
- ParanoidPost.create(title: "testing")
423
- end
424
-
425
- before do
426
- post.delete!
427
- end
428
-
429
- let(:raw) do
430
- ParanoidPost.collection.find(_id: post.id).first
431
- end
432
-
433
- it "hard deletes the document" do
434
- expect(raw).to be_nil
435
- end
436
- end
437
-
438
- context "when the document is embedded" do
439
-
440
- let(:person) do
441
- Person.create
442
- end
443
-
444
- let(:phone) do
445
- person.paranoid_phones.create(number: "911")
446
- end
447
-
448
- before do
449
- phone.delete!
450
- end
451
-
452
- let(:raw) do
453
- Person.collection.find(_id: person.id).first
454
- end
455
-
456
- it "hard deletes the document" do
457
- expect(raw["paranoid_phones"]).to be_empty
458
- end
459
- end
460
-
461
- context "when the document has a dependent relation" do
462
-
463
- let(:post) do
464
- ParanoidPost.create(title: "test")
465
- end
466
-
467
- let!(:author) do
468
- post.authors.create(name: "poe")
469
- end
470
-
471
- before do
472
- post.delete!
473
- end
474
-
475
- it "cascades the dependent option" do
476
- expect {
477
- author.reload
478
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
479
- end
480
- end
481
- end
482
-
483
- describe "#delete" do
484
-
485
- context "when the document is a root" do
486
-
487
- let(:post) do
488
- ParanoidPost.create(title: "testing")
489
- end
490
-
491
- before do
492
- post.delete
493
- end
494
-
495
- let(:raw) do
496
- ParanoidPost.collection.find(_id: post.id).first
497
- end
498
-
499
- it "soft deletes the document" do
500
- expect(raw["deleted_at"]).to be_within(1).of(Time.now)
501
- end
502
-
503
- it "does not return the document in a find" do
504
- expect {
505
- ParanoidPost.find(post.id)
506
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
507
- end
508
- end
509
-
510
- context "when the document is embedded" do
511
-
512
- let(:person) do
513
- Person.create
514
- end
515
-
516
- let(:phone) do
517
- person.paranoid_phones.create(number: "911")
518
- end
519
-
520
- before do
521
- phone.delete
522
- end
523
-
524
- let(:raw) do
525
- Person.collection.find(_id: person.id).first
526
- end
527
-
528
- it "soft deletes the document" do
529
- expect(raw["paranoid_phones"].first["deleted_at"]).to be_within(1).of(Time.now)
530
- end
531
-
532
- it "does not return the document in a find" do
533
- expect {
534
- person.paranoid_phones.find(phone.id)
535
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
536
- end
537
-
538
- it "does not include the document in the relation" do
539
- expect(person.paranoid_phones.scoped).to be_empty
540
- end
541
- end
542
-
543
- context "when the document has a dependent relation" do
544
-
545
- let(:post) do
546
- ParanoidPost.create(title: "test")
547
- end
548
-
549
- let!(:author) do
550
- post.authors.create(name: "poe")
551
- end
552
-
553
- before do
554
- post.delete
555
- end
556
-
557
- it "cascades the dependent option" do
558
- expect {
559
- author.reload
560
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
561
- end
562
- end
563
-
564
- context "when the document has a dependent: :restrict relation" do
565
-
566
- let(:post) do
567
- ParanoidPost.create(title: "test")
568
- end
569
-
570
- let!(:title) do
571
- post.titles.create
572
- end
573
-
574
- before do
575
- begin
576
- post.delete
577
- rescue Mongoid::Errors::DeleteRestriction
578
- end
579
- end
580
-
581
- it "does not destroy the document" do
582
- expect(post).not_to be_destroyed
583
- end
584
- end
585
- end
586
-
587
- describe "#remove" do
588
-
589
- let(:post) do
590
- ParanoidPost.new
591
- end
592
-
593
- let!(:time) do
594
- Time.now
595
- end
596
-
597
- before do
598
- post.remove
599
- end
600
-
601
- it "sets the deleted flag" do
602
- expect(post).to be_destroyed
603
- end
604
- end
605
-
606
- describe "#restore" do
607
-
608
- context "when the document is a root" do
609
-
610
- let(:post) do
611
- ParanoidPost.create(title: "testing")
612
- end
613
-
614
- before do
615
- post.delete
616
- post.restore
617
- end
618
-
619
- it "removes the deleted at time" do
620
- expect(post.deleted_at).to be_nil
621
- end
622
-
623
- it "persists the change" do
624
- expect(post.reload.deleted_at).to be_nil
625
- end
626
-
627
- it "marks document again as persisted" do
628
- expect(post.persisted?).to be_truthy
629
- end
630
-
631
- context "will run callback" do
632
-
633
- it "before restore" do
634
- expect(post.before_restore_called).to be_truthy
635
- end
636
-
637
- it "after restore" do
638
- expect(post.after_restore_called).to be_truthy
639
- end
640
-
641
- it "around restore" do
642
- expect(post.around_before_restore_called).to be_truthy
643
- expect(post.around_after_restore_called).to be_truthy
644
- end
645
- end
646
-
647
- end
648
-
649
- context "when the document is embedded" do
650
-
651
- let(:person) do
652
- Person.create
653
- end
654
-
655
- let(:phone) do
656
- person.paranoid_phones.create(number: "911")
657
- end
658
-
659
- before do
660
- phone.delete
661
- phone.restore
662
- end
663
-
664
- it "removes the deleted at time" do
665
- expect(phone.deleted_at).to be_nil
666
- end
667
-
668
- it "persists the change" do
669
- expect(person.reload.paranoid_phones.first.deleted_at).to be_nil
670
- end
671
- end
672
- end
673
-
674
- describe ".scoped" do
675
-
676
- let(:scoped) do
677
- ParanoidPost.scoped
678
- end
679
-
680
- it "returns a scoped criteria" do
681
- expect(scoped.selector).to eq({ "deleted_at" => nil })
682
- end
683
- end
684
-
685
- describe "#set" do
686
-
687
- let!(:post) do
688
- ParanoidPost.create
689
- end
690
-
691
- let(:time) do
692
- 20.days.ago
693
- end
694
-
695
- let!(:set) do
696
- post.set(:deleted_at => time)
697
- end
698
-
699
- it "persists the change" do
700
- expect(post.reload.deleted_at).to be_within(1).of(time)
701
- end
702
- end
703
-
704
- describe ".unscoped" do
705
-
706
- let(:unscoped) do
707
- ParanoidPost.unscoped
708
- end
709
-
710
- it "returns an unscoped criteria" do
711
- expect(unscoped.selector).to eq({})
712
- end
713
- end
714
-
715
- describe "#to_param" do
716
-
717
- let(:post) do
718
- ParanoidPost.new(title: "testing")
719
- end
720
-
721
- context "when the document is new" do
722
-
723
- it "still returns nil" do
724
- expect(post.to_param).to be_nil
725
- end
726
- end
727
-
728
- context "when the document is not deleted" do
729
-
730
- before do
731
- post.save
732
- end
733
-
734
- it "returns the id as a string" do
735
- expect(post.to_param).to eq(post.id.to_s)
736
- end
737
- end
738
-
739
- context "when the document is deleted" do
740
-
741
- before do
742
- post.save
743
- post.delete
744
- end
745
-
746
- it "returns the id as a string" do
747
- expect(post.to_param).to eq(post.id.to_s)
748
- end
749
- end
750
- end
751
- end
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Paranoia do
4
+ context 'configuring the paranoid_field setting' do
5
+ before do
6
+ Mongoid::Paranoia.configure do |c|
7
+ c.paranoid_field = :myFieldName
8
+ end
9
+ end
10
+
11
+ describe '.configure' do
12
+ before do
13
+ class ParanoidConfigured
14
+ include Mongoid::Document
15
+ include Mongoid::Paranoia
16
+ end
17
+ end
18
+
19
+ it 'allows custom setting of the paranoid_field' do
20
+ paranoid_configured = ParanoidConfigured.new
21
+ expect(paranoid_configured.attribute_names).to include('myFieldName')
22
+ end
23
+
24
+ after(:each) do
25
+ Mongoid::Paranoia.reset
26
+ end
27
+ end
28
+
29
+ describe '.reset' do
30
+ before do
31
+ Mongoid::Paranoia.reset
32
+
33
+ # the configuration gets set at include time
34
+ # so you need to reset before defining a new class
35
+ class ParanoidConfiguredReset
36
+ include Mongoid::Document
37
+ include Mongoid::Paranoia
38
+ end
39
+ end
40
+
41
+ it 'restores the paranoid_field to the default setting' do
42
+ paranoid_configured = ParanoidConfiguredReset.new
43
+ expect(paranoid_configured.attribute_names).to include('deleted_at')
44
+ end
45
+ end
46
+ end
47
+
48
+ describe ".scoped" do
49
+
50
+ it "returns a scoped criteria" do
51
+ expect(ParanoidPost.scoped.selector).to eq({ "deleted_at" => nil })
52
+ end
53
+ end
54
+
55
+ describe ".deleted" do
56
+
57
+ context "when called on a root document" do
58
+
59
+ let(:post) do
60
+ ParanoidPost.create(title: "testing")
61
+ end
62
+
63
+ before do
64
+ post.destroy
65
+ end
66
+
67
+ let(:deleted) do
68
+ ParanoidPost.deleted
69
+ end
70
+
71
+ it "returns the deleted documents" do
72
+ expect(deleted).to eq([ post ])
73
+ end
74
+ end
75
+
76
+ context "when called on an embedded document" do
77
+
78
+ let(:person) do
79
+ Person.create
80
+ end
81
+
82
+ let(:phone) do
83
+ person.paranoid_phones.create
84
+ end
85
+
86
+ before do
87
+ phone.destroy
88
+ person.reload
89
+ end
90
+
91
+ it "returns the deleted documents" do
92
+ expect(person.paranoid_phones.deleted.to_a).to eq([ phone ])
93
+ end
94
+
95
+ it "returns the correct count" do
96
+ expect(person.paranoid_phones.deleted.count).to eq(1)
97
+ end
98
+ end
99
+ end
100
+
101
+ describe "#destroy!" do
102
+
103
+ context "when the document is a root" do
104
+
105
+ let(:post) do
106
+ ParanoidPost.create(title: "testing")
107
+ end
108
+
109
+ before do
110
+ post.destroy!
111
+ end
112
+
113
+ let(:raw) do
114
+ ParanoidPost.collection.find(_id: post.id).first
115
+ end
116
+
117
+ it "hard deletes the document" do
118
+ expect(raw).to be_nil
119
+ end
120
+
121
+ it "executes the before destroy callbacks" do
122
+ expect(post.before_destroy_called).to be_truthy
123
+ end
124
+
125
+ it "executes the after destroy callbacks" do
126
+ expect(post.after_destroy_called).to be_truthy
127
+ end
128
+
129
+ it "executes the before remove callbacks" do
130
+ expect(post.before_remove_called).to be_truthy
131
+ end
132
+
133
+ it "executes the after remove callbacks" do
134
+ expect(post.after_remove_called).to be_truthy
135
+ end
136
+ end
137
+
138
+ context "when the document is embedded" do
139
+
140
+ let(:person) do
141
+ Person.create
142
+ end
143
+
144
+ let(:phone) do
145
+ person.paranoid_phones.create(number: "911")
146
+ end
147
+
148
+ before do
149
+ phone.destroy!
150
+ end
151
+
152
+ let(:raw) do
153
+ Person.collection.find(_id: person.id).first
154
+ end
155
+
156
+ it "hard deletes the document" do
157
+ expect(raw["paranoid_phones"]).to be_empty
158
+ end
159
+
160
+ it "executes the before destroy callbacks" do
161
+ expect(phone.before_destroy_called).to be_truthy
162
+ end
163
+
164
+ it "executes the after destroy callbacks" do
165
+ expect(phone.after_destroy_called).to be_truthy
166
+ end
167
+ end
168
+
169
+ context "when the document has a dependent relation" do
170
+
171
+ let(:post) do
172
+ ParanoidPost.create(title: "test")
173
+ end
174
+
175
+ let!(:author) do
176
+ post.authors.create(name: "poe")
177
+ end
178
+
179
+ before do
180
+ post.destroy!
181
+ end
182
+
183
+ it "cascades the dependent option" do
184
+ expect {
185
+ author.reload
186
+ }.to raise_error(Mongoid::Errors::DocumentNotFound)
187
+ end
188
+ end
189
+ end
190
+
191
+ describe "#destroy" do
192
+
193
+ context "when the document is a root" do
194
+
195
+ let(:post) do
196
+ ParanoidPost.create(title: "testing")
197
+ end
198
+
199
+ before do
200
+ post.destroy
201
+ end
202
+
203
+ let(:raw) do
204
+ ParanoidPost.collection.find(_id: post.id).first
205
+ end
206
+
207
+ it "soft deletes the document" do
208
+ expect(raw["deleted_at"]).to be_within(1).of(Time.now)
209
+ end
210
+
211
+ it "is still marked as persisted" do
212
+ expect(post.persisted?).to eq(true)
213
+ end
214
+
215
+ it "does not return the document in a find" do
216
+ expect {
217
+ ParanoidPost.find(post.id)
218
+ }.to raise_error(Mongoid::Errors::DocumentNotFound)
219
+ end
220
+
221
+ it "executes the before destroy callbacks" do
222
+ expect(post.before_destroy_called).to be_truthy
223
+ end
224
+
225
+ it "executes the after destroy callbacks" do
226
+ expect(post.after_destroy_called).to be_truthy
227
+ end
228
+
229
+ it "does not execute the before remove callbacks" do
230
+ expect(post.before_remove_called).to be_falsey
231
+ end
232
+
233
+ it "does not execute the after remove callbacks" do
234
+ expect(post.after_remove_called).to be_falsey
235
+ end
236
+ end
237
+
238
+ context "when the document is embedded" do
239
+
240
+ let(:person) do
241
+ Person.create
242
+ end
243
+
244
+ let(:phone) do
245
+ person.paranoid_phones.create(number: "911")
246
+ end
247
+
248
+ before do
249
+ phone.destroy
250
+ end
251
+
252
+ let(:raw) do
253
+ Person.collection.find(_id: person.id).first
254
+ end
255
+
256
+ it "soft deletes the document" do
257
+ expect(raw["paranoid_phones"].first["deleted_at"]).to be_within(1).of(Time.now)
258
+ end
259
+
260
+ it "does not return the document in a find" do
261
+ expect {
262
+ person.paranoid_phones.find(phone.id)
263
+ }.to raise_error(Mongoid::Errors::DocumentNotFound)
264
+ end
265
+
266
+ it "does not include the document in the relation" do
267
+ expect(person.paranoid_phones.scoped).to be_empty
268
+ end
269
+
270
+ it "executes the before destroy callbacks" do
271
+ expect(phone.before_destroy_called).to be_truthy
272
+ end
273
+
274
+ it "executes the after destroy callbacks" do
275
+ expect(phone.after_destroy_called).to be_truthy
276
+ end
277
+ end
278
+
279
+ context "when the document has a dependent: :delete relation" do
280
+
281
+ let(:post) do
282
+ ParanoidPost.create(title: "test")
283
+ end
284
+
285
+ let!(:author) do
286
+ post.authors.create(name: "poe")
287
+ end
288
+
289
+ before do
290
+ post.destroy
291
+ end
292
+
293
+ it "cascades the dependent option" do
294
+ expect {
295
+ author.reload
296
+ }.to raise_error(Mongoid::Errors::DocumentNotFound)
297
+ end
298
+ end
299
+
300
+ context "when the document has a dependent: :restrict relation" do
301
+
302
+ let(:post) do
303
+ ParanoidPost.create(title: "test")
304
+ end
305
+
306
+ let!(:title) do
307
+ post.titles.create
308
+ end
309
+
310
+ before do
311
+ begin
312
+ post.destroy
313
+ rescue Mongoid::Errors::DeleteRestriction
314
+ end
315
+ end
316
+
317
+ it "does not destroy the document" do
318
+ expect(post).not_to be_destroyed
319
+ end
320
+ end
321
+ end
322
+
323
+ describe "#destroyed?" do
324
+
325
+ context "when the document is a root" do
326
+
327
+ let(:post) do
328
+ ParanoidPost.create(title: "testing")
329
+ end
330
+
331
+ context "when the document is hard deleted" do
332
+
333
+ before do
334
+ post.destroy!
335
+ end
336
+
337
+ it "returns true" do
338
+ expect(post).to be_destroyed
339
+ end
340
+ end
341
+
342
+ context "when the document is soft deleted" do
343
+
344
+ before do
345
+ post.destroy
346
+ end
347
+
348
+ it "returns true" do
349
+ expect(post).to be_destroyed
350
+ end
351
+
352
+ it "returns true for deleted scope document" do
353
+ expect(ParanoidPost.deleted.last).to be_destroyed
354
+ end
355
+ end
356
+ end
357
+
358
+ context "when the document is embedded" do
359
+
360
+ let(:person) do
361
+ Person.create
362
+ end
363
+
364
+ let(:phone) do
365
+ person.paranoid_phones.create(number: "911")
366
+ end
367
+
368
+ context "when the document is hard deleted" do
369
+
370
+ before do
371
+ phone.destroy!
372
+ end
373
+
374
+ it "returns true" do
375
+ expect(phone).to be_destroyed
376
+ end
377
+ end
378
+
379
+ context "when the document is soft deleted" do
380
+
381
+ before do
382
+ phone.destroy
383
+ end
384
+
385
+ it "returns true" do
386
+ expect(phone).to be_destroyed
387
+ end
388
+ end
389
+ end
390
+ end
391
+
392
+ describe "#deleted?" do
393
+
394
+ context "when the document is a root" do
395
+
396
+ let(:post) do
397
+ ParanoidPost.create(title: "testing")
398
+ end
399
+
400
+ context "when the document is hard deleted" do
401
+
402
+ before do
403
+ post.destroy!
404
+ end
405
+
406
+ it "returns true" do
407
+ expect(post).to be_deleted
408
+ end
409
+ end
410
+
411
+ context "when the document is soft deleted" do
412
+
413
+ before do
414
+ post.destroy
415
+ end
416
+
417
+ it "returns true" do
418
+ expect(post).to be_deleted
419
+ end
420
+ end
421
+ end
422
+
423
+ context "when the document is embedded" do
424
+
425
+ let(:person) do
426
+ Person.create
427
+ end
428
+
429
+ let(:phone) do
430
+ person.paranoid_phones.create(number: "911")
431
+ end
432
+
433
+ context "when the document is hard deleted" do
434
+
435
+ before do
436
+ phone.destroy!
437
+ end
438
+
439
+ it "returns true" do
440
+ expect(phone).to be_deleted
441
+ end
442
+ end
443
+
444
+ context "when the document is soft deleted" do
445
+
446
+ before do
447
+ phone.destroy
448
+ end
449
+
450
+ it "returns true" do
451
+ expect(phone).to be_deleted
452
+ end
453
+ end
454
+
455
+ context "when the document has non-dependent relation" do
456
+ let(:post) do
457
+ ParanoidPost.create(title: "test")
458
+ end
459
+
460
+ let!(:tag) do
461
+ post.tags.create(text: "tagie")
462
+ end
463
+
464
+ before do
465
+ post.delete
466
+ end
467
+
468
+ it "doesn't cascades the dependent option" do
469
+ expect(tag.reload).to eq(tag)
470
+ end
471
+
472
+ end
473
+ end
474
+ end
475
+
476
+ describe "#delete!" do
477
+
478
+ context "when the document is a root" do
479
+
480
+ let(:post) do
481
+ ParanoidPost.create(title: "testing")
482
+ end
483
+
484
+ before do
485
+ post.delete!
486
+ end
487
+
488
+ let(:raw) do
489
+ ParanoidPost.collection.find(_id: post.id).first
490
+ end
491
+
492
+ it "hard deletes the document" do
493
+ expect(raw).to be_nil
494
+ end
495
+ end
496
+
497
+ context "when the document is embedded" do
498
+
499
+ let(:person) do
500
+ Person.create
501
+ end
502
+
503
+ let(:phone) do
504
+ person.paranoid_phones.create(number: "911")
505
+ end
506
+
507
+ before do
508
+ phone.delete!
509
+ end
510
+
511
+ let(:raw) do
512
+ Person.collection.find(_id: person.id).first
513
+ end
514
+
515
+ it "hard deletes the document" do
516
+ expect(raw["paranoid_phones"]).to be_empty
517
+ end
518
+ end
519
+
520
+ context "when the document has a dependent relation" do
521
+
522
+ let(:post) do
523
+ ParanoidPost.create(title: "test")
524
+ end
525
+
526
+ let!(:author) do
527
+ post.authors.create(name: "poe")
528
+ end
529
+
530
+ before do
531
+ post.delete!
532
+ end
533
+
534
+ it "cascades the dependent option" do
535
+ expect {
536
+ author.reload
537
+ }.to raise_error(Mongoid::Errors::DocumentNotFound)
538
+ end
539
+ end
540
+ end
541
+
542
+ describe "#delete" do
543
+
544
+ context "when the document is a root" do
545
+
546
+ let(:post) do
547
+ ParanoidPost.create(title: "testing")
548
+ end
549
+
550
+ before do
551
+ post.delete
552
+ end
553
+
554
+ let(:raw) do
555
+ ParanoidPost.collection.find(_id: post.id).first
556
+ end
557
+
558
+ it "soft deletes the document" do
559
+ expect(raw["deleted_at"]).to be_within(1).of(Time.now)
560
+ end
561
+
562
+ it "does not return the document in a find" do
563
+ expect {
564
+ ParanoidPost.find(post.id)
565
+ }.to raise_error(Mongoid::Errors::DocumentNotFound)
566
+ end
567
+ end
568
+
569
+ context "when the document is embedded" do
570
+
571
+ let(:person) do
572
+ Person.create
573
+ end
574
+
575
+ let(:phone) do
576
+ person.paranoid_phones.create(number: "911")
577
+ end
578
+
579
+ before do
580
+ phone.delete
581
+ end
582
+
583
+ let(:raw) do
584
+ Person.collection.find(_id: person.id).first
585
+ end
586
+
587
+ it "soft deletes the document" do
588
+ expect(raw["paranoid_phones"].first["deleted_at"]).to be_within(1).of(Time.now)
589
+ end
590
+
591
+ it "does not return the document in a find" do
592
+ expect {
593
+ person.paranoid_phones.find(phone.id)
594
+ }.to raise_error(Mongoid::Errors::DocumentNotFound)
595
+ end
596
+
597
+ it "does not include the document in the relation" do
598
+ expect(person.paranoid_phones.scoped).to be_empty
599
+ end
600
+ end
601
+
602
+ context "when the document has a dependent relation" do
603
+
604
+ let(:post) do
605
+ ParanoidPost.create(title: "test")
606
+ end
607
+
608
+ let!(:author) do
609
+ post.authors.create(name: "poe")
610
+ end
611
+
612
+ before do
613
+ post.delete
614
+ end
615
+
616
+ it "cascades the dependent option" do
617
+ expect {
618
+ author.reload
619
+ }.to raise_error(Mongoid::Errors::DocumentNotFound)
620
+ end
621
+ end
622
+
623
+ context "when the document has a dependent: :restrict relation" do
624
+
625
+ let(:post) do
626
+ ParanoidPost.create(title: "test")
627
+ end
628
+
629
+ let!(:title) do
630
+ post.titles.create
631
+ end
632
+
633
+ before do
634
+ begin
635
+ post.delete
636
+ rescue Mongoid::Errors::DeleteRestriction
637
+ end
638
+ end
639
+
640
+ it "does not destroy the document" do
641
+ expect(post).not_to be_destroyed
642
+ end
643
+ end
644
+ end
645
+
646
+ describe "#remove" do
647
+
648
+ let(:post) do
649
+ ParanoidPost.new
650
+ end
651
+
652
+ let!(:time) do
653
+ Time.now
654
+ end
655
+
656
+ before do
657
+ post.remove
658
+ end
659
+
660
+ it "sets the deleted flag" do
661
+ expect(post).to be_destroyed
662
+ end
663
+ end
664
+
665
+ describe "#restore" do
666
+
667
+ context "when the document is a root" do
668
+
669
+ let(:post) do
670
+ ParanoidPost.create(title: "testing")
671
+ end
672
+
673
+ before do
674
+ post.delete
675
+ post.restore
676
+ end
677
+
678
+ it "removes the deleted at time" do
679
+ expect(post.deleted_at).to be_nil
680
+ end
681
+
682
+ it "persists the change" do
683
+ expect(post.reload.deleted_at).to be_nil
684
+ end
685
+
686
+ it "marks document again as persisted" do
687
+ expect(post.persisted?).to be_truthy
688
+ end
689
+
690
+ context "will run callback" do
691
+
692
+ it "before restore" do
693
+ expect(post.before_restore_called).to be_truthy
694
+ end
695
+
696
+ it "after restore" do
697
+ expect(post.after_restore_called).to be_truthy
698
+ end
699
+
700
+ it "around restore" do
701
+ expect(post.around_before_restore_called).to be_truthy
702
+ expect(post.around_after_restore_called).to be_truthy
703
+ end
704
+ end
705
+ end
706
+
707
+ context "when the document is embedded" do
708
+
709
+ let(:person) do
710
+ Person.create
711
+ end
712
+
713
+ let(:phone) do
714
+ person.paranoid_phones.create(number: "911")
715
+ end
716
+
717
+ before do
718
+ phone.delete
719
+ phone.restore
720
+ end
721
+
722
+ it "removes the deleted at time" do
723
+ expect(phone.deleted_at).to be_nil
724
+ end
725
+
726
+ it "persists the change" do
727
+ expect(person.reload.paranoid_phones.first.deleted_at).to be_nil
728
+ end
729
+ end
730
+ end
731
+
732
+ describe "#restore_relations" do
733
+
734
+ subject { ParaBase.create }
735
+
736
+ let!(:para_has_one) { subject.para_has_one = ParaHasOne.create }
737
+ let!(:para_has_many) { 2.times.map { subject.para_has_many.create } }
738
+ let!(:para_habtm) { 3.times.map { subject.para_habtm.create } }
739
+ let!(:para_belongs_to) { subject.para_belongs_to = ParaBelongsTo.create }
740
+ let!(:para_embeds_one) { subject.para_embeds_one = ParaEmbedsOne.new }
741
+ let!(:para_embeds_many) { 2.times.map { subject.para_embeds_many.build } }
742
+
743
+ let!(:norm_has_one) { subject.norm_has_one = NormHasOne.create }
744
+ let!(:norm_has_many) { 2.times.map { subject.norm_has_many.create } }
745
+ let!(:norm_habtm) { 3.times.map { subject.norm_habtm.create } }
746
+ let!(:norm_belongs_to) { subject.norm_belongs_to = NormBelongsTo.create }
747
+ let!(:norm_embeds_one) { subject.norm_embeds_one = NormEmbedsOne.new }
748
+ let!(:norm_embeds_many) { 2.times.map { subject.norm_embeds_many.build } }
749
+
750
+ let(:prepare) do
751
+ subject.destroy
752
+ subject.restore
753
+ end
754
+
755
+ context "restores paranoid associations" do
756
+ before { prepare }
757
+
758
+ it { expect{ subject.restore_relations}.to change{ ParaHasOne.count }.by(1) }
759
+ it { expect{ subject.restore_relations}.to change{ ParaHasMany.count }.by(2) }
760
+ it { expect{ subject.restore_relations}.to change{ ParaHabtm.count }.by(3) }
761
+ it { expect{ subject.restore_relations}.to change{ ParaBelongsTo.count }.by(1) }
762
+ end
763
+
764
+ context "does not affect embedded paranoid documents" do
765
+ before { prepare }
766
+
767
+ it { expect{ subject.restore_relations}.to_not change{ subject.para_embeds_one } }
768
+ it { expect{ subject.restore_relations}.to_not change{ subject.para_embeds_many.count } }
769
+ end
770
+
771
+ context "does not affect non-paranoid documents" do
772
+ before { prepare }
773
+
774
+ it { expect{ subject.restore_relations}.to_not change{ NormHasOne.count } }
775
+ it { expect{ subject.restore_relations}.to_not change{ NormHasMany.count } }
776
+ it { expect{ subject.restore_relations}.to_not change{ NormHabtm.count } }
777
+ it { expect{ subject.restore_relations}.to_not change{ NormBelongsTo.count } }
778
+ it { expect{ subject.restore_relations}.to_not change{ subject.norm_embeds_one } }
779
+ it { expect{ subject.restore_relations}.to_not change{ subject.norm_embeds_many.count } }
780
+ end
781
+
782
+ context "recursion" do
783
+
784
+ let!(:para_habtm_norm_has_one) { subject.para_habtm.first.norm_has_one = NormHasOne.create } # not restored
785
+ let!(:para_habtm_para_has_one) { subject.para_habtm.first.para_has_one = ParaHasOne.create } # restored
786
+ let!(:para_habtm_norm_has_many) { 2.times.map { subject.para_habtm.first.norm_has_many = NormHasMany.create } } # not restored
787
+ let!(:para_habtm_para_has_many) { 3.times.map { subject.para_habtm.second.para_has_many = ParaHasMany.create } } # restored
788
+
789
+ # Untestable due to infinite recursion condition in #destroy
790
+ # let!(:para_habtm_norm_habtm) { 3.times.map { subject.para_habtm.second.norm_habtm.create } } # not restored
791
+ # let!(:para_habtm_recursive) { 2.times.map { subject.para_habtm.first.recursive.create } } # restored
792
+
793
+ before do
794
+ subject.destroy
795
+ subject.restore
796
+ end
797
+
798
+ it { expect{ subject.restore_relations}.to change { ParaHasOne.count }.by(2) }
799
+ it { expect{ subject.restore_relations}.to change { ParaHasMany.count }.by(3) }
800
+
801
+ # Untestable due to infinite recursion condition in #destroy
802
+ # it { expect{ ParaHabtm.unscoped.each(&:restore)}.to change { ParaHabtm.count }.by(5) }
803
+
804
+ it { expect{ subject.restore_relations}.to_not change { NormHasOne.count } }
805
+ it { expect{ subject.restore_relations}.to_not change { NormHasMany.count } }
806
+ it { expect{ subject.restore_relations}.to_not change { NormHabtm.count } }
807
+ end
808
+ end
809
+
810
+ describe ".scoped" do
811
+
812
+ let(:scoped) do
813
+ ParanoidPost.scoped
814
+ end
815
+
816
+ it "returns a scoped criteria" do
817
+ expect(scoped.selector).to eq({ "deleted_at" => nil })
818
+ end
819
+ end
820
+
821
+ describe "#set" do
822
+
823
+ let!(:post) do
824
+ ParanoidPost.create
825
+ end
826
+
827
+ let(:time) do
828
+ 20.days.ago
829
+ end
830
+
831
+ let!(:set) do
832
+ post.set(:deleted_at => time)
833
+ end
834
+
835
+ it "persists the change" do
836
+ expect(post.reload.deleted_at).to be_within(1).of(time)
837
+ end
838
+ end
839
+
840
+ describe ".unscoped" do
841
+
842
+ let(:unscoped) do
843
+ ParanoidPost.unscoped
844
+ end
845
+
846
+ it "returns an unscoped criteria" do
847
+ expect(unscoped.selector).to eq({})
848
+ end
849
+ end
850
+
851
+ describe "#to_param" do
852
+
853
+ let(:post) do
854
+ ParanoidPost.new(title: "testing")
855
+ end
856
+
857
+ context "when the document is new" do
858
+
859
+ it "still returns nil" do
860
+ expect(post.to_param).to be_nil
861
+ end
862
+ end
863
+
864
+ context "when the document is not deleted" do
865
+
866
+ before do
867
+ post.save
868
+ end
869
+
870
+ it "returns the id as a string" do
871
+ expect(post.to_param).to eq(post.id.to_s)
872
+ end
873
+ end
874
+
875
+ context "when the document is deleted" do
876
+
877
+ before do
878
+ post.save
879
+ post.delete
880
+ end
881
+
882
+ it "returns the id as a string" do
883
+ expect(post.to_param).to eq(post.id.to_s)
884
+ end
885
+ end
886
+ end
887
+ end