mongoid 7.1.2 → 7.1.4
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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/Rakefile +14 -0
- data/lib/config/locales/en.yml +1 -1
- data/lib/mongoid/association/accessors.rb +37 -2
- data/lib/mongoid/association/referenced/belongs_to/binding.rb +1 -1
- data/lib/mongoid/clients/factory.rb +2 -2
- data/lib/mongoid/clients/options.rb +8 -8
- data/lib/mongoid/clients/sessions.rb +20 -4
- data/lib/mongoid/clients/storage_options.rb +5 -5
- data/lib/mongoid/criteria/queryable/extensions/regexp.rb +3 -3
- data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +12 -0
- data/lib/mongoid/errors/no_client_config.rb +2 -2
- data/lib/mongoid/errors/no_default_client.rb +1 -1
- data/lib/mongoid/fields/validators/macro.rb +4 -1
- data/lib/mongoid/query_cache.rb +21 -9
- data/lib/mongoid/tasks/database.rb +38 -3
- data/lib/mongoid/version.rb +1 -1
- data/spec/app/models/passport.rb +1 -0
- data/spec/app/models/phone.rb +1 -0
- data/spec/integration/app_spec.rb +76 -14
- data/spec/integration/associations/has_many_spec.rb +34 -0
- data/spec/integration/associations/has_one_spec.rb +34 -0
- data/spec/integration/bson_regexp_raw_spec.rb +20 -0
- data/spec/integration/criteria/date_field_spec.rb +41 -0
- data/spec/integration/shardable_spec.rb +20 -4
- data/spec/mongoid/association/accessors_spec.rb +238 -63
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +138 -0
- data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +105 -0
- data/spec/mongoid/clients/factory_spec.rb +8 -8
- data/spec/mongoid/clients/options_spec.rb +9 -9
- data/spec/mongoid/contextual/geo_near_spec.rb +11 -2
- data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +1 -1
- data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +19 -7
- data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +28 -1
- data/spec/mongoid/errors/no_client_config_spec.rb +2 -2
- data/spec/mongoid/errors/no_client_database_spec.rb +3 -3
- data/spec/mongoid/errors/no_client_hosts_spec.rb +3 -3
- data/spec/mongoid/fields_spec.rb +24 -1
- data/spec/mongoid/query_cache_spec.rb +60 -7
- metadata +487 -479
- metadata.gz.sig +0 -0
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require 'spec_helper'
|
5
|
+
|
6
|
+
describe 'has_many associations' do
|
7
|
+
context 'destroying parent in transaction with dependent child' do
|
8
|
+
require_transaction_support
|
9
|
+
|
10
|
+
let(:artist) { Artist.create! }
|
11
|
+
let(:album) { Album.create!(artist: artist) }
|
12
|
+
|
13
|
+
before do
|
14
|
+
Artist.delete_all
|
15
|
+
Album.delete_all
|
16
|
+
|
17
|
+
album
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'works' do
|
21
|
+
Artist.count.should == 1
|
22
|
+
Album.count.should == 1
|
23
|
+
|
24
|
+
artist.with_session do |session|
|
25
|
+
session.with_transaction do
|
26
|
+
artist.destroy
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
Artist.count.should == 0
|
31
|
+
Album.count.should == 0
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require 'spec_helper'
|
5
|
+
|
6
|
+
describe 'has_one associations' do
|
7
|
+
context 'destroying parent in transaction with dependent child' do
|
8
|
+
require_transaction_support
|
9
|
+
|
10
|
+
let(:person) { Person.create! }
|
11
|
+
let(:game) { Game.create!(person: person) }
|
12
|
+
|
13
|
+
before do
|
14
|
+
Person.delete_all
|
15
|
+
Game.delete_all
|
16
|
+
|
17
|
+
game
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'works' do
|
21
|
+
Person.count.should == 1
|
22
|
+
Game.count.should == 1
|
23
|
+
|
24
|
+
Person.with_session do |session|
|
25
|
+
session.with_transaction do
|
26
|
+
person.destroy
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
Person.count.should == 0
|
31
|
+
Game.count.should == 0
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require 'spec_helper'
|
5
|
+
|
6
|
+
describe BSON::Regexp::Raw do
|
7
|
+
context 'fully qualified name' do
|
8
|
+
it 'can be created' do
|
9
|
+
regexp = BSON::Regexp::Raw.new('foo')
|
10
|
+
regexp.pattern.should == 'foo'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'via ::Regexp' do
|
15
|
+
it 'can be created' do
|
16
|
+
regexp = Regexp::Raw.new('foo')
|
17
|
+
regexp.pattern.should == 'foo'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require 'spec_helper'
|
5
|
+
|
6
|
+
describe 'Queries on Date fields' do
|
7
|
+
let(:query) do
|
8
|
+
Band.where(founded: arg)
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:selector) { query.selector }
|
12
|
+
|
13
|
+
shared_examples 'converts to beginning of day in UTC' do
|
14
|
+
it 'converts to beginning of day in UTC' do
|
15
|
+
selector['founded'].should == arg.dup.beginning_of_day.utc.beginning_of_day
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'using Time' do
|
20
|
+
let(:arg) do
|
21
|
+
Time.now.freeze
|
22
|
+
end
|
23
|
+
|
24
|
+
it_behaves_like 'converts to beginning of day in UTC'
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'using TimeWithZone' do
|
28
|
+
let(:time_zone_name) { 'Pacific Time (US & Canada)' }
|
29
|
+
let(:arg) { Time.now.in_time_zone(time_zone_name).freeze }
|
30
|
+
|
31
|
+
it_behaves_like 'converts to beginning of day in UTC'
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'using DateTime' do
|
35
|
+
let(:arg) do
|
36
|
+
DateTime.now.freeze
|
37
|
+
end
|
38
|
+
|
39
|
+
it_behaves_like 'converts to beginning of day in UTC'
|
40
|
+
end
|
41
|
+
end
|
@@ -122,11 +122,27 @@ describe 'Sharding helpers' do
|
|
122
122
|
shard_collections.should == []
|
123
123
|
end
|
124
124
|
|
125
|
-
|
126
|
-
|
125
|
+
context 'pre-4.2' do
|
126
|
+
max_server_version '4.0'
|
127
127
|
|
128
|
-
|
129
|
-
|
128
|
+
it 'does not shards collection' do
|
129
|
+
shard_collections
|
130
|
+
|
131
|
+
lambda do
|
132
|
+
model_cls.collection.database.command(collStats: model_cls.collection.name).first
|
133
|
+
end.should raise_error(Mongo::Error::OperationFailure, /Collection.*not found/)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
context '4.2+' do
|
138
|
+
min_server_version '4.2'
|
139
|
+
|
140
|
+
it 'does not shards collection' do
|
141
|
+
shard_collections
|
142
|
+
|
143
|
+
stats = model_cls.collection.database.command(collStats: model_cls.collection.name).first
|
144
|
+
stats[:sharded].should be false
|
145
|
+
end
|
130
146
|
end
|
131
147
|
end
|
132
148
|
end
|
@@ -11,9 +11,9 @@ describe Mongoid::Association::Accessors do
|
|
11
11
|
Person.create
|
12
12
|
end
|
13
13
|
|
14
|
-
context "when the
|
14
|
+
context "when the association is a has one" do
|
15
15
|
|
16
|
-
context "when the
|
16
|
+
context "when the association exists" do
|
17
17
|
|
18
18
|
let!(:game) do
|
19
19
|
person.build_game
|
@@ -24,7 +24,7 @@ describe Mongoid::Association::Accessors do
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
context "when the
|
27
|
+
context "when the association does not exist" do
|
28
28
|
|
29
29
|
context "when not autobuilding" do
|
30
30
|
|
@@ -42,9 +42,9 @@ describe Mongoid::Association::Accessors do
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
-
context "when the
|
45
|
+
context "when the association is a has many" do
|
46
46
|
|
47
|
-
context "when the
|
47
|
+
context "when the association has documents" do
|
48
48
|
|
49
49
|
let!(:post) do
|
50
50
|
person.posts.build
|
@@ -55,7 +55,7 @@ describe Mongoid::Association::Accessors do
|
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
|
-
context "when the
|
58
|
+
context "when the association does not have documents" do
|
59
59
|
|
60
60
|
it "returns false" do
|
61
61
|
expect(person).to_not have_posts
|
@@ -63,9 +63,9 @@ describe Mongoid::Association::Accessors do
|
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
|
-
context "when the
|
66
|
+
context "when the association is a has and belongs to many" do
|
67
67
|
|
68
|
-
context "when the
|
68
|
+
context "when the association has documents" do
|
69
69
|
|
70
70
|
let!(:preference) do
|
71
71
|
person.preferences.build
|
@@ -76,7 +76,7 @@ describe Mongoid::Association::Accessors do
|
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
79
|
-
context "when the
|
79
|
+
context "when the association does not have documents" do
|
80
80
|
|
81
81
|
it "returns false" do
|
82
82
|
expect(person).to_not have_preferences
|
@@ -84,9 +84,9 @@ describe Mongoid::Association::Accessors do
|
|
84
84
|
end
|
85
85
|
end
|
86
86
|
|
87
|
-
context "when the
|
87
|
+
context "when the association is a belongs to" do
|
88
88
|
|
89
|
-
context "when the
|
89
|
+
context "when the association is named next" do
|
90
90
|
|
91
91
|
let(:user) do
|
92
92
|
User.create
|
@@ -97,7 +97,7 @@ describe Mongoid::Association::Accessors do
|
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
100
|
-
context "when the
|
100
|
+
context "when the association exists" do
|
101
101
|
|
102
102
|
let!(:game) do
|
103
103
|
person.build_game
|
@@ -108,9 +108,9 @@ describe Mongoid::Association::Accessors do
|
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
111
|
-
context "when the
|
111
|
+
context "when the association does not exist" do
|
112
112
|
|
113
|
-
context "when the
|
113
|
+
context "when the association does not autobuild" do
|
114
114
|
|
115
115
|
let(:game) do
|
116
116
|
Game.new
|
@@ -121,7 +121,7 @@ describe Mongoid::Association::Accessors do
|
|
121
121
|
end
|
122
122
|
end
|
123
123
|
|
124
|
-
context "when the
|
124
|
+
context "when the association autobuilds" do
|
125
125
|
|
126
126
|
let(:book) do
|
127
127
|
Book.new
|
@@ -134,9 +134,9 @@ describe Mongoid::Association::Accessors do
|
|
134
134
|
end
|
135
135
|
end
|
136
136
|
|
137
|
-
context "when the
|
137
|
+
context "when the association is an embeds one" do
|
138
138
|
|
139
|
-
context "when the
|
139
|
+
context "when the association exists" do
|
140
140
|
|
141
141
|
let!(:name) do
|
142
142
|
person.build_name
|
@@ -147,16 +147,16 @@ describe Mongoid::Association::Accessors do
|
|
147
147
|
end
|
148
148
|
end
|
149
149
|
|
150
|
-
context "when the
|
150
|
+
context "when the association does not exist" do
|
151
151
|
|
152
|
-
context "when the
|
152
|
+
context "when the association does not autobuild" do
|
153
153
|
|
154
154
|
it "returns false" do
|
155
155
|
expect(person).to_not have_name
|
156
156
|
end
|
157
157
|
end
|
158
158
|
|
159
|
-
context "when the
|
159
|
+
context "when the association autobuilds" do
|
160
160
|
|
161
161
|
let(:person) do
|
162
162
|
Person.new
|
@@ -169,9 +169,9 @@ describe Mongoid::Association::Accessors do
|
|
169
169
|
end
|
170
170
|
end
|
171
171
|
|
172
|
-
context "when the
|
172
|
+
context "when the association is an embeds many" do
|
173
173
|
|
174
|
-
context "when the
|
174
|
+
context "when the association has documents" do
|
175
175
|
|
176
176
|
let!(:address) do
|
177
177
|
person.addresses.build
|
@@ -182,7 +182,7 @@ describe Mongoid::Association::Accessors do
|
|
182
182
|
end
|
183
183
|
end
|
184
184
|
|
185
|
-
context "when the
|
185
|
+
context "when the association does not have documents" do
|
186
186
|
|
187
187
|
it "returns false" do
|
188
188
|
expect(person).to_not have_addresses
|
@@ -190,9 +190,9 @@ describe Mongoid::Association::Accessors do
|
|
190
190
|
end
|
191
191
|
end
|
192
192
|
|
193
|
-
context "when the
|
193
|
+
context "when the association is an embedded in" do
|
194
194
|
|
195
|
-
context "when the
|
195
|
+
context "when the association exists" do
|
196
196
|
|
197
197
|
let!(:name) do
|
198
198
|
person.build_name
|
@@ -203,9 +203,9 @@ describe Mongoid::Association::Accessors do
|
|
203
203
|
end
|
204
204
|
end
|
205
205
|
|
206
|
-
context "when the
|
206
|
+
context "when the association does not exist" do
|
207
207
|
|
208
|
-
context "when the
|
208
|
+
context "when the association does not autobuild" do
|
209
209
|
|
210
210
|
let(:name) do
|
211
211
|
Name.new
|
@@ -216,7 +216,7 @@ describe Mongoid::Association::Accessors do
|
|
216
216
|
end
|
217
217
|
end
|
218
218
|
|
219
|
-
context "when the
|
219
|
+
context "when the association autobuilds" do
|
220
220
|
|
221
221
|
let(:passport) do
|
222
222
|
Passport.new
|
@@ -236,11 +236,11 @@ describe Mongoid::Association::Accessors do
|
|
236
236
|
Person.new
|
237
237
|
end
|
238
238
|
|
239
|
-
context "when autobuilding the
|
239
|
+
context "when autobuilding the association" do
|
240
240
|
|
241
|
-
context "when the
|
241
|
+
context "when the association is an embeds one" do
|
242
242
|
|
243
|
-
context "when the
|
243
|
+
context "when the association does not exist" do
|
244
244
|
|
245
245
|
let!(:passport) do
|
246
246
|
person.passport
|
@@ -255,7 +255,7 @@ describe Mongoid::Association::Accessors do
|
|
255
255
|
end
|
256
256
|
end
|
257
257
|
|
258
|
-
context "when the
|
258
|
+
context "when the association exists" do
|
259
259
|
|
260
260
|
let!(:passport) do
|
261
261
|
person.build_passport(number: "123123321")
|
@@ -264,16 +264,17 @@ describe Mongoid::Association::Accessors do
|
|
264
264
|
it "does not build a new document" do
|
265
265
|
expect(person.passport).to eq(passport)
|
266
266
|
end
|
267
|
+
|
267
268
|
end
|
268
269
|
end
|
269
270
|
|
270
|
-
context "when the
|
271
|
+
context "when the association is an embedded in" do
|
271
272
|
|
272
273
|
let(:passport) do
|
273
274
|
Passport.new
|
274
275
|
end
|
275
276
|
|
276
|
-
context "when the
|
277
|
+
context "when the association does not exist" do
|
277
278
|
|
278
279
|
let(:person) do
|
279
280
|
passport.person
|
@@ -284,7 +285,7 @@ describe Mongoid::Association::Accessors do
|
|
284
285
|
end
|
285
286
|
end
|
286
287
|
|
287
|
-
context "when the
|
288
|
+
context "when the association exists" do
|
288
289
|
|
289
290
|
let!(:person) do
|
290
291
|
passport.build_person(title: "sir")
|
@@ -296,9 +297,9 @@ describe Mongoid::Association::Accessors do
|
|
296
297
|
end
|
297
298
|
end
|
298
299
|
|
299
|
-
context "when the
|
300
|
+
context "when the association is a has one" do
|
300
301
|
|
301
|
-
context "when the
|
302
|
+
context "when the association does not exist" do
|
302
303
|
|
303
304
|
let(:book) do
|
304
305
|
person.book
|
@@ -309,7 +310,7 @@ describe Mongoid::Association::Accessors do
|
|
309
310
|
end
|
310
311
|
end
|
311
312
|
|
312
|
-
context "when the
|
313
|
+
context "when the association exists" do
|
313
314
|
|
314
315
|
let!(:book) do
|
315
316
|
person.build_book(title: "art of war")
|
@@ -321,13 +322,13 @@ describe Mongoid::Association::Accessors do
|
|
321
322
|
end
|
322
323
|
end
|
323
324
|
|
324
|
-
context "when the
|
325
|
+
context "when the association is a belongs to" do
|
325
326
|
|
326
327
|
let(:book) do
|
327
328
|
Book.new
|
328
329
|
end
|
329
330
|
|
330
|
-
context "when the
|
331
|
+
context "when the association does not exist" do
|
331
332
|
|
332
333
|
let(:person) do
|
333
334
|
book.person
|
@@ -338,7 +339,7 @@ describe Mongoid::Association::Accessors do
|
|
338
339
|
end
|
339
340
|
end
|
340
341
|
|
341
|
-
context "when the
|
342
|
+
context "when the association exists" do
|
342
343
|
|
343
344
|
let!(:person) do
|
344
345
|
book.build_person(title: "sir")
|
@@ -351,13 +352,13 @@ describe Mongoid::Association::Accessors do
|
|
351
352
|
end
|
352
353
|
end
|
353
354
|
|
354
|
-
context "when the
|
355
|
+
context "when the association is not polymorphic" do
|
355
356
|
|
356
357
|
let(:person) do
|
357
358
|
Person.create
|
358
359
|
end
|
359
360
|
|
360
|
-
context "when the
|
361
|
+
context "when the association is a many to many" do
|
361
362
|
|
362
363
|
let!(:preference) do
|
363
364
|
Preference.create(name: "Setting")
|
@@ -367,7 +368,7 @@ describe Mongoid::Association::Accessors do
|
|
367
368
|
person.preferences << Preference.last
|
368
369
|
end
|
369
370
|
|
370
|
-
context "when reloading the
|
371
|
+
context "when reloading the association directly" do
|
371
372
|
|
372
373
|
let(:preferences) do
|
373
374
|
person.preferences(true)
|
@@ -409,7 +410,7 @@ describe Mongoid::Association::Accessors do
|
|
409
410
|
end
|
410
411
|
end
|
411
412
|
|
412
|
-
context "when the
|
413
|
+
context "when the association is a many to one" do
|
413
414
|
|
414
415
|
let!(:post) do
|
415
416
|
Post.create(title: "First!")
|
@@ -419,7 +420,7 @@ describe Mongoid::Association::Accessors do
|
|
419
420
|
person.posts << Post.last
|
420
421
|
end
|
421
422
|
|
422
|
-
context "when reloading the
|
423
|
+
context "when reloading the association directly" do
|
423
424
|
|
424
425
|
let(:posts) do
|
425
426
|
person.posts(true)
|
@@ -461,7 +462,7 @@ describe Mongoid::Association::Accessors do
|
|
461
462
|
end
|
462
463
|
end
|
463
464
|
|
464
|
-
context "when the
|
465
|
+
context "when the association is a references one" do
|
465
466
|
|
466
467
|
let!(:game) do
|
467
468
|
Game.create(name: "Centipeded")
|
@@ -471,7 +472,7 @@ describe Mongoid::Association::Accessors do
|
|
471
472
|
person.game = Game.last
|
472
473
|
end
|
473
474
|
|
474
|
-
context "when reloading the
|
475
|
+
context "when reloading the association directly" do
|
475
476
|
|
476
477
|
let(:reloaded_game) do
|
477
478
|
person.game(true)
|
@@ -514,7 +515,7 @@ describe Mongoid::Association::Accessors do
|
|
514
515
|
end
|
515
516
|
end
|
516
517
|
|
517
|
-
context "when the
|
518
|
+
context "when the association is polymorphic" do
|
518
519
|
|
519
520
|
context "when there's a single references many/one" do
|
520
521
|
|
@@ -568,9 +569,183 @@ describe Mongoid::Association::Accessors do
|
|
568
569
|
end
|
569
570
|
end
|
570
571
|
end
|
572
|
+
|
573
|
+
context 'when projecting' do
|
574
|
+
context 'embeds_one' do
|
575
|
+
|
576
|
+
let!(:person) do
|
577
|
+
Person.create!(passport: Passport.new(number: "123123321", country: "USA"))
|
578
|
+
end
|
579
|
+
|
580
|
+
context "when the record is queried with the embedded association projected" do
|
581
|
+
let(:persisted_person) { Person.only(:passport).first }
|
582
|
+
|
583
|
+
it 'creates an accessor for the projected embedded document' do
|
584
|
+
expect(persisted_person.passport.number).to eq("123123321")
|
585
|
+
expect(persisted_person.passport.country).to eq("USA")
|
586
|
+
end
|
587
|
+
end
|
588
|
+
|
589
|
+
context 'when the record is queried with a field on the embedded association projected' do
|
590
|
+
let(:persisted_person) { Person.only("pass.number").first }
|
591
|
+
|
592
|
+
it 'creates an accessor for the projected field on the embedded document' do
|
593
|
+
expect(persisted_person.passport.number).to eq("123123321")
|
594
|
+
end
|
595
|
+
|
596
|
+
it 'does not create an accessor for another field on the embedded document' do
|
597
|
+
expect do
|
598
|
+
persisted_person.passport.country
|
599
|
+
end.to raise_error(ActiveModel::MissingAttributeError)
|
600
|
+
end
|
601
|
+
end
|
602
|
+
|
603
|
+
context 'when projecting association and a field in association' do
|
604
|
+
shared_examples 'is prohibited on 4.4+ server' do
|
605
|
+
context '4.4 server and higher' do
|
606
|
+
min_server_version '4.4'
|
607
|
+
|
608
|
+
it 'is not allowed by server' do
|
609
|
+
lambda do
|
610
|
+
persisted_person
|
611
|
+
end.should raise_error(Mongo::Error::OperationFailure, /Path collision at pass/)
|
612
|
+
end
|
613
|
+
end
|
614
|
+
end
|
615
|
+
|
616
|
+
context 'association then field' do
|
617
|
+
let(:persisted_person) { Person.only(:pass, "pass.number").first }
|
618
|
+
|
619
|
+
context '4.2 server and lower' do
|
620
|
+
max_server_version '4.2'
|
621
|
+
|
622
|
+
it 'creates an accessor for the projected field on the embedded document' do
|
623
|
+
expect(persisted_person.passport.number).to eq("123123321")
|
624
|
+
end
|
625
|
+
|
626
|
+
it 'does not create an accessor for another field on the embedded document' do
|
627
|
+
expect do
|
628
|
+
persisted_person.passport.country
|
629
|
+
end.to raise_error(ActiveModel::MissingAttributeError)
|
630
|
+
end
|
631
|
+
end
|
632
|
+
|
633
|
+
include_examples 'is prohibited on 4.4+ server'
|
634
|
+
end
|
635
|
+
|
636
|
+
context 'field then association' do
|
637
|
+
let(:persisted_person) { Person.only('pass.number', :pass).first }
|
638
|
+
|
639
|
+
context '4.2 server and lower' do
|
640
|
+
max_server_version '4.2'
|
641
|
+
|
642
|
+
it 'creates an accessor for the projected field on the embedded document' do
|
643
|
+
expect(persisted_person.passport.number).to eq("123123321")
|
644
|
+
end
|
645
|
+
|
646
|
+
it 'retrieves other fields' do
|
647
|
+
persisted_person.passport.country.should == 'USA'
|
648
|
+
end
|
649
|
+
end
|
650
|
+
|
651
|
+
include_examples 'is prohibited on 4.4+ server'
|
652
|
+
end
|
653
|
+
end
|
654
|
+
end
|
655
|
+
|
656
|
+
context 'embeds_many' do
|
657
|
+
|
658
|
+
let!(:person) do
|
659
|
+
Person.create!(phone_numbers: [
|
660
|
+
Phone.new(number: '111-111-1111', landline: true),
|
661
|
+
])
|
662
|
+
end
|
663
|
+
|
664
|
+
context 'when the record is queried with the embedded association projected' do
|
665
|
+
let(:persisted_person) { Person.only(:phone_numbers).first }
|
666
|
+
|
667
|
+
it 'creates an accessor for the embedded document' do
|
668
|
+
expect(persisted_person.phone_numbers.first.number).to eq('111-111-1111')
|
669
|
+
expect(persisted_person.phone_numbers.first.landline).to be true
|
670
|
+
end
|
671
|
+
end
|
672
|
+
|
673
|
+
shared_examples 'allows access to field of projected association' do
|
674
|
+
it 'creates an accessor for the embedded document' do
|
675
|
+
expect(persisted_person.phone_numbers.first).to be_a_kind_of(Phone)
|
676
|
+
end
|
677
|
+
|
678
|
+
it 'creates an accessor for the projected field on the embedded document' do
|
679
|
+
expect(persisted_person.phone_numbers.first.number).to eq('111-111-1111')
|
680
|
+
end
|
681
|
+
end
|
682
|
+
|
683
|
+
context 'when the record is queried with a field on the embedded association projected' do
|
684
|
+
let(:persisted_person) { Person.only("phone_numbers.number").first }
|
685
|
+
|
686
|
+
include_examples 'allows access to field of projected association'
|
687
|
+
|
688
|
+
it 'does not create an accessor for another field on the embedded document' do
|
689
|
+
expect do
|
690
|
+
persisted_person.phone_numbers.first.landline
|
691
|
+
end.to raise_error(ActiveModel::MissingAttributeError)
|
692
|
+
end
|
693
|
+
end
|
694
|
+
|
695
|
+
context 'when projecting association and a field in association' do
|
696
|
+
|
697
|
+
shared_examples 'is prohibited on 4.4+ server' do
|
698
|
+
context '4.4 server and higher' do
|
699
|
+
min_server_version '4.4'
|
700
|
+
|
701
|
+
it 'is not allowed by server' do
|
702
|
+
lambda do
|
703
|
+
persisted_person
|
704
|
+
end.should raise_error(Mongo::Error::OperationFailure, /Path collision at phone_numbers/)
|
705
|
+
end
|
706
|
+
end
|
707
|
+
end
|
708
|
+
|
709
|
+
context 'association then field' do
|
710
|
+
let(:persisted_person) { Person.only(:phone_numbers, 'phone_numbers.number').first }
|
711
|
+
|
712
|
+
context '4.2 server and lower' do
|
713
|
+
max_server_version '4.2'
|
714
|
+
|
715
|
+
include_examples 'allows access to field of projected association'
|
716
|
+
|
717
|
+
it 'does not create an accessor for another field on the embedded document' do
|
718
|
+
expect do
|
719
|
+
persisted_person.phone_numbers.first.landline
|
720
|
+
end.to raise_error(ActiveModel::MissingAttributeError)
|
721
|
+
end
|
722
|
+
|
723
|
+
end
|
724
|
+
|
725
|
+
include_examples 'is prohibited on 4.4+ server'
|
726
|
+
end
|
727
|
+
|
728
|
+
context 'field then association' do
|
729
|
+
let(:persisted_person) { Person.only('phone_numbers.number', :phone_numbers).first }
|
730
|
+
|
731
|
+
context '4.2 server and lower' do
|
732
|
+
max_server_version '4.2'
|
733
|
+
|
734
|
+
include_examples 'allows access to field of projected association'
|
735
|
+
|
736
|
+
it 'retrieves all fields of association' do
|
737
|
+
persisted_person.phone_numbers.first.landline.should be true
|
738
|
+
end
|
739
|
+
end
|
740
|
+
|
741
|
+
include_examples 'is prohibited on 4.4+ server'
|
742
|
+
end
|
743
|
+
end
|
744
|
+
end
|
745
|
+
end
|
571
746
|
end
|
572
747
|
|
573
|
-
context "when setting
|
748
|
+
context "when setting associations to empty values" do
|
574
749
|
|
575
750
|
context "when the document is a referenced in" do
|
576
751
|
|
@@ -578,7 +753,7 @@ describe Mongoid::Association::Accessors do
|
|
578
753
|
Post.new
|
579
754
|
end
|
580
755
|
|
581
|
-
context "when setting the
|
756
|
+
context "when setting the association directly" do
|
582
757
|
|
583
758
|
before do
|
584
759
|
post.person = ""
|
@@ -607,7 +782,7 @@ describe Mongoid::Association::Accessors do
|
|
607
782
|
Person.new
|
608
783
|
end
|
609
784
|
|
610
|
-
context "when setting the
|
785
|
+
context "when setting the association directly" do
|
611
786
|
|
612
787
|
before do
|
613
788
|
person.game = ""
|
@@ -743,7 +918,7 @@ describe Mongoid::Association::Accessors do
|
|
743
918
|
|
744
919
|
before do
|
745
920
|
game.person_id = ""
|
746
|
-
game.save
|
921
|
+
game.save!
|
747
922
|
end
|
748
923
|
|
749
924
|
it "sets the foreign key to empty" do
|
@@ -755,7 +930,7 @@ describe Mongoid::Association::Accessors do
|
|
755
930
|
|
756
931
|
before do
|
757
932
|
game.person_id = person.id.to_s
|
758
|
-
game.save
|
933
|
+
game.save!
|
759
934
|
end
|
760
935
|
|
761
936
|
it "sets the foreign key as ObjectID" do
|
@@ -767,7 +942,7 @@ describe Mongoid::Association::Accessors do
|
|
767
942
|
|
768
943
|
before do
|
769
944
|
game.person_id = person.id
|
770
|
-
game.save
|
945
|
+
game.save!
|
771
946
|
end
|
772
947
|
|
773
948
|
it "keeps the foreign key as ObjectID" do
|
@@ -775,30 +950,30 @@ describe Mongoid::Association::Accessors do
|
|
775
950
|
end
|
776
951
|
end
|
777
952
|
|
778
|
-
context "when setting ids multiple times on the
|
953
|
+
context "when setting ids multiple times on the association itself" do
|
779
954
|
|
780
955
|
before do
|
781
956
|
game.person = person.id
|
782
957
|
game.person = person.id
|
783
958
|
end
|
784
959
|
|
785
|
-
it "sets the
|
960
|
+
it "sets the association foreign key" do
|
786
961
|
expect(game.person_id).to eq(person.id)
|
787
962
|
end
|
788
963
|
|
789
|
-
it "sets the appropriate
|
964
|
+
it "sets the appropriate association" do
|
790
965
|
expect(game.person).to eq(person)
|
791
966
|
end
|
792
967
|
end
|
793
968
|
end
|
794
969
|
|
795
|
-
context 'when setting the
|
970
|
+
context 'when setting the association more than once' do
|
796
971
|
|
797
972
|
let(:person) do
|
798
973
|
Person.create
|
799
974
|
end
|
800
975
|
|
801
|
-
context 'when the
|
976
|
+
context 'when the association is a references one' do
|
802
977
|
|
803
978
|
let(:game) do
|
804
979
|
Game.create
|
@@ -814,7 +989,7 @@ describe Mongoid::Association::Accessors do
|
|
814
989
|
end
|
815
990
|
end
|
816
991
|
|
817
|
-
context 'when the
|
992
|
+
context 'when the association is a references many' do
|
818
993
|
|
819
994
|
let!(:preference) do
|
820
995
|
Preference.create(name: "Setting")
|
@@ -829,7 +1004,7 @@ describe Mongoid::Association::Accessors do
|
|
829
1004
|
end
|
830
1005
|
end
|
831
1006
|
|
832
|
-
context 'when the
|
1007
|
+
context 'when the association is an embeds one' do
|
833
1008
|
|
834
1009
|
let!(:name) do
|
835
1010
|
Name.new
|
@@ -845,7 +1020,7 @@ describe Mongoid::Association::Accessors do
|
|
845
1020
|
end
|
846
1021
|
end
|
847
1022
|
|
848
|
-
context 'when the
|
1023
|
+
context 'when the association is an embeds many' do
|
849
1024
|
|
850
1025
|
let!(:address) do
|
851
1026
|
Address.new
|