mongoid 8.0.13 → 8.0.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/mongoid/association/referenced/has_many/proxy.rb +4 -2
- data/lib/mongoid/attributes/readonly.rb +19 -1
- data/lib/mongoid/changeable.rb +22 -4
- data/lib/mongoid/contextual/memory.rb +1 -1
- data/lib/mongoid/contextual/none.rb +51 -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/reloadable.rb +1 -0
- data/lib/mongoid/threaded.rb +1 -1
- data/lib/mongoid/version.rb +1 -1
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +48 -0
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +83 -0
- data/spec/mongoid/attributes/readonly_dotted_spec.rb +332 -0
- data/spec/mongoid/attributes/readonly_spec.rb +387 -0
- data/spec/mongoid/changeable_spec.rb +18 -0
- data/spec/mongoid/config_spec.rb +7 -0
- data/spec/mongoid/contextual/memory_spec.rb +25 -0
- data/spec/mongoid/contextual/none_spec.rb +18 -0
- data/spec/mongoid/criteria_spec.rb +17 -0
- data/spec/mongoid/findable_spec.rb +14 -0
- data/spec/mongoid/matcher/regexp_budget_spec.rb +6 -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 +1862 -1835
|
@@ -83,6 +83,28 @@ 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.title_previously_changed?).to be true
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
it 'reports the saved change for an aliased readonly field' do
|
|
93
|
+
expect(person.previous_changes['at']).to eq([ nil, Time.at(42) ])
|
|
94
|
+
expect(person.aliased_timestamp_previously_changed?).to be true
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it 'does not persist a later change to the readonly field' do
|
|
98
|
+
person.title = 'mr'
|
|
99
|
+
person.score = 1
|
|
100
|
+
person.save!
|
|
101
|
+
|
|
102
|
+
expect(person.previous_changes).not_to have_key('title')
|
|
103
|
+
expect(person.previous_changes).to have_key('score')
|
|
104
|
+
|
|
105
|
+
expect(person.reload.title).to eq('sir')
|
|
106
|
+
expect(person.reload.score).to eq(1)
|
|
107
|
+
end
|
|
86
108
|
end
|
|
87
109
|
|
|
88
110
|
context "when updating an existing readonly field" do
|
|
@@ -233,6 +255,371 @@ describe Mongoid::Attributes::Readonly do
|
|
|
233
255
|
end
|
|
234
256
|
end
|
|
235
257
|
|
|
258
|
+
context 'when modifying a readonly field in place' do
|
|
259
|
+
let(:attributes) do
|
|
260
|
+
%i[map aliases array]
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
let(:person) do
|
|
264
|
+
Person.create!(map: { 'enabled' => false }, aliases: [ 'original' ], array: [ 'original' ], title: 'old')
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
it 'does not persist changes to a readonly hash' do
|
|
268
|
+
person.map['enabled'] = true
|
|
269
|
+
person.save!
|
|
270
|
+
|
|
271
|
+
expect(person.reload.map).to eq('enabled' => false)
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
it 'does not mark the document as changed' do
|
|
275
|
+
person.map['enabled'] = true
|
|
276
|
+
|
|
277
|
+
expect(person).not_to be_changed
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
it 'does not mark an aliased readonly field as changed' do
|
|
281
|
+
person.array << 'added'
|
|
282
|
+
|
|
283
|
+
expect(person).not_to be_changed
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
it 'allows in-place changes before the document is created' do
|
|
287
|
+
new_person = Person.new(map: { 'enabled' => false })
|
|
288
|
+
new_person.map['enabled'] = true
|
|
289
|
+
new_person.save!
|
|
290
|
+
|
|
291
|
+
expect(new_person.reload.map).to eq('enabled' => true)
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
it 'does not report the ignored change as saved' do
|
|
295
|
+
person.map['enabled'] = true
|
|
296
|
+
person.title = 'new'
|
|
297
|
+
person.save!
|
|
298
|
+
|
|
299
|
+
expect(person.previous_changes).not_to have_key('map')
|
|
300
|
+
expect(person.previous_changes).to have_key('title')
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
it 'does not update timestamps for an ignored change' do
|
|
304
|
+
model = test_model do
|
|
305
|
+
include Mongoid::Timestamps
|
|
306
|
+
|
|
307
|
+
field :settings, type: Hash
|
|
308
|
+
attr_readonly :settings
|
|
309
|
+
end
|
|
310
|
+
document = Timecop.freeze(Time.utc(2020, 1, 1)) do
|
|
311
|
+
model.create!(settings: { 'enabled' => false })
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
document.settings['enabled'] = true
|
|
315
|
+
Timecop.freeze(Time.utc(2020, 1, 2)) { document.save! }
|
|
316
|
+
|
|
317
|
+
expect(document.reload.updated_at).to eq(Time.utc(2020, 1, 1))
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
it 'does not persist changes made through attributes' do
|
|
321
|
+
person.map
|
|
322
|
+
person.attributes['map']['enabled'] = true
|
|
323
|
+
person.save!
|
|
324
|
+
|
|
325
|
+
expect(person.reload.map).to eq('enabled' => false)
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
it 'resets an in-place change to a readonly hash' do
|
|
329
|
+
person.map['enabled'] = true
|
|
330
|
+
person.reset_map!
|
|
331
|
+
|
|
332
|
+
expect(person.map).to eq('enabled' => false)
|
|
333
|
+
expect(person).not_to be_changed
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
context 'when a tracked field becomes readonly' do
|
|
337
|
+
let(:model) do
|
|
338
|
+
test_model do
|
|
339
|
+
include Mongoid::Timestamps
|
|
340
|
+
|
|
341
|
+
field :settings, type: Hash
|
|
342
|
+
field :title
|
|
343
|
+
end
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
let(:document) do
|
|
347
|
+
Timecop.freeze(Time.utc(2020, 1, 1)) do
|
|
348
|
+
model.create!(settings: { 'enabled' => false }, title: 'old')
|
|
349
|
+
end
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
before do
|
|
353
|
+
document.settings['enabled'] = true
|
|
354
|
+
model.attr_readonly :settings
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
it 'does not persist the ignored change' do
|
|
358
|
+
document.save!
|
|
359
|
+
|
|
360
|
+
expect(document.reload.settings).to eq('enabled' => false)
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
it 'does not report the ignored change as dirty' do
|
|
364
|
+
expect(document).not_to be_changed
|
|
365
|
+
expect(document.changes).not_to have_key('settings')
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
it 'does not report the readonly field as changed' do
|
|
369
|
+
expect(document.settings_changed?).to be false
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
it 'does not report the ignored change as saved' do
|
|
373
|
+
document.title = 'new'
|
|
374
|
+
document.save!
|
|
375
|
+
|
|
376
|
+
expect(document.previous_changes).not_to have_key('settings')
|
|
377
|
+
expect(document.settings_previously_changed?).to be false
|
|
378
|
+
expect(document.previous_changes).to have_key('title')
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
it 'does not update timestamps for the ignored change' do
|
|
382
|
+
Timecop.freeze(Time.utc(2020, 1, 2)) { document.save! }
|
|
383
|
+
|
|
384
|
+
expect(document.reload.updated_at).to eq(Time.utc(2020, 1, 1))
|
|
385
|
+
end
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
it 'does not persist changes to a readonly array' do
|
|
389
|
+
person.aliases << 'added'
|
|
390
|
+
person.save!
|
|
391
|
+
|
|
392
|
+
expect(person.reload.aliases).to eq([ 'original' ])
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
it 'does not persist changes to an aliased readonly field' do
|
|
396
|
+
person.array << 'added'
|
|
397
|
+
person.save!
|
|
398
|
+
|
|
399
|
+
expect(person.reload.array).to eq([ 'original' ])
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
it 'persists changes to other fields' do
|
|
403
|
+
person.map['enabled'] = true
|
|
404
|
+
person.title = 'new'
|
|
405
|
+
person.save!
|
|
406
|
+
|
|
407
|
+
expect(person.reload.map).to eq('enabled' => false)
|
|
408
|
+
expect(person.title).to eq('new')
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
it 'still allows atomic set on a readonly field' do
|
|
412
|
+
person.set('map.enabled' => true)
|
|
413
|
+
|
|
414
|
+
expect(person.reload.map).to eq('enabled' => true)
|
|
415
|
+
end
|
|
416
|
+
|
|
417
|
+
it 'still allows atomic unset on a readonly field' do
|
|
418
|
+
person.unset('map.enabled')
|
|
419
|
+
|
|
420
|
+
expect(person.reload.map).to eq({})
|
|
421
|
+
end
|
|
422
|
+
|
|
423
|
+
context 'when an atomic array update fails' do
|
|
424
|
+
it 'restores the readonly array in memory and in the database' do
|
|
425
|
+
expect do
|
|
426
|
+
person.atomically do
|
|
427
|
+
person.push(aliases: 'added')
|
|
428
|
+
raise 'abort'
|
|
429
|
+
end
|
|
430
|
+
end.to raise_error('abort')
|
|
431
|
+
|
|
432
|
+
expect(Person.collection.find(_id: person.id).first['aliases']).to eq([ 'original' ])
|
|
433
|
+
expect(person.aliases).to eq([ 'original' ])
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
it 'still allows a successful atomic update' do
|
|
437
|
+
person.atomically { person.push(aliases: 'added') }
|
|
438
|
+
|
|
439
|
+
expect(person.reload.aliases).to eq(%w[original added])
|
|
440
|
+
end
|
|
441
|
+
end
|
|
442
|
+
end
|
|
443
|
+
|
|
444
|
+
context 'when an embedded document has a readonly hash' do
|
|
445
|
+
let(:attributes) do
|
|
446
|
+
:title
|
|
447
|
+
end
|
|
448
|
+
|
|
449
|
+
let(:person) do
|
|
450
|
+
Person.create!(addresses: [ { street: 'Main', map: { 'enabled' => false } } ])
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
around do |example|
|
|
454
|
+
original = Address.readonly_attributes
|
|
455
|
+
Address.attr_readonly :map
|
|
456
|
+
example.run
|
|
457
|
+
ensure
|
|
458
|
+
Address.readonly_attributes = original
|
|
459
|
+
end
|
|
460
|
+
|
|
461
|
+
it 'allows the initial value to be saved' do
|
|
462
|
+
expect(person.reload.addresses.first.map).to eq('enabled' => false)
|
|
463
|
+
end
|
|
464
|
+
|
|
465
|
+
it 'ignores in-place changes when saving the parent' do
|
|
466
|
+
person.addresses.first.map['enabled'] = true
|
|
467
|
+
person.save!
|
|
468
|
+
|
|
469
|
+
expect(person.reload.addresses.first.map).to eq('enabled' => false)
|
|
470
|
+
end
|
|
471
|
+
|
|
472
|
+
it 'ignores in-place changes when saving the embedded document' do
|
|
473
|
+
address = person.addresses.first
|
|
474
|
+
address.map['enabled'] = true
|
|
475
|
+
address.save!
|
|
476
|
+
|
|
477
|
+
expect(person.reload.addresses.first.map).to eq('enabled' => false)
|
|
478
|
+
end
|
|
479
|
+
|
|
480
|
+
it 'allows an initial value when adding a new embedded document' do
|
|
481
|
+
person.addresses.build(street: 'Second', map: { 'enabled' => true })
|
|
482
|
+
person.save!
|
|
483
|
+
|
|
484
|
+
expect(person.reload.addresses.last.map).to eq('enabled' => true)
|
|
485
|
+
end
|
|
486
|
+
|
|
487
|
+
it 'reports the initial value as a saved change' do
|
|
488
|
+
address = person.addresses.first
|
|
489
|
+
|
|
490
|
+
expect(address.previous_changes['map']).to eq([ nil, { 'enabled' => false } ])
|
|
491
|
+
expect(address.map_previously_changed?).to be true
|
|
492
|
+
end
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
context 'when a persisted root document is converted with becomes' do
|
|
496
|
+
let(:attributes) do
|
|
497
|
+
:title
|
|
498
|
+
end
|
|
499
|
+
|
|
500
|
+
let(:model) do
|
|
501
|
+
test_model do
|
|
502
|
+
field :settings, type: Hash
|
|
503
|
+
attr_readonly :settings
|
|
504
|
+
end
|
|
505
|
+
end
|
|
506
|
+
|
|
507
|
+
let(:original) do
|
|
508
|
+
model.create!(settings: { 'enabled' => false })
|
|
509
|
+
end
|
|
510
|
+
|
|
511
|
+
it 'does not persist an in-place change to the readonly field of the became root document' do
|
|
512
|
+
became = original.becomes(model)
|
|
513
|
+
became.settings['enabled'] = true
|
|
514
|
+
became.save!
|
|
515
|
+
|
|
516
|
+
expect(model.collection.find(_id: original.id).first['settings']).to eq('enabled' => false)
|
|
517
|
+
end
|
|
518
|
+
end
|
|
519
|
+
|
|
520
|
+
context 'when a persisted document is converted to another class with becomes' do
|
|
521
|
+
let(:attributes) do
|
|
522
|
+
:title
|
|
523
|
+
end
|
|
524
|
+
|
|
525
|
+
let(:model) do
|
|
526
|
+
test_model do
|
|
527
|
+
field :settings, type: Hash
|
|
528
|
+
attr_readonly :settings
|
|
529
|
+
end
|
|
530
|
+
end
|
|
531
|
+
|
|
532
|
+
let(:subclass) do
|
|
533
|
+
Class.new(model)
|
|
534
|
+
end
|
|
535
|
+
|
|
536
|
+
let(:original) do
|
|
537
|
+
model.create!(settings: { 'enabled' => false })
|
|
538
|
+
end
|
|
539
|
+
|
|
540
|
+
it 'does not persist an in-place change to the readonly field of the became document' do
|
|
541
|
+
became = original.becomes(subclass)
|
|
542
|
+
became.settings['enabled'] = true
|
|
543
|
+
became.save!
|
|
544
|
+
|
|
545
|
+
expect(model.collection.find(_id: original.id).first['settings']).to eq('enabled' => false)
|
|
546
|
+
end
|
|
547
|
+
end
|
|
548
|
+
|
|
549
|
+
context 'when a persisted document with embedded documents is converted with becomes' do
|
|
550
|
+
let(:attributes) do
|
|
551
|
+
:title
|
|
552
|
+
end
|
|
553
|
+
|
|
554
|
+
let(:person) do
|
|
555
|
+
Person.create!(addresses: [ { street: 'Main', map: { 'enabled' => false } } ])
|
|
556
|
+
end
|
|
557
|
+
|
|
558
|
+
around do |example|
|
|
559
|
+
original = Address.readonly_attributes
|
|
560
|
+
Address.attr_readonly :map
|
|
561
|
+
example.run
|
|
562
|
+
ensure
|
|
563
|
+
Address.readonly_attributes = original
|
|
564
|
+
end
|
|
565
|
+
|
|
566
|
+
it 'does not persist an in-place change to the readonly field of a became embedded document' do
|
|
567
|
+
became = person.becomes(Person)
|
|
568
|
+
became.addresses.first.map['enabled'] = true
|
|
569
|
+
became.save!
|
|
570
|
+
|
|
571
|
+
stored = Person.collection.find(_id: person.id).first['addresses'].first['map']
|
|
572
|
+
expect(stored).to eq('enabled' => false)
|
|
573
|
+
end
|
|
574
|
+
end
|
|
575
|
+
|
|
576
|
+
context 'when a new document is converted with becomes' do
|
|
577
|
+
let(:attributes) do
|
|
578
|
+
:title
|
|
579
|
+
end
|
|
580
|
+
|
|
581
|
+
let(:model) do
|
|
582
|
+
test_model do
|
|
583
|
+
field :settings, type: Hash
|
|
584
|
+
attr_readonly :settings
|
|
585
|
+
end
|
|
586
|
+
end
|
|
587
|
+
|
|
588
|
+
it 'reports the readonly value as a saved change when the became document is created' do
|
|
589
|
+
became = model.new(settings: { 'enabled' => false }).becomes(model)
|
|
590
|
+
became.save!
|
|
591
|
+
|
|
592
|
+
expect(became.previous_changes['settings']).to eq([ nil, { 'enabled' => false } ])
|
|
593
|
+
expect(model.collection.find(_id: became.id).first['settings']).to eq('enabled' => false)
|
|
594
|
+
end
|
|
595
|
+
end
|
|
596
|
+
|
|
597
|
+
context 'when a field is named like an internal dirty tracking method' do
|
|
598
|
+
let(:attributes) do
|
|
599
|
+
:title
|
|
600
|
+
end
|
|
601
|
+
|
|
602
|
+
let(:model) do
|
|
603
|
+
test_model do
|
|
604
|
+
field :name, type: String
|
|
605
|
+
field :readonly_change_ignored, type: String
|
|
606
|
+
end
|
|
607
|
+
end
|
|
608
|
+
|
|
609
|
+
it 'tracks changes and creates the document' do
|
|
610
|
+
document = model.new(name: 'original', readonly_change_ignored: 'value')
|
|
611
|
+
|
|
612
|
+
expect(document.name_changed?).to be true
|
|
613
|
+
|
|
614
|
+
document.save!
|
|
615
|
+
|
|
616
|
+
expect(document.reload.name).to eq('original')
|
|
617
|
+
expect(document.readonly_change_ignored).to eq('value')
|
|
618
|
+
expect(document.readonly_change_ignored?).to be true
|
|
619
|
+
expect(document).not_to be_changed
|
|
620
|
+
end
|
|
621
|
+
end
|
|
622
|
+
|
|
236
623
|
context 'when a foreign_key is defined as readonly' do
|
|
237
624
|
|
|
238
625
|
let(:attributes) do
|
|
@@ -1342,6 +1342,24 @@ describe Mongoid::Changeable do
|
|
|
1342
1342
|
expect(person.title).to be_nil
|
|
1343
1343
|
end
|
|
1344
1344
|
end
|
|
1345
|
+
|
|
1346
|
+
context 'when a resizable attribute was read but its value has not changed' do
|
|
1347
|
+
let(:person) do
|
|
1348
|
+
Person.create!(map: { 'enabled' => false })
|
|
1349
|
+
end
|
|
1350
|
+
|
|
1351
|
+
it 'keeps the current value object so held references stay attached' do
|
|
1352
|
+
map = person.map
|
|
1353
|
+
person.reset_map!
|
|
1354
|
+
|
|
1355
|
+
expect(person.attributes['map']).to equal(map)
|
|
1356
|
+
|
|
1357
|
+
map['enabled'] = true
|
|
1358
|
+
person.save!
|
|
1359
|
+
|
|
1360
|
+
expect(person.reload.map).to eq('enabled' => true)
|
|
1361
|
+
end
|
|
1362
|
+
end
|
|
1345
1363
|
end
|
|
1346
1364
|
|
|
1347
1365
|
describe "#reset_attribute_to_default!" do
|
data/spec/mongoid/config_spec.rb
CHANGED
|
@@ -233,6 +233,13 @@ describe Mongoid::Config do
|
|
|
233
233
|
end
|
|
234
234
|
end
|
|
235
235
|
|
|
236
|
+
# Config.reset does not invoke the option's on_change callback, so use the
|
|
237
|
+
# setter to restore any global state the callback modifies (e.g. the BSON
|
|
238
|
+
# registry for allow_bson5_decimal128).
|
|
239
|
+
after do
|
|
240
|
+
Mongoid.send("#{option}=", default)
|
|
241
|
+
end
|
|
242
|
+
|
|
236
243
|
context 'when the value is false' do
|
|
237
244
|
|
|
238
245
|
let(:conf) do
|
|
@@ -438,6 +438,31 @@ describe Mongoid::Contextual::Memory do
|
|
|
438
438
|
}.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
|
|
439
439
|
end
|
|
440
440
|
end
|
|
441
|
+
|
|
442
|
+
context "when the field name is a method name" do
|
|
443
|
+
let!(:person) do
|
|
444
|
+
Person.create!(ssn: "secret-ssn")
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
let!(:address) do
|
|
448
|
+
person.addresses.create!(street: "hobrecht")
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
let(:criteria) do
|
|
452
|
+
Address.all.tap do |crit|
|
|
453
|
+
crit.documents = [ address ]
|
|
454
|
+
end
|
|
455
|
+
end
|
|
456
|
+
|
|
457
|
+
let(:context) do
|
|
458
|
+
described_class.new(criteria)
|
|
459
|
+
end
|
|
460
|
+
|
|
461
|
+
it "does not call the method" do
|
|
462
|
+
expect(address).not_to receive(:destroy)
|
|
463
|
+
expect(context.distinct(:destroy)).to eq([ nil ])
|
|
464
|
+
end
|
|
465
|
+
end
|
|
441
466
|
end
|
|
442
467
|
|
|
443
468
|
context "when legacy_pluck_distinct is false" do
|
|
@@ -53,6 +53,14 @@ describe Mongoid::Contextual::None do
|
|
|
53
53
|
end
|
|
54
54
|
end
|
|
55
55
|
|
|
56
|
+
describe '#destroy' do
|
|
57
|
+
it 'returns zero and destroys nothing' do
|
|
58
|
+
expect(context.destroy).to eq(0)
|
|
59
|
+
expect(context.destroy_all).to eq(0)
|
|
60
|
+
expect(Band.where(name: 'Depeche Mode').count).to eq(1)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
56
64
|
describe "#pluck" do
|
|
57
65
|
it "returns an empty array" do
|
|
58
66
|
expect(context.pluck(:id)).to eq([])
|
|
@@ -71,6 +79,16 @@ describe Mongoid::Contextual::None do
|
|
|
71
79
|
end
|
|
72
80
|
end
|
|
73
81
|
|
|
82
|
+
describe '#update' do
|
|
83
|
+
it 'returns false without attributes and nil with them, updating nothing' do
|
|
84
|
+
expect(context.update).to be(false)
|
|
85
|
+
expect(context.update(name: 'The Cure')).to be_nil
|
|
86
|
+
expect(context.update_all).to be(false)
|
|
87
|
+
expect(context.update_all(name: 'The Cure')).to be_nil
|
|
88
|
+
expect(Band.where(name: 'Depeche Mode').first.name).to eq('Depeche Mode')
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
74
92
|
describe "#first" do
|
|
75
93
|
it "returns nil" do
|
|
76
94
|
expect(context.first).to be_nil
|
|
@@ -1507,6 +1507,23 @@ describe Mongoid::Criteria do
|
|
|
1507
1507
|
it "returns the same criteria" do
|
|
1508
1508
|
expect(merged).to equal(criteria)
|
|
1509
1509
|
end
|
|
1510
|
+
|
|
1511
|
+
context 'when merging a none criteria into a criteria with a cached context' do
|
|
1512
|
+
let(:criteria) { Band.where(name: 'Depeche Mode') }
|
|
1513
|
+
let(:mergeable) { Band.none }
|
|
1514
|
+
|
|
1515
|
+
before do
|
|
1516
|
+
Band.create!(name: 'Depeche Mode')
|
|
1517
|
+
end
|
|
1518
|
+
|
|
1519
|
+
it 'does not use the cached context' do
|
|
1520
|
+
expect(criteria.count).to eq(1)
|
|
1521
|
+
|
|
1522
|
+
merged
|
|
1523
|
+
|
|
1524
|
+
expect(criteria.count).to eq(0)
|
|
1525
|
+
end
|
|
1526
|
+
end
|
|
1510
1527
|
end
|
|
1511
1528
|
|
|
1512
1529
|
describe "#min" do
|
|
@@ -457,6 +457,20 @@ describe Mongoid::Findable do
|
|
|
457
457
|
expect(criteria.size).to eq(0)
|
|
458
458
|
end
|
|
459
459
|
end
|
|
460
|
+
|
|
461
|
+
context 'when none is called on a criteria with a cached context' do
|
|
462
|
+
let(:criteria) do
|
|
463
|
+
Band.where(name: 'Depeche Mode')
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
it 'does not use the cached context' do
|
|
467
|
+
expect(criteria.count).to eq(1)
|
|
468
|
+
|
|
469
|
+
criteria.none
|
|
470
|
+
|
|
471
|
+
expect(criteria.count).to eq(0)
|
|
472
|
+
end
|
|
473
|
+
end
|
|
460
474
|
end
|
|
461
475
|
|
|
462
476
|
describe ".pluck" do
|
|
@@ -32,6 +32,12 @@ describe Mongoid::Matcher::RegexpBudget do
|
|
|
32
32
|
# exercising one has to hand .open something to bound.
|
|
33
33
|
let(:regexp_selector) { { 'name' => /\Aabc\z/ } }
|
|
34
34
|
|
|
35
|
+
describe 'REGEXP_BUDGET_KEY' do
|
|
36
|
+
it 'is namespaced so a foreign thread-local cannot collide with it' do
|
|
37
|
+
expect(Mongoid::Threaded::REGEXP_BUDGET_KEY).to start_with('[mongoid]:')
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
35
41
|
describe '.open' do
|
|
36
42
|
context 'when no limit is configured' do
|
|
37
43
|
config_override :in_memory_regexp_time_limit, nil
|
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
require "spec_helper"
|
|
4
4
|
|
|
5
|
+
# Retrieve the singleton class for the given class.
|
|
6
|
+
def singleton_class_for(klass)
|
|
7
|
+
class <<klass; self; end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# Helper method for removing a declared scope
|
|
11
|
+
def remove_scope(klass, scope)
|
|
12
|
+
if klass._declared_scopes[scope]
|
|
13
|
+
singleton_class_for(klass).remove_method(scope)
|
|
14
|
+
klass._declared_scopes.delete(scope)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
5
18
|
describe Mongoid::Scopable do
|
|
6
19
|
|
|
7
20
|
describe ".default_scope" do
|
|
@@ -111,9 +124,92 @@ describe Mongoid::Scopable do
|
|
|
111
124
|
end
|
|
112
125
|
end
|
|
113
126
|
|
|
114
|
-
context
|
|
127
|
+
context 'when the default scope is none' do
|
|
128
|
+
before do
|
|
129
|
+
Band.create!(name: 'Depeche Mode', active: true)
|
|
130
|
+
Band.default_scope -> { Band.none }
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
after do
|
|
134
|
+
Band.default_scoping = nil
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
it 'does not return any documents' do
|
|
138
|
+
expect(Band.count).to eq(0)
|
|
139
|
+
expect(Band.all.to_a).to be_empty
|
|
140
|
+
expect(Band.where(name: 'Depeche Mode').first).to be_nil
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
it 'does not update documents' do
|
|
144
|
+
Band.update_all(active: false)
|
|
145
|
+
expect(Band.unscoped.first.active).to be(true)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
it 'does not delete documents' do
|
|
149
|
+
expect(Band.delete_all).to eq(0)
|
|
150
|
+
expect(Band.unscoped.count).to eq(1)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
it 'removes the none from the default scope when unscoping' do
|
|
154
|
+
crit = Band.all
|
|
155
|
+
expect(crit.count).to eq(0)
|
|
156
|
+
expect(crit.unscoped.count).to eq(1)
|
|
157
|
+
expect(Band.all.merge(Band.all).unscoped.count).to eq(1)
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
it 'keeps an explicit none when unscoping' do
|
|
161
|
+
expect(Band.none.unscoped.count).to eq(0)
|
|
162
|
+
expect(Band.all.none.unscoped.count).to eq(0)
|
|
163
|
+
expect(Band.all.merge(Band.none).unscoped.count).to eq(0)
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
context 'when the default scope is conditionally none' do
|
|
168
|
+
before do
|
|
169
|
+
Band.create!(name: 'Depeche Mode', active: true)
|
|
170
|
+
Band.default_scope -> { allow_all ? Band.where(active: true) : Band.none }
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
after do
|
|
174
|
+
Band.default_scoping = nil
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
context 'when the condition restricts the scope' do
|
|
178
|
+
let(:allow_all) { false }
|
|
179
|
+
|
|
180
|
+
it 'does not return any documents' do
|
|
181
|
+
expect(Band.count).to eq(0)
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
context 'when the condition allows all documents' do
|
|
186
|
+
let(:allow_all) { true }
|
|
187
|
+
|
|
188
|
+
it 'returns the documents' do
|
|
189
|
+
expect(Band.count).to eq(1)
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
context 'when multiple default scopes are combined with none last' do
|
|
195
|
+
before do
|
|
196
|
+
Band.create!(name: 'Depeche Mode', active: true)
|
|
197
|
+
Band.default_scope -> { Band.where(active: true) }
|
|
198
|
+
Band.default_scope -> { Band.none }
|
|
199
|
+
end
|
|
115
200
|
|
|
116
|
-
|
|
201
|
+
after do
|
|
202
|
+
Band.default_scoping = nil
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
it 'does not return any documents' do
|
|
206
|
+
expect(Band.count).to eq(0)
|
|
207
|
+
expect(Band.all.to_a).to be_empty
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
context 'when parent class has default scope' do
|
|
212
|
+
let(:selector) do
|
|
117
213
|
AudibleSound.all.selector
|
|
118
214
|
end
|
|
119
215
|
|
|
@@ -884,6 +980,24 @@ describe Mongoid::Scopable do
|
|
|
884
980
|
expect(Circle.visible.selector).to eq("radius" => 5)
|
|
885
981
|
end
|
|
886
982
|
end
|
|
983
|
+
|
|
984
|
+
context 'when the scope name matches an internal criteria helper' do
|
|
985
|
+
before do
|
|
986
|
+
Band.scope(:none_state, -> { where(active: true) })
|
|
987
|
+
Band.scope(:clear_default_scope_none, -> { where(origin: 'England') })
|
|
988
|
+
end
|
|
989
|
+
|
|
990
|
+
after do
|
|
991
|
+
remove_scope(Band, :none_state)
|
|
992
|
+
remove_scope(Band, :clear_default_scope_none)
|
|
993
|
+
end
|
|
994
|
+
|
|
995
|
+
it 'applies both scopes when chained on a criteria' do
|
|
996
|
+
selector = Band.where(name: 'Depeche Mode').none_state.clear_default_scope_none.selector
|
|
997
|
+
|
|
998
|
+
expect(selector).to eq('name' => 'Depeche Mode', 'active' => true, 'origin' => 'England')
|
|
999
|
+
end
|
|
1000
|
+
end
|
|
887
1001
|
end
|
|
888
1002
|
|
|
889
1003
|
describe ".scoped" do
|