mongoid 9.0.12 → 9.1.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 (1006) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -8
  3. data/README.md +5 -11
  4. data/Rakefile +35 -38
  5. data/lib/config/locales/en.yml +11 -45
  6. data/lib/mongoid/association/accessors.rb +19 -26
  7. data/lib/mongoid/association/bindable.rb +43 -45
  8. data/lib/mongoid/association/builders.rb +18 -15
  9. data/lib/mongoid/association/constrainable.rb +3 -5
  10. data/lib/mongoid/association/depending.rb +38 -46
  11. data/lib/mongoid/association/eager.rb +47 -13
  12. data/lib/mongoid/association/eager_loadable.rb +152 -24
  13. data/lib/mongoid/association/embedded/batchable.rb +28 -30
  14. data/lib/mongoid/association/embedded/cyclic.rb +8 -11
  15. data/lib/mongoid/association/embedded/eager.rb +1 -6
  16. data/lib/mongoid/association/embedded/embedded_in/binding.rb +6 -9
  17. data/lib/mongoid/association/embedded/embedded_in/buildable.rb +2 -3
  18. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +2 -0
  19. data/lib/mongoid/association/embedded/embedded_in.rb +25 -22
  20. data/lib/mongoid/association/embedded/embeds_many/binding.rb +0 -2
  21. data/lib/mongoid/association/embedded/embeds_many/buildable.rb +2 -3
  22. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +1 -1
  23. data/lib/mongoid/association/embedded/embeds_many.rb +24 -21
  24. data/lib/mongoid/association/embedded/embeds_one/binding.rb +0 -2
  25. data/lib/mongoid/association/embedded/embeds_one/buildable.rb +2 -6
  26. data/lib/mongoid/association/embedded/embeds_one.rb +20 -18
  27. data/lib/mongoid/association/embedded.rb +0 -1
  28. data/lib/mongoid/association/macros.rb +3 -7
  29. data/lib/mongoid/association/many.rb +75 -3
  30. data/lib/mongoid/association/marshalable.rb +0 -3
  31. data/lib/mongoid/association/nested/many.rb +23 -47
  32. data/lib/mongoid/association/nested/nested_buildable.rb +6 -23
  33. data/lib/mongoid/association/nested/one.rb +4 -7
  34. data/lib/mongoid/association/nested.rb +1 -3
  35. data/lib/mongoid/association/one.rb +0 -3
  36. data/lib/mongoid/association/options.rb +8 -7
  37. data/lib/mongoid/association/referenced/auto_save.rb +0 -2
  38. data/lib/mongoid/association/referenced/belongs_to/binding.rb +11 -15
  39. data/lib/mongoid/association/referenced/belongs_to/buildable.rb +23 -5
  40. data/lib/mongoid/association/referenced/belongs_to/eager.rb +1 -3
  41. data/lib/mongoid/association/referenced/belongs_to.rb +48 -43
  42. data/lib/mongoid/association/referenced/counter_cache.rb +5 -10
  43. data/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb +0 -2
  44. data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +8 -4
  45. data/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb +0 -3
  46. data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +52 -47
  47. data/lib/mongoid/association/referenced/has_many/binding.rb +0 -2
  48. data/lib/mongoid/association/referenced/has_many/buildable.rb +11 -5
  49. data/lib/mongoid/association/referenced/has_many/eager.rb +0 -3
  50. data/lib/mongoid/association/referenced/has_many/enumerable.rb +107 -38
  51. data/lib/mongoid/association/referenced/has_many/proxy.rb +11 -106
  52. data/lib/mongoid/association/referenced/has_many.rb +30 -24
  53. data/lib/mongoid/association/referenced/has_one/binding.rb +0 -2
  54. data/lib/mongoid/association/referenced/has_one/buildable.rb +11 -9
  55. data/lib/mongoid/association/referenced/has_one/eager.rb +0 -3
  56. data/lib/mongoid/association/referenced/has_one.rb +26 -21
  57. data/lib/mongoid/association/referenced/syncable.rb +30 -34
  58. data/lib/mongoid/association/referenced.rb +0 -1
  59. data/lib/mongoid/association/reflections.rb +0 -3
  60. data/lib/mongoid/association/relatable.rb +69 -83
  61. data/lib/mongoid/association.rb +12 -15
  62. data/lib/mongoid/atomic/modifiers.rb +65 -25
  63. data/lib/mongoid/atomic/paths/embedded/many.rb +4 -7
  64. data/lib/mongoid/atomic/paths/embedded/one.rb +2 -4
  65. data/lib/mongoid/atomic/paths/embedded.rb +3 -6
  66. data/lib/mongoid/atomic/paths/root.rb +1 -4
  67. data/lib/mongoid/atomic/paths.rb +2 -3
  68. data/lib/mongoid/atomic.rb +0 -3
  69. data/lib/mongoid/attributes/dynamic.rb +6 -7
  70. data/lib/mongoid/attributes/nested.rb +8 -10
  71. data/lib/mongoid/attributes/projector.rb +7 -20
  72. data/lib/mongoid/attributes/readonly.rb +6 -11
  73. data/lib/mongoid/attributes.rb +18 -34
  74. data/lib/mongoid/cacheable.rb +24 -6
  75. data/lib/mongoid/clients/factory.rb +12 -15
  76. data/lib/mongoid/clients/options.rb +2 -5
  77. data/lib/mongoid/clients/sessions.rb +50 -60
  78. data/lib/mongoid/clients/storage_options.rb +1 -4
  79. data/lib/mongoid/clients/validators/storage.rb +2 -3
  80. data/lib/mongoid/clients/validators.rb +1 -2
  81. data/lib/mongoid/clients.rb +8 -11
  82. data/lib/mongoid/collection_configurable.rb +10 -22
  83. data/lib/mongoid/composable.rb +30 -34
  84. data/lib/mongoid/config/defaults.rb +7 -4
  85. data/lib/mongoid/config/encryption.rb +19 -39
  86. data/lib/mongoid/config/environment.rb +7 -16
  87. data/lib/mongoid/config/introspection.rb +14 -16
  88. data/lib/mongoid/config/options.rb +16 -10
  89. data/lib/mongoid/config/validators/async_query_executor.rb +3 -6
  90. data/lib/mongoid/config/validators/client.rb +12 -15
  91. data/lib/mongoid/config/validators/option.rb +3 -5
  92. data/lib/mongoid/config/validators.rb +3 -4
  93. data/lib/mongoid/config.rb +127 -84
  94. data/lib/mongoid/contextual/aggregable/memory.rb +4 -9
  95. data/lib/mongoid/contextual/aggregable/mongo.rb +20 -22
  96. data/lib/mongoid/contextual/aggregable/none.rb +2 -4
  97. data/lib/mongoid/contextual/aggregable.rb +5 -7
  98. data/lib/mongoid/contextual/atomic.rb +20 -24
  99. data/lib/mongoid/contextual/command.rb +0 -3
  100. data/lib/mongoid/contextual/map_reduce.rb +12 -16
  101. data/lib/mongoid/contextual/memory.rb +76 -80
  102. data/lib/mongoid/contextual/mongo/documents_loader.rb +2 -3
  103. data/lib/mongoid/contextual/mongo.rb +169 -85
  104. data/lib/mongoid/contextual/none.rb +9 -9
  105. data/lib/mongoid/contextual/queryable.rb +1 -4
  106. data/lib/mongoid/contextual.rb +18 -7
  107. data/lib/mongoid/copyable.rb +7 -9
  108. data/lib/mongoid/criteria/findable.rb +5 -7
  109. data/lib/mongoid/criteria/includable.rb +37 -8
  110. data/lib/mongoid/criteria/inspectable.rb +2 -5
  111. data/lib/mongoid/criteria/marshalable.rb +4 -8
  112. data/lib/mongoid/criteria/modifiable.rb +3 -11
  113. data/lib/mongoid/criteria/options.rb +0 -3
  114. data/lib/mongoid/criteria/permission.rb +34 -37
  115. data/lib/mongoid/criteria/queryable/aggregable.rb +9 -10
  116. data/lib/mongoid/criteria/queryable/expandable.rb +4 -9
  117. data/lib/mongoid/criteria/queryable/extensions/array.rb +7 -10
  118. data/lib/mongoid/criteria/queryable/extensions/big_decimal.rb +3 -5
  119. data/lib/mongoid/criteria/queryable/extensions/boolean.rb +1 -4
  120. data/lib/mongoid/criteria/queryable/extensions/date.rb +2 -6
  121. data/lib/mongoid/criteria/queryable/extensions/date_time.rb +3 -7
  122. data/lib/mongoid/criteria/queryable/extensions/hash.rb +1 -4
  123. data/lib/mongoid/criteria/queryable/extensions/nil_class.rb +20 -11
  124. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +15 -11
  125. data/lib/mongoid/criteria/queryable/extensions/object.rb +7 -8
  126. data/lib/mongoid/criteria/queryable/extensions/range.rb +6 -9
  127. data/lib/mongoid/criteria/queryable/extensions/regexp.rb +10 -12
  128. data/lib/mongoid/criteria/queryable/extensions/set.rb +4 -6
  129. data/lib/mongoid/criteria/queryable/extensions/string.rb +5 -9
  130. data/lib/mongoid/criteria/queryable/extensions/symbol.rb +3 -7
  131. data/lib/mongoid/criteria/queryable/extensions/time.rb +2 -6
  132. data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +2 -12
  133. data/lib/mongoid/criteria/queryable/extensions.rb +18 -21
  134. data/lib/mongoid/criteria/queryable/key.rb +12 -18
  135. data/lib/mongoid/criteria/queryable/macroable.rb +0 -3
  136. data/lib/mongoid/criteria/queryable/mergeable.rb +31 -43
  137. data/lib/mongoid/criteria/queryable/optional.rb +17 -18
  138. data/lib/mongoid/criteria/queryable/options.rb +20 -9
  139. data/lib/mongoid/criteria/queryable/pipeline.rb +6 -9
  140. data/lib/mongoid/criteria/queryable/selectable.rb +130 -301
  141. data/lib/mongoid/criteria/queryable/selector.rb +20 -29
  142. data/lib/mongoid/criteria/queryable/smash.rb +1 -6
  143. data/lib/mongoid/criteria/queryable/storable.rb +20 -39
  144. data/lib/mongoid/criteria/queryable.rb +25 -15
  145. data/lib/mongoid/criteria/scopable.rb +5 -11
  146. data/lib/mongoid/criteria/translator.rb +5 -8
  147. data/lib/mongoid/criteria.rb +109 -54
  148. data/lib/mongoid/deprecable.rb +1 -4
  149. data/lib/mongoid/deprecation.rb +2 -6
  150. data/lib/mongoid/document.rb +1 -1
  151. data/lib/mongoid/encryptable.rb +1 -48
  152. data/lib/mongoid/equality.rb +4 -5
  153. data/lib/mongoid/errors/ambiguous_relationship.rb +2 -5
  154. data/lib/mongoid/errors/attribute_not_loaded.rb +1 -4
  155. data/lib/mongoid/errors/callback.rb +1 -4
  156. data/lib/mongoid/errors/create_collection_failure.rb +8 -11
  157. data/lib/mongoid/errors/criteria_argument_required.rb +2 -5
  158. data/lib/mongoid/errors/delete_restriction.rb +1 -4
  159. data/lib/mongoid/errors/document_not_destroyed.rb +1 -4
  160. data/lib/mongoid/errors/document_not_found.rb +16 -21
  161. data/lib/mongoid/errors/drop_collection_failure.rb +7 -10
  162. data/lib/mongoid/errors/empty_config_file.rb +1 -4
  163. data/lib/mongoid/errors/immutable_attribute.rb +1 -4
  164. data/lib/mongoid/errors/in_memory_collation_not_supported.rb +1 -4
  165. data/lib/mongoid/errors/invalid_async_query_executor.rb +2 -5
  166. data/lib/mongoid/errors/invalid_auto_encryption_configuration.rb +2 -5
  167. data/lib/mongoid/errors/invalid_collection.rb +1 -3
  168. data/lib/mongoid/errors/invalid_config_file.rb +1 -4
  169. data/lib/mongoid/errors/invalid_config_option.rb +2 -5
  170. data/lib/mongoid/errors/invalid_dependent_strategy.rb +6 -10
  171. data/lib/mongoid/errors/invalid_discriminator_key_target.rb +2 -4
  172. data/lib/mongoid/errors/invalid_dot_dollar_assignment.rb +1 -4
  173. data/lib/mongoid/errors/invalid_elem_match_operator.rb +5 -9
  174. data/lib/mongoid/errors/invalid_estimated_count_criteria.rb +1 -4
  175. data/lib/mongoid/errors/invalid_estimated_count_scoping.rb +1 -4
  176. data/lib/mongoid/errors/invalid_expression_operator.rb +3 -7
  177. data/lib/mongoid/errors/invalid_field.rb +2 -5
  178. data/lib/mongoid/errors/invalid_field_operator.rb +5 -9
  179. data/lib/mongoid/errors/invalid_field_option.rb +2 -5
  180. data/lib/mongoid/errors/invalid_field_type.rb +1 -4
  181. data/lib/mongoid/errors/invalid_find.rb +1 -4
  182. data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +1 -4
  183. data/lib/mongoid/errors/invalid_includes.rb +3 -6
  184. data/lib/mongoid/errors/invalid_index.rb +1 -4
  185. data/lib/mongoid/errors/invalid_options.rb +1 -4
  186. data/lib/mongoid/errors/invalid_path.rb +1 -4
  187. data/lib/mongoid/errors/invalid_persistence_option.rb +4 -7
  188. data/lib/mongoid/errors/invalid_query.rb +6 -11
  189. data/lib/mongoid/errors/invalid_relation.rb +10 -13
  190. data/lib/mongoid/errors/invalid_relation_option.rb +4 -7
  191. data/lib/mongoid/errors/invalid_scope.rb +1 -4
  192. data/lib/mongoid/errors/invalid_session_nesting.rb +0 -3
  193. data/lib/mongoid/errors/invalid_set_polymorphic_relation.rb +2 -4
  194. data/lib/mongoid/errors/invalid_storage_options.rb +1 -4
  195. data/lib/mongoid/errors/invalid_time.rb +1 -4
  196. data/lib/mongoid/errors/invalid_transaction_nesting.rb +0 -3
  197. data/lib/mongoid/errors/inverse_not_found.rb +1 -4
  198. data/lib/mongoid/errors/mixed_client_configuration.rb +1 -4
  199. data/lib/mongoid/errors/mixed_relations.rb +1 -3
  200. data/lib/mongoid/errors/mongoid_error.rb +7 -11
  201. data/lib/mongoid/errors/nested_attributes_metadata_not_found.rb +1 -4
  202. data/lib/mongoid/errors/no_client_config.rb +1 -4
  203. data/lib/mongoid/errors/no_client_database.rb +1 -4
  204. data/lib/mongoid/errors/no_client_hosts.rb +1 -4
  205. data/lib/mongoid/errors/no_clients_config.rb +1 -4
  206. data/lib/mongoid/errors/no_default_client.rb +1 -4
  207. data/lib/mongoid/errors/no_environment.rb +1 -4
  208. data/lib/mongoid/errors/no_map_reduce_output.rb +1 -4
  209. data/lib/mongoid/errors/no_metadata.rb +1 -4
  210. data/lib/mongoid/errors/no_parent.rb +1 -4
  211. data/lib/mongoid/errors/readonly_attribute.rb +1 -4
  212. data/lib/mongoid/errors/readonly_document.rb +1 -4
  213. data/lib/mongoid/errors/rollback.rb +0 -2
  214. data/lib/mongoid/errors/scope_overwrite.rb +2 -4
  215. data/lib/mongoid/errors/sessions_not_supported.rb +0 -3
  216. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +1 -3
  217. data/lib/mongoid/errors/transaction_error.rb +0 -3
  218. data/lib/mongoid/errors/transactions_not_supported.rb +0 -3
  219. data/lib/mongoid/errors/unknown_attribute.rb +1 -4
  220. data/lib/mongoid/errors/unknown_model.rb +1 -4
  221. data/lib/mongoid/errors/unregistered_class.rb +1 -1
  222. data/lib/mongoid/errors/unsaved_document.rb +1 -3
  223. data/lib/mongoid/errors/unsupported_isolation_level.rb +22 -0
  224. data/lib/mongoid/errors/unsupported_javascript.rb +1 -4
  225. data/lib/mongoid/errors/validations.rb +3 -5
  226. data/lib/mongoid/errors.rb +72 -74
  227. data/lib/mongoid/evolvable.rb +0 -3
  228. data/lib/mongoid/extensions/array.rb +5 -7
  229. data/lib/mongoid/extensions/big_decimal.rb +6 -7
  230. data/lib/mongoid/extensions/binary.rb +3 -6
  231. data/lib/mongoid/extensions/boolean.rb +4 -7
  232. data/lib/mongoid/extensions/bson_document.rb +28 -0
  233. data/lib/mongoid/extensions/date.rb +10 -14
  234. data/lib/mongoid/extensions/date_time.rb +2 -6
  235. data/lib/mongoid/extensions/decimal128.rb +1 -5
  236. data/lib/mongoid/extensions/false_class.rb +3 -5
  237. data/lib/mongoid/extensions/float.rb +5 -10
  238. data/lib/mongoid/extensions/hash.rb +5 -10
  239. data/lib/mongoid/extensions/integer.rb +5 -10
  240. data/lib/mongoid/extensions/module.rb +1 -4
  241. data/lib/mongoid/extensions/nil_class.rb +0 -1
  242. data/lib/mongoid/extensions/object.rb +9 -14
  243. data/lib/mongoid/extensions/object_id.rb +2 -6
  244. data/lib/mongoid/extensions/range.rb +14 -15
  245. data/lib/mongoid/extensions/raw_value.rb +0 -3
  246. data/lib/mongoid/extensions/regexp.rb +3 -6
  247. data/lib/mongoid/extensions/set.rb +3 -4
  248. data/lib/mongoid/extensions/string.rb +8 -12
  249. data/lib/mongoid/extensions/symbol.rb +3 -7
  250. data/lib/mongoid/extensions/time.rb +24 -26
  251. data/lib/mongoid/extensions/time_with_zone.rb +4 -7
  252. data/lib/mongoid/extensions/true_class.rb +3 -5
  253. data/lib/mongoid/extensions.rb +26 -26
  254. data/lib/mongoid/factory.rb +5 -9
  255. data/lib/mongoid/fields/encrypted.rb +0 -1
  256. data/lib/mongoid/fields/foreign_key.rb +9 -10
  257. data/lib/mongoid/fields/localized.rb +12 -12
  258. data/lib/mongoid/fields/standard.rb +5 -7
  259. data/lib/mongoid/fields/validators/macro.rb +23 -34
  260. data/lib/mongoid/fields/validators.rb +1 -2
  261. data/lib/mongoid/fields.rb +151 -58
  262. data/lib/mongoid/findable.rb +82 -53
  263. data/lib/mongoid/indexable/specification.rb +20 -6
  264. data/lib/mongoid/indexable/validators/options.rb +33 -40
  265. data/lib/mongoid/indexable.rb +41 -52
  266. data/lib/mongoid/inspectable.rb +4 -7
  267. data/lib/mongoid/interceptable.rb +41 -40
  268. data/lib/mongoid/loadable.rb +17 -18
  269. data/lib/mongoid/loggable.rb +4 -8
  270. data/lib/mongoid/matchable.rb +1 -8
  271. data/lib/mongoid/matcher/all.rb +2 -5
  272. data/lib/mongoid/matcher/and.rb +0 -3
  273. data/lib/mongoid/matcher/bits.rb +13 -8
  274. data/lib/mongoid/matcher/bits_all_clear.rb +1 -3
  275. data/lib/mongoid/matcher/bits_all_set.rb +1 -3
  276. data/lib/mongoid/matcher/bits_any_clear.rb +1 -3
  277. data/lib/mongoid/matcher/bits_any_set.rb +1 -3
  278. data/lib/mongoid/matcher/elem_match.rb +13 -15
  279. data/lib/mongoid/matcher/elem_match_expression.rb +0 -3
  280. data/lib/mongoid/matcher/eq.rb +0 -3
  281. data/lib/mongoid/matcher/eq_impl.rb +12 -17
  282. data/lib/mongoid/matcher/eq_impl_with_regexp.rb +4 -2
  283. data/lib/mongoid/matcher/exists.rb +1 -4
  284. data/lib/mongoid/matcher/expression.rb +5 -10
  285. data/lib/mongoid/matcher/expression_operator.rb +1 -3
  286. data/lib/mongoid/matcher/field_expression.rb +10 -13
  287. data/lib/mongoid/matcher/field_operator.rb +4 -6
  288. data/lib/mongoid/matcher/gt.rb +0 -3
  289. data/lib/mongoid/matcher/gte.rb +0 -3
  290. data/lib/mongoid/matcher/in.rb +9 -12
  291. data/lib/mongoid/matcher/lt.rb +0 -3
  292. data/lib/mongoid/matcher/lte.rb +0 -3
  293. data/lib/mongoid/matcher/mod.rb +4 -8
  294. data/lib/mongoid/matcher/ne.rb +0 -3
  295. data/lib/mongoid/matcher/nin.rb +0 -3
  296. data/lib/mongoid/matcher/nor.rb +1 -6
  297. data/lib/mongoid/matcher/not.rb +0 -3
  298. data/lib/mongoid/matcher/or.rb +0 -3
  299. data/lib/mongoid/matcher/regex.rb +12 -11
  300. data/lib/mongoid/matcher/size.rb +2 -5
  301. data/lib/mongoid/matcher/type.rb +32 -37
  302. data/lib/mongoid/matcher.rb +7 -16
  303. data/lib/mongoid/model_resolver.rb +1 -0
  304. data/lib/mongoid/persistable/creatable.rb +23 -7
  305. data/lib/mongoid/persistable/deletable.rb +6 -7
  306. data/lib/mongoid/persistable/destroyable.rb +2 -6
  307. data/lib/mongoid/persistable/incrementable.rb +1 -3
  308. data/lib/mongoid/persistable/logical.rb +3 -5
  309. data/lib/mongoid/persistable/maxable.rb +2 -4
  310. data/lib/mongoid/persistable/minable.rb +2 -4
  311. data/lib/mongoid/persistable/multipliable.rb +1 -3
  312. data/lib/mongoid/persistable/poppable.rb +2 -4
  313. data/lib/mongoid/persistable/pullable.rb +3 -5
  314. data/lib/mongoid/persistable/pushable.rb +5 -7
  315. data/lib/mongoid/persistable/renamable.rb +1 -3
  316. data/lib/mongoid/persistable/savable.rb +0 -3
  317. data/lib/mongoid/persistable/settable.rb +4 -13
  318. data/lib/mongoid/persistable/unsettable.rb +1 -3
  319. data/lib/mongoid/persistable/updatable.rb +16 -24
  320. data/lib/mongoid/persistable/upsertable.rb +8 -10
  321. data/lib/mongoid/persistable.rb +28 -27
  322. data/lib/mongoid/persistence_context.rb +34 -76
  323. data/lib/mongoid/pluckable.rb +0 -2
  324. data/lib/mongoid/positional.rb +5 -11
  325. data/lib/mongoid/railtie.rb +16 -22
  326. data/lib/mongoid/railties/console_sandbox.rb +6 -8
  327. data/lib/mongoid/railties/controller_runtime.rb +7 -11
  328. data/lib/mongoid/railties/database.rake +42 -44
  329. data/lib/mongoid/scopable.rb +23 -31
  330. data/lib/mongoid/search_indexable.rb +337 -14
  331. data/lib/mongoid/selectable.rb +1 -3
  332. data/lib/mongoid/serializable.rb +6 -8
  333. data/lib/mongoid/shardable.rb +8 -17
  334. data/lib/mongoid/stateful.rb +5 -10
  335. data/lib/mongoid/stringified_symbol.rb +5 -9
  336. data/lib/mongoid/tasks/database.rake +31 -32
  337. data/lib/mongoid/tasks/database.rb +37 -53
  338. data/lib/mongoid/tasks/encryption.rb +10 -15
  339. data/lib/mongoid/threaded/lifecycle.rb +6 -9
  340. data/lib/mongoid/threaded.rb +132 -69
  341. data/lib/mongoid/timestamps/created/short.rb +2 -3
  342. data/lib/mongoid/timestamps/created.rb +1 -2
  343. data/lib/mongoid/timestamps/short.rb +0 -1
  344. data/lib/mongoid/timestamps/timeless.rb +8 -13
  345. data/lib/mongoid/timestamps/updated/short.rb +2 -3
  346. data/lib/mongoid/timestamps/updated.rb +2 -5
  347. data/lib/mongoid/timestamps.rb +4 -6
  348. data/lib/mongoid/touchable.rb +19 -11
  349. data/lib/mongoid/traversable.rb +3 -2
  350. data/lib/mongoid/validatable/associated.rb +1 -5
  351. data/lib/mongoid/validatable/format.rb +0 -2
  352. data/lib/mongoid/validatable/length.rb +0 -2
  353. data/lib/mongoid/validatable/localizable.rb +0 -3
  354. data/lib/mongoid/validatable/macros.rb +0 -2
  355. data/lib/mongoid/validatable/numericality.rb +24 -3
  356. data/lib/mongoid/validatable/presence.rb +8 -10
  357. data/lib/mongoid/validatable/queryable.rb +0 -3
  358. data/lib/mongoid/validatable/uniqueness.rb +12 -14
  359. data/lib/mongoid/validatable.rb +15 -20
  360. data/lib/mongoid/version.rb +1 -1
  361. data/lib/mongoid/warnings.rb +10 -11
  362. data/lib/mongoid.rb +30 -34
  363. data/lib/rails/generators/mongoid/config/config_generator.rb +2 -4
  364. data/lib/rails/generators/mongoid/config/templates/mongoid.rb +4 -5
  365. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +2 -2
  366. data/lib/rails/generators/mongoid/model/model_generator.rb +5 -8
  367. data/lib/rails/generators/mongoid_generator.rb +6 -10
  368. data/lib/rails/mongoid.rb +1 -3
  369. data/spec/integration/active_job_spec.rb +1 -2
  370. data/spec/integration/app_spec.rb +142 -185
  371. data/spec/integration/associations/belongs_to_spec.rb +30 -28
  372. data/spec/integration/associations/embedded_dirty_spec.rb +0 -1
  373. data/spec/integration/associations/embedded_spec.rb +41 -43
  374. data/spec/integration/associations/embeds_many_spec.rb +46 -29
  375. data/spec/integration/associations/embeds_one_spec.rb +2 -1
  376. data/spec/integration/associations/foreign_key_spec.rb +10 -11
  377. data/spec/integration/associations/foreign_key_spec_models.rb +8 -9
  378. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +18 -30
  379. data/spec/integration/associations/has_many_spec.rb +17 -18
  380. data/spec/integration/associations/has_one_spec.rb +42 -45
  381. data/spec/integration/associations/nested_attributes_assignment_spec.rb +36 -39
  382. data/spec/integration/associations/reverse_population_spec.rb +4 -5
  383. data/spec/integration/associations/reverse_population_spec_models.rb +0 -1
  384. data/spec/integration/associations/scope_option_spec.rb +18 -20
  385. data/spec/integration/atomic/modifiers_spec.rb +72 -52
  386. data/spec/integration/bson_regexp_raw_spec.rb +2 -3
  387. data/spec/integration/caching_spec.rb +68 -0
  388. data/spec/integration/callbacks_models.rb +11 -9
  389. data/spec/integration/callbacks_spec.rb +78 -80
  390. data/spec/integration/contextual/empty_spec.rb +19 -20
  391. data/spec/integration/criteria/alias_query_spec.rb +12 -19
  392. data/spec/integration/criteria/date_field_spec.rb +1 -2
  393. data/spec/integration/criteria/default_scope_spec.rb +23 -18
  394. data/spec/integration/criteria/logical_spec.rb +18 -19
  395. data/spec/integration/criteria/range_spec.rb +77 -86
  396. data/spec/integration/criteria/raw_value_spec.rb +122 -109
  397. data/spec/integration/criteria/time_with_zone_spec.rb +33 -24
  398. data/spec/integration/discriminator_key_spec.rb +85 -85
  399. data/spec/integration/discriminator_value_spec.rb +70 -73
  400. data/spec/integration/document_spec.rb +9 -10
  401. data/spec/integration/dots_and_dollars_spec.rb +105 -130
  402. data/spec/integration/encryption_spec.rb +8 -160
  403. data/spec/integration/i18n_fallbacks_spec.rb +7 -8
  404. data/spec/integration/isolation_state_spec.rb +241 -0
  405. data/spec/integration/matcher_examples_spec.rb +145 -168
  406. data/spec/integration/matcher_operator_data/in.yml +6 -6
  407. data/spec/integration/matcher_operator_data/regex.yml +0 -21
  408. data/spec/integration/matcher_operator_spec.rb +16 -26
  409. data/spec/integration/matcher_spec.rb +31 -32
  410. data/spec/integration/persistence/collection_options_spec.rb +0 -2
  411. data/spec/integration/persistence/range_field_spec.rb +24 -2
  412. data/spec/integration/server_query_spec.rb +43 -37
  413. data/spec/integration/shardable_spec.rb +10 -11
  414. data/spec/integration/stringified_symbol_field_spec.rb +28 -43
  415. data/spec/lite_spec_helper.rb +11 -12
  416. data/spec/mongoid/association/accessors_spec.rb +197 -285
  417. data/spec/mongoid/association/auto_save_spec.rb +62 -93
  418. data/spec/mongoid/association/builders_spec.rb +121 -74
  419. data/spec/mongoid/association/constrainable_spec.rb +9 -22
  420. data/spec/mongoid/association/counter_cache_spec.rb +71 -104
  421. data/spec/mongoid/association/depending_spec.rb +166 -208
  422. data/spec/mongoid/association/eager_spec.rb +416 -80
  423. data/spec/mongoid/association/embedded/cyclic_spec.rb +36 -48
  424. data/spec/mongoid/association/embedded/dirty_spec.rb +15 -20
  425. data/spec/mongoid/association/embedded/embedded_in/binding_spec.rb +36 -54
  426. data/spec/mongoid/association/embedded/embedded_in/buildable_spec.rb +8 -15
  427. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +135 -175
  428. data/spec/mongoid/association/embedded/embedded_in_spec.rb +25 -127
  429. data/spec/mongoid/association/embedded/embeds_many/binding_spec.rb +9 -14
  430. data/spec/mongoid/association/embedded/embeds_many/buildable_spec.rb +38 -51
  431. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +1151 -1340
  432. data/spec/mongoid/association/embedded/embeds_many_models.rb +17 -17
  433. data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +11 -11
  434. data/spec/mongoid/association/embedded/embeds_many_spec.rb +25 -138
  435. data/spec/mongoid/association/embedded/embeds_one/binding_spec.rb +15 -23
  436. data/spec/mongoid/association/embedded/embeds_one/buildable_spec.rb +18 -26
  437. data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +204 -273
  438. data/spec/mongoid/association/embedded/embeds_one_dnl_models.rb +0 -1
  439. data/spec/mongoid/association/embedded/embeds_one_models.rb +0 -1
  440. data/spec/mongoid/association/embedded/embeds_one_query_spec.rb +5 -6
  441. data/spec/mongoid/association/embedded/embeds_one_spec.rb +28 -143
  442. data/spec/mongoid/association/macros_spec.rb +213 -300
  443. data/spec/mongoid/association/nested/many_spec.rb +51 -73
  444. data/spec/mongoid/association/nested/one_spec.rb +56 -80
  445. data/spec/mongoid/association/options_spec.rb +187 -310
  446. data/spec/mongoid/association/polymorphic_spec.rb +35 -53
  447. data/spec/mongoid/association/referenced/belongs_to/binding_spec.rb +55 -76
  448. data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +36 -41
  449. data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +81 -101
  450. data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +258 -345
  451. data/spec/mongoid/association/referenced/belongs_to_models.rb +4 -3
  452. data/spec/mongoid/association/referenced/belongs_to_query_spec.rb +3 -4
  453. data/spec/mongoid/association/referenced/belongs_to_spec.rb +91 -294
  454. data/spec/mongoid/association/referenced/has_and_belongs_to_many/binding_spec.rb +42 -56
  455. data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +41 -32
  456. data/spec/mongoid/association/referenced/has_and_belongs_to_many/eager_spec.rb +36 -42
  457. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_persistence_spec.rb +8 -9
  458. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +771 -1023
  459. data/spec/mongoid/association/referenced/has_and_belongs_to_many_models.rb +9 -9
  460. data/spec/mongoid/association/referenced/has_and_belongs_to_many_query_spec.rb +5 -6
  461. data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +59 -177
  462. data/spec/mongoid/association/referenced/has_many/binding_spec.rb +29 -43
  463. data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +53 -53
  464. data/spec/mongoid/association/referenced/has_many/eager_spec.rb +104 -67
  465. data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +431 -675
  466. data/spec/mongoid/association/referenced/has_many/proxy_query_spec.rb +1 -2
  467. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +173 -156
  468. data/spec/mongoid/association/referenced/has_many_models.rb +1 -2
  469. data/spec/mongoid/association/referenced/has_many_query_spec.rb +3 -4
  470. data/spec/mongoid/association/referenced/has_many_spec.rb +44 -188
  471. data/spec/mongoid/association/referenced/has_one/binding_spec.rb +26 -38
  472. data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +28 -29
  473. data/spec/mongoid/association/referenced/has_one/eager_spec.rb +29 -42
  474. data/spec/mongoid/association/referenced/has_one/proxy_spec.rb +270 -332
  475. data/spec/mongoid/association/referenced/has_one_models.rb +1 -2
  476. data/spec/mongoid/association/referenced/has_one_query_spec.rb +3 -4
  477. data/spec/mongoid/association/referenced/has_one_spec.rb +40 -188
  478. data/spec/mongoid/association/reflections_spec.rb +14 -24
  479. data/spec/mongoid/association/syncable_spec.rb +102 -125
  480. data/spec/mongoid/association_spec.rb +47 -70
  481. data/spec/mongoid/atomic/modifiers_spec.rb +184 -197
  482. data/spec/mongoid/atomic/paths/embedded/many_spec.rb +39 -52
  483. data/spec/mongoid/atomic/paths/embedded/one_spec.rb +35 -49
  484. data/spec/mongoid/atomic/paths/root_spec.rb +9 -15
  485. data/spec/mongoid/atomic/paths_spec.rb +81 -117
  486. data/spec/mongoid/atomic_spec.rb +156 -171
  487. data/spec/mongoid/attributes/dynamic_spec.rb +5 -6
  488. data/spec/mongoid/attributes/nested_spec.rb +1221 -1888
  489. data/spec/mongoid/attributes/nested_spec_models.rb +8 -9
  490. data/spec/mongoid/attributes/projector_spec.rb +2 -6
  491. data/spec/mongoid/attributes/readonly_spec.rb +73 -97
  492. data/spec/mongoid/attributes_spec.rb +669 -826
  493. data/spec/mongoid/cacheable_spec.rb +74 -44
  494. data/spec/mongoid/changeable_spec.rb +591 -733
  495. data/spec/mongoid/clients/factory_spec.rb +122 -154
  496. data/spec/mongoid/clients/options_spec.rb +40 -59
  497. data/spec/mongoid/clients/sessions_spec.rb +25 -44
  498. data/spec/mongoid/clients/transactions_spec.rb +33 -50
  499. data/spec/mongoid/clients/transactions_spec_models.rb +3 -4
  500. data/spec/mongoid/clients_spec.rb +265 -355
  501. data/spec/mongoid/collection_configurable_spec.rb +9 -12
  502. data/spec/mongoid/composable_spec.rb +2 -9
  503. data/spec/mongoid/config/defaults_spec.rb +24 -49
  504. data/spec/mongoid/config/encryption_spec.rb +53 -284
  505. data/spec/mongoid/config/environment_spec.rb +30 -40
  506. data/spec/mongoid/config/introspection_spec.rb +42 -37
  507. data/spec/mongoid/config/options_spec.rb +14 -21
  508. data/spec/mongoid/config_spec.rb +145 -195
  509. data/spec/mongoid/contextual/aggregable/memory_spec.rb +98 -226
  510. data/spec/mongoid/contextual/aggregable/memory_table_spec.rb +4 -7
  511. data/spec/mongoid/contextual/aggregable/mongo_spec.rb +149 -187
  512. data/spec/mongoid/contextual/aggregable/none_spec.rb +14 -16
  513. data/spec/mongoid/contextual/atomic_spec.rb +223 -271
  514. data/spec/mongoid/contextual/map_reduce_spec.rb +110 -138
  515. data/spec/mongoid/contextual/memory_spec.rb +706 -1031
  516. data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +12 -13
  517. data/spec/mongoid/contextual/mongo_spec.rb +1008 -1213
  518. data/spec/mongoid/contextual/none_spec.rb +54 -59
  519. data/spec/mongoid/contextual/short_circuit_spec.rb +139 -0
  520. data/spec/mongoid/copyable_spec.rb +254 -300
  521. data/spec/mongoid/copyable_spec_models.rb +0 -1
  522. data/spec/mongoid/criteria/findable_spec.rb +322 -387
  523. data/spec/mongoid/criteria/includable_spec.rb +421 -372
  524. data/spec/mongoid/criteria/includable_spec_models.rb +16 -7
  525. data/spec/mongoid/criteria/inspectable_spec.rb +11 -14
  526. data/spec/mongoid/criteria/marshalable_spec.rb +11 -14
  527. data/spec/mongoid/criteria/modifiable_spec.rb +366 -512
  528. data/spec/mongoid/criteria/options_spec.rb +2 -7
  529. data/spec/mongoid/criteria/queryable/aggregable_spec.rb +112 -141
  530. data/spec/mongoid/criteria/queryable/expandable_spec.rb +8 -11
  531. data/spec/mongoid/criteria/queryable/extensions/array_spec.rb +127 -184
  532. data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +123 -60
  533. data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +84 -117
  534. data/spec/mongoid/criteria/queryable/extensions/date_spec.rb +44 -65
  535. data/spec/mongoid/criteria/queryable/extensions/date_time_spec.rb +86 -108
  536. data/spec/mongoid/criteria/queryable/extensions/float_spec.rb +94 -33
  537. data/spec/mongoid/criteria/queryable/extensions/hash_spec.rb +103 -138
  538. data/spec/mongoid/criteria/queryable/extensions/integer_spec.rb +95 -34
  539. data/spec/mongoid/criteria/queryable/extensions/nil_class_spec.rb +25 -38
  540. data/spec/mongoid/criteria/queryable/extensions/numeric_spec.rb +26 -28
  541. data/spec/mongoid/criteria/queryable/extensions/object_spec.rb +32 -54
  542. data/spec/mongoid/criteria/queryable/extensions/range_spec.rb +136 -130
  543. data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +16 -24
  544. data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +15 -24
  545. data/spec/mongoid/criteria/queryable/extensions/set_spec.rb +12 -19
  546. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +46 -63
  547. data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +28 -42
  548. data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +73 -96
  549. data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +71 -92
  550. data/spec/mongoid/criteria/queryable/key_spec.rb +32 -36
  551. data/spec/mongoid/criteria/queryable/mergeable_spec.rb +44 -49
  552. data/spec/mongoid/criteria/queryable/optional_spec.rb +373 -486
  553. data/spec/mongoid/criteria/queryable/options_spec.rb +172 -123
  554. data/spec/mongoid/criteria/queryable/pipeline_spec.rb +65 -87
  555. data/spec/mongoid/criteria/queryable/queryable_spec.rb +41 -53
  556. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +576 -682
  557. data/spec/mongoid/criteria/queryable/selectable_shared_examples.rb +7 -11
  558. data/spec/mongoid/criteria/queryable/selectable_spec.rb +807 -998
  559. data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +154 -391
  560. data/spec/mongoid/criteria/queryable/selector_spec.rb +294 -390
  561. data/spec/mongoid/criteria/queryable/smash_spec.rb +6 -11
  562. data/spec/mongoid/criteria/queryable/storable_spec.rb +54 -81
  563. data/spec/mongoid/criteria/queryable_mql_spec.rb +92 -0
  564. data/spec/mongoid/criteria/scopable_spec.rb +102 -138
  565. data/spec/mongoid/criteria/translator_spec.rb +59 -60
  566. data/spec/mongoid/criteria_projection_spec.rb +81 -99
  567. data/spec/mongoid/criteria_spec.rb +935 -952
  568. data/spec/mongoid/document_fields_spec.rb +28 -30
  569. data/spec/mongoid/document_persistence_context_spec.rb +13 -15
  570. data/spec/mongoid/document_query_spec.rb +9 -12
  571. data/spec/mongoid/document_spec.rb +317 -409
  572. data/spec/mongoid/equality_spec.rb +69 -102
  573. data/spec/mongoid/errors/ambiguous_relationship_spec.rb +8 -11
  574. data/spec/mongoid/errors/attribute_not_loaded_spec.rb +7 -10
  575. data/spec/mongoid/errors/callback_spec.rb +8 -11
  576. data/spec/mongoid/errors/delete_restriction_spec.rb +5 -8
  577. data/spec/mongoid/errors/document_not_destroyed_spec.rb +7 -10
  578. data/spec/mongoid/errors/document_not_found_spec.rb +51 -61
  579. data/spec/mongoid/errors/invalid_collection_spec.rb +9 -13
  580. data/spec/mongoid/errors/invalid_config_file_spec.rb +8 -11
  581. data/spec/mongoid/errors/invalid_config_option_spec.rb +8 -11
  582. data/spec/mongoid/errors/invalid_field_option_spec.rb +7 -10
  583. data/spec/mongoid/errors/invalid_field_spec.rb +10 -11
  584. data/spec/mongoid/errors/invalid_field_type_spec.rb +9 -12
  585. data/spec/mongoid/errors/invalid_find_spec.rb +8 -11
  586. data/spec/mongoid/errors/invalid_includes_spec.rb +9 -13
  587. data/spec/mongoid/errors/invalid_index_spec.rb +8 -11
  588. data/spec/mongoid/errors/invalid_options_spec.rb +8 -11
  589. data/spec/mongoid/errors/invalid_path_spec.rb +7 -10
  590. data/spec/mongoid/errors/invalid_relation_spec.rb +15 -16
  591. data/spec/mongoid/errors/invalid_scope_spec.rb +8 -11
  592. data/spec/mongoid/errors/invalid_set_polymorphic_relation_spec.rb +3 -6
  593. data/spec/mongoid/errors/invalid_storage_options_spec.rb +8 -11
  594. data/spec/mongoid/errors/invalid_time_spec.rb +8 -11
  595. data/spec/mongoid/errors/inverse_not_found_spec.rb +9 -12
  596. data/spec/mongoid/errors/mixed_client_configuration_spec.rb +9 -12
  597. data/spec/mongoid/errors/mixed_relations_spec.rb +8 -11
  598. data/spec/mongoid/errors/mongoid_error_spec.rb +21 -24
  599. data/spec/mongoid/errors/nested_attributes_metadata_not_found_spec.rb +6 -9
  600. data/spec/mongoid/errors/no_client_config_spec.rb +7 -10
  601. data/spec/mongoid/errors/no_client_database_spec.rb +9 -12
  602. data/spec/mongoid/errors/no_client_hosts_spec.rb +9 -12
  603. data/spec/mongoid/errors/no_clients_config_spec.rb +7 -10
  604. data/spec/mongoid/errors/no_environment_spec.rb +8 -11
  605. data/spec/mongoid/errors/no_map_reduce_output_spec.rb +8 -11
  606. data/spec/mongoid/errors/no_metadata_spec.rb +7 -10
  607. data/spec/mongoid/errors/no_parent_spec.rb +7 -10
  608. data/spec/mongoid/errors/readonly_attribute_spec.rb +7 -10
  609. data/spec/mongoid/errors/readonly_document_spec.rb +6 -9
  610. data/spec/mongoid/errors/scope_overwrite_spec.rb +9 -12
  611. data/spec/mongoid/errors/too_many_nested_attribute_records_spec.rb +9 -12
  612. data/spec/mongoid/errors/unknown_attribute_spec.rb +5 -8
  613. data/spec/mongoid/errors/unsaved_document_spec.rb +8 -11
  614. data/spec/mongoid/errors/unsupported_javascript_spec.rb +9 -12
  615. data/spec/mongoid/errors/validations_spec.rb +11 -14
  616. data/spec/mongoid/extensions/array_spec.rb +70 -92
  617. data/spec/mongoid/extensions/big_decimal_spec.rb +233 -313
  618. data/spec/mongoid/extensions/binary_spec.rb +21 -32
  619. data/spec/mongoid/extensions/boolean_spec.rb +52 -75
  620. data/spec/mongoid/extensions/date_class_mongoize_spec.rb +43 -54
  621. data/spec/mongoid/extensions/date_spec.rb +26 -38
  622. data/spec/mongoid/extensions/date_time_spec.rb +22 -32
  623. data/spec/mongoid/extensions/decimal128_spec.rb +12 -18
  624. data/spec/mongoid/extensions/false_class_spec.rb +10 -17
  625. data/spec/mongoid/extensions/float_spec.rb +40 -61
  626. data/spec/mongoid/extensions/hash_spec.rb +46 -64
  627. data/spec/mongoid/extensions/integer_spec.rb +40 -61
  628. data/spec/mongoid/extensions/module_spec.rb +13 -18
  629. data/spec/mongoid/extensions/nil_class_spec.rb +2 -5
  630. data/spec/mongoid/extensions/object_id_spec.rb +159 -247
  631. data/spec/mongoid/extensions/object_spec.rb +31 -47
  632. data/spec/mongoid/extensions/range_spec.rb +112 -115
  633. data/spec/mongoid/extensions/raw_value_spec.rb +10 -12
  634. data/spec/mongoid/extensions/regexp_spec.rb +23 -36
  635. data/spec/mongoid/extensions/set_spec.rb +38 -49
  636. data/spec/mongoid/extensions/string_spec.rb +118 -167
  637. data/spec/mongoid/extensions/stringified_symbol_spec.rb +29 -43
  638. data/spec/mongoid/extensions/symbol_spec.rb +12 -20
  639. data/spec/mongoid/extensions/time_spec.rb +160 -202
  640. data/spec/mongoid/extensions/time_with_zone_spec.rb +79 -105
  641. data/spec/mongoid/extensions/true_class_spec.rb +10 -17
  642. data/spec/mongoid/factory_spec.rb +125 -148
  643. data/spec/mongoid/fields/foreign_key_spec.rb +124 -169
  644. data/spec/mongoid/fields/localized_spec.rb +140 -185
  645. data/spec/mongoid/fields/standard_spec.rb +43 -59
  646. data/spec/mongoid/fields_spec.rb +751 -620
  647. data/spec/mongoid/findable_spec.rb +260 -228
  648. data/spec/mongoid/indexable/specification_spec.rb +42 -58
  649. data/spec/mongoid/indexable_spec.rb +260 -264
  650. data/spec/mongoid/inspectable_spec.rb +45 -55
  651. data/spec/mongoid/interceptable_spec.rb +784 -922
  652. data/spec/mongoid/interceptable_spec_models.rb +35 -35
  653. data/spec/mongoid/loading_spec.rb +47 -43
  654. data/spec/mongoid/loggable_spec.rb +3 -6
  655. data/spec/mongoid/matcher/extract_attribute_spec.rb +2 -6
  656. data/spec/mongoid/mongoizable_spec.rb +81 -90
  657. data/spec/mongoid/monkey_patches_spec.rb +4 -0
  658. data/spec/mongoid/persistable/creatable_spec.rb +181 -220
  659. data/spec/mongoid/persistable/deletable_spec.rb +139 -179
  660. data/spec/mongoid/persistable/destroyable_spec.rb +135 -164
  661. data/spec/mongoid/persistable/incrementable_spec.rb +65 -84
  662. data/spec/mongoid/persistable/logical_spec.rb +48 -61
  663. data/spec/mongoid/persistable/maxable_spec.rb +45 -44
  664. data/spec/mongoid/persistable/minable_spec.rb +45 -44
  665. data/spec/mongoid/persistable/multipliable_spec.rb +54 -69
  666. data/spec/mongoid/persistable/poppable_spec.rb +47 -60
  667. data/spec/mongoid/persistable/pullable_spec.rb +79 -103
  668. data/spec/mongoid/persistable/pushable_spec.rb +106 -131
  669. data/spec/mongoid/persistable/renamable_spec.rb +52 -65
  670. data/spec/mongoid/persistable/savable_spec.rb +152 -189
  671. data/spec/mongoid/persistable/settable_spec.rb +121 -145
  672. data/spec/mongoid/persistable/unsettable_spec.rb +56 -73
  673. data/spec/mongoid/persistable/updatable_spec.rb +185 -232
  674. data/spec/mongoid/persistable/upsertable_spec.rb +66 -77
  675. data/spec/mongoid/persistable_spec.rb +132 -139
  676. data/spec/mongoid/persistence_context_spec.rb +31 -89
  677. data/spec/mongoid/positional_spec.rb +67 -79
  678. data/spec/mongoid/railties/bson_object_id_serializer_spec.rb +0 -3
  679. data/spec/mongoid/railties/console_sandbox_spec.rb +0 -1
  680. data/spec/mongoid/relations/proxy_spec.rb +0 -1
  681. data/spec/mongoid/reloadable_spec.rb +250 -145
  682. data/spec/mongoid/scopable_spec.rb +319 -398
  683. data/spec/mongoid/search_indexable_spec.rb +446 -41
  684. data/spec/mongoid/selectable_spec.rb +36 -51
  685. data/spec/mongoid/serializable_spec.rb +269 -312
  686. data/spec/mongoid/shardable_models.rb +5 -5
  687. data/spec/mongoid/shardable_spec.rb +44 -46
  688. data/spec/mongoid/stateful_spec.rb +55 -75
  689. data/spec/mongoid/tasks/database_rake_spec.rb +138 -141
  690. data/spec/mongoid/tasks/database_spec.rb +80 -98
  691. data/spec/mongoid/tasks/encryption_spec.rb +22 -20
  692. data/spec/mongoid/threaded_spec.rb +57 -83
  693. data/spec/mongoid/timestamps/created/short_spec.rb +12 -16
  694. data/spec/mongoid/timestamps/created_spec.rb +16 -20
  695. data/spec/mongoid/timestamps/timeless_spec.rb +31 -43
  696. data/spec/mongoid/timestamps/updated/short_spec.rb +21 -28
  697. data/spec/mongoid/timestamps/updated_spec.rb +18 -25
  698. data/spec/mongoid/timestamps_spec.rb +171 -188
  699. data/spec/mongoid/timestamps_spec_models.rb +12 -13
  700. data/spec/mongoid/touchable_spec.rb +330 -294
  701. data/spec/mongoid/touchable_spec_models.rb +35 -37
  702. data/spec/mongoid/traversable_spec.rb +435 -471
  703. data/spec/mongoid/validatable/associated_spec.rb +42 -60
  704. data/spec/mongoid/validatable/format_spec.rb +24 -34
  705. data/spec/mongoid/validatable/length_spec.rb +62 -86
  706. data/spec/mongoid/validatable/numericality_spec.rb +114 -23
  707. data/spec/mongoid/validatable/presence_spec.rb +149 -203
  708. data/spec/mongoid/validatable/uniqueness_spec.rb +632 -830
  709. data/spec/mongoid/validatable_spec.rb +71 -103
  710. data/spec/mongoid/warnings_spec.rb +5 -9
  711. data/spec/mongoid_spec.rb +25 -41
  712. data/spec/rails/controller_extension/controller_runtime_spec.rb +45 -49
  713. data/spec/rails/mongoid_spec.rb +15 -15
  714. data/spec/shared/lib/mrss/cluster_config.rb +5 -40
  715. data/spec/shared/lib/mrss/constraints.rb +7 -46
  716. data/spec/shared/lib/mrss/docker_runner.rb +10 -21
  717. data/spec/shared/lib/mrss/eg_config_utils.rb +0 -31
  718. data/spec/shared/lib/mrss/release/candidate.rb +18 -12
  719. data/spec/shared/lib/mrss/server_version_registry.rb +1 -5
  720. data/spec/shared/share/Dockerfile.erb +6 -8
  721. data/spec/shared/shlib/server.sh +0 -50
  722. data/spec/shared/shlib/set_env.sh +4 -10
  723. data/spec/spec_helper.rb +54 -31
  724. data/spec/support/authorization.rb +0 -1
  725. data/spec/support/client_registry.rb +1 -2
  726. data/spec/support/constraints.rb +8 -25
  727. data/spec/support/crypt/models.rb +7 -165
  728. data/spec/support/crypt.rb +6 -9
  729. data/spec/support/expectations.rb +21 -7
  730. data/spec/support/feature_sandbox.rb +1 -4
  731. data/spec/support/helpers.rb +0 -1
  732. data/spec/support/immutable_ids.rb +20 -21
  733. data/spec/support/macros.rb +1 -3
  734. data/spec/support/models/account.rb +4 -5
  735. data/spec/support/models/acolyte.rb +5 -5
  736. data/spec/support/models/actor.rb +3 -3
  737. data/spec/support/models/actress.rb +0 -1
  738. data/spec/support/models/address.rb +11 -11
  739. data/spec/support/models/address_component.rb +1 -1
  740. data/spec/support/models/address_number.rb +1 -1
  741. data/spec/support/models/agency.rb +1 -1
  742. data/spec/support/models/agent.rb +1 -1
  743. data/spec/support/models/album.rb +1 -2
  744. data/spec/support/models/alert.rb +1 -1
  745. data/spec/support/models/animal.rb +4 -5
  746. data/spec/support/models/answer.rb +1 -1
  747. data/spec/support/models/appointment.rb +2 -2
  748. data/spec/support/models/armrest.rb +0 -1
  749. data/spec/support/models/array_field.rb +0 -1
  750. data/spec/support/models/article.rb +0 -1
  751. data/spec/support/models/artist.rb +16 -12
  752. data/spec/support/models/artwork.rb +1 -1
  753. data/spec/support/models/audible_sound.rb +0 -1
  754. data/spec/support/models/audio.rb +2 -2
  755. data/spec/support/models/augmentation.rb +1 -2
  756. data/spec/support/models/author.rb +1 -1
  757. data/spec/support/models/baby.rb +1 -1
  758. data/spec/support/models/band.rb +0 -1
  759. data/spec/support/models/bar.rb +2 -2
  760. data/spec/support/models/basic.rb +0 -1
  761. data/spec/support/models/bed.rb +0 -1
  762. data/spec/support/models/big_palette.rb +0 -1
  763. data/spec/support/models/birthday.rb +3 -5
  764. data/spec/support/models/bolt.rb +0 -1
  765. data/spec/support/models/bomb.rb +1 -1
  766. data/spec/support/models/book.rb +1 -1
  767. data/spec/support/models/breed.rb +1 -1
  768. data/spec/support/models/browser.rb +1 -2
  769. data/spec/support/models/building.rb +0 -1
  770. data/spec/support/models/building_address.rb +1 -1
  771. data/spec/support/models/bus.rb +1 -1
  772. data/spec/support/models/business.rb +2 -2
  773. data/spec/support/models/callback_test.rb +1 -1
  774. data/spec/support/models/canvas.rb +5 -6
  775. data/spec/support/models/car.rb +0 -1
  776. data/spec/support/models/cat.rb +0 -2
  777. data/spec/support/models/catalog.rb +0 -1
  778. data/spec/support/models/category.rb +1 -1
  779. data/spec/support/models/child.rb +1 -1
  780. data/spec/support/models/child_doc.rb +5 -6
  781. data/spec/support/models/church.rb +1 -1
  782. data/spec/support/models/circle.rb +0 -1
  783. data/spec/support/models/circuit.rb +2 -2
  784. data/spec/support/models/circus.rb +0 -1
  785. data/spec/support/models/code.rb +2 -1
  786. data/spec/support/models/coding/pull_request.rb +0 -1
  787. data/spec/support/models/coding.rb +0 -1
  788. data/spec/support/models/comment.rb +0 -1
  789. data/spec/support/models/company.rb +0 -1
  790. data/spec/support/models/consumption_period.rb +0 -1
  791. data/spec/support/models/contextable_item.rb +1 -1
  792. data/spec/support/models/contractor.rb +1 -1
  793. data/spec/support/models/cookie.rb +0 -1
  794. data/spec/support/models/country_code.rb +2 -3
  795. data/spec/support/models/courier_job.rb +2 -2
  796. data/spec/support/models/cover.rb +0 -1
  797. data/spec/support/models/crate.rb +0 -1
  798. data/spec/support/models/customer.rb +0 -1
  799. data/spec/support/models/customer_address.rb +0 -1
  800. data/spec/support/models/deed.rb +1 -1
  801. data/spec/support/models/definition.rb +1 -1
  802. data/spec/support/models/delegating_patient.rb +0 -1
  803. data/spec/support/models/description.rb +0 -1
  804. data/spec/support/models/dictionary.rb +1 -1
  805. data/spec/support/models/division.rb +2 -2
  806. data/spec/support/models/doctor.rb +0 -1
  807. data/spec/support/models/dog.rb +2 -2
  808. data/spec/support/models/dokument.rb +1 -1
  809. data/spec/support/models/draft.rb +0 -1
  810. data/spec/support/models/dragon.rb +1 -1
  811. data/spec/support/models/driver.rb +2 -2
  812. data/spec/support/models/drug.rb +1 -1
  813. data/spec/support/models/dungeon.rb +1 -1
  814. data/spec/support/models/edit.rb +1 -1
  815. data/spec/support/models/email.rb +1 -1
  816. data/spec/support/models/employer.rb +1 -2
  817. data/spec/support/models/entry.rb +1 -1
  818. data/spec/support/models/eraser.rb +0 -1
  819. data/spec/support/models/even.rb +1 -1
  820. data/spec/support/models/event.rb +3 -4
  821. data/spec/support/models/exhibition.rb +1 -1
  822. data/spec/support/models/exhibitor.rb +1 -1
  823. data/spec/support/models/explosion.rb +1 -1
  824. data/spec/support/models/eye.rb +0 -1
  825. data/spec/support/models/eye_bowl.rb +2 -3
  826. data/spec/support/models/face.rb +2 -3
  827. data/spec/support/models/fanatic.rb +1 -1
  828. data/spec/support/models/favorite.rb +1 -1
  829. data/spec/support/models/filesystem.rb +1 -1
  830. data/spec/support/models/fire_hydrant.rb +1 -1
  831. data/spec/support/models/firefox.rb +0 -1
  832. data/spec/support/models/fish.rb +0 -1
  833. data/spec/support/models/folder.rb +0 -2
  834. data/spec/support/models/folder_item.rb +1 -3
  835. data/spec/support/models/fruits.rb +9 -5
  836. data/spec/support/models/game.rb +3 -4
  837. data/spec/support/models/ghost.rb +0 -1
  838. data/spec/support/models/guitar.rb +0 -1
  839. data/spec/support/models/hole.rb +0 -1
  840. data/spec/support/models/home.rb +1 -1
  841. data/spec/support/models/house.rb +2 -2
  842. data/spec/support/models/html_writer.rb +0 -1
  843. data/spec/support/models/id_key.rb +3 -3
  844. data/spec/support/models/idnodef.rb +0 -1
  845. data/spec/support/models/image.rb +0 -1
  846. data/spec/support/models/implant.rb +1 -2
  847. data/spec/support/models/instrument.rb +2 -3
  848. data/spec/support/models/item.rb +2 -2
  849. data/spec/support/models/jar.rb +1 -2
  850. data/spec/support/models/kaleidoscope.rb +1 -1
  851. data/spec/support/models/kangaroo.rb +1 -1
  852. data/spec/support/models/label.rb +2 -2
  853. data/spec/support/models/language.rb +1 -1
  854. data/spec/support/models/lat_lng.rb +0 -1
  855. data/spec/support/models/league.rb +1 -1
  856. data/spec/support/models/learner.rb +0 -1
  857. data/spec/support/models/line_item.rb +1 -1
  858. data/spec/support/models/location.rb +1 -1
  859. data/spec/support/models/login.rb +1 -2
  860. data/spec/support/models/manufacturer.rb +0 -1
  861. data/spec/support/models/meat.rb +1 -1
  862. data/spec/support/models/membership.rb +1 -1
  863. data/spec/support/models/message.rb +1 -2
  864. data/spec/support/models/minim.rb +0 -1
  865. data/spec/support/models/mixed_drink.rb +1 -1
  866. data/spec/support/models/modifier_models.rb +25 -0
  867. data/spec/support/models/mop.rb +0 -1
  868. data/spec/support/models/movie.rb +1 -1
  869. data/spec/support/models/my_hash.rb +1 -2
  870. data/spec/support/models/name.rb +4 -5
  871. data/spec/support/models/name_only.rb +0 -1
  872. data/spec/support/models/node.rb +1 -1
  873. data/spec/support/models/note.rb +3 -3
  874. data/spec/support/models/nut.rb +0 -1
  875. data/spec/support/models/odd.rb +1 -1
  876. data/spec/support/models/order.rb +1 -1
  877. data/spec/support/models/ordered_post.rb +1 -1
  878. data/spec/support/models/ordered_preference.rb +1 -1
  879. data/spec/support/models/oscar.rb +1 -1
  880. data/spec/support/models/other_owner_object.rb +0 -1
  881. data/spec/support/models/override.rb +3 -11
  882. data/spec/support/models/ownable.rb +1 -1
  883. data/spec/support/models/owner.rb +1 -1
  884. data/spec/support/models/pack.rb +0 -1
  885. data/spec/support/models/page.rb +3 -3
  886. data/spec/support/models/page_question.rb +1 -1
  887. data/spec/support/models/palette.rb +2 -2
  888. data/spec/support/models/parent.rb +2 -2
  889. data/spec/support/models/parent_doc.rb +1 -1
  890. data/spec/support/models/passport.rb +0 -1
  891. data/spec/support/models/patient.rb +2 -2
  892. data/spec/support/models/pdf_writer.rb +0 -1
  893. data/spec/support/models/pencil.rb +0 -1
  894. data/spec/support/models/person.rb +32 -28
  895. data/spec/support/models/pet.rb +1 -1
  896. data/spec/support/models/pet_owner.rb +1 -1
  897. data/spec/support/models/phone.rb +1 -2
  898. data/spec/support/models/piano.rb +0 -1
  899. data/spec/support/models/pizza.rb +1 -1
  900. data/spec/support/models/player.rb +5 -5
  901. data/spec/support/models/post.rb +12 -12
  902. data/spec/support/models/post_genre.rb +1 -1
  903. data/spec/support/models/powerup.rb +1 -2
  904. data/spec/support/models/preference.rb +2 -2
  905. data/spec/support/models/princess.rb +1 -1
  906. data/spec/support/models/product.rb +3 -3
  907. data/spec/support/models/profile.rb +2 -1
  908. data/spec/support/models/pronunciation.rb +1 -1
  909. data/spec/support/models/pub.rb +2 -2
  910. data/spec/support/models/publication/encyclopedia.rb +0 -1
  911. data/spec/support/models/publication/review.rb +0 -1
  912. data/spec/support/models/publication.rb +0 -1
  913. data/spec/support/models/purchase.rb +1 -1
  914. data/spec/support/models/purchased_item.rb +1 -1
  915. data/spec/support/models/question.rb +2 -2
  916. data/spec/support/models/quiz.rb +1 -1
  917. data/spec/support/models/rating.rb +1 -1
  918. data/spec/support/models/record.rb +2 -3
  919. data/spec/support/models/registry.rb +1 -1
  920. data/spec/support/models/role.rb +1 -1
  921. data/spec/support/models/root_category.rb +1 -1
  922. data/spec/support/models/sandbox/app/models/app_models_message.rb +0 -1
  923. data/spec/support/models/sandbox/lib/models/lib_models_message.rb +0 -1
  924. data/spec/support/models/sandbox/sandbox_message.rb +0 -1
  925. data/spec/support/models/sandbox/sandbox_user.rb +0 -1
  926. data/spec/support/models/sandbox/subdir/sandbox_comment.rb +0 -1
  927. data/spec/support/models/sandwich.rb +1 -1
  928. data/spec/support/models/scheduler.rb +0 -1
  929. data/spec/support/models/school.rb +0 -1
  930. data/spec/support/models/scribe.rb +1 -1
  931. data/spec/support/models/sealer.rb +0 -1
  932. data/spec/support/models/seat.rb +0 -1
  933. data/spec/support/models/seo.rb +1 -1
  934. data/spec/support/models/series.rb +1 -1
  935. data/spec/support/models/server.rb +1 -1
  936. data/spec/support/models/service.rb +2 -2
  937. data/spec/support/models/shape.rb +3 -3
  938. data/spec/support/models/shelf.rb +1 -1
  939. data/spec/support/models/shield.rb +2 -2
  940. data/spec/support/models/shipment_address.rb +0 -1
  941. data/spec/support/models/shipping_container.rb +1 -1
  942. data/spec/support/models/shipping_pack.rb +0 -1
  943. data/spec/support/models/shirt.rb +0 -1
  944. data/spec/support/models/shop.rb +2 -2
  945. data/spec/support/models/short_agent.rb +0 -1
  946. data/spec/support/models/short_quiz.rb +1 -1
  947. data/spec/support/models/simple.rb +1 -1
  948. data/spec/support/models/slave.rb +1 -1
  949. data/spec/support/models/song.rb +1 -2
  950. data/spec/support/models/sound.rb +2 -2
  951. data/spec/support/models/spacer.rb +0 -1
  952. data/spec/support/models/square.rb +0 -1
  953. data/spec/support/models/staff.rb +0 -1
  954. data/spec/support/models/store_as_dup_test1.rb +2 -2
  955. data/spec/support/models/store_as_dup_test2.rb +1 -1
  956. data/spec/support/models/store_as_dup_test3.rb +2 -2
  957. data/spec/support/models/store_as_dup_test4.rb +1 -1
  958. data/spec/support/models/strategy.rb +0 -1
  959. data/spec/support/models/student.rb +1 -2
  960. data/spec/support/models/sub_item.rb +0 -1
  961. data/spec/support/models/subscription.rb +2 -2
  962. data/spec/support/models/survey.rb +2 -2
  963. data/spec/support/models/symptom.rb +2 -2
  964. data/spec/support/models/system_role.rb +2 -3
  965. data/spec/support/models/tag.rb +2 -2
  966. data/spec/support/models/target.rb +1 -1
  967. data/spec/support/models/template.rb +1 -1
  968. data/spec/support/models/thing.rb +1 -1
  969. data/spec/support/models/threadlocker.rb +0 -1
  970. data/spec/support/models/title.rb +0 -1
  971. data/spec/support/models/tool.rb +3 -3
  972. data/spec/support/models/topping.rb +1 -1
  973. data/spec/support/models/toy.rb +0 -1
  974. data/spec/support/models/track.rb +1 -1
  975. data/spec/support/models/translation.rb +1 -1
  976. data/spec/support/models/tree.rb +2 -3
  977. data/spec/support/models/truck.rb +0 -1
  978. data/spec/support/models/updatable.rb +0 -1
  979. data/spec/support/models/user.rb +3 -4
  980. data/spec/support/models/user_account.rb +3 -3
  981. data/spec/support/models/validation_callback.rb +4 -4
  982. data/spec/support/models/vehicle.rb +2 -3
  983. data/spec/support/models/version.rb +1 -1
  984. data/spec/support/models/vertex.rb +0 -1
  985. data/spec/support/models/vet_visit.rb +1 -1
  986. data/spec/support/models/video.rb +2 -2
  987. data/spec/support/models/video_game.rb +0 -1
  988. data/spec/support/models/washer.rb +0 -1
  989. data/spec/support/models/weapon.rb +1 -2
  990. data/spec/support/models/wiki_page.rb +2 -3
  991. data/spec/support/models/word.rb +2 -2
  992. data/spec/support/models/word_origin.rb +1 -2
  993. data/spec/support/models/writer.rb +3 -3
  994. data/spec/support/rails_mock.rb +7 -12
  995. data/spec/support/shared/time.rb +0 -1
  996. data/spec/support/sinatra_mock.rb +4 -2
  997. data/spec/support/spec_config.rb +19 -18
  998. metadata +14 -14
  999. data/lib/mongoid/errors/in_memory_regexp_timeout.rb +0 -26
  1000. data/lib/mongoid/errors/no_encryption_schema.rb +0 -28
  1001. data/lib/mongoid/field_readable.rb +0 -70
  1002. data/lib/mongoid/matcher/regexp_budget.rb +0 -383
  1003. data/spec/integration/matcher_regexp_timeout_spec.rb +0 -215
  1004. data/spec/integration/query_operator_guard_spec.rb +0 -89
  1005. data/spec/mongoid/encryptable_spec.rb +0 -61
  1006. data/spec/mongoid/matcher/regexp_budget_spec.rb +0 -570
