mongoid 9.0.1 → 9.0.2

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.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/lib/config/locales/en.yml +16 -0
  3. data/lib/mongoid/association/accessors.rb +7 -2
  4. data/lib/mongoid/association/nested/one.rb +14 -1
  5. data/lib/mongoid/association/referenced/belongs_to/binding.rb +7 -1
  6. data/lib/mongoid/association/referenced/belongs_to/buildable.rb +1 -1
  7. data/lib/mongoid/association/referenced/belongs_to.rb +15 -0
  8. data/lib/mongoid/association/referenced/has_many.rb +9 -8
  9. data/lib/mongoid/association/referenced/has_one/buildable.rb +3 -8
  10. data/lib/mongoid/association/referenced/with_polymorphic_criteria.rb +41 -0
  11. data/lib/mongoid/attributes/nested.rb +2 -1
  12. data/lib/mongoid/clients/sessions.rb +12 -15
  13. data/lib/mongoid/composable.rb +2 -0
  14. data/lib/mongoid/document.rb +2 -0
  15. data/lib/mongoid/errors/unrecognized_model_alias.rb +53 -0
  16. data/lib/mongoid/errors/unrecognized_resolver.rb +27 -0
  17. data/lib/mongoid/errors/unregistered_class.rb +47 -0
  18. data/lib/mongoid/errors.rb +3 -0
  19. data/lib/mongoid/identifiable.rb +28 -0
  20. data/lib/mongoid/model_resolver.rb +154 -0
  21. data/lib/mongoid/persistence_context.rb +2 -1
  22. data/lib/mongoid/traversable.rb +5 -0
  23. data/lib/mongoid/version.rb +1 -1
  24. data/spec/integration/associations/belongs_to_spec.rb +129 -0
  25. data/spec/integration/persistence/collection_options_spec.rb +36 -0
  26. data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +4 -0
  27. data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +1 -0
  28. data/spec/mongoid/association/referenced/belongs_to_spec.rb +58 -21
  29. data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +4 -0
  30. data/spec/mongoid/attributes/nested_spec.rb +1 -0
  31. data/spec/mongoid/clients/transactions_spec.rb +2 -2
  32. data/spec/mongoid/model_resolver_spec.rb +167 -0
  33. data/spec/mongoid/monkey_patches_spec.rb +1 -1
  34. data/spec/mongoid/persistence_context_spec.rb +17 -4
  35. data/spec/mongoid/serializable_spec.rb +16 -9
  36. metadata +14 -4
