mongoid 7.3.2 → 7.3.5

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 (64) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/lib/config/locales/en.yml +13 -0
  4. data/lib/mongoid/association/embedded/batchable.rb +20 -1
  5. data/lib/mongoid/association/referenced/has_many/enumerable.rb +3 -7
  6. data/lib/mongoid/association/referenced/has_many/proxy.rb +2 -2
  7. data/lib/mongoid/association/relatable.rb +2 -0
  8. data/lib/mongoid/atomic/paths/embedded/many.rb +19 -0
  9. data/lib/mongoid/atomic.rb +26 -2
  10. data/lib/mongoid/config/environment.rb +9 -1
  11. data/lib/mongoid/contextual/atomic.rb +7 -2
  12. data/lib/mongoid/contextual/none.rb +3 -0
  13. data/lib/mongoid/criteria/queryable/selectable.rb +2 -2
  14. data/lib/mongoid/criteria/queryable/storable.rb +5 -5
  15. data/lib/mongoid/document.rb +3 -2
  16. data/lib/mongoid/errors/empty_config_file.rb +26 -0
  17. data/lib/mongoid/errors/invalid_config_file.rb +26 -0
  18. data/lib/mongoid/errors.rb +2 -0
  19. data/lib/mongoid/persistable/upsertable.rb +1 -1
  20. data/lib/mongoid/persistence_context.rb +3 -1
  21. data/lib/mongoid/query_cache.rb +11 -1
  22. data/lib/mongoid/tasks/database.rb +1 -1
  23. data/lib/mongoid/touchable.rb +10 -0
  24. data/lib/mongoid/version.rb +1 -1
  25. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +7 -2
  26. data/spec/integration/associations/embeds_many_spec.rb +139 -0
  27. data/spec/integration/contextual/empty_spec.rb +142 -0
  28. data/spec/integration/stringified_symbol_field_spec.rb +2 -2
  29. data/spec/lite_spec_helper.rb +8 -1
  30. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +21 -0
  31. data/spec/mongoid/association/embedded/embeds_many_models.rb +137 -0
  32. data/spec/mongoid/association/referenced/belongs_to_query_spec.rb +20 -0
  33. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +8 -0
  34. data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +244 -92
  35. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +30 -14
  36. data/spec/mongoid/association/referenced/has_many_models.rb +17 -0
  37. data/spec/mongoid/clients/factory_spec.rb +9 -3
  38. data/spec/mongoid/clients/options_spec.rb +11 -5
  39. data/spec/mongoid/config/environment_spec.rb +86 -8
  40. data/spec/mongoid/config_spec.rb +89 -16
  41. data/spec/mongoid/contextual/atomic_spec.rb +64 -25
  42. data/spec/mongoid/contextual/geo_near_spec.rb +1 -1
  43. data/spec/mongoid/copyable_spec.rb +1 -1
  44. data/spec/mongoid/criteria_spec.rb +32 -0
  45. data/spec/mongoid/document_spec.rb +21 -1
  46. data/spec/mongoid/errors/invalid_config_file_spec.rb +32 -0
  47. data/spec/mongoid/persistable/updatable_spec.rb +2 -0
  48. data/spec/mongoid/query_cache_spec.rb +26 -2
  49. data/spec/mongoid/scopable_spec.rb +11 -0
  50. data/spec/mongoid/touchable_spec.rb +18 -0
  51. data/spec/mongoid/touchable_spec_models.rb +2 -0
  52. data/spec/shared/lib/mrss/cluster_config.rb +6 -1
  53. data/spec/shared/lib/mrss/constraints.rb +21 -4
  54. data/spec/shared/lib/mrss/event_subscriber.rb +200 -0
  55. data/spec/shared/lib/mrss/server_version_registry.rb +17 -12
  56. data/spec/shared/lib/mrss/session_registry.rb +69 -0
  57. data/spec/shared/lib/mrss/session_registry_legacy.rb +60 -0
  58. data/spec/shared/share/Dockerfile.erb +8 -7
  59. data/spec/shared/shlib/server.sh +72 -22
  60. data/spec/support/models/audible_sound.rb +3 -0
  61. data.tar.gz.sig +0 -0
  62. metadata +627 -608
  63. metadata.gz.sig +0 -0
  64. data/spec/support/session_registry.rb +0 -50
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+ # encoding: utf-8
3
+
4
+ require 'spec_helper'
5
+
6
+ describe 'Contextual classes when dealing with empty result set' do
7
+ shared_examples 'behave as expected' do
8
+ context '#exists?' do
9
+ it 'is false' do
10
+ context.exists?.should be false
11
+ end
12
+ end
13
+
14
+ context '#count' do
15
+ it 'is 0' do
16
+ context.count.should == 0
17
+ end
18
+ end
19
+
20
+ context '#length' do
21
+ it 'is 0' do
22
+ context.length.should == 0
23
+ end
24
+ end
25
+
26
+ # #estimated_count only exists for Mongo
27
+
28
+ context '#distinct' do
29
+ it 'is empty array' do
30
+ context.distinct(:foo).should == []
31
+ end
32
+ end
33
+
34
+ context '#each' do
35
+ context 'with block' do
36
+ it 'does not invoke the block' do
37
+ called = false
38
+ context.each do
39
+ called = true
40
+ end
41
+ called.should be false
42
+ end
43
+ end
44
+
45
+ context 'without block' do
46
+ it 'returns Enumerable' do
47
+ context.each.should be_a(Enumerable)
48
+ end
49
+
50
+ it 'returns empty Enumerable' do
51
+ context.each.to_a.should == []
52
+ end
53
+ end
54
+ end
55
+
56
+ context '#map' do
57
+ context 'with block' do
58
+ it 'does not invoke the block' do
59
+ called = false
60
+ context.map do
61
+ called = true
62
+ end
63
+ called.should be false
64
+ end
65
+ end
66
+
67
+ context 'without block' do
68
+ it 'returns empty array' do
69
+ skip 'MONGOID-5148'
70
+
71
+ context.map(:field).should == []
72
+ end
73
+ end
74
+ end
75
+
76
+ context '#first' do
77
+ it 'is nil' do
78
+ context.first.should be nil
79
+ end
80
+ end
81
+
82
+ context '#find_first' do
83
+ it 'is nil' do
84
+ context.find_first.should be nil
85
+ end
86
+ end
87
+
88
+ context '#one' do
89
+ it 'is nil' do
90
+ context.one.should be nil
91
+ end
92
+ end
93
+
94
+ context '#last' do
95
+ it 'is nil' do
96
+ context.last.should be nil
97
+ end
98
+ end
99
+ end
100
+
101
+ let(:context) do
102
+ context_cls.new(criteria)
103
+ end
104
+
105
+ before do
106
+ # Create an object of the same class used in the Criteria instance
107
+ # to verify we are using the Contextual classes.
108
+ Mop.create!
109
+ end
110
+
111
+ context 'Mongo' do
112
+ let(:context_cls) { Mongoid::Contextual::Mongo }
113
+
114
+ let(:criteria) do
115
+ Mop.and(Mop.where(a: 1), Mop.where(a: 2))
116
+ end
117
+
118
+ include_examples 'behave as expected'
119
+ end
120
+
121
+ context 'Memory' do
122
+ let(:context_cls) { Mongoid::Contextual::Memory }
123
+
124
+ let(:criteria) do
125
+ Mop.all.tap do |criteria|
126
+ criteria.documents = []
127
+ end
128
+ end
129
+
130
+ include_examples 'behave as expected'
131
+ end
132
+
133
+ context 'None' do
134
+ let(:context_cls) { Mongoid::Contextual::None }
135
+
136
+ let(:criteria) do
137
+ Mop.none
138
+ end
139
+
140
+ include_examples 'behave as expected'
141
+ end
142
+ end
@@ -31,8 +31,8 @@ describe "StringifiedSymbol fields" do
31
31
  end