@@ -1,139 +1,123 @@
1
1
  # frozen_string_literal: true
2
- # rubocop:todo all
3
2
 
4
- require "spec_helper"
3
+ require 'spec_helper'
5
4
 
6
5
  describe Mongoid::Criteria do
7
-
8
- describe "#==" do
9
-
6
+ describe '#==' do
10
7
  let(:criteria) do
11
- Band.where(name: "Depeche Mode")
8
+ Band.where(name: 'Depeche Mode')
12
9
  end
13
10
 
14
- context "when the other is a criteria" do
15
-
16
- context "when the criteria are the same" do
17
-
11
+ context 'when the other is a criteria' do
12
+ context 'when the criteria are the same' do
18
13
  let(:other) do
19
- Band.where(name: "Depeche Mode")
14
+ Band.where(name: 'Depeche Mode')
20
15
  end
21
16
 
22
- it "returns true" do
17
+ it 'returns true' do
23
18
  expect(criteria).to eq(other)
24
19
  end
25
20
  end
26
21
 
27
- context "when the criteria differ" do
28
-
22
+ context 'when the criteria differ' do
29
23
  let(:other) do
30
- Band.where(name: "Tool")
24
+ Band.where(name: 'Tool')
31
25
  end
32
26
 
33
- it "returns false" do
34
- expect(criteria).to_not eq(other)
27
+ it 'returns false' do
28
+ expect(criteria).not_to eq(other)
35
29
  end
