mongoid 4.0.2 → 5.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (177) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +66 -1
  3. data/README.md +14 -13
  4. data/lib/config/locales/en.yml +28 -28
  5. data/lib/mongoid.rb +28 -21
  6. data/lib/mongoid/atomic.rb +2 -4
  7. data/lib/mongoid/attributes.rb +7 -7
  8. data/lib/mongoid/attributes/processing.rb +4 -1
  9. data/lib/mongoid/attributes/readonly.rb +2 -2
  10. data/lib/mongoid/changeable.rb +4 -6
  11. data/lib/mongoid/clients.rb +142 -0
  12. data/lib/mongoid/clients/factory.rb +78 -0
  13. data/lib/mongoid/{sessions → clients}/options.rb +30 -19
  14. data/lib/mongoid/{sessions → clients}/storage_options.rb +27 -13
  15. data/lib/mongoid/{sessions → clients}/thread_options.rb +6 -3
  16. data/lib/mongoid/clients/validators.rb +2 -0
  17. data/lib/mongoid/{sessions → clients}/validators/storage.rb +5 -2
  18. data/lib/mongoid/composable.rb +3 -3
  19. data/lib/mongoid/config.rb +39 -41
  20. data/lib/mongoid/config/environment.rb +1 -1
  21. data/lib/mongoid/config/validators.rb +1 -1
  22. data/lib/mongoid/config/validators/{session.rb → client.rb} +31 -28
  23. data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
  24. data/lib/mongoid/contextual/atomic.rb +11 -11
  25. data/lib/mongoid/contextual/command.rb +9 -6
  26. data/lib/mongoid/contextual/geo_near.rb +17 -1
  27. data/lib/mongoid/contextual/map_reduce.rb +12 -11
  28. data/lib/mongoid/contextual/memory.rb +2 -5
  29. data/lib/mongoid/contextual/mongo.rb +92 -82
  30. data/lib/mongoid/contextual/none.rb +13 -0
  31. data/lib/mongoid/copyable.rb +6 -1
  32. data/lib/mongoid/criteria.rb +36 -3
  33. data/lib/mongoid/document.rb +3 -4
  34. data/lib/mongoid/errors.rb +6 -6
  35. data/lib/mongoid/errors/{mixed_session_configuration.rb → mixed_client_configuration.rb} +5 -5
  36. data/lib/mongoid/errors/no_client_config.rb +22 -0
  37. data/lib/mongoid/errors/{no_session_database.rb → no_client_database.rb} +4 -4
  38. data/lib/mongoid/errors/{no_session_hosts.rb → no_client_hosts.rb} +4 -4
  39. data/lib/mongoid/errors/{no_sessions_config.rb → no_clients_config.rb} +4 -4
  40. data/lib/mongoid/errors/no_default_client.rb +23 -0
  41. data/lib/mongoid/extensions/hash.rb +5 -1
  42. data/lib/mongoid/extensions/object.rb +3 -2
  43. data/lib/mongoid/extensions/set.rb +5 -5
  44. data/lib/mongoid/factory.rb +4 -2
  45. data/lib/mongoid/fields.rb +7 -2
  46. data/lib/mongoid/findable.rb +4 -1
  47. data/lib/mongoid/indexable.rb +15 -9
  48. data/lib/mongoid/persistable.rb +1 -2
  49. data/lib/mongoid/persistable/creatable.rb +2 -2
  50. data/lib/mongoid/persistable/deletable.rb +3 -3
  51. data/lib/mongoid/persistable/incrementable.rb +1 -1
  52. data/lib/mongoid/persistable/logical.rb +1 -1
  53. data/lib/mongoid/persistable/poppable.rb +1 -1
  54. data/lib/mongoid/persistable/pullable.rb +2 -2
  55. data/lib/mongoid/persistable/pushable.rb +2 -2
  56. data/lib/mongoid/persistable/renamable.rb +1 -1
  57. data/lib/mongoid/persistable/settable.rb +1 -1
  58. data/lib/mongoid/persistable/unsettable.rb +1 -1
  59. data/lib/mongoid/persistable/updatable.rb +2 -2
  60. data/lib/mongoid/persistable/upsertable.rb +1 -1
  61. data/lib/mongoid/query_cache.rb +98 -104
  62. data/lib/mongoid/railtie.rb +1 -21
  63. data/lib/mongoid/railties/database.rake +1 -1
  64. data/lib/mongoid/relations/builders.rb +3 -1
  65. data/lib/mongoid/relations/counter_cache.rb +1 -1
  66. data/lib/mongoid/relations/embedded/batchable.rb +3 -10
  67. data/lib/mongoid/relations/embedded/many.rb +4 -2
  68. data/lib/mongoid/relations/many.rb +1 -0
  69. data/lib/mongoid/relations/proxy.rb +6 -6
  70. data/lib/mongoid/relations/referenced/many.rb +2 -1
  71. data/lib/mongoid/relations/targets/enumerable.rb +11 -11
  72. data/lib/mongoid/relations/touchable.rb +1 -1
  73. data/lib/mongoid/reloadable.rb +2 -2
  74. data/lib/mongoid/scopable.rb +6 -17
  75. data/lib/mongoid/selectable.rb +1 -36
  76. data/lib/mongoid/serializable.rb +2 -2
  77. data/lib/mongoid/stateful.rb +0 -1
  78. data/lib/mongoid/tasks/database.rake +2 -2
  79. data/lib/mongoid/tasks/database.rb +23 -16
  80. data/lib/mongoid/threaded.rb +54 -33
  81. data/lib/mongoid/threaded/lifecycle.rb +21 -16
  82. data/lib/mongoid/traversable.rb +16 -1
  83. data/lib/mongoid/validatable.rb +1 -1
  84. data/lib/mongoid/validatable/queryable.rb +1 -1
  85. data/lib/mongoid/validatable/uniqueness.rb +3 -20
  86. data/lib/mongoid/version.rb +1 -1
  87. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +91 -57
  88. data/lib/rails/mongoid.rb +2 -2
  89. data/spec/app/models/audio.rb +1 -1
  90. data/spec/app/models/band.rb +1 -0
  91. data/spec/app/models/company.rb +5 -0
  92. data/spec/app/models/label.rb +7 -0
  93. data/spec/app/models/pub.rb +6 -0
  94. data/spec/app/models/staff.rb +7 -0
  95. data/spec/app/models/store_as_dup_test1.rb +5 -0
  96. data/spec/app/models/store_as_dup_test2.rb +5 -0
  97. data/spec/config/mongoid.yml +7 -25
  98. data/spec/mongoid/atomic/paths_spec.rb +3 -11
  99. data/spec/mongoid/attributes/nested_spec.rb +16 -16
  100. data/spec/mongoid/attributes/readonly_spec.rb +80 -18
  101. data/spec/mongoid/attributes_spec.rb +3 -3
  102. data/spec/mongoid/changeable_spec.rb +70 -0
  103. data/spec/mongoid/clients/factory_spec.rb +284 -0
  104. data/spec/mongoid/{sessions → clients}/options_spec.rb +4 -6
  105. data/spec/mongoid/clients_spec.rb +739 -0
  106. data/spec/mongoid/config/environment_spec.rb +14 -11
  107. data/spec/mongoid/config_spec.rb +33 -48
  108. data/spec/mongoid/contextual/atomic_spec.rb +1 -17
  109. data/spec/mongoid/contextual/geo_near_spec.rb +35 -0
  110. data/spec/mongoid/contextual/mongo_spec.rb +26 -83
  111. data/spec/mongoid/contextual/none_spec.rb +15 -0
  112. data/spec/mongoid/copyable_spec.rb +35 -1
  113. data/spec/mongoid/criteria/findable_spec.rb +197 -0
  114. data/spec/mongoid/criteria/modifiable_spec.rb +7 -29
  115. data/spec/mongoid/criteria_spec.rb +74 -91
  116. data/spec/mongoid/document_spec.rb +1 -1
  117. data/spec/mongoid/errors/{mixed_session_configuration_spec.rb → mixed_client_configuration_spec.rb} +1 -1
  118. data/spec/mongoid/errors/{no_session_config_spec.rb → no_client_config_spec.rb} +4 -4
  119. data/spec/mongoid/errors/{no_session_database_spec.rb → no_client_database_spec.rb} +4 -4
  120. data/spec/mongoid/errors/{no_session_hosts_spec.rb → no_client_hosts_spec.rb} +3 -3
  121. data/spec/mongoid/errors/{no_sessions_config_spec.rb → no_clients_config_spec.rb} +2 -2
  122. data/spec/mongoid/fields/localized_spec.rb +1 -0
  123. data/spec/mongoid/fields_spec.rb +1 -0
  124. data/spec/mongoid/findable_spec.rb +2 -23
  125. data/spec/mongoid/indexable_spec.rb +12 -8
  126. data/spec/mongoid/interceptable_spec.rb +15 -0
  127. data/spec/mongoid/persistable/settable_spec.rb +16 -0
  128. data/spec/mongoid/persistable/updatable_spec.rb +3 -2
  129. data/spec/mongoid/persistable_spec.rb +4 -4
  130. data/spec/mongoid/query_cache_spec.rb +13 -8
  131. data/spec/mongoid/relations/auto_save_spec.rb +1 -1
  132. data/spec/mongoid/relations/counter_cache_spec.rb +34 -0
  133. data/spec/mongoid/relations/eager/belongs_to_spec.rb +9 -0
  134. data/spec/mongoid/relations/eager/has_and_belongs_to_many_spec.rb +3 -3
  135. data/spec/mongoid/relations/embedded/many_spec.rb +123 -1
  136. data/spec/mongoid/relations/embedded/one_spec.rb +3 -3
  137. data/spec/mongoid/relations/proxy_spec.rb +28 -0
  138. data/spec/mongoid/relations/referenced/in_spec.rb +1 -1
  139. data/spec/mongoid/relations/referenced/many_spec.rb +47 -23
  140. data/spec/mongoid/relations/referenced/many_to_many_spec.rb +1 -1
  141. data/spec/mongoid/relations/referenced/one_spec.rb +1 -1
  142. data/spec/mongoid/relations/targets/enumerable_spec.rb +9 -2
  143. data/spec/mongoid/reloadable_spec.rb +6 -6
  144. data/spec/mongoid/scopable_spec.rb +41 -28
  145. data/spec/mongoid/selectable_spec.rb +6 -16
  146. data/spec/mongoid/tasks/database_rake_spec.rb +13 -13
  147. data/spec/mongoid/tasks/database_spec.rb +2 -2
  148. data/spec/mongoid/threaded_spec.rb +0 -7
  149. data/spec/mongoid/traversable_spec.rb +2 -2
  150. data/spec/mongoid/validatable/uniqueness_spec.rb +30 -1
  151. data/spec/mongoid_spec.rb +13 -15
  152. data/spec/rails/mongoid_spec.rb +13 -4
  153. data/spec/spec_helper.rb +44 -27
  154. data/spec/support/authorization.rb +12 -0
  155. data/spec/support/expectations.rb +14 -0
  156. metadata +52 -59
  157. data/lib/mongoid/contextual/find_and_modify.rb +0 -69
  158. data/lib/mongoid/contextual/text_search.rb +0 -178
  159. data/lib/mongoid/criteria/#findable.rb# +0 -141
  160. data/lib/mongoid/errors/no_default_session.rb +0 -23
  161. data/lib/mongoid/errors/no_session_config.rb +0 -22
  162. data/lib/mongoid/log_subscriber.rb +0 -55
  163. data/lib/mongoid/positional.rb +0 -71
  164. data/lib/mongoid/sessions.rb +0 -125
  165. data/lib/mongoid/sessions/factory.rb +0 -131
  166. data/lib/mongoid/sessions/mongo_uri.rb +0 -93
  167. data/lib/mongoid/sessions/validators.rb +0 -2
  168. data/lib/mongoid/support/query_counter.rb +0 -23
  169. data/spec/helpers.rb +0 -18
  170. data/spec/mongoid/#atomic_spec.rb# +0 -365
  171. data/spec/mongoid/contextual/find_and_modify_spec.rb +0 -220
  172. data/spec/mongoid/contextual/text_search_spec.rb +0 -209
  173. data/spec/mongoid/log_subscriber_spec.rb +0 -75
  174. data/spec/mongoid/positional_spec.rb +0 -222
  175. data/spec/mongoid/sessions/factory_spec.rb +0 -333
  176. data/spec/mongoid/sessions/mongo_uri_spec.rb +0 -103
  177. data/spec/mongoid/sessions_spec.rb +0 -1252
