mongoid 7.4.3 → 7.5.0

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 (46) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/lib/config/locales/en.yml +7 -0
  4. data/lib/mongoid/association/embedded/batchable.rb +3 -20
  5. data/lib/mongoid/association/macros.rb +20 -0
  6. data/lib/mongoid/association/referenced/has_many/enumerable.rb +12 -8
  7. data/lib/mongoid/association/referenced/has_many/proxy.rb +2 -2
  8. data/lib/mongoid/atomic/paths/embedded/many.rb +0 -19
  9. data/lib/mongoid/config.rb +6 -1
  10. data/lib/mongoid/contextual/memory.rb +144 -12
  11. data/lib/mongoid/contextual/mongo.rb +118 -26
  12. data/lib/mongoid/contextual/none.rb +45 -1
  13. data/lib/mongoid/criteria/queryable/extensions/array.rb +2 -0
  14. data/lib/mongoid/criteria/queryable/extensions/hash.rb +2 -0
  15. data/lib/mongoid/criteria/queryable/mergeable.rb +21 -0
  16. data/lib/mongoid/criteria/queryable/selectable.rb +26 -10
  17. data/lib/mongoid/criteria.rb +2 -0
  18. data/lib/mongoid/document.rb +2 -0
  19. data/lib/mongoid/equality.rb +4 -4
  20. data/lib/mongoid/errors/document_not_found.rb +23 -6
  21. data/lib/mongoid/fields.rb +145 -21
  22. data/lib/mongoid/findable.rb +20 -5
  23. data/lib/mongoid/version.rb +1 -1
  24. data/lib/mongoid/warnings.rb +29 -0
  25. data/lib/mongoid.rb +1 -0
  26. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +4 -3
  27. data/spec/integration/i18n_fallbacks_spec.rb +15 -1
  28. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +0 -21
  29. data/spec/mongoid/association/embedded/embeds_many_models.rb +0 -121
  30. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +0 -8
  31. data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +54 -0
  32. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +8 -24
  33. data/spec/mongoid/clients/options_spec.rb +1 -0
  34. data/spec/mongoid/config_spec.rb +10 -4
  35. data/spec/mongoid/contextual/memory_spec.rb +826 -65
  36. data/spec/mongoid/contextual/mongo_spec.rb +781 -18
  37. data/spec/mongoid/contextual/none_spec.rb +46 -0
  38. data/spec/mongoid/criteria/queryable/selectable_spec.rb +212 -39
  39. data/spec/mongoid/criteria_spec.rb +8 -0
  40. data/spec/mongoid/equality_spec.rb +12 -12
  41. data/spec/mongoid/errors/document_not_found_spec.rb +49 -0
  42. data/spec/mongoid/findable_spec.rb +30 -0
  43. data/spec/support/models/code.rb +2 -0
  44. data.tar.gz.sig +0 -0
  45. metadata +3 -2
  46. metadata.gz.sig +0 -0
@@ -67,124 +67,3 @@ class EmmOuter
67
67
 
68
68
  field :level, :type => Integer
69
69
  end