36
30
  end
37
31
  end
38
32
 
39
- context "when the other is an enumerable" do
40
-
41
- context "when the entries are the same" do
42
-
33
+ context 'when the other is an enumerable' do
34
+ context 'when the entries are the same' do
43
35
  let!(:band) do
44
- Band.create!(name: "Depeche Mode")
36
+ Band.create!(name: 'Depeche Mode')
45
37
  end
46
38
 
47
39
  let(:other) do
48
40
  [ band ]
49
41
  end
50
42
 
51
- it "returns true" do
43
+ it 'returns true' do
52
44
  expect(criteria).to eq(other)
53
45
  end
54
46
  end
55
47
 
56
- context "when the entries are not the same" do
57
-
48
+ context 'when the entries are not the same' do
58
49
  let!(:band) do
59
- Band.create!(name: "Depeche Mode")
50
+ Band.create!(name: 'Depeche Mode')
60
51
  end
61
52
 
62
53
  let!(:other_band) do
63
- Band.create!(name: "Tool")
54
+ Band.create!(name: 'Tool')
64
55
  end
65
56
 
66
57
  let(:other) do
67
58
  [ other_band ]
68
59
  end
69
60
 
70
- it "returns false" do
71
- expect(criteria).to_not eq(other)
61
+ it 'returns false' do
62
+ expect(criteria).not_to eq(other)
72
63
  end
73
64
  end
74
65
  end
75
66
 
76
- context "when the other is neither a criteria or enumerable" do
77
-
78
- it "returns false" do
79
- expect(criteria).to_not eq("test")
67
+ context 'when the other is neither a criteria or enumerable' do
68
+ it 'returns false' do
69
+ expect(criteria).not_to eq('test')
80
70
  end
81
71
  end
82
72
  end
83
73
 
84
- describe "#===" do
85
-
86
- context "when the other is a criteria" do
87
-
74
+ describe '#===' do
75
+ context 'when the other is a criteria' do
88
76
  let(:other) do
89
- Band.where(name: "Depeche Mode")
77
+ Band.where(name: 'Depeche Mode')
90
78
  end
91
79
 
92
- it "returns true" do
93
- expect(described_class === other).to be true
80
+ it 'returns true' do
81
+ expect(described_class === other).to be true # rubocop:disable Style/CaseEquality
94
82
  end
95
83
  end
96
84
 
97
- context "when the other is not a criteria" do
98
-
99
- it "returns false" do
100
- expect(described_class === []).to be false
85
+ context 'when the other is not a criteria' do
86
+ it 'returns false' do
87
+ expect(described_class === []).to be false # rubocop:disable Style/CaseEquality
101
88
  end
102
89
  end
103
90
  end
104
91
 
105
- describe "#asc" do
106
-
92
+ describe '#asc' do
107
93
  let(:person) do
108
94
  Person.create!
109
95
  end
110
96
 
111
- context "when the documents are embedded" do
112
-
97
+ context 'when the documents are embedded' do
113
98
  let!(:hobrecht) do
114
- person.addresses.create!(street: "hobrecht", name: "hobrecht")
99
+ person.addresses.create!(street: 'hobrecht', name: 'hobrecht')
115
100
  end
116
101
 
117
102
  let!(:friedel) do
118
- person.addresses.create!(street: "friedel", name: "friedel")
103
+ person.addresses.create!(street: 'friedel', name: 'friedel')
119
104
  end
120
105
 
121
106
  let!(:pfluger) do
122
- person.addresses.create!(street: "pfluger", name: "pfluger")
107
+ person.addresses.create!(street: 'pfluger', name: 'pfluger')
123
108
  end
124
109
 
125
110
  let(:criteria) do
126
111
  person.addresses.asc(:name)
127
112
  end
128
113
 
129
- it "returns the sorted documents" do
114
+ it 'returns the sorted documents' do
130
115
  expect(criteria).to eq([ friedel, hobrecht, pfluger ])
131
116
  end
132
117
  end
133
118
  end
134
119
 
135
- describe "#batch_size" do
136
-
120
+ describe '#batch_size' do
137
121
  let(:person) do
138
122
  Person.create!
139
123
  end
@@ -142,17 +126,16 @@ describe Mongoid::Criteria do
142
126
  Person.batch_size(1000)
143
127
  end
144
128
 
145
- it "adds the batch size option" do
129
+ it 'adds the batch size option' do
146
130
  expect(criteria.options[:batch_size]).to eq(1000)
147
131
  end
148
132
 
149
- it "returns the correct documents" do
133
+ it 'returns the correct documents' do
150
134
  expect(criteria).to eq([ person ])
151
135
  end
152
136
  end
153
137
 
154
- describe "#read" do
155
-
138
+ describe '#read' do
156
139
  let(:person) do
157
140
  Person.create!
158
141
  end
@@ -161,21 +144,19 @@ describe Mongoid::Criteria do
161
144
  Person.read(mode: :secondary)
162
145
  end
163
146
 
164
- it "adds the read option" do
147
+ it 'adds the read option' do
165
148
  expect(criteria.options[:read]).to eq(mode: :secondary)
166
149
  end
167
150
  end
168
151
 
169
- describe "#aggregates" do
170
-
171
- context "when provided a single field" do
172
-
152
+ describe '#aggregates' do
153
+ context 'when provided a single field' do
173
154
  let!(:depeche) do
174
- Band.create!(name: "Depeche Mode", likes: 1000)
155
+ Band.create!(name: 'Depeche Mode', likes: 1000)
175
156
  end
176
157
 
177
158
  let!(:tool) do
178
- Band.create!(name: "Tool", likes: 500)
159
+ Band.create!(name: 'Tool', likes: 500)
179
160
  end
180
161
 
181
162
  let(:criteria) do
@@ -186,38 +167,36 @@ describe Mongoid::Criteria do
186
167
  criteria.aggregates(:likes)
187
168
  end
188
169
 
189
- it "returns an avg" do
190
- expect(aggregates["avg"]).to eq(750)
170
+ it 'returns an avg' do
171
+ expect(aggregates['avg']).to eq(750)
191
172
  end
192
173
 
193
- it "returns a count" do
194
- expect(aggregates["count"]).to eq(2)
174
+ it 'returns a count' do
175
+ expect(aggregates['count']).to eq(2)
195
176
  end
196
177
 
197
- it "returns a max" do
198
- expect(aggregates["max"]).to eq(1000)
178
+ it 'returns a max' do
179
+ expect(aggregates['max']).to eq(1000)
199
180
  end
200
181
 
201
- it "returns a min" do
202
- expect(aggregates["min"]).to eq(500)
182
+ it 'returns a min' do
183
+ expect(aggregates['min']).to eq(500)
203
184
  end
204
185
 
205
- it "returns a sum" do
206
- expect(aggregates["sum"]).to eq(1500)
186
+ it 'returns a sum' do
187
+ expect(aggregates['sum']).to eq(1500)
207
188
  end
208
189
  end
209
190
  end
210
191
 
211
- describe "#avg" do
212
-
213
- context "when provided a single field" do
214
-
192
+ describe '#avg' do
193
+ context 'when provided a single field' do
215
194
  let!(:depeche) do
216
- Band.create!(name: "Depeche Mode", likes: 1000)
195
+ Band.create!(name: 'Depeche Mode', likes: 1000)
217
196
  end
218
197
 
219
198
  let!(:tool) do
220
- Band.create!(name: "Tool", likes: 500)
199
+ Band.create!(name: 'Tool', likes: 500)
221
200
  end
222
201
 
223
202
  let(:criteria) do
@@ -228,73 +207,67 @@ describe Mongoid::Criteria do
228
207
  criteria.avg(:likes)
229
208
  end
230
209
 
231
- it "returns the avg of the provided field" do
210
+ it 'returns the avg of the provided field' do
232
211
  expect(avg).to eq(750)
233
212
  end
234
213
  end
235
214
  end
236
215
 
237
- [ :all, :all_in ].each do |method|
238
-
239
- describe "\##{method}" do
240
-
216
+ %i[all all_in].each do |method|
217
+ describe "##{method}" do
241
218
  let!(:match) do
242
- Band.create!(genres: [ "electro", "dub" ])
219
+ Band.create!(genres: %w[electro dub])
243
220
  end
244
221
 
245
222
  let!(:non_match) do
246
- Band.create!(genres: [ "house" ])
223
+ Band.create!(genres: [ 'house' ])
247
224
  end
248
225
 
249
226
  let(:criteria) do
250
- Band.send(method, genres: [ "electro", "dub" ])
227
+ Band.send(method, genres: %w[electro dub])
251
228
  end
252
229
 
253
- it "returns the matching documents" do
230
+ it 'returns the matching documents' do
254
231
  expect(criteria).to eq([ match ])
255
232
  end
256
233
  end
257
234
  end
258
235
 
259
- [ :and, :all_of ].each do |method|
260
-
261
- describe "\##{method}" do
262
-
236
+ %i[and all_of].each do |method|
237
+ describe "##{method}" do
263
238
  let!(:match) do
264
- Band.create!(name: "Depeche Mode", genres: [ "electro" ])
239
+ Band.create!(name: 'Depeche Mode', genres: [ 'electro' ])
265
240
  end
266
241
 
267
242
  let!(:non_match) do
268
- Band.create!(genres: [ "house" ])
243
+ Band.create!(genres: [ 'house' ])
269
244
  end
270
245
 
271
246
  let(:criteria) do
272
- Band.send(method, { genres: "electro" }, { name: "Depeche Mode" })
247
+ Band.send(method, { genres: 'electro' }, { name: 'Depeche Mode' })
273
248
  end
274
249
 
275
- it "returns the matching documents" do
250
+ it 'returns the matching documents' do
276
251
  expect(criteria).to eq([ match ])
277
252
  end
278
253
  end
279
254
  end
280
255
 
281
- describe "#as_json" do
282
-
256
+ describe '#as_json' do
283
257
  let!(:band) do
284
- Band.create!(name: "Depeche Mode")
258
+ Band.create!(name: 'Depeche Mode')
285
259
  end
286
260
 
287
261
  let(:criteria) do
