mongoid 5.4.0 → 6.4.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (301) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/README.md +3 -3
  4. data/Rakefile +26 -0
  5. data/lib/config/locales/en.yml +40 -0
  6. data/lib/mongoid/atomic/modifiers.rb +2 -2
  7. data/lib/mongoid/atomic.rb +5 -5
  8. data/lib/mongoid/attributes/readonly.rb +22 -0
  9. data/lib/mongoid/attributes.rb +22 -21
  10. data/lib/mongoid/cacheable.rb +36 -0
  11. data/lib/mongoid/changeable.rb +36 -0
  12. data/lib/mongoid/clients/options.rb +55 -250
  13. data/lib/mongoid/clients/sessions.rb +113 -0
  14. data/lib/mongoid/clients/storage_options.rb +2 -69
  15. data/lib/mongoid/clients.rb +10 -63
  16. data/lib/mongoid/composable.rb +29 -2
  17. data/lib/mongoid/config.rb +1 -0
  18. data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
  19. data/lib/mongoid/contextual/atomic.rb +4 -4
  20. data/lib/mongoid/contextual/map_reduce.rb +7 -3
  21. data/lib/mongoid/contextual/memory.rb +9 -4
  22. data/lib/mongoid/contextual/mongo.rb +65 -30
  23. data/lib/mongoid/contextual/none.rb +12 -0
  24. data/lib/mongoid/copyable.rb +13 -6
  25. data/lib/mongoid/criteria/marshalable.rb +2 -2
  26. data/lib/mongoid/criteria/modifiable.rb +29 -3
  27. data/lib/mongoid/criteria/options.rb +25 -0
  28. data/lib/mongoid/criteria/queryable/aggregable.rb +120 -0
  29. data/lib/mongoid/criteria/queryable/extensions/array.rb +185 -0
  30. data/lib/mongoid/criteria/queryable/extensions/big_decimal.rb +37 -0
  31. data/lib/mongoid/criteria/queryable/extensions/boolean.rb +34 -0
  32. data/lib/mongoid/criteria/queryable/extensions/date.rb +63 -0
  33. data/lib/mongoid/criteria/queryable/extensions/date_time.rb +53 -0
  34. data/lib/mongoid/criteria/queryable/extensions/hash.rb +200 -0
  35. data/lib/mongoid/criteria/queryable/extensions/nil_class.rb +86 -0
  36. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +90 -0
  37. data/lib/mongoid/criteria/queryable/extensions/object.rb +206 -0
  38. data/lib/mongoid/criteria/queryable/extensions/range.rb +70 -0
  39. data/lib/mongoid/criteria/queryable/extensions/regexp.rb +79 -0
  40. data/lib/mongoid/criteria/queryable/extensions/set.rb +34 -0
  41. data/lib/mongoid/criteria/queryable/extensions/string.rb +137 -0
  42. data/lib/mongoid/criteria/queryable/extensions/symbol.rb +79 -0
  43. data/lib/mongoid/criteria/queryable/extensions/time.rb +60 -0
  44. data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +54 -0
  45. data/lib/mongoid/criteria/queryable/extensions.rb +28 -0
  46. data/lib/mongoid/criteria/queryable/forwardable.rb +65 -0
  47. data/lib/mongoid/criteria/queryable/key.rb +103 -0
  48. data/lib/mongoid/criteria/queryable/macroable.rb +27 -0
  49. data/lib/mongoid/criteria/queryable/mergeable.rb +273 -0
  50. data/lib/mongoid/criteria/queryable/optional.rb +429 -0
  51. data/lib/mongoid/criteria/queryable/options.rb +153 -0
  52. data/lib/mongoid/criteria/queryable/pipeline.rb +111 -0
  53. data/lib/mongoid/criteria/queryable/selectable.rb +689 -0
  54. data/lib/mongoid/criteria/queryable/selector.rb +212 -0
  55. data/lib/mongoid/criteria/queryable/smash.rb +104 -0
  56. data/lib/mongoid/criteria/queryable.rb +87 -0
  57. data/lib/mongoid/criteria.rb +6 -2
  58. data/lib/mongoid/document.rb +34 -41
  59. data/lib/mongoid/errors/ambiguous_relationship.rb +1 -1
  60. data/lib/mongoid/errors/in_memory_collation_not_supported.rb +1 -1
  61. data/lib/mongoid/errors/invalid_field.rb +2 -2
  62. data/lib/mongoid/errors/invalid_persistence_option.rb +29 -0
  63. data/lib/mongoid/errors/invalid_relation.rb +66 -0
  64. data/lib/mongoid/errors/invalid_session_use.rb +24 -0
  65. data/lib/mongoid/errors.rb +3 -0
  66. data/lib/mongoid/evolvable.rb +1 -1
  67. data/lib/mongoid/extensions/big_decimal.rb +17 -8
  68. data/lib/mongoid/extensions/date.rb +4 -1
  69. data/lib/mongoid/extensions/decimal128.rb +3 -3
  70. data/lib/mongoid/extensions/hash.rb +1 -0
  71. data/lib/mongoid/extensions/regexp.rb +1 -0
  72. data/lib/mongoid/extensions/string.rb +6 -3
  73. data/lib/mongoid/extensions/time.rb +4 -1
  74. data/lib/mongoid/extensions.rb +0 -4
  75. data/lib/mongoid/factory.rb +2 -1
  76. data/lib/mongoid/fields/validators/macro.rb +18 -0
  77. data/lib/mongoid/findable.rb +2 -2
  78. data/lib/mongoid/indexable.rb +16 -14
  79. data/lib/mongoid/interceptable.rb +9 -22
  80. data/lib/mongoid/matchable/all.rb +2 -2
  81. data/lib/mongoid/matchable/and.rb +3 -3
  82. data/lib/mongoid/matchable/default.rb +2 -2
  83. data/lib/mongoid/matchable/elem_match.rb +28 -0
  84. data/lib/mongoid/matchable/exists.rb +2 -2
  85. data/lib/mongoid/matchable/gt.rb +4 -2
  86. data/lib/mongoid/matchable/gte.rb +4 -2
  87. data/lib/mongoid/matchable/in.rb +2 -2
  88. data/lib/mongoid/matchable/lt.rb +4 -2
  89. data/lib/mongoid/matchable/lte.rb +4 -2
  90. data/lib/mongoid/matchable/ne.rb +2 -2
  91. data/lib/mongoid/matchable/nin.rb +2 -2
  92. data/lib/mongoid/matchable/nor.rb +37 -0
  93. data/lib/mongoid/matchable/or.rb +3 -3
  94. data/lib/mongoid/matchable/regexp.rb +3 -3
  95. data/lib/mongoid/matchable/size.rb +2 -2
  96. data/lib/mongoid/matchable.rb +16 -7
  97. data/lib/mongoid/persistable/creatable.rb +5 -3
  98. data/lib/mongoid/persistable/deletable.rb +5 -3
  99. data/lib/mongoid/persistable/destroyable.rb +1 -5
  100. data/lib/mongoid/persistable/settable.rb +5 -5
  101. data/lib/mongoid/persistable/updatable.rb +7 -14
  102. data/lib/mongoid/persistable/upsertable.rb +2 -1
  103. data/lib/mongoid/persistable.rb +4 -6
  104. data/lib/mongoid/persistence_context.rb +220 -0
  105. data/lib/mongoid/query_cache.rb +67 -23
  106. data/lib/mongoid/railtie.rb +17 -1
  107. data/lib/mongoid/railties/controller_runtime.rb +86 -0
  108. data/lib/mongoid/relations/accessors.rb +3 -0
  109. data/lib/mongoid/relations/auto_save.rb +12 -4
  110. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +4 -4
  111. data/lib/mongoid/relations/counter_cache.rb +15 -5
  112. data/lib/mongoid/relations/eager/base.rb +3 -3
  113. data/lib/mongoid/relations/eager/has_and_belongs_to_many.rb +2 -2
  114. data/lib/mongoid/relations/eager/has_many.rb +1 -1
  115. data/lib/mongoid/relations/eager.rb +6 -11
  116. data/lib/mongoid/relations/embedded/batchable.rb +20 -18
  117. data/lib/mongoid/relations/embedded/in.rb +13 -1
  118. data/lib/mongoid/relations/embedded/many.rb +51 -10
  119. data/lib/mongoid/relations/embedded/one.rb +14 -1
  120. data/lib/mongoid/relations/macros.rb +9 -1
  121. data/lib/mongoid/relations/many.rb +4 -0
  122. data/lib/mongoid/relations/metadata.rb +3 -3
  123. data/lib/mongoid/relations/options.rb +2 -2
  124. data/lib/mongoid/relations/proxy.rb +1 -31
  125. data/lib/mongoid/relations/referenced/in.rb +19 -10
  126. data/lib/mongoid/relations/referenced/many.rb +30 -26
  127. data/lib/mongoid/relations/referenced/many_to_many.rb +20 -13
  128. data/lib/mongoid/relations/referenced/one.rb +15 -1
  129. data/lib/mongoid/relations/synchronization.rb +12 -12
  130. data/lib/mongoid/relations/targets/enumerable.rb +24 -4
  131. data/lib/mongoid/relations/touchable.rb +7 -4
  132. data/lib/mongoid/reloadable.rb +2 -2
  133. data/lib/mongoid/scopable.rb +3 -3
  134. data/lib/mongoid/serializable.rb +1 -1
  135. data/lib/mongoid/stateful.rb +1 -0
  136. data/lib/mongoid/tasks/database.rb +3 -2
  137. data/lib/mongoid/threaded.rb +74 -0
  138. data/lib/mongoid/traversable.rb +1 -1
  139. data/lib/mongoid/validatable/uniqueness.rb +1 -2
  140. data/lib/mongoid/version.rb +1 -1
  141. data/lib/mongoid.rb +6 -6
  142. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +18 -3
  143. data/spec/app/models/agent.rb +2 -0
  144. data/spec/app/models/album.rb +5 -1
  145. data/spec/app/models/array_field.rb +7 -0
  146. data/spec/app/models/artist.rb +21 -0
  147. data/spec/app/models/band.rb +3 -0
  148. data/spec/app/models/book.rb +2 -1
  149. data/spec/app/models/delegating_patient.rb +16 -0
  150. data/spec/app/models/dokument.rb +1 -0
  151. data/spec/app/models/ordered_post.rb +5 -0
  152. data/spec/app/models/oscar.rb +1 -2
  153. data/spec/app/models/page.rb +1 -1
  154. data/spec/app/models/person.rb +3 -3
  155. data/spec/app/models/princess.rb +2 -0
  156. data/spec/app/models/record.rb +1 -0
  157. data/spec/app/models/subscription.rb +1 -0
  158. data/spec/app/models/thing.rb +1 -1
  159. data/spec/config/mongoid.yml +15 -0
  160. data/spec/integration/document_spec.rb +22 -0
  161. data/spec/mongoid/atomic/modifiers_spec.rb +3 -3
  162. data/spec/mongoid/atomic_spec.rb +5 -5
  163. data/spec/mongoid/attributes/nested_spec.rb +18 -14
  164. data/spec/mongoid/attributes/readonly_spec.rb +87 -44
  165. data/spec/mongoid/attributes_spec.rb +90 -5
  166. data/spec/mongoid/cacheable_spec.rb +112 -0
  167. data/spec/mongoid/changeable_spec.rb +58 -0
  168. data/spec/mongoid/clients/factory_spec.rb +80 -28
  169. data/spec/mongoid/clients/options_spec.rb +396 -95
  170. data/spec/mongoid/clients/sessions_spec.rb +334 -0
  171. data/spec/mongoid/clients_spec.rb +243 -101
  172. data/spec/mongoid/composable_spec.rb +7 -0
  173. data/spec/mongoid/config_spec.rb +67 -11
  174. data/spec/mongoid/contextual/atomic_spec.rb +3 -3
  175. data/spec/mongoid/contextual/geo_near_spec.rb +1 -0
  176. data/spec/mongoid/contextual/mongo_spec.rb +275 -22
  177. data/spec/mongoid/contextual/none_spec.rb +15 -0
  178. data/spec/mongoid/copyable_spec.rb +13 -4
  179. data/spec/mongoid/criteria/modifiable_spec.rb +297 -16
  180. data/spec/mongoid/criteria/options_spec.rb +29 -0
  181. data/spec/mongoid/criteria/queryable/aggregable_spec.rb +370 -0
  182. data/spec/mongoid/criteria/queryable/extensions/array_spec.rb +523 -0
  183. data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +59 -0
  184. data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +58 -0
  185. data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +213 -0
  186. data/spec/mongoid/criteria/queryable/extensions/date_spec.rb +330 -0
  187. data/spec/mongoid/criteria/queryable/extensions/date_time_spec.rb +405 -0
  188. data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +58 -0
  189. data/spec/mongoid/criteria/queryable/extensions/float_spec.rb +65 -0
  190. data/spec/mongoid/criteria/queryable/extensions/hash_spec.rb +327 -0
  191. data/spec/mongoid/criteria/queryable/extensions/integer_spec.rb +65 -0
  192. data/spec/mongoid/criteria/queryable/extensions/nil_class_spec.rb +77 -0
  193. data/spec/mongoid/criteria/queryable/extensions/object_spec.rb +108 -0
  194. data/spec/mongoid/criteria/queryable/extensions/range_spec.rb +309 -0
  195. data/spec/mongoid/{extensions/origin → criteria/queryable/extensions}/regexp_raw_spec.rb +2 -2
  196. data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +90 -0
  197. data/spec/mongoid/criteria/queryable/extensions/set_spec.rb +39 -0
  198. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +302 -0
  199. data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +167 -0
  200. data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +376 -0
  201. data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +347 -0
  202. data/spec/mongoid/criteria/queryable/forwardable_spec.rb +87 -0
  203. data/spec/mongoid/criteria/queryable/key_spec.rb +52 -0
  204. data/spec/mongoid/criteria/queryable/mergeable_spec.rb +49 -0
  205. data/spec/mongoid/criteria/queryable/optional_spec.rb +1799 -0
  206. data/spec/mongoid/criteria/queryable/options_spec.rb +360 -0
  207. data/spec/mongoid/criteria/queryable/pipeline_spec.rb +200 -0
  208. data/spec/mongoid/criteria/queryable/queryable_spec.rb +137 -0
  209. data/spec/mongoid/criteria/queryable/selectable_spec.rb +4242 -0
  210. data/spec/mongoid/criteria/queryable/selector_spec.rb +844 -0
  211. data/spec/mongoid/criteria/queryable/smash_spec.rb +30 -0
  212. data/spec/mongoid/criteria/scopable_spec.rb +81 -0
  213. data/spec/mongoid/criteria_spec.rb +156 -22
  214. data/spec/mongoid/document_spec.rb +100 -90
  215. data/spec/mongoid/errors/invalid_relation_spec.rb +37 -0
  216. data/spec/mongoid/errors/mongoid_error_spec.rb +6 -3
  217. data/spec/mongoid/extensions/big_decimal_spec.rb +321 -19
  218. data/spec/mongoid/extensions/boolean_spec.rb +14 -0
  219. data/spec/mongoid/extensions/date_spec.rb +2 -6
  220. data/spec/mongoid/extensions/date_time_spec.rb +2 -6
  221. data/spec/mongoid/extensions/decimal128_spec.rb +1 -1
  222. data/spec/mongoid/extensions/float_spec.rb +8 -1
  223. data/spec/mongoid/extensions/hash_spec.rb +15 -0
  224. data/spec/mongoid/extensions/integer_spec.rb +8 -1
  225. data/spec/mongoid/extensions/object_spec.rb +11 -0
  226. data/spec/mongoid/extensions/regexp_spec.rb +23 -0
  227. data/spec/mongoid/extensions/string_spec.rb +53 -4
  228. data/spec/mongoid/extensions/time_spec.rb +2 -6
  229. data/spec/mongoid/extensions/time_with_zone_spec.rb +2 -6
  230. data/spec/mongoid/factory_spec.rb +11 -0
  231. data/spec/mongoid/fields_spec.rb +1 -1
  232. data/spec/mongoid/findable_spec.rb +47 -2
  233. data/spec/mongoid/indexable_spec.rb +15 -3
  234. data/spec/mongoid/interceptable_spec.rb +85 -19
  235. data/spec/mongoid/matchable/all_spec.rb +4 -4
  236. data/spec/mongoid/matchable/and_spec.rb +10 -10
  237. data/spec/mongoid/matchable/default_spec.rb +12 -12
  238. data/spec/mongoid/matchable/elem_match_spec.rb +86 -0
  239. data/spec/mongoid/matchable/exists_spec.rb +5 -5
  240. data/spec/mongoid/matchable/gt_spec.rb +18 -7
  241. data/spec/mongoid/matchable/gte_spec.rb +17 -7
  242. data/spec/mongoid/matchable/in_spec.rb +5 -5
  243. data/spec/mongoid/matchable/lt_spec.rb +18 -7
  244. data/spec/mongoid/matchable/lte_spec.rb +18 -7
  245. data/spec/mongoid/matchable/ne_spec.rb +5 -5
  246. data/spec/mongoid/matchable/nin_spec.rb +5 -5
  247. data/spec/mongoid/matchable/nor_spec.rb +209 -0
  248. data/spec/mongoid/matchable/or_spec.rb +7 -7
  249. data/spec/mongoid/matchable/regexp_spec.rb +5 -5
  250. data/spec/mongoid/matchable/size_spec.rb +3 -3
  251. data/spec/mongoid/matchable_spec.rb +199 -54
  252. data/spec/mongoid/persistable/creatable_spec.rb +7 -2
  253. data/spec/mongoid/persistable/deletable_spec.rb +35 -1
  254. data/spec/mongoid/persistable/destroyable_spec.rb +25 -2
  255. data/spec/mongoid/persistable/incrementable_spec.rb +6 -6
  256. data/spec/mongoid/persistable/savable_spec.rb +34 -29
  257. data/spec/mongoid/persistable/settable_spec.rb +77 -27
  258. data/spec/mongoid/persistable/updatable_spec.rb +182 -3
  259. data/spec/mongoid/persistable_spec.rb +16 -16
  260. data/spec/mongoid/persistence_context_spec.rb +694 -0
  261. data/spec/mongoid/positional_spec.rb +1 -1
  262. data/spec/mongoid/query_cache_spec.rb +170 -12
  263. data/spec/mongoid/relations/accessors_spec.rb +1 -1
  264. data/spec/mongoid/relations/auto_save_spec.rb +39 -6
  265. data/spec/mongoid/relations/bindings/referenced/many_to_many_spec.rb +4 -4
  266. data/spec/mongoid/relations/builders_spec.rb +37 -10
  267. data/spec/mongoid/relations/counter_cache_spec.rb +64 -3
  268. data/spec/mongoid/relations/eager/has_and_belongs_to_many_spec.rb +16 -0
  269. data/spec/mongoid/relations/eager_spec.rb +40 -0
  270. data/spec/mongoid/relations/embedded/many_spec.rb +305 -59
  271. data/spec/mongoid/relations/embedded/one_spec.rb +2 -1
  272. data/spec/mongoid/relations/macros_spec.rb +415 -7
  273. data/spec/mongoid/relations/metadata_spec.rb +15 -1
  274. data/spec/mongoid/relations/proxy_spec.rb +27 -1
  275. data/spec/mongoid/relations/referenced/in_spec.rb +41 -1
  276. data/spec/mongoid/relations/referenced/many_spec.rb +35 -25
  277. data/spec/mongoid/relations/referenced/many_to_many_spec.rb +14 -26
  278. data/spec/mongoid/relations/synchronization_spec.rb +48 -2
  279. data/spec/mongoid/relations/targets/enumerable_spec.rb +108 -0
  280. data/spec/mongoid/relations/touchable_spec.rb +40 -0
  281. data/spec/mongoid/reloadable_spec.rb +51 -0
  282. data/spec/mongoid/scopable_spec.rb +13 -0
  283. data/spec/mongoid/serializable_spec.rb +0 -50
  284. data/spec/mongoid/threaded_spec.rb +68 -0
  285. data/spec/mongoid/validatable/presence_spec.rb +1 -1
  286. data/spec/mongoid/validatable/uniqueness_spec.rb +18 -9
  287. data/spec/mongoid/validatable_spec.rb +16 -0
  288. data/spec/rails/controller_extension/controller_runtime_spec.rb +110 -0
  289. data/spec/spec_helper.rb +101 -8
  290. data/spec/support/cluster_config.rb +158 -0
  291. data/spec/support/constraints.rb +101 -0
  292. data/spec/support/macros.rb +20 -0
  293. data/spec/support/session_registry.rb +50 -0
  294. data/spec/support/spec_config.rb +42 -0
  295. data.tar.gz.sig +0 -0
  296. metadata +163 -61
  297. metadata.gz.sig +0 -0
  298. data/lib/mongoid/clients/thread_options.rb +0 -19
  299. data/lib/mongoid/extensions/origin/regexp_raw.rb +0 -43
  300. data/lib/mongoid/railties/document.rb +0 -12
  301. data/spec/mongoid/railties/document_spec.rb +0 -24