@@ -11,21 +11,24 @@ describe Mongoid::Config::Environment do
11
11
 
12
12
  context "when using rails" do
13
13
 
14
- before do
15
- module Rails
16
- class << self
17
- def env; "production"; end
14
+ context "when an environment exists" do
15
+
16
+ before do
17
+ module Rails
18
+ class << self
19
+ def env; "production"; end
20
+ end
18
21
  end
19
22
  end
20
- end
21
23
 
22
- after do
23
- RailsTemp = Rails
24
- Object.send(:remove_const, :Rails)
25
- end
24
+ after do
25
+ RailsTemp = Rails
26
+ Object.send(:remove_const, :Rails)
27
+ end
26
28
 
27
- it "returns the rails environment" do
28
- expect(described_class.env_name).to eq("production")
29
+ it "returns the rails environment" do
30
+ expect(described_class.env_name).to eq("production")
31
+ end
29
32
  end
30
33
  end
31
34
 
@@ -15,7 +15,7 @@ describe Mongoid::Config do
15
15
  described_class.connect_to(database_id, read: :primary)
16
16
  end
17
17
 
18
- context "when a default session config exists" do
18
+ context "when a default client config exists" do
19
19
 
20
20
  context "when a default database is configured" do
21
21
 
@@ -23,13 +23,13 @@ describe Mongoid::Config do
23
23
  {
24
24
  default: {
25
25
  database: database_id,
26
- hosts: [ "localhost:27017" ]
26
+ hosts: [ "127.0.0.1:27017" ]
27
27
  }
28
28
  }