@@ -0,0 +1,167 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'support/feature_sandbox'
5
+
6
+ MONGOID_MODEL_RESOLVER_KEY__ = :__separate_instance_spec_key
7
+ Mongoid::ModelResolver.register_resolver Mongoid::ModelResolver.new, MONGOID_MODEL_RESOLVER_KEY__
8
+
9
+ def quarantine(context, &block)
10
+ state = {}
11
+
12
+ context.before(:context) do
13
+ state[:quarantine] = FeatureSandbox.start_quarantine
14
+ block&.call
15
+ end
16
+
17
+ context.after(:context) do
18
+ FeatureSandbox.end_quarantine(state[:quarantine])
19
+ end
20
+ end
21
+
22
+ describe Mongoid::ModelResolver do
23
+ shared_examples 'a resolver' do |**kwargs|
24
+ it 'includes the class name when asked for all keys of the given model' do
25
+ expect(resolver.keys_for(model_class.new)).to include(model_class.name)
26
+ end
27
+
28
+ if kwargs[:with_aliases].nil?
29
+ it 'uses the class name as the default key for the given model' do
30
+ expect(resolver.default_key_for(model_class.new)).to eq model_class.name
31
+ end
32
+ elsif kwargs[:with_aliases].is_a?(Array)
33
+ it 'uses the first alias as the default key for the given model' do
34
+ expect(resolver.default_key_for(model_class.new)).to eq kwargs[:with_aliases].first
35
+ end
36
+ else
37
+ it 'uses the alias as the default key for the given model' do
38
+ expect(resolver.default_key_for(model_class.new)).to eq kwargs[:with_aliases]
39
+ end
40
+ end
41
+
42
+ it 'returns the model class when queried with the class name' do
43
+ expect(resolver.model_for(model_class.name)).to eq model_class
44
+ end
45
+
46
+ Array(kwargs[:with_aliases]).each do |model_alias|
47
+ it "includes the alias #{model_alias.inspect} when asked for all keys of the given model" do
48
+ expect(resolver.keys_for(model_class.new)).to include(model_alias)
49
+ end
50
+
51
+ it "returns the model class when queried with #{model_alias.inspect}" do
52
+ expect(resolver.model_for(model_alias)).to eq model_class
53
+ end
54
+ end
55
+ end
56
+
57
+ context 'when using the default instance' do
58
+ let(:resolver) { described_class.instance }
59
+
60
+ context 'when an alias is not specified' do
61
+ quarantine(self) do
62
+ Object.class_eval <<-RUBY, __FILE__, __LINE__ + 1
63
+ module Mongoid; module Specs; module DefaultInstance
64
+ class Vanilla; include Mongoid::Document; end
65
+ end; end; end
66
+ RUBY
67
+ end
68
+
69
+ let(:model_class) { Mongoid::Specs::DefaultInstance::Vanilla }
70
+
71
+ it_behaves_like 'a resolver'
72
+ end
73
+
74
+ context 'when one alias is specified' do
75
+ quarantine(self) do
76
+ Object.class_eval <<-RUBY, __FILE__, __LINE__ + 1
77
+ module Mongoid; module Specs; module DefaultInstance
78
+ class Aliased
79
+ include Mongoid::Document
80
+ identify_as 'aliased'
81
+ end
82
+ end; end; end
83
+ RUBY
84
+ end
85
+
86
+ let(:model_class) { Mongoid::Specs::DefaultInstance::Aliased }
87
+
88
+ it_behaves_like 'a resolver', with_aliases: 'aliased'
89
+ end
90
+
91
+ context 'when multiple aliases are specified' do
92
+ quarantine(self) do
93
+ Object.class_eval <<-RUBY, __FILE__, __LINE__ + 1
94
+ module Mongoid; module Specs; module DefaultInstance
95
+ class AliasedMultiple
96
+ include Mongoid::Document
97
+ identify_as 'aliased', 'alias2', 'alias3'
98
+ end
99
+ end; end; end
100
+ RUBY
101
+ end
102
+
103
+ let(:model_class) { Mongoid::Specs::DefaultInstance::AliasedMultiple }
104
+
105
+ it_behaves_like 'a resolver', with_aliases: %w[ aliased alias2 alias3 ]
106
+ end
107
+ end
108
+
109
+ context 'when using a separate instance' do
110
+ let(:resolver) { described_class.resolver(MONGOID_MODEL_RESOLVER_KEY__) }
111
+
112
+ it 'does not refer to the default instance' do
113
+ expect(resolver).not_to eq described_class.instance
114
+ end
115
+
116
+ context 'when an alias is not specified' do
117
+ quarantine(self) do
118
+ Object.class_eval <<-RUBY, __FILE__, __LINE__ + 1
119
+ module Mongoid; module Specs; module SeparateInstance
120
+ class Vanilla
121
+ include Mongoid::Document
122
+ identify_as resolver: MONGOID_MODEL_RESOLVER_KEY__
123
+ end
124
+ end; end; end
125
+ RUBY
126
+ end
127
+
128
+ let(:model_class) { Mongoid::Specs::SeparateInstance::Vanilla }
129
+
130
+ it_behaves_like 'a resolver'
131
+ end
132
+
133
+ context 'when one alias is specified' do
134
+ quarantine(self) do
135
+ Object.class_eval <<-RUBY, __FILE__, __LINE__ + 1
136
+ module Mongoid; module Specs; module SeparateInstance
137
+ class Aliased
138
+ include Mongoid::Document
139
+ identify_as 'aliased', resolver: MONGOID_MODEL_RESOLVER_KEY__
140
+ end
141
+ end; end; end
142
+ RUBY
143
+ end
144
+
145
+ let(:model_class) { Mongoid::Specs::SeparateInstance::Aliased }
146
+
147
+ it_behaves_like 'a resolver', with_aliases: 'aliased'
148
+ end
149
+
150
+ context 'when multiple aliases are specified' do
151
+ quarantine(self) do
152
+ Object.class_eval <<-RUBY, __FILE__, __LINE__ + 1
153
+ module Mongoid; module Specs; module SeparateInstance
154
+ class AliasedMultiple
155
+ include Mongoid::Document
156
+ identify_as 'aliased', 'alias2', 'alias3', resolver: MONGOID_MODEL_RESOLVER_KEY__
157
+ end
158
+ end; end; end
159
+ RUBY
160
+ end
161
+
162
+ let(:model_class) { Mongoid::Specs::SeparateInstance::AliasedMultiple }
163
+
164
+ it_behaves_like 'a resolver', with_aliases: %w[ aliased alias2 alias3 ]
165
+ end
166
+ end
167
+ end
@@ -5,7 +5,7 @@ require 'spec_helper'
5
5
  # @note This test ensures that we do not inadvertently introduce new monkey patches