@@ -277,28 +277,77 @@ describe Mongoid::Extensions::String do
277
277
  context "when the string is an integer" do
278
278
 
279
279
  it "returns true" do
280
- expect("1234").to be_numeric
280
+ expect("1234".numeric?).to eq(true)
281
281
  end
282
282
  end
283
283
 
284
284
  context "when string is a float" do
285
285
 
286
286
  it "returns true" do
287
- expect("1234.123").to be_numeric
287
+ expect("1234.123".numeric?).to eq(true)
288
288
  end
289
289
  end
290
290
 
291
291
  context "when the string is has exponents" do
292
292
 
293
293
  it "returns true" do
294
- expect("1234.123123E4").to be_numeric
294
+ expect("1234.123123E4".numeric?).to eq(true)
295
295
  end
296
296
  end
297
297
 
298
298
  context "when the string is non numeric" do
299
299
 
300
300
  it "returns false" do
301
- expect("blah").to_not be_numeric
301
+ expect("blah".numeric?).to eq(false)
302
+ end
303
+ end
304
+
305
+ context "when the string is NaN" do
306
+
307
+ it "returns true" do
308
+ expect("NaN".numeric?).to eq(true)
309
+ end
310
+ end
311
+
312
+ context "when the string is NaN and junk in front" do
313
+
314
+ it "returns false" do
315
+ expect("a\nNaN".numeric?).to eq(false)
316
+ end
317
+ end
318
+
319
+ context "when the string is NaN and whitespace at end" do
320
+
321
+ it "returns false" do
322
+ expect("NaN\n".numeric?).to eq(false)
323
+ end
324
+ end
325
+
326
+ context "when the string is Infinity" do
327
+
328
+ it "returns true" do
329
+ expect("Infinity".numeric?).to eq(true)
330
+ end
331
+ end
332
+
333
+ context "when the string contains Infinity and junk in front" do
334
+
335
+ it "returns false" do
336
+ expect("a\nInfinity".numeric?).to eq(false)
337
+ end
338
+ end
339
+
340
+ context "when the string contains Infinity and whitespace at end" do
341
+
342
+ it "returns false" do
343
+ expect("Infinity\n".numeric?).to eq(false)
344
+ end
345
+ end
346
+
347
+ context "when the string is -Infinity" do
348
+
349
+ it "returns true" do
350
+ expect("-Infinity".numeric?).to eq(true)
302
351
  end