29
29
  end
30
30
 
31
31
  before do
32
- described_class.sessions = config
32
+ described_class.send(:clients=, config)
33
33
  end
34
34
 
35
35
  it "returns true" do
@@ -38,10 +38,10 @@ describe Mongoid::Config do
38
38
  end
39
39
  end
40
40
 
41
- context "when no default session config exists" do
41
+ context "when no default client config exists" do
42
42
 
43
43
  before do
44
- described_class.sessions.clear
44
+ described_class.clients.clear
45
45
  end
46
46
 
47
47
  it "returns false" do
@@ -73,19 +73,19 @@ describe Mongoid::Config do
73
73
  File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
74
74
  end
75
75
 
76
- context "when existing sessions exist in the configuration" do
76
+ context "when existing clients exist in the configuration" do
77
77
 
78
- let(:session) do
79
- Moped::Session.new([ "127.0.0.1:27017" ])
78
+ let(:client) do
79
+ Mongo::Client.new([ "127.0.0.1:27017" ])
80
80
  end
81
81
 
82
82
  before do
83
- Mongoid::Threaded.sessions[:test] = session
83
+ Mongoid::Clients.clients[:test] = client
84
84
  described_class.load!(file, :test)
85
85
  end
86
86
 
87
- it "clears the previous sessions" do
88
- expect(Mongoid::Threaded.sessions[:test]).to be_nil
87
+ it "clears the previous clients" do
88
+ expect(Mongoid::Clients.clients[:test]).to be_nil
89
89
  end
