mongoid-paranoia 1.3.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +20 -0
- data/.travis.yml +2 -1
- data/README.md +1 -1
- data/lib/mongoid/core_ext/relations/embedded/many.rb +0 -1
- data/lib/mongoid/core_ext/validatable/uniqueness.rb +1 -3
- data/lib/mongoid/paranoia.rb +12 -13
- data/lib/mongoid/paranoia/version.rb +1 -1
- data/mongoid-paranoia.gemspec +3 -3
- data/spec/app/models/address.rb +9 -8
- data/spec/app/models/appointment.rb +1 -1
- data/spec/app/models/paranoid_post.rb +2 -2
- data/spec/app/models/person.rb +25 -21
- data/spec/app/models/phone.rb +1 -1
- data/spec/app/models/service.rb +1 -1
- data/spec/app/models/symptom.rb +1 -1
- data/spec/app/models/tag.rb +1 -1
- data/spec/app/models/video.rb +1 -1
- data/spec/mongoid/attributes/nested_spec.rb +39 -57
- data/spec/mongoid/criteria/scopable_spec.rb +14 -24
- data/spec/mongoid/document_spec.rb +6 -8
- data/spec/mongoid/paranoia_spec.rb +153 -201
- data/spec/mongoid/scoping_spec.rb +14 -24
- data/spec/mongoid/validatable/uniqueness_spec.rb +14 -21
- data/spec/spec_helper.rb +18 -21
- metadata +8 -4
data/spec/app/models/video.rb
CHANGED
@@ -1,47 +1,38 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Mongoid::Attributes::Nested do
|
4
|
-
|
5
4
|
describe "##{name}_attributes=" do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
context "when the relation is an embeds many" do
|
10
|
-
|
5
|
+
context 'when the parent document is new' do
|
6
|
+
context 'when the relation is an embeds many' do
|
11
7
|
let(:person) do
|
12
8
|
Person.new
|
13
9
|
end
|
14
10
|
|
15
11
|
let(:address_one) do
|
16
|
-
Address.new(street:
|
12
|
+
Address.new(street: 'Unter den Linden')
|
17
13
|
end
|
18
14
|
|
19
15
|
let(:address_two) do
|
20
|
-
Address.new(street:
|
16
|
+
Address.new(street: 'Kurfeurstendamm')
|
21
17
|
end
|
22
18
|
|
23
19
|
let(:phone_one) do
|
24
|
-
ParanoidPhone.new(number:
|
20
|
+
ParanoidPhone.new(number: '1')
|
25
21
|
end
|
26
22
|
|
27
23
|
let(:phone_two) do
|
28
|
-
ParanoidPhone.new(number:
|
24
|
+
ParanoidPhone.new(number: '2')
|
29
25
|
end
|
30
26
|
|
31
|
-
context
|
32
|
-
|
27
|
+
context 'when ids are passed' do
|
33
28
|
before do
|
34
|
-
person.addresses << [
|
29
|
+
person.addresses << [address_one, address_two]
|
35
30
|
end
|
36
31
|
|
37
|
-
context
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
context "when allow_destroy is true" do
|
42
|
-
|
43
|
-
context "when the child has defaults" do
|
44
|
-
|
32
|
+
context 'when destroy attributes are passed' do
|
33
|
+
context 'when the ids match' do
|
34
|
+
context 'when allow_destroy is true' do
|
35
|
+
context 'when the child has defaults' do
|
45
36
|
before(:all) do
|
46
37
|
Person.accepts_nested_attributes_for :appointments, allow_destroy: true
|
47
38
|
end
|
@@ -50,16 +41,13 @@ describe Mongoid::Attributes::Nested do
|
|
50
41
|
Person.send(:undef_method, :appointments_attributes=)
|
51
42
|
end
|
52
43
|
|
53
|
-
context
|
54
|
-
|
44
|
+
context 'when the parent is persisted' do
|
55
45
|
let!(:persisted) do
|
56
46
|
Person.create(age: 42)
|
57
47
|
end
|
58
48
|
|
59
|
-
context
|
60
|
-
|
61
|
-
context "when the child is paranoid" do
|
62
|
-
|
49
|
+
context 'when the child returns false in a before callback' do
|
50
|
+
context 'when the child is paranoid' do
|
63
51
|
before(:all) do
|
64
52
|
Person.accepts_nested_attributes_for :paranoid_phones, allow_destroy: true
|
65
53
|
end
|
@@ -75,10 +63,10 @@ describe Mongoid::Attributes::Nested do
|
|
75
63
|
|
76
64
|
before do
|
77
65
|
persisted.paranoid_phones_attributes =
|
78
|
-
{
|
66
|
+
{ 'foo' => { 'id' => phone.id, 'number' => 42, '_destroy' => true } }
|
79
67
|
end
|
80
68
|
|
81
|
-
it
|
69
|
+
it 'does not destroy the child' do
|
82
70
|
expect(persisted.reload.paranoid_phones).not_to be_empty
|
83
71
|
end
|
84
72
|
end
|
@@ -86,8 +74,7 @@ describe Mongoid::Attributes::Nested do
|
|
86
74
|
end
|
87
75
|
end
|
88
76
|
|
89
|
-
context
|
90
|
-
|
77
|
+
context 'when the child is paranoid' do
|
91
78
|
before(:all) do
|
92
79
|
Person.send(:undef_method, :paranoid_phones_attributes=)
|
93
80
|
Person.accepts_nested_attributes_for :paranoid_phones,
|
@@ -99,65 +86,60 @@ describe Mongoid::Attributes::Nested do
|
|
99
86
|
Person.accepts_nested_attributes_for :paranoid_phones
|
100
87
|
end
|
101
88
|
|
102
|
-
[
|
103
|
-
|
89
|
+
[1, '1', true, 'true'].each do |truth|
|
104
90
|
context "when passed a #{truth} with destroy" do
|
105
|
-
|
106
|
-
context "when the parent is persisted" do
|
107
|
-
|
91
|
+
context 'when the parent is persisted' do
|
108
92
|
let!(:persisted) do
|
109
93
|
Person.create do |p|
|
110
|
-
p.paranoid_phones << [
|
94
|
+
p.paranoid_phones << [phone_one, phone_two]
|
111
95
|
end
|
112
96
|
end
|
113
97
|
|
114
|
-
context
|
115
|
-
|
98
|
+
context 'when setting, pulling, and pushing in one op' do
|
116
99
|
before do
|
117
100
|
persisted.paranoid_phones_attributes =
|
118
101
|
{
|
119
|
-
|
120
|
-
|
121
|
-
|
102
|
+
'bar' => { 'id' => phone_one.id, '_destroy' => truth },
|
103
|
+
'foo' => { 'id' => phone_two.id, 'number' => '3' },
|
104
|
+
'baz' => { 'number' => '4' }
|
122
105
|
}
|
123
106
|
end
|
124
107
|
|
125
|
-
it
|
108
|
+
it 'removes the first document from the relation' do
|
126
109
|
expect(persisted.paranoid_phones.size).to eq(2)
|
127
110
|
end
|
128
111
|
|
129
|
-
it
|
130
|
-
expect(persisted.paranoid_phones.first.number).to eq(
|
112
|
+
it 'does not delete the unmarked document' do
|
113
|
+
expect(persisted.paranoid_phones.first.number).to eq('3')
|
131
114
|
end
|
132
115
|
|
133
|
-
it
|
134
|
-
expect(persisted.paranoid_phones.last.number).to eq(
|
116
|
+
it 'adds the new document to the relation' do
|
117
|
+
expect(persisted.paranoid_phones.last.number).to eq('4')
|
135
118
|
end
|
136
119
|
|
137
|
-
it
|
120
|
+
it 'has the proper persisted count' do
|
138
121
|
expect(persisted.paranoid_phones.count).to eq(1)
|
139
122
|
end
|
140
123
|
|
141
|
-
it
|
124
|
+
it 'soft deletes the removed document' do
|
142
125
|
expect(phone_one).to be_destroyed
|
143
126
|
end
|
144
127
|
|
145
|
-
context
|
146
|
-
|
128
|
+
context 'when saving the parent' do
|
147
129
|
before do
|
148
130
|
persisted.with(safe: true).save
|
149
131
|
end
|
150
132
|
|
151
|
-
it
|
133
|
+
it 'deletes the marked document from the relation' do
|
152
134
|
expect(persisted.reload.paranoid_phones.count).to eq(2)
|
153
135
|
end
|
154
136
|
|
155
|
-
it
|
156
|
-
expect(persisted.reload.paranoid_phones.first.number).to eq(
|
137
|
+
it 'does not delete the unmarked document' do
|
138
|
+
expect(persisted.reload.paranoid_phones.first.number).to eq('3')
|
157
139
|
end
|
158
140
|
|
159
|
-
it
|
160
|
-
expect(persisted.reload.paranoid_phones.last.number).to eq(
|
141
|
+
it 'persists the new document to the relation' do
|
142
|
+
expect(persisted.reload.paranoid_phones.last.number).to eq('4')
|
161
143
|
end
|
162
144
|
end
|
163
145
|
end
|
@@ -1,54 +1,44 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Mongoid::Criteria::Scopable do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
context "when calling a class method" do
|
8
|
-
|
4
|
+
context 'when the document is paranoid' do
|
5
|
+
context 'when calling a class method' do
|
9
6
|
let(:criteria) do
|
10
7
|
Fish.fresh
|
11
8
|
end
|
12
9
|
|
13
|
-
it
|
14
|
-
expect(criteria.selector).to eq(
|
15
|
-
"deleted_at" => nil, "fresh" => true
|
16
|
-
})
|
10
|
+
it 'includes the deleted_at criteria in the selector' do
|
11
|
+
expect(criteria.selector).to eq('deleted_at' => nil, 'fresh' => true)
|
17
12
|
end
|
18
13
|
end
|
19
14
|
|
20
|
-
context
|
21
|
-
|
15
|
+
context 'when chaining a class method to unscoped' do
|
22
16
|
let(:criteria) do
|
23
17
|
Fish.unscoped.fresh
|
24
18
|
end
|
25
19
|
|
26
|
-
it
|
27
|
-
expect(criteria.selector).to eq(
|
20
|
+
it 'does not include the deleted_at in the selector' do
|
21
|
+
expect(criteria.selector).to eq('fresh' => true)
|
28
22
|
end
|
29
23
|
end
|
30
24
|
|
31
|
-
context
|
32
|
-
|
25
|
+
context 'when chaining a class method to deleted' do
|
33
26
|
let(:criteria) do
|
34
27
|
Fish.deleted.fresh
|
35
28
|
end
|
36
29
|
|
37
|
-
it
|
38
|
-
expect(criteria.selector).to eq({
|
39
|
-
"deleted_at" => { "$ne" => nil }, "fresh" => true
|
40
|
-
})
|
30
|
+
it 'includes the deleted_at $ne criteria in the selector' do
|
31
|
+
expect(criteria.selector).to eq('deleted_at' => { '$ne' => nil }, 'fresh' => true)
|
41
32
|
end
|
42
33
|
end
|
43
34
|
|
44
|
-
context
|
45
|
-
|
35
|
+
context 'when chaining a where to unscoped' do
|
46
36
|
let(:criteria) do
|
47
37
|
Fish.unscoped.where(fresh: true)
|
48
38
|
end
|
49
39
|
|
50
|
-
it
|
51
|
-
expect(criteria.selector).to eq(
|
40
|
+
it 'includes no default scoping information in the selector' do
|
41
|
+
expect(criteria.selector).to eq('fresh' => true)
|
52
42
|
end
|
53
43
|
end
|
54
44
|
end
|
@@ -1,19 +1,17 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative '../spec_helper'
|
2
2
|
|
3
3
|
describe Mongoid::Document do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
context "when Mongoid::Paranoia is included" do
|
4
|
+
describe '.paranoid?' do
|
5
|
+
context 'when Mongoid::Paranoia is included' do
|
8
6
|
subject { ParanoidPost }
|
9
|
-
it
|
7
|
+
it 'returns true' do
|
10
8
|
expect(subject.paranoid?).to be_truthy
|
11
9
|
end
|
12
10
|
end
|
13
11
|
|
14
|
-
context
|
12
|
+
context 'when Mongoid::Paranoia not included' do
|
15
13
|
subject { Author }
|
16
|
-
it
|
14
|
+
it 'returns true' do
|
17
15
|
expect(subject.paranoid?).to be_falsey
|
18
16
|
end
|
19
17
|
end
|
@@ -1,20 +1,16 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative '../spec_helper'
|
2
2
|
|
3
3
|
describe Mongoid::Paranoia do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
it "returns a scoped criteria" do
|
8
|
-
expect(ParanoidPost.scoped.selector).to eq({ "deleted_at" => nil })
|
4
|
+
describe '.scoped' do
|
5
|
+
it 'returns a scoped criteria' do
|
6
|
+
expect(ParanoidPost.scoped.selector).to eq('deleted_at' => nil)
|
9
7
|
end
|
10
8
|
end
|
11
9
|
|
12
|
-
describe
|
13
|
-
|
14
|
-
context "when called on a root document" do
|
15
|
-
|
10
|
+
describe '.deleted' do
|
11
|
+
context 'when called on a root document' do
|
16
12
|
let(:post) do
|
17
|
-
ParanoidPost.create(title:
|
13
|
+
ParanoidPost.create(title: 'testing')
|
18
14
|
end
|
19
15
|
|
20
16
|
before do
|
@@ -25,13 +21,12 @@ describe Mongoid::Paranoia do
|
|
25
21
|
ParanoidPost.deleted
|
26
22
|
end
|
27
23
|
|
28
|
-
it
|
29
|
-
expect(deleted).to eq([
|
24
|
+
it 'returns the deleted documents' do
|
25
|
+
expect(deleted).to eq([post])
|
30
26
|
end
|
31
27
|
end
|
32
28
|
|
33
|
-
context
|
34
|
-
|
29
|
+
context 'when called on an embedded document' do
|
35
30
|
let(:person) do
|
36
31
|
Person.create
|
37
32
|
end
|
@@ -45,22 +40,20 @@ describe Mongoid::Paranoia do
|
|
45
40
|
person.reload
|
46
41
|
end
|
47
42
|
|
48
|
-
it
|
49
|
-
expect(person.paranoid_phones.deleted.to_a).to eq([
|
43
|
+
it 'returns the deleted documents' do
|
44
|
+
expect(person.paranoid_phones.deleted.to_a).to eq([phone])
|
50
45
|
end
|
51
46
|
|
52
|
-
it
|
47
|
+
it 'returns the correct count' do
|
53
48
|
expect(person.paranoid_phones.deleted.count).to eq(1)
|
54
49
|
end
|
55
50
|
end
|
56
51
|
end
|
57
52
|
|
58
|
-
describe
|
59
|
-
|
60
|
-
context "when the document is a root" do
|
61
|
-
|
53
|
+
describe '#destroy!' do
|
54
|
+
context 'when the document is a root' do
|
62
55
|
let(:post) do
|
63
|
-
ParanoidPost.create(title:
|
56
|
+
ParanoidPost.create(title: 'testing')
|
64
57
|
end
|
65
58
|
|
66
59
|
before do
|
@@ -71,27 +64,26 @@ describe Mongoid::Paranoia do
|
|
71
64
|
ParanoidPost.collection.find(_id: post.id).first
|
72
65
|
end
|
73
66
|
|
74
|
-
it
|
67
|
+
it 'hard deletes the document' do
|
75
68
|
expect(raw).to be_nil
|
76
69
|
end
|
77
70
|
|
78
|
-
it
|
71
|
+
it 'executes the before destroy callbacks' do
|
79
72
|
expect(post.before_destroy_called).to be_truthy
|
80
73
|
end
|
81
74
|
|
82
|
-
it
|
75
|
+
it 'executes the after destroy callbacks' do
|
83
76
|
expect(post.after_destroy_called).to be_truthy
|
84
77
|
end
|
85
78
|
end
|
86
79
|
|
87
|
-
context
|
88
|
-
|
80
|
+
context 'when the document is embedded' do
|
89
81
|
let(:person) do
|
90
82
|
Person.create
|
91
83
|
end
|
92
84
|
|
93
85
|
let(:phone) do
|
94
|
-
person.paranoid_phones.create(number:
|
86
|
+
person.paranoid_phones.create(number: '911')
|
95
87
|
end
|
96
88
|
|
97
89
|
before do
|
@@ -102,47 +94,44 @@ describe Mongoid::Paranoia do
|
|
102
94
|
Person.collection.find(_id: person.id).first
|
103
95
|
end
|
104
96
|
|
105
|
-
it
|
106
|
-
expect(raw[
|
97
|
+
it 'hard deletes the document' do
|
98
|
+
expect(raw['paranoid_phones']).to be_empty
|
107
99
|
end
|
108
100
|
|
109
|
-
it
|
101
|
+
it 'executes the before destroy callbacks' do
|
110
102
|
expect(phone.before_destroy_called).to be_truthy
|
111
103
|
end
|
112
104
|
|
113
|
-
it
|
105
|
+
it 'executes the after destroy callbacks' do
|
114
106
|
expect(phone.after_destroy_called).to be_truthy
|
115
107
|
end
|
116
108
|
end
|
117
109
|
|
118
|
-
context
|
119
|
-
|
110
|
+
context 'when the document has a dependent relation' do
|
120
111
|
let(:post) do
|
121
|
-
ParanoidPost.create(title:
|
112
|
+
ParanoidPost.create(title: 'test')
|
122
113
|
end
|
123
114
|
|
124
115
|
let!(:author) do
|
125
|
-
post.authors.create(name:
|
116
|
+
post.authors.create(name: 'poe')
|
126
117
|
end
|
127
118
|
|
128
119
|
before do
|
129
120
|
post.destroy!
|
130
121
|
end
|
131
122
|
|
132
|
-
it
|
133
|
-
expect
|
123
|
+
it 'cascades the dependent option' do
|
124
|
+
expect do
|
134
125
|
author.reload
|
135
|
-
|
126
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound)
|
136
127
|
end
|
137
128
|
end
|
138
129
|
end
|
139
130
|
|
140
|
-
describe
|
141
|
-
|
142
|
-
context "when the document is a root" do
|
143
|
-
|
131
|
+
describe '#destroy' do
|
132
|
+
context 'when the document is a root' do
|
144
133
|
let(:post) do
|
145
|
-
ParanoidPost.create(title:
|
134
|
+
ParanoidPost.create(title: 'testing')
|
146
135
|
end
|
147
136
|
|
148
137
|
before do
|
@@ -153,33 +142,32 @@ describe Mongoid::Paranoia do
|
|
153
142
|
ParanoidPost.collection.find(_id: post.id).first
|
154
143
|
end
|
155
144
|
|
156
|
-
it
|
157
|
-
expect(raw[
|
145
|
+
it 'soft deletes the document' do
|
146
|
+
expect(raw['deleted_at']).to be_within(1).of(Time.now)
|
158
147
|
end
|
159
148
|
|
160
|
-
it
|
161
|
-
expect
|
149
|
+
it 'does not return the document in a find' do
|
150
|
+
expect do
|
162
151
|
ParanoidPost.find(post.id)
|
163
|
-
|
152
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound)
|
164
153
|
end
|
165
154
|
|
166
|
-
it
|
155
|
+
it 'executes the before destroy callbacks' do
|
167
156
|
expect(post.before_destroy_called).to be_truthy
|
168
157
|
end
|
169
158
|
|
170
|
-
it
|
159
|
+
it 'executes the after destroy callbacks' do
|
171
160
|
expect(post.after_destroy_called).to be_truthy
|
172
161
|
end
|
173
162
|
end
|
174
163
|
|
175
|
-
context
|
176
|
-
|
164
|
+
context 'when the document is embedded' do
|
177
165
|
let(:person) do
|
178
166
|
Person.create
|
179
167
|
end
|
180
168
|
|
181
169
|
let(:phone) do
|
182
|
-
person.paranoid_phones.create(number:
|
170
|
+
person.paranoid_phones.create(number: '911')
|
183
171
|
end
|
184
172
|
|
185
173
|
before do
|
@@ -190,54 +178,52 @@ describe Mongoid::Paranoia do
|
|
190
178
|
Person.collection.find(_id: person.id).first
|
191
179
|
end
|
192
180
|
|
193
|
-
it
|
194
|
-
expect(raw[
|
181
|
+
it 'soft deletes the document' do
|
182
|
+
expect(raw['paranoid_phones'].first['deleted_at']).to be_within(1).of(Time.now)
|
195
183
|
end
|
196
184
|
|
197
|
-
it
|
198
|
-
expect
|
185
|
+
it 'does not return the document in a find' do
|
186
|
+
expect do
|
199
187
|
person.paranoid_phones.find(phone.id)
|
200
|
-
|
188
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound)
|
201
189
|
end
|
202
190
|
|
203
|
-
it
|
191
|
+
it 'does not include the document in the relation' do
|
204
192
|
expect(person.paranoid_phones.scoped).to be_empty
|
205
193
|
end
|
206
194
|
|
207
|
-
it
|
195
|
+
it 'executes the before destroy callbacks' do
|
208
196
|
expect(phone.before_destroy_called).to be_truthy
|
209
197
|
end
|
210
198
|
|
211
|
-
it
|
199
|
+
it 'executes the after destroy callbacks' do
|
212
200
|
expect(phone.after_destroy_called).to be_truthy
|
213
201
|
end
|
214
202
|
end
|
215
203
|
|
216
|
-
context
|
217
|
-
|
204
|
+
context 'when the document has a dependent: :delete relation' do
|
218
205
|
let(:post) do
|
219
|
-
ParanoidPost.create(title:
|
206
|
+
ParanoidPost.create(title: 'test')
|
220
207
|
end
|
221
208
|
|
222
209
|
let!(:author) do
|
223
|
-
post.authors.create(name:
|
210
|
+
post.authors.create(name: 'poe')
|
224
211
|
end
|
225
212
|
|
226
213
|
before do
|
227
214
|
post.destroy
|
228
215
|
end
|
229
216
|
|
230
|
-
it
|
231
|
-
expect
|
217
|
+
it 'cascades the dependent option' do
|
218
|
+
expect do
|
232
219
|
author.reload
|
233
|
-
|
220
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound)
|
234
221
|
end
|
235
222
|
end
|
236
223
|
|
237
|
-
context
|
238
|
-
|
224
|
+
context 'when the document has a dependent: :restrict relation' do
|
239
225
|
let(:post) do
|
240
|
-
ParanoidPost.create(title:
|
226
|
+
ParanoidPost.create(title: 'test')
|
241
227
|
end
|
242
228
|
|
243
229
|
let!(:title) do
|
@@ -251,148 +237,132 @@ describe Mongoid::Paranoia do
|
|
251
237
|
end
|
252
238
|
end
|
253
239
|
|
254
|
-
it
|
240
|
+
it 'does not destroy the document' do
|
255
241
|
expect(post).not_to be_destroyed
|
256
242
|
end
|
257
243
|
end
|
258
244
|
end
|
259
245
|
|
260
|
-
describe
|
261
|
-
|
262
|
-
context "when the document is a root" do
|
263
|
-
|
246
|
+
describe '#destroyed?' do
|
247
|
+
context 'when the document is a root' do
|
264
248
|
let(:post) do
|
265
|
-
ParanoidPost.create(title:
|
249
|
+
ParanoidPost.create(title: 'testing')
|
266
250
|
end
|
267
251
|
|
268
|
-
context
|
269
|
-
|
252
|
+
context 'when the document is hard deleted' do
|
270
253
|
before do
|
271
254
|
post.destroy!
|
272
255
|
end
|
273
256
|
|
274
|
-
it
|
257
|
+
it 'returns true' do
|
275
258
|
expect(post).to be_destroyed
|
276
259
|
end
|
277
260
|
end
|
278
261
|
|
279
|
-
context
|
280
|
-
|
262
|
+
context 'when the document is soft deleted' do
|
281
263
|
before do
|
282
264
|
post.destroy
|
283
265
|
end
|
284
266
|
|
285
|
-
it
|
267
|
+
it 'returns true' do
|
286
268
|
expect(post).to be_destroyed
|
287
269
|
end
|
288
270
|
end
|
289
271
|
end
|
290
272
|
|
291
|
-
context
|
292
|
-
|
273
|
+
context 'when the document is embedded' do
|
293
274
|
let(:person) do
|
294
275
|
Person.create
|
295
276
|
end
|
296
277
|
|
297
278
|
let(:phone) do
|
298
|
-
person.paranoid_phones.create(number:
|
279
|
+
person.paranoid_phones.create(number: '911')
|
299
280
|
end
|
300
281
|
|
301
|
-
context
|
302
|
-
|
282
|
+
context 'when the document is hard deleted' do
|
303
283
|
before do
|
304
284
|
phone.destroy!
|
305
285
|
end
|
306
286
|
|
307
|
-
it
|
287
|
+
it 'returns true' do
|
308
288
|
expect(phone).to be_destroyed
|
309
289
|
end
|
310
290
|
end
|
311
291
|
|
312
|
-
context
|
313
|
-
|
292
|
+
context 'when the document is soft deleted' do
|
314
293
|
before do
|
315
294
|
phone.destroy
|
316
295
|
end
|
317
296
|
|
318
|
-
it
|
297
|
+
it 'returns true' do
|
319
298
|
expect(phone).to be_destroyed
|
320
299
|
end
|
321
300
|
end
|
322
301
|
end
|
323
302
|
end
|
324
303
|
|
325
|
-
describe
|
326
|
-
|
327
|
-
context "when the document is a root" do
|
328
|
-
|
304
|
+
describe '#deleted?' do
|
305
|
+
context 'when the document is a root' do
|
329
306
|
let(:post) do
|
330
|
-
ParanoidPost.create(title:
|
307
|
+
ParanoidPost.create(title: 'testing')
|
331
308
|
end
|
332
309
|
|
333
|
-
context
|
334
|
-
|
310
|
+
context 'when the document is hard deleted' do
|
335
311
|
before do
|
336
312
|
post.destroy!
|
337
313
|
end
|
338
314
|
|
339
|
-
it
|
315
|
+
it 'returns true' do
|
340
316
|
expect(post).to be_deleted
|
341
317
|
end
|
342
318
|
end
|
343
319
|
|
344
|
-
context
|
345
|
-
|
320
|
+
context 'when the document is soft deleted' do
|
346
321
|
before do
|
347
322
|
post.destroy
|
348
323
|
end
|
349
324
|
|
350
|
-
it
|
325
|
+
it 'returns true' do
|
351
326
|
expect(post).to be_deleted
|
352
327
|
end
|
353
328
|
end
|
354
329
|
end
|
355
330
|
|
356
|
-
context
|
357
|
-
|
331
|
+
context 'when the document is embedded' do
|
358
332
|
let(:person) do
|
359
333
|
Person.create
|
360
334
|
end
|
361
335
|
|
362
336
|
let(:phone) do
|
363
|
-
person.paranoid_phones.create(number:
|
337
|
+
person.paranoid_phones.create(number: '911')
|
364
338
|
end
|
365
339
|
|
366
|
-
context
|
367
|
-
|
340
|
+
context 'when the document is hard deleted' do
|
368
341
|
before do
|
369
342
|
phone.destroy!
|
370
343
|
end
|
371
344
|
|
372
|
-
it
|
345
|
+
it 'returns true' do
|
373
346
|
expect(phone).to be_deleted
|
374
347
|
end
|
375
348
|
end
|
376
349
|
|
377
|
-
context
|
378
|
-
|
350
|
+
context 'when the document is soft deleted' do
|
379
351
|
before do
|
380
352
|
phone.destroy
|
381
353
|
end
|
382
354
|
|
383
|
-
it
|
355
|
+
it 'returns true' do
|
384
356
|
expect(phone).to be_deleted
|
385
357
|
end
|
386
358
|
end
|
387
359
|
end
|
388
360
|
end
|
389
361
|
|
390
|
-
describe
|
391
|
-
|
392
|
-
context "when the document is a root" do
|
393
|
-
|
362
|
+
describe '#delete!' do
|
363
|
+
context 'when the document is a root' do
|
394
364
|
let(:post) do
|
395
|
-
ParanoidPost.create(title:
|
365
|
+
ParanoidPost.create(title: 'testing')
|
396
366
|
end
|
397
367
|
|
398
368
|
before do
|
@@ -403,19 +373,18 @@ describe Mongoid::Paranoia do
|
|
403
373
|
ParanoidPost.collection.find(_id: post.id).first
|
404
374
|
end
|
405
375
|
|
406
|
-
it
|
376
|
+
it 'hard deletes the document' do
|
407
377
|
expect(raw).to be_nil
|
408
378
|
end
|
409
379
|
end
|
410
380
|
|
411
|
-
context
|
412
|
-
|
381
|
+
context 'when the document is embedded' do
|
413
382
|
let(:person) do
|
414
383
|
Person.create
|
415
384
|
end
|
416
385
|
|
417
386
|
let(:phone) do
|
418
|
-
person.paranoid_phones.create(number:
|
387
|
+
person.paranoid_phones.create(number: '911')
|
419
388
|
end
|
420
389
|
|
421
390
|
before do
|
@@ -426,39 +395,36 @@ describe Mongoid::Paranoia do
|
|
426
395
|
Person.collection.find(_id: person.id).first
|
427
396
|
end
|
428
397
|
|
429
|
-
it
|
430
|
-
expect(raw[
|
398
|
+
it 'hard deletes the document' do
|
399
|
+
expect(raw['paranoid_phones']).to be_empty
|
431
400
|
end
|
432
401
|
end
|
433
402
|
|
434
|
-
context
|
435
|
-
|
403
|
+
context 'when the document has a dependent relation' do
|
436
404
|
let(:post) do
|
437
|
-
ParanoidPost.create(title:
|
405
|
+
ParanoidPost.create(title: 'test')
|
438
406
|
end
|
439
407
|
|
440
408
|
let!(:author) do
|
441
|
-
post.authors.create(name:
|
409
|
+
post.authors.create(name: 'poe')
|
442
410
|
end
|
443
411
|
|
444
412
|
before do
|
445
413
|
post.delete!
|
446
414
|
end
|
447
415
|
|
448
|
-
it
|
449
|
-
expect
|
416
|
+
it 'cascades the dependent option' do
|
417
|
+
expect do
|
450
418
|
author.reload
|
451
|
-
|
419
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound)
|
452
420
|
end
|
453
421
|
end
|
454
422
|
end
|
455
423
|
|
456
|
-
describe
|
457
|
-
|
458
|
-
context "when the document is a root" do
|
459
|
-
|
424
|
+
describe '#delete' do
|
425
|
+
context 'when the document is a root' do
|
460
426
|
let(:post) do
|
461
|
-
ParanoidPost.create(title:
|
427
|
+
ParanoidPost.create(title: 'testing')
|
462
428
|
end
|
463
429
|
|
464
430
|
before do
|
@@ -469,29 +435,28 @@ describe Mongoid::Paranoia do
|
|
469
435
|
ParanoidPost.collection.find(_id: post.id).first
|
470
436
|
end
|
471
437
|
|
472
|
-
it
|
473
|
-
expect(raw[
|
438
|
+
it 'soft deletes the document' do
|
439
|
+
expect(raw['deleted_at']).to be_within(1).of(Time.now)
|
474
440
|
end
|
475
441
|
|
476
|
-
it
|
477
|
-
expect
|
442
|
+
it 'does not return the document in a find' do
|
443
|
+
expect do
|
478
444
|
ParanoidPost.find(post.id)
|
479
|
-
|
445
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound)
|
480
446
|
end
|
481
447
|
|
482
|
-
it
|
448
|
+
it 'clears out the persistence options' do
|
483
449
|
expect(ParanoidPost.persistence_options).to be_nil
|
484
450
|
end
|
485
451
|
end
|
486
452
|
|
487
|
-
context
|
488
|
-
|
453
|
+
context 'when the document is embedded' do
|
489
454
|
let(:person) do
|
490
455
|
Person.create
|
491
456
|
end
|
492
457
|
|
493
458
|
let(:phone) do
|
494
|
-
person.paranoid_phones.create(number:
|
459
|
+
person.paranoid_phones.create(number: '911')
|
495
460
|
end
|
496
461
|
|
497
462
|
before do
|
@@ -502,46 +467,44 @@ describe Mongoid::Paranoia do
|
|
502
467
|
Person.collection.find(_id: person.id).first
|
503
468
|
end
|
504
469
|
|
505
|
-
it
|
506
|
-
expect(raw[
|
470
|
+
it 'soft deletes the document' do
|
471
|
+
expect(raw['paranoid_phones'].first['deleted_at']).to be_within(1).of(Time.now)
|
507
472
|
end
|
508
473
|
|
509
|
-
it
|
510
|
-
expect
|
474
|
+
it 'does not return the document in a find' do
|
475
|
+
expect do
|
511
476
|
person.paranoid_phones.find(phone.id)
|
512
|
-
|
477
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound)
|
513
478
|
end
|
514
479
|
|
515
|
-
it
|
480
|
+
it 'does not include the document in the relation' do
|
516
481
|
expect(person.paranoid_phones.scoped).to be_empty
|
517
482
|
end
|
518
483
|
end
|
519
484
|
|
520
|
-
context
|
521
|
-
|
485
|
+
context 'when the document has a dependent relation' do
|
522
486
|
let(:post) do
|
523
|
-
ParanoidPost.create(title:
|
487
|
+
ParanoidPost.create(title: 'test')
|
524
488
|
end
|
525
489
|
|
526
490
|
let!(:author) do
|
527
|
-
post.authors.create(name:
|
491
|
+
post.authors.create(name: 'poe')
|
528
492
|
end
|
529
493
|
|
530
494
|
before do
|
531
495
|
post.delete
|
532
496
|
end
|
533
497
|
|
534
|
-
it
|
535
|
-
expect
|
498
|
+
it 'cascades the dependent option' do
|
499
|
+
expect do
|
536
500
|
author.reload
|
537
|
-
|
501
|
+
end.to raise_error(Mongoid::Errors::DocumentNotFound)
|
538
502
|
end
|
539
503
|
end
|
540
504
|
|
541
|
-
context
|
542
|
-
|
505
|
+
context 'when the document has a dependent: :restrict relation' do
|
543
506
|
let(:post) do
|
544
|
-
ParanoidPost.create(title:
|
507
|
+
ParanoidPost.create(title: 'test')
|
545
508
|
end
|
546
509
|
|
547
510
|
let!(:title) do
|
@@ -555,14 +518,13 @@ describe Mongoid::Paranoia do
|
|
555
518
|
end
|
556
519
|
end
|
557
520
|
|
558
|
-
it
|
521
|
+
it 'does not destroy the document' do
|
559
522
|
expect(post).not_to be_destroyed
|
560
523
|
end
|
561
524
|
end
|
562
525
|
end
|
563
526
|
|
564
|
-
describe
|
565
|
-
|
527
|
+
describe '#remove' do
|
566
528
|
let(:post) do
|
567
529
|
ParanoidPost.new
|
568
530
|
end
|
@@ -575,17 +537,15 @@ describe Mongoid::Paranoia do
|
|
575
537
|
post.remove
|
576
538
|
end
|
577
539
|
|
578
|
-
it
|
540
|
+
it 'sets the deleted flag' do
|
579
541
|
expect(post).to be_destroyed
|
580
542
|
end
|
581
543
|
end
|
582
544
|
|
583
|
-
describe
|
584
|
-
|
585
|
-
context "when the document is a root" do
|
586
|
-
|
545
|
+
describe '#restore' do
|
546
|
+
context 'when the document is a root' do
|
587
547
|
let(:post) do
|
588
|
-
ParanoidPost.create(title:
|
548
|
+
ParanoidPost.create(title: 'testing')
|
589
549
|
end
|
590
550
|
|
591
551
|
before do
|
@@ -593,27 +553,26 @@ describe Mongoid::Paranoia do
|
|
593
553
|
post.restore
|
594
554
|
end
|
595
555
|
|
596
|
-
it
|
556
|
+
it 'removes the deleted at time' do
|
597
557
|
expect(post.deleted_at).to be_nil
|
598
558
|
end
|
599
559
|
|
600
|
-
it
|
560
|
+
it 'persists the change' do
|
601
561
|
expect(post.reload.deleted_at).to be_nil
|
602
562
|
end
|
603
563
|
|
604
|
-
it
|
564
|
+
it 'marks document again as persisted' do
|
605
565
|
expect(post.persisted?).to be_truthy
|
606
566
|
end
|
607
567
|
end
|
608
568
|
|
609
|
-
context
|
610
|
-
|
569
|
+
context 'when the document is embedded' do
|
611
570
|
let(:person) do
|
612
571
|
Person.create
|
613
572
|
end
|
614
573
|
|
615
574
|
let(:phone) do
|
616
|
-
person.paranoid_phones.create(number:
|
575
|
+
person.paranoid_phones.create(number: '911')
|
617
576
|
end
|
618
577
|
|
619
578
|
before do
|
@@ -621,29 +580,27 @@ describe Mongoid::Paranoia do
|
|
621
580
|
phone.restore
|
622
581
|
end
|
623
582
|
|
624
|
-
it
|
583
|
+
it 'removes the deleted at time' do
|
625
584
|
expect(phone.deleted_at).to be_nil
|
626
585
|
end
|
627
586
|
|
628
|
-
it
|
587
|
+
it 'persists the change' do
|
629
588
|
expect(person.reload.paranoid_phones.first.deleted_at).to be_nil
|
630
589
|
end
|
631
590
|
end
|
632
591
|
end
|
633
592
|
|
634
|
-
describe
|
635
|
-
|
593
|
+
describe '.scoped' do
|
636
594
|
let(:scoped) do
|
637
595
|
ParanoidPost.scoped
|
638
596
|
end
|
639
597
|
|
640
|
-
it
|
641
|
-
expect(scoped.selector).to eq(
|
598
|
+
it 'returns a scoped criteria' do
|
599
|
+
expect(scoped.selector).to eq('deleted_at' => nil)
|
642
600
|
end
|
643
601
|
end
|
644
602
|
|
645
|
-
describe
|
646
|
-
|
603
|
+
describe '#set' do
|
647
604
|
let!(:post) do
|
648
605
|
ParanoidPost.create
|
649
606
|
end
|
@@ -656,54 +613,49 @@ describe Mongoid::Paranoia do
|
|
656
613
|
post.set(deleted_at: time)
|
657
614
|
end
|
658
615
|
|
659
|
-
it
|
616
|
+
it 'persists the change' do
|
660
617
|
expect(post.reload.deleted_at).to be_within(1).of(time)
|
661
618
|
end
|
662
619
|
end
|
663
620
|
|
664
|
-
describe
|
665
|
-
|
621
|
+
describe '.unscoped' do
|
666
622
|
let(:unscoped) do
|
667
623
|
ParanoidPost.unscoped
|
668
624
|
end
|
669
625
|
|
670
|
-
it
|
626
|
+
it 'returns an unscoped criteria' do
|
671
627
|
expect(unscoped.selector).to eq({})
|
672
628
|
end
|
673
629
|
end
|
674
630
|
|
675
|
-
describe
|
676
|
-
|
631
|
+
describe '#to_param' do
|
677
632
|
let(:post) do
|
678
|
-
ParanoidPost.new(title:
|
633
|
+
ParanoidPost.new(title: 'testing')
|
679
634
|
end
|
680
635
|
|
681
|
-
context
|
682
|
-
|
683
|
-
it "still returns nil" do
|
636
|
+
context 'when the document is new' do
|
637
|
+
it 'still returns nil' do
|
684
638
|
expect(post.to_param).to be_nil
|
685
639
|
end
|
686
640
|
end
|
687
641
|
|
688
|
-
context
|
689
|
-
|
642
|
+
context 'when the document is not deleted' do
|
690
643
|
before do
|
691
644
|
post.save
|
692
645
|
end
|
693
646
|
|
694
|
-
it
|
647
|
+
it 'returns the id as a string' do
|
695
648
|
expect(post.to_param).to eq(post.id.to_s)
|
696
649
|
end
|
697
650
|
end
|
698
651
|
|
699
|
-
context
|
700
|
-
|
652
|
+
context 'when the document is deleted' do
|
701
653
|
before do
|
702
654
|
post.save
|
703
655
|
post.delete
|
704
656
|
end
|
705
657
|
|
706
|
-
it
|
658
|
+
it 'returns the id as a string' do
|
707
659
|
expect(post.to_param).to eq(post.id.to_s)
|
708
660
|
end
|
709
661
|
end
|