32
32
  end
33
33
 
34
- # Using command monitoring to test that StringifiedSymbol sends a string and returns a symbol
35
- let(:client) { Order.collection.client }
34
+ # Using command monitoring to test that StringifiedSymbol sends a string and returns a symbol
35
+ let(:client) { Order.collection.client }
36
36
 
37
37
  before do
38
38
  client.subscribe(Mongo::Monitoring::COMMAND, subscriber)
@@ -19,7 +19,14 @@ autoload :Timecop, 'timecop'
19
19
 
20
20
  require 'support/spec_config'
21
21
  require 'mrss/lite_constraints'
22
- require "support/session_registry"
22
+
23
+ if Gem::Version.new(Mongo::VERSION) < Gem::Version.new('2.18.0.alpha')
24
+ require "mrss/session_registry_legacy"
25
+ else
26
+ require "mrss/session_registry"
27
+ end
28
+
29
+ Mrss.patch_mongo_for_session_registry
23
30
 
24
31
  unless SpecConfig.instance.ci?
25
32
  begin
@@ -2,6 +2,7 @@
2
2
  # encoding: utf-8
3
3
 
4
4
  require "spec_helper"
5
+ require_relative '../embeds_many_models.rb'
5
6
 
6
7
  describe Mongoid::Association::Embedded::EmbedsMany::Proxy do