90
90
  end
91
91
 
@@ -174,7 +174,7 @@ describe Mongoid::Config do
174
174
  end
175
175
  end
176
176
 
177
- context "when session configurations are provided" do
177
+ context "when client configurations are provided" do
178
178
 
179
179
  context "when a default is provided" do
180
180
 
@@ -183,7 +183,7 @@ describe Mongoid::Config do
183
183
  end
184
184
 
185
185
  let(:default) do
186
- described_class.sessions[:default]
186
+ described_class.clients[:default]
187
187
  end
188
188
 
189
189
  it "sets the default hosts" do
@@ -197,25 +197,10 @@ describe Mongoid::Config do
197
197
  end
198
198
 
199
199
  it "sets the read option" do
200
- expect(options["read"]).to eq("primary")
200
+ expect(options["read"]).to eq({ "mode" => "primary" })
201
201
  end
202
202
  end
203
203
  end
204
-
205
- context "when a secondary is provided", config: :mongohq do
206
-
207
- before do
208
- described_class.load!(file)
209
- end
210
-
211
- let(:secondary) do
212
- described_class.sessions[:mongohq_single]
213
- end
214
-
215
- it "sets the secondary host" do
216
- expect(secondary["hosts"]).to eq([ ENV["MONGOHQ_SINGLE_URL"] ])
217
- end
218
- end
219
204
  end