303
352
  end
304
353
  end
@@ -204,12 +204,8 @@ describe Mongoid::Extensions::Time do
204
204
 
205
205
  context "when the string is an invalid time" do
206
206
 
207
- let(:epoch) do
208
- Time.utc(1970, 1, 1, 0, 0, 0, 0)
209
- end
210
-
211
- it "converts the time to epoch" do
212
- expect(Time.mongoize("time")).to eq(epoch)
207
+ it "converts the time to nil" do
208
+ expect(Time.mongoize("time")).to eq(nil)
213
209
  end
214
210
  end
215
211
 
@@ -204,12 +204,8 @@ describe Mongoid::Extensions::TimeWithZone do
204
204
 
205
205
  context "when the string is an invalid time" do
206
206
 
207
- let(:epoch) do
208
- Time.utc(1970, 1, 1, 0, 0, 0)
209
- end
210
-
211
- it "returns epoch" do
212
- expect(ActiveSupport::TimeWithZone.mongoize("time")).to eq(epoch)
207
+ it "returns nil" do
208
+ expect(ActiveSupport::TimeWithZone.mongoize("time")).to eq(nil)
213
209
  end
214
210
  end
215
211
 
@@ -88,6 +88,17 @@ describe Mongoid::Factory do
88
88
  expect(person.title).to eq("Sir")