70
-
71
- class EmmCustomerAddress
72
- include Mongoid::Document
73
-
74
- embedded_in :addressable, polymorphic: true, inverse_of: :work_address
75
- end
76
-
77
- class EmmFriend
78
- include Mongoid::Document
79
-
80
- embedded_in :befriendable, polymorphic: true
81
- end
82
-
83
- class EmmCustomer
84
- include Mongoid::Document
85
-
86
- embeds_one :home_address, class_name: 'EmmCustomerAddress', as: :addressable
87
- embeds_one :work_address, class_name: 'EmmCustomerAddress', as: :addressable
88
-
89
- embeds_many :close_friends, class_name: 'EmmFriend', as: :befriendable
90
- embeds_many :acquaintances, class_name: 'EmmFriend', as: :befriendable
91
- end
92
-
93
- class EmmUser
94
- include Mongoid::Document
95
- include Mongoid::Timestamps
96
-
97
- embeds_many :orders, class_name: 'EmmOrder'
98
- end
99
-
100
- class EmmOrder
101
- include Mongoid::Document
102
-
103
- field :amount, type: Integer
104
-
105
- embedded_in :user, class_name: 'EmmUser'
106
- end
107
-
108
- module EmmSpec
109
- # There is also a top-level Car class defined.
110
- class Car
111
- include Mongoid::Document
112
-
113
- embeds_many :doors
114
- end
115
-
116
- class Door
117
- include Mongoid::Document
118
-
119
- embedded_in :car
120
- end
121
-
122
- class Tank
123
- include Mongoid::Document
124
-
125
- embeds_many :guns
126
- embeds_many :emm_turrets
127
- # This association references a model that is not in our module,
128
- # and it does not define class_name hence Mongoid will not be able to
129
- # figure out the inverse for this association.
130
- embeds_many :emm_hatches
131
-
132
- # class_name is intentionally unqualified, references a class in the
133
- # same module. Rails permits class_name to be unqualified like this.
134
- embeds_many :launchers, class_name: 'Launcher'
135
- end
136
-
137
- class Gun
138
- include Mongoid::Document
139
-
140
- embedded_in :tank
141
- end
142
-
143
- class Launcher
144
- include Mongoid::Document
145
-
146
- # class_name is intentionally unqualified.
147
- embedded_in :tank, class_name: 'Tank'
148
- end
149
- end
150
-
151
- # This is intentionally on top level.
152
- class EmmTurret
153
- include Mongoid::Document
154
-
155
- embedded_in :tank, class_name: 'EmmSpec::Tank'
156
- end
157
-
158
- # This is intentionally on top level.
159
- class EmmHatch
160
- include Mongoid::Document
161
-
162
- # No :class_name option on this association intentionally.
163
- embedded_in :tank
164
- end
165
-
166
- class EmmPost
167
- include Mongoid::Document
168
-
169
- embeds_many :company_tags, class_name: "EmmCompanyTag"
170
- embeds_many :user_tags, class_name: "EmmUserTag"
171
- end
172
-
173
-
174
- class EmmCompanyTag
175
- include Mongoid::Document
176
-
177
- field :title, type: String
178
-
179
- embedded_in :post, class_name: "EmmPost"
180
- end
181
-
182
-
183
- class EmmUserTag
184
- include Mongoid::Document
185
-
186
- field :title, type: String
187
-
188
- embedded_in :post, class_name: "EmmPost"
189
- end
190
-
@@ -2394,10 +2394,6 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2394
2394
  it "removes the ids from the foreign key" do
2395
2395
  expect(person.preference_ids).to eq([ preference_two.id ])
2396
2396
  end
2397
-
2398
- it "sets the association locally" do
2399
- expect(person.preferences).to eq([preference_two])
2400
- end
2401
2397
  end
2402
2398
 
2403
2399
  context "when conditions are not provided" do
@@ -2424,10 +2420,6 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2424
2420
  it "returns the number of documents deleted" do
2425
2421
  expect(deleted).to eq(2)
2426
2422
  end
2427
-
2428
- it "sets the association locally" do
2429
- expect(person.preferences).to eq([])
2430
- end
2431
2423
  end
2432
2424
  end
2433
2425
  end
@@ -1264,6 +1264,33 @@ describe Mongoid::Association::Referenced::HasMany::Enumerable do
1264
1264
  end
1265
1265
  end
1266
1266
 
1267
+ context 'when including a limit' do
1268
+
1269
+ let(:person) do
1270
+ Person.create!
1271
+ end
1272
+
1273
+ let(:criteria) do
1274
+ Post.where(person_id: person.id)
1275
+ end
1276
+
1277
+ let(:enumerable) do
1278
+ described_class.new(criteria)
1279
+ end
1280
+
1281
+ let!(:first_post) do
1282
+ person.posts.create!(title: "One")
1283
+ end
1284
+
1285
+ let!(:second_post) do
1286
+ person.posts.create!(title: "Two")
1287
+ end
1288
+
1289
+ it 'returns the matching document' do
1290
+ expect(enumerable.first(1)).to eq([first_post])
1291
+ end
1292
+ end
1293
+
1267
1294
  context 'when the id_sort option is not provided' do
1268
1295
 
1269
1296
  let(:person) do
@@ -1707,6 +1734,33 @@ describe Mongoid::Association::Referenced::HasMany::Enumerable do
1707
1734
  end
1708
1735
  end
1709
1736
 
1737
+ context 'when including a limit' do
1738
+
1739
+ let(:person) do
1740
+ Person.create!
1741
+ end
1742
+
1743
+ let(:criteria) do
1744
+ Post.where(person_id: person.id)
1745
+ end
1746
+
1747
+ let(:enumerable) do
1748
+ described_class.new(criteria)
1749
+ end
1750
+
1751
+ let!(:first_post) do
1752
+ person.posts.create!(title: "One")
1753
+ end
1754
+
1755
+ let!(:second_post) do
1756
+ person.posts.create!(title: "Two")
1757
+ end
1758
+
1759
+ it 'returns the matching document' do
1760
+ expect(enumerable.last(1)).to eq([second_post])
1761
+ end
1762
+ end
1763
+
1710
1764
  context 'when the id_sort option is none' do
1711
1765
 
1712
1766
  let(:person) do
@@ -2239,8 +2239,10 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2239
2239
  Person.create!(username: 'durran')
2240
2240
  end
2241
2241
 