220
205
  end
221
206
  end
@@ -243,66 +228,66 @@ describe Mongoid::Config do
243
228
  end
244
229
  end
245
230
 
246
- describe "#sessions=" do
231
+ describe "#clients=" do
247
232
 
248
- context "when no sessions configuration exists" do
233
+ context "when no clients configuration exists" do
249
234
 
250
235
  it "raises an error" do
251
236
  expect {
252
- described_class.sessions = nil
253
- }.to raise_error(Mongoid::Errors::NoSessionsConfig)
237
+ described_class.send(:clients=, nil)
238
+ }.to raise_error(Mongoid::Errors::NoClientsConfig)
254
239
  end
255
240
  end
256
241
 
257
- context "when no default session exists" do
242
+ context "when no default client exists" do
258
243
 
259
244
  it "raises an error" do
260
245
  expect {
261
- described_class.sessions = {}
262
- }.to raise_error(Mongoid::Errors::NoDefaultSession)
246
+ described_class.send(:clients=, {})
247
+ }.to raise_error(Mongoid::Errors::NoDefaultClient)
263
248
  end
264
249
  end
265
250
 
266
- context "when a default session exists" do
251
+ context "when a default client exists" do
267
252
 
268
253
  context "when no hosts are provided" do
269
254
 
270
- let(:sessions) do
255
+ let(:clients) do
271
256
  { "default" => { database: database_id }}
272
257
  end
273
258
 
274
259
  it "raises an error" do
275
260
  expect {
276
- described_class.sessions = sessions
277
- }.to raise_error(Mongoid::Errors::NoSessionHosts)
261
+ described_class.send(:clients=, clients)
262
+ }.to raise_error(Mongoid::Errors::NoClientHosts)
278
263
  end
279
264
  end
280
265
 
281
266
  context "when no database is provided" do
282
267
 
283
- let(:sessions) do
284
- { "default" => { hosts: [ "localhost:27017" ] }}
268
+ let(:clients) do
269
+ { "default" => { hosts: [ "127.0.0.1:27017" ] }}
285
270
  end
286
271
 
287
272
  it "raises an error" do
288
273
  expect {
289
- described_class.sessions = sessions
290
- }.to raise_error(Mongoid::Errors::NoSessionDatabase)
274
+ described_class.send(:clients=, clients)
275
+ }.to raise_error(Mongoid::Errors::NoClientDatabase)
291
276
  end
292
277
  end
293
278
 
294
279
  context "when a uri and standard options are provided" do
295
280
 
296
- let(:sessions) do
281
+ let(:clients) do
297
282
  { "default" =>
298
- { hosts: [ "localhost:27017" ], uri: "mongodb://localhost:27017" }
283
+ { hosts: [ "127.0.0.1:27017" ], uri: "mongodb://127.0.0.1:27017" }
299
284
  }
300
285
  end
301
286
 
302
287
  it "raises an error" do
303
288
  expect {
304
- described_class.sessions = sessions
305
- }.to raise_error(Mongoid::Errors::MixedSessionConfiguration)
289
+ described_class.send(:clients=, clients)
290
+ }.to raise_error(Mongoid::Errors::MixedClientConfiguration)
306
291
  end
307
292
  end
308
293
  end
@@ -81,10 +81,6 @@ describe Mongoid::Contextual::Atomic do
81
81
  Band.create(likes: 60)
82
82
  end
83
83
 