89
89
  end
90
90
  end
91
+
92
+ context "when the type is a symbol" do
93
+
94
+ let(:person) do
95
+ described_class.build(Person, {:_type => "Doctor"})
96
+ end
97
+
98
+ it "instantiates the subclass" do
99
+ expect(person.class).to eq(Doctor)
100
+ end
101
+ end
91
102
  end
92
103
 
93
104
  describe ".from_db" do
@@ -1171,7 +1171,7 @@ describe Mongoid::Fields do
1171
1171
  end
1172
1172
 
1173
1173
  let(:decimal) do
1174
- BigDecimal.new("1000000.00")
1174
+ BigDecimal("1000000.00")
1175
1175
  end
1176
1176
 
1177
1177
  context "when setting to a big decimal" do
@@ -10,7 +10,7 @@ describe Mongoid::Findable do
10
10
  end
11
11
 
12
12
  it "returns the distinct values for the field" do
13
- expect(Band.distinct(:name)).to eq([ "Tool", "Photek" ])
13
+ expect(Band.distinct(:name).sort).to eq([ "Photek", "Tool" ])
14
14
  end
15
15
  end
16
16
 
@@ -476,7 +476,7 @@ describe Mongoid::Findable do
476
476
  end
477
477
  end
478
478
 
479
- Origin::Selectable.forwardables.each do |method|
479
+ Mongoid::Criteria::Queryable::Selectable.forwardables.each do |method|
480
480
 