7
8
 
@@ -4400,4 +4401,24 @@ describe Mongoid::Association::Embedded::EmbedsMany::Proxy do
4400
4401
  end
4401
4402
  end
4402
4403
  end
4404
+
4405
+ context "when using assign_attributes with an already populated array" do
4406
+ let(:post) { EmmPost.create! }
4407
+
4408
+ before do
4409
+ post.assign_attributes(company_tags: [{id: BSON::ObjectId.new, title: 'a'}],
4410
+ user_tags: [{id: BSON::ObjectId.new, title: 'b'}])
4411
+ post.save!
4412
+ post.reload
4413
+ post.assign_attributes(company_tags: [{id: BSON::ObjectId.new, title: 'c'}],
4414
+ user_tags: [])
4415
+ post.save!
4416
+ post.reload
4417
+ end
4418
+
4419
+ it "has the correct embedded documents" do
4420
+ expect(post.company_tags.length).to eq(1)
4421
+ expect(post.company_tags.first.title).to eq("c")
4422
+ end
4423
+ end
4403
4424
  end
@@ -52,3 +52,140 @@ class EmmProduct
52
52
 
53
53
  field :name, type: String
54
54
  end
55
+
56
+ class EmmInner
57
+ include Mongoid::Document
58
+
59
+ embeds_many :friends, :class_name => self.name, :cyclic => true
60
+ embedded_in :parent, :class_name => self.name, :cyclic => true
61
+
62
+ field :level, :type => Integer
63
+ end
64
+
65
+ class EmmOuter
66
+ include Mongoid::Document
67
+ embeds_many :inners, class_name: 'EmmInner'
68
+
69
+ field :level, :type => Integer
70
+ end
71
+
72
+ class EmmCustomerAddress
73
+ include Mongoid::Document
74
+
75
+ embedded_in :addressable, polymorphic: true, inverse_of: :work_address
76
+ end
77
+
78
+ class EmmFriend
79
+ include Mongoid::Document
80
+
81
+ embedded_in :befriendable, polymorphic: true
82
+ end
83
+
84
+ class EmmCustomer
85
+ include Mongoid::Document
86
+
87
+ embeds_one :home_address, class_name: 'EmmCustomerAddress', as: :addressable
88
+ embeds_one :work_address, class_name: 'EmmCustomerAddress', as: :addressable
89
+
90
+ embeds_many :close_friends, class_name: 'EmmFriend', as: :befriendable
91
+ embeds_many :acquaintances, class_name: 'EmmFriend', as: :befriendable
92
+ end
93
+
94
+ class EmmUser
95
+ include Mongoid::Document
96
+ include Mongoid::Timestamps
97
+
98
+ embeds_many :orders, class_name: 'EmmOrder'
99
+ end
100
+
101
+ class EmmOrder
102
+ include Mongoid::Document
103
+
104
+ field :amount, type: Integer
105
+
106
+ embedded_in :user, class_name: 'EmmUser'
107
+ end
108
+
109
+ module EmmSpec
110
+ # There is also a top-level Car class defined.
111
+ class Car
112
+ include Mongoid::Document
113
+
114
+ embeds_many :doors
115
+ end
116
+
117
+ class Door
118
+ include Mongoid::Document
119
+
120
+ embedded_in :car
121
+ end
122
+
123
+ class Tank
124
+ include Mongoid::Document
125
+
126
+ embeds_many :guns
127
+ embeds_many :emm_turrets
128
+ # This association references a model that is not in our module,
129
+ # and it does not define class_name hence Mongoid will not be able to
130
+ # figure out the inverse for this association.
131
+ embeds_many :emm_hatches
132
+
133
+ # class_name is intentionally unqualified, references a class in the
134
+ # same module. Rails permits class_name to be unqualified like this.
135
+ embeds_many :launchers, class_name: 'Launcher'
136
+ end
137
+
138
+ class Gun
139
+ include Mongoid::Document
140
+
141
+ embedded_in :tank
142
+ end
143
+
144
+ class Launcher
145
+ include Mongoid::Document
146
+
147
+ # class_name is intentionally unqualified.
148
+ embedded_in :tank, class_name: 'Tank'
149
+ end
150
+ end
151
+
152
+ # This is intentionally on top level.
153
+ class EmmTurret
154
+ include Mongoid::Document
155
+
156
+ embedded_in :tank, class_name: 'EmmSpec::Tank'
157
+ end
158
+
159
+ # This is intentionally on top level.
160
+ class EmmHatch
161
+ include Mongoid::Document
162
+
163
+ # No :class_name option on this association intentionally.
164
+ embedded_in :tank
165
+ end
166
+
167
+ class EmmPost
168
+ include Mongoid::Document
169
+
170
+ embeds_many :company_tags, class_name: "EmmCompanyTag"
171
+ embeds_many :user_tags, class_name: "EmmUserTag"
172
+ end
173
+
174
+
175
+ class EmmCompanyTag
176
+ include Mongoid::Document
177
+
178
+ field :title, type: String
179
+
180
+ embedded_in :post, class_name: "EmmPost"
181
+ end
182
+
183
+
184
+ class EmmUserTag
185
+ include Mongoid::Document
186
+
187
+ field :title, type: String
188
+
189
+ embedded_in :post, class_name: "EmmPost"
190
+ end
191
+
@@ -35,4 +35,24 @@ describe Mongoid::Association::Referenced::BelongsTo do
35
35
  expect(school.team).to eq('Bulldogs')