84
- let!(:smiths) do
85
- Band.create
86
- end
87
-
88
84
  let(:criteria) do
89
85
  Band.all
90
86
  end
@@ -102,10 +98,6 @@ describe Mongoid::Contextual::Atomic do
102
98
  it "performs the bitwise operation on initialized fields" do
103
99
  expect(depeche_mode.reload.likes).to eq(12)
104
100
  end
105
-
106
- it "does not error on non initialized fields" do
107
- expect(smiths.reload.likes).to eq(0)
108
- end
109
101
  end
110
102
 
111
103
  context "when performing a bitwise or" do
@@ -117,10 +109,6 @@ describe Mongoid::Contextual::Atomic do
117
109
  it "performs the bitwise operation on initialized fields" do
118
110
  expect(depeche_mode.reload.likes).to eq(61)
119
111
  end
120
-
121
- it "does not error on non initialized fields" do
122
- expect(smiths.reload.likes).to eq(13)
123
- end
124
112
  end
125
113
 
126
114
  context "when chaining bitwise operations" do
@@ -132,12 +120,8 @@ describe Mongoid::Contextual::Atomic do
132
120
  it "performs the bitwise operation on initialized fields" do
133
121
  expect(depeche_mode.reload.likes).to eq(14)
134
122
  end
135
-
136
- it "does not error on non initialized fields" do
137
- expect(smiths.reload.likes).to eq(10)
138
- end
139
123
  end
140
- end if mongodb_version > "2.5"
124
+ end
141
125
 
142
126
  describe "#inc" do
143
127
 
@@ -317,6 +317,41 @@ describe Mongoid::Contextual::GeoNear do
317
317
  end
318
318
  end
319
319
 
320
+ describe "#min_distance" do
321
+
322
+ let!(:collection) do
323
+ Pub.collection
324
+ end
325
+
326
+ before do
327
+ Pub.create_indexes
328
+ end
329
+
330
+ let!(:bar_one) do
331
+ Pub.create(location: [ 52.30, 13.25 ])
332
+ end
333
+
334
+ let!(:bar_two) do
335
+ Pub.create(location: [ 52.30, 13.35 ])
336
+ end
337
+
338
+ context "when results are returned" do
339
+
340
+ let(:criteria) do
341
+ Pub.all
342
+ end
343
+
344
+ let(:geo_near) do
345
+ described_class.new(collection, criteria, [ 52, 13 ]).spherical.min_distance(0.0)
346
+ end
347
+
348
+ it "sets the min distance" do
349
+ expect(geo_near.to_a).to include(bar_one)
350
+ expect(geo_near.to_a).to include(bar_two)
351
+ end
352
+ end
353
+ end
354
+
320
355
  describe "#inspect" do
321
356
 
322
357
  let!(:collection) do
@@ -107,7 +107,7 @@ describe Mongoid::Contextual::Mongo do
107
107
  end
108
108
 
109
109
  before do
110
- expect(context.query).to receive(:count).once.and_return(1)
110
+ expect(context.view).to receive(:count).once.and_return(1)
111
111
  end
112
112
 
113
113
  it "returns the count cached value after first call" do
@@ -164,18 +164,18 @@ describe Mongoid::Contextual::Mongo do
164
164
  end
165
165
  end
166
166
 
167
- context "when provided limit true" do
167
+ context "when provided limit" do
168
168
 
169
169
  before do
170
170
  2.times { Band.create(name: "Depeche Mode") }
171
171
  end
172
172
 
173
173
  let(:context) do
174
- described_class.new(criteria.limit(2))
174
+ described_class.new(criteria)
175
175
  end
176
176
 
177
177
  let(:count) do
178
- context.count(true)
178
+ context.count(limit: 2)
179
179
  end
180
180
 
181
181
  it "returns the number of documents that match" do
@@ -497,7 +497,7 @@ describe Mongoid::Contextual::Mongo do
497
497
  end
498
498
 
499
499
  it "hits the database again" do
500
- expect(context).to receive(:query).once.and_call_original
500
+ expect(context).to receive(:view).once.and_call_original
501
501
  expect(context).to be_exists
502
502
  end
503
503
  end