481
481
  describe "##{method}" do
482
482
 
@@ -485,4 +485,49 @@ describe Mongoid::Findable do
485
485
  end
486
486
  end
487
487
  end
488
+
489
+ context 'when Mongoid is configured to use activesupport time zone' do
490
+
491
+ before do
492
+ Mongoid.use_utc = false
493
+ Mongoid.use_activesupport_time_zone = true
494
+ Time.zone = "Asia/Kolkata"
495
+ end
496
+
497
+ let!(:time) do
498
+ Time.zone.now.tap do |t|
499
+ User.create(last_login: t, name: 'Tom')
500
+ end
501
+ end
502
+
503
+ it 'uses activesupport time zone' do
504
+ expect(User.distinct(:last_login).first.to_s).to eql(time.in_time_zone('Asia/Kolkata').to_s)
505
+ end
506
+
507
+ it 'loads other fields accurately' do
508
+ expect(User.distinct(:name)).to match_array(['Tom'])
509
+ end
510
+ end
511
+
512
+ context 'when Mongoid is not configured to use activesupport time zone' do
513
+
514
+ before do
515
+ Mongoid.use_utc = true
516
+ Mongoid.use_activesupport_time_zone = false
517
+ end
518
+
519
+ let!(:time) do
520
+ Time.now.tap do |t|
521
+ User.create(last_login: t, name: 'Tom')
522
+ end
523
+ end
524
+
525
+ it 'uses utc' do
526
+ expect(User.distinct(:last_login).first.to_s).to eql(time.utc.to_s)
527
+ end
528
+
529
+ it 'loads other fields accurately' do
530
+ expect(User.distinct(:name)).to match_array(['Tom'])
531
+ end
532
+ end
488
533
  end
@@ -2,6 +2,10 @@ require "spec_helper"
2
2
 
3
3
  describe Mongoid::Indexable do
4
4
 
5
+ after do
6
+ Person.collection.drop
7
+ end
8
+
5
9
  describe ".included" do
6
10
 
7
11
  let(:klass) do