2242
- let!(:post1) { person.posts.create!(title: "Testing") }
2243
- let!(:post2) { person.posts.create!(title: "Test") }
2242
+ before do
2243
+ person.posts.create!(title: "Testing")
2244
+ person.posts.create!(title: "Test")
2245
+ end
2244
2246
 
2245
2247
  it "removes the correct posts" do
2246
2248
  person.posts.send(method, { title: "Testing" })
@@ -2256,11 +2258,6 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2256
2258
  it "returns the number of documents deleted" do
2257
2259
  expect(person.posts.send(method, { title: "Testing" })).to eq(1)
2258
2260
  end
2259
-
2260
- it "sets the association locally" do
2261
- person.posts.send(method, { title: "Testing" })
2262
- expect(person.posts).to eq([post2])
2263
- end
2264
2261
  end
2265
2262
 
2266
2263
  context "when conditions are not provided" do
@@ -2287,11 +2284,6 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2287
2284
  it "returns the number of documents deleted" do
2288
2285
  expect(person.posts.send(method)).to eq(2)
2289
2286
  end
2290
-
2291
- it "sets the association locally" do
2292
- person.posts.send(method)
2293
- expect(person.posts).to eq([])
2294
- end
2295
2287
  end
2296
2288
  end
2297
2289
 
@@ -2303,8 +2295,10 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2303
2295
  Movie.create!(title: "Bladerunner")
2304
2296
  end
2305
2297
 
2306
- let!(:rating1) { movie.ratings.create!(value: 1) }
2307
- let!(:rating2) { movie.ratings.create!(value: 2) }
2298
+ before do
2299
+ movie.ratings.create!(value: 1)
2300
+ movie.ratings.create!(value: 2)
2301
+ end
2308
2302
 
2309
2303
  it "removes the correct ratings" do
2310
2304
  movie.ratings.send(method, { value: 1 })
@@ -2319,11 +2313,6 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2319
2313
  it "returns the number of documents deleted" do
2320
2314
  expect(movie.ratings.send(method, { value: 1 })).to eq(1)
2321
2315
  end
2322
-
2323
- it "sets the association locally" do
2324
- movie.ratings.send(method, { value: 1 })
2325
- expect(movie.ratings).to eq([rating2])
2326
- end
2327
2316
  end
2328
2317
 
2329
2318
  context "when conditions are not provided" do
@@ -2350,11 +2339,6 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2350
2339
  it "returns the number of documents deleted" do
2351
2340
  expect(movie.ratings.send(method)).to eq(2)
2352
2341
  end
2353
-
2354
- it "sets the association locally" do
2355
- movie.ratings.send(method)
2356
- expect(movie.ratings).to eq([])
2357
- end
2358
2342
  end
2359
2343
  end
2360
2344
  end
@@ -413,6 +413,7 @@ describe Mongoid::Clients::Options, retry: 3 do
413
413
  end
414
414
 
415
415
  context 'when the options create a new cluster' do
416
+ retry_test
416
417
  # This test fails on sharded topologies in Evergreen but not locally
417
418
  require_topology :single, :replica_set
418
419
 
@@ -204,7 +204,7 @@ describe Mongoid::Config do
204
204
 
205
205
  Mongoid.configure { |config| config.load_configuration(configuration) }
206
206
 
207
- expect(Mongoid::Config.discriminator_key).to be("_type")
207
+ expect(Mongoid::Config.discriminator_key).to eq("_type")
208
208
  end
209
209
  end
210
210
 
@@ -215,11 +215,11 @@ describe Mongoid::Config do
215
215
 
216
216
  Mongoid.configure { |config| config.load_configuration(configuration) }
217
217
 
218
- expect(Mongoid::Config.discriminator_key).to be("test")
218
+ expect(Mongoid::Config.discriminator_key).to eq("test")
219
219
  end
220
220
 
221
221
  it 'is set globally' do
222
- expect(Mongoid.discriminator_key).to be("test")
222
+ expect(Mongoid.discriminator_key).to eq("test")
223
223
  end
224
224
  end
225
225
  end
@@ -260,7 +260,7 @@ describe Mongoid::Config do
260
260
  let(:conf) { CONFIG }
261
261
 
262
262
  it "it is set to its default" do
263
- expect(Mongoid.send(option)).to be(default)
263
+ expect(Mongoid.send(option)).to eq(default)
264
264
  end
265
265
  end
266
266
  end
@@ -328,6 +328,12 @@ describe Mongoid::Config do
328
328
  it_behaves_like "a config option"
329
329
  end
330
330
 
331
+ context 'when setting the overwrite_chained_operators option in the config' do
332
+ let(:option) { :overwrite_chained_operators }
333
+ let(:default) { true }
334
+
335
+ it_behaves_like "a config option"
336
+ end
331
337
 
332
338
  describe "#load!" do
333
339