@@ -520,7 +520,7 @@ describe Mongoid::Contextual::Mongo do
520
520
  end
521
521
 
522
522
  it "does not hit the database" do
523
- expect(context).to receive(:query).never
523
+ expect(context).to receive(:view).never
524
524
  expect(context).to be_exists
525
525
  end
526
526
  end
@@ -534,7 +534,7 @@ describe Mongoid::Contextual::Mongo do
534
534
  end
535
535
 
536
536
  it "does not hit the database" do
537
- expect(context).to receive(:query).never
537
+ expect(context).to receive(:view).never
538
538
  expect(context).to be_exists
539
539
  end
540
540
  end
@@ -553,11 +553,11 @@ describe Mongoid::Contextual::Mongo do
553
553
  end
554
554
 
555
555
  it "returns the criteria explain path" do
556
- expect(context.explain["cursor"]).to eq("BasicCursor")
556
+ expect(context.explain).to_not be_empty
557
557
  end
558
558
  end
559
559
 
560
- describe "#find_and_modify" do
560
+ describe "#find_one_and_update" do
561
561
 
562
562
  let!(:depeche) do
563
563
  Band.create(name: "Depeche Mode")
@@ -580,7 +580,7 @@ describe Mongoid::Contextual::Mongo do
580
580
  end
581
581
 
582
582
  let!(:result) do
583
- context.find_and_modify("$inc" => { likes: 1 })
583
+ context.find_one_and_update("$inc" => { likes: 1 })
584
584
  end
585
585
 
586
586
  it "returns the first matching document" do
@@ -603,7 +603,7 @@ describe Mongoid::Contextual::Mongo do
603
603
  end
604
604
 
605
605
  let!(:result) do
606
- context.find_and_modify("$inc" => { likes: 1 })
606
+ context.find_one_and_update("$inc" => { likes: 1 })
607
607
  end
608
608
 
609
609
  it "returns the first matching document" do
@@ -626,7 +626,7 @@ describe Mongoid::Contextual::Mongo do
626
626
  end
627
627
 
628
628
  let!(:result) do
629
- context.find_and_modify("$inc" => { likes: 1 })
629
+ context.find_one_and_update("$inc" => { likes: 1 })
630
630
  end
631
631
 
632
632
  it "returns the first matching document" do
@@ -653,7 +653,7 @@ describe Mongoid::Contextual::Mongo do
653
653
  end
654
654
 
655
655
  let!(:result) do
656
- context.find_and_modify({ "$inc" => { likes: 1 }}, new: true)
656
+ context.find_one_and_update({ "$inc" => { likes: 1 }}, return_document: :after)
657
657
  end
658
658
 
659
659
  it "returns the first matching document" do
@@ -676,7 +676,7 @@ describe Mongoid::Contextual::Mongo do
676
676
  end
677
677
 
678
678
  let!(:result) do
679
- context.find_and_modify({}, remove: true)
679
+ context.find_one_and_delete
680
680
  end
681
681
 
682
682
  it "returns the first matching document" do
@@ -702,7 +702,7 @@ describe Mongoid::Contextual::Mongo do
702
702
  end
703
703
 
704
704
  let(:result) do
705
- context.find_and_modify("$inc" => { likes: 1 })
705
+ context.find_one_and_update("$inc" => { likes: 1 })
706
706
  end
707
707
 
708
708
  it "returns nil" do
@@ -807,7 +807,7 @@ describe Mongoid::Contextual::Mongo do
807
807
  end
808
808
 
809
809
  it "returns the first document without touching the database" do
810
- expect(context).to receive(:query).never
810
+ expect(context).to receive(:view).never
811
811
  expect(context.send(method)).to eq(depeche_mode)
812
812
  end
813
813
  end
@@ -819,7 +819,7 @@ describe Mongoid::Contextual::Mongo do
819
819
  end
820
820
 
821
821
  it "returns the first document without touching the database" do
822
- expect(context).to receive(:query).never
822
+ expect(context).to receive(:view).never
823
823
  expect(context.send(method)).to eq(depeche_mode)
824
824
  end
825
825
  end