6
6
  # to Mongoid. Existing monkey patch methods which are marked with +Mongoid.deprecated+
7
7
  # are excluded from this test.
8
- RSpec.describe('Do not add monkey patches') do # rubocop:disable RSpec/DescribeClass
8
+ RSpec.describe('Do not add monkey patches') do
9
9
  classes = [
10
10
  Object,
11
11
  Array,
@@ -206,12 +206,25 @@ describe Mongoid::PersistenceContext do
206
206
 
207
207
  context 'when the options are valid extra options' do
208
208
 
209
- let(:options) do
210
- { collection: 'other' }
209
+ context 'collection' do
210
+
211
+ let(:options) do
212
+ { collection: 'other' }
213
+ end
214
+
215
+ it 'sets the options on the persistence context object' do
216
+ expect(persistence_context.collection_name).to eq(options[:collection].to_sym)
217
+ end
211
218
  end
212
219
 
213
- it 'sets the options on the persistence context object' do
214
- expect(persistence_context.collection_name).to eq(options[:collection].to_sym)
220
+ context 'collection_options' do
221
+ let(:options) do
222
+ { collection_options: { capped: true } }
223
+ end
224
+
225
+ it 'does not propagate to client options' do
226
+ expect(persistence_context.send(:client_options).key?(:collection_options)).to eq(false)
227
+ end
215
228
  end
216
229
  end
217
230
 
@@ -511,13 +511,15 @@ describe Mongoid::Serializable do
511
511
  end
512
512
 
513
513
  it "includes the first relation" do
514
- expect(relation_hash[0]).to include
514
+ expect(relation_hash[0]).to include(
515
515
  { "_id" => "kudamm", "street" => "Kudamm" }
516
+ )
516
517
  end
517
518
 
518
519
  it "includes the second relation" do
519
- expect(relation_hash[1]).to include
520
+ expect(relation_hash[1]).to include(
520
521
  { "_id" => "tauentzienstr", "street" => "Tauentzienstr" }
522
+ )
521
523
  end
522
524
  end
523
525
 
@@ -528,13 +530,15 @@ describe Mongoid::Serializable do
528
530
  end
529
531
 
530
532
  it "includes the first relation" do
531
- expect(relation_hash[0]).to include
533
+ expect(relation_hash[0]).to include(
532
534
  { "_id" => "kudamm", "street" => "Kudamm" }
535
+ )
533
536
  end
534
537
 
535
538
  it "includes the second relation" do
536
- expect(relation_hash[1]).to include
539
+ expect(relation_hash[1]).to include(
537
540
  { "_id" => "tauentzienstr", "street" => "Tauentzienstr" }
541
+ )
538
542
  end
539
543
  end
540
544
 
@@ -653,8 +657,9 @@ describe Mongoid::Serializable do
653
657
  end
654
658
 
655
659
  it "includes the specified relation" do
656
- expect(relation_hash).to include
657
- { "_id" => "leo-marvin", "first_name" => "Leo", "last_name" => "Marvin" }
660
+ expect(relation_hash).to include(
661
+ { "_id" => "Leo-Marvin", "first_name" => "Leo", "last_name" => "Marvin" }
662
+ )
658
663
  end
659
664
  end
660
665
 
@@ -665,8 +670,9 @@ describe Mongoid::Serializable do
665
670
  end
666
671
 
667
672
  it "includes the specified relation" do
668
- expect(relation_hash).to include
669
- { "_id" => "leo-marvin", "first_name" => "Leo", "last_name" => "Marvin" }
673
+ expect(relation_hash).to include(
674
+ { "_id" => "Leo-Marvin", "first_name" => "Leo", "last_name" => "Marvin" }
675
+ )
670
676
  end
671
677
  end
672
678
 
@@ -677,8 +683,9 @@ describe Mongoid::Serializable do
677
683
  end
678
684
 
679
685
  it "includes the specified relation sans exceptions" do
680
- expect(relation_hash).to include
686
+ expect(relation_hash).to include(
681
687
  { "first_name" => "Leo", "last_name" => "Marvin" }
688
+ )
682
689
  end
683
690
  end
684
691
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 9.0.1
4
+ version: 9.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - The MongoDB Ruby Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-29 00:00:00.000000000 Z
11
+ date: 2024-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '5.1'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '7.2'
22
+ version: '7.3'
23
23
  - - "!="
24
24
  - !ruby/object:Gem::Version
25
25
  version: 7.0.0
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '5.1'
33
33
  - - "<"
34
34
  - !ruby/object:Gem::Version
35
- version: '7.2'
35
+ version: '7.3'
36
36
  - - "!="
37
37
  - !ruby/object:Gem::Version
38
38
  version: 7.0.0
@@ -168,6 +168,7 @@ files:
168
168
  - lib/mongoid/association/referenced/has_one/eager.rb
169
169
  - lib/mongoid/association/referenced/has_one/proxy.rb
170
170
  - lib/mongoid/association/referenced/syncable.rb
171
+ - lib/mongoid/association/referenced/with_polymorphic_criteria.rb
171
172
  - lib/mongoid/association/reflections.rb
172
173
  - lib/mongoid/association/relatable.rb
173
174
  - lib/mongoid/atomic.rb
@@ -335,6 +336,9 @@ files:
335
336
  - lib/mongoid/errors/transactions_not_supported.rb
336
337
  - lib/mongoid/errors/unknown_attribute.rb
337
338
  - lib/mongoid/errors/unknown_model.rb
339
+ - lib/mongoid/errors/unrecognized_model_alias.rb
340
+ - lib/mongoid/errors/unrecognized_resolver.rb
341
+ - lib/mongoid/errors/unregistered_class.rb
338
342
  - lib/mongoid/errors/unsaved_document.rb
339
343
  - lib/mongoid/errors/unsupported_javascript.rb
340
344
  - lib/mongoid/errors/validations.rb
@@ -373,6 +377,7 @@ files:
373
377
  - lib/mongoid/fields/validators.rb
374
378
  - lib/mongoid/fields/validators/macro.rb
375
379
  - lib/mongoid/findable.rb
380
+ - lib/mongoid/identifiable.rb
376
381
  - lib/mongoid/indexable.rb
377
382
  - lib/mongoid/indexable/specification.rb
378
383
  - lib/mongoid/indexable/validators/options.rb
@@ -413,6 +418,7 @@ files:
413
418
  - lib/mongoid/matcher/regex.rb
414
419
  - lib/mongoid/matcher/size.rb
415
420
  - lib/mongoid/matcher/type.rb
421
+ - lib/mongoid/model_resolver.rb
416
422
  - lib/mongoid/persistable.rb
417
423
  - lib/mongoid/persistable/creatable.rb
418
424
  - lib/mongoid/persistable/deletable.rb
@@ -578,6 +584,7 @@ files:
578
584
  - spec/integration/matcher_operator_data/type_undefined.yml
579
585
  - spec/integration/matcher_operator_spec.rb
580
586
  - spec/integration/matcher_spec.rb
587
+ - spec/integration/persistence/collection_options_spec.rb
581
588
  - spec/integration/persistence/range_field_spec.rb
582
589
  - spec/integration/server_query_spec.rb
583
590
  - spec/integration/shardable_spec.rb
@@ -828,6 +835,7 @@ files:
828
835
  - spec/mongoid/matcher/extract_attribute_data/numeric_keys.yml
829
836
  - spec/mongoid/matcher/extract_attribute_data/traversal.yml
830
837
  - spec/mongoid/matcher/extract_attribute_spec.rb
838
+ - spec/mongoid/model_resolver_spec.rb
831
839
  - spec/mongoid/mongoizable_spec.rb
832
840
  - spec/mongoid/monkey_patches_spec.rb
833
841
  - spec/mongoid/persistable/creatable_spec.rb
@@ -1298,6 +1306,7 @@ test_files:
1298
1306
  - spec/integration/matcher_operator_data/type_undefined.yml
1299
1307
  - spec/integration/matcher_operator_spec.rb
1300
1308
  - spec/integration/matcher_spec.rb
1309
+ - spec/integration/persistence/collection_options_spec.rb
1301
1310
  - spec/integration/persistence/range_field_spec.rb
1302
1311
  - spec/integration/server_query_spec.rb
1303
1312
  - spec/integration/shardable_spec.rb
@@ -1548,6 +1557,7 @@ test_files:
1548
1557
  - spec/mongoid/matcher/extract_attribute_data/numeric_keys.yml
1549
1558
  - spec/mongoid/matcher/extract_attribute_data/traversal.yml
1550
1559
  - spec/mongoid/matcher/extract_attribute_spec.rb
1560
+ - spec/mongoid/model_resolver_spec.rb
1551
1561
  - spec/mongoid/mongoizable_spec.rb
1552
1562
  - spec/mongoid/monkey_patches_spec.rb
1553
1563
  - spec/mongoid/persistable/creatable_spec.rb