@@ -58,7 +62,9 @@ describe Mongoid::Indexable do
58
62
  end
59
63
 
60
64
  let(:indexes) do
61
- klass.with(database: "mongoid_optional").collection.indexes
65
+ klass.with(database: "mongoid_optional") do |klass|
66
+ klass.collection.indexes
67
+ end
62
68
  end
63
69
 
64
70
  it "creates the indexes" do
@@ -107,7 +113,9 @@ describe Mongoid::Indexable do
107
113
  end
108
114
 
109
115
  let(:indexes) do
110
- klass.with(database: "mongoid_optional").collection.indexes
116
+ klass.with(database: "mongoid_optional") do |klass|
117
+ klass.collection.indexes
118
+ end
111
119
  end
112
120
 
113
121
  it "creates the indexes" do
@@ -121,7 +129,7 @@ describe Mongoid::Indexable do
121
129
  Class.new do
122
130
  include Mongoid::Document
123
131
  store_in collection: "test_db_indexes"
124
- index({name: 1}, {collation: {locale: 'en_US', strength: 2}})
132
+ index({ name: 1 }, { collation: { locale: 'en_US', strength: 2 }})
125
133
  end
126
134
  end
127
135
 
@@ -181,6 +189,10 @@ describe Mongoid::Indexable do
181
189
  end
182
190
  end
183
191
 
192
+ after do
193
+ klass.collection.drop
194
+ end
195
+
184
196
  context "when indexing a field that is aliased" do
185
197
 
186
198
  before do
@@ -280,13 +280,18 @@ describe Mongoid::Interceptable do
280
280
  end
281
281
  end
282
282
 
283
- context "callback returns false" do
283
+ context "callback aborts the callback chain" do
284
284
 
285
285
  before do
286
- expect(artist).to receive(:before_create_stub).once.and_return(false)
286
+ Artist.before_create(:before_create_fail_stub)
287
+ expect(artist).to receive(:before_create_fail_stub).once.and_call_original
287
288
  artist.save
288
289
  end
289
290
 
291
+ after do
292
+ Artist.reset_callbacks(:create)
293
+ end
294
+
290
295
  it "does not get saved" do
291
296
  expect(artist.persisted?).to be false
292
297
  end
@@ -316,13 +321,18 @@ describe Mongoid::Interceptable do
316
321
  end
317
322
  end
318
323
 
319
- context "when callback returns false" do
324
+ context "when callback halts the callback chain" do
320
325
 
321
326
  before do
322
- expect(artist).to receive(:before_save_stub).once.and_return(false)
327
+ Artist.before_save(:before_save_fail_stub)
328
+ end
329
+
330
+ after do
331
+ Artist.reset_callbacks(:save)
323
332
  end
324
333
 
325
334
  it "the save returns false" do
335
+ expect(artist).to receive(:before_save_fail_stub).once.and_call_original
326
336
  expect(artist.save).to be false
327
337
  end
328
338
  end
@@ -343,7 +353,7 @@ describe Mongoid::Interceptable do
343
353
  context "when the callback returns true" do
344
354
 
345
355
  before do
346
- expect(artist).to receive(:before_save_stub).once.and_return(true)
356
+ expect(artist).to receive(:before_update_stub).once.and_return(true)
347
357
  end
348
358
 
349
359
  it "the save returns true" do
@@ -351,13 +361,18 @@ describe Mongoid::Interceptable do
351
361
  end
352
362
  end
353
363
 
354
- context "when the callback returns false" do
364
+ context "when the callback halts the callback chain" do
355
365
 
356
366
  before do
357
- expect(artist).to receive(:before_save_stub).once.and_return(false)
367
+ Artist.before_update(:before_update_fail_stub)
368
+ end
369
+
370
+ after do
371
+ Artist.reset_callbacks(:update)
358
372
  end
359
373
 
360
374
  it "the save returns false" do
375
+ expect(artist).to receive(:before_update_fail_stub).once.and_call_original
361
376
  expect(artist.save).to be false
362
377
  end
363
378
  end
@@ -389,13 +404,18 @@ describe Mongoid::Interceptable do
389
404
  end
390
405
  end
391
406
 
392
- context "when the callback returns false" do
407
+ context "when the callback halts the callback chain" do
393
408
 
394
409
  before do
395
- expect(artist).to receive(:before_destroy_stub).once.and_return(false)
410
+ Artist.before_destroy(:before_destroy_fail_stub)
411
+ end
412
+
413
+ after do
414
+ Artist.reset_callbacks(:destroy)
396
415
  end
397
416
 
398
417
  it "the destroy returns false" do
418
+ expect(artist).to receive(:before_destroy_fail_stub).once.and_call_original
399
419
  expect(artist.destroy).to be false
400
420
  end
401
421
  end
@@ -464,32 +484,40 @@ describe Mongoid::Interceptable do
464
484
 
465
485
  context "when the child does not have the same callback defined" do
466
486
 
467
- let(:band) do
468
- Band.new
487
+ let(:exhibition) do
488
+ Exhibition.new
469
489
  end
470
490
 
471
- let!(:record) do
472
- band.records.build
491
+ let!(:exhibitor) do
492
+ exhibition.exhibitors.build
473
493
  end
474
494
 
475
495
  context "when running the callbacks directly" do
476
496
 
477
497
  before(:all) do
478
- Band.define_model_callbacks(:rearrange)
479
- Band.after_rearrange { }
498
+ Exhibition.define_model_callbacks(:rearrange)
499
+ Exhibition.after_rearrange { }
480
500
  end
481
501
 
482
502
  after(:all) do
483
- Band.reset_callbacks(:rearrange)
503
+ Exhibition.reset_callbacks(:rearrange)
484
504
  end
485
505
 
486
506
  it "does not cascade to the child" do
