mongoid 7.0.1 → 7.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/lib/mongoid/association.rb +0 -1
- data/lib/mongoid/association/depending.rb +22 -1
- data/lib/mongoid/association/embedded/embeds_one/proxy.rb +3 -3
- data/lib/mongoid/association/referenced/has_many/proxy.rb +1 -1
- data/lib/mongoid/association/relatable.rb +16 -2
- data/lib/mongoid/attributes/nested.rb +14 -3
- data/lib/mongoid/contextual/map_reduce.rb +1 -1
- data/lib/mongoid/copyable.rb +3 -2
- data/lib/mongoid/document.rb +2 -0
- data/lib/mongoid/matchable.rb +3 -0
- data/lib/mongoid/matchable/nor.rb +37 -0
- data/lib/mongoid/persistable/settable.rb +58 -13
- data/lib/mongoid/persistence_context.rb +5 -1
- data/lib/mongoid/touchable.rb +102 -0
- data/lib/mongoid/version.rb +1 -1
- data/spec/app/models/array_field.rb +7 -0
- data/spec/app/models/updatable.rb +7 -0
- data/spec/mongoid/association/accessors_spec.rb +39 -0
- data/spec/mongoid/association/depending_spec.rb +253 -0
- data/spec/mongoid/association/polymorphic_spec.rb +59 -0
- data/spec/mongoid/association/referenced/belongs_to_spec.rb +3 -3
- data/spec/mongoid/association/referenced/has_one_spec.rb +59 -0
- data/spec/mongoid/attributes/nested_spec.rb +18 -2
- data/spec/mongoid/clients/factory_spec.rb +3 -3
- data/spec/mongoid/clients/options_spec.rb +28 -13
- data/spec/mongoid/clients/sessions_spec.rb +3 -3
- data/spec/mongoid/clients/transactions_spec.rb +369 -0
- data/spec/mongoid/copyable_spec.rb +16 -0
- data/spec/mongoid/matchable/nor_spec.rb +209 -0
- data/spec/mongoid/matchable_spec.rb +26 -1
- data/spec/mongoid/persistable/settable_spec.rb +128 -9
- data/spec/mongoid/{association/touchable_spec.rb → touchable_spec.rb} +28 -7
- data/spec/spec_helper.rb +8 -0
- metadata +457 -448
- metadata.gz.sig +0 -0
- data/lib/mongoid/association/touchable.rb +0 -97
data/lib/mongoid/version.rb
CHANGED
@@ -687,6 +687,45 @@ describe Mongoid::Association::Accessors do
|
|
687
687
|
end
|
688
688
|
end
|
689
689
|
|
690
|
+
context 'when setting associations to nil values' do
|
691
|
+
context 'when the document embeds one' do
|
692
|
+
let(:definitions) do
|
693
|
+
class AccessorEmbedding
|
694
|
+
include Mongoid::Document
|
695
|
+
|
696
|
+
embeds_one :accessor_embedded
|
697
|
+
end
|
698
|
+
|
699
|
+
class AccessorEmbedded
|
700
|
+
include Mongoid::Document
|
701
|
+
|
702
|
+
embedded_in :accessor_embedding
|
703
|
+
end
|
704
|
+
|
705
|
+
define_embedded
|
706
|
+
end
|
707
|
+
|
708
|
+
let(:embedding) do
|
709
|
+
AccessorEmbedding.create!
|
710
|
+
end
|
711
|
+
|
712
|
+
let(:define_embedded) do
|
713
|
+
AccessorEmbedded.create!(accessor_embedding: embedding)
|
714
|
+
end
|
715
|
+
|
716
|
+
it 'successfully removes the embedded document' do
|
717
|
+
definitions
|
718
|
+
|
719
|
+
expect(embedding.accessor_embedded).not_to be_nil
|
720
|
+
|
721
|
+
embedding.accessor_embedded = nil
|
722
|
+
embedding.save!
|
723
|
+
|
724
|
+
expect(embedding.reload.accessor_embedded).to be_nil
|
725
|
+
end
|
726
|
+
end
|
727
|
+
end
|
728
|
+
|
690
729
|
context "when setting association foreign keys" do
|
691
730
|
|
692
731
|
let(:game) do
|
@@ -2,6 +2,259 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe Mongoid::Association::Depending do
|
4
4
|
|
5
|
+
describe '#self.included' do
|
6
|
+
|
7
|
+
context 'when a destroy dependent is defined' do
|
8
|
+
|
9
|
+
context 'when the model is a subclass' do
|
10
|
+
|
11
|
+
context 'when transitive dependents are defined' do
|
12
|
+
|
13
|
+
let(:define_classes) do
|
14
|
+
class DependentReportCard
|
15
|
+
include Mongoid::Document
|
16
|
+
|
17
|
+
belongs_to :dependent_student
|
18
|
+
end
|
19
|
+
|
20
|
+
class DependentUser
|
21
|
+
include Mongoid::Document
|
22
|
+
end
|
23
|
+
|
24
|
+
class DependentStudent < DependentUser
|
25
|
+
belongs_to :dependent_teacher
|
26
|
+
has_many :dependent_report_cards, dependent: :destroy
|
27
|
+
end
|
28
|
+
|
29
|
+
class DependentDerivedStudent < DependentStudent; end
|
30
|
+
|
31
|
+
class DependentTeacher
|
32
|
+
include Mongoid::Document
|
33
|
+
|
34
|
+
has_many :dependent_students, dependent: :destroy
|
35
|
+
end
|
36
|
+
|
37
|
+
class DependentCollegeUser < DependentUser; end
|
38
|
+
end
|
39
|
+
|
40
|
+
it "does not add the dependent to superclass" do
|
41
|
+
define_classes
|
42
|
+
|
43
|
+
expect(DependentUser.dependents).to be_empty
|
44
|
+
|
45
|
+
u = DependentUser.create!
|
46
|
+
expect(u.dependents).to be_empty
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'does not impede destroying the superclass' do
|
50
|
+
define_classes
|
51
|
+
|
52
|
+
u = DependentUser.create!
|
53
|
+
expect { u.destroy! }.not_to raise_error
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'adds the dependent' do
|
57
|
+
define_classes
|
58
|
+
|
59
|
+
expect(DependentStudent.dependents.length).to be(1)
|
60
|
+
expect(DependentStudent.dependents.first.name).to be(:dependent_report_cards)
|
61
|
+
|
62
|
+
s = DependentStudent.create!
|
63
|
+
expect(s.dependents.length).to be(1)
|
64
|
+
expect(s.dependents.first.name).to be(:dependent_report_cards)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'facilitates proper destroying of the object' do
|
68
|
+
define_classes
|
69
|
+
|
70
|
+
s = DependentStudent.create!
|
71
|
+
r = DependentReportCard.create!(dependent_student: s)
|
72
|
+
s.destroy!
|
73
|
+
|
74
|
+
expect { DependentReportCard.find(r.id) }.to raise_error(Mongoid::Errors::DocumentNotFound)
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'facilitates proper transitive destroying of the object' do
|
78
|
+
define_classes
|
79
|
+
|
80
|
+
t = DependentTeacher.create!
|
81
|
+
s = DependentStudent.create!(dependent_teacher: t)
|
82
|
+
r = DependentReportCard.create!(dependent_student: s)
|
83
|
+
s.destroy!
|
84
|
+
|
85
|
+
expect { DependentReportCard.find(r.id) }.to raise_error(Mongoid::Errors::DocumentNotFound)
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'adds the dependent to subclasses' do
|
89
|
+
define_classes
|
90
|
+
|
91
|
+
expect(DependentDerivedStudent.dependents.length).to be(1)
|
92
|
+
expect(DependentDerivedStudent.dependents.first.name).to be(:dependent_report_cards)
|
93
|
+
|
94
|
+
s = DependentDerivedStudent.create!
|
95
|
+
expect(s.dependents.length).to be(1)
|
96
|
+
expect(s.dependents.first.name).to be(:dependent_report_cards)
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'facilitates proper destroying of the object from subclasses' do
|
100
|
+
define_classes
|
101
|
+
|
102
|
+
s = DependentDerivedStudent.create!
|
103
|
+
r = DependentReportCard.create!(dependent_student: s)
|
104
|
+
s.destroy!
|
105
|
+
|
106
|
+
expect { DependentReportCard.find(r.id) }.to raise_error(Mongoid::Errors::DocumentNotFound)
|
107
|
+
end
|
108
|
+
|
109
|
+
it "doesn't add the dependent to sibling classes" do
|
110
|
+
define_classes
|
111
|
+
|
112
|
+
expect(DependentCollegeUser.dependents).to be_empty
|
113
|
+
|
114
|
+
c = DependentCollegeUser.create!
|
115
|
+
expect(c.dependents).to be_empty
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'does not impede destroying the sibling class' do
|
119
|
+
define_classes
|
120
|
+
|
121
|
+
c = DependentCollegeUser.create!
|
122
|
+
expect { c.destroy! }.not_to raise_error
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context 'when a superclass is reopened and a new dependent is added' do
|
127
|
+
let(:define_classes) do
|
128
|
+
class DependentOwnedOne
|
129
|
+
include Mongoid::Document
|
130
|
+
|
131
|
+
belongs_to :dependent_superclass
|
132
|
+
end
|
133
|
+
|
134
|
+
class DependentOwnedTwo
|
135
|
+
include Mongoid::Document
|
136
|
+
|
137
|
+
belongs_to :dependent_superclass
|
138
|
+
end
|
139
|
+
|
140
|
+
class DependentSuperclass
|
141
|
+
include Mongoid::Document
|
142
|
+
has_one :dependent_owned_one
|
143
|
+
has_one :dependent_owned_two
|
144
|
+
end
|
145
|
+
|
146
|
+
class DependentSubclass < DependentSuperclass
|
147
|
+
has_one :dependent_owned_two, dependent: :nullify
|
148
|
+
end
|
149
|
+
|
150
|
+
class DependentSuperclass
|
151
|
+
has_one :dependent_owned_one, dependent: :destroy
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
it 'defines the dependent from the reopened superclass on the subclass' do
|
156
|
+
define_classes
|
157
|
+
|
158
|
+
DependentSubclass.create!.destroy!
|
159
|
+
|
160
|
+
expect(DependentSubclass.dependents.length).to be(1)
|
161
|
+
expect(DependentSubclass.dependents.last.name).to be(:dependent_owned_two)
|
162
|
+
expect(DependentSubclass.dependents.last.options[:dependent]).to be(:nullify)
|
163
|
+
|
164
|
+
subclass = DependentSubclass.create!
|
165
|
+
expect(subclass.dependents.last.name).to be(:dependent_owned_two)
|
166
|
+
expect(subclass.dependents.last.options[:dependent]).to be(:nullify)
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'causes the destruction of the inherited destroy dependent' do
|
170
|
+
define_classes
|
171
|
+
|
172
|
+
subclass = DependentSubclass.create!
|
173
|
+
owned = DependentOwnedOne.create!(dependent_superclass: subclass)
|
174
|
+
subclass.destroy!
|
175
|
+
|
176
|
+
expect {
|
177
|
+
DependentOwnedOne.find(owned.id)
|
178
|
+
}.to raise_error(Mongoid::Errors::DocumentNotFound)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
context 'when a separate subclass overrides the destroy dependent' do
|
183
|
+
let(:define_classes) do
|
184
|
+
class Dep
|
185
|
+
include Mongoid::Document
|
186
|
+
|
187
|
+
belongs_to :double_assoc
|
188
|
+
end
|
189
|
+
|
190
|
+
class DoubleAssoc
|
191
|
+
include Mongoid::Document
|
192
|
+
|
193
|
+
has_many :deps, dependent: :destroy
|
194
|
+
end
|
195
|
+
|
196
|
+
class DoubleAssocOne < DoubleAssoc
|
197
|
+
has_many :deps, dependent: :nullify, inverse_of: :double_assoc
|
198
|
+
end
|
199
|
+
|
200
|
+
class DoubleAssocTwo < DoubleAssocOne
|
201
|
+
has_many :deps, dependent: :destroy, inverse_of: :double_assoc
|
202
|
+
end
|
203
|
+
|
204
|
+
class DoubleAssocThree < DoubleAssoc; end
|
205
|
+
end
|
206
|
+
|
207
|
+
it 'adds the non-destroy dependent correctly to the subclass with the override' do
|
208
|
+
define_classes
|
209
|
+
|
210
|
+
expect(DoubleAssocOne.dependents.length).to be(1)
|
211
|
+
expect(DoubleAssocOne.dependents.first.name).to be(:deps)
|
212
|
+
expect(DoubleAssocOne.dependents.first.options[:dependent]).to be(:nullify)
|
213
|
+
|
214
|
+
one = DoubleAssocOne.create!
|
215
|
+
expect(one.dependents.length).to be(1)
|
216
|
+
expect(one.dependents.first.name).to be(:deps)
|
217
|
+
expect(one.dependents.first.options[:dependent]).to be(:nullify)
|
218
|
+
end
|
219
|
+
|
220
|
+
it 'does not cause the destruction of the non-destroy dependent' do
|
221
|
+
define_classes
|
222
|
+
|
223
|
+
one = DoubleAssocOne.create!
|
224
|
+
dep = Dep.create!(double_assoc: one)
|
225
|
+
one.destroy!
|
226
|
+
|
227
|
+
expect { Dep.find(dep.id) }.not_to raise_error
|
228
|
+
expect(dep.double_assoc).to be_nil
|
229
|
+
end
|
230
|
+
|
231
|
+
it 'adds the destroy dependent correctly to the subclass without the override' do
|
232
|
+
define_classes
|
233
|
+
|
234
|
+
expect(DoubleAssocTwo.dependents.length).to be(1)
|
235
|
+
expect(DoubleAssocTwo.dependents.first.name).to be(:deps)
|
236
|
+
expect(DoubleAssocTwo.dependents.first.options[:dependent]).to be(:destroy)
|
237
|
+
|
238
|
+
two = DoubleAssocTwo.create!
|
239
|
+
expect(two.dependents.length).to be(1)
|
240
|
+
expect(two.dependents.first.name).to be(:deps)
|
241
|
+
expect(two.dependents.first.options[:dependent]).to be(:destroy)
|
242
|
+
end
|
243
|
+
|
244
|
+
it 'causes the destruction of the destroy dependent' do
|
245
|
+
define_classes
|
246
|
+
|
247
|
+
two = DoubleAssocTwo.create!
|
248
|
+
dep = Dep.create!(double_assoc: two)
|
249
|
+
two.destroy!
|
250
|
+
|
251
|
+
expect { Dep.find(dep.id) }.to raise_error(Mongoid::Errors::DocumentNotFound)
|
252
|
+
end
|
253
|
+
end
|
254
|
+
end
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
5
258
|
around(:each) do |example|
|
6
259
|
relations_before = Person.relations
|
7
260
|
example.run
|
@@ -98,4 +98,63 @@ describe "Polymorphic Associations" do
|
|
98
98
|
end
|
99
99
|
end
|
100
100
|
end
|
101
|
+
|
102
|
+
context 'when the relation is touchable' do
|
103
|
+
|
104
|
+
context 'when the relation is embedded' do
|
105
|
+
|
106
|
+
let(:define_classes) do
|
107
|
+
|
108
|
+
class FirstOwner
|
109
|
+
include Mongoid::Document
|
110
|
+
|
111
|
+
embeds_one :owned, class_name: 'Owned', as: :embedded_relation_polymorphic_touch_owner
|
112
|
+
end
|
113
|
+
|
114
|
+
class SecondOwner
|
115
|
+
include Mongoid::Document
|
116
|
+
|
117
|
+
embeds_one :owned, class_name: 'Owned', as: :embedded_relation_polymorphic_touch_owner
|
118
|
+
end
|
119
|
+
|
120
|
+
class Owned
|
121
|
+
include Mongoid::Document
|
122
|
+
|
123
|
+
embedded_in :embedded_relation_polymorphic_touch_owner, polymorphic: true, touch: true
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'successfully defines the touch method' do
|
128
|
+
expect { define_classes }.not_to raise_error
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
context 'when the relation is not embedded' do
|
133
|
+
|
134
|
+
let(:define_classes) do
|
135
|
+
|
136
|
+
class FirstOwner
|
137
|
+
include Mongoid::Document
|
138
|
+
|
139
|
+
has_one :owned, class_name: 'Owned', as: :belongs_relation_polymorphic_touch_owner
|
140
|
+
end
|
141
|
+
|
142
|
+
class SecondOwner
|
143
|
+
include Mongoid::Document
|
144
|
+
|
145
|
+
has_one :owned, class_name: 'Owned', as: :belongs_relation_polymorphic_touch_owner
|
146
|
+
end
|
147
|
+
|
148
|
+
class Owned
|
149
|
+
include Mongoid::Document
|
150
|
+
|
151
|
+
belongs_to :belongs_relation_polymorphic_touch_owner, polymorphic: true, touch: true
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
it 'successfully defines the touch method' do
|
156
|
+
expect { define_classes }.not_to raise_error
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
101
160
|
end
|
@@ -449,7 +449,7 @@ describe Mongoid::Association::Referenced::BelongsTo do
|
|
449
449
|
end
|
450
450
|
|
451
451
|
it 'sets up touch' do
|
452
|
-
expect(Mongoid::
|
452
|
+
expect(Mongoid::Touchable).to receive(:define_touchable!).with(association)
|
453
453
|
association.setup!
|
454
454
|
end
|
455
455
|
end
|
@@ -463,7 +463,7 @@ describe Mongoid::Association::Referenced::BelongsTo do
|
|
463
463
|
end
|
464
464
|
|
465
465
|
it 'does not set up touch' do
|
466
|
-
expect(Mongoid::
|
466
|
+
expect(Mongoid::Touchable).not_to receive(:define_touchable!).with(association)
|
467
467
|
association.setup!
|
468
468
|
end
|
469
469
|
end
|
@@ -477,7 +477,7 @@ describe Mongoid::Association::Referenced::BelongsTo do
|
|
477
477
|
end
|
478
478
|
|
479
479
|
it 'does not set up touch' do
|
480
|
-
expect(Mongoid::
|
480
|
+
expect(Mongoid::Touchable).not_to receive(:define_touchable!).with(association)
|
481
481
|
association.setup!
|
482
482
|
end
|
483
483
|
end
|
@@ -890,6 +890,33 @@ describe Mongoid::Association::Referenced::HasOne do
|
|
890
890
|
|
891
891
|
describe '#relation_class_name' do
|
892
892
|
|
893
|
+
context 'when the classes are defined in a module' do
|
894
|
+
|
895
|
+
let(:define_classes) do
|
896
|
+
module HasOneAssociationClassName
|
897
|
+
class OwnedClass
|
898
|
+
include Mongoid::Document
|
899
|
+
|
900
|
+
belongs_to :owner_class
|
901
|
+
end
|
902
|
+
|
903
|
+
class OwnerClass
|
904
|
+
include Mongoid::Document
|
905
|
+
|
906
|
+
has_one :owned_class
|
907
|
+
end
|
908
|
+
end
|
909
|
+
end
|
910
|
+
|
911
|
+
it 'returns the proper namespaced class name' do
|
912
|
+
define_classes
|
913
|
+
|
914
|
+
expect(
|
915
|
+
HasOneAssociationClassName::OwnedClass.relations['owner_class'].relation_class_name
|
916
|
+
).to eq('HasOneAssociationClassName::OwnerClass')
|
917
|
+
end
|
918
|
+
end
|
919
|
+
|
893
920
|
context 'when the :class_name option is specified' do
|
894
921
|
|
895
922
|
let(:options) do
|
@@ -1200,6 +1227,38 @@ describe Mongoid::Association::Referenced::HasOne do
|
|
1200
1227
|
end
|
1201
1228
|
end
|
1202
1229
|
|
1230
|
+
context 'when the classes are defined in a module' do
|
1231
|
+
|
1232
|
+
let(:define_classes) do
|
1233
|
+
module HasOneAssociationModuleDefinitions
|
1234
|
+
class OwnedClass
|
1235
|
+
include Mongoid::Document
|
1236
|
+
|
1237
|
+
belongs_to :owner_class
|
1238
|
+
end
|
1239
|
+
|
1240
|
+
class OwnerClass
|
1241
|
+
include Mongoid::Document
|
1242
|
+
|
1243
|
+
has_one :owned_class
|
1244
|
+
end
|
1245
|
+
end
|
1246
|
+
end
|
1247
|
+
|
1248
|
+
let(:owner) do
|
1249
|
+
HasOneAssociationModuleDefinitions::OwnerClass.create!
|
1250
|
+
end
|
1251
|
+
|
1252
|
+
let(:owned) do
|
1253
|
+
define_classes
|
1254
|
+
HasOneAssociationModuleDefinitions::OwnedClass.create!(owner_class: owner)
|
1255
|
+
end
|
1256
|
+
|
1257
|
+
it 'successfully creates the owned document' do
|
1258
|
+
expect { owned }.not_to raise_error
|
1259
|
+
end
|
1260
|
+
end
|
1261
|
+
|
1203
1262
|
describe '#nested_builder' do
|
1204
1263
|
|
1205
1264
|
it 'returns an instance of Association::Nested::One' do
|