mongoid 8.1.12 → 8.1.14
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
- data/lib/config/locales/en.yml +26 -0
- data/lib/mongoid/association/depending.rb +8 -4
- data/lib/mongoid/association/nested/many.rb +38 -3
- data/lib/mongoid/association/nested/nested_buildable.rb +14 -0
- data/lib/mongoid/association/referenced/has_many/proxy.rb +103 -2
- data/lib/mongoid/attributes/readonly.rb +19 -1
- data/lib/mongoid/changeable.rb +22 -4
- data/lib/mongoid/config.rb +62 -0
- data/lib/mongoid/contextual/aggregable/memory.rb +5 -2
- data/lib/mongoid/contextual/memory.rb +19 -8
- data/lib/mongoid/contextual/none.rb +51 -0
- data/lib/mongoid/criteria/queryable/mergeable.rb +12 -0
- data/lib/mongoid/criteria/queryable/selectable.rb +109 -0
- data/lib/mongoid/criteria/scopable.rb +7 -1
- data/lib/mongoid/criteria.rb +45 -1
- data/lib/mongoid/document.rb +10 -0
- data/lib/mongoid/errors/in_memory_regexp_timeout.rb +26 -0
- data/lib/mongoid/errors.rb +1 -0
- data/lib/mongoid/field_readable.rb +70 -0
- data/lib/mongoid/matchable.rb +6 -1
- data/lib/mongoid/matcher/eq_impl_with_regexp.rb +3 -5
- data/lib/mongoid/matcher/regex.rb +8 -9
- data/lib/mongoid/matcher/regexp_budget.rb +389 -0
- data/lib/mongoid/matcher.rb +1 -0
- data/lib/mongoid/reloadable.rb +1 -0
- data/lib/mongoid/threaded.rb +3 -0
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid/warnings.rb +1 -0
- data/spec/integration/app_spec.rb +8 -0
- data/spec/integration/associations/has_and_belongs_to_many_spec.rb +17 -2
- data/spec/integration/dots_and_dollars_spec.rb +12 -2
- data/spec/integration/matcher_operator_data/regex.yml +21 -0
- data/spec/integration/matcher_regexp_timeout_spec.rb +215 -0
- data/spec/integration/query_operator_guard_spec.rb +89 -0
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +96 -0
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +228 -0
- data/spec/mongoid/attributes/nested_spec.rb +204 -10
- data/spec/mongoid/attributes/readonly_dotted_spec.rb +332 -0
- data/spec/mongoid/attributes/readonly_spec.rb +389 -0
- data/spec/mongoid/changeable_spec.rb +18 -0
- data/spec/mongoid/config_spec.rb +7 -0
- data/spec/mongoid/contextual/aggregable/memory_spec.rb +91 -0
- data/spec/mongoid/contextual/memory_spec.rb +202 -0
- data/spec/mongoid/contextual/none_spec.rb +22 -5
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +2 -0
- data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +235 -0
- data/spec/mongoid/criteria_spec.rb +19 -0
- data/spec/mongoid/findable_spec.rb +14 -0
- data/spec/mongoid/matcher/regexp_budget_spec.rb +576 -0
- data/spec/mongoid/scopable_spec.rb +116 -2
- data/spec/spec_helper.rb +8 -0
- data/spec/support/models/delete_all_account.rb +10 -0
- data/spec/support/models/delete_all_item.rb +9 -0
- data/spec/support/models/delete_all_scoped_item.rb +11 -0
- data/spec/support/models/delete_all_tag.rb +7 -0
- metadata +1900 -1860
|
@@ -83,6 +83,30 @@ describe Mongoid::Attributes::Readonly do
|
|
|
83
83
|
it "persists the third readonly value" do
|
|
84
84
|
expect(person.reload.aliased_timestamp).to eq(Time.at(42))
|
|
85
85
|
end
|
|
86
|
+
|
|
87
|
+
it 'reports the readonly value as a saved change' do
|
|
88
|
+
expect(person.previous_changes['title']).to eq([ nil, 'sir' ])
|
|
89
|
+
expect(person.saved_change_to_attribute('title')).to eq([ nil, 'sir' ])
|
|
90
|
+
expect(person.saved_change_to_attribute?(:title)).to be true
|
|
91
|
+
expect(person.title_previously_changed?).to be true
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
it 'reports the saved change for an aliased readonly field' do
|
|
95
|
+
expect(person.previous_changes['at']).to eq([ nil, Time.at(42) ])
|
|
96
|
+
expect(person.saved_change_to_attribute?(:aliased_timestamp)).to be true
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
it 'does not persist a later change to the readonly field' do
|
|
100
|
+
person.title = 'mr'
|
|
101
|
+
person.score = 1
|
|
102
|
+
person.save!
|
|
103
|
+
|
|
104
|
+
expect(person.previous_changes).not_to have_key('title')
|
|
105
|
+
expect(person.previous_changes).to have_key('score')
|
|
106
|
+
|
|
107
|
+
expect(person.reload.title).to eq('sir')
|
|
108
|
+
expect(person.reload.score).to eq(1)
|
|
109
|
+
end
|
|
86
110
|
end
|
|
87
111
|
|
|
88
112
|
context "when updating an existing readonly field" do
|
|
@@ -233,6 +257,371 @@ describe Mongoid::Attributes::Readonly do
|
|
|
233
257
|
end
|
|
234
258
|
end
|
|
235
259
|
|
|
260
|
+
context 'when modifying a readonly field in place' do
|
|
261
|
+
let(:attributes) do
|
|
262
|
+
%i[map aliases array]
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
let(:person) do
|
|
266
|
+
Person.create!(map: { 'enabled' => false }, aliases: [ 'original' ], array: [ 'original' ], title: 'old')
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
it 'does not persist changes to a readonly hash' do
|
|
270
|
+
person.map['enabled'] = true
|
|
271
|
+
person.save!
|
|
272
|
+
|
|
273
|
+
expect(person.reload.map).to eq('enabled' => false)
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
it 'does not mark the document as changed' do
|
|
277
|
+
person.map['enabled'] = true
|
|
278
|
+
|
|
279
|
+
expect(person).not_to be_changed
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
it 'does not mark an aliased readonly field as changed' do
|
|
283
|
+
person.array << 'added'
|
|
284
|
+
|
|
285
|
+
expect(person).not_to be_changed
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
it 'allows in-place changes before the document is created' do
|
|
289
|
+
new_person = Person.new(map: { 'enabled' => false })
|
|
290
|
+
new_person.map['enabled'] = true
|
|
291
|
+
new_person.save!
|
|
292
|
+
|
|
293
|
+
expect(new_person.reload.map).to eq('enabled' => true)
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
it 'does not report the ignored change as saved' do
|
|
297
|
+
person.map['enabled'] = true
|
|
298
|
+
person.title = 'new'
|
|
299
|
+
person.save!
|
|
300
|
+
|
|
301
|
+
expect(person.previous_changes).not_to have_key('map')
|
|
302
|
+
expect(person.previous_changes).to have_key('title')
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
it 'does not update timestamps for an ignored change' do
|
|
306
|
+
model = test_model do
|
|
307
|
+
include Mongoid::Timestamps
|
|
308
|
+
|
|
309
|
+
field :settings, type: Hash
|
|
310
|
+
attr_readonly :settings
|
|
311
|
+
end
|
|
312
|
+
document = Timecop.freeze(Time.utc(2020, 1, 1)) do
|
|
313
|
+
model.create!(settings: { 'enabled' => false })
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
document.settings['enabled'] = true
|
|
317
|
+
Timecop.freeze(Time.utc(2020, 1, 2)) { document.save! }
|
|
318
|
+
|
|
319
|
+
expect(document.reload.updated_at).to eq(Time.utc(2020, 1, 1))
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
it 'does not persist changes made through attributes' do
|
|
323
|
+
person.map
|
|
324
|
+
person.attributes['map']['enabled'] = true
|
|
325
|
+
person.save!
|
|
326
|
+
|
|
327
|
+
expect(person.reload.map).to eq('enabled' => false)
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
it 'resets an in-place change to a readonly hash' do
|
|
331
|
+
person.map['enabled'] = true
|
|
332
|
+
person.reset_map!
|
|
333
|
+
|
|
334
|
+
expect(person.map).to eq('enabled' => false)
|
|
335
|
+
expect(person).not_to be_changed
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
context 'when a tracked field becomes readonly' do
|
|
339
|
+
let(:model) do
|
|
340
|
+
test_model do
|
|
341
|
+
include Mongoid::Timestamps
|
|
342
|
+
|
|
343
|
+
field :settings, type: Hash
|
|
344
|
+
field :title
|
|
345
|
+
end
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
let(:document) do
|
|
349
|
+
Timecop.freeze(Time.utc(2020, 1, 1)) do
|
|
350
|
+
model.create!(settings: { 'enabled' => false }, title: 'old')
|
|
351
|
+
end
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
before do
|
|
355
|
+
document.settings['enabled'] = true
|
|
356
|
+
model.attr_readonly :settings
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
it 'does not persist the ignored change' do
|
|
360
|
+
document.save!
|
|
361
|
+
|
|
362
|
+
expect(document.reload.settings).to eq('enabled' => false)
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
it 'does not report the ignored change as dirty' do
|
|
366
|
+
expect(document).not_to be_changed
|
|
367
|
+
expect(document.changes).not_to have_key('settings')
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
it 'does not report the readonly field as changed' do
|
|
371
|
+
expect(document.settings_changed?).to be false
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
it 'does not report the ignored change as saved' do
|
|
375
|
+
document.title = 'new'
|
|
376
|
+
document.save!
|
|
377
|
+
|
|
378
|
+
expect(document.previous_changes).not_to have_key('settings')
|
|
379
|
+
expect(document.saved_change_to_attribute?(:settings)).to be false
|
|
380
|
+
expect(document.previous_changes).to have_key('title')
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
it 'does not update timestamps for the ignored change' do
|
|
384
|
+
Timecop.freeze(Time.utc(2020, 1, 2)) { document.save! }
|
|
385
|
+
|
|
386
|
+
expect(document.reload.updated_at).to eq(Time.utc(2020, 1, 1))
|
|
387
|
+
end
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
it 'does not persist changes to a readonly array' do
|
|
391
|
+
person.aliases << 'added'
|
|
392
|
+
person.save!
|
|
393
|
+
|
|
394
|
+
expect(person.reload.aliases).to eq([ 'original' ])
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
it 'does not persist changes to an aliased readonly field' do
|
|
398
|
+
person.array << 'added'
|
|
399
|
+
person.save!
|
|
400
|
+
|
|
401
|
+
expect(person.reload.array).to eq([ 'original' ])
|
|
402
|
+
end
|
|
403
|
+
|
|
404
|
+
it 'persists changes to other fields' do
|
|
405
|
+
person.map['enabled'] = true
|
|
406
|
+
person.title = 'new'
|
|
407
|
+
person.save!
|
|
408
|
+
|
|
409
|
+
expect(person.reload.map).to eq('enabled' => false)
|
|
410
|
+
expect(person.title).to eq('new')
|
|
411
|
+
end
|
|
412
|
+
|
|
413
|
+
it 'still allows atomic set on a readonly field' do
|
|
414
|
+
person.set('map.enabled' => true)
|
|
415
|
+
|
|
416
|
+
expect(person.reload.map).to eq('enabled' => true)
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
it 'still allows atomic unset on a readonly field' do
|
|
420
|
+
person.unset('map.enabled')
|
|
421
|
+
|
|
422
|
+
expect(person.reload.map).to eq({})
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
context 'when an atomic array update fails' do
|
|
426
|
+
it 'restores the readonly array in memory and in the database' do
|
|
427
|
+
expect do
|
|
428
|
+
person.atomically do
|
|
429
|
+
person.push(aliases: 'added')
|
|
430
|
+
raise 'abort'
|
|
431
|
+
end
|
|
432
|
+
end.to raise_error('abort')
|
|
433
|
+
|
|
434
|
+
expect(Person.collection.find(_id: person.id).first['aliases']).to eq([ 'original' ])
|
|
435
|
+
expect(person.aliases).to eq([ 'original' ])
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
it 'still allows a successful atomic update' do
|
|
439
|
+
person.atomically { person.push(aliases: 'added') }
|
|
440
|
+
|
|
441
|
+
expect(person.reload.aliases).to eq(%w[original added])
|
|
442
|
+
end
|
|
443
|
+
end
|
|
444
|
+
end
|
|
445
|
+
|
|
446
|
+
context 'when an embedded document has a readonly hash' do
|
|
447
|
+
let(:attributes) do
|
|
448
|
+
:title
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
let(:person) do
|
|
452
|
+
Person.create!(addresses: [ { street: 'Main', map: { 'enabled' => false } } ])
|
|
453
|
+
end
|
|
454
|
+
|
|
455
|
+
around do |example|
|
|
456
|
+
original = Address.readonly_attributes
|
|
457
|
+
Address.attr_readonly :map
|
|
458
|
+
example.run
|
|
459
|
+
ensure
|
|
460
|
+
Address.readonly_attributes = original
|
|
461
|
+
end
|
|
462
|
+
|
|
463
|
+
it 'allows the initial value to be saved' do
|
|
464
|
+
expect(person.reload.addresses.first.map).to eq('enabled' => false)
|
|
465
|
+
end
|
|
466
|
+
|
|
467
|
+
it 'ignores in-place changes when saving the parent' do
|
|
468
|
+
person.addresses.first.map['enabled'] = true
|
|
469
|
+
person.save!
|
|
470
|
+
|
|
471
|
+
expect(person.reload.addresses.first.map).to eq('enabled' => false)
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
it 'ignores in-place changes when saving the embedded document' do
|
|
475
|
+
address = person.addresses.first
|
|
476
|
+
address.map['enabled'] = true
|
|
477
|
+
address.save!
|
|
478
|
+
|
|
479
|
+
expect(person.reload.addresses.first.map).to eq('enabled' => false)
|
|
480
|
+
end
|
|
481
|
+
|
|
482
|
+
it 'allows an initial value when adding a new embedded document' do
|
|
483
|
+
person.addresses.build(street: 'Second', map: { 'enabled' => true })
|
|
484
|
+
person.save!
|
|
485
|
+
|
|
486
|
+
expect(person.reload.addresses.last.map).to eq('enabled' => true)
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
it 'reports the initial value as a saved change' do
|
|
490
|
+
address = person.addresses.first
|
|
491
|
+
|
|
492
|
+
expect(address.previous_changes['map']).to eq([ nil, { 'enabled' => false } ])
|
|
493
|
+
expect(address.saved_change_to_attribute?(:map)).to be true
|
|
494
|
+
end
|
|
495
|
+
end
|
|
496
|
+
|
|
497
|
+
context 'when a persisted root document is converted with becomes' do
|
|
498
|
+
let(:attributes) do
|
|
499
|
+
:title
|
|
500
|
+
end
|
|
501
|
+
|
|
502
|
+
let(:model) do
|
|
503
|
+
test_model do
|
|
504
|
+
field :settings, type: Hash
|
|
505
|
+
attr_readonly :settings
|
|
506
|
+
end
|
|
507
|
+
end
|
|
508
|
+
|
|
509
|
+
let(:original) do
|
|
510
|
+
model.create!(settings: { 'enabled' => false })
|
|
511
|
+
end
|
|
512
|
+
|
|
513
|
+
it 'does not persist an in-place change to the readonly field of the became root document' do
|
|
514
|
+
became = original.becomes(model)
|
|
515
|
+
became.settings['enabled'] = true
|
|
516
|
+
became.save!
|
|
517
|
+
|
|
518
|
+
expect(model.collection.find(_id: original.id).first['settings']).to eq('enabled' => false)
|
|
519
|
+
end
|
|
520
|
+
end
|
|
521
|
+
|
|
522
|
+
context 'when a persisted document is converted to another class with becomes' do
|
|
523
|
+
let(:attributes) do
|
|
524
|
+
:title
|
|
525
|
+
end
|
|
526
|
+
|
|
527
|
+
let(:model) do
|
|
528
|
+
test_model do
|
|
529
|
+
field :settings, type: Hash
|
|
530
|
+
attr_readonly :settings
|
|
531
|
+
end
|
|
532
|
+
end
|
|
533
|
+
|
|
534
|
+
let(:subclass) do
|
|
535
|
+
Class.new(model)
|
|
536
|
+
end
|
|
537
|
+
|
|
538
|
+
let(:original) do
|
|
539
|
+
model.create!(settings: { 'enabled' => false })
|
|
540
|
+
end
|
|
541
|
+
|
|
542
|
+
it 'does not persist an in-place change to the readonly field of the became document' do
|
|
543
|
+
became = original.becomes(subclass)
|
|
544
|
+
became.settings['enabled'] = true
|
|
545
|
+
became.save!
|
|
546
|
+
|
|
547
|
+
expect(model.collection.find(_id: original.id).first['settings']).to eq('enabled' => false)
|
|
548
|
+
end
|
|
549
|
+
end
|
|
550
|
+
|
|
551
|
+
context 'when a persisted document with embedded documents is converted with becomes' do
|
|
552
|
+
let(:attributes) do
|
|
553
|
+
:title
|
|
554
|
+
end
|
|
555
|
+
|
|
556
|
+
let(:person) do
|
|
557
|
+
Person.create!(addresses: [ { street: 'Main', map: { 'enabled' => false } } ])
|
|
558
|
+
end
|
|
559
|
+
|
|
560
|
+
around do |example|
|
|
561
|
+
original = Address.readonly_attributes
|
|
562
|
+
Address.attr_readonly :map
|
|
563
|
+
example.run
|
|
564
|
+
ensure
|
|
565
|
+
Address.readonly_attributes = original
|
|
566
|
+
end
|
|
567
|
+
|
|
568
|
+
it 'does not persist an in-place change to the readonly field of a became embedded document' do
|
|
569
|
+
became = person.becomes(Person)
|
|
570
|
+
became.addresses.first.map['enabled'] = true
|
|
571
|
+
became.save!
|
|
572
|
+
|
|
573
|
+
stored = Person.collection.find(_id: person.id).first['addresses'].first['map']
|
|
574
|
+
expect(stored).to eq('enabled' => false)
|
|
575
|
+
end
|
|
576
|
+
end
|
|
577
|
+
|
|
578
|
+
context 'when a new document is converted with becomes' do
|
|
579
|
+
let(:attributes) do
|
|
580
|
+
:title
|
|
581
|
+
end
|
|
582
|
+
|
|
583
|
+
let(:model) do
|
|
584
|
+
test_model do
|
|
585
|
+
field :settings, type: Hash
|
|
586
|
+
attr_readonly :settings
|
|
587
|
+
end
|
|
588
|
+
end
|
|
589
|
+
|
|
590
|
+
it 'reports the readonly value as a saved change when the became document is created' do
|
|
591
|
+
became = model.new(settings: { 'enabled' => false }).becomes(model)
|
|
592
|
+
became.save!
|
|
593
|
+
|
|
594
|
+
expect(became.previous_changes['settings']).to eq([ nil, { 'enabled' => false } ])
|
|
595
|
+
expect(model.collection.find(_id: became.id).first['settings']).to eq('enabled' => false)
|
|
596
|
+
end
|
|
597
|
+
end
|
|
598
|
+
|
|
599
|
+
context 'when a field is named like an internal dirty tracking method' do
|
|
600
|
+
let(:attributes) do
|
|
601
|
+
:title
|
|
602
|
+
end
|
|
603
|
+
|
|
604
|
+
let(:model) do
|
|
605
|
+
test_model do
|
|
606
|
+
field :name, type: String
|
|
607
|
+
field :readonly_change_ignored, type: String
|
|
608
|
+
end
|
|
609
|
+
end
|
|
610
|
+
|
|
611
|
+
it 'tracks changes and creates the document' do
|
|
612
|
+
document = model.new(name: 'original', readonly_change_ignored: 'value')
|
|
613
|
+
|
|
614
|
+
expect(document.name_changed?).to be true
|
|
615
|
+
|
|
616
|
+
document.save!
|
|
617
|
+
|
|
618
|
+
expect(document.reload.name).to eq('original')
|
|
619
|
+
expect(document.readonly_change_ignored).to eq('value')
|
|
620
|
+
expect(document.readonly_change_ignored?).to be true
|
|
621
|
+
expect(document).not_to be_changed
|
|
622
|
+
end
|
|
623
|
+
end
|
|
624
|
+
|
|
236
625
|
context 'when a foreign_key is defined as readonly' do
|
|
237
626
|
|
|
238
627
|
let(:attributes) do
|
|
@@ -1506,6 +1506,24 @@ describe Mongoid::Changeable do
|
|
|
1506
1506
|
expect(person.title).to be_nil
|
|
1507
1507
|
end
|
|
1508
1508
|
end
|
|
1509
|
+
|
|
1510
|
+
context 'when a resizable attribute was read but its value has not changed' do
|
|
1511
|
+
let(:person) do
|
|
1512
|
+
Person.create!(map: { 'enabled' => false })
|
|
1513
|
+
end
|
|
1514
|
+
|
|
1515
|
+
it 'keeps the current value object so held references stay attached' do
|
|
1516
|
+
map = person.map
|
|
1517
|
+
person.reset_map!
|
|
1518
|
+
|
|
1519
|
+
expect(person.attributes['map']).to equal(map)
|
|
1520
|
+
|
|
1521
|
+
map['enabled'] = true
|
|
1522
|
+
person.save!
|
|
1523
|
+
|
|
1524
|
+
expect(person.reload.map).to eq('enabled' => true)
|
|
1525
|
+
end
|
|
1526
|
+
end
|
|
1509
1527
|
end
|
|
1510
1528
|
|
|
1511
1529
|
describe "#reset_attribute_to_default!" do
|
data/spec/mongoid/config_spec.rb
CHANGED
|
@@ -306,6 +306,13 @@ describe Mongoid::Config do
|
|
|
306
306
|
end
|
|
307
307
|
end
|
|
308
308
|
|
|
309
|
+
# Config.reset does not invoke the option's on_change callback, so use the
|
|
310
|
+
# setter to restore any global state the callback modifies (e.g. the BSON
|
|
311
|
+
# registry for allow_bson5_decimal128).
|
|
312
|
+
after do
|
|
313
|
+
Mongoid.send("#{option}=", default)
|
|
314
|
+
end
|
|
315
|
+
|
|
309
316
|
context 'when the value is false' do
|
|
310
317
|
|
|
311
318
|
let(:conf) do
|
|
@@ -570,4 +570,95 @@ describe Mongoid::Contextual::Aggregable::Memory do
|
|
|
570
570
|
end
|
|
571
571
|
end
|
|
572
572
|
end
|
|
573
|
+
|
|
574
|
+
context 'when the field name is a method name' do
|
|
575
|
+
let!(:depeche) do
|
|
576
|
+
Band.create!(name: 'Depeche Mode', likes: 1000)
|
|
577
|
+
end
|
|
578
|
+
|
|
579
|
+
let(:criteria) do
|
|
580
|
+
Band.all.tap do |crit|
|
|
581
|
+
crit.documents = [ depeche ]
|
|
582
|
+
end
|
|
583
|
+
end
|
|
584
|
+
|
|
585
|
+
it 'does not call the method for sum' do
|
|
586
|
+
expect(depeche).not_to receive(:delete)
|
|
587
|
+
expect(context.sum(:delete)).to eq(0)
|
|
588
|
+
end
|
|
589
|
+
|
|
590
|
+
it 'does not call the method for min' do
|
|
591
|
+
expect(depeche).not_to receive(:delete)
|
|
592
|
+
expect(context.min(:delete)).to be_nil
|
|
593
|
+
end
|
|
594
|
+
|
|
595
|
+
it 'does not call the method for max' do
|
|
596
|
+
expect(depeche).not_to receive(:delete)
|
|
597
|
+
expect(context.max(:delete)).to be_nil
|
|
598
|
+
end
|
|
599
|
+
|
|
600
|
+
it 'does not call the method for avg' do
|
|
601
|
+
expect(depeche).not_to receive(:delete)
|
|
602
|
+
expect(context.avg(:delete)).to be_nil
|
|
603
|
+
end
|
|
604
|
+
|
|
605
|
+
it 'does not call the method for aggregates' do
|
|
606
|
+
expect(depeche).not_to receive(:delete)
|
|
607
|
+
expect(context.aggregates(:delete))
|
|
608
|
+
.to eq('count' => 0, 'avg' => nil, 'max' => nil, 'min' => nil, 'sum' => 0)
|
|
609
|
+
end
|
|
610
|
+
|
|
611
|
+
it 'does not call a private method for avg' do
|
|
612
|
+
expect(depeche).not_to receive(:exit)
|
|
613
|
+
expect(context.avg(:exit)).to be_nil
|
|
614
|
+
end
|
|
615
|
+
end
|
|
616
|
+
|
|
617
|
+
context 'when the field is not defined' do
|
|
618
|
+
let!(:depeche) do
|
|
619
|
+
Band.create!(name: 'Depeche Mode', likes: 1000)
|
|
620
|
+
end
|
|
621
|
+
|
|
622
|
+
let(:criteria) do
|
|
623
|
+
Band.all.tap do |crit|
|
|
624
|
+
crit.documents = [ depeche ]
|
|
625
|
+
end
|
|
626
|
+
end
|
|
627
|
+
|
|
628
|
+
it 'returns zero for sum' do
|
|
629
|
+
expect(context.sum(:not_a_field)).to eq(0)
|
|
630
|
+
end
|
|
631
|
+
|
|
632
|
+
it 'returns nil for min' do
|
|
633
|
+
expect(context.min(:not_a_field)).to be_nil
|
|
634
|
+
end
|
|
635
|
+
|
|
636
|
+
it 'returns nil for max' do
|
|
637
|
+
expect(context.max(:not_a_field)).to be_nil
|
|
638
|
+
end
|
|
639
|
+
|
|
640
|
+
it 'returns nil for avg' do
|
|
641
|
+
expect(context.avg(:not_a_field)).to be_nil
|
|
642
|
+
end
|
|
643
|
+
end
|
|
644
|
+
|
|
645
|
+
context 'when the field is the foreign key of a many to many association' do
|
|
646
|
+
let!(:preference) do
|
|
647
|
+
Preference.create!(name: 'nature')
|
|
648
|
+
end
|
|
649
|
+
|
|
650
|
+
let!(:person) do
|
|
651
|
+
Person.new(preferences: [ preference ])
|
|
652
|
+
end
|
|
653
|
+
|
|
654
|
+
let(:criteria) do
|
|
655
|
+
Person.all.tap do |crit|
|
|
656
|
+
crit.documents = [ person ]
|
|
657
|
+
end
|
|
658
|
+
end
|
|
659
|
+
|
|
660
|
+
it 'aggregates the stored ids' do
|
|
661
|
+
expect(context.max(:preference_ids)).to eq([ preference.id ])
|
|
662
|
+
end
|
|
663
|
+
end
|
|
573
664
|
end
|