487
- expect(band.run_callbacks(:rearrange)).to be true
507
+ expect(exhibition.run_callbacks(:rearrange)).to be true
488
508
  end
489
509
  end
490
510
 
491
511
  context "when the callbacks get triggered by a destroy" do
492
512
 
513
+ let(:band) do
514
+ Band.new
515
+ end
516
+
517
+ let!(:record) do
518
+ band.records.build
519
+ end
520
+
493
521
  before(:all) do
494
522
  Band.define_model_callbacks(:rearrange)
495
523
  Band.set_callback(:validation, :before) do
@@ -498,7 +526,7 @@ describe Mongoid::Interceptable do
498
526
  end
499
527
 
500
528
  after(:all) do
501
- Band.reset_callbacks(:rearrange)
529
+ begin; Band.reset_callbacks(:rearrange); rescue; end
502
530
  end
503
531
 
504
532
  let(:attributes) do
@@ -1132,6 +1160,25 @@ describe Mongoid::Interceptable do
1132
1160
  expect(band.reload.records.first.before_validation_called).to be true
1133
1161
  end
1134
1162
  end
1163
+
1164
+ context 'when the parent is updated' do
1165
+
1166
+ let(:band) do
1167
+ Band.create(name: "Moderat")
1168
+ end
1169
+
1170
+ before do
1171
+ band.update(records: [ { name: 'Black on Both Sides' }])
1172
+ end
1173
+
1174
+ it 'executes the callback' do
1175
+ expect(band.records.first.before_validation_called).to be true
1176
+ end
1177
+
1178
+ it 'persists the change' do
1179
+ expect(band.reload.records.first.before_validation_called).to be true
1180
+ end
1181
+ end
1135
1182
  end
1136
1183
 
1137
1184
  context "when the child is persisted" do
@@ -1434,6 +1481,25 @@ describe Mongoid::Interceptable do
1434
1481
  end
1435
1482
  end
1436
1483
 
1484
+ describe '#after_destroy' do
1485
+
1486
+ context 'when the parent is updated in a child after_destroy callback' do
1487
+
1488
+ let!(:person) do
1489
+ Person.create!(ordered_posts: [OrderedPost.new])
1490
+ end
1491
+
1492
+ before do
1493
+ post = OrderedPost.first
1494
+ post.destroy
1495
+ end
1496
+
1497
+ it 'updates the parent' do
1498
+ expect(person.reload.title).to eq('Minus one ordered post.')
1499
+ end
1500
+ end
1501
+ end
1502
+
1437
1503
  describe "#before_validation" do
1438
1504
 
1439
1505
  context "when the child is new" do
@@ -1544,7 +1610,7 @@ describe Mongoid::Interceptable do
1544
1610
 
1545
1611
  before(:all) do
1546
1612
  Person.before_save do |doc|
1547
- doc.mode != :prevent_save
1613
+ throw(:abort) if doc.mode == :prevent_save
1548
1614
  end
1549
1615
  end
1550
1616
 
@@ -6,25 +6,25 @@ describe Mongoid::Matchable::All do
6
6
  described_class.new(["first", "second", "third"])
7
7
  end
8
8
 
9
- describe "#matches?" do
9
+ describe "#_matches?" do
10
10
 
11
11
  context "when the attribute includes all of the values" do
12
12
 
13
13
  it "returns true" do
14
- expect(matcher.matches?("$all" => ["first", /\Asec.*\z/])).to be true
14
+ expect(matcher._matches?("$all" => ["first", /\Asec.*\z/])).to be true
15
15
  end
16
16
  end
17
17
 
18
18
  context "when the attributes doesn't include all of the values" do
19
19
 
20
20
  it "returns false" do
21
- expect(matcher.matches?("$all" => ["second", "third", "fourth"])).to be false
21
+ expect(matcher._matches?("$all" => ["second", "third", "fourth"])).to be false
22
22
  end
23
23
  end
24
24
 
25
25
  context "when the value is empty" do
26
26
  it "returns false" do
27
- expect(matcher.matches?("$all" => [])).to be false
27
+ expect(matcher._matches?("$all" => [])).to be false
28
28
  end
29
29
  end
30
30
  end
@@ -10,7 +10,7 @@ describe Mongoid::Matchable::And do
10
10
  described_class.new("value", person)
11
11
  end
12
12
 
13
- describe "#matches?" do
13
+ describe "#_matches?" do
14
14
 
15
15
  context "when provided a simple expression" do
16
16
 
@@ -19,7 +19,7 @@ describe Mongoid::Matchable::And do
19
19
  context "when the value matches" do
20
20
 
21
21
  let(:matches) do
22
- matcher.matches?([ { title: "Sir" } ])
22
+ matcher._matches?([ { title: "Sir" } ])
23
23
  end
24
24
 
25
25
  before do
@@ -34,7 +34,7 @@ describe Mongoid::Matchable::And do
34
34
  context "when the expression is a $not" do
35
35
 
36
36
  let(:matches) do
37
- matcher.matches?([ { title: {:$not => /Foobar/ } }])
37
+ matcher._matches?([ { title: {:$not => /Foobar/ } }])
38
38
  end
39
39
 
40
40
  context "when the value matches" do
@@ -59,7 +59,7 @@ describe Mongoid::Matchable::And do
59
59
  context "when the value does not match" do
60
60
 
61
61
  let(:matches) do
62
- matcher.matches?([ { title: "Sir" } ])
62
+ matcher._matches?([ { title: "Sir" } ])
63
63
  end
64
64
 
65
65
  it "returns false" do
@@ -73,7 +73,7 @@ describe Mongoid::Matchable::And do
73
73
  context "when all of the values are equal" do
74
74
 
75
75
  let(:matches) do