36
36
  end
37
37
  end
38
+
39
+ context 'when projecting with #only while having similar inverse_of candidates' do
40
+ before do
41
+ alice = HmmOwner.create!(name: 'Alice')
42
+ bob = HmmOwner.create!(name: 'Bob')
43
+
44
+ HmmPet.create!(name: 'Rex', current_owner: bob, previous_owner: alice)
45
+ end
46
+
47
+ let(:pet) { HmmPet.where(name: 'Rex').only(:name, :previous_owner_id, 'previous_owner.name').first }
48
+
49
+ it 'populates specified fields' do
50
+ expect(pet.name).to eq('Rex')
51
+ expect(pet.previous_owner.name).to eq('Alice')
52
+ end
53
+
54
+ it 'does not try to load the inverse for an association that explicitly prevents it' do
55
+ expect { pet.previous_owner.name }.not_to raise_error
56
+ end
57
+ end
38
58
  end
@@ -2395,6 +2395,10 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2395
2395
  it "removes the ids from the foreign key" do
2396
2396
  expect(person.preference_ids).to eq([ preference_two.id ])
2397
2397
  end
2398
+
2399
+ it "sets the association locally" do
2400
+ expect(person.preferences).to eq([preference_two])
2401
+ end
2398
2402
  end
2399
2403
 
2400
2404
  context "when conditions are not provided" do
@@ -2421,6 +2425,10 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2421
2425
  it "returns the number of documents deleted" do
2422
2426
  expect(deleted).to eq(2)
2423
2427
  end
2428
+
2429
+ it "sets the association locally" do
2430
+ expect(person.preferences).to eq([])
2431
+ end
2424
2432
  end
2425
2433
  end
2426
2434
  end