@@ -845,20 +845,16 @@ describe Mongoid::Contextual::Mongo do
845
845
  expect(context.klass).to eq(Band)
846
846
  end
847
847
 
848
- it "sets the query" do
849
- expect(context.query).to be_a(Moped::Query)
850
- end
851
-
852
- it "sets the query selector" do
853
- expect(context.query.selector).to eq({ "name" => "Depeche Mode" })
848
+ it "sets the view" do
849
+ expect(context.view).to be_a(Mongo::Collection::View)
854
850
  end
855
851
 
856
- it "sets timeout options" do
857
- expect(context.query.operation.flags).to eq([ :no_cursor_timeout ])
852
+ it "sets the view selector" do
853
+ expect(context.view.selector).to eq({ "name" => "Depeche Mode" })
858
854
  end
859
855
  end
860
856
 
861
- describe "#last" do
857
+ pending "#last" do
862
858
 
863
859
  context "when no default scope" do
864
860
 
@@ -957,7 +953,7 @@ describe Mongoid::Contextual::Mongo do
957
953
  context "when calling more than once" do
958
954
 
959
955
  before do
960
- expect(context.query).to receive(:count).once.and_return(2)
956
+ expect(context.view).to receive(:count).once.and_return(2)
961
957
  end
962
958
 
963
959
  it "returns the cached value for subsequent calls" do
@@ -969,7 +965,7 @@ describe Mongoid::Contextual::Mongo do
969
965
 
970
966
  before do
971
967
  context.entries
972
- expect(context.query).to receive(:count).once.and_return(2)
968
+ expect(context.view).to receive(:count).once.and_return(2)
973
969
  end
974
970
 
975
971
  it "returns the cached value for all calls" do
@@ -1006,7 +1002,7 @@ describe Mongoid::Contextual::Mongo do
1006
1002
  context "when calling more than once" do
1007
1003
 
1008
1004
  before do
1009
- expect(context.query).to receive(:count).once.and_return(1)
1005
+ expect(context.view).to receive(:count).once.and_return(1)
1010
1006
  end
1011
1007
 
1012
1008
  it "returns the cached value for subsequent calls" do
@@ -1018,7 +1014,7 @@ describe Mongoid::Contextual::Mongo do
1018
1014
 
1019
1015
  before do
1020
1016
  context.entries
1021
- expect(context.query).to receive(:count).once.and_return(1)
1017
+ expect(context.view).to receive(:count).once.and_return(1)
1022
1018
  end
1023
1019
 
1024
1020
  it "returns the cached value for all calls" do
@@ -1517,59 +1513,6 @@ describe Mongoid::Contextual::Mongo do
1517
1513
  end
1518
1514
  end
1519
1515
 
1520
- describe "#text_search" do
1521
-
1522
- let(:criteria) do
1523
- Word.all
1524
- end
1525
-
1526
- let(:context) do
1527
- described_class.new(criteria)
1528
- end
1529
-
1530
- before do
1531
- Word.with(database: "admin").mongo_session.command(setParameter: 1, textSearchEnabled: true)
1532
- Word.create_indexes
1533
- Word.create!(name: "phase", origin: "latin")
1534
- end
1535
-
1536
- after(:all) do
1537
- Word.remove_indexes
1538
- end
1539
-
1540
- context "when the search is projecting" do
1541
-
1542
- let(:search) do
1543
- context.text_search("phase").project(name: 1)
1544
- end
1545
-
1546
- let(:documents) do
1547
- search.entries
1548
- end
1549
-
1550
- it "limits the fields to the projection" do
1551
- expect {
1552
- documents.first.origin
1553
- }.to raise_error(ActiveModel::MissingAttributeError)
1554
- end
1555
- end
1556
-
1557
- context "when the search is not projecting" do
1558
-
1559
- let(:search) do
1560
- context.text_search("phase")
1561
- end
1562
-
1563
- let(:documents) do
1564
- search.entries
1565
- end
1566
-
1567
- it "returns all fields" do
1568
- expect(documents.first.origin).to eq("latin")
1569
- end
1570
- end
1571
- end
1572
-
1573
1516
  describe "#update" do
1574
1517
 
1575
1518
  let!(:depeche_mode) do