288
- Band.where(name: "Depeche Mode")
262
+ Band.where(name: 'Depeche Mode')
289
263
  end
290
264
 
291
- it "returns the criteria as a json hash" do
265
+ it 'returns the criteria as a json hash' do
292
266
  expect(criteria.as_json).to eq([ band.serializable_hash.as_json ])
293
267
  end
294
268
  end
295
269
 
296
- describe "#between" do
297
-
270
+ describe '#between' do
298
271
  let!(:match) do
299
272
  Band.create!(member_count: 3)
300
273
  end
@@ -307,69 +280,65 @@ describe Mongoid::Criteria do
307
280
  Band.between(member_count: 1..5)
308
281
  end
309
282
 
310
- it "returns the matching documents" do
283
+ it 'returns the matching documents' do
311
284
  expect(criteria).to eq([ match ])
312
285
  end
313
286
  end
314
287
 
315
- [ :build, :new ].each do |method|
316
-
288
+ %i[build new].each do |method|
317
289
  describe "##{method}" do
318
-
319
290
  let(:criteria) do
320
- Band.where(name: "Depeche Mode")
291
+ Band.where(name: 'Depeche Mode')
321
292
  end
322
293
 
323
- context "when provided valid attributes" do
324
-
294
+ context 'when provided valid attributes' do
325
295
  let(:band) do
326
- criteria.send(method, genres: [ "electro" ])
296
+ criteria.send(method, genres: [ 'electro' ])
327
297
  end
328
298
 
329
- it "returns the new document" do
299
+ it 'returns the new document' do
330
300
  expect(band).to be_new_record
331
301
  end
332
302
 
333
- it "sets the criteria attributes" do
334
- expect(band.name).to eq("Depeche Mode")
303
+ it 'sets the criteria attributes' do
304
+ expect(band.name).to eq('Depeche Mode')
335
305
  end
336
306
 
337
- it "sets the attributes passed to build" do
338
- expect(band.genres).to eq([ "electro" ])
307
+ it 'sets the attributes passed to build' do
308
+ expect(band.genres).to eq([ 'electro' ])
339
309
  end
340
310
  end
341
311
 
342
- context "when provided a block" do
343
- context "when provided valid attributes" do
312
+ context 'when provided a block' do
313
+ context 'when provided valid attributes' do
344
314
  let(:band) do
345
315
  criteria.send(method) do |c|
346
- c.genres = [ "electro" ]
316
+ c.genres = [ 'electro' ]
347
317
  end
348
318
  end
349
319
 
350
- it "returns the new document" do
320
+ it 'returns the new document' do
351
321
  expect(band).to be_new_record
352
322
  end
353
323
 
354
- it "sets the criteria attributes" do
355
- expect(band.name).to eq("Depeche Mode")
324
+ it 'sets the criteria attributes' do
325
+ expect(band.name).to eq('Depeche Mode')
356
326
  end
357
327
 
358
- it "sets the attributes passed to build" do
359
- expect(band.genres).to eq([ "electro" ])
328
+ it 'sets the attributes passed to build' do
329
+ expect(band.genres).to eq([ 'electro' ])
360
330
  end
361
331
  end
362
332
  end
363
333
  end
364
334
  end
365
335
 
366
- describe "#cache" do
367
-
336
+ describe '#cache' do
368
337
  let!(:person) do
369
338
  Person.create!
370
339
  end
371
340
 
372
- context "when the query cache is enabled" do
341
+ context 'when the query cache is enabled' do
373
342
  query_cache_enabled
374
343
 
375
344
  let(:criteria) do
@@ -377,19 +346,17 @@ describe Mongoid::Criteria do
377
346
  end
378
347
 
379
348
  before do
380
- criteria.each {}
349
+ criteria.to_a
381
350
  end
382
351
 
383
- it "does not hit the database after first iteration" do
352
+ it 'does not hit the database after first iteration' do
384
353
  expect_no_queries do
385
- criteria.each do |doc|
386
- expect(doc).to eq(person)
387
- end
354
+ expect(criteria).to all(eq(person))
388
355
  end
389
356
  end
390
357
  end
391
358
 
392
- context "when the criteria is eager loading" do
359
+ context 'when the criteria is eager loading' do
393
360
  query_cache_enabled
394
361
 
395
362
  let(:criteria) do
@@ -397,29 +364,28 @@ describe Mongoid::Criteria do
397
364
  end
398
365
 
399
366
  before do
400
- criteria.each {}
367
+ criteria.to_a
401
368
  end
402
369
 
403
- it "does not hit the database after first iteration" do
370
+ it 'does not hit the database after first iteration' do
404
371
  expect_no_queries do
405
- criteria.each do |doc|
406
- expect(doc).to eq(person)
407
- end
372
+ expect(criteria).to all(eq(person))
408
373
  end
409
374
  end
410
375
  end
411
376
  end
412
377
 
413
- [ :clone, :dup ].each do |method|
414
-
415
- describe "\##{method}" do
416
-
378
+ %i[clone dup].each do |method|
379
+ describe "##{method}" do
417
380
  let(:band) do
418
381
  Band.new
419
382
  end
383
+ let(:clone) do
384
+ criteria.send(method)
385
+ end
420
386
 
421
387
  let(:criteria) do
422
- Band.where(name: "Depeche Mode").asc(:name).includes(:records).read(mode: :secondary)
388
+ Band.where(name: 'Depeche Mode').asc(:name).includes(:records).read(mode: :secondary)
423
389
  end
424
390
 
425
391
  before do
@@ -427,51 +393,47 @@ describe Mongoid::Criteria do
427
393
  criteria.context
428
394
  end
429
395
 
430
- let(:clone) do
431
- criteria.send(method)
432
- end
433
-
434
- it "contains an equal selector" do
435
- expect(clone.selector).to eq({ "name" => "Depeche Mode" })
396
+ it 'contains an equal selector' do
397
+ expect(clone.selector).to eq({ 'name' => 'Depeche Mode' })
436
398
  end
437
399
 
438
- it "clones the selector" do
439
- expect(clone.selector).to_not equal(criteria.selector)
400
+ it 'clones the selector' do
401
+ expect(clone.selector).not_to equal(criteria.selector)
440
402
  end
441
403
 
442
- it "contains equal options" do
443
- expect(clone.options).to eq({ sort: { "name" => 1 }, read: { mode: :secondary } })
404
+ it 'contains equal options' do
405
+ expect(clone.options).to eq({ sort: { 'name' => 1 }, read: { mode: :secondary } })
444
406
  end
445
407
 
446
- it "clones the options" do
447
- expect(clone.options).to_not equal(criteria.options)
408
+ it 'clones the options' do
409
+ expect(clone.options).not_to equal(criteria.options)
448
410
  end
449
411
 
450
- it "contains equal inclusions" do
451
- expect(clone.inclusions).to eq([ Band.relations["records"] ])
412
+ it 'contains equal inclusions' do
413
+ expect(clone.inclusions).to eq([ Band.relations['records'] ])
452
414
  end
453
415
 
454
- it "clones the inclusions" do
455
- expect(clone.inclusions).to_not equal(criteria.inclusions)
416
+ it 'clones the inclusions' do
417
+ expect(clone.inclusions).not_to equal(criteria.inclusions)
456
418
  end
457
419
 
458
- it "contains equal documents" do
420
+ it 'contains equal documents' do
459
421
  expect(clone.documents).to eq([ band ])
460
422
  end
461
423
 
462
- it "clones the documents" do
463
- expect(clone.documents).to_not equal(criteria.documents)
424
+ it 'clones the documents' do
425
+ expect(clone.documents).not_to equal(criteria.documents)
464
426
  end
465
427
 
466
- it "contains equal scoping options" do
428
+ it 'contains equal scoping options' do
467
429
  expect(clone.scoping_options).to eq([ nil, nil ])
468
430
  end
469
431
 
470
- it "clones the scoping options" do
471
- expect(clone.scoping_options).to_not equal(criteria.scoping_options)
432
+ it 'clones the scoping options' do
433
+ expect(clone.scoping_options).not_to equal(criteria.scoping_options)
472
434
  end
473
435
 
474
- it "sets the context to nil" do
436
+ it 'sets the context to nil' do
475
437
  expect(clone.instance_variable_get(:@context)).to be_nil
476
438
  end
477
439
 
@@ -481,57 +443,51 @@ describe Mongoid::Criteria do
481
443
  end
482
444
  end
483
445
 
484
- describe "#context" do
485
-
486
- context "when the model is embedded" do
487
-
446
+ describe '#context' do
447
+ context 'when the model is embedded' do
488
448
  let(:criteria) do
489
449
  described_class.new(Record) do |criteria|
490
450
  criteria.embedded = true
491
451
  end
492
452
  end
493
453
 
494
- it "returns the embedded context" do
454
+ it 'returns the embedded context' do
495
455
  expect(criteria.context).to be_a(Mongoid::Contextual::Memory)
496
456
  end
497
457
  end
498
458
 
499
- context "when the model is not embedded" do
500
-
459
+ context 'when the model is not embedded' do
501
460
  let(:criteria) do
502
461
  described_class.new(Band)
503
462
  end
504
463
 
505
- it "returns the mongo context" do
464
+ it 'returns the mongo context' do
506
465
  expect(criteria.context).to be_a(Mongoid::Contextual::Mongo)
507
466
  end
508
467
  end
509
468
  end
510
469
 
511
- describe "#delete" do
512
-
470
+ describe '#delete' do
513
471
  let(:depeche) do
514
- Band.create!(name: "Depeche Mode")
472
+ Band.create!(name: 'Depeche Mode')
515
473
  end
516
474
 
517
475
  let(:tool) do
518
- Band.create!(name: "Tool")
476
+ Band.create!(name: 'Tool')
519
477
  end
520
478
 
521
- context "when no selector is provided" do
522
-
479
+ context 'when no selector is provided' do
523
480
  before do
524
481
  Band.all.delete
525
482
  end
526
483
 
527
- it "deletes all the documents from the database" do
484
+ it 'deletes all the documents from the database' do
528
485
  expect(Band.count).to eq(0)
529
486
  end
530
487
  end
531
488
  end
532
489
 
533
- describe "#documents" do
534
-
490
+ describe '#documents' do
535
491
  let(:band) do
536
492
  Band.new
537
493
  end
@@ -542,13 +498,12 @@ describe Mongoid::Criteria do
542
498
  end
543
499
  end
544
500
 
545
- it "returns the documents" do
501
+ it 'returns the documents' do
546
502
  expect(criteria.documents).to eq([ band ])
547
503
  end
548
504
  end
549
505
 
550
- describe "#documents=" do
551
-
506
+ describe '#documents=' do
552
507
  let(:band) do
553
508
  Band.new
554
509
  end
@@ -561,119 +516,107 @@ describe Mongoid::Criteria do
561
516
  criteria.documents = [ band ]
562
517
  end
563
518
 
564
- it "sets the documents" do
519
+ it 'sets the documents' do
565
520
  expect(criteria.documents).to eq([ band ])
566
521
  end
567
522
  end
568
523
 
569
- describe "#each" do
570
-
524
+ describe '#each' do
571
525
  let!(:band) do
572
- Band.create!(name: "Depeche Mode")
526
+ Band.create!(name: 'Depeche Mode')
573
527
  end
574
528
 
575
- context "when provided a block" do
576
-
529
+ context 'when provided a block' do
577
530
  let(:criteria) do
578
- Band.where(name: "Depeche Mode")
531
+ Band.where(name: 'Depeche Mode')
579
532
  end
580
533
 
581
- it "iterates over the matching documents" do
582
- criteria.each do |doc|
583
- expect(doc).to eq(band)
584
- end
534
+ it 'iterates over the matching documents' do
535
+ expect(criteria).to all(eq(band))
585
536
  end
586
537
  end
587
538
  end
588
539
 
589
- describe "#elem_match" do
590
-
540
+ describe '#elem_match' do
591
541
  let!(:match) do
592
- Band.create!(name: "Depeche Mode").tap do |band|
542
+ Band.create!(name: 'Depeche Mode').tap do |band|
593
543
  r = band.records
594
- r.create!(name: "101")
544
+ r.create!(name: '101')
595
545
  end
596
546
  end
597
547
 
598
548
  let!(:non_match) do
599
- Band.create!(genres: [ "house" ])
549
+ Band.create!(genres: [ 'house' ])
600
550
  end
601
551
 
602
552
  let(:criteria) do
603
- Band.elem_match(records: { name: "101" })
553
+ Band.elem_match(records: { name: '101' })
604
554
  end
605
555
 
606
- it "returns the matching documents" do
556
+ it 'returns the matching documents' do
607
557
  expect(criteria).to eq([ match ])
608
558
  end
609
559
  end
610
560
 
611
- describe "#embedded?" do
612
-
561
+ describe '#embedded?' do
613
562
  let(:person) do
614
563
  Person.new
615
564
  end
616
565
 
617
- context "when the criteria is embedded" do
618
-
566
+ context 'when the criteria is embedded' do
619
567
  let(:criteria) do
620
- person.addresses.where(street: "hobrecht")
568
+ person.addresses.where(street: 'hobrecht')
621
569
  end
622
570
 
623
- it "returns true" do
571
+ it 'returns true' do
624
572
  expect(criteria).to be_embedded
625
573
  end
626
574
  end
627
575
 
628
- context "when the criteria is not embedded" do
629
-
576
+ context 'when the criteria is not embedded' do
630
577
  let(:criteria) do
631
578
  Person.where(active: true)
632
579
  end
633
580
 
634
- it "returns false" do
635
- expect(criteria).to_not be_embedded
581
+ it 'returns false' do
582
+ expect(criteria).not_to be_embedded
636
583
  end
637
584
  end
638
585
  end
639
586
 
640
- describe "#empty?" do
641
-
642
- context "when matching documents exist" do
643
-
587
+ describe '#empty?' do
588
+ context 'when matching documents exist' do
644
589
  let!(:match) do
645
- Band.create!(name: "Depeche Mode")
590
+ Band.create!(name: 'Depeche Mode')
646
591
  end
647
592
 
648
593
  let(:criteria) do
649
- Band.where(name: "Depeche Mode")
594
+ Band.where(name: 'Depeche Mode')
650
595
  end
651
596
 
652
- it "returns false" do
653
- expect(criteria).to_not be_empty
597
+ it 'returns false' do
598
+ expect(criteria).not_to be_empty
654
599
  end
655
600
  end
656
601
 
657
- context "when no matching documents exist" do
658
-
602
+ context 'when no matching documents exist' do
659
603
  let!(:nonmatch) do
660
- Band.create!(name: "New Order")
604
+ Band.create!(name: 'New Order')
661
605
  end
662
606
 
663
607
  let(:criteria) do
664
- Band.where(name: "Depeche Mode")
608
+ Band.where(name: 'Depeche Mode')
665
609
  end
666
610
 
667
- it "returns true" do
611
+ it 'returns true' do
668
612
  expect(criteria).to be_empty
669
613
  end
670
614
  end
671
615
  end
672
616
 
673
- describe "#exists" do
674
-
617
+ describe '#exists' do
675
618
  let!(:match) do
676
- Band.create!(name: "Depeche Mode")
619
+ Band.create!(name: 'Depeche Mode')
677
620
  end
678
621
 
679
622
  let!(:non_match) do
@@ -684,99 +627,91 @@ describe Mongoid::Criteria do
684
627
  Band.exists(name: true)
685
628
  end
686
629
 
687
- it "returns the matching documents" do
630
+ it 'returns the matching documents' do
688
631
  expect(criteria).to eq([ match ])
689
632
  end
690
633
  end
691
634
 
692
- describe "#exists?" do
693
-
694
- context "when matching documents exist" do
695
-
635
+ describe '#exists?' do
636
+ context 'when matching documents exist' do
696
637
  let!(:match) do
697
- Band.create!(name: "Depeche Mode")
638
+ Band.create!(name: 'Depeche Mode')
698
639
  end
699
640
 
700
641
  let(:criteria) do
701
- Band.where(name: "Depeche Mode")
642
+ Band.where(name: 'Depeche Mode')
702
643
  end
703
644
 
704
- it "returns true" do
645
+ it 'returns true' do
705
646
  expect(criteria.exists?).to be true
706
647
  end
707
648
  end
708
649
 
709
- context "when no matching documents exist" do
710
-
650
+ context 'when no matching documents exist' do
711
651
  let(:criteria) do
712
- Band.where(name: "Depeche Mode")
652
+ Band.where(name: 'Depeche Mode')
713
653
  end
714
654
 
715
- it "returns false" do
655
+ it 'returns false' do
716
656
  expect(criteria.exists?).to be false
717
657
  end
718
658
  end
719
659
  end
720
660
 
721
- describe "#explain" do
722
-
661
+ describe '#explain' do
723
662
  let(:criteria) do
724
- Band.where(name: "Depeche Mode")
663
+ Band.where(name: 'Depeche Mode')
725
664
  end
726
665
 
727
- it "returns the criteria explain path" do
728
- expect(criteria.explain).to_not be_empty
666
+ it 'returns the criteria explain path' do
667
+ expect(criteria.explain).not_to be_empty
729
668
  end
730
669
  end
731
670
 
732
- describe "#extract_id" do
733
-
671
+ describe '#extract_id' do
734
672
  let(:id) do
735
673
  BSON::ObjectId.new
736
674
  end
737
675
 
738
- context "when an id exists" do
739
-
676
+ context 'when an id exists' do
740
677
  let(:criteria) do
741
678
  described_class.new(Band) do |criteria|
742
679
  criteria.selector[:id] = id
743
680
  end
744
681
  end
745
682
 
746
- it "returns the id" do
683
+ it 'returns the id' do
747
684
  expect(criteria.extract_id).to eq(id)
748
685
  end
749
686
  end
750
687
 
751
- context "when an _id exists" do
752
-
688
+ context 'when an _id exists' do
753
689
  let(:criteria) do
754
690
  described_class.new(Band) do |criteria|
755
691
  criteria.selector[:_id] = id
756
692
  end
757
693
  end
758
694
 
759
- it "returns the _id" do
695
+ it 'returns the _id' do
760
696
  expect(criteria.extract_id).to eq(id)
761
697
  end
762
698
  end
763
699
  end
764
700
 
765
- describe "#field_list" do
766
-
767
- context "when using the default discriminator key" do
701
+ describe '#field_list' do
702
+ context 'when using the default discriminator key' do
768
703
  let(:criteria) do
769
704
  Doctor.only(:_id)
770
705
  end
771
706
 
772
- it "returns the fields with required _id minus type" do
773
- expect(criteria.field_list).to eq([ "_id" ])
707
+ it 'returns the fields with required _id minus type' do
708
+ expect(criteria.field_list).to eq([ '_id' ])
774
709
  end
775
710
  end
776
711
 
777
- context "when using a custom discriminator key" do
712
+ context 'when using a custom discriminator key' do
778
713
  before do
779
- Person.discriminator_key = "dkey"
714
+ Person.discriminator_key = 'dkey'
780
715
  end
781
716
 
782
717
  after do
@@ -787,228 +722,215 @@ describe Mongoid::Criteria do
787
722
  Doctor.only(:_id, :_type)
788
723
  end
789
724
 
790
- it "returns the fields with type without dkey" do
791
- expect(criteria.field_list).to eq([ "_id", "_type" ])
725
+ it 'returns the fields with type without dkey' do
726
+ expect(criteria.field_list).to eq(%w[_id _type])
792
727
  end
793
728
  end
794
729
  end
795
730
 
796
- describe "#find" do
731
+ describe '#find' do
797
732
  let!(:depeche) do
798
- Band.create!(name: "Depeche Mode")
733
+ Band.create!(name: 'Depeche Mode')
799
734
  end
800
735
 
801
736
  let(:criteria) do
802
- Band.where(name: "Depeche Mode")
737
+ Band.where(name: 'Depeche Mode')
803
738
  end
804
739
 
805
- context "when given a block" do
806
- it "behaves as Enumerable" do
807
- result = criteria.find { |c| c.name == "Depeche Mode" }
740
+ context 'when given a block' do
741
+ it 'behaves as Enumerable' do
742
+ result = criteria.find { |c| c.name == 'Depeche Mode' }
808
743
  expect(result).to eq(depeche)
809
744
  end
810
745
  end
811
746
 
812
- context "when given a Proc and a block" do
813
- it "behaves as Enumerable" do
814
- result = criteria.find(-> {"default"}) { |c| c.name == "Not Depeche Mode" }
815
- expect(result).to eq("default")
747
+ context 'when given a Proc and a block' do
748
+ it 'behaves as Enumerable' do
749
+ result = criteria.find(-> { 'default' }) { |c| c.name == 'Not Depeche Mode' }
750
+ expect(result).to eq('default')
816
751
  end
817
752
  end
818
753
 
819
- context "when given a Proc without a block" do
820
- it "raises an error" do
754
+ context 'when given a Proc without a block' do
755
+ it 'raises an error' do
821
756
  lambda do
822
- criteria.find(-> {"default"})
823
- # Proc is not serializable to a BSON type
757
+ criteria.find(-> { 'default' })
758
+ # Proc is not serializable to a BSON type
824
759
  end.should raise_error(BSON::Error::UnserializableClass)
825
760
  end
826
761
  end
827
762
 
828
- context "when given an id" do
829
- it "behaves as Findable" do
763
+ context 'when given an id' do
764
+ it 'behaves as Findable' do
830
765
  result = criteria.find(depeche.id)
831
766
  expect(result).to eq(depeche)
832
767
  end
833
768
  end
834
769
  end
835
770
 
836
- describe "#find_one_and_update" do
837
-
771
+ describe '#find_one_and_update' do
838
772
  let!(:depeche) do
839
- Band.create!(name: "Depeche Mode")
773
+ Band.create!(name: 'Depeche Mode')
840
774
  end
841
775
 
842
776
  let!(:tool) do
843
- Band.create!(name: "Tool")
777
+ Band.create!(name: 'Tool')
844
778
  end
845
779
 
846
- context "when the selector matches" do
847
-
848
- context "when the identity map is enabled" do
849
-
850
- context "when returning the updated document" do
851
-
780
+ context 'when the selector matches' do
781
+ context 'when the identity map is enabled' do
782
+ context 'when returning the updated document' do
852
783
  let(:criteria) do
853
- Band.where(name: "Depeche Mode")
784
+ Band.where(name: 'Depeche Mode')
854
785
  end
855
786
 
856
787
  let(:result) do
857
- criteria.find_one_and_update({ "$inc" => { likes: 1 }}, return_document: :after)
788
+ criteria.find_one_and_update({ '$inc' => { likes: 1 } }, return_document: :after)
858
789
  end
859
790
 
860
- it "returns the first matching document" do
791
+ it 'returns the first matching document' do
861
792
  expect(result).to eq(depeche)
862
793
  end
863
794
  end
864
795
 
865
- context "when not returning the updated document" do
866
-
796
+ context 'when not returning the updated document' do
867
797
  let(:criteria) do
868
- Band.where(name: "Depeche Mode")
798
+ Band.where(name: 'Depeche Mode')
869
799
  end
870
800
 
871
801
  let!(:result) do
872
- criteria.find_one_and_update("$inc" => { likes: 1 })
802
+ criteria.find_one_and_update('$inc' => { likes: 1 })
873
803
  end
874
804
 
875
805
  before do
876
806
  depeche.reload
877
807
  end
878
808
 
879
- it "returns the first matching document" do
809
+ it 'returns the first matching document' do
880
810
  expect(result).to eq(depeche)
881
811
  end
882
812
  end
883
813
  end
884
814
 
885
- context "when not providing options" do
886
-
815
+ context 'when not providing options' do
887
816
  let(:criteria) do
888
- Band.where(name: "Depeche Mode")
817
+ Band.where(name: 'Depeche Mode')
889
818
  end
890
819
 
891
820
  let!(:result) do
892
- criteria.find_one_and_update("$inc" => { likes: 1 })
821
+ criteria.find_one_and_update('$inc' => { likes: 1 })
893
822
  end
894
823
 
895
- it "returns the first matching document" do
824
+ it 'returns the first matching document' do
896
825
  expect(result).to eq(depeche)
897
826
  end
898
827
 
899
- it "updates the document in the database" do
828
+ it 'updates the document in the database' do
900
829
  expect(depeche.reload.likes).to eq(1)
901
830
  end
902
831
  end
903
832
 
904
- context "when sorting" do
905
-
833
+ context 'when sorting' do
906
834
  let(:criteria) do
907
835
  Band.desc(:name)
908
836
  end
909
837
 
910
838
  let!(:result) do
911
- criteria.find_one_and_update("$inc" => { likes: 1 })
839
+ criteria.find_one_and_update('$inc' => { likes: 1 })
912
840
  end
913
841
 
914
- it "returns the first matching document" do
842
+ it 'returns the first matching document' do
915
843
  expect(result).to eq(tool)
916
844
  end
917
845
 
918
- it "updates the document in the database" do
846
+ it 'updates the document in the database' do
919
847
  expect(tool.reload.likes).to eq(1)
920
848
  end
921
849
  end
922
850
 
923
- context "when limiting fields" do
924
-
851
+ context 'when limiting fields' do
925
852
  let(:criteria) do
926
853
  Band.only(:_id)
927
854
  end
928
855
 
929
856
  let!(:result) do
930
- criteria.find_one_and_update("$inc" => { likes: 1 })
857
+ criteria.find_one_and_update('$inc' => { likes: 1 })
931
858
  end
932
859
 
933
- it "returns the first matching document" do
860
+ it 'returns the first matching document' do
934
861
  expect(result).to eq(depeche)
935
862
  end
936
863
 
937
- it "limits the returned fields" do
864
+ it 'limits the returned fields' do
938
865
  expect(result.name).to be_nil
939
866
  end
940
867
 
941
- it "updates the document in the database" do
868
+ it 'updates the document in the database' do
942
869
  expect(depeche.reload.likes).to eq(1)
943
870
  end
944
871
  end
945
872
 
946
- context "when returning new" do
947
-
873
+ context 'when returning new' do
948
874
  let(:criteria) do
949
- Band.where(name: "Depeche Mode")
875
+ Band.where(name: 'Depeche Mode')
950
876
  end
951
877
 
952
878
  let!(:result) do
953
- criteria.find_one_and_update({ "$inc" => { likes: 1 }}, return_document: :after)
879
+ criteria.find_one_and_update({ '$inc' => { likes: 1 } }, return_document: :after)
954
880
  end
955
881
 
956
- it "returns the first matching document" do
882
+ it 'returns the first matching document' do
957
883
  expect(result).to eq(depeche)
958
884
  end
959
885
 
960
- it "returns the updated document" do
886
+ it 'returns the updated document' do
961
887
  expect(result.likes).to eq(1)
962
888
  end
963
889
  end
964
890
 
965
- context "when removing" do
966
-
891
+ context 'when removing' do
967
892
  let(:criteria) do