76
- matcher.matches?(
76
+ matcher._matches?(
77
77
  [ { title: "Sir" }, { _id: person.id } ]
78
78
  )
79
79
  end
@@ -90,7 +90,7 @@ describe Mongoid::Matchable::And do
90
90
  context "when one of the values does not match" do
91
91
 
92
92
  let(:matches) do
93
- matcher.matches?(
93
+ matcher._matches?(
94
94
  [ { title: "Sir" }, { _id: BSON::ObjectId.new } ]
95
95
  )
96
96
  end
@@ -107,7 +107,7 @@ describe Mongoid::Matchable::And do
107
107
  context "when provided no expressions" do
108
108
 
109
109
  let(:matches) do
110
- matcher.matches?([])
110
+ matcher._matches?([])
111
111
  end
112
112
 
113
113
  it "returns true" do
@@ -122,7 +122,7 @@ describe Mongoid::Matchable::And do
122
122
  context "when all of the values are equal" do
123
123
 
124
124
  let(:matches) do
125
- matcher.matches?(
125
+ matcher._matches?(
126
126
  [
127
127
  { title: { "$in" => [ "Sir", "Madam" ] } },
128
128
  { _id: person.id }
@@ -142,7 +142,7 @@ describe Mongoid::Matchable::And do
142
142
  context "when one of the values does not match" do
143
143
 
144
144
  let(:matches) do
145
- matcher.matches?(
145
+ matcher._matches?(
146
146
  [
147
147
  { title: { "$in" => [ "Prince", "Madam" ] } },
148
148
  { _id: BSON::ObjectId.new }
@@ -164,7 +164,7 @@ describe Mongoid::Matchable::And do
164
164
  context "when all the expressions match" do
165
165
 
166
166
  let(:matches) do
167
- matcher.matches?(
167
+ matcher._matches?(
168
168
  [
169
169
  { title: "Sir", age: 23 },
170
170
  { _id: person.id }
@@ -2,7 +2,7 @@ require "spec_helper"
2
2
 
3
3
  describe Mongoid::Matchable::Default do
4
4
 
5
- describe "#matches?" do
5
+ describe "#_matches?" do
6
6
 
7
7
  context "when comparing strings" do
8
8
 
@@ -13,14 +13,14 @@ describe Mongoid::Matchable::Default do
13
13
  context "when the values are equal" do
14
14
 
15
15
  it "returns true" do
16
- expect(matcher.matches?("Testing")).to be true
16
+ expect(matcher._matches?("Testing")).to be true
17
17
  end
18
18
  end
19
19
 
20
20
  context "when the values are not equal" do
21
21
 
22
22
  it "returns false" do
23
- expect(matcher.matches?("Other")).to be false
23
+ expect(matcher._matches?("Other")).to be false
24
24
  end
25
25
  end
26
26
  end
@@ -38,14 +38,14 @@ describe Mongoid::Matchable::Default do
38
38
  context "when the values are equal" do
39
39
 
40
40
  it "returns true" do
41
- expect(matcher.matches?(object_id)).to be true
41
+ expect(matcher._matches?(object_id)).to be true
42
42
  end
43
43
  end
44
44
 
45
45
  context "when the values are not equal" do
46
46
 
47
47
  it "returns false" do
48
- expect(matcher.matches?(BSON::ObjectId.new)).to be false
48
+ expect(matcher._matches?(BSON::ObjectId.new)).to be false
49
49
  end
50
50
  end
51
51
  end
@@ -61,14 +61,14 @@ describe Mongoid::Matchable::Default do
61
61
  context "when the value is a string" do
62
62
 
63
63
  it "returns true" do
64
- expect(matcher.matches?("Test1")).to be true
64
+ expect(matcher._matches?("Test1")).to be true
65
65
  end
66
66
  end
67
67
 
68
68
  context "when the value is a regexp" do
69
69
 
70
70
  it "returns true" do
71
- expect(matcher.matches?(/^Test[3-5]$/)).to be true
71
+ expect(matcher._matches?(/^Test[3-5]$/)).to be true
72
72
  end
73
73
  end
74
74
  end
@@ -76,7 +76,7 @@ describe Mongoid::Matchable::Default do
76
76
  context "when the attribute does not contain the value" do
77
77
 
78
78
  it "returns false" do
79
- expect(matcher.matches?("Test4")).to be false
79
+ expect(matcher._matches?("Test4")).to be false
80
80
  end
81
81
  end
82
82
  end
@@ -94,14 +94,14 @@ describe Mongoid::Matchable::Default do
94
94
  context "when the attribute contains the value" do
95
95
 
96
96
  it "returns true" do
97
- expect(matcher.matches?(object_id)).to be true
97
+ expect(matcher._matches?(object_id)).to be true
98
98
  end
99
99
  end
100
100
 
101
101
  context "when the attribute does not contain the value" do
102
102
 
103
103
  it "returns false" do
104
- expect(matcher.matches?(BSON::ObjectId.new)).to be false
104
+ expect(matcher._matches?(BSON::ObjectId.new)).to be false
105
105
  end
106
106
  end
107
107
  end
@@ -115,14 +115,14 @@ describe Mongoid::Matchable::Default do
115
115
  context "when the attribute contains the value" do
116
116
 
117
117
  it "returns true" do
118
- expect(matcher.matches?(["Test1", "Test2", "Test3"])).to be true
118
+ expect(matcher._matches?(["Test1", "Test2", "Test3"])).to be true
119
119
  end
120
120
  end
121
121
 
122
122
  context "when the attribute does not contain the value" do
123
123
 
124
124
  it "returns false" do
125
- expect(matcher.matches?(["Test1", "Test2"])).to be false
125
+ expect(matcher._matches?(["Test1", "Test2"])).to be false
126
126
  end
127
127
  end
128
128
  end