968
- Band.where(name: "Depeche Mode")
893
+ Band.where(name: 'Depeche Mode')
969
894
  end
970
895
 
971
896
  let!(:result) do
972
897
  criteria.find_one_and_delete
973
898
  end
974
899
 
975
- it "returns the first matching document" do
900
+ it 'returns the first matching document' do
976
901
  expect(result).to eq(depeche)
977
902
  end
978
903
 
979
- it "deletes the document from the database" do
980
- expect {
904
+ it 'deletes the document from the database' do
905
+ expect do
981
906
  depeche.reload
982
- }.to raise_error(Mongoid::Errors::DocumentNotFound, /Document\(s\) not found for class Band with id\(s\)/)
907
+ end.to raise_error(Mongoid::Errors::DocumentNotFound, /Document\(s\) not found for class Band with id\(s\)/)
983
908
  end
984
909
  end
985
910
  end
986
911
 
987
- context "when the selector does not match" do
988
-
912
+ context 'when the selector does not match' do
989
913
  let(:criteria) do
990
- Band.where(name: "Placebo")
914
+ Band.where(name: 'Placebo')
991
915
  end
992
916
 
993
917
  let(:result) do
994
- criteria.find_one_and_update("$inc" => { likes: 1 })
918
+ criteria.find_one_and_update('$inc' => { likes: 1 })
995
919
  end
996
920
 
997
- context "without upsert" do
998
-
921
+ context 'without upsert' do
999
922
  let(:result) do
1000
- criteria.find_one_and_update("$inc" => { likes: 1 })
923
+ criteria.find_one_and_update('$inc' => { likes: 1 })
1001
924
  end
1002
925
 
1003
- it "returns nil" do
926
+ it 'returns nil' do
1004
927
  expect(result).to be_nil
1005
928
  end
1006
929
  end
1007
930
  end
1008
931
  end
1009
932
 
1010
- describe "#freeze" do
1011
-
933
+ describe '#freeze' do
1012
934
  let(:criteria) do
1013
935
  Band.all
1014
936
  end
@@ -1017,21 +939,20 @@ describe Mongoid::Criteria do
1017
939
  criteria.freeze
1018
940
  end
1019
941
 
1020
- it "freezes the criteria" do
942
+ it 'freezes the criteria' do
1021
943
  expect(criteria).to be_frozen
1022
944
  end
1023
945
 
1024
- it "initializes inclusions" do
946
+ it 'initializes inclusions' do
1025
947
  expect(criteria.inclusions).to be_empty
1026
948
  end
1027
949
 
1028
- it "initializes the context" do
1029
- expect(criteria.context).to_not be_nil
950
+ it 'initializes the context' do
951
+ expect(criteria.context).not_to be_nil
1030
952
  end
1031
953
  end
1032
954
 
1033
- describe "#eq" do
1034
-
955
+ describe '#eq' do
1035
956
  let!(:match) do
1036
957
  Band.create(member_count: 5)
1037
958
  end
@@ -1044,13 +965,12 @@ describe Mongoid::Criteria do
1044
965
  Band.eq(member_count: 5)
1045
966
  end
1046
967
 
1047
- it "returns the matching documents" do
968
+ it 'returns the matching documents' do
1048
969
  expect(criteria).to eq([ match ])
1049
970
  end
1050
971
  end
1051
972
 
1052
- describe "#gt" do
1053
-
973
+ describe '#gt' do
1054
974
  let!(:match) do
1055
975
  Band.create!(member_count: 5)
1056
976
  end
@@ -1063,13 +983,12 @@ describe Mongoid::Criteria do
1063
983
  Band.gt(member_count: 4)
1064
984
  end
1065
985
 
1066
- it "returns the matching documents" do
986
+ it 'returns the matching documents' do
1067
987
  expect(criteria).to eq([ match ])
1068
988
  end
1069
989
  end
1070
990
 
1071
- describe "#gte" do
1072
-
991
+ describe '#gte' do
1073
992
  let!(:match) do
1074
993
  Band.create!(member_count: 5)
1075
994
  end
@@ -1082,36 +1001,32 @@ describe Mongoid::Criteria do
1082
1001
  Band.gte(member_count: 5)
1083
1002
  end
1084
1003
 
1085
- it "returns the matching documents" do
1004
+ it 'returns the matching documents' do
1086
1005
  expect(criteria).to eq([ match ])
1087
1006
  end
1088
1007
  end
1089
1008
 
1090
- [ :in, :any_in ].each do |method|
1091
-
1092
- describe "\##{method}" do
1093
-
1094
- context "when querying on a normal field" do
1095
-
1009
+ %i[in any_in].each do |method|
1010
+ describe "##{method}" do
1011
+ context 'when querying on a normal field' do
1096
1012
  let!(:match) do
1097
- Band.create!(genres: [ "electro", "dub" ])
1013
+ Band.create!(genres: %w[electro dub])
1098
1014
  end
1099
1015
 
1100
1016
  let!(:non_match) do
1101
- Band.create!(genres: [ "house" ])
1017
+ Band.create!(genres: [ 'house' ])
1102
1018
  end
1103
1019
 
1104
1020
  let(:criteria) do
1105
- Band.send(method, genres: [ "dub" ])
1021
+ Band.send(method, genres: [ 'dub' ])
1106
1022
  end
1107
1023
 
1108
- it "returns the matching documents" do
1024
+ it 'returns the matching documents' do
1109
1025
  expect(criteria).to eq([ match ])
1110
1026
  end
1111
1027
  end
1112
1028
 
1113
- context "when querying on a foreign key" do
1114
-
1029
+ context 'when querying on a foreign key' do
1115
1030
  let(:id) do
1116
1031
  BSON::ObjectId.new
1117
1032
  end
@@ -1120,43 +1035,38 @@ describe Mongoid::Criteria do
1120
1035
  Person.create!(preference_ids: [ id ])
1121
1036
  end
1122
1037
 
1123
- context "when providing valid ids" do
1124
-
1038
+ context 'when providing valid ids' do
1125
1039
  let(:criteria) do
1126
1040
  Person.send(method, preference_ids: [ id ])
1127
1041
  end
1128
1042
 
1129
- it "returns the matching documents" do
1043
+ it 'returns the matching documents' do
1130
1044
  expect(criteria).to eq([ match_one ])
1131
1045
  end
1132
1046
  end
1133
1047
 
1134
- context "when providing empty strings" do
1135
-
1048
+ context 'when providing empty strings' do
1136
1049
  let(:criteria) do
1137
- Person.send(method, preference_ids: [ id, "" ])
1050
+ Person.send(method, preference_ids: [ id, '' ])
1138
1051
  end
1139
1052
 
1140
- it "returns the matching documents" do
1053
+ it 'returns the matching documents' do
1141
1054
  expect(criteria).to eq([ match_one ])
1142
1055
  end
1143
1056
  end
1144
1057
 
1145
- context "when providing nils" do
1146
-
1147
- context "when the relation is a many to many" do
1148
-
1058
+ context 'when providing nils' do
1059
+ context 'when the relation is a many to many' do
1149
1060
  let(:criteria) do
1150
1061
  Person.send(method, preference_ids: [ id, nil ])
1151
1062
  end
1152
1063
 
1153
- it "returns the matching documents" do
1064
+ it 'returns the matching documents' do
1154
1065
  expect(criteria).to eq([ match_one ])
1155
1066
  end
1156
1067
  end
1157
1068
 
1158
- context "when the relation is a one to one" do
1159
-
1069
+ context 'when the relation is a one to one' do
1160
1070
  let!(:game) do
1161
1071
  Game.create!
1162
1072
  end
@@ -1165,7 +1075,7 @@ describe Mongoid::Criteria do
1165
1075
  Game.send(method, person_id: [ nil ])
1166
1076
  end
1167
1077
 
1168
- it "returns the matching documents" do
1078
+ it 'returns the matching documents' do
1169
1079
  expect(criteria).to eq([ game ])
1170
1080
  end
1171
1081
  end
@@ -1174,27 +1084,25 @@ describe Mongoid::Criteria do
1174
1084
  end
1175
1085
  end
1176
1086
 
1177
- describe "#initialize" do
1178
-
1087
+ describe '#initialize' do
1179
1088
  let(:criteria) do
1180
1089
  described_class.new(Band)
1181
1090
  end
1182
1091
 
1183
- it "sets the class" do
1092
+ it 'sets the class' do
1184
1093
  expect(criteria.klass).to eq(Band)
1185
1094
  end
1186
1095
 
1187
- it "sets the aliased fields" do
1096
+ it 'sets the aliased fields' do
1188
1097
  expect(criteria.aliased_fields).to eq(Band.aliased_fields)
1189
1098
  end
1190
1099
 
1191
- it "sets the serializers" do
1100
+ it 'sets the serializers' do
1192
1101
  expect(criteria.serializers).to eq(Band.fields)
1193
1102
  end
1194
1103
  end
1195
1104
 
1196
- describe "#lt" do
1197
-
1105
+ describe '#lt' do
1198
1106
  let!(:match) do
1199
1107
  Band.create!(member_count: 1)
1200
1108
  end
@@ -1207,13 +1115,12 @@ describe Mongoid::Criteria do
1207
1115
  Band.lt(member_count: 4)
1208
1116
  end
1209
1117
 
1210
- it "returns the matching documents" do
1118
+ it 'returns the matching documents' do
1211
1119
  expect(criteria).to eq([ match ])
1212
1120
  end
1213
1121
  end
1214
1122
 
1215
- describe "#lte" do
1216
-
1123
+ describe '#lte' do
1217
1124
  let!(:match) do
1218
1125
  Band.create!(member_count: 4)
1219
1126
  end
@@ -1226,22 +1133,21 @@ describe Mongoid::Criteria do
1226
1133
  Band.lte(member_count: 4)
1227
1134
  end
1228
1135
 
1229
- it "returns the matching documents" do
1136
+ it 'returns the matching documents' do
1230
1137
  expect(criteria).to eq([ match ])
1231
1138
  end
1232
1139
  end
1233
1140
 
1234
- describe "#map_reduce" do
1235
-
1141
+ describe '#map_reduce' do
1236
1142
  let(:map) do
1237
- %Q{
1143
+ %{
1238
1144
  function() {
1239
1145
  emit(this.name, { likes: this.likes });
1240
1146
  }}
1241
1147
  end
1242
1148
 
1243
1149
  let(:reduce) do
1244
- %Q{
1150
+ %{
1245
1151
  function(key, values) {
1246
1152
  var result = { likes: 0 };
1247
1153
  values.forEach(function(value) {
@@ -1252,72 +1158,64 @@ describe Mongoid::Criteria do
1252
1158
  end
1253
1159
 
1254
1160
  let!(:depeche_mode) do
1255
- Band.create!(name: "Depeche Mode", likes: 200)
1161
+ Band.create!(name: 'Depeche Mode', likes: 200)
1256
1162
  end
1257
1163
 
1258
1164
  let!(:tool) do
1259
- Band.create!(name: "Tool", likes: 100)
1165
+ Band.create!(name: 'Tool', likes: 100)
1260
1166
  end
1261
1167
 
1262
- context "when no timeout options are provided" do
1263
-
1168
+ context 'when no timeout options are provided' do
1264
1169
  let(:map_reduce) do
1265
1170
  Band.limit(2).map_reduce(map, reduce).out(inline: 1)
1266
1171
  end
1267
1172
 
1268
- it "returns the map/reduce results" do
1173
+ it 'returns the map/reduce results' do
1269
1174
  expect(map_reduce.sort_by { |doc| doc['_id'] }).to eq([
1270
- { "_id" => "Depeche Mode", "value" => { "likes" => 200 }},
1271
- { "_id" => "Tool", "value" => { "likes" => 100 }}
1272
- ])
1175
+ { '_id' => 'Depeche Mode', 'value' => { 'likes' => 200 } },
1176
+ { '_id' => 'Tool', 'value' => { 'likes' => 100 } }
1177
+ ])
1273
1178
  end
1274
1179
  end
1275
1180
  end
1276
1181
 
1277
- describe "#max" do
1278
-
1279
- context "when provided a single field" do
1280
-
1182
+ describe '#max' do
1183
+ context 'when provided a single field' do
1281
1184
  let!(:depeche) do
1282
- Band.create!(name: "Depeche Mode", likes: 1000)
1185
+ Band.create!(name: 'Depeche Mode', likes: 1000)
1283
1186
  end
1284
1187
 
1285
1188
  let!(:tool) do
1286
- Band.create!(name: "Tool", likes: 500)
1189
+ Band.create!(name: 'Tool', likes: 500)
1287
1190
  end
1288
1191
 
1289
1192
  let(:criteria) do
1290
1193
  Band.all
1291
1194
  end
1292
1195
 
1293
- context "when provided a symbol" do
1294
-
1196
+ context 'when provided a symbol' do
1295
1197
  let(:max) do
1296
1198
  criteria.max(:likes)
1297
1199
  end
1298
1200
 
1299
- it "returns the max of the provided field" do
1201
+ it 'returns the max of the provided field' do
1300
1202
  expect(max).to eq(1000)
1301
1203
  end
1302
1204
  end
1303
1205
 
1304
- context "when provided a block" do
1305
-
1206
+ context 'when provided a block' do
1306
1207
  let(:max) do
1307
- criteria.max do |a, b|
1308
- a.likes <=> b.likes
1309
- end
1208
+ criteria.max_by(&:likes)
1310
1209
  end
1311
1210
 
1312
- it "returns the document with the max value for the field" do
1211
+ it 'returns the document with the max value for the field' do
1313
1212
  expect(max).to eq(depeche)
1314
1213
  end
1315
1214
  end
1316
1215
  end
1317
1216
  end
1318
1217
 
1319
- describe "#max_distance" do
1320
-
1218
+ describe '#max_distance' do
1321
1219
  before do
1322
1220
  Bar.create_indexes
1323
1221
  end
@@ -1334,23 +1232,21 @@ describe Mongoid::Criteria do
1334
1232
  Bar.near(location: [ 52, 13 ]).max_distance(location: 5)
1335
1233
  end
1336
1234
 
1337
- it "returns the matching documents" do
1235
+ it 'returns the matching documents' do
1338
1236
  expect(criteria).to eq([ match ])
1339
1237
  end
1340
1238
  end
1341
1239
 
1342
- describe "#merge" do
1343
-
1240
+ describe '#merge' do
1344
1241
  let(:band) do
1345
1242
  Band.new
1346
1243
  end
1347
1244
 
1348
1245
  let(:criteria) do
1349
- Band.scoped.where(name: "Depeche Mode").asc(:name)
1246
+ Band.scoped.where(name: 'Depeche Mode').asc(:name)
1350
1247
  end
1351
1248
 
1352
- context "when merging with another criteria" do
1353
-
1249
+ context 'when merging with another criteria' do
1354
1250
  let(:mergeable) do
1355
1251
  Band.includes(:records).tap do |crit|
1356
1252
  crit.documents = [ band ]
@@ -1358,83 +1254,83 @@ describe Mongoid::Criteria do
1358
1254
  end
1359
1255
 
1360
1256
  let(:association) do
1361
- Band.relations["records"]
1257
+ Band.relations['records']
1362
1258
  end
1363
1259
 
1364
1260
  let(:merged) do
1365
1261
  criteria.merge(mergeable)
1366
1262
  end
1367
1263
 
1368
- it "merges the selector" do
1369
- expect(merged.selector).to eq({ "name" => "Depeche Mode" })
1264
+ it 'merges the selector' do
1265
+ expect(merged.selector).to eq({ 'name' => 'Depeche Mode' })
1370
1266
  end
1371
1267
 
1372
- it "merges the options" do
1373
- expect(merged.options).to eq({ sort: { "name" => 1 }})
1268
+ it 'merges the options' do
1269
+ expect(merged.options).to eq({ sort: { 'name' => 1 } })
1374
1270
  end
1375
1271
 
1376
- it "merges the documents" do
1272
+ it 'merges the documents' do
1377
1273
  expect(merged.documents).to eq([ band ])
1378
1274
  end
1379
1275
 
1380
- it "merges the scoping options" do
1276
+ it 'merges the scoping options' do
1381
1277
  expect(merged.scoping_options).to eq([ nil, nil ])
1382
1278
  end
1383
1279
 
1384
- it "merges the inclusions" do
1280
+ it 'merges the inclusions' do
1385
1281
  expect(merged.inclusions).to eq([ association ])
1386
1282
  end
1387
1283
 
1388
- it "returns a new criteria" do
1389
- expect(merged).to_not equal(criteria)
1284
+ it 'returns a new criteria' do
1285
+ expect(merged).not_to equal(criteria)
1390
1286
  end
1391
1287
  end
1392
1288
 
1393
- context "when merging with a hash" do
1394
-
1289
+ context 'when merging with a hash' do
1395
1290
  let(:mergeable) do
1396
1291
  { klass: Band, includes: [ :records ] }
1397
1292
  end
1398
1293
 
1399
1294
  let(:association) do
1400
- Band.relations["records"]
1295
+ Band.relations['records']
1401
1296
  end
1402
1297
 
1403
1298
  let(:merged) do
1404
1299
  criteria.merge(mergeable)
1405
1300
  end
1406
1301
 
1407
- it "merges the selector" do
1408
- expect(merged.selector).to eq({ "name" => "Depeche Mode" })
1302
+ it 'merges the selector' do
1303
+ expect(merged.selector).to eq({ 'name' => 'Depeche Mode' })
1409
1304
  end
1410
1305
 
1411
- it "merges the options" do
1412
- expect(merged.options).to eq({ sort: { "name" => 1 }})
1306
+ it 'merges the options' do
1307
+ expect(merged.options).to eq({ sort: { 'name' => 1 } })
1413
1308
  end
1414
1309
 
1415
- it "merges the scoping options" do
1310
+ it 'merges the scoping options' do
1416
1311
  expect(merged.scoping_options).to eq([ nil, nil ])
1417
1312
  end
1418
1313
 
1419
- it "merges the inclusions" do
1314
+ it 'merges the inclusions' do
1420
1315
  expect(merged.inclusions).to eq([ association ])
1421
1316
  end
1422
1317
 
1423
- it "returns a new criteria" do
1424
- expect(merged).to_not equal(criteria)
1318
+ it 'returns a new criteria' do
1319
+ expect(merged).not_to equal(criteria)
1425
1320
  end
1426
1321
  end
1427
1322
  end
1428
1323
 
1429
1324
  describe '#merge!' do
1325
+ subject(:merged) { criteria.merge!(other) }
1326
+
1430
1327
  let(:band) { Band.new }
1431
1328
  let(:criteria) { Band.scoped.where(name: 'Depeche Mode').asc(:name) }
1432
1329
  let(:association) { Band.relations['records'] }
1433
- subject(:merged) { criteria.merge!(other) }
1434
1330
 
1435
1331
  context 'when merging a Criteria' do
1436
1332
  let(:other) do
1437
- { klass: Band, includes: [:records] }
1333
+ { klass: Band, includes: [ :records ] }
1438
1334
  end
1439
1335
 
1440
1336
  it 'merges the selector' do
@@ -1442,7 +1338,7 @@ describe Mongoid::Criteria do
1442
1338
  end
1443
1339
 
1444
1340
  it 'merges the options' do
1445
- expect(merged.options).to eq({ sort: { 'name' => 1 }})
1341
+ expect(merged.options).to eq({ sort: { 'name' => 1 } })
1446
1342
  end
1447
1343
 
1448
1344
  it 'merges the scoping options' do
@@ -1470,7 +1366,7 @@ describe Mongoid::Criteria do
1470
1366
  end
1471
1367
 
1472
1368
  it 'merges the options' do
1473
- expect(merged.options).to eq({ sort: { 'name' => 1 }})
1369
+ expect(merged.options).to eq({ sort: { 'name' => 1 } })
1474
1370
  end
1475
1371
 
1476
1372
  it 'merges the documents' do
@@ -1491,50 +1387,43 @@ describe Mongoid::Criteria do
1491
1387
  end
1492
1388
  end
1493
1389
 
1494
- describe "#min" do
1495
-
1496
- context "when provided a single field" do
1497
-
1390
+ describe '#min' do
1391
+ context 'when provided a single field' do
1498
1392
  let!(:depeche) do
1499
- Band.create!(name: "Depeche Mode", likes: 1000)
1393
+ Band.create!(name: 'Depeche Mode', likes: 1000)
1500
1394
  end
1501
1395
 
1502
1396
  let!(:tool) do
1503
- Band.create!(name: "Tool", likes: 500)
1397
+ Band.create!(name: 'Tool', likes: 500)
1504
1398
  end
1505
1399
 
1506
1400
  let(:criteria) do
1507
1401
  Band.all
1508
1402
  end
1509
1403
 
1510
- context "when provided a symbol" do
1511
-
1404
+ context 'when provided a symbol' do
1512
1405
  let(:min) do
1513
1406
  criteria.min(:likes)
1514
1407
  end
1515
1408
 
1516
- it "returns the min of the provided field" do
1409
+ it 'returns the min of the provided field' do
1517
1410
  expect(min).to eq(500)
1518
1411
  end
1519
1412
  end
1520
1413
 
1521
- context "when provided a block" do
1522
-
1414
+ context 'when provided a block' do
1523
1415
  let(:min) do
1524
- criteria.min do |a, b|
1525
- a.likes <=> b.likes
1526
- end
1416
+ criteria.min_by(&:likes)
1527
1417
  end
1528
1418
 
1529
- it "returns the document with the min value for the field" do
1419
+ it 'returns the document with the min value for the field' do
1530
1420
  expect(min).to eq(tool)
1531
1421
  end
1532
1422
  end
1533
1423
  end
1534
1424
  end
1535
1425
 
1536
- describe "#mod" do
1537
-
1426
+ describe '#mod' do
1538
1427
  let!(:match) do
1539
1428
  Band.create!(member_count: 5)
1540
1429
  end
@@ -1547,32 +1436,30 @@ describe Mongoid::Criteria do
1547
1436
  Band.mod(member_count: [ 4, 1 ])
1548
1437
  end
1549
1438
 
1550
- it "returns the matching documents" do
1439
+ it 'returns the matching documents' do
1551
1440
  expect(criteria).to eq([ match ])
1552
1441
  end
1553
1442
  end
1554
1443
 
1555
- describe "#ne" do
1556
-
1444
+ describe '#ne' do
1557
1445
  let!(:match) do
1558
- Band.create!(name: "Depeche Mode")
1446
+ Band.create!(name: 'Depeche Mode')
1559
1447
  end
1560
1448
 
1561
1449
  let!(:non_match) do
1562
- Band.create!(name: "Tool")
1450
+ Band.create!(name: 'Tool')
1563
1451
  end
1564
1452
 
1565
1453
  let(:criteria) do
1566
- Band.ne(name: "Tool")
1454
+ Band.ne(name: 'Tool')
1567
1455
  end
1568
1456
 
1569
- it "returns the matching documents" do
1457
+ it 'returns the matching documents' do
1570
1458
  expect(criteria).to eq([ match ])
1571
1459
  end
1572
1460
  end
1573
1461
 
1574
- describe "#near" do
1575
-
1462
+ describe '#near' do
1576
1463
  before do
1577
1464
  Bar.create_indexes
1578
1465
  end
@@ -1585,13 +1472,12 @@ describe Mongoid::Criteria do
1585
1472
  Bar.near(location: [ 52, 13 ])
1586
1473
  end
1587
1474
 
1588
- it "returns the matching documents" do
1475
+ it 'returns the matching documents' do
1589
1476
  expect(criteria).to eq([ match ])
1590
1477
  end
1591
1478
  end
1592
1479
 
1593
- describe "#near_sphere" do
1594
-
1480
+ describe '#near_sphere' do
1595
1481
  before do
1596
1482
  Bar.create_indexes
1597
1483
  end
@@ -1604,129 +1490,118 @@ describe Mongoid::Criteria do
1604
1490
  Bar.near_sphere(location: [ 52, 13 ])
1605
1491
  end
1606
1492
 
1607
- it "returns the matching documents" do
1493
+ it 'returns the matching documents' do
1608
1494
  expect(criteria).to eq([ match ])
1609
1495
  end
1610
1496
  end
1611
1497
 
1612
- describe "#nin" do
1613
-
1498
+ describe '#nin' do
1614
1499
  let!(:match) do
1615
- Band.create!(name: "Depeche Mode")
1500
+ Band.create!(name: 'Depeche Mode')
1616
1501
  end
1617
1502
 
1618
1503
  let!(:non_match) do
1619
- Band.create!(name: "Tool")
1504
+ Band.create!(name: 'Tool')
1620
1505
  end
1621
1506
 
1622
1507
  let(:criteria) do
1623
- Band.nin(name: [ "Tool" ])
1508
+ Band.nin(name: [ 'Tool' ])
1624
1509
  end
1625
1510
 
1626
- it "returns the matching documents" do
1511
+ it 'returns the matching documents' do
1627
1512
  expect(criteria).to eq([ match ])
1628
1513
  end
1629
1514
  end
1630
1515
 
1631
- describe "#nor" do
1632
-
1516
+ describe '#nor' do
1633
1517
  let!(:match) do
1634
- Band.create!(name: "Depeche Mode")
1518
+ Band.create!(name: 'Depeche Mode')
1635
1519
  end
1636
1520
 
1637
1521
  let!(:non_match) do
1638
- Band.create!(name: "Tool")
1522
+ Band.create!(name: 'Tool')
1639
1523
  end
1640
1524
 
1641
1525
  let(:criteria) do
1642
- Band.nor({ name: "Tool" }, { name: "New Order" })
1526
+ Band.nor({ name: 'Tool' }, { name: 'New Order' })
1643
1527
  end
1644
1528
 
1645
- it "returns the matching documents" do
1529
+ it 'returns the matching documents' do
1646
1530
  expect(criteria).to eq([ match ])
1647
1531
  end
1648
1532
  end
1649
1533
 
1650
- [ :or, :any_of ].each do |method|
1651
-
1652
- describe "\##{method}" do
1653
-
1534
+ %i[or any_of].each do |method|
1535
+ describe "##{method}" do
1654
1536
  let!(:match) do
1655
- Band.create!(name: "Depeche Mode")
1537
+ Band.create!(name: 'Depeche Mode')
1656
1538
  end
1657
1539
 
1658
1540
  let!(:non_match) do
1659
- Band.create!(name: "Tool")
1541
+ Band.create!(name: 'Tool')
1660
1542
  end
1661
1543
 
1662
- context "when sending a normal $or criterion" do
1663
-
1544
+ context 'when sending a normal $or criterion' do
1664
1545
  let(:criteria) do
1665
- Band.send(method, { name: "Depeche Mode" }, { name: "New Order" })
1546
+ Band.send(method, { name: 'Depeche Mode' }, { name: 'New Order' })
1666
1547
  end
1667
1548
 
1668
- it "returns the matching documents" do
1549
+ it 'returns the matching documents' do
1669
1550
  expect(criteria).to eq([ match ])
1670
1551
  end
1671
1552
  end
1672
1553
 
1673
- context "when matching against an id or other parameter" do
1674
-
1554
+ context 'when matching against an id or other parameter' do
1675
1555
  let(:criteria) do
1676
- Band.send(method, { id: match.id }, { name: "New Order" })
1556
+ Band.send(method, { id: match.id }, { name: 'New Order' })
1677
1557
  end
1678
1558
 
1679
- it "returns the matching documents" do
1559
+ it 'returns the matching documents' do
1680
1560
  expect(criteria).to eq([ match ])
1681
1561
  end
1682
1562
  end
1683
1563
  end
1684
1564
  end
1685
1565
 
1686
- describe "#pluck" do
1687
-
1566
+ describe '#pluck' do
1688
1567
  let!(:depeche) do
1689
- Band.create!(name: "Depeche Mode", likes: 3)
1568
+ Band.create!(name: 'Depeche Mode', likes: 3)
1690
1569
  end
1691
1570
 
1692
1571
  let!(:tool) do
1693
- Band.create!(name: "Tool", likes: 3)
1572
+ Band.create!(name: 'Tool', likes: 3)
1694
1573
  end
1695
1574
 
1696
1575
  let!(:photek) do
1697
- Band.create!(name: "Photek", likes: 1)
1576
+ Band.create!(name: 'Photek', likes: 1)
1698
1577
  end
1699
1578
 
1700
1579
  let(:maniacs) do
1701
- Band.create!(name: "10,000 Maniacs", likes: 1, sales: "1E2")
1580
+ Band.create!(name: '10,000 Maniacs', likes: 1, sales: '1E2')
1702
1581
  end
1703
1582
 
1704
- context "when the field is aliased" do
1705
-
1583
+ context 'when the field is aliased' do
1706
1584
  let!(:expensive) do
1707
- Product.create!(price: 100000)
1585
+ Product.create!(price: 100_000)
1708
1586
  end
1709
1587
 
1710
1588
  let!(:cheap) do
1711
1589
  Product.create!(price: 1)
1712
1590
  end
1713
1591
 
1714
- context "when using alias_attribute" do
1715
-
1592
+ context 'when using alias_attribute' do
1716
1593
  let(:plucked) do
1717
1594
  Product.pluck(:price)
1718
1595
  end
1719
1596
 
1720
- it "uses the aliases" do
1721
- expect(plucked).to eq([ 100000, 1 ])
1597
+ it 'uses the aliases' do
1598
+ expect(plucked).to eq([ 100_000, 1 ])
1722
1599
  end
1723
1600
  end
1724
1601
  end
1725
1602
 
1726
- context "when the criteria matches" do
1727
-
1728
- context "when there are no duplicate values" do
1729
-
1603
+ context 'when the criteria matches' do
1604
+ context 'when there are no duplicate values' do
1730
1605
  let(:criteria) do
1731
1606
  Band.where(:name.exists => true)
1732
1607
  end
@@ -1735,18 +1610,17 @@ describe Mongoid::Criteria do
1735
1610
  criteria.pluck(:name)
1736
1611
  end
1737
1612
 
1738
- it "returns the values" do
1739
- expect(plucked).to contain_exactly("Depeche Mode", "Tool", "Photek")
1613
+ it 'returns the values' do
1614
+ expect(plucked).to contain_exactly('Depeche Mode', 'Tool', 'Photek')
1740
1615
  end
1741
1616
 
1742
- context "when subsequently executing the criteria without a pluck" do
1743
- it "does not limit the fields" do
1617
+ context 'when subsequently executing the criteria without a pluck' do
1618
+ it 'does not limit the fields' do
1744
1619
  expect(criteria.first.likes).to eq(3)
1745
1620
  end
1746
1621
  end
1747
1622
 
1748
1623
  context 'when the field is a subdocument' do
1749
-
1750
1624
  let(:criteria) do
1751
1625
  Band.where(name: 'FKA Twigs')
1752
1626
  end
@@ -1763,8 +1637,8 @@ describe Mongoid::Criteria do
1763
1637
 
1764
1638
  let(:expected) do
1765
1639
  [
1766
- ["FKA Twigs", nil],
1767
- ['FKA Twigs', ["LP1"]]
1640
+ [ 'FKA Twigs', nil ],
1641
+ [ 'FKA Twigs', [ 'LP1' ] ]
1768
1642
  ]
1769
1643
  end
1770
1644
 
@@ -1774,7 +1648,6 @@ describe Mongoid::Criteria do
1774
1648
  end
1775
1649
 
1776
1650
  context 'when only a subdocument field is plucked' do
1777
-
1778
1651
  before do
1779
1652
  Band.create!(name: 'FKA Twigs')
1780
1653
  Band.create!(name: 'FKA Twigs', records: [ Record.new(name: 'LP1') ])
@@ -1787,7 +1660,7 @@ describe Mongoid::Criteria do
1787
1660
  let(:expected) do
1788
1661
  [
1789
1662
  nil,
1790
- ["LP1"]
1663
+ [ 'LP1' ]
1791
1664
  ]
1792
1665
  end
1793
1666
 
@@ -1798,83 +1671,75 @@ describe Mongoid::Criteria do
1798
1671
  end
1799
1672
  end
1800
1673
 
1801
- context "when plucking multi-fields" do
1802
-
1674
+ context 'when plucking multi-fields' do
1803
1675
  let(:plucked) do
1804
1676
  Band.where(:name.exists => true).pluck(:name, :likes)
1805
1677
  end
1806
1678
 
1807
- it "returns the values" do
1808
- expect(plucked).to contain_exactly(["Depeche Mode", 3], ["Tool", 3], ["Photek", 1])
1679
+ it 'returns the values' do
1680
+ expect(plucked).to contain_exactly([ 'Depeche Mode', 3 ], [ 'Tool', 3 ], [ 'Photek', 1 ])
1809
1681
  end
1810
1682
  end
1811
1683
 
1812
- context "when there are duplicate values" do
1813
-
1684
+ context 'when there are duplicate values' do
1814
1685
  let(:plucked) do
1815
1686
  Band.where(:name.exists => true).pluck(:likes)
1816
1687
  end
1817
1688
 
1818
- it "returns the duplicates" do
1689
+ it 'returns the duplicates' do
1819
1690
  expect(plucked).to contain_exactly(3, 3, 1)
1820
1691
  end
1821
1692
  end
1822
1693
  end
1823
1694
 
1824
- context "when the criteria does not match" do
1825
-
1695
+ context 'when the criteria does not match' do
1826
1696
  let(:plucked) do
1827
- Band.where(name: "New Order").pluck(:_id)
1697
+ Band.where(name: 'New Order').pluck(:_id)
1828
1698
  end
1829
1699
 
1830
- it "returns an empty array" do
1700
+ it 'returns an empty array' do
1831
1701
  expect(plucked).to be_empty
1832
1702
  end
1833
1703
  end
1834
1704
 
1835
- context "when plucking an aliased field" do
1836
-
1705
+ context 'when plucking an aliased field' do
1837
1706
  let(:plucked) do
1838
1707
  Band.all.pluck(:id)
1839
1708
  end
1840
1709
 
1841
- it "returns the field values" do
1710
+ it 'returns the field values' do
1842
1711
  expect(plucked).to eq([ depeche.id, tool.id, photek.id ])
1843
1712
  end
1844
1713
  end
1845
1714
 
1846
- context "when plucking existent and non-existent fields" do
1847
-
1715
+ context 'when plucking existent and non-existent fields' do
1848
1716
  let(:plucked) do
1849
1717
  Band.all.pluck(:id, :fooz)
1850
1718
  end
1851
1719
 
1852
- it "returns nil for the field that doesnt exist" do
1853
- expect(plucked).to eq([[depeche.id, nil], [tool.id, nil], [photek.id, nil] ])
1720
+ it 'returns nil for the field that doesnt exist' do
1721
+ expect(plucked).to eq([ [ depeche.id, nil ], [ tool.id, nil ], [ photek.id, nil ] ])
1854
1722
  end
1855
1723
  end
1856
1724
 
1857
- context "when plucking a field that doesnt exist" do
1858
-
1859
- context "when pluck one field" do
1860
-
1725
+ context 'when plucking a field that doesnt exist' do
1726
+ context 'when pluck one field' do
1861
1727
  let(:plucked) do
1862
1728
  Band.all.pluck(:foo)
1863
1729
  end
1864
1730
 
1865
- it "returns an array with nil values" do
1866
- expect(plucked).to eq([nil, nil, nil])
1731
+ it 'returns an array with nil values' do
1732
+ expect(plucked).to eq([ nil, nil, nil ])
1867
1733
  end
1868
1734
  end
1869
1735
 
1870
- context "when pluck multiple fields" do
1871
-
1736
+ context 'when pluck multiple fields' do
1872
1737
  let(:plucked) do
1873
1738
  Band.all.pluck(:foo, :bar)
1874
1739
  end
1875
1740
 
1876
- it "returns an array of arrays with nil values" do
1877
- expect(plucked).to eq([[nil, nil], [nil, nil], [nil, nil]])
1741
+ it 'returns an array of arrays with nil values' do
1742
+ expect(plucked).to eq([ [ nil, nil ], [ nil, nil ], [ nil, nil ] ])
1878
1743
  end
1879
1744
  end
1880
1745
  end
@@ -1908,16 +1773,15 @@ describe Mongoid::Criteria do
1908
1773
  end
1909
1774
 
1910
1775
  it 'returns the full translations hash to _translations' do
1911
- expect(plucked_translations.first).to eq({"de"=>"deutsch-text", "en"=>"english-text"})
1776
+ expect(plucked_translations.first).to eq({ 'de' => 'deutsch-text', 'en' => 'english-text' })
1912
1777
  end
1913
1778
 
1914
1779
  it 'returns both' do
1915
- expect(plucked_translations_both.first).to eq([{"de"=>"deutsch-text", "en"=>"english-text"}, "deutsch-text"])
1780
+ expect(plucked_translations_both.first).to eq([ { 'de' => 'deutsch-text', 'en' => 'english-text' }, 'deutsch-text' ])
1916
1781
  end
1917
1782
  end
1918
1783
 
1919
1784
  context 'when plucking a specific locale' do
1920
-
1921
1785
  let(:plucked) do
1922
1786
  Dictionary.all.pluck(:'description.de')
1923
1787
  end
@@ -1928,7 +1792,6 @@ describe Mongoid::Criteria do
1928
1792
  end
1929
1793
 
1930
1794
  context 'when plucking a specific locale from _translations field' do
1931
-
1932
1795
  let(:plucked) do
1933
1796
  Dictionary.all.pluck(:'description_translations.de')
1934
1797
  end
@@ -1949,55 +1812,54 @@ describe Mongoid::Criteria do
1949
1812
  Dictionary.all.pluck(:description).first
1950
1813
  end
1951
1814
 
1952
- it "correctly uses the fallback" do
1815
+ it 'correctly uses the fallback' do
1953
1816
  I18n.locale = :en
1954
- d = Dictionary.create!(description: 'english-text')
1817
+ Dictionary.create!(description: 'english-text')
1955
1818
  I18n.locale = :he
1956
- plucked.should == "english-text"
1819
+ plucked.should eq 'english-text'
1957
1820
  end
1958
1821
  end
1959
1822
 
1960
- context "when the localized field is embedded" do
1823
+ context 'when the localized field is embedded' do
1961
1824
  with_default_i18n_configs
1962
1825
 
1963
1826
  before do
1964
1827
  p = Passport.new
1965
1828
  I18n.locale = :en
1966
- p.name = "Neil"
1829
+ p.name = 'Neil'
1967
1830
  I18n.locale = :he
1968
- p.name = "Nissim"
1831
+ p.name = 'Nissim'
1969
1832
 
1970
- Person.create!(passport: p, employer_id: 12345)
1833
+ Person.create!(passport: p, employer_id: 12_345)
1971
1834
  end
1972
1835
 
1973
1836
  let(:plucked) do
1974
- Person.where(employer_id: 12345).pluck("pass.name").first
1837
+ Person.where(employer_id: 12_345).pluck('pass.name').first
1975
1838
  end
1976
1839
 
1977
1840
  let(:plucked_translations) do
1978
- Person.where(employer_id: 12345).pluck("pass.name_translations").first
1841
+ Person.where(employer_id: 12_345).pluck('pass.name_translations').first
1979
1842
  end
1980
1843
 
1981
1844
  let(:plucked_translations_field) do
1982
- Person.where(employer_id: 12345).pluck("pass.name_translations.en").first
1845
+ Person.where(employer_id: 12_345).pluck('pass.name_translations.en').first
1983
1846
  end
1984
1847
 
1985
- it "returns the translation for the current locale" do
1986
- expect(plucked).to eq("Nissim")
1848
+ it 'returns the translation for the current locale' do
1849
+ expect(plucked).to eq('Nissim')
1987
1850
  end
1988
1851
 
1989
- it "returns the full _translation hash" do
1990
- expect(plucked_translations).to eq({ "en" => "Neil", "he" => "Nissim" })
1852
+ it 'returns the full _translation hash' do
1853
+ expect(plucked_translations).to eq({ 'en' => 'Neil', 'he' => 'Nissim' })
1991
1854
  end
1992
1855
 
1993
- it "returns the translation for the requested locale" do
1994
- expect(plucked_translations_field).to eq("Neil")
1856
+ it 'returns the translation for the requested locale' do
1857
+ expect(plucked_translations_field).to eq('Neil')
1995
1858
  end
1996
1859
  end
1997
1860
  end
1998
1861
 
1999
1862
  context 'when plucking a field to be demongoized' do
2000
-
2001
1863
  let(:plucked) do
2002
1864
  Band.where(name: maniacs.name).pluck(:sales)
2003
1865
  end
@@ -2005,87 +1867,87 @@ describe Mongoid::Criteria do
2005
1867
  context 'when value is stored as string' do
2006
1868
  config_override :map_big_decimal_to_decimal128, false
2007
1869
 
2008
- it "demongoizes the field" do
1870
+ it 'demongoizes the field' do
2009
1871
  expect(plucked.first).to be_a(BigDecimal)
2010
- expect(plucked.first).to eq(BigDecimal("1E2"))
1872
+ expect(plucked.first).to eq(BigDecimal('1E2'))
2011
1873
  end
2012
1874
  end
2013
1875
 
2014
1876
  context 'when value is stored as decimal128' do
2015
1877
  config_override :map_big_decimal_to_decimal128, true
2016
1878
 
2017
- it "demongoizes the field" do
1879
+ it 'demongoizes the field' do
2018
1880
  expect(plucked.first).to be_a(BigDecimal)
2019
- expect(plucked.first).to eq(BigDecimal("1E2"))
1881
+ expect(plucked.first).to eq(BigDecimal('1E2'))
2020
1882
  end
2021
1883
  end
2022
1884
  end
2023
1885
 
2024
- context "when plucking an embedded field" do
2025
- let(:label) { Label.new(sales: "1E2") }
1886
+ context 'when plucking an embedded field' do
1887
+ let(:label) { Label.new(sales: '1E2') }
2026
1888
  let!(:band) { Band.create!(label: label) }
2027
1889
 
2028
- let(:plucked) { Band.where(_id: band.id).pluck("label.sales") }
1890
+ let(:plucked) { Band.where(_id: band.id).pluck('label.sales') }
2029
1891
 
2030
- it "demongoizes the field" do
2031
- expect(plucked).to eq([ BigDecimal("1E2") ])
1892
+ it 'demongoizes the field' do
1893
+ expect(plucked).to eq([ BigDecimal('1E2') ])
2032
1894
  end
2033
1895
  end
2034
1896
 
2035
- context "when plucking an embeds_many field" do
2036
- let(:label) { Label.new(sales: "1E2") }
2037
- let!(:band) { Band.create!(labels: [label]) }
1897
+ context 'when plucking an embeds_many field' do
1898
+ let(:label) { Label.new(sales: '1E2') }
1899
+ let!(:band) { Band.create!(labels: [ label ]) }
2038
1900
 
2039
- let(:plucked) { Band.where(_id: band.id).pluck("labels.sales") }
1901
+ let(:plucked) { Band.where(_id: band.id).pluck('labels.sales') }
2040
1902
 
2041
- it "demongoizes the field" do
2042
- expect(plucked.first).to eq([ BigDecimal("1E2") ])
1903
+ it 'demongoizes the field' do
1904
+ expect(plucked.first).to eq([ BigDecimal('1E2') ])
2043
1905
  end
2044
1906
  end
2045
1907
 
2046
- context "when plucking a nonexistent embedded field" do
2047
- let(:label) { Label.new(sales: "1E2") }
1908
+ context 'when plucking a nonexistent embedded field' do
1909
+ let(:label) { Label.new(sales: '1E2') }
2048
1910
  let!(:band) { Band.create!(label: label) }
2049
1911
 
2050
- let(:plucked) { Band.where(_id: band.id).pluck("label.qwerty") }
1912
+ let(:plucked) { Band.where(_id: band.id).pluck('label.qwerty') }
2051
1913
 
2052
- it "returns nil" do
1914
+ it 'returns nil' do
2053
1915
  expect(plucked.first).to eq(nil)
2054
1916
  end
2055
1917
  end
2056
1918
 
2057
- context "when tallying deeply nested arrays/embedded associations" do
2058
-
1919
+ context 'when tallying deeply nested arrays/embedded associations' do
2059
1920
  before do
2060
- Person.create!(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 2 } } ]))) ])
2061
- Person.create!(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 2 } } ]))) ])
2062
- Person.create!(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 3 } } ]))) ])
1921
+ Person.create!(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } },
1922
+ { y: { z: 2 } } ]))) ])
1923
+ Person.create!(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } },
1924
+ { y: { z: 2 } } ]))) ])
1925
+ Person.create!(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } },
1926
+ { y: { z: 3 } } ]))) ])
2063
1927
  end
2064
1928
 
2065
1929
  let(:plucked) do
2066
- Person.pluck("addresses.code.deepest.array.y.z")
1930
+ Person.pluck('addresses.code.deepest.array.y.z')
2067
1931
  end
2068
1932
 
2069
- it "returns the correct hash" do
1933
+ it 'returns the correct hash' do
2070
1934
  expect(plucked).to eq([
2071
- [ [ 1, 2 ] ], [ [ 1, 2 ] ], [ [ 1, 3 ] ]
2072
- ])
1935
+ [ [ 1, 2 ] ], [ [ 1, 2 ] ], [ [ 1, 3 ] ]
1936
+ ])
2073
1937
  end
2074
1938
  end
2075
1939
  end
2076
1940
 
2077
- describe "#pick" do
2078
-
1941
+ describe '#pick' do
2079
1942
  let!(:depeche) do
2080
- Band.create!(name: "Depeche Mode", likes: 3)
1943
+ Band.create!(name: 'Depeche Mode', likes: 3)
2081
1944
  end
2082
1945
 
2083
1946
  let!(:tool) do
2084
- Band.create!(name: "Tool", likes: 3)
1947
+ Band.create!(name: 'Tool', likes: 3)
2085
1948
  end
2086
1949
 
2087
- context "when picking a field" do
2088
-
1950
+ context 'when picking a field' do
2089
1951
  let(:criteria) do
2090
1952
  Band.all
2091
1953
  end
@@ -2094,13 +1956,12 @@ describe Mongoid::Criteria do
2094
1956
  criteria.pick(:name)
2095
1957
  end
2096
1958
 
2097
- it "returns one element" do
2098
- expect(picked).to eq("Depeche Mode")
1959
+ it 'returns one element' do
1960
+ expect(picked).to eq('Depeche Mode')
2099
1961
  end
2100
1962
  end
2101
1963
 
2102
- context "when picking multiple fields" do
2103
-
1964
+ context 'when picking multiple fields' do
2104
1965
  let(:criteria) do
2105
1966
  Band.all
2106
1967
  end
@@ -2109,98 +1970,87 @@ describe Mongoid::Criteria do
2109
1970
  criteria.pick(:name, :likes)
2110
1971
  end
2111
1972
 
2112
- it "returns an array" do
2113
- expect(picked).to eq([ "Depeche Mode", 3 ])
1973
+ it 'returns an array' do
1974
+ expect(picked).to eq([ 'Depeche Mode', 3 ])
2114
1975
  end
2115
1976
  end
2116
1977
  end
2117
1978
 
2118
- describe "#respond_to?" do
2119
-
1979
+ describe '#respond_to?' do
2120
1980
  let(:criteria) do
2121
1981
  described_class.new(Person)
2122
1982
  end
2123
1983
 
2124
1984
  before do
2125
1985
  class Person
2126
- def self.ages; self; end
1986
+ def self.ages
1987
+ self
1988
+ end
2127
1989
  end
2128
1990
  end
2129
1991
 
2130
- context "when asking about a model public class method" do
2131
-
2132
- it "returns true" do
1992
+ context 'when asking about a model public class method' do
1993
+ it 'returns true' do
2133
1994
  expect(criteria).to respond_to(:ages)
2134
1995
  end
2135
1996
  end
2136
1997
 
2137
- context "when asking about a model private class method" do
2138
-
2139
- context "when including private methods" do
2140
-
2141
- it "returns true" do
1998
+ context 'when asking about a model private class method' do
1999
+ context 'when including private methods' do
2000
+ it 'returns true' do
2142
2001
  expect(criteria.respond_to?(:for_ids, true)).to be true
2143
2002
  end
2144
2003
  end
2145
2004
  end
2146
2005
 
2147
- context "when asking about a model class public instance method" do
2148
-
2149
- it "returns true" do
2006
+ context 'when asking about a model class public instance method' do
2007
+ it 'returns true' do
2150
2008
  expect(criteria.respond_to?(:join)).to be true
2151
2009
  end
2152
2010
  end
2153
2011
 
2154
- context "when asking about a model private instance method" do
2155
-
2156
- context "when not including private methods" do
2157
-
2158
- it "returns false" do
2159
- expect(criteria).to_not respond_to(:initialize_copy)
2012
+ context 'when asking about a model private instance method' do
2013
+ context 'when not including private methods' do
2014
+ it 'returns false' do
2015
+ expect(criteria).not_to respond_to(:initialize_copy)
2160
2016
  end
2161
2017
  end
2162
2018
 
2163
- context "when including private methods" do
2164
-
2165
- it "returns true" do
2019
+ context 'when including private methods' do
2020
+ it 'returns true' do
2166
2021
  expect(criteria.respond_to?(:initialize_copy, true)).to be true
2167
2022
  end
2168
2023
  end
2169
2024
  end
2170
2025
 
2171
- context "when asking about a criteria instance method" do
2172
-
2173
- it "returns true" do
2026
+ context 'when asking about a criteria instance method' do
2027
+ it 'returns true' do
2174
2028
  expect(criteria).to respond_to(:context)
2175
2029
  end
2176
2030
  end
2177
2031
 
2178
- context "when asking about a private criteria instance method" do
2179
-
2180
- context "when not including private methods" do
2181
-
2182
- it "returns false" do
2183
- expect(criteria).to_not respond_to(:puts)
2032
+ context 'when asking about a private criteria instance method' do
2033
+ context 'when not including private methods' do
2034
+ it 'returns false' do
2035
+ expect(criteria).not_to respond_to(:puts)
2184
2036
  end
2185
2037
  end
2186
2038
 
2187
- context "when including private methods" do
2188
-
2189
- it "returns true" do
2039
+ context 'when including private methods' do
2040
+ it 'returns true' do
2190
2041
  expect(criteria.respond_to?(:puts, true)).to be true
2191
2042
  end
2192
2043
  end
2193
2044
  end
2194
2045
  end
2195
2046
 
2196
- describe "#sort" do
2197
-
2047
+ describe '#sort' do
2198
2048
  let!(:depeche) do
2199
- Band.create!(name: "Depeche Mode", likes: 1000)
2049
+ Band.create!(name: 'Depeche Mode', likes: 1000)
2200
2050
  end
2201
2051
 
2202
2052
  let!(:tool) do
2203
- Band.create!(name: "Tool", likes: 500)
2053
+ Band.create!(name: 'Tool', likes: 500)
2204
2054
  end
2205
2055
 
2206
2056
  let(:sorted) do
@@ -2209,195 +2059,355 @@ describe Mongoid::Criteria do
2209
2059
  end
2210
2060
  end
2211
2061
 
2212
- it "sorts the results in memory" do
2062
+ it 'sorts the results in memory' do
2213
2063
  expect(sorted).to eq([ tool, depeche ])
2214
2064
  end
2215
2065
  end
2216
2066
 
2217
- describe "#sum" do
2218
-
2219
- context "when provided a single field" do
2220
-
2067
+ describe '#sum' do
2068
+ context 'when provided a single field' do
2221
2069
  let!(:depeche) do
2222
- Band.create!(name: "Depeche Mode", likes: 1000)
2070
+ Band.create!(name: 'Depeche Mode', likes: 1000)
2223
2071
  end
2224
2072
 
2225
2073
  let!(:tool) do
2226
- Band.create!(name: "Tool", likes: 500)
2074
+ Band.create!(name: 'Tool', likes: 500)
2227
2075
  end
2228
2076
 
2229
2077
  let(:criteria) do
2230
2078
  Band.all
2231
2079
  end
2232
2080
 
2233
- context "when provided a symbol" do
2234
-
2081
+ context 'when provided a symbol' do
2235
2082
  let(:sum) do
2236
2083
  criteria.sum(:likes)
2237
2084
  end
2238
2085
 
2239
- it "returns the sum of the provided field" do
2086
+ it 'returns the sum of the provided field' do
2240
2087
  expect(sum).to eq(1500)
2241
2088
  end
2242
2089
  end
2243
2090
 
2244
- context "when provided a block" do
2245
-
2091
+ context 'when provided a block' do
2246
2092
  let(:sum) do
2247
2093
  criteria.sum(&:likes)
2248
2094
  end
2249
2095
 
2250
- it "returns the sum for the provided block" do
2096
+ it 'returns the sum for the provided block' do
2251
2097
  expect(sum).to eq(1500)
2252
2098
  end
2253
2099
  end
2254
2100
  end
2255
2101
  end
2256
2102
 
2257
- describe "#to_ary" do
2258
-
2103
+ describe '#to_ary' do
2259
2104
  let!(:band) do
2260
- Band.create!(name: "Depeche Mode")
2105
+ Band.create!(name: 'Depeche Mode')
2261
2106
  end
2262
2107
 
2263
2108
  let(:criteria) do
2264
- Band.where(name: "Depeche Mode")
2109
+ Band.where(name: 'Depeche Mode')
2265
2110
  end
2266
2111
 
2267
- it "returns the executed criteria" do
2112
+ it 'returns the executed criteria' do
2268
2113
  expect(criteria.to_ary).to eq([ band ])
2269
2114
  end
2270
2115
  end
2271
2116
 
2272
- describe "#max_scan" do
2117
+ describe '#raw' do
2118
+ let(:result) { results[0] }
2119
+
2120
+ context 'when the parameters are inconsistent' do
2121
+ let(:results) { criteria.raw(false, typed: false).to_a }
2122
+ let(:criteria) { Person }
2123
+
2124
+ it 'raises an ArgumentError' do
2125
+ expect { result }.to raise_error(ArgumentError)
2126
+ end
2127
+ end
2128
+
2129
+ context 'when returning untyped results' do
2130
+ let(:results) { criteria.raw.to_a }
2131
+
2132
+ context 'without associations' do
2133
+ before do
2134
+ Band.create(name: 'the band',
2135
+ active: true,
2136
+ genres: %w[ abc def ],
2137
+ member_count: 112,
2138
+ rating: 4.2,
2139
+ created: Time.now,
2140
+ updated: Time.now,
2141
+ sales: 1_234_567.89,
2142
+ decimal: 9_876_543.21,
2143
+ decibels: 140..170,
2144
+ deleted: false,
2145
+ mojo: Math::PI,
2146
+ tags: { 'one' => 1, 'two' => 2 },
2147
+ location: LatLng.new(41.74, -111.83))
2148
+ end
2149
+
2150
+ let(:criteria) { Band.where(name: 'the band') }
2151
+
2152
+ it 'returns a hash' do
2153
+ expect(result).to be_a(Hash)
2154
+ end
2155
+
2156
+ it 'does not demongoize the result' do
2157
+ expect(result['genres']).to be_a(Array)
2158
+ expect(result['decibels']).to eq({ 'min' => 140, 'max' => 170 })
2159
+ expect(result['location']).to eq [ -111.83, 41.74 ]
2160
+ end
2161
+ end
2162
+
2163
+ context 'with associations' do
2164
+ before do
2165
+ Person.create({
2166
+ addresses: [ Address.new(end_date: 2.months.from_now) ],
2167
+ passport: Passport.new(exp: 1.year.from_now)
2168
+ })
2169
+ end
2170
+
2171
+ let(:criteria) { Person }
2172
+
2173
+ it 'demongoizes the embedded relation' do
2174
+ expect(result['addresses']).to be_a(Array)
2175
+ expect(result['addresses'][0]['end_date']).to be_a(Time)
2176
+
2177
+ # `pass` is how it is stored, `passport` is how it is aliased
2178
+ expect(result['pass']).to be_a(Hash)
2179
+ expect(result['pass']['exp']).to be_a(Time)
2180
+ end
2181
+ end
2182
+
2183
+ context 'with projections' do
2184
+ before { Person.create(title: 'sir', dob: Date.new(1980, 1, 1)) }
2185
+
2186
+ context 'using #only' do
2187
+ let(:criteria) { Person.only(:dob) }
2188
+
2189
+ it 'produces a hash with only the _id and the requested key' do
2190
+ expect(result).to be_a(Hash)
2191
+ expect(result.keys).to eq %w[ _id dob ]
2192
+ expect(result['dob']).to eq Date.new(1980, 1, 1)
2193
+ end
2194
+ end
2195
+
2196
+ context 'using #without' do
2197
+ let(:criteria) { Person.without(:dob) }
2198
+
2199
+ it 'produces a hash that excludes requested key' do
2200
+ expect(result).to be_a(Hash)
2201
+ expect(result.keys).not_to include('dob')
2202
+ expect(result.keys).to be_present
2203
+ end
2204
+ end
2205
+ end
2206
+ end
2207
+
2208
+ context 'when returning typed results' do
2209
+ let(:results) { criteria.raw(typed: true).to_a }
2210
+
2211
+ context 'without associations' do
2212
+ before do
2213
+ Band.create(name: 'the band',
2214
+ active: true,
2215
+ genres: %w[ abc def ],
2216
+ member_count: 112,
2217
+ rating: 4.2,
2218
+ created: Time.now,
2219
+ updated: Time.now,
2220
+ sales: 1_234_567.89,
2221
+ decimal: 9_876_543.21,
2222
+ decibels: 140..170,
2223
+ deleted: false,
2224
+ mojo: Math::PI,
2225
+ tags: { 'one' => 1, 'two' => 2 },
2226
+ location: LatLng.new(41.74, -111.83))
2227
+ end
2228
+
2229
+ let(:criteria) { Band.where(name: 'the band') }
2230
+
2231
+ it 'returns a hash' do
2232
+ expect(result).to be_a(Hash)
2233
+ end
2234
+
2235
+ it 'demongoizes the result' do
2236
+ expect(result['genres']).to be_a(Array)
2237
+ expect(result['decibels']).to be_a(Range)
2238
+ expect(result['location']).to be_a(LatLng)
2239
+ end
2240
+ end
2241
+
2242
+ context 'with associations' do
2243
+ before do
2244
+ Person.create({
2245
+ addresses: [ Address.new(end_date: 2.months.from_now) ],
2246
+ passport: Passport.new(exp: 1.year.from_now)
2247
+ })
2248
+ end
2249
+
2250
+ let(:criteria) { Person }
2251
+
2252
+ it 'demongoizes the embedded relation' do
2253
+ expect(result['addresses']).to be_a(Array)
2254
+ expect(result['addresses'][0]['end_date']).to be_a(Date)
2255
+
2256
+ # `pass` is how it is stored, `passport` is how it is aliased
2257
+ expect(result['pass']).to be_a(Hash)
2258
+ expect(result['pass']['exp']).to be_a(Date)
2259
+ end
2260
+ end
2261
+
2262
+ context 'with projections' do
2263
+ before { Person.create(title: 'sir', dob: Date.new(1980, 1, 1)) }
2264
+
2265
+ context 'using #only' do
2266
+ let(:criteria) { Person.only(:dob) }
2267
+
2268
+ it 'produces a hash with only the _id and the requested key' do
2269
+ expect(result).to be_a(Hash)
2270
+ expect(result.keys).to eq %w[ _id dob ]
2271
+ expect(result['dob']).to eq Date.new(1980, 1, 1)
2272
+ end
2273
+ end
2274
+
2275
+ context 'using #without' do
2276
+ let(:criteria) { Person.without(:dob) }
2277
+
2278
+ it 'produces a hash that excludes requested key' do
2279
+ expect(result).to be_a(Hash)
2280
+ expect(result.keys).not_to include('dob')
2281
+ expect(result.keys).to be_present
2282
+ end
2283
+ end
2284
+ end
2285
+ end
2286
+ end
2287
+
2288
+ describe '#max_scan' do
2273
2289
  max_server_version '4.0'
2274
2290
 
2275
2291
  let!(:band) do
2276
- Band.create!(name: "Depeche Mode")
2292
+ Band.create!(name: 'Depeche Mode')
2277
2293
  end
2278
2294
 
2279
2295
  let!(:band2) do
2280
- Band.create!(name: "Tool")
2296
+ Band.create!(name: 'Tool')
2281
2297
  end
2282
2298
 
2283
2299
  let(:criteria) do
2284
2300
  Band.where({}).max_scan(1)
2285
2301
  end
2286
2302
 
2287
- it "executes the criteria while properly giving the max scan to Mongo" do
2288
- expect(criteria.to_ary).to eq [band]
2303
+ it 'executes the criteria while properly giving the max scan to Mongo' do
2304
+ expect(criteria.to_ary).to eq [ band ]
2289
2305
  end
2290
2306
  end
2291
2307
 
2292
- describe "#to_proc" do
2293
-
2308
+ describe '#to_proc' do
2294
2309
  let(:criteria) do
2295
2310
  Band.all
2296
2311
  end
2297
2312
 
2298
- it "returns a proc" do
2313
+ it 'returns a proc' do
2299
2314
  expect(criteria.to_proc).to be_a(Proc)
2300
2315
  end
2301
2316
 
2302
- it "wraps the criteria in the proc" do
2317
+ it 'wraps the criteria in the proc' do
2303
2318
  expect(criteria.to_proc[]).to eq(criteria)
2304
2319
  end
2305
2320
  end
2306
2321
 
2307
- describe "#type" do
2308
- context "when using the default discriminator_key" do
2309
- context "when the type is a string" do
2310
-
2322
+ describe '#type' do
2323
+ context 'when using the default discriminator_key' do
2324
+ context 'when the type is a string' do
2311
2325
  let!(:browser) do
2312
2326
  Browser.create!
2313
2327
  end
2314
2328
 
2315
2329
  let(:criteria) do
2316
- Canvas.all.type("Browser")
2330
+ Canvas.all.type('Browser')
2317
2331
  end
2318
2332
 
2319
- it "returns documents with the provided type" do
2333
+ it 'returns documents with the provided type' do
2320
2334
  expect(criteria).to eq([ browser ])
2321
2335
  end
2322
2336
  end
2323
2337
 
2324
- context "when the type is an Array of type" do
2325
-
2338
+ context 'when the type is an Array of type' do
2326
2339
  let!(:browser) do
2327
2340
  Firefox.create!
2328
2341
  end
2329
2342
 
2330
2343
  let(:criteria) do
2331
- Canvas.all.type([ "Browser", "Firefox" ])
2344
+ Canvas.all.type(%w[Browser Firefox])
2332
2345
  end
2333
2346
 
2334
- it "returns documents with the provided types" do
2347
+ it 'returns documents with the provided types' do
2335
2348
  expect(criteria).to eq([ browser ])
2336
2349
  end
2337
2350
  end
2338
2351
  end
2339
2352
 
2340
- context "when using a custom discriminator_key" do
2353
+ context 'when using a custom discriminator_key' do
2341
2354
  before do
2342
- Canvas.discriminator_key = "dkey"
2355
+ Canvas.discriminator_key = 'dkey'
2343
2356
  end
2344
2357
 
2345
2358
  after do
2346
2359
  Canvas.discriminator_key = nil
2347
2360
  end
2348
2361
 
2349
- context "when the type is a string" do
2350
-
2362
+ context 'when the type is a string' do
2351
2363
  let!(:browser) do
2352
2364
  Browser.create!
2353
2365
  end
2354
2366
 
2355
2367
  let(:criteria) do
2356
- Canvas.all.type("Browser")
2368
+ Canvas.all.type('Browser')
2357
2369
  end
2358
2370
 
2359
- it "returns documents with the provided type" do
2371
+ it 'returns documents with the provided type' do
2360
2372
  expect(criteria).to eq([ browser ])
2361
2373
  end
2362
2374
  end
2363
2375
 
2364
- context "when the type is an Array of type" do
2365
-
2376
+ context 'when the type is an Array of type' do
2366
2377
  let!(:browser) do
2367
2378
  Firefox.create!
2368
2379
  end
2369
2380
 
2370
2381
  let(:criteria) do
2371
- Canvas.all.type([ "Browser", "Firefox" ])
2382
+ Canvas.all.type(%w[Browser Firefox])
2372
2383
  end
2373
2384
 
2374
- it "returns documents with the provided types" do
2385
+ it 'returns documents with the provided types' do
2375
2386
  expect(criteria).to eq([ browser ])
2376
2387
  end
2377
2388
  end
2378
2389
  end
2379
2390
  end
2380
2391
 
2381
- describe "#where" do
2382
-
2392
+ describe '#where' do
2383
2393
  let!(:match) do
2384
- Band.create!(name: "Depeche Mode")
2394
+ Band.create!(name: 'Depeche Mode')
2385
2395
  end
2386
2396
 
2387
2397
  let!(:non_match) do
2388
- Band.create!(name: "Tool")
2398
+ Band.create!(name: 'Tool')
2389
2399
  end
2390
2400
 
2391
2401
  context 'when provided no arguments' do
2392
2402
  context 'on a model class' do
2393
2403
  it 'returns an empty criteria' do
2394
- Band.where.selector.should == {}
2404
+ Band.where.selector.should eq({})
2395
2405
  end
2396
2406
  end
2397
2407
 
2398
2408
  context 'on an association' do
2399
2409
  it 'returns an empty criteria' do
2400
- match.records.where.selector.should == {}
2410
+ match.records.where.selector.should eq({})
2401
2411
  end
2402
2412
  end
2403
2413
  end
@@ -2406,7 +2416,7 @@ describe Mongoid::Criteria do
2406
2416
  context 'on a model class' do
2407
2417
  it 'raises ArgumentError' do
2408
2418
  lambda do
2409
- Band.where({foo: 1}, {bar: 2})
2419
+ Band.where({ foo: 1 }, { bar: 2 })
2410
2420
  end.should raise_error(ArgumentError, /where requires zero or one arguments/)
2411
2421
  end
2412
2422
  end
@@ -2414,52 +2424,44 @@ describe Mongoid::Criteria do
2414
2424
  context 'on an association' do
2415
2425
  it 'raises ArgumentError' do
2416
2426
  lambda do
2417
- match.records.where({foo: 1}, {bar: 2})
2427
+ match.records.where({ foo: 1 }, { bar: 2 })
2418
2428
  end.should raise_error(ArgumentError, /where requires zero or one arguments/)
2419
2429
  end
2420
2430
  end
2421
2431
  end
2422
2432
 
2423
- context "when provided a string" do
2424
-
2425
- context "when the criteria is embedded" do
2426
-
2427
- it "raises an error" do
2428
- expect {
2429
- match.records.where("this.name == null")
2430
- }.to raise_error(Mongoid::Errors::UnsupportedJavascript)
2433
+ context 'when provided a string' do
2434
+ context 'when the criteria is embedded' do
2435
+ it 'raises an error' do
2436
+ expect do
2437
+ match.records.where('this.name == null')
2438
+ end.to raise_error(Mongoid::Errors::UnsupportedJavascript)
2431
2439
  end
2432
2440
  end
2433
2441
 
2434
- context "when the criteria is not embedded" do
2435
- # String criteria compile to $where, which requires the opt-in.
2436
- config_override :allow_unsafe_query_operators, true
2437
-
2442
+ context 'when the criteria is not embedded' do
2438
2443
  let(:criteria) do
2439
2444
  Band.where("this.name == 'Depeche Mode'")
2440
2445
  end
2441
2446
 
2442
- it "returns the matching documents" do
2447
+ it 'returns the matching documents' do
2443
2448
  expect(criteria).to eq([ match ])
2444
2449
  end
2445
2450
  end
2446
2451
  end
2447
2452
 
2448
- context "when provided criterion" do
2449
-
2450
- context "when the criteria is standard" do
2451
-
2453
+ context 'when provided criterion' do
2454
+ context 'when the criteria is standard' do
2452
2455
  let(:criteria) do
2453
- Band.where(name: "Depeche Mode")
2456
+ Band.where(name: 'Depeche Mode')
2454
2457
  end
2455
2458
 
2456
- it "returns the matching documents" do
2459
+ it 'returns the matching documents' do
2457
2460
  expect(criteria).to eq([ match ])
2458
2461
  end
2459
2462
  end
2460
2463
 
2461
- context "when the criteria is an exact fk array match" do
2462
-
2464
+ context 'when the criteria is an exact fk array match' do
2463
2465
  let(:id_one) do
2464
2466
  BSON::ObjectId.new
2465
2467
  end
@@ -2472,13 +2474,12 @@ describe Mongoid::Criteria do
2472
2474
  Account.where(agent_ids: [ id_one, id_two ])
2473
2475
  end
2474
2476
 
2475
- it "does not wrap the array in another array" do
2476
- expect(criteria.selector).to eq({ "agent_ids" => [ id_one, id_two ]})
2477
+ it 'does not wrap the array in another array' do
2478
+ expect(criteria.selector).to eq({ 'agent_ids' => [ id_one, id_two ] })
2477
2479
  end
2478
2480
  end
2479
2481
 
2480
- context "when querying on a big decimal" do
2481
-
2482
+ context 'when querying on a big decimal' do
2482
2483
  context 'when map_big_decimal_to_decimal128 is false' do
2483
2484
  config_override :map_big_decimal_to_decimal128, false
2484
2485
 
@@ -2487,14 +2488,14 @@ describe Mongoid::Criteria do
2487
2488
  end
2488
2489
 
2489
2490
  let!(:band) do
2490
- Band.create!(name: "Boards of Canada", sales: sales)
2491
+ Band.create!(name: 'Boards of Canada', sales: sales)
2491
2492
  end
2492
2493
 
2493
2494
  let(:from_db) do
2494
2495
  Band.where(sales: sales).first
2495
2496
  end
2496
2497
 
2497
- it "finds the document by the big decimal value" do
2498
+ it 'finds the document by the big decimal value' do
2498
2499
  expect(from_db).to eq(band)
2499
2500
  end
2500
2501
  end
@@ -2507,14 +2508,14 @@ describe Mongoid::Criteria do
2507
2508
  end
2508
2509
 
2509
2510
  let!(:band) do
2510
- Band.create!(name: "Boards of Canada", sales: sales)
2511
+ Band.create!(name: 'Boards of Canada', sales: sales)
2511
2512
  end
2512
2513
 
2513
2514
  let(:from_db) do
2514
2515
  Band.where(sales: sales).first
2515
2516
  end
2516
2517
 
2517
- it "finds the document by the big decimal value" do
2518
+ it 'finds the document by the big decimal value' do
2518
2519
  expect(from_db).to eq(band)
2519
2520
  end
2520
2521
  end
@@ -2528,33 +2529,33 @@ describe Mongoid::Criteria do
2528
2529
 
2529
2530
  let!(:band) do
2530
2531
  Mongoid.map_big_decimal_to_decimal128 = false
2531
- Band.create!(name: "Boards of Canada", sales: sales)
2532
+ Band.create!(name: 'Boards of Canada', sales: sales)
2532
2533
  end
2533
2534
 
2534
- it "cannot find values when querying using a BigDecimal value" do
2535
+ it 'cannot find values when querying using a BigDecimal value' do
2535
2536
  Mongoid.map_big_decimal_to_decimal128 = true
2536
2537
  from_db = Band.where(sales: sales).first
2537
2538
  expect(from_db).to eq(nil)
2538
2539
  end
2539
2540
 
2540
- it "cannot find values when querying using a string value" do
2541
+ it 'cannot find values when querying using a string value' do
2541
2542
  Mongoid.map_big_decimal_to_decimal128 = true
2542
2543
  from_db = Band.where(sales: sales.to_s).first
2543
2544
  expect(from_db).to eq(nil)
2544
2545
  end
2545
2546
 
2546
- context "after converting value" do
2547
+ context 'after converting value' do
2547
2548
  before do
2548
2549
  Mongoid.map_big_decimal_to_decimal128 = true
2549
2550
  band.set(sales: band.sales)
2550
2551
  end
2551
2552
 
2552
- it "can find values when querying using a BigDecimal value" do
2553
+ it 'can find values when querying using a BigDecimal value' do
2553
2554
  from_db = Band.where(sales: sales).first
2554
2555
  expect(from_db).to eq(band)
2555
2556
  end
2556
2557
 
2557
- it "can find values when querying using a string value" do
2558
+ it 'can find values when querying using a string value' do
2558
2559
  from_db = Band.where(sales: sales.to_s).first
2559
2560
  expect(from_db).to eq(band)
2560
2561
  end
@@ -2562,8 +2563,7 @@ describe Mongoid::Criteria do
2562
2563
  end
2563
2564
  end
2564
2565
 
2565
- context "when querying on a big decimal from a dynamic field" do
2566
-
2566
+ context 'when querying on a big decimal from a dynamic field' do
2567
2567
  context 'when map_big_decimal_to_decimal128 is false' do
2568
2568
  config_override :map_big_decimal_to_decimal128, false
2569
2569
 
@@ -2572,14 +2572,14 @@ describe Mongoid::Criteria do
2572
2572
  end
2573
2573
 
2574
2574
  let!(:band) do
2575
- Band.create!(name: "Boards of Canada", fans: fans)
2575
+ Band.create!(name: 'Boards of Canada', fans: fans)
2576
2576
  end
2577
2577
 
2578
2578
  let(:from_db) do
2579
2579
  Band.where(fans: fans.to_s).first
2580
2580
  end
2581
2581
 
2582
- it "finds the document by the big decimal value" do
2582
+ it 'finds the document by the big decimal value' do
2583
2583
  expect(from_db).to eq(band)
2584
2584
  end
2585
2585
  end
@@ -2592,40 +2592,38 @@ describe Mongoid::Criteria do
2592
2592
  end
2593
2593
 
2594
2594
  let!(:band) do
2595
- Band.create!(name: "Boards of Canada", fans: fans)
2595
+ Band.create!(name: 'Boards of Canada', fans: fans)
2596
2596
  end
2597
2597
 
2598
2598
  let(:from_db) do
2599
2599
  Band.where(fans: fans).first
2600
2600
  end
2601
2601
 
2602
- it "only finds the document by the string value" do
2602
+ it 'only finds the document by the string value' do
2603
2603
  expect(from_db).to eq(band)
2604
2604
  end
2605
2605
  end
2606
2606
  end
2607
2607
 
2608
- context "when querying on a BSON::Decimal128" do
2609
-
2608
+ context 'when querying on a BSON::Decimal128' do
2610
2609
  let(:decimal) do
2611
- BSON::Decimal128.new("0.0005")
2610
+ BSON::Decimal128.new('0.0005')
2612
2611
  end
2613
2612
 
2614
2613
  let!(:band) do
2615
- Band.create!(name: "Boards of Canada", decimal: decimal)
2614
+ Band.create!(name: 'Boards of Canada', decimal: decimal)
2616
2615
  end
2617
2616
 
2618
2617
  let(:from_db) do
2619
2618
  Band.where(decimal: decimal).first
2620
2619
  end
2621
2620
 
2622
- it "finds the document by the big decimal value" do
2621
+ it 'finds the document by the big decimal value' do
2623
2622
  expect(from_db).to eq(band)
2624
2623
  end
2625
2624
  end
2626
2625
 
2627
2626
  context 'when querying on a polymorphic relation' do
2628
-
2629
2627
  let(:movie) do
2630
2628
  Movie.create!
2631
2629
  end
@@ -2639,14 +2637,13 @@ describe Mongoid::Criteria do
2639
2637
  end
2640
2638
  end
2641
2639
 
2642
- context "when querying an embedded document with aliases" do
2643
-
2640
+ context 'when querying an embedded document with aliases' do
2644
2641
  let(:criteria) do
2645
- Person.where("phones.extension" => "123")
2642
+ Person.where('phones.extension' => '123')
2646
2643
  end
2647
2644
 
2648
- it "expands the aliases" do
2649
- expect(criteria.selector).to eq("mobile_phones.ext" => "123")
2645
+ it 'expands the aliases' do
2646
+ expect(criteria.selector).to eq('mobile_phones.ext' => '123')
2650
2647
  end
2651
2648
  end
2652
2649
  end
@@ -2663,7 +2660,7 @@ describe Mongoid::Criteria do
2663
2660
  let(:criteria) { Band.where(foo: 1).where(foo: 2) }
2664
2661
 
2665
2662
  it 'combines criteria' do
2666
- expect(criteria.selector).to eq('foo' => 1, '$and' => [{'foo' => 2}])
2663
+ expect(criteria.selector).to eq('foo' => 1, '$and' => [ { 'foo' => 2 } ])
2667
2664
  end
2668
2665
  end
2669
2666
 
@@ -2672,7 +2669,8 @@ describe Mongoid::Criteria do
2672
2669
 
2673
2670
  it 'combines criteria' do
2674
2671
  expect(criteria.selector).to eq(
2675
- 'foo' => 1, '$and' => [{'foo' => 2}], 'bar' => 3)
2672
+ 'foo' => 1, '$and' => [ { 'foo' => 2 } ], 'bar' => 3
2673
+ )
2676
2674
  end
2677
2675
  end
2678
2676
 
@@ -2681,11 +2679,12 @@ describe Mongoid::Criteria do
2681
2679
 
2682
2680
  it 'combines criteria' do
2683
2681
  expect(criteria.selector).to eq(
2684
- 'foo' => 1, '$and' => [{'foo' => 2}], 'bar' => 3)
2682
+ 'foo' => 1, '$and' => [ { 'foo' => 2 } ], 'bar' => 3
2683
+ )
2685
2684
  end
2686
2685
  end
2687
2686
 
2688
- context "when duplicating where conditions" do
2687
+ context 'when duplicating where conditions' do
2689
2688
  let(:criteria) { Sound.where(active: true).where(active: true) }
2690
2689
 
2691
2690
  it 'does not duplicate criteria' do
@@ -2693,12 +2692,13 @@ describe Mongoid::Criteria do
2693
2692
  end
2694
2693
  end
2695
2694
 
2696
- context "when duplicating where conditions with different values" do
2695
+ context 'when duplicating where conditions with different values' do
2697
2696
  let(:criteria) { Sound.where(active: true).where(active: false).where(active: true).where(active: false) }
2698
2697
 
2699
2698
  it 'does not duplicate criteria' do
2700
2699
  expect(criteria.selector).to eq(
2701
- 'active' => true, '$and' => [{'active' => false}])
2700
+ 'active' => true, '$and' => [ { 'active' => false } ]
2701
+ )
2702
2702
  end
2703
2703
  end
2704
2704
 
@@ -2707,171 +2707,170 @@ describe Mongoid::Criteria do
2707
2707
  # it is difficult to analyze the criteria used. For this reason, I have
2708
2708
  # extracted the crux of the issue, adding an _id to the the criteria twice,
2709
2709
  # and used that for the test case.
2710
- context "when searching by _id twice" do
2710
+ context 'when searching by _id twice' do
2711
2711
  let(:_id) { BSON::ObjectId.new }
2712
2712
  let(:criteria) { Band.where(_id: _id) }
2713
- let(:dup_criteria) { criteria.where(_id: _id)}
2713
+ let(:dup_criteria) { criteria.where(_id: _id) }
2714
2714
 
2715
- it "does not duplicate the criteria" do
2716
- expect(dup_criteria.selector).to eq({ "_id" => _id })
2715
+ it 'does not duplicate the criteria' do
2716
+ expect(dup_criteria.selector).to eq({ '_id' => _id })
2717
2717
  end
2718
2718
  end
2719
2719
 
2720
- context "when querying an embedded field" do
2721
- let(:criteria) { Band.where("label.name": 12345) }
2720
+ context 'when querying an embedded field' do
2721
+ let(:criteria) { Band.where('label.name': 12_345) }
2722
2722
 
2723
- it "mongoizes the embedded field in the selector" do
2724
- expect(criteria.selector).to eq("label.name" => "12345")
2723
+ it 'mongoizes the embedded field in the selector' do
2724
+ expect(criteria.selector).to eq('label.name' => '12345')
2725
2725
  end
2726
2726
  end
2727
2727
 
2728
- context "when querying with a range" do
2729
-
2730
- context "when querying an embeds_many association" do
2728
+ context 'when querying with a range' do
2729
+ context 'when querying an embeds_many association' do
2731
2730
  let(:criteria) do
2732
- Band.where("labels" => 10..15)
2731
+ Band.where('labels' => 10..15)
2733
2732
  end
2734
2733
 
2735
- it "correctly uses elemMatch without an inner key" do
2734
+ it 'correctly uses elemMatch without an inner key' do
2736
2735
  expect(criteria.selector).to eq(
2737
- "labels" => {
2738
- "$elemMatch" => { "$gte" => 10, "$lte" => 15 }
2736
+ 'labels' => {
2737
+ '$elemMatch' => { '$gte' => 10, '$lte' => 15 }
2739
2738
  }
2740
2739
  )
2741
2740
  end
2742
2741
  end
2743
2742
 
2744
- context "when querying an element in an embeds_many association" do
2743
+ context 'when querying an element in an embeds_many association' do
2745
2744
  let(:criteria) do
2746
- Band.where("labels.age" => 10..15)
2745
+ Band.where('labels.age' => 10..15)
2747
2746
  end
2748
2747
 
2749
- it "correctly uses elemMatch" do
2748
+ it 'correctly uses elemMatch' do
2750
2749
  expect(criteria.selector).to eq(
2751
- "labels" => {
2752
- "$elemMatch" => {
2753
- "age" => { "$gte" => 10, "$lte" => 15 }
2750
+ 'labels' => {
2751
+ '$elemMatch' => {
2752
+ 'age' => { '$gte' => 10, '$lte' => 15 }
2754
2753
  }
2755
2754
  }
2756
2755
  )
2757
2756
  end
2758
2757
  end
2759
2758
 
2760
- context "when querying a field of type array" do
2759
+ context 'when querying a field of type array' do
2761
2760
  let(:criteria) do
2762
- Band.where("genres" => 10..15)
2761
+ Band.where('genres' => 10..15)
2763
2762
  end
2764
2763
 
2765
- it "correctly uses elemMatch without an inner key" do
2764
+ it 'correctly uses elemMatch without an inner key' do
2766
2765
  expect(criteria.selector).to eq(
2767
- "genres" => {
2768
- "$elemMatch" => { "$gte" => 10, "$lte" => 15 }
2766
+ 'genres' => {
2767
+ '$elemMatch' => { '$gte' => 10, '$lte' => 15 }
2769
2768
  }
2770
2769
  )
2771
2770
  end
2772
2771
  end
2773
2772
 
2774
- context "when querying an aliased field of type array" do
2773
+ context 'when querying an aliased field of type array' do
2775
2774
  let(:criteria) do
2776
- Person.where("array" => 10..15)
2775
+ Person.where('array' => 10..15)
2777
2776
  end
2778
2777
 
2779
- it "correctly uses the aliased field and elemMatch" do
2778
+ it 'correctly uses the aliased field and elemMatch' do
2780
2779
  expect(criteria.selector).to eq(
2781
- "a" => {
2782
- "$elemMatch" => { "$gte" => 10, "$lte" => 15 }
2780
+ 'a' => {
2781
+ '$elemMatch' => { '$gte' => 10, '$lte' => 15 }
2783
2782
  }
2784
2783
  )
2785
2784
  end
2786
2785
  end
2787
2786
 
2788
- context "when querying a field inside an array" do
2787
+ context 'when querying a field inside an array' do
2789
2788
  let(:criteria) do
2790
- Band.where("genres.age" => 10..15)
2789
+ Band.where('genres.age' => 10..15)
2791
2790
  end
2792
2791
 
2793
- it "correctly uses elemMatch" do
2792
+ it 'correctly uses elemMatch' do
2794
2793
  expect(criteria.selector).to eq(
2795
- "genres" => {
2796
- "$elemMatch" => {
2797
- "age" => { "$gte" => 10, "$lte" => 15 }
2794
+ 'genres' => {
2795
+ '$elemMatch' => {
2796
+ 'age' => { '$gte' => 10, '$lte' => 15 }
2798
2797
  }
2799
2798
  }
2800
2799
  )
2801
2800
  end
2802
2801
  end
2803
2802
 
2804
- context "when there are no embeds_manys or Arrays" do
2803
+ context 'when there are no embeds_manys or Arrays' do
2805
2804
  let(:criteria) do
2806
- Band.where("fans.info.age" => 10..15)
2805
+ Band.where('fans.info.age' => 10..15)
2807
2806
  end
2808
2807
 
2809
- it "does not use elemMatch" do
2808
+ it 'does not use elemMatch' do
2810
2809
  expect(criteria.selector).to eq(
2811
- "fans.info.age" => { "$gte" => 10, "$lte" => 15 }
2810
+ 'fans.info.age' => { '$gte' => 10, '$lte' => 15 }
2812
2811
  )
2813
2812
  end
2814
2813
  end
2815
2814
 
2816
- context "when querying a nested element in an embeds_many association" do
2815
+ context 'when querying a nested element in an embeds_many association' do
2817
2816
  let(:criteria) do
2818
- Band.where("labels.age.number" => 10..15)
2817
+ Band.where('labels.age.number' => 10..15)
2819
2818
  end
2820
2819
 
2821
- it "correctly uses elemMatch" do
2820
+ it 'correctly uses elemMatch' do
2822
2821
  expect(criteria.selector).to eq(
2823
- "labels" => {
2824
- "$elemMatch" => {
2825
- "age.number" => { "$gte" => 10, "$lte" => 15 }
2822
+ 'labels' => {
2823
+ '$elemMatch' => {
2824
+ 'age.number' => { '$gte' => 10, '$lte' => 15 }
2826
2825
  }
2827
2826
  }
2828
2827
  )
2829
2828
  end
2830
2829
  end
2831
2830
 
2832
- context "when querying a nested element in an Array" do
2831
+ context 'when querying a nested element in an Array' do
2833
2832
  let(:criteria) do
2834
- Band.where("genres.name.length" => 10..15)
2833
+ Band.where('genres.name.length' => 10..15)
2835
2834
  end
2836
2835
 
2837
- it "correctly uses elemMatch" do
2836
+ it 'correctly uses elemMatch' do
2838
2837
  expect(criteria.selector).to eq(
2839
- "genres" => {
2840
- "$elemMatch" => {
2841
- "name.length" => { "$gte" => 10, "$lte" => 15 }
2838
+ 'genres' => {
2839
+ '$elemMatch' => {
2840
+ 'name.length' => { '$gte' => 10, '$lte' => 15 }
2842
2841
  }
2843
2842
  }
2844
2843
  )
2845
2844
  end
2846
2845
  end
2847
2846
 
2848
- context "when querying a nested element in a nested embeds_many association" do
2849
- context "when the outer association is an embeds_many" do
2847
+ context 'when querying a nested element in a nested embeds_many association' do
2848
+ context 'when the outer association is an embeds_many' do
2850
2849
  let(:criteria) do
2851
- Band.where("records.tracks.name.length" => 10..15)
2850
+ Band.where('records.tracks.name.length' => 10..15)
2852
2851
  end
2853
2852
 
2854
- it "correctly uses elemMatch" do
2853
+ it 'correctly uses elemMatch' do
2855
2854
  expect(criteria.selector).to eq(
2856
- "records.tracks" => {
2857
- "$elemMatch" => {
2858
- "name.length" => { "$gte" => 10, "$lte" => 15 }
2855
+ 'records.tracks' => {
2856
+ '$elemMatch' => {
2857
+ 'name.length' => { '$gte' => 10, '$lte' => 15 }
2859
2858
  }
2860
2859
  }
2861
2860
  )
2862
2861
  end
2863
2862
  end
2864
2863
 
2865
- context "when the outer association is an embeds_one" do
2864
+ context 'when the outer association is an embeds_one' do
2866
2865
  let(:criteria) do
2867
- Person.where("name.translations.language.length" => 10..15)
2866
+ Person.where('name.translations.language.length' => 10..15)
2868
2867
  end
2869
2868
 
2870
- it "correctly uses elemMatch" do
2869
+ it 'correctly uses elemMatch' do
2871
2870
  expect(criteria.selector).to eq(
2872
- "name.translations" => {
2873
- "$elemMatch" => {
2874
- "language.length" => { "$gte" => 10, "$lte" => 15 }
2871
+ 'name.translations' => {
2872
+ '$elemMatch' => {
2873
+ 'language.length' => { '$gte' => 10, '$lte' => 15 }
2875
2874
  }
2876
2875
  }
2877
2876
  )
@@ -2879,48 +2878,49 @@ describe Mongoid::Criteria do
2879
2878
  end
2880
2879
  end
2881
2880
 
2882
- context "when querying a deeply nested array" do
2881
+ context 'when querying a deeply nested array' do
2883
2882
  let(:criteria) do
2884
- Person.where("addresses.code.deepest.array.element.item" => 10..15)
2883
+ Person.where('addresses.code.deepest.array.element.item' => 10..15)
2885
2884
  end
2886
2885
 
2887
- it "correctly uses elemMatch" do
2886
+ it 'correctly uses elemMatch' do
2888
2887
  expect(criteria.selector).to eq(
2889
- "addresses.code.deepest.array" => {
2890
- "$elemMatch" => {
2891
- "element.item" => { "$gte" => 10, "$lte" => 15 }
2888
+ 'addresses.code.deepest.array' => {
2889
+ '$elemMatch' => {
2890
+ 'element.item' => { '$gte' => 10, '$lte' => 15 }
2892
2891
  }
2893
2892
  }
2894
2893
  )
2895
2894
  end
2896
2895
  end
2897
2896
 
2898
- context "when there are multiple conditions" do
2897
+ context 'when there are multiple conditions' do
2899
2898
  let(:criteria) do
2900
- Band.where("$or" => [{"labels.age" => 10..15}, {labels: 8}])
2899
+ Band.where('$or' => [ { 'labels.age' => 10..15 }, { labels: 8 } ])
2901
2900
  end
2902
2901
 
2903
- it "correctly combines the conditions" do
2904
- expect(criteria.selector).to eq("$or" => [
2905
- { "labels" => {
2906
- "$elemMatch" => {
2907
- "age" => { "$gte" => 10, "$lte" => 15 }
2908
- } } },
2909
- { "labels" => 8 }
2910
- ])
2902
+ it 'correctly combines the conditions' do
2903
+ expect(criteria.selector).to eq('$or' => [
2904
+ { 'labels' => {
2905
+ '$elemMatch' => {
2906
+ 'age' => { '$gte' => 10, '$lte' => 15 }
2907
+ }
2908
+ } },
2909
+ { 'labels' => 8 }
2910
+ ])
2911
2911
  end
2912
2912
  end
2913
2913
 
2914
- context "when the association is aliased" do
2914
+ context 'when the association is aliased' do
2915
2915
  let(:criteria) do
2916
- Person.where("passport.passport_pages.num_stamps" => 10..18)
2916
+ Person.where('passport.passport_pages.num_stamps' => 10..18)
2917
2917
  end
2918
2918
 
2919
- it "correctly uses the aliased association" do
2919
+ it 'correctly uses the aliased association' do
2920
2920
  expect(criteria.selector).to eq(
2921
- "pass.passport_pages" => {
2922
- "$elemMatch" => {
2923
- "num_stamps" => { "$gte" => 10, "$lte" => 18 }
2921
+ 'pass.passport_pages' => {
2922
+ '$elemMatch' => {
2923
+ 'num_stamps' => { '$gte' => 10, '$lte' => 18 }
2924
2924
  }
2925
2925
  }
2926
2926
  )
@@ -2928,24 +2928,23 @@ describe Mongoid::Criteria do
2928
2928
  end
2929
2929
  end
2930
2930
 
2931
- context "when searching for a regex on a symbol field" do
2932
- let!(:person) { Person.create!(species: :hello)}
2931
+ context 'when searching for a regex on a symbol field' do
2932
+ let!(:person) { Person.create!(species: :hello) }
2933
2933
  let(:criteria) { Person.where(species: /ell/) }
2934
2934
 
2935
- it "creates the correct criteria" do
2936
- expect(criteria.selector).to eq({ "species" => /ell/ })
2935
+ it 'creates the correct criteria' do
2936
+ expect(criteria.selector).to eq({ 'species' => /ell/ })
2937
2937
  end
2938
2938
 
2939
- it "finds the document" do
2939
+ it 'finds the document' do
2940
2940
  expect(criteria.first).to eq(person)
2941
2941
  end
2942
2942
  end
2943
2943
  end
2944
2944
 
2945
- describe "#for_js" do
2946
-
2945
+ describe '#for_js' do
2947
2946
  let!(:match) do
2948
- Band.create!(name: "Depeche Mode")
2947
+ Band.create!(name: 'Depeche Mode')
2949
2948
  end
2950
2949
 
2951
2950
  it 'is deprecated' do
@@ -2954,44 +2953,41 @@ describe Mongoid::Criteria do
2954
2953
  Band.for_js("this.name == 'Depeche Mode'")
2955
2954
  end
2956
2955
 
2957
- context "when the code has no scope" do
2958
-
2956
+ context 'when the code has no scope' do
2959
2957
  let(:criteria) do
2960
2958
  Band.for_js("this.name == 'Depeche Mode'")
2961
2959
  end
2962
2960
 
2963
- it "returns the matching documents" do
2961
+ it 'returns the matching documents' do
2964
2962
  expect(criteria).to eq([ match ])
2965
2963
  end
2966
2964
  end
2967
2965
 
2968
- context "when the code has scope" do
2966
+ context 'when the code has scope' do
2969
2967
  max_server_version '4.2'
2970
2968
 
2971
2969
  let(:criteria) do
2972
- Band.for_js("this.name == param", param: "Depeche Mode")
2970
+ Band.for_js('this.name == param', param: 'Depeche Mode')
2973
2971
  end
2974
2972
 
2975
- it "returns the matching documents" do
2973
+ it 'returns the matching documents' do
2976
2974
  expect(criteria).to eq([ match ])
2977
2975
  end
2978
2976
  end
2979
2977
  end
2980
2978
 
2981
- describe "#method_missing" do
2982
-
2979
+ describe '#method_missing' do
2983
2980
  let(:criteria) do
2984
2981
  Person.all
2985
2982
  end
2986
2983
 
2987
- context "when the method exists on the class" do
2988
-
2984
+ context 'when the method exists on the class' do
2989
2985
  before do
2990
2986
  expect(Person).to receive(:minor).and_call_original
2991
2987
  expect(Person).to receive(:older_than).and_call_original
2992
2988
  end
2993
2989
 
2994
- it "calls the method on the class" do
2990
+ it 'calls the method on the class' do
2995
2991
  expect(criteria.minor).to be_empty
2996
2992
  expect do
2997
2993
  criteria.older_than(age: 25)
@@ -2999,66 +2995,61 @@ describe Mongoid::Criteria do
2999
2995
  end
3000
2996
  end
3001
2997
 
3002
- context "when the method exists on the criteria" do
3003
-
2998
+ context 'when the method exists on the criteria' do
3004
2999
  before do
3005
3000
  expect(criteria).to receive(:only).and_call_original
3006
3001
  end
3007
3002
 
3008
- it "calls the method on the criteria" do
3003
+ it 'calls the method on the criteria' do
3009
3004
  expect(criteria.only).to eq(criteria)
3010
3005
  end
3011
3006
  end
3012
3007
 
3013
- context "when the method exists on array" do
3014
-
3008
+ context 'when the method exists on array' do
3015
3009
  before do
3016
3010
  expect(criteria).to receive(:entries).and_call_original
3017
3011
  end
3018
3012
 
3019
- it "calls the method on the criteria" do
3013
+ it 'calls the method on the criteria' do
3020
3014
  expect(criteria.at(0)).to be_nil
3021
3015
  end
3022
3016
  end
3023
3017
 
3024
- context "when the method does not exist" do
3025
-
3018
+ context 'when the method does not exist' do
3026
3019
  before do
3027
- expect(criteria).to receive(:entries).never
3020
+ expect(criteria).not_to receive(:entries)
3028
3021
  end
3029
3022
 
3030
- it "raises an error" do
3031
- expect {
3023
+ it 'raises an error' do
3024
+ expect do
3032
3025
  criteria.to_hash
3033
- }.to raise_error(NoMethodError)
3026
+ end.to raise_error(NoMethodError)
3034
3027
  end
3035
3028
  end
3036
3029
  end
3037
3030
 
3038
- describe "#uniq" do
3039
-
3031
+ describe '#uniq' do
3040
3032
  let!(:band_one) do
3041
- Band.create!(name: "New Order")
3033
+ Band.create!(name: 'New Order')
3042
3034
  end
3043
3035
 
3044
3036
  let!(:band_two) do
3045
- Band.create!(name: "New Order")
3037
+ Band.create!(name: 'New Order')
3046
3038
  end
3047
3039
 
3048
3040
  let(:criteria) do
3049
3041
  Band.all
3050
3042
  end
3051
3043
 
3052
- it "passes the block through method_missing" do
3044
+ it 'passes the block through method_missing' do
3053
3045
  expect(criteria.uniq(&:name)).to eq([ band_one ])
3054
3046
  end
3055
3047
  end
3056
3048
 
3057
- describe "#with" do
3058
-
3049
+ describe '#with' do
3059
3050
  let!(:criteria_and_collection) do
3060
3051
  collection = nil
3061
- criteria = Band.where(name: "Depeche Mode").with(collection: "artists") do |crit|
3052
+ criteria = Band.where(name: 'Depeche Mode').with(collection: 'artists') do |crit|
3062
3053
  collection = crit.collection
3063
3054
  crit
3064
3055
  end
@@ -3073,19 +3064,17 @@ describe Mongoid::Criteria do
3073
3064
  criteria_and_collection[1]
3074
3065
  end
3075
3066
 
3076
- it "retains the criteria selection" do
3077
- expect(criteria.selector).to eq("name" => "Depeche Mode")
3067
+ it 'retains the criteria selection' do
3068
+ expect(criteria.selector).to eq('name' => 'Depeche Mode')
3078
3069
  end
3079
3070
 
3080
- it "sets the persistence options" do
3081
- expect(collection.name).to eq("artists")
3071
+ it 'sets the persistence options' do
3072
+ expect(collection.name).to eq('artists')
3082
3073
  end
3083
3074
  end
3084
3075
 
3085
- describe "#geo_spatial" do
3086
-
3087
- context "when checking within a polygon" do
3088
-
3076
+ describe '#geo_spatial' do
3077
+ context 'when checking within a polygon' do
3089
3078
  before do
3090
3079
  Bar.create_indexes
3091
3080
  end
@@ -3096,54 +3085,51 @@ describe Mongoid::Criteria do
3096
3085
 
3097
3086
  let(:criteria) do
3098
3087
  Bar.geo_spatial(
3099
- :location.within_polygon => [[[ 50, 10 ], [ 50, 20 ], [ 60, 20 ], [ 60, 10 ], [ 50, 10 ]]]
3088
+ :location.within_polygon => [ [ [ 50, 10 ], [ 50, 20 ], [ 60, 20 ], [ 60, 10 ], [ 50, 10 ] ] ]
3100
3089
  )
3101
3090
  end
3102
3091
 
3103
- it "returns the matching documents" do
3092
+ it 'returns the matching documents' do
3104
3093
  expect(criteria).to eq([ match ])
3105
3094
  end
3106
3095
  end
3107
3096
  end
3108
3097
 
3109
- describe "#with_size" do
3110
-
3098
+ describe '#with_size' do
3111
3099
  let!(:match) do
3112
- Band.create!(genres: [ "electro", "dub" ])
3100
+ Band.create!(genres: %w[electro dub])
3113
3101
  end
3114
3102
 
3115
3103
  let!(:non_match) do
3116
- Band.create!(genres: [ "house" ])
3104
+ Band.create!(genres: [ 'house' ])
3117
3105
  end
3118
3106
 
3119
3107
  let(:criteria) do
3120
3108
  Band.with_size(genres: 2)
3121
3109
  end
3122
3110
 
3123
- it "returns the matching documents" do
3111
+ it 'returns the matching documents' do
3124
3112
  expect(criteria).to eq([ match ])
3125
3113
  end
3126
3114
  end
3127
3115
 
3128
- describe "#with_type" do
3129
-
3116
+ describe '#with_type' do
3130
3117
  let!(:match) do
3131
- Band.create!(name: "Depeche Mode")
3118
+ Band.create!(name: 'Depeche Mode')
3132
3119
  end
3133
3120
 
3134
3121
  let(:criteria) do
3135
3122
  Band.with_type(name: 2)
3136
3123
  end
3137
3124
 
3138
- it "returns the matching documents" do
3125
+ it 'returns the matching documents' do
3139
3126
  expect(criteria).to eq([ match ])
3140
3127
  end
3141
3128
  end
3142
3129
 
3143
- describe "#type_selection" do
3144
- context "when using the default discriminator_key" do
3145
- context "when only one subclass exists" do
3146
-
3130
+ describe '#type_selection' do
3131
+ context 'when using the default discriminator_key' do
3132
+ context 'when only one subclass exists' do
3147
3133
  let(:criteria) do
3148
3134
  described_class.new(Firefox)
3149
3135
  end
@@ -3152,13 +3138,12 @@ describe Mongoid::Criteria do
3152
3138
  criteria.send(:type_selection)
3153
3139
  end
3154
3140
 
3155
- it "does not use an $in query" do
3156
- expect(selection).to eq({ _type: "Firefox" })
3141
+ it 'does not use an $in query' do
3142
+ expect(selection).to eq({ _type: 'Firefox' })
3157
3143
  end
3158
3144
  end
3159
3145
 
3160
- context "when more than one subclass exists" do
3161
-
3146
+ context 'when more than one subclass exists' do
3162
3147
  let(:criteria) do
3163
3148
  described_class.new(Browser)
3164
3149
  end
@@ -3167,23 +3152,22 @@ describe Mongoid::Criteria do
3167
3152
  criteria.send(:type_selection)
3168
3153
  end
3169
3154
 
3170
- it "does not use an $in query" do
3171
- expect(selection).to eq({ _type: { "$in" => [ "Firefox", "Browser" ]}})
3155
+ it 'does not use an $in query' do
3156
+ expect(selection).to eq({ _type: { '$in' => %w[Firefox Browser] } })
3172
3157
  end
3173
3158
  end
3174
3159
  end
3175
3160
 
3176
- context "when using a custom discriminator_key" do
3161
+ context 'when using a custom discriminator_key' do
3177
3162
  before do
3178
- Canvas.discriminator_key = "dkey"
3163
+ Canvas.discriminator_key = 'dkey'
3179
3164
  end
3180
3165
 
3181
3166
  after do
3182
3167
  Canvas.discriminator_key = nil
3183
3168
  end
3184
3169
 
3185
- context "when only one subclass exists" do
3186
-
3170
+ context 'when only one subclass exists' do
3187
3171
  let(:criteria) do
3188
3172
  described_class.new(Firefox)
3189
3173
  end
@@ -3192,13 +3176,12 @@ describe Mongoid::Criteria do
3192
3176
  criteria.send(:type_selection)
3193
3177
  end
3194
3178
 
3195
- it "does not use an $in query" do
3196
- expect(selection).to eq({ dkey: "Firefox" })
3179
+ it 'does not use an $in query' do
3180
+ expect(selection).to eq({ dkey: 'Firefox' })
3197
3181
  end
3198
3182
  end
3199
3183
 
3200
- context "when more than one subclass exists" do
3201
-
3184
+ context 'when more than one subclass exists' do
3202
3185
  let(:criteria) do
3203
3186
  described_class.new(Browser)
3204
3187
  end
@@ -3207,8 +3190,8 @@ describe Mongoid::Criteria do
3207
3190
  criteria.send(:type_selection)
3208
3191
  end
3209
3192
 
3210
- it "does not use an $in query" do
3211
- expect(selection).to eq({ dkey: { "$in" => [ "Firefox", "Browser" ]}})
3193
+ it 'does not use an $in query' do
3194
+ expect(selection).to eq({ dkey: { '$in' => %w[Firefox Browser] } })
3212
3195
  end
3213
3196
  end
3214
3197
  end
@@ -3278,13 +3261,13 @@ describe Mongoid::Criteria do
3278
3261
  {
3279
3262
  klass: Band,
3280
3263
  gt: { members: 2 },
3281
- in: { genre: ['rock', 'metal'] }
3264
+ in: { genre: %w[rock metal] }
3282
3265
  }
3283
3266
  end
3284
3267
 
3285
3268
  it 'applies selector methods' do
3286
3269
  expect(criteria.selector['members']).to eq({ '$gt' => 2 })
3287
- expect(criteria.selector['genre']).to eq({ '$in' => ['rock', 'metal'] })
3270
+ expect(criteria.selector['genre']).to eq({ '$in' => %w[rock metal] })
3288
3271
  end
3289
3272
  end
3290
3273
 
@@ -3315,7 +3298,7 @@ describe Mongoid::Criteria do
3315
3298
 
3316
3299
  context 'when attempting to call create!' do
3317
3300
  let(:hash) do
3318
- { klass: Band, 'create!': { name: 'Malicious' } }
3301
+ { klass: Band, create!: { name: 'Malicious' } }
3319
3302
  end
3320
3303
 
3321
3304
  it 'raises ArgumentError' do
@@ -3345,7 +3328,7 @@ describe Mongoid::Criteria do
3345
3328
 
3346
3329
  context 'when attempting to call execute_or_raise' do
3347
3330
  let(:hash) do
3348
- { klass: Band, execute_or_raise: ['id1', 'id2'] }
3331
+ { klass: Band, execute_or_raise: %w[id1 id2] }
3349
3332
  end
3350
3333
 
3351
3334
  it 'raises ArgumentError' do
@@ -3431,20 +3414,20 @@ describe Mongoid::Criteria do
3431
3414
  it 'allows all whitelisted methods' do
3432
3415
  # Sample of allowed methods from each category
3433
3416
  allowed_sample = {
3434
- where: { name: 'Test' }, # Query selector
3417
+ where: { name: 'Test' }, # Query selector
3435
3418
  limit: 10, # Query option
3436
3419
  skip: 5, # Query option
3437
- gt: { age: 18 }, # Query selector
3438
- in: { status: ['active'] }, # Query selector
3439
- ascending: :name, # Sorting
3440
- includes: :notes, # Eager loading
3441
- merge: { klass: Band }, # Merge
3420
+ gt: { age: 18 }, # Query selector
3421
+ in: { status: [ 'active' ] }, # Query selector
3422
+ ascending: :name, # Sorting
3423
+ includes: :notes, # Eager loading
3424
+ merge: { klass: Band } # Merge
3442
3425
  }
3443
3426
 
3444
3427
  allowed_sample.each do |method, args|
3445
3428
  hash = { klass: Band, method => args }
3446
- expect { described_class.from_hash(hash) }.not_to raise_error,
3447
- "Expected method '#{method}' to be allowed but it was blocked"
3429
+ expect { described_class.from_hash(hash) }.not_to raise_error
3430
+ # "Expected method '#{method}' to be allowed but it was blocked"
3448
3431
  end
3449
3432
  end
3450
3433
  end