mongoid-ultra 9.0.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (1086) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +22 -0
  3. data/README.md +156 -0
  4. data/Rakefile +34 -0
  5. data/lib/config/locales/en.yml +706 -0
  6. data/lib/mongoid/association/accessors.rb +430 -0
  7. data/lib/mongoid/association/bindable.rb +227 -0
  8. data/lib/mongoid/association/builders.rb +86 -0
  9. data/lib/mongoid/association/constrainable.rb +42 -0
  10. data/lib/mongoid/association/depending.rb +128 -0
  11. data/lib/mongoid/association/eager.rb +159 -0
  12. data/lib/mongoid/association/eager_loadable.rb +58 -0
  13. data/lib/mongoid/association/embedded/batchable.rb +396 -0
  14. data/lib/mongoid/association/embedded/cyclic.rb +102 -0
  15. data/lib/mongoid/association/embedded/eager.rb +22 -0
  16. data/lib/mongoid/association/embedded/embedded_in/binding.rb +74 -0
  17. data/lib/mongoid/association/embedded/embedded_in/buildable.rb +38 -0
  18. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +124 -0
  19. data/lib/mongoid/association/embedded/embedded_in.rb +141 -0
  20. data/lib/mongoid/association/embedded/embeds_many/binding.rb +42 -0
  21. data/lib/mongoid/association/embedded/embeds_many/buildable.rb +46 -0
  22. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +584 -0
  23. data/lib/mongoid/association/embedded/embeds_many.rb +183 -0
  24. data/lib/mongoid/association/embedded/embeds_one/binding.rb +43 -0
  25. data/lib/mongoid/association/embedded/embeds_one/buildable.rb +53 -0
  26. data/lib/mongoid/association/embedded/embeds_one/proxy.rb +183 -0
  27. data/lib/mongoid/association/embedded/embeds_one.rb +150 -0
  28. data/lib/mongoid/association/embedded.rb +7 -0
  29. data/lib/mongoid/association/macros.rb +228 -0
  30. data/lib/mongoid/association/many.rb +202 -0
  31. data/lib/mongoid/association/marshalable.rb +31 -0
  32. data/lib/mongoid/association/nested/many.rb +192 -0
  33. data/lib/mongoid/association/nested/nested_buildable.rb +66 -0
  34. data/lib/mongoid/association/nested/one.rb +154 -0
  35. data/lib/mongoid/association/nested.rb +15 -0
  36. data/lib/mongoid/association/one.rb +53 -0
  37. data/lib/mongoid/association/options.rb +127 -0
  38. data/lib/mongoid/association/proxy.rb +200 -0
  39. data/lib/mongoid/association/referenced/auto_save.rb +74 -0
  40. data/lib/mongoid/association/referenced/belongs_to/binding.rb +83 -0
  41. data/lib/mongoid/association/referenced/belongs_to/buildable.rb +48 -0
  42. data/lib/mongoid/association/referenced/belongs_to/eager.rb +73 -0
  43. data/lib/mongoid/association/referenced/belongs_to/proxy.rb +124 -0
  44. data/lib/mongoid/association/referenced/belongs_to.rb +226 -0
  45. data/lib/mongoid/association/referenced/counter_cache.rb +152 -0
  46. data/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb +72 -0
  47. data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +40 -0
  48. data/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb +53 -0
  49. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +345 -0
  50. data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +277 -0
  51. data/lib/mongoid/association/referenced/has_many/binding.rb +36 -0
  52. data/lib/mongoid/association/referenced/has_many/buildable.rb +38 -0
  53. data/lib/mongoid/association/referenced/has_many/eager.rb +44 -0
  54. data/lib/mongoid/association/referenced/has_many/enumerable.rb +490 -0
  55. data/lib/mongoid/association/referenced/has_many/proxy.rb +552 -0
  56. data/lib/mongoid/association/referenced/has_many.rb +251 -0
  57. data/lib/mongoid/association/referenced/has_one/binding.rb +42 -0
  58. data/lib/mongoid/association/referenced/has_one/buildable.rb +74 -0
  59. data/lib/mongoid/association/referenced/has_one/eager.rb +36 -0
  60. data/lib/mongoid/association/referenced/has_one/proxy.rb +108 -0
  61. data/lib/mongoid/association/referenced/has_one.rb +184 -0
  62. data/lib/mongoid/association/referenced/syncable.rb +155 -0
  63. data/lib/mongoid/association/referenced.rb +9 -0
  64. data/lib/mongoid/association/reflections.rb +69 -0
  65. data/lib/mongoid/association/relatable.rb +489 -0
  66. data/lib/mongoid/association.rb +135 -0
  67. data/lib/mongoid/atomic/modifiers.rb +297 -0
  68. data/lib/mongoid/atomic/paths/embedded/many.rb +60 -0
  69. data/lib/mongoid/atomic/paths/embedded/one.rb +40 -0
  70. data/lib/mongoid/atomic/paths/embedded.rb +27 -0
  71. data/lib/mongoid/atomic/paths/root.rb +36 -0
  72. data/lib/mongoid/atomic/paths.rb +4 -0
  73. data/lib/mongoid/atomic.rb +332 -0
  74. data/lib/mongoid/attributes/dynamic.rb +141 -0
  75. data/lib/mongoid/attributes/nested.rb +93 -0
  76. data/lib/mongoid/attributes/processing.rb +139 -0
  77. data/lib/mongoid/attributes/projector.rb +119 -0
  78. data/lib/mongoid/attributes/readonly.rb +73 -0
  79. data/lib/mongoid/attributes.rb +368 -0
  80. data/lib/mongoid/cacheable.rb +33 -0
  81. data/lib/mongoid/changeable.rb +520 -0
  82. data/lib/mongoid/clients/factory.rb +115 -0
  83. data/lib/mongoid/clients/options.rb +119 -0
  84. data/lib/mongoid/clients/sessions.rb +146 -0
  85. data/lib/mongoid/clients/storage_options.rb +76 -0
  86. data/lib/mongoid/clients/validators/storage.rb +46 -0
  87. data/lib/mongoid/clients/validators.rb +3 -0
  88. data/lib/mongoid/clients.rb +79 -0
  89. data/lib/mongoid/collection_configurable.rb +58 -0
  90. data/lib/mongoid/composable.rb +132 -0
  91. data/lib/mongoid/config/defaults.rb +45 -0
  92. data/lib/mongoid/config/encryption.rb +203 -0
  93. data/lib/mongoid/config/environment.rb +82 -0
  94. data/lib/mongoid/config/introspection.rb +150 -0
  95. data/lib/mongoid/config/options.rb +86 -0
  96. data/lib/mongoid/config/validators/async_query_executor.rb +24 -0
  97. data/lib/mongoid/config/validators/client.rb +128 -0
  98. data/lib/mongoid/config/validators/option.rb +24 -0
  99. data/lib/mongoid/config/validators.rb +5 -0
  100. data/lib/mongoid/config.rb +380 -0
  101. data/lib/mongoid/contextual/aggregable/memory.rb +122 -0
  102. data/lib/mongoid/contextual/aggregable/mongo.rb +140 -0
  103. data/lib/mongoid/contextual/aggregable/none.rb +65 -0
  104. data/lib/mongoid/contextual/aggregable.rb +17 -0
  105. data/lib/mongoid/contextual/atomic.rb +245 -0
  106. data/lib/mongoid/contextual/command.rb +32 -0
  107. data/lib/mongoid/contextual/geo_near.rb +236 -0
  108. data/lib/mongoid/contextual/map_reduce.rb +230 -0
  109. data/lib/mongoid/contextual/memory.rb +770 -0
  110. data/lib/mongoid/contextual/mongo/documents_loader.rb +177 -0
  111. data/lib/mongoid/contextual/mongo.rb +1065 -0
  112. data/lib/mongoid/contextual/none.rb +326 -0
  113. data/lib/mongoid/contextual/queryable.rb +24 -0
  114. data/lib/mongoid/contextual.rb +66 -0
  115. data/lib/mongoid/copyable.rb +115 -0
  116. data/lib/mongoid/criteria/findable.rb +143 -0
  117. data/lib/mongoid/criteria/includable.rb +93 -0
  118. data/lib/mongoid/criteria/inspectable.rb +24 -0
  119. data/lib/mongoid/criteria/marshalable.rb +55 -0
  120. data/lib/mongoid/criteria/modifiable.rb +235 -0
  121. data/lib/mongoid/criteria/options.rb +24 -0
  122. data/lib/mongoid/criteria/permission.rb +71 -0
  123. data/lib/mongoid/criteria/queryable/aggregable.rb +109 -0
  124. data/lib/mongoid/criteria/queryable/expandable.rb +68 -0
  125. data/lib/mongoid/criteria/queryable/extensions/array.rb +151 -0
  126. data/lib/mongoid/criteria/queryable/extensions/big_decimal.rb +55 -0
  127. data/lib/mongoid/criteria/queryable/extensions/boolean.rb +33 -0
  128. data/lib/mongoid/criteria/queryable/extensions/date.rb +63 -0
  129. data/lib/mongoid/criteria/queryable/extensions/date_time.rb +52 -0
  130. data/lib/mongoid/criteria/queryable/extensions/hash.rb +163 -0
  131. data/lib/mongoid/criteria/queryable/extensions/nil_class.rb +75 -0
  132. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +73 -0
  133. data/lib/mongoid/criteria/queryable/extensions/object.rb +182 -0
  134. data/lib/mongoid/criteria/queryable/extensions/range.rb +102 -0
  135. data/lib/mongoid/criteria/queryable/extensions/regexp.rb +72 -0
  136. data/lib/mongoid/criteria/queryable/extensions/set.rb +33 -0
  137. data/lib/mongoid/criteria/queryable/extensions/string.rb +112 -0
  138. data/lib/mongoid/criteria/queryable/extensions/symbol.rb +64 -0
  139. data/lib/mongoid/criteria/queryable/extensions/time.rb +60 -0
  140. data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +66 -0
  141. data/lib/mongoid/criteria/queryable/extensions.rb +25 -0
  142. data/lib/mongoid/criteria/queryable/key.rb +177 -0
  143. data/lib/mongoid/criteria/queryable/macroable.rb +26 -0
  144. data/lib/mongoid/criteria/queryable/mergeable.rb +434 -0
  145. data/lib/mongoid/criteria/queryable/optional.rb +380 -0
  146. data/lib/mongoid/criteria/queryable/options.rb +136 -0
  147. data/lib/mongoid/criteria/queryable/pipeline.rb +106 -0
  148. data/lib/mongoid/criteria/queryable/selectable.rb +952 -0
  149. data/lib/mongoid/criteria/queryable/selector.rb +289 -0
  150. data/lib/mongoid/criteria/queryable/smash.rb +128 -0
  151. data/lib/mongoid/criteria/queryable/storable.rb +237 -0
  152. data/lib/mongoid/criteria/queryable.rb +90 -0
  153. data/lib/mongoid/criteria/scopable.rb +166 -0
  154. data/lib/mongoid/criteria/translator.rb +45 -0
  155. data/lib/mongoid/criteria.rb +570 -0
  156. data/lib/mongoid/deprecable.rb +36 -0
  157. data/lib/mongoid/deprecation.rb +25 -0
  158. data/lib/mongoid/document.rb +389 -0
  159. data/lib/mongoid/encryptable.rb +49 -0
  160. data/lib/mongoid/equality.rb +63 -0
  161. data/lib/mongoid/errors/ambiguous_relationship.rb +50 -0
  162. data/lib/mongoid/errors/attribute_not_loaded.rb +33 -0
  163. data/lib/mongoid/errors/callback.rb +24 -0
  164. data/lib/mongoid/errors/create_collection_failure.rb +33 -0
  165. data/lib/mongoid/errors/criteria_argument_required.rb +18 -0
  166. data/lib/mongoid/errors/delete_restriction.rb +26 -0
  167. data/lib/mongoid/errors/document_not_destroyed.rb +22 -0
  168. data/lib/mongoid/errors/document_not_found.rb +125 -0
  169. data/lib/mongoid/errors/drop_collection_failure.rb +27 -0
  170. data/lib/mongoid/errors/empty_config_file.rb +25 -0
  171. data/lib/mongoid/errors/immutable_attribute.rb +26 -0
  172. data/lib/mongoid/errors/in_memory_collation_not_supported.rb +19 -0
  173. data/lib/mongoid/errors/invalid_async_query_executor.rb +25 -0
  174. data/lib/mongoid/errors/invalid_auto_encryption_configuration.rb +32 -0
  175. data/lib/mongoid/errors/invalid_collection.rb +19 -0
  176. data/lib/mongoid/errors/invalid_config_file.rb +25 -0
  177. data/lib/mongoid/errors/invalid_config_option.rb +26 -0
  178. data/lib/mongoid/errors/invalid_dependent_strategy.rb +31 -0
  179. data/lib/mongoid/errors/invalid_discriminator_key_target.rb +24 -0
  180. data/lib/mongoid/errors/invalid_dot_dollar_assignment.rb +23 -0
  181. data/lib/mongoid/errors/invalid_elem_match_operator.rb +32 -0
  182. data/lib/mongoid/errors/invalid_estimated_count_criteria.rb +25 -0
  183. data/lib/mongoid/errors/invalid_estimated_count_scoping.rb +25 -0
  184. data/lib/mongoid/errors/invalid_expression_operator.rb +27 -0
  185. data/lib/mongoid/errors/invalid_field.rb +65 -0
  186. data/lib/mongoid/errors/invalid_field_operator.rb +32 -0
  187. data/lib/mongoid/errors/invalid_field_option.rb +34 -0
  188. data/lib/mongoid/errors/invalid_field_type.rb +26 -0
  189. data/lib/mongoid/errors/invalid_find.rb +18 -0
  190. data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +22 -0
  191. data/lib/mongoid/errors/invalid_includes.rb +31 -0
  192. data/lib/mongoid/errors/invalid_index.rb +27 -0
  193. data/lib/mongoid/errors/invalid_options.rb +27 -0
  194. data/lib/mongoid/errors/invalid_path.rb +20 -0
  195. data/lib/mongoid/errors/invalid_persistence_option.rb +26 -0
  196. data/lib/mongoid/errors/invalid_query.rb +40 -0
  197. data/lib/mongoid/errors/invalid_relation.rb +61 -0
  198. data/lib/mongoid/errors/invalid_relation_option.rb +28 -0
  199. data/lib/mongoid/errors/invalid_scope.rb +23 -0
  200. data/lib/mongoid/errors/invalid_session_nesting.rb +16 -0
  201. data/lib/mongoid/errors/invalid_set_polymorphic_relation.rb +39 -0
  202. data/lib/mongoid/errors/invalid_storage_options.rb +26 -0
  203. data/lib/mongoid/errors/invalid_storage_parent.rb +27 -0
  204. data/lib/mongoid/errors/invalid_time.rb +21 -0
  205. data/lib/mongoid/errors/invalid_transaction_nesting.rb +16 -0
  206. data/lib/mongoid/errors/inverse_not_found.rb +28 -0
  207. data/lib/mongoid/errors/mixed_client_configuration.rb +27 -0
  208. data/lib/mongoid/errors/mixed_relations.rb +31 -0
  209. data/lib/mongoid/errors/mongoid_error.rb +91 -0
  210. data/lib/mongoid/errors/nested_attributes_metadata_not_found.rb +27 -0
  211. data/lib/mongoid/errors/no_client_config.rb +21 -0
  212. data/lib/mongoid/errors/no_client_database.rb +26 -0
  213. data/lib/mongoid/errors/no_client_hosts.rb +26 -0
  214. data/lib/mongoid/errors/no_clients_config.rb +19 -0
  215. data/lib/mongoid/errors/no_default_client.rb +22 -0
  216. data/lib/mongoid/errors/no_environment.rb +18 -0
  217. data/lib/mongoid/errors/no_map_reduce_output.rb +23 -0
  218. data/lib/mongoid/errors/no_metadata.rb +20 -0
  219. data/lib/mongoid/errors/no_parent.rb +23 -0
  220. data/lib/mongoid/errors/readonly_attribute.rb +24 -0
  221. data/lib/mongoid/errors/readonly_document.rb +21 -0
  222. data/lib/mongoid/errors/rollback.rb +14 -0
  223. data/lib/mongoid/errors/scope_overwrite.rb +22 -0
  224. data/lib/mongoid/errors/sessions_not_supported.rb +16 -0
  225. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +22 -0
  226. data/lib/mongoid/errors/transaction_error.rb +22 -0
  227. data/lib/mongoid/errors/transactions_not_supported.rb +16 -0
  228. data/lib/mongoid/errors/unknown_attribute.rb +24 -0
  229. data/lib/mongoid/errors/unknown_model.rb +24 -0
  230. data/lib/mongoid/errors/unsaved_document.rb +20 -0
  231. data/lib/mongoid/errors/unsupported_javascript.rb +26 -0
  232. data/lib/mongoid/errors/validations.rb +30 -0
  233. data/lib/mongoid/errors.rb +73 -0
  234. data/lib/mongoid/evolvable.rb +18 -0
  235. data/lib/mongoid/extensions/array.rb +173 -0
  236. data/lib/mongoid/extensions/big_decimal.rb +90 -0
  237. data/lib/mongoid/extensions/binary.rb +42 -0
  238. data/lib/mongoid/extensions/boolean.rb +26 -0
  239. data/lib/mongoid/extensions/date.rb +87 -0
  240. data/lib/mongoid/extensions/date_time.rb +60 -0
  241. data/lib/mongoid/extensions/decimal128.rb +36 -0
  242. data/lib/mongoid/extensions/false_class.rb +35 -0
  243. data/lib/mongoid/extensions/float.rb +55 -0
  244. data/lib/mongoid/extensions/hash.rb +248 -0
  245. data/lib/mongoid/extensions/integer.rb +63 -0
  246. data/lib/mongoid/extensions/module.rb +27 -0
  247. data/lib/mongoid/extensions/nil_class.rb +30 -0
  248. data/lib/mongoid/extensions/object.rb +250 -0
  249. data/lib/mongoid/extensions/object_id.rb +49 -0
  250. data/lib/mongoid/extensions/range.rb +108 -0
  251. data/lib/mongoid/extensions/raw_value.rb +32 -0
  252. data/lib/mongoid/extensions/regexp.rb +34 -0
  253. data/lib/mongoid/extensions/set.rb +57 -0
  254. data/lib/mongoid/extensions/string.rb +171 -0
  255. data/lib/mongoid/extensions/symbol.rb +38 -0
  256. data/lib/mongoid/extensions/time.rb +105 -0
  257. data/lib/mongoid/extensions/time_with_zone.rb +74 -0
  258. data/lib/mongoid/extensions/true_class.rb +35 -0
  259. data/lib/mongoid/extensions.rb +27 -0
  260. data/lib/mongoid/factory.rb +134 -0
  261. data/lib/mongoid/fields/encrypted.rb +41 -0
  262. data/lib/mongoid/fields/foreign_key.rb +166 -0
  263. data/lib/mongoid/fields/localized.rb +99 -0
  264. data/lib/mongoid/fields/standard.rb +241 -0
  265. data/lib/mongoid/fields/validators/macro.rb +125 -0
  266. data/lib/mongoid/fields/validators.rb +3 -0
  267. data/lib/mongoid/fields.rb +851 -0
  268. data/lib/mongoid/findable.rb +243 -0
  269. data/lib/mongoid/indexable/specification.rb +138 -0
  270. data/lib/mongoid/indexable/validators/options.rb +110 -0
  271. data/lib/mongoid/indexable.rb +148 -0
  272. data/lib/mongoid/inspectable.rb +52 -0
  273. data/lib/mongoid/interceptable.rb +320 -0
  274. data/lib/mongoid/loadable.rb +82 -0
  275. data/lib/mongoid/loggable.rb +66 -0
  276. data/lib/mongoid/matchable.rb +23 -0
  277. data/lib/mongoid/matcher/all.rb +22 -0
  278. data/lib/mongoid/matcher/and.rb +21 -0
  279. data/lib/mongoid/matcher/bits.rb +41 -0
  280. data/lib/mongoid/matcher/bits_all_clear.rb +20 -0
  281. data/lib/mongoid/matcher/bits_all_set.rb +20 -0
  282. data/lib/mongoid/matcher/bits_any_clear.rb +20 -0
  283. data/lib/mongoid/matcher/bits_any_set.rb +20 -0
  284. data/lib/mongoid/matcher/elem_match.rb +36 -0
  285. data/lib/mongoid/matcher/elem_match_expression.rb +20 -0
  286. data/lib/mongoid/matcher/eq.rb +11 -0
  287. data/lib/mongoid/matcher/eq_impl.rb +62 -0
  288. data/lib/mongoid/matcher/eq_impl_with_regexp.rb +25 -0
  289. data/lib/mongoid/matcher/exists.rb +15 -0
  290. data/lib/mongoid/matcher/expression.rb +35 -0
  291. data/lib/mongoid/matcher/expression_operator.rb +19 -0
  292. data/lib/mongoid/matcher/field_expression.rb +62 -0
  293. data/lib/mongoid/matcher/field_operator.rb +54 -0
  294. data/lib/mongoid/matcher/gt.rb +17 -0
  295. data/lib/mongoid/matcher/gte.rb +17 -0
  296. data/lib/mongoid/matcher/in.rb +25 -0
  297. data/lib/mongoid/matcher/lt.rb +17 -0
  298. data/lib/mongoid/matcher/lte.rb +17 -0
  299. data/lib/mongoid/matcher/mod.rb +17 -0
  300. data/lib/mongoid/matcher/ne.rb +16 -0
  301. data/lib/mongoid/matcher/nin.rb +11 -0
  302. data/lib/mongoid/matcher/nor.rb +25 -0
  303. data/lib/mongoid/matcher/not.rb +29 -0
  304. data/lib/mongoid/matcher/or.rb +21 -0
  305. data/lib/mongoid/matcher/regex.rb +41 -0
  306. data/lib/mongoid/matcher/size.rb +26 -0
  307. data/lib/mongoid/matcher/type.rb +99 -0
  308. data/lib/mongoid/matcher.rb +115 -0
  309. data/lib/mongoid/persistable/creatable.rb +184 -0
  310. data/lib/mongoid/persistable/deletable.rb +136 -0
  311. data/lib/mongoid/persistable/destroyable.rb +80 -0
  312. data/lib/mongoid/persistable/incrementable.rb +35 -0
  313. data/lib/mongoid/persistable/logical.rb +36 -0
  314. data/lib/mongoid/persistable/maxable.rb +36 -0
  315. data/lib/mongoid/persistable/minable.rb +36 -0
  316. data/lib/mongoid/persistable/multipliable.rb +35 -0
  317. data/lib/mongoid/persistable/poppable.rb +36 -0
  318. data/lib/mongoid/persistable/pullable.rb +50 -0
  319. data/lib/mongoid/persistable/pushable.rb +66 -0
  320. data/lib/mongoid/persistable/renamable.rb +37 -0
  321. data/lib/mongoid/persistable/savable.rb +59 -0
  322. data/lib/mongoid/persistable/settable.rb +93 -0
  323. data/lib/mongoid/persistable/unsettable.rb +36 -0
  324. data/lib/mongoid/persistable/updatable.rb +233 -0
  325. data/lib/mongoid/persistable/upsertable.rb +82 -0
  326. data/lib/mongoid/persistable.rb +317 -0
  327. data/lib/mongoid/persistence_context.rb +275 -0
  328. data/lib/mongoid/positional.rb +72 -0
  329. data/lib/mongoid/query_cache.rb +64 -0
  330. data/lib/mongoid/railtie.rb +122 -0
  331. data/lib/mongoid/railties/console_sandbox.rb +41 -0
  332. data/lib/mongoid/railties/controller_runtime.rb +88 -0
  333. data/lib/mongoid/railties/database.rake +93 -0
  334. data/lib/mongoid/reloadable.rb +94 -0
  335. data/lib/mongoid/scopable.rb +323 -0
  336. data/lib/mongoid/selectable.rb +49 -0
  337. data/lib/mongoid/serializable.rb +170 -0
  338. data/lib/mongoid/shardable.rb +154 -0
  339. data/lib/mongoid/stateful.rb +155 -0
  340. data/lib/mongoid/stringified_symbol.rb +52 -0
  341. data/lib/mongoid/tasks/database.rake +51 -0
  342. data/lib/mongoid/tasks/database.rb +214 -0
  343. data/lib/mongoid/tasks/encryption.rake +19 -0
  344. data/lib/mongoid/tasks/encryption.rb +102 -0
  345. data/lib/mongoid/threaded/lifecycle.rb +150 -0
  346. data/lib/mongoid/threaded.rb +396 -0
  347. data/lib/mongoid/timestamps/created/short.rb +20 -0
  348. data/lib/mongoid/timestamps/created.rb +34 -0
  349. data/lib/mongoid/timestamps/short.rb +11 -0
  350. data/lib/mongoid/timestamps/timeless.rb +95 -0
  351. data/lib/mongoid/timestamps/updated/short.rb +20 -0
  352. data/lib/mongoid/timestamps/updated.rb +44 -0
  353. data/lib/mongoid/timestamps.rb +17 -0
  354. data/lib/mongoid/touchable.rb +228 -0
  355. data/lib/mongoid/traversable.rb +337 -0
  356. data/lib/mongoid/utils.rb +22 -0
  357. data/lib/mongoid/validatable/associated.rb +47 -0
  358. data/lib/mongoid/validatable/format.rb +21 -0
  359. data/lib/mongoid/validatable/length.rb +21 -0
  360. data/lib/mongoid/validatable/localizable.rb +29 -0
  361. data/lib/mongoid/validatable/macros.rb +89 -0
  362. data/lib/mongoid/validatable/presence.rb +81 -0
  363. data/lib/mongoid/validatable/queryable.rb +29 -0
  364. data/lib/mongoid/validatable/uniqueness.rb +304 -0
  365. data/lib/mongoid/validatable.rb +163 -0
  366. data/lib/mongoid/version.rb +5 -0
  367. data/lib/mongoid/warnings.rb +30 -0
  368. data/lib/mongoid.rb +157 -0
  369. data/lib/rails/generators/mongoid/config/config_generator.rb +36 -0
  370. data/lib/rails/generators/mongoid/config/templates/mongoid.rb +24 -0
  371. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +137 -0
  372. data/lib/rails/generators/mongoid/model/model_generator.rb +25 -0
  373. data/lib/rails/generators/mongoid/model/templates/model.rb.tt +19 -0
  374. data/lib/rails/generators/mongoid_generator.rb +29 -0
  375. data/lib/rails/mongoid.rb +27 -0
  376. data/spec/README.md +33 -0
  377. data/spec/config/mongoid.yml +70 -0
  378. data/spec/config/mongoid_with_schema_map_uuid.yml +27 -0
  379. data/spec/integration/app_spec.rb +377 -0
  380. data/spec/integration/associations/belongs_to_spec.rb +33 -0
  381. data/spec/integration/associations/embedded_dirty_spec.rb +57 -0
  382. data/spec/integration/associations/embedded_spec.rb +351 -0
  383. data/spec/integration/associations/embeds_many_spec.rb +219 -0
  384. data/spec/integration/associations/embeds_one_spec.rb +41 -0
  385. data/spec/integration/associations/foreign_key_spec.rb +107 -0
  386. data/spec/integration/associations/foreign_key_spec_models.rb +64 -0
  387. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +21 -0
  388. data/spec/integration/associations/has_many_spec.rb +128 -0
  389. data/spec/integration/associations/has_one_spec.rb +272 -0
  390. data/spec/integration/associations/nested_attributes_assignment_spec.rb +115 -0
  391. data/spec/integration/associations/reverse_population_spec.rb +34 -0
  392. data/spec/integration/associations/reverse_population_spec_models.rb +36 -0
  393. data/spec/integration/associations/scope_option_spec.rb +101 -0
  394. data/spec/integration/atomic/modifiers_spec.rb +116 -0
  395. data/spec/integration/bson_regexp_raw_spec.rb +19 -0
  396. data/spec/integration/callbacks_models.rb +155 -0
  397. data/spec/integration/callbacks_spec.rb +557 -0
  398. data/spec/integration/contextual/empty_spec.rb +141 -0
  399. data/spec/integration/criteria/alias_query_spec.rb +97 -0
  400. data/spec/integration/criteria/date_field_spec.rb +40 -0
  401. data/spec/integration/criteria/default_scope_spec.rb +72 -0
  402. data/spec/integration/criteria/logical_spec.rb +105 -0
  403. data/spec/integration/criteria/range_spec.rb +357 -0
  404. data/spec/integration/criteria/raw_value_spec.rb +525 -0
  405. data/spec/integration/criteria/time_with_zone_spec.rb +142 -0
  406. data/spec/integration/discriminator_key_spec.rb +391 -0
  407. data/spec/integration/discriminator_value_spec.rb +206 -0
  408. data/spec/integration/document_spec.rb +51 -0
  409. data/spec/integration/dots_and_dollars_spec.rb +277 -0
  410. data/spec/integration/encryption_spec.rb +81 -0
  411. data/spec/integration/i18n_fallbacks_spec.rb +74 -0
  412. data/spec/integration/matcher_examples_spec.rb +765 -0
  413. data/spec/integration/matcher_operator_data/all.yml +140 -0
  414. data/spec/integration/matcher_operator_data/and.yml +93 -0
  415. data/spec/integration/matcher_operator_data/bits_all_clear.yml +159 -0
  416. data/spec/integration/matcher_operator_data/bits_all_set.yml +159 -0
  417. data/spec/integration/matcher_operator_data/bits_any_clear.yml +159 -0
  418. data/spec/integration/matcher_operator_data/bits_any_set.yml +159 -0
  419. data/spec/integration/matcher_operator_data/comment.yml +22 -0
  420. data/spec/integration/matcher_operator_data/elem_match.yml +409 -0
  421. data/spec/integration/matcher_operator_data/elem_match_expr.yml +213 -0
  422. data/spec/integration/matcher_operator_data/eq.yml +191 -0
  423. data/spec/integration/matcher_operator_data/exists.yml +213 -0
  424. data/spec/integration/matcher_operator_data/generic_op.yml +17 -0
  425. data/spec/integration/matcher_operator_data/gt.yml +132 -0
  426. data/spec/integration/matcher_operator_data/gt_types.yml +63 -0
  427. data/spec/integration/matcher_operator_data/gte.yml +132 -0
  428. data/spec/integration/matcher_operator_data/gte_types.yml +15 -0
  429. data/spec/integration/matcher_operator_data/implicit.yml +331 -0
  430. data/spec/integration/matcher_operator_data/implicit_traversal.yml +112 -0
  431. data/spec/integration/matcher_operator_data/in.yml +210 -0
  432. data/spec/integration/matcher_operator_data/invalid_op.yml +59 -0
  433. data/spec/integration/matcher_operator_data/invalid_syntax.yml +39 -0
  434. data/spec/integration/matcher_operator_data/lt.yml +132 -0
  435. data/spec/integration/matcher_operator_data/lt_types.yml +15 -0
  436. data/spec/integration/matcher_operator_data/lte.yml +132 -0
  437. data/spec/integration/matcher_operator_data/lte_types.yml +15 -0
  438. data/spec/integration/matcher_operator_data/mod.yml +55 -0
  439. data/spec/integration/matcher_operator_data/multiple.yml +29 -0
  440. data/spec/integration/matcher_operator_data/ne.yml +150 -0
  441. data/spec/integration/matcher_operator_data/ne_types.yml +15 -0
  442. data/spec/integration/matcher_operator_data/nin.yml +114 -0
  443. data/spec/integration/matcher_operator_data/nor.yml +126 -0
  444. data/spec/integration/matcher_operator_data/not.yml +196 -0
  445. data/spec/integration/matcher_operator_data/or.yml +137 -0
  446. data/spec/integration/matcher_operator_data/regex.yml +174 -0
  447. data/spec/integration/matcher_operator_data/regex_options.yml +72 -0
  448. data/spec/integration/matcher_operator_data/size.yml +174 -0
  449. data/spec/integration/matcher_operator_data/type.yml +70 -0
  450. data/spec/integration/matcher_operator_data/type_array.yml +16 -0
  451. data/spec/integration/matcher_operator_data/type_binary.yml +18 -0
  452. data/spec/integration/matcher_operator_data/type_boolean.yml +39 -0
  453. data/spec/integration/matcher_operator_data/type_code.yml +26 -0
  454. data/spec/integration/matcher_operator_data/type_code_with_scope.yml +26 -0
  455. data/spec/integration/matcher_operator_data/type_date.yml +39 -0
  456. data/spec/integration/matcher_operator_data/type_db_pointer.yml +19 -0
  457. data/spec/integration/matcher_operator_data/type_decimal.yml +41 -0
  458. data/spec/integration/matcher_operator_data/type_double.yml +15 -0
  459. data/spec/integration/matcher_operator_data/type_int32.yml +33 -0
  460. data/spec/integration/matcher_operator_data/type_int64.yml +33 -0
  461. data/spec/integration/matcher_operator_data/type_max_key.yml +17 -0
  462. data/spec/integration/matcher_operator_data/type_min_key.yml +17 -0
  463. data/spec/integration/matcher_operator_data/type_null.yml +23 -0
  464. data/spec/integration/matcher_operator_data/type_object.yml +23 -0
  465. data/spec/integration/matcher_operator_data/type_object_id.yml +25 -0
  466. data/spec/integration/matcher_operator_data/type_regex.yml +44 -0
  467. data/spec/integration/matcher_operator_data/type_string.yml +15 -0
  468. data/spec/integration/matcher_operator_data/type_symbol.yml +32 -0
  469. data/spec/integration/matcher_operator_data/type_timestamp.yml +25 -0
  470. data/spec/integration/matcher_operator_data/type_undefined.yml +17 -0
  471. data/spec/integration/matcher_operator_spec.rb +122 -0
  472. data/spec/integration/matcher_spec.rb +237 -0
  473. data/spec/integration/persistence/range_field_spec.rb +350 -0
  474. data/spec/integration/server_query_spec.rb +141 -0
  475. data/spec/integration/shardable_spec.rb +148 -0
  476. data/spec/integration/stringified_symbol_field_spec.rb +203 -0
  477. data/spec/lite_spec_helper.rb +77 -0
  478. data/spec/mongoid/association/accessors_spec.rb +1049 -0
  479. data/spec/mongoid/association/auto_save_spec.rb +403 -0
  480. data/spec/mongoid/association/builders_spec.rb +255 -0
  481. data/spec/mongoid/association/constrainable_spec.rb +117 -0
  482. data/spec/mongoid/association/counter_cache_spec.rb +452 -0
  483. data/spec/mongoid/association/depending_spec.rb +959 -0
  484. data/spec/mongoid/association/eager_spec.rb +331 -0
  485. data/spec/mongoid/association/embedded/cyclic_spec.rb +180 -0
  486. data/spec/mongoid/association/embedded/dirty_spec.rb +67 -0
  487. data/spec/mongoid/association/embedded/embedded_in/binding_spec.rb +174 -0
  488. data/spec/mongoid/association/embedded/embedded_in/buildable_spec.rb +90 -0
  489. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +646 -0
  490. data/spec/mongoid/association/embedded/embedded_in_spec.rb +914 -0
  491. data/spec/mongoid/association/embedded/embeds_many/binding_spec.rb +56 -0
  492. data/spec/mongoid/association/embedded/embeds_many/buildable_spec.rb +218 -0
  493. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +4858 -0
  494. data/spec/mongoid/association/embedded/embeds_many_models.rb +241 -0
  495. data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +63 -0
  496. data/spec/mongoid/association/embedded/embeds_many_spec.rb +932 -0
  497. data/spec/mongoid/association/embedded/embeds_one/binding_spec.rb +79 -0
  498. data/spec/mongoid/association/embedded/embeds_one/buildable_spec.rb +106 -0
  499. data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +1038 -0
  500. data/spec/mongoid/association/embedded/embeds_one_dnl_models.rb +8 -0
  501. data/spec/mongoid/association/embedded/embeds_one_models.rb +77 -0
  502. data/spec/mongoid/association/embedded/embeds_one_query_spec.rb +28 -0
  503. data/spec/mongoid/association/embedded/embeds_one_spec.rb +984 -0
  504. data/spec/mongoid/association/macros_spec.rb +1116 -0
  505. data/spec/mongoid/association/nested/many_spec.rb +232 -0
  506. data/spec/mongoid/association/nested/one_spec.rb +253 -0
  507. data/spec/mongoid/association/options_spec.rb +1323 -0
  508. data/spec/mongoid/association/polymorphic_spec.rb +162 -0
  509. data/spec/mongoid/association/referenced/belongs_to/binding_spec.rb +244 -0
  510. data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +292 -0
  511. data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +412 -0
  512. data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +1388 -0
  513. data/spec/mongoid/association/referenced/belongs_to_models.rb +11 -0
  514. data/spec/mongoid/association/referenced/belongs_to_query_spec.rb +57 -0
  515. data/spec/mongoid/association/referenced/belongs_to_spec.rb +2030 -0
  516. data/spec/mongoid/association/referenced/has_and_belongs_to_many/binding_spec.rb +180 -0
  517. data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +148 -0
  518. data/spec/mongoid/association/referenced/has_and_belongs_to_many/eager_spec.rb +196 -0
  519. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_persistence_spec.rb +76 -0
  520. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +3812 -0
  521. data/spec/mongoid/association/referenced/has_and_belongs_to_many_models.rb +92 -0
  522. data/spec/mongoid/association/referenced/has_and_belongs_to_many_query_spec.rb +39 -0
  523. data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +1104 -0
  524. data/spec/mongoid/association/referenced/has_many/binding_spec.rb +155 -0
  525. data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +251 -0
  526. data/spec/mongoid/association/referenced/has_many/eager_spec.rb +290 -0
  527. data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +2232 -0
  528. data/spec/mongoid/association/referenced/has_many/proxy_query_spec.rb +23 -0
  529. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +4118 -0
  530. data/spec/mongoid/association/referenced/has_many_models.rb +97 -0
  531. data/spec/mongoid/association/referenced/has_many_query_spec.rb +37 -0
  532. data/spec/mongoid/association/referenced/has_many_spec.rb +1273 -0
  533. data/spec/mongoid/association/referenced/has_one/binding_spec.rb +133 -0
  534. data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +159 -0
  535. data/spec/mongoid/association/referenced/has_one/eager_spec.rb +193 -0
  536. data/spec/mongoid/association/referenced/has_one/proxy_spec.rb +1248 -0
  537. data/spec/mongoid/association/referenced/has_one_models.rb +113 -0
  538. data/spec/mongoid/association/referenced/has_one_query_spec.rb +37 -0
  539. data/spec/mongoid/association/referenced/has_one_spec.rb +1383 -0
  540. data/spec/mongoid/association/reflections_spec.rb +92 -0
  541. data/spec/mongoid/association/syncable_spec.rb +513 -0
  542. data/spec/mongoid/association_spec.rb +191 -0
  543. data/spec/mongoid/atomic/modifiers_spec.rb +505 -0
  544. data/spec/mongoid/atomic/paths/embedded/many_spec.rb +120 -0
  545. data/spec/mongoid/atomic/paths/embedded/one_spec.rb +112 -0
  546. data/spec/mongoid/atomic/paths/root_spec.rb +50 -0
  547. data/spec/mongoid/atomic/paths_spec.rb +350 -0
  548. data/spec/mongoid/atomic_spec.rb +412 -0
  549. data/spec/mongoid/attributes/dynamic_spec.rb +152 -0
  550. data/spec/mongoid/attributes/nested_spec.rb +5000 -0
  551. data/spec/mongoid/attributes/nested_spec_models.rb +48 -0
  552. data/spec/mongoid/attributes/projector_data/embedded.yml +105 -0
  553. data/spec/mongoid/attributes/projector_data/fields.yml +93 -0
  554. data/spec/mongoid/attributes/projector_spec.rb +40 -0
  555. data/spec/mongoid/attributes/readonly_spec.rb +271 -0
  556. data/spec/mongoid/attributes_spec.rb +2709 -0
  557. data/spec/mongoid/cacheable_spec.rb +114 -0
  558. data/spec/mongoid/changeable_spec.rb +2189 -0
  559. data/spec/mongoid/clients/factory_spec.rb +471 -0
  560. data/spec/mongoid/clients/options_spec.rb +521 -0
  561. data/spec/mongoid/clients/sessions_spec.rb +297 -0
  562. data/spec/mongoid/clients/transactions_spec.rb +1077 -0
  563. data/spec/mongoid/clients/transactions_spec_models.rb +114 -0
  564. data/spec/mongoid/clients_spec.rb +1218 -0
  565. data/spec/mongoid/collection_configurable_spec.rb +158 -0
  566. data/spec/mongoid/composable_spec.rb +33 -0
  567. data/spec/mongoid/config/defaults_spec.rb +92 -0
  568. data/spec/mongoid/config/encryption_spec.rb +150 -0
  569. data/spec/mongoid/config/environment_spec.rb +175 -0
  570. data/spec/mongoid/config/introspection_spec.rb +113 -0
  571. data/spec/mongoid/config/options_spec.rb +75 -0
  572. data/spec/mongoid/config_spec.rb +861 -0
  573. data/spec/mongoid/contextual/aggregable/memory_spec.rb +573 -0
  574. data/spec/mongoid/contextual/aggregable/memory_table.yml +88 -0
  575. data/spec/mongoid/contextual/aggregable/memory_table_spec.rb +62 -0
  576. data/spec/mongoid/contextual/aggregable/mongo_spec.rb +595 -0
  577. data/spec/mongoid/contextual/aggregable/none_spec.rb +48 -0
  578. data/spec/mongoid/contextual/atomic_spec.rb +1142 -0
  579. data/spec/mongoid/contextual/geo_near_spec.rb +471 -0
  580. data/spec/mongoid/contextual/map_reduce_spec.rb +459 -0
  581. data/spec/mongoid/contextual/memory_spec.rb +2931 -0
  582. data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +193 -0
  583. data/spec/mongoid/contextual/mongo_spec.rb +4534 -0
  584. data/spec/mongoid/contextual/none_spec.rb +170 -0
  585. data/spec/mongoid/copyable_spec.rb +1142 -0
  586. data/spec/mongoid/copyable_spec_models.rb +47 -0
  587. data/spec/mongoid/criteria/findable_spec.rb +1132 -0
  588. data/spec/mongoid/criteria/includable_spec.rb +1492 -0
  589. data/spec/mongoid/criteria/includable_spec_models.rb +54 -0
  590. data/spec/mongoid/criteria/inspectable_spec.rb +29 -0
  591. data/spec/mongoid/criteria/marshalable_spec.rb +47 -0
  592. data/spec/mongoid/criteria/modifiable_spec.rb +1812 -0
  593. data/spec/mongoid/criteria/options_spec.rb +31 -0
  594. data/spec/mongoid/criteria/queryable/aggregable_spec.rb +372 -0
  595. data/spec/mongoid/criteria/queryable/expandable_spec.rb +61 -0
  596. data/spec/mongoid/criteria/queryable/extensions/array_spec.rb +513 -0
  597. data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +169 -0
  598. data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +215 -0
  599. data/spec/mongoid/criteria/queryable/extensions/date_spec.rb +352 -0
  600. data/spec/mongoid/criteria/queryable/extensions/date_time_spec.rb +434 -0
  601. data/spec/mongoid/criteria/queryable/extensions/float_spec.rb +67 -0
  602. data/spec/mongoid/criteria/queryable/extensions/hash_spec.rb +314 -0
  603. data/spec/mongoid/criteria/queryable/extensions/integer_spec.rb +67 -0
  604. data/spec/mongoid/criteria/queryable/extensions/nil_class_spec.rb +79 -0
  605. data/spec/mongoid/criteria/queryable/extensions/numeric_spec.rb +119 -0
  606. data/spec/mongoid/criteria/queryable/extensions/object_spec.rb +110 -0
  607. data/spec/mongoid/criteria/queryable/extensions/range_spec.rb +370 -0
  608. data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +91 -0
  609. data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +92 -0
  610. data/spec/mongoid/criteria/queryable/extensions/set_spec.rb +41 -0
  611. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +264 -0
  612. data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +110 -0
  613. data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +406 -0
  614. data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +387 -0
  615. data/spec/mongoid/criteria/queryable/key_spec.rb +96 -0
  616. data/spec/mongoid/criteria/queryable/mergeable_spec.rb +182 -0
  617. data/spec/mongoid/criteria/queryable/optional_spec.rb +1332 -0
  618. data/spec/mongoid/criteria/queryable/options_spec.rb +362 -0
  619. data/spec/mongoid/criteria/queryable/pipeline_spec.rb +214 -0
  620. data/spec/mongoid/criteria/queryable/queryable_spec.rb +139 -0
  621. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +2538 -0
  622. data/spec/mongoid/criteria/queryable/selectable_shared_examples.rb +38 -0
  623. data/spec/mongoid/criteria/queryable/selectable_spec.rb +2327 -0
  624. data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +589 -0
  625. data/spec/mongoid/criteria/queryable/selector_spec.rb +990 -0
  626. data/spec/mongoid/criteria/queryable/smash_spec.rb +32 -0
  627. data/spec/mongoid/criteria/queryable/storable_spec.rb +203 -0
  628. data/spec/mongoid/criteria/scopable_spec.rb +601 -0
  629. data/spec/mongoid/criteria/translator_spec.rb +132 -0
  630. data/spec/mongoid/criteria_projection_spec.rb +406 -0
  631. data/spec/mongoid/criteria_spec.rb +3238 -0
  632. data/spec/mongoid/document_fields_spec.rb +262 -0
  633. data/spec/mongoid/document_persistence_context_spec.rb +32 -0
  634. data/spec/mongoid/document_query_spec.rb +89 -0
  635. data/spec/mongoid/document_spec.rb +1365 -0
  636. data/spec/mongoid/equality_spec.rb +241 -0
  637. data/spec/mongoid/errors/ambiguous_relationship_spec.rb +31 -0
  638. data/spec/mongoid/errors/attribute_not_loaded_spec.rb +31 -0
  639. data/spec/mongoid/errors/callback_spec.rb +31 -0
  640. data/spec/mongoid/errors/delete_restriction_spec.rb +31 -0
  641. data/spec/mongoid/errors/document_not_destroyed_spec.rb +35 -0
  642. data/spec/mongoid/errors/document_not_found_spec.rb +182 -0
  643. data/spec/mongoid/errors/invalid_collection_spec.rb +38 -0
  644. data/spec/mongoid/errors/invalid_config_file_spec.rb +31 -0
  645. data/spec/mongoid/errors/invalid_config_option_spec.rb +31 -0
  646. data/spec/mongoid/errors/invalid_field_option_spec.rb +31 -0
  647. data/spec/mongoid/errors/invalid_field_spec.rb +39 -0
  648. data/spec/mongoid/errors/invalid_field_type_spec.rb +55 -0
  649. data/spec/mongoid/errors/invalid_find_spec.rb +31 -0
  650. data/spec/mongoid/errors/invalid_includes_spec.rb +42 -0
  651. data/spec/mongoid/errors/invalid_index_spec.rb +31 -0
  652. data/spec/mongoid/errors/invalid_options_spec.rb +31 -0
  653. data/spec/mongoid/errors/invalid_path_spec.rb +25 -0
  654. data/spec/mongoid/errors/invalid_relation_spec.rb +39 -0
  655. data/spec/mongoid/errors/invalid_scope_spec.rb +31 -0
  656. data/spec/mongoid/errors/invalid_set_polymorphic_relation_spec.rb +19 -0
  657. data/spec/mongoid/errors/invalid_storage_options_spec.rb +31 -0
  658. data/spec/mongoid/errors/invalid_time_spec.rb +31 -0
  659. data/spec/mongoid/errors/inverse_not_found_spec.rb +31 -0
  660. data/spec/mongoid/errors/mixed_client_configuration_spec.rb +31 -0
  661. data/spec/mongoid/errors/mixed_relations_spec.rb +31 -0
  662. data/spec/mongoid/errors/mongoid_error_spec.rb +83 -0
  663. data/spec/mongoid/errors/nested_attributes_metadata_not_found_spec.rb +31 -0
  664. data/spec/mongoid/errors/no_client_config_spec.rb +31 -0
  665. data/spec/mongoid/errors/no_client_database_spec.rb +31 -0
  666. data/spec/mongoid/errors/no_client_hosts_spec.rb +31 -0
  667. data/spec/mongoid/errors/no_clients_config_spec.rb +31 -0
  668. data/spec/mongoid/errors/no_environment_spec.rb +31 -0
  669. data/spec/mongoid/errors/no_map_reduce_output_spec.rb +31 -0
  670. data/spec/mongoid/errors/no_metadata_spec.rb +25 -0
  671. data/spec/mongoid/errors/no_parent_spec.rb +31 -0
  672. data/spec/mongoid/errors/readonly_attribute_spec.rb +31 -0
  673. data/spec/mongoid/errors/readonly_document_spec.rb +31 -0
  674. data/spec/mongoid/errors/scope_overwrite_spec.rb +31 -0
  675. data/spec/mongoid/errors/too_many_nested_attribute_records_spec.rb +31 -0
  676. data/spec/mongoid/errors/unknown_attribute_spec.rb +31 -0
  677. data/spec/mongoid/errors/unsaved_document_spec.rb +39 -0
  678. data/spec/mongoid/errors/unsupported_javascript_spec.rb +31 -0
  679. data/spec/mongoid/errors/validations_spec.rb +47 -0
  680. data/spec/mongoid/extensions/array_spec.rb +620 -0
  681. data/spec/mongoid/extensions/big_decimal_spec.rb +908 -0
  682. data/spec/mongoid/extensions/binary_spec.rb +97 -0
  683. data/spec/mongoid/extensions/boolean_spec.rb +137 -0
  684. data/spec/mongoid/extensions/date_class_mongoize_spec.rb +339 -0
  685. data/spec/mongoid/extensions/date_spec.rb +131 -0
  686. data/spec/mongoid/extensions/date_time_spec.rb +107 -0
  687. data/spec/mongoid/extensions/decimal128_spec.rb +46 -0
  688. data/spec/mongoid/extensions/false_class_spec.rb +44 -0
  689. data/spec/mongoid/extensions/float_spec.rb +140 -0
  690. data/spec/mongoid/extensions/hash_spec.rb +518 -0
  691. data/spec/mongoid/extensions/integer_spec.rb +135 -0
  692. data/spec/mongoid/extensions/module_spec.rb +44 -0
  693. data/spec/mongoid/extensions/nil_class_spec.rb +13 -0
  694. data/spec/mongoid/extensions/object_id_spec.rb +948 -0
  695. data/spec/mongoid/extensions/object_spec.rb +288 -0
  696. data/spec/mongoid/extensions/range_spec.rb +364 -0
  697. data/spec/mongoid/extensions/raw_value_spec.rb +66 -0
  698. data/spec/mongoid/extensions/regexp_spec.rb +97 -0
  699. data/spec/mongoid/extensions/set_spec.rb +141 -0
  700. data/spec/mongoid/extensions/string_spec.rb +387 -0
  701. data/spec/mongoid/extensions/stringified_symbol_spec.rb +84 -0
  702. data/spec/mongoid/extensions/symbol_spec.rb +71 -0
  703. data/spec/mongoid/extensions/time_spec.rb +706 -0
  704. data/spec/mongoid/extensions/time_with_zone_spec.rb +361 -0
  705. data/spec/mongoid/extensions/true_class_spec.rb +44 -0
  706. data/spec/mongoid/factory_spec.rb +534 -0
  707. data/spec/mongoid/fields/foreign_key_spec.rb +718 -0
  708. data/spec/mongoid/fields/localized_spec.rb +571 -0
  709. data/spec/mongoid/fields/standard_spec.rb +168 -0
  710. data/spec/mongoid/fields_spec.rb +2062 -0
  711. data/spec/mongoid/findable_spec.rb +929 -0
  712. data/spec/mongoid/indexable/specification_spec.rb +247 -0
  713. data/spec/mongoid/indexable_spec.rb +962 -0
  714. data/spec/mongoid/inspectable_spec.rb +86 -0
  715. data/spec/mongoid/interceptable_spec.rb +2373 -0
  716. data/spec/mongoid/interceptable_spec_models.rb +307 -0
  717. data/spec/mongoid/loading_spec.rb +109 -0
  718. data/spec/mongoid/loggable_spec.rb +23 -0
  719. data/spec/mongoid/matcher/extract_attribute_data/numeric_keys.yml +104 -0
  720. data/spec/mongoid/matcher/extract_attribute_data/traversal.yml +239 -0
  721. data/spec/mongoid/matcher/extract_attribute_spec.rb +36 -0
  722. data/spec/mongoid/mongoizable_spec.rb +285 -0
  723. data/spec/mongoid/persistable/creatable_spec.rb +662 -0
  724. data/spec/mongoid/persistable/deletable_spec.rb +580 -0
  725. data/spec/mongoid/persistable/destroyable_spec.rb +652 -0
  726. data/spec/mongoid/persistable/incrementable_spec.rb +278 -0
  727. data/spec/mongoid/persistable/logical_spec.rb +196 -0
  728. data/spec/mongoid/persistable/maxable_spec.rb +146 -0
  729. data/spec/mongoid/persistable/minable_spec.rb +146 -0
  730. data/spec/mongoid/persistable/multipliable_spec.rb +226 -0
  731. data/spec/mongoid/persistable/poppable_spec.rb +167 -0
  732. data/spec/mongoid/persistable/pullable_spec.rb +330 -0
  733. data/spec/mongoid/persistable/pushable_spec.rb +425 -0
  734. data/spec/mongoid/persistable/renamable_spec.rb +187 -0
  735. data/spec/mongoid/persistable/savable_spec.rb +780 -0
  736. data/spec/mongoid/persistable/settable_spec.rb +581 -0
  737. data/spec/mongoid/persistable/unsettable_spec.rb +207 -0
  738. data/spec/mongoid/persistable/updatable_spec.rb +752 -0
  739. data/spec/mongoid/persistable/upsertable_spec.rb +228 -0
  740. data/spec/mongoid/persistable_spec.rb +321 -0
  741. data/spec/mongoid/persistence_context_spec.rb +695 -0
  742. data/spec/mongoid/positional_spec.rb +223 -0
  743. data/spec/mongoid/query_cache_middleware_spec.rb +50 -0
  744. data/spec/mongoid/query_cache_spec.rb +832 -0
  745. data/spec/mongoid/railties/console_sandbox_spec.rb +43 -0
  746. data/spec/mongoid/relations/proxy_spec.rb +126 -0
  747. data/spec/mongoid/reloadable_spec.rb +629 -0
  748. data/spec/mongoid/scopable_spec.rb +1312 -0
  749. data/spec/mongoid/selectable_spec.rb +136 -0
  750. data/spec/mongoid/serializable_spec.rb +942 -0
  751. data/spec/mongoid/shardable_models.rb +75 -0
  752. data/spec/mongoid/shardable_spec.rb +296 -0
  753. data/spec/mongoid/stateful_spec.rb +271 -0
  754. data/spec/mongoid/tasks/database_rake_spec.rb +370 -0
  755. data/spec/mongoid/tasks/database_spec.rb +308 -0
  756. data/spec/mongoid/tasks/encryption_spec.rb +147 -0
  757. data/spec/mongoid/threaded_spec.rb +343 -0
  758. data/spec/mongoid/timestamps/created/short_spec.rb +53 -0
  759. data/spec/mongoid/timestamps/created_spec.rb +46 -0
  760. data/spec/mongoid/timestamps/timeless_spec.rb +149 -0
  761. data/spec/mongoid/timestamps/updated/short_spec.rb +92 -0
  762. data/spec/mongoid/timestamps/updated_spec.rb +84 -0
  763. data/spec/mongoid/timestamps_spec.rb +498 -0
  764. data/spec/mongoid/timestamps_spec_models.rb +67 -0
  765. data/spec/mongoid/touchable_spec.rb +1403 -0
  766. data/spec/mongoid/touchable_spec_models.rb +153 -0
  767. data/spec/mongoid/traversable_spec.rb +1395 -0
  768. data/spec/mongoid/validatable/associated_spec.rb +208 -0
  769. data/spec/mongoid/validatable/format_spec.rb +85 -0
  770. data/spec/mongoid/validatable/length_spec.rb +225 -0
  771. data/spec/mongoid/validatable/numericality_spec.rb +32 -0
  772. data/spec/mongoid/validatable/presence_spec.rb +592 -0
  773. data/spec/mongoid/validatable/uniqueness_spec.rb +2548 -0
  774. data/spec/mongoid/validatable_spec.rb +327 -0
  775. data/spec/mongoid/warnings_spec.rb +35 -0
  776. data/spec/mongoid_spec.rb +98 -0
  777. data/spec/rails/controller_extension/controller_runtime_spec.rb +112 -0
  778. data/spec/rails/mongoid_spec.rb +53 -0
  779. data/spec/shared/LICENSE +20 -0
  780. data/spec/shared/bin/get-mongodb-download-url +17 -0
  781. data/spec/shared/bin/s3-copy +45 -0
  782. data/spec/shared/bin/s3-upload +69 -0
  783. data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
  784. data/spec/shared/lib/mrss/cluster_config.rb +231 -0
  785. data/spec/shared/lib/mrss/constraints.rb +378 -0
  786. data/spec/shared/lib/mrss/docker_runner.rb +291 -0
  787. data/spec/shared/lib/mrss/eg_config_utils.rb +51 -0
  788. data/spec/shared/lib/mrss/event_subscriber.rb +210 -0
  789. data/spec/shared/lib/mrss/lite_constraints.rb +238 -0
  790. data/spec/shared/lib/mrss/server_version_registry.rb +120 -0
  791. data/spec/shared/lib/mrss/session_registry.rb +69 -0
  792. data/spec/shared/lib/mrss/session_registry_legacy.rb +60 -0
  793. data/spec/shared/lib/mrss/spec_organizer.rb +179 -0
  794. data/spec/shared/lib/mrss/utils.rb +15 -0
  795. data/spec/shared/share/Dockerfile.erb +325 -0
  796. data/spec/shared/share/haproxy-1.conf +16 -0
  797. data/spec/shared/share/haproxy-2.conf +17 -0
  798. data/spec/shared/shlib/config.sh +27 -0
  799. data/spec/shared/shlib/distro.sh +74 -0
  800. data/spec/shared/shlib/server.sh +392 -0
  801. data/spec/shared/shlib/set_env.sh +169 -0
  802. data/spec/spec_helper.rb +210 -0
  803. data/spec/support/authorization.rb +15 -0
  804. data/spec/support/client_registry.rb +9 -0
  805. data/spec/support/constraints.rb +77 -0
  806. data/spec/support/crypt/models.rb +43 -0
  807. data/spec/support/crypt.rb +79 -0
  808. data/spec/support/expectations.rb +32 -0
  809. data/spec/support/feature_sandbox.rb +71 -0
  810. data/spec/support/helpers.rb +11 -0
  811. data/spec/support/immutable_ids.rb +118 -0
  812. data/spec/support/macros.rb +129 -0
  813. data/spec/support/models/account.rb +38 -0
  814. data/spec/support/models/acolyte.rb +19 -0
  815. data/spec/support/models/actor.rb +20 -0
  816. data/spec/support/models/actress.rb +4 -0
  817. data/spec/support/models/address.rb +87 -0
  818. data/spec/support/models/address_component.rb +7 -0
  819. data/spec/support/models/address_number.rb +8 -0
  820. data/spec/support/models/agency.rb +7 -0
  821. data/spec/support/models/agent.rb +18 -0
  822. data/spec/support/models/album.rb +20 -0
  823. data/spec/support/models/alert.rb +9 -0
  824. data/spec/support/models/animal.rb +28 -0
  825. data/spec/support/models/answer.rb +8 -0
  826. data/spec/support/models/appointment.rb +9 -0
  827. data/spec/support/models/armrest.rb +9 -0
  828. data/spec/support/models/array_field.rb +7 -0
  829. data/spec/support/models/article.rb +14 -0
  830. data/spec/support/models/artist.rb +90 -0
  831. data/spec/support/models/artwork.rb +6 -0
  832. data/spec/support/models/audible_sound.rb +3 -0
  833. data/spec/support/models/audio.rb +7 -0
  834. data/spec/support/models/augmentation.rb +25 -0
  835. data/spec/support/models/author.rb +8 -0
  836. data/spec/support/models/baby.rb +6 -0
  837. data/spec/support/models/band.rb +45 -0
  838. data/spec/support/models/bar.rb +12 -0
  839. data/spec/support/models/basic.rb +8 -0
  840. data/spec/support/models/bed.rb +3 -0
  841. data/spec/support/models/big_palette.rb +4 -0
  842. data/spec/support/models/birthday.rb +15 -0
  843. data/spec/support/models/bolt.rb +7 -0
  844. data/spec/support/models/bomb.rb +6 -0
  845. data/spec/support/models/book.rb +19 -0
  846. data/spec/support/models/breed.rb +6 -0
  847. data/spec/support/models/browser.rb +8 -0
  848. data/spec/support/models/building.rb +10 -0
  849. data/spec/support/models/building_address.rb +9 -0
  850. data/spec/support/models/bus.rb +9 -0
  851. data/spec/support/models/business.rb +7 -0
  852. data/spec/support/models/callback_test.rb +11 -0
  853. data/spec/support/models/canvas.rb +27 -0
  854. data/spec/support/models/car.rb +3 -0
  855. data/spec/support/models/cat.rb +10 -0
  856. data/spec/support/models/catalog.rb +24 -0
  857. data/spec/support/models/category.rb +10 -0
  858. data/spec/support/models/child.rb +6 -0
  859. data/spec/support/models/child_doc.rb +24 -0
  860. data/spec/support/models/church.rb +8 -0
  861. data/spec/support/models/circle.rb +5 -0
  862. data/spec/support/models/circuit.rb +6 -0
  863. data/spec/support/models/circus.rb +12 -0
  864. data/spec/support/models/code.rb +15 -0
  865. data/spec/support/models/coding/pull_request.rb +11 -0
  866. data/spec/support/models/coding.rb +3 -0
  867. data/spec/support/models/comment.rb +18 -0
  868. data/spec/support/models/company.rb +7 -0
  869. data/spec/support/models/consumption_period.rb +9 -0
  870. data/spec/support/models/contextable_item.rb +7 -0
  871. data/spec/support/models/contractor.rb +7 -0
  872. data/spec/support/models/cookie.rb +8 -0
  873. data/spec/support/models/country_code.rb +12 -0
  874. data/spec/support/models/courier_job.rb +6 -0
  875. data/spec/support/models/cover.rb +10 -0
  876. data/spec/support/models/crate.rb +12 -0
  877. data/spec/support/models/customer.rb +10 -0
  878. data/spec/support/models/customer_address.rb +11 -0
  879. data/spec/support/models/deed.rb +7 -0
  880. data/spec/support/models/definition.rb +11 -0
  881. data/spec/support/models/delegating_patient.rb +15 -0
  882. data/spec/support/models/description.rb +13 -0
  883. data/spec/support/models/dictionary.rb +18 -0
  884. data/spec/support/models/division.rb +12 -0
  885. data/spec/support/models/doctor.rb +14 -0
  886. data/spec/support/models/dog.rb +9 -0
  887. data/spec/support/models/dokument.rb +8 -0
  888. data/spec/support/models/draft.rb +11 -0
  889. data/spec/support/models/dragon.rb +6 -0
  890. data/spec/support/models/driver.rb +9 -0
  891. data/spec/support/models/drug.rb +8 -0
  892. data/spec/support/models/dungeon.rb +6 -0
  893. data/spec/support/models/edit.rb +7 -0
  894. data/spec/support/models/email.rb +8 -0
  895. data/spec/support/models/employer.rb +7 -0
  896. data/spec/support/models/entry.rb +8 -0
  897. data/spec/support/models/eraser.rb +3 -0
  898. data/spec/support/models/even.rb +9 -0
  899. data/spec/support/models/event.rb +24 -0
  900. data/spec/support/models/exhibition.rb +6 -0
  901. data/spec/support/models/exhibitor.rb +8 -0
  902. data/spec/support/models/explosion.rb +6 -0
  903. data/spec/support/models/eye.rb +11 -0
  904. data/spec/support/models/eye_bowl.rb +11 -0
  905. data/spec/support/models/face.rb +10 -0
  906. data/spec/support/models/fanatic.rb +8 -0
  907. data/spec/support/models/favorite.rb +14 -0
  908. data/spec/support/models/filesystem.rb +7 -0
  909. data/spec/support/models/fire_hydrant.rb +8 -0
  910. data/spec/support/models/firefox.rb +6 -0
  911. data/spec/support/models/fish.rb +9 -0
  912. data/spec/support/models/folder.rb +9 -0
  913. data/spec/support/models/folder_item.rb +11 -0
  914. data/spec/support/models/fruits.rb +36 -0
  915. data/spec/support/models/game.rb +21 -0
  916. data/spec/support/models/ghost.rb +9 -0
  917. data/spec/support/models/guitar.rb +4 -0
  918. data/spec/support/models/hole.rb +12 -0
  919. data/spec/support/models/home.rb +6 -0
  920. data/spec/support/models/house.rb +8 -0
  921. data/spec/support/models/html_writer.rb +5 -0
  922. data/spec/support/models/id_key.rb +8 -0
  923. data/spec/support/models/idnodef.rb +7 -0
  924. data/spec/support/models/image.rb +24 -0
  925. data/spec/support/models/implant.rb +27 -0
  926. data/spec/support/models/instrument.rb +8 -0
  927. data/spec/support/models/item.rb +10 -0
  928. data/spec/support/models/jar.rb +9 -0
  929. data/spec/support/models/kaleidoscope.rb +8 -0
  930. data/spec/support/models/kangaroo.rb +6 -0
  931. data/spec/support/models/label.rb +52 -0
  932. data/spec/support/models/language.rb +7 -0
  933. data/spec/support/models/lat_lng.rb +17 -0
  934. data/spec/support/models/league.rb +13 -0
  935. data/spec/support/models/learner.rb +4 -0
  936. data/spec/support/models/line_item.rb +8 -0
  937. data/spec/support/models/location.rb +10 -0
  938. data/spec/support/models/login.rb +10 -0
  939. data/spec/support/models/manufacturer.rb +9 -0
  940. data/spec/support/models/meat.rb +6 -0
  941. data/spec/support/models/membership.rb +7 -0
  942. data/spec/support/models/message.rb +13 -0
  943. data/spec/support/models/minim.rb +6 -0
  944. data/spec/support/models/mixed_drink.rb +6 -0
  945. data/spec/support/models/mop.rb +24 -0
  946. data/spec/support/models/movie.rb +15 -0
  947. data/spec/support/models/my_hash.rb +4 -0
  948. data/spec/support/models/name.rb +26 -0
  949. data/spec/support/models/name_only.rb +8 -0
  950. data/spec/support/models/node.rb +7 -0
  951. data/spec/support/models/note.rb +19 -0
  952. data/spec/support/models/nut.rb +7 -0
  953. data/spec/support/models/odd.rb +9 -0
  954. data/spec/support/models/order.rb +12 -0
  955. data/spec/support/models/ordered_post.rb +13 -0
  956. data/spec/support/models/ordered_preference.rb +8 -0
  957. data/spec/support/models/oscar.rb +16 -0
  958. data/spec/support/models/other_owner_object.rb +4 -0
  959. data/spec/support/models/override.rb +18 -0
  960. data/spec/support/models/ownable.rb +8 -0
  961. data/spec/support/models/owner.rb +10 -0
  962. data/spec/support/models/pack.rb +5 -0
  963. data/spec/support/models/page.rb +18 -0
  964. data/spec/support/models/page_question.rb +6 -0
  965. data/spec/support/models/palette.rb +9 -0
  966. data/spec/support/models/parent.rb +7 -0
  967. data/spec/support/models/parent_doc.rb +8 -0
  968. data/spec/support/models/passport.rb +22 -0
  969. data/spec/support/models/patient.rb +11 -0
  970. data/spec/support/models/pdf_writer.rb +5 -0
  971. data/spec/support/models/pencil.rb +3 -0
  972. data/spec/support/models/person.rb +231 -0
  973. data/spec/support/models/pet.rb +25 -0
  974. data/spec/support/models/pet_owner.rb +8 -0
  975. data/spec/support/models/phone.rb +13 -0
  976. data/spec/support/models/piano.rb +4 -0
  977. data/spec/support/models/pizza.rb +9 -0
  978. data/spec/support/models/player.rb +39 -0
  979. data/spec/support/models/post.rb +52 -0
  980. data/spec/support/models/post_genre.rb +8 -0
  981. data/spec/support/models/powerup.rb +25 -0
  982. data/spec/support/models/preference.rb +11 -0
  983. data/spec/support/models/princess.rb +12 -0
  984. data/spec/support/models/product.rb +20 -0
  985. data/spec/support/models/profile.rb +17 -0
  986. data/spec/support/models/pronunciation.rb +7 -0
  987. data/spec/support/models/pub.rb +8 -0
  988. data/spec/support/models/publication/encyclopedia.rb +11 -0
  989. data/spec/support/models/publication/review.rb +13 -0
  990. data/spec/support/models/publication.rb +4 -0
  991. data/spec/support/models/purchase.rb +6 -0
  992. data/spec/support/models/purchased_item.rb +10 -0
  993. data/spec/support/models/question.rb +10 -0
  994. data/spec/support/models/quiz.rb +9 -0
  995. data/spec/support/models/rating.rb +10 -0
  996. data/spec/support/models/record.rb +54 -0
  997. data/spec/support/models/registry.rb +7 -0
  998. data/spec/support/models/role.rb +9 -0
  999. data/spec/support/models/root_category.rb +6 -0
  1000. data/spec/support/models/sandbox/app/models/app_models_message.rb +3 -0
  1001. data/spec/support/models/sandbox/lib/models/lib_models_message.rb +3 -0
  1002. data/spec/support/models/sandbox/sandbox_message.rb +3 -0
  1003. data/spec/support/models/sandbox/sandbox_user.rb +3 -0
  1004. data/spec/support/models/sandbox/subdir/sandbox_comment.rb +3 -0
  1005. data/spec/support/models/sandwich.rb +11 -0
  1006. data/spec/support/models/scheduler.rb +9 -0
  1007. data/spec/support/models/school.rb +14 -0
  1008. data/spec/support/models/scribe.rb +7 -0
  1009. data/spec/support/models/sealer.rb +7 -0
  1010. data/spec/support/models/seat.rb +24 -0
  1011. data/spec/support/models/seo.rb +9 -0
  1012. data/spec/support/models/series.rb +7 -0
  1013. data/spec/support/models/server.rb +15 -0
  1014. data/spec/support/models/service.rb +24 -0
  1015. data/spec/support/models/shape.rb +14 -0
  1016. data/spec/support/models/shelf.rb +7 -0
  1017. data/spec/support/models/shield.rb +18 -0
  1018. data/spec/support/models/shipment_address.rb +5 -0
  1019. data/spec/support/models/shipping_container.rb +7 -0
  1020. data/spec/support/models/shipping_pack.rb +5 -0
  1021. data/spec/support/models/shirt.rb +11 -0
  1022. data/spec/support/models/shop.rb +8 -0
  1023. data/spec/support/models/short_agent.rb +6 -0
  1024. data/spec/support/models/short_quiz.rb +7 -0
  1025. data/spec/support/models/simple.rb +7 -0
  1026. data/spec/support/models/slave.rb +8 -0
  1027. data/spec/support/models/song.rb +10 -0
  1028. data/spec/support/models/sound.rb +7 -0
  1029. data/spec/support/models/spacer.rb +7 -0
  1030. data/spec/support/models/square.rb +6 -0
  1031. data/spec/support/models/staff.rb +9 -0
  1032. data/spec/support/models/store_as_dup_test1.rb +7 -0
  1033. data/spec/support/models/store_as_dup_test2.rb +7 -0
  1034. data/spec/support/models/store_as_dup_test3.rb +7 -0
  1035. data/spec/support/models/store_as_dup_test4.rb +7 -0
  1036. data/spec/support/models/strategy.rb +5 -0
  1037. data/spec/support/models/student.rb +14 -0
  1038. data/spec/support/models/sub_item.rb +5 -0
  1039. data/spec/support/models/subscription.rb +7 -0
  1040. data/spec/support/models/survey.rb +7 -0
  1041. data/spec/support/models/symptom.rb +8 -0
  1042. data/spec/support/models/system_role.rb +7 -0
  1043. data/spec/support/models/tag.rb +10 -0
  1044. data/spec/support/models/target.rb +7 -0
  1045. data/spec/support/models/template.rb +7 -0
  1046. data/spec/support/models/thing.rb +11 -0
  1047. data/spec/support/models/threadlocker.rb +7 -0
  1048. data/spec/support/models/title.rb +5 -0
  1049. data/spec/support/models/tool.rb +10 -0
  1050. data/spec/support/models/topping.rb +7 -0
  1051. data/spec/support/models/toy.rb +9 -0
  1052. data/spec/support/models/track.rb +40 -0
  1053. data/spec/support/models/translation.rb +7 -0
  1054. data/spec/support/models/tree.rb +11 -0
  1055. data/spec/support/models/truck.rb +7 -0
  1056. data/spec/support/models/updatable.rb +7 -0
  1057. data/spec/support/models/user.rb +23 -0
  1058. data/spec/support/models/user_account.rb +12 -0
  1059. data/spec/support/models/validation_callback.rb +12 -0
  1060. data/spec/support/models/vehicle.rb +18 -0
  1061. data/spec/support/models/version.rb +7 -0
  1062. data/spec/support/models/vertex.rb +8 -0
  1063. data/spec/support/models/vet_visit.rb +7 -0
  1064. data/spec/support/models/video.rb +15 -0
  1065. data/spec/support/models/video_game.rb +3 -0
  1066. data/spec/support/models/washer.rb +7 -0
  1067. data/spec/support/models/weapon.rb +25 -0
  1068. data/spec/support/models/wiki_page.rb +17 -0
  1069. data/spec/support/models/word.rb +17 -0
  1070. data/spec/support/models/word_origin.rb +13 -0
  1071. data/spec/support/models/writer.rb +13 -0
  1072. data/spec/support/rails_mock.rb +31 -0
  1073. data/spec/support/schema_maps/schema_map_aws.json +17 -0
  1074. data/spec/support/schema_maps/schema_map_aws_key_alt_names.json +12 -0
  1075. data/spec/support/schema_maps/schema_map_azure.json +17 -0
  1076. data/spec/support/schema_maps/schema_map_azure_key_alt_names.json +12 -0
  1077. data/spec/support/schema_maps/schema_map_gcp.json +17 -0
  1078. data/spec/support/schema_maps/schema_map_gcp_key_alt_names.json +12 -0
  1079. data/spec/support/schema_maps/schema_map_kmip.json +17 -0
  1080. data/spec/support/schema_maps/schema_map_kmip_key_alt_names.json +12 -0
  1081. data/spec/support/schema_maps/schema_map_local.json +18 -0
  1082. data/spec/support/schema_maps/schema_map_local_key_alt_names.json +12 -0
  1083. data/spec/support/shared/time.rb +40 -0
  1084. data/spec/support/sinatra_mock.rb +6 -0
  1085. data/spec/support/spec_config.rb +80 -0
  1086. metadata +1944 -0
@@ -0,0 +1,2931 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe Mongoid::Contextual::Memory do
6
+
7
+ shared_examples "raises an error when no documents" do
8
+ context "when there are no matching documents" do
9
+ let(:criteria) do
10
+ Address.all.tap do |crit|
11
+ crit.documents = []
12
+ end
13
+ end
14
+
15
+ it "returns nil" do
16
+ expect do
17
+ context.send(method)
18
+ end.to raise_error(Mongoid::Errors::DocumentNotFound, /Could not find a document of class Address./)
19
+ end
20
+ end
21
+ end
22
+
23
+ shared_examples "returns nil when no documents" do
24
+ context "when there are no matching documents" do
25
+ let(:criteria) do
26
+ Address.all.tap do |crit|
27
+ crit.documents = []
28
+ end
29
+ end
30
+
31
+ it "returns nil" do
32
+ expect(context.send(method)).to be_nil
33
+ end
34
+ end
35
+ end
36
+
37
+ [ :blank?, :empty? ].each do |method|
38
+
39
+ describe "##{method}" do
40
+
41
+ let(:hobrecht) do
42
+ Address.new(street: "hobrecht")
43
+ end
44
+
45
+ let(:friedel) do
46
+ Address.new(street: "friedel")
47
+ end
48
+
49
+ context "when there are matching documents" do
50
+
51
+ let(:criteria) do
52
+ Address.where(street: "hobrecht").tap do |crit|
53
+ crit.documents = [ hobrecht, friedel ]
54
+ end
55
+ end
56
+
57
+ let(:context) do
58
+ described_class.new(criteria)
59
+ end
60
+
61
+ it "returns false" do
62
+ expect(context.send(method)).to be false
63
+ end
64
+ end
65
+
66
+ context "when there are no matching documents" do
67
+
68
+ let(:criteria) do
69
+ Address.where(street: "pfluger").tap do |crit|
70
+ crit.documents = [ hobrecht, friedel ]
71
+ end
72
+ end
73
+
74
+ let(:context) do
75
+ described_class.new(criteria)
76
+ end
77
+
78
+ it "returns true" do
79
+ expect(context.send(method)).to be true
80
+ end
81
+ end
82
+
83
+ context 'when there is a collation on the criteria' do
84
+
85
+ let(:criteria) do
86
+ Address.where(street: "hobrecht").tap do |crit|
87
+ crit.documents = [ hobrecht, friedel ]
88
+ end.collation(locale: 'en_US', strength: 2)
89
+ end
90
+
91
+ it "raises an exception" do
92
+ expect {
93
+ context.send(method)
94
+ }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ describe "#count" do
101
+
102
+ let!(:hobrecht) do
103
+ Address.new(street: "hobrecht")
104
+ end
105
+
106
+ let!(:friedel) do
107
+ Address.new(street: "friedel")
108
+ end
109
+
110
+ let(:criteria) do
111
+ Address.where(street: "hobrecht").tap do |crit|
112
+ crit.documents = [ hobrecht, friedel ]
113
+ end
114
+ end
115
+
116
+ let(:context) do
117
+ described_class.new(criteria)
118
+ end
119
+
120
+ context "context when no arguments are provided" do
121
+
122
+ it "returns the number of matches" do
123
+ expect(context.count).to eq(1)
124
+ end
125
+ end
126
+
127
+ context 'when there is a collation on the criteria' do
128
+
129
+ let(:criteria) do
130
+ Address.where(street: "hobrecht").tap do |crit|
131
+ crit.documents = [ hobrecht, friedel ]
132
+ end.collation(locale: 'en_US', strength: 2)
133
+ end
134
+
135
+ it "raises an exception" do
136
+ expect {
137
+ context.count
138
+ }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
139
+ end
140
+ end
141
+
142
+ context "when provided a document" do
143
+
144
+ context "when the document matches" do
145
+
146
+ let(:count) do
147
+ context.count(hobrecht)
148
+ end
149
+
150
+ it "returns 1" do
151
+ expect(count).to eq(1)
152
+ end
153
+ end
154
+
155
+ context "when the document does not match" do
156
+
157
+ let(:count) do
158
+ context.count(friedel)
159
+ end
160
+
161
+ it "returns 0" do
162
+ expect(count).to eq(0)
163
+ end
164
+ end
165
+ end
166
+
167
+ context "when provided a block" do
168
+
169
+ context "when the block evals 1 to true" do
170
+
171
+ let(:count) do
172
+ context.count do |doc|
173
+ doc.street == "hobrecht"
174
+ end
175
+ end
176
+
177
+ it "returns 1" do
178
+ expect(count).to eq(1)
179
+ end
180
+ end
181
+
182
+ context "when the block evals none to true" do
183
+
184
+ let(:count) do
185
+ context.count do |doc|
186
+ doc.street == "friedel"
187
+ end
188
+ end
189
+
190
+ it "returns 0" do
191
+ expect(count).to eq(0)
192
+ end
193
+ end
194
+ end
195
+ end
196
+
197
+ [ :delete, :delete_all ].each do |method|
198
+
199
+ let(:person) do
200
+ Person.create!
201
+ end
202
+
203
+ let(:hobrecht) do
204
+ person.addresses.create!(street: "hobrecht")
205
+ end
206
+
207
+ let(:friedel) do
208
+ person.addresses.create!(street: "friedel")
209
+ end
210
+
211
+ let(:pfluger) do
212
+ person.addresses.create!(street: "pfluger")
213
+ end
214
+
215
+ describe "##{method}" do
216
+
217
+ context "when embedded a single level" do
218
+
219
+ let(:criteria) do
220
+ Address.any_in(street: [ "hobrecht", "friedel" ]).tap do |crit|
221
+ crit.documents = [ hobrecht, friedel, pfluger ]
222
+ end
223
+ end
224
+
225
+ let(:context) do
226
+ described_class.new(criteria)
227
+ end
228
+
229
+ let!(:deleted) do
230
+ context.send(method)
231
+ end
232
+
233
+ it "deletes the first matching document" do
234
+ expect(hobrecht).to be_destroyed
235
+ end
236
+
237
+ it "deletes the last matching document" do
238
+ expect(friedel).to be_destroyed
239
+ end
240
+
241
+ it "does not delete non matching docs" do
242
+ expect(pfluger).to_not be_destroyed
243
+ end
244
+
245
+ it "removes the docs from the relation" do
246
+ expect(person.addresses).to eq([ pfluger ])
247
+ end
248
+
249
+ it "removes the docs from the context" do
250
+ expect(context.entries).to be_empty
251
+ end
252
+
253
+ it "persists the changes to the database" do
254
+ expect(person.reload.addresses).to eq([ pfluger ])
255
+ end
256
+
257
+ it "returns the number of deleted documents" do
258
+ expect(deleted).to eq(2)
259
+ end
260
+ end
261
+
262
+ context "when embedded multiple levels" do
263
+
264
+ let!(:home) do
265
+ hobrecht.locations.create!(name: "home")
266
+ end
267
+
268
+ let!(:work) do
269
+ hobrecht.locations.create!(name: "work")
270
+ end
271
+
272
+ let(:criteria) do
273
+ Location.where(name: "work").tap do |crit|
274
+ crit.documents = [ home, work ]
275
+ end
276
+ end
277
+
278
+ let(:context) do
279
+ described_class.new(criteria)
280
+ end
281
+
282
+ let!(:deleted) do
283
+ context.send(method)
284
+ end
285
+
286
+ it "deletes the first matching document" do
287
+ expect(work).to be_destroyed
288
+ end
289
+
290
+ it "does not delete non matching docs" do
291
+ expect(home).to_not be_destroyed
292
+ end
293
+
294
+ it "removes the docs from the relation" do
295
+ expect(person.addresses.first.locations).to eq([ home ])
296
+ end
297
+
298
+ it "removes the docs from the context" do
299
+ expect(context.entries).to be_empty
300
+ end
301
+
302
+ it "persists the changes to the database" do
303
+ expect(person.reload.addresses.first.locations).to eq([ home ])
304
+ end
305
+
306
+ it "returns the number of deleted documents" do
307
+ expect(deleted).to eq(1)
308
+ end
309
+ end
310
+
311
+ context 'when there is a collation on the criteria' do
312
+
313
+ let(:criteria) do
314
+ Address.any_in(street: [ "hobrecht", "friedel" ]).tap do |crit|
315
+ crit.documents = [ hobrecht, friedel, pfluger ]
316
+ end.collation(locale: 'en_US', strength: 2)
317
+ end
318
+
319
+ it "raises an exception" do
320
+ expect {
321
+ context.send(method)
322
+ }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
323
+ end
324
+ end
325
+ end
326
+ end
327
+
328
+ [ :destroy, :destroy_all ].each do |method|
329
+
330
+ let(:person) do
331
+ Person.create!
332
+ end
333
+
334
+ let(:hobrecht) do
335
+ person.addresses.create!(street: "hobrecht")
336
+ end
337
+
338
+ let(:friedel) do
339
+ person.addresses.create!(street: "friedel")
340
+ end
341
+
342
+ let(:pfluger) do
343
+ person.addresses.create!(street: "pfluger")
344
+ end
345
+
346
+ let(:criteria) do
347
+ Address.any_in(street: [ "hobrecht", "friedel" ]).tap do |crit|
348
+ crit.documents = [ hobrecht, friedel, pfluger ]
349
+ end
350
+ end
351
+
352
+ let(:context) do
353
+ described_class.new(criteria)
354
+ end
355
+
356
+ describe "##{method}" do
357
+
358
+ context 'when there is no collation on the criteria' do
359
+
360
+ let!(:destroyed) do
361
+ context.send(method)
362
+ end
363
+
364
+ it "deletes the first matching document" do
365
+ expect(hobrecht).to be_destroyed
366
+ end
367
+
368
+ it "deletes the last matching document" do
369
+ expect(friedel).to be_destroyed
370
+ end
371
+
372
+ it "does not delete non matching docs" do
373
+ expect(pfluger).to_not be_destroyed
374
+ end
375
+
376
+ it "removes the docs from the relation" do
377
+ expect(person.addresses).to eq([ pfluger ])
378
+ end
379
+
380
+ it "removes the docs from the context" do
381
+ expect(context.entries).to be_empty
382
+ end
383
+
384
+ it "persists the changes to the database" do
385
+ expect(person.reload.addresses).to eq([ pfluger ])
386
+ end
387
+
388
+ it "returns the number of destroyed documents" do
389
+ expect(destroyed).to eq(2)
390
+ end
391
+ end
392
+
393
+ context 'when there is a collation on the criteria' do
394
+
395
+ let(:criteria) do
396
+ Address.any_in(street: [ "hobrecht", "friedel" ]).tap do |crit|
397
+ crit.documents = [ hobrecht, friedel, pfluger ]
398
+ end.collation(locale: 'en_US', strength: 2)
399
+ end
400
+
401
+ it "raises an exception" do
402
+ expect {
403
+ context.send(method)
404
+ }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
405
+ end
406
+ end
407
+ end
408
+ end
409
+
410
+ describe "#distinct" do
411
+ let(:depeche) { Band.create!(name: "Depeche Mode", years: 30, sales: "1E2") }
412
+ let(:new_order) { Band.create!(name: "New Order", years: 25, sales: "2E3") }
413
+ let(:maniacs) { Band.create!(name: "10,000 Maniacs", years: 20, sales: "1E2") }
414
+
415
+ let(:criteria) do
416
+ Band.all.tap do |crit|
417
+ crit.documents = [ depeche, new_order, maniacs ]
418
+ end
419
+ end
420
+
421
+ let(:context) do
422
+ described_class.new(criteria)
423
+ end
424
+
425
+ context "when limiting the result set" do
426
+
427
+ let(:criteria) do
428
+ Band.where(name: "Depeche Mode").tap do |crit|
429
+ crit.documents = [ depeche ]
430
+ end
431
+ end
432
+
433
+ it "returns the distinct matching fields" do
434
+ expect(context.distinct(:name)).to eq([ "Depeche Mode" ])
435
+ end
436
+ end
437
+
438
+ context "when not limiting the result set" do
439
+
440
+ it "returns the distinct field values" do
441
+ expect(context.distinct(:name).sort).to eq([ "10,000 Maniacs", "Depeche Mode", "New Order" ].sort)
442
+ end
443
+ end
444
+
445
+ context "when providing an aliased field" do
446
+
447
+ it "returns the distinct field values" do
448
+ expect(context.distinct(:years).sort).to eq([ 20, 25, 30 ])
449
+ end
450
+ end
451
+
452
+ context "when providing a demongoizable field" do
453
+
454
+ it "returns the non-demongoized distinct field values" do
455
+ expect(context.distinct(:sales).sort).to eq([ BigDecimal("1E2"), BigDecimal("2E3") ])
456
+ end
457
+ end
458
+
459
+ context "when getting a localized field" do
460
+ with_default_i18n_configs
461
+
462
+ before do
463
+ I18n.locale = :en
464
+ d = Dictionary.create!(description: 'english-text')
465
+ I18n.locale = :de
466
+ d.description = 'deutsch-text'
467
+ d.save!
468
+ end
469
+
470
+ let(:criteria) do
471
+ Dictionary.all.tap do |crit|
472
+ crit.documents = [ Dictionary.first ]
473
+ end
474
+ end
475
+
476
+ context "when getting the field without _translations" do
477
+ it "gets the demongoized localized field" do
478
+ expect(context.distinct(:description)).to eq([ 'deutsch-text' ])
479
+ end
480
+ end
481
+
482
+ context "when getting the field with _translations" do
483
+
484
+ it "gets the full hash" do
485
+ expect(context.distinct(:description_translations)).to eq([ { "de" => "deutsch-text", "en" => "english-text" } ])
486
+ end
487
+ end
488
+
489
+ context 'when plucking a specific locale' do
490
+
491
+ let(:distinct) do
492
+ context.distinct(:'description.de')
493
+ end
494
+
495
+ it 'returns the specific translation' do
496
+ expect(distinct).to eq([ "deutsch-text" ])
497
+ end
498
+ end
499
+
500
+ context 'when plucking a specific locale from _translations field' do
501
+
502
+ let(:distinct) do
503
+ context.distinct(:'description_translations.de')
504
+ end
505
+
506
+ it 'returns the specific translations' do
507
+ expect(distinct).to eq(['deutsch-text'])
508
+ end
509
+ end
510
+
511
+ context 'when fallbacks are enabled with a locale list' do
512
+ require_fallbacks
513
+ with_default_i18n_configs
514
+
515
+ before do
516
+ I18n.fallbacks[:he] = [ :en ]
517
+ end
518
+
519
+ let(:distinct) do
520
+ context.distinct(:description).first
521
+ end
522
+
523
+ it "correctly uses the fallback" do
524
+ I18n.locale = :en
525
+ Dictionary.create!(description: 'english-text')
526
+ I18n.locale = :he
527
+ distinct.should == "english-text"
528
+ end
529
+ end
530
+
531
+ context "when the localized field is embedded" do
532
+ with_default_i18n_configs
533
+
534
+ let(:person) do
535
+ p = Passport.new
536
+ I18n.locale = :en
537
+ p.name = "Neil"
538
+ I18n.locale = :he
539
+ p.name = "Nissim"
540
+
541
+ Person.create!(passport: p, employer_id: 12345)
542
+ end
543
+
544
+ let(:criteria) do
545
+ Person.where(employer_id: 12345).tap do |crit|
546
+ crit.documents = [ person ]
547
+ end
548
+ end
549
+
550
+ let(:distinct) do
551
+ context.distinct("pass.name").first
552
+ end
553
+
554
+ let(:distinct_translations) do
555
+ context.distinct("pass.name_translations").first
556
+ end
557
+
558
+ let(:distinct_translations_field) do
559
+ context.distinct("pass.name_translations.en").first
560
+ end
561
+
562
+ it "returns the translation for the current locale" do
563
+ expect(distinct).to eq("Nissim")
564
+ end
565
+
566
+ it "returns the full _translation hash" do
567
+ expect(distinct_translations).to eq({ "en" => "Neil", "he" => "Nissim" })
568
+ end
569
+
570
+ it "returns the translation for the requested locale" do
571
+ expect(distinct_translations_field).to eq("Neil")
572
+ end
573
+ end
574
+ end
575
+
576
+ context "when getting an embedded field" do
577
+
578
+ let(:label) { Label.new(sales: "1E2") }
579
+ let!(:band) { Band.create!(label: label) }
580
+ let(:criteria) do
581
+ Band.where(_id: band.id).tap do |crit|
582
+ crit.documents = [ band ]
583
+ end
584
+ end
585
+
586
+ it "returns the distinct matching fields" do
587
+ expect(context.distinct("label.sales")).to eq([ BigDecimal("1E2") ])
588
+ end
589
+ end
590
+ end
591
+
592
+ describe "#each" do
593
+
594
+ let(:hobrecht) do
595
+ Address.new(street: "hobrecht")
596
+ end
597
+
598
+ let(:friedel) do
599
+ Address.new(street: "friedel")
600
+ end
601
+
602
+ let(:criteria) do
603
+ Address.where(street: "hobrecht").tap do |crit|
604
+ crit.documents = [ hobrecht, friedel ]
605
+ end
606
+ end
607
+
608
+ let(:context) do
609
+ described_class.new(criteria)
610
+ end
611
+
612
+ context "when skip and limit outside of range" do
613
+
614
+ before do
615
+ context.skip(10).limit(2)
616
+ end
617
+
618
+ it "contains no documents" do
619
+ expect(context.map(&:street)).to be_empty
620
+ end
621
+
622
+ context "when calling next on the enumerator" do
623
+
624
+ it "raises a stop iteration error" do
625
+ expect {
626
+ context.each.next
627
+ }.to raise_error(StopIteration)
628
+ end
629
+ end
630
+ end
631
+
632
+ context "when providing a block" do
633
+
634
+ it "yields mongoid documents to the block" do
635
+ context.each do |doc|
636
+ expect(doc).to be_a(Mongoid::Document)
637
+ end
638
+ end
639
+
640
+ it "iterates over the matching documents" do
641
+ context.each do |doc|
642
+ expect(doc).to eq(hobrecht)
643
+ end
644
+ end
645
+ end
646
+
647
+ context "when no block is provided" do
648
+
649
+ let(:enum) do
650
+ context.each
651
+ end
652
+
653
+ it "returns an enumerator" do
654
+ expect(enum).to be_a(Enumerator)
655
+ end
656
+
657
+ context "when iterating over the enumerator" do
658
+
659
+ context "when iterating with each" do
660
+
661
+ it "yields mongoid documents to the block" do
662
+ enum.each do |doc|
663
+ expect(doc).to be_a(Mongoid::Document)
664
+ end
665
+ end
666
+ end
667
+
668
+ context "when iterating with next" do
669
+
670
+ it "yields mongoid documents" do
671
+ expect(enum.next).to be_a(Mongoid::Document)
672
+ end
673
+ end
674
+ end
675
+ end
676
+
677
+ context 'when there is a collation on the criteria' do
678
+
679
+ let(:criteria) do
680
+ Address.where(street: "hobrecht").tap do |crit|
681
+ crit.documents = [ hobrecht, friedel ]
682
+ end.collation(locale: 'en_US', strength: 2)
683
+ end
684
+
685
+ it "raises an exception" do
686
+ expect {
687
+ context.each
688
+ }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
689
+ end
690
+ end
691
+ end
692
+
693
+ describe "#exists?" do
694
+
695
+ let(:hobrecht) do
696
+ Address.new(street: "hobrecht")
697
+ end
698
+
699
+ let(:friedel) do
700
+ Address.new(street: "friedel")
701
+ end
702
+
703
+ let(:criteria) do
704
+ Address.where(street: "hobrecht").tap do |crit|
705
+ crit.documents = [ hobrecht, friedel ]
706
+ end
707
+ end
708
+
709
+ context "when not passing options" do
710
+
711
+ context "when there are matching documents" do
712
+
713
+ let(:context) do
714
+ described_class.new(criteria)
715
+ end
716
+
717
+ it "returns true" do
718
+ expect(context).to be_exists
719
+ end
720
+ end
721
+
722
+ context "when there are no matching documents" do
723
+
724
+ let(:criteria) do
725
+ Address.where(street: "pfluger").tap do |crit|
726
+ crit.documents = [ hobrecht, friedel ]
727
+ end
728
+ end
729
+
730
+ let(:context) do
731
+ described_class.new(criteria)
732
+ end
733
+
734
+ it "returns false" do
735
+ expect(context).to_not be_exists
736
+ end
737
+ end
738
+
739
+ context 'when there is a collation on the criteria' do
740
+
741
+ let(:criteria) do
742
+ Address.where(street: "pfluger").tap do |crit|
743
+ crit.documents = [ hobrecht, friedel ]
744
+ end.collation(locale: 'en_US', strength: 2)
745
+ end
746
+
747
+ it "raises an exception" do
748
+ expect {
749
+ context
750
+ }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
751
+ end
752
+ end
753
+ end
754
+
755
+ context "when passing an _id" do
756
+
757
+ context "when its of type BSON::ObjectId" do
758
+
759
+ context "when calling it on an empty criteria" do
760
+
761
+ it "returns true" do
762
+ expect(criteria.exists?(hobrecht._id)).to be true
763
+ end
764
+ end
765
+
766
+ context "when calling it on a criteria that includes the object" do
767
+
768
+ it "returns true" do
769
+ expect(criteria.where(street: hobrecht.street).exists?(hobrecht._id)).to be true
770
+ end
771
+ end
772
+
773
+ context "when calling it on a criteria that does not include the object" do
774
+
775
+ it "returns false" do
776
+ expect(criteria.where(street: "bogus").exists?(hobrecht._id)).to be false
777
+ end
778
+ end
779
+
780
+ context "when the id does not exist" do
781
+
782
+ it "returns false" do
783
+ expect(criteria.exists?(BSON::ObjectId.new)).to be false
784
+ end
785
+ end
786
+ end
787
+
788
+ context "when its of type String" do
789
+
790
+ context "when the id exists" do
791
+
792
+ it "returns true" do
793
+ expect(criteria.exists?(hobrecht._id.to_s)).to be true
794
+ end
795
+ end
796
+
797
+ context "when the id does not exist" do
798
+
799
+ it "returns false" do
800
+ expect(criteria.exists?(BSON::ObjectId.new.to_s)).to be false
801
+ end
802
+ end
803
+ end
804
+ end
805
+
806
+ context "when passing a hash" do
807
+
808
+ context "when calling it on an empty criteria" do
809
+
810
+ it "returns true" do
811
+ expect(criteria.exists?(street: hobrecht.street)).to be true
812
+ end
813
+ end
814
+
815
+ context "when calling it on a criteria that includes the object" do
816
+
817
+ it "returns true" do
818
+ expect(criteria.where(_id: hobrecht._id).exists?(street: hobrecht.street)).to be true
819
+ end
820
+ end
821
+
822
+ context "when calling it on a criteria that does not include the object" do
823
+
824
+ it "returns false" do
825
+ expect(criteria.where(_id: BSON::ObjectId.new).exists?(street: hobrecht.street)).to be false
826
+ end
827
+ end
828
+
829
+ context "when the conditions don't match" do
830
+
831
+ it "returns false" do
832
+ expect(criteria.exists?(street: "bogus")).to be false
833
+ end
834
+ end
835
+ end
836
+
837
+ context "when passing false" do
838
+
839
+ it "returns false" do
840
+ expect(criteria.exists?(false)).to be false
841
+ end
842
+ end
843
+
844
+ context "when passing nil" do
845
+
846
+ it "returns false" do
847
+ expect(criteria.exists?(nil)).to be false
848
+ end
849
+ end
850
+
851
+ context "when the limit is 0" do
852
+
853
+ it "returns false" do
854
+ expect(criteria.limit(0).exists?).to be false
855
+ end
856
+ end
857
+ end
858
+
859
+ [ :first, :one ].each do |method|
860
+
861
+ describe "##{method}" do
862
+
863
+ let(:method) { method }
864
+
865
+ let(:hobrecht) do
866
+ Address.new(street: "hobrecht")
867
+ end
868
+
869
+ let(:friedel) do
870
+ Address.new(street: "friedel")
871
+ end
872
+
873
+ let(:criteria) do
874
+ Address.where(:street.in => [ "hobrecht", "friedel" ]).tap do |crit|
875
+ crit.documents = [ hobrecht, friedel ]
876
+ end
877
+ end
878
+
879
+ let(:context) do
880
+ described_class.new(criteria)
881
+ end
882
+
883
+ it "returns the first matching document" do
884
+ expect(context.send(method)).to eq(hobrecht)
885
+ end
886
+
887
+ it "returns a list when passing a limit" do
888
+ expect(context.send(method, 2)).to eq([ hobrecht, friedel ])
889
+ end
890
+
891
+ it "returns a list when passing 1" do
892
+ expect(context.send(method, 1)).to eq([ hobrecht ])
893
+ end
894
+
895
+ include_examples "returns nil when no documents"
896
+
897
+ context 'when there is a collation on the criteria' do
898
+
899
+ let(:criteria) do
900
+ Address.where(:street.in => [ "hobrecht", "friedel" ]).tap do |crit|
901
+ crit.documents = [ hobrecht, friedel ]
902
+ end.collation(locale: 'en_US', strength: 2)
903
+ end
904
+
905
+ it "raises an exception" do
906
+ expect {
907
+ context.send(method)
908
+ }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
909
+ end
910
+ end
911
+ end
912
+ end
913
+
914
+ describe "#first!" do
915
+
916
+ let(:method) { :first! }
917
+
918
+ let(:hobrecht) do
919
+ Address.new(street: "hobrecht")
920
+ end
921
+
922
+ let(:friedel) do
923
+ Address.new(street: "friedel")
924
+ end
925
+
926
+ let(:criteria) do
927
+ Address.where(:street.in => [ "hobrecht", "friedel" ]).tap do |crit|
928
+ crit.documents = [ hobrecht, friedel ]
929
+ end
930
+ end
931
+
932
+ let(:context) do
933
+ described_class.new(criteria)
934
+ end
935
+
936
+ it "returns the first matching document" do
937
+ expect(context.first!).to eq(hobrecht)
938
+ end
939
+
940
+ include_examples "raises an error when no documents"
941
+ end
942
+
943
+ describe "#take" do
944
+
945
+ let(:method) { :take }
946
+
947
+ let(:hobrecht) do
948
+ Address.new(street: "hobrecht")
949
+ end
950
+
951
+ let(:friedel) do
952
+ Address.new(street: "friedel")
953
+ end
954
+
955
+ let(:criteria) do
956
+ Address.where(:street.in => [ "hobrecht", "friedel" ]).tap do |crit|
957
+ crit.documents = [ hobrecht, friedel ]
958
+ end
959
+ end
960
+
961
+ let(:context) do
962
+ described_class.new(criteria)
963
+ end
964
+
965
+ it "returns the first matching document" do
966
+ expect(context.take).to eq(hobrecht)
967
+ end
968
+
969
+ it "returns an array when passing a limit" do
970
+ expect(context.take(2)).to eq([ hobrecht, friedel ])
971
+ end
972
+
973
+ it "returns an array when passing a limit as 1" do
974
+ expect(context.take(1)).to eq([ hobrecht ])
975
+ end
976
+
977
+ include_examples "returns nil when no documents"
978
+
979
+ context 'when there is a collation on the criteria' do
980
+
981
+ let(:criteria) do
982
+ Address.where(:street.in => [ "hobrecht", "friedel" ]).tap do |crit|
983
+ crit.documents = [ hobrecht, friedel ]
984
+ end.collation(locale: 'en_US', strength: 2)
985
+ end
986
+
987
+ it "raises an exception" do
988
+ expect {
989
+ context.take
990
+ }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
991
+ end
992
+ end
993
+ end
994
+
995
+ describe "#take!" do
996
+
997
+ let(:method) { :take! }
998
+
999
+ let(:hobrecht) do
1000
+ Address.new(street: "hobrecht")
1001
+ end
1002
+
1003
+ let(:friedel) do
1004
+ Address.new(street: "friedel")
1005
+ end
1006
+
1007
+ let(:criteria) do
1008
+ Address.where(:street.in => [ "hobrecht", "friedel" ]).tap do |crit|
1009
+ crit.documents = [ hobrecht, friedel ]
1010
+ end
1011
+ end
1012
+
1013
+ let(:context) do
1014
+ described_class.new(criteria)
1015
+ end
1016
+
1017
+ it "returns the first matching document" do
1018
+ expect(context.take!).to eq(hobrecht)
1019
+ end
1020
+
1021
+ include_examples "raises an error when no documents"
1022
+
1023
+ context 'when there is a collation on the criteria' do
1024
+
1025
+ let(:criteria) do
1026
+ Address.where(:street.in => [ "hobrecht", "friedel" ]).tap do |crit|
1027
+ crit.documents = [ hobrecht, friedel ]
1028
+ end.collation(locale: 'en_US', strength: 2)
1029
+ end
1030
+
1031
+ it "raises an exception" do
1032
+ expect {
1033
+ context.take
1034
+ }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
1035
+ end
1036
+ end
1037
+ end
1038
+
1039
+ describe "#initialize" do
1040
+
1041
+ context "when the criteria has no options" do
1042
+
1043
+ let(:hobrecht) do
1044
+ Address.new(street: "hobrecht")
1045
+ end
1046
+
1047
+ let(:friedel) do
1048
+ Address.new(street: "friedel")
1049
+ end
1050
+
1051
+ let(:criteria) do
1052
+ Address.where(street: "hobrecht").tap do |crit|
1053
+ crit.documents = [ hobrecht, friedel ]
1054
+ end
1055
+ end
1056
+
1057
+ let(:context) do
1058
+ described_class.new(criteria)
1059
+ end
1060
+
1061
+ it "sets the criteria" do
1062
+ expect(context.criteria).to eq(criteria)
1063
+ end
1064
+
1065
+ it "sets the klass" do
1066
+ expect(context.klass).to eq(Address)
1067
+ end
1068
+
1069
+ it "sets the matching documents" do
1070
+ expect(context.documents).to eq([ hobrecht ])
1071
+ end
1072
+ end
1073
+
1074
+ context "when the criteria skips" do
1075
+
1076
+ let(:hobrecht) do
1077
+ Address.new(street: "hobrecht")
1078
+ end
1079
+
1080
+ let(:friedel) do
1081
+ Address.new(street: "friedel")
1082
+ end
1083
+
1084
+ let(:criteria) do
1085
+ Address.all.skip(1).tap do |crit|
1086
+ crit.documents = [ hobrecht, friedel ]
1087
+ end
1088
+ end
1089
+
1090
+ let(:context) do
1091
+ described_class.new(criteria)
1092
+ end
1093
+
1094
+ it "limits the matching documents" do
1095
+ expect(context).to eq([ friedel ])
1096
+ end
1097
+ end
1098
+
1099
+ context "when the criteria limits" do
1100
+
1101
+ let(:hobrecht) do
1102
+ Address.new(street: "hobrecht")
1103
+ end
1104
+
1105
+ let(:friedel) do
1106
+ Address.new(street: "friedel")
1107
+ end
1108
+
1109
+ let(:criteria) do
1110
+ Address.all.limit(1).tap do |crit|
1111
+ crit.documents = [ hobrecht, friedel ]
1112
+ end
1113
+ end
1114
+
1115
+ let(:context) do
1116
+ described_class.new(criteria)
1117
+ end
1118
+
1119
+ it "limits the matching documents" do
1120
+ expect(context).to eq([ hobrecht ])
1121
+ end
1122
+ end
1123
+
1124
+ context 'when there is a collation on the criteria' do
1125
+
1126
+ let(:criteria) do
1127
+ Address.all.limit(1).tap do |crit|
1128
+ crit.documents = [ hobrecht, friedel ]
1129
+ end.collation(locale: 'en_US', strength: 2)
1130
+ end
1131
+
1132
+ it "raises an exception" do
1133
+ expect {
1134
+ context
1135
+ }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
1136
+ end
1137
+ end
1138
+ end
1139
+
1140
+ describe "#last" do
1141
+
1142
+ let(:method) { :last }
1143
+
1144
+ let(:hobrecht) do
1145
+ Address.new(street: "hobrecht")
1146
+ end
1147
+
1148
+ let(:friedel) do
1149
+ Address.new(street: "friedel")
1150
+ end
1151
+
1152
+ let(:criteria) do
1153
+ Address.where(:street.in => [ "hobrecht", "friedel" ]).tap do |crit|
1154
+ crit.documents = [ hobrecht, friedel ]
1155
+ end
1156
+ end
1157
+
1158
+ let(:context) do
1159
+ described_class.new(criteria)
1160
+ end
1161
+
1162
+ it "returns the last matching document" do
1163
+ expect(context.last).to eq(friedel)
1164
+ end
1165
+
1166
+ it "returns a list when a limit is passed" do
1167
+ expect(context.last(2)).to eq([ hobrecht, friedel ])
1168
+ end
1169
+
1170
+ it "returns a list when the limit is 1" do
1171
+ expect(context.last(1)).to eq([ friedel ])
1172
+ end
1173
+
1174
+ include_examples "returns nil when no documents"
1175
+
1176
+ context 'when there is a collation on the criteria' do
1177
+
1178
+ let(:criteria) do
1179
+ Address.where(:street.in => [ "hobrecht", "friedel" ]).tap do |crit|
1180
+ crit.documents = [ hobrecht, friedel ]
1181
+ end.collation(locale: 'en_US', strength: 2)
1182
+ end
1183
+
1184
+ it "raises an exception" do
1185
+ expect {
1186
+ context.last
1187
+ }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
1188
+ end
1189
+ end
1190
+ end
1191
+
1192
+ describe "#last!" do
1193
+ let(:method) { :last! }
1194
+
1195
+ let(:hobrecht) do
1196
+ Address.new(street: "hobrecht")
1197
+ end
1198
+
1199
+ let(:friedel) do
1200
+ Address.new(street: "friedel")
1201
+ end
1202
+
1203
+ let(:criteria) do
1204
+ Address.where(:street.in => [ "hobrecht", "friedel" ]).tap do |crit|
1205
+ crit.documents = [ hobrecht, friedel ]
1206
+ end
1207
+ end
1208
+
1209
+ let(:context) do
1210
+ described_class.new(criteria)
1211
+ end
1212
+
1213
+ it "returns the last matching document" do
1214
+ expect(context.last!).to eq(friedel)
1215
+ end
1216
+
1217
+ include_examples "raises an error when no documents"
1218
+ end
1219
+
1220
+ [ :second,
1221
+ :third,
1222
+ :fourth,
1223
+ :fifth,
1224
+ :second_to_last,
1225
+ :third_to_last
1226
+ ].each do |meth|
1227
+ describe "##{meth}" do
1228
+ let(:method) { meth }
1229
+
1230
+ let(:addresses) do
1231
+ [
1232
+ Address.new,
1233
+ Address.new,
1234
+ Address.new,
1235
+ Address.new,
1236
+ Address.new,
1237
+ ]
1238
+ end
1239
+
1240
+ let(:criteria) do
1241
+ Address.all.tap do |crit|
1242
+ crit.documents = addresses
1243
+ end
1244
+ end
1245
+
1246
+ let(:context) do
1247
+ described_class.new(criteria)
1248
+ end
1249
+
1250
+ it "returns the matching document" do
1251
+ expect(context.send(method)).to eq(addresses.send(method))
1252
+ end
1253
+
1254
+ include_examples "returns nil when no documents"
1255
+ end
1256
+
1257
+ describe "##{meth}!" do
1258
+ let(:method) { "#{meth}!" }
1259
+
1260
+ let(:addresses) do
1261
+ [
1262
+ Address.new,
1263
+ Address.new,
1264
+ Address.new,
1265
+ Address.new,
1266
+ Address.new,
1267
+ ]
1268
+ end
1269
+
1270
+ let(:criteria) do
1271
+ Address.all.tap do |crit|
1272
+ crit.documents = addresses
1273
+ end
1274
+ end
1275
+
1276
+ let(:context) do
1277
+ described_class.new(criteria)
1278
+ end
1279
+
1280
+ it "returns the matching document" do
1281
+ expect(context.send(method)).to eq(addresses.send(meth))
1282
+ end
1283
+
1284
+ include_examples "raises an error when no documents"
1285
+ end
1286
+ end
1287
+
1288
+ [ :length, :size ].each do |method|
1289
+
1290
+ describe "##{method}" do
1291
+
1292
+ let(:hobrecht) do
1293
+ Address.new(street: "hobrecht")
1294
+ end
1295
+
1296
+ let(:friedel) do
1297
+ Address.new(street: "friedel")
1298
+ end
1299
+
1300
+ context "when there are matching documents" do
1301
+
1302
+ let(:criteria) do
1303
+ Address.where(street: "hobrecht").tap do |crit|
1304
+ crit.documents = [ hobrecht, friedel ]
1305
+ end
1306
+ end
1307
+
1308
+ let(:context) do
1309
+ described_class.new(criteria)
1310
+ end
1311
+
1312
+ it "returns the number of matches" do
1313
+ expect(context.send(method)).to eq(1)
1314
+ end
1315
+ end
1316
+
1317
+ context "when there are no matching documents" do
1318
+
1319
+ let(:criteria) do
1320
+ Address.where(street: "pfluger").tap do |crit|
1321
+ crit.documents = [ hobrecht, friedel ]
1322
+ end
1323
+ end
1324
+
1325
+ let(:context) do
1326
+ described_class.new(criteria)
1327
+ end
1328
+
1329
+ it "returns zero" do
1330
+ expect(context.send(method)).to eq(0)
1331
+ end
1332
+ end
1333
+
1334
+ context 'when there is a collation on the criteria' do
1335
+
1336
+ let(:criteria) do
1337
+ Address.where(street: "hobrecht").tap do |crit|
1338
+ crit.documents = [ hobrecht, friedel ]
1339
+ end.collation(locale: 'en_US', strength: 2)
1340
+ end
1341
+
1342
+ it "raises an exception" do
1343
+ expect {
1344
+ context.send(method)
1345
+ }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
1346
+ end
1347
+ end
1348
+ end
1349
+ end
1350
+
1351
+ describe "#limit" do
1352
+
1353
+ let(:hobrecht) do
1354
+ Address.new(street: "hobrecht")
1355
+ end
1356
+
1357
+ let(:friedel) do
1358
+ Address.new(street: "friedel")
1359
+ end
1360
+
1361
+ let(:pfluger) do
1362
+ Address.new(street: "pfluger")
1363
+ end
1364
+
1365
+ let(:criteria) do
1366
+ Address.all.tap do |crit|
1367
+ crit.documents = [ hobrecht, friedel, pfluger ]
1368
+ end
1369
+ end
1370
+
1371
+ let(:context) do
1372
+ described_class.new(criteria)
1373
+ end
1374
+
1375
+ context 'when there is no collation on the criteria' do
1376
+
1377
+ let!(:limit) do
1378
+ context.limit(2)
1379
+ end
1380
+
1381
+ it "returns the context" do
1382
+ expect(limit).to eq(context)
1383
+ end
1384
+
1385
+ context "when asking for all documents" do
1386
+
1387
+ context "when only a limit exists" do
1388
+
1389
+ it "only returns the limited documents" do
1390
+ expect(context.entries).to eq([ hobrecht, friedel ])
1391
+ end
1392
+ end
1393
+
1394
+ context "when a skip and limit exist" do
1395
+
1396
+ before do
1397
+ limit.skip(1)
1398
+ end
1399
+
1400
+ it "applies the skip before the limit" do
1401
+ expect(context.entries).to eq([ friedel, pfluger ])
1402
+ end
1403
+ end
1404
+ end
1405
+ end
1406
+
1407
+ context 'when there is a collation on the criteria' do
1408
+
1409
+ let(:criteria) do
1410
+ Address.all.tap do |crit|
1411
+ crit.documents = [ hobrecht, friedel, pfluger ]
1412
+ end.collation(locale: 'en_US', strength: 2)
1413
+ end
1414
+
1415
+ it "raises an exception" do
1416
+ expect {
1417
+ context.limit(2)
1418
+ }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
1419
+ end
1420
+ end
1421
+ end
1422
+
1423
+ describe "#pluck" do
1424
+
1425
+ let(:context) do
1426
+ described_class.new(criteria)
1427
+ end
1428
+
1429
+ let!(:depeche) do
1430
+ Band.create!(name: "Depeche Mode", likes: 3)
1431
+ end
1432
+
1433
+ let!(:tool) do
1434
+ Band.create!(name: "Tool", likes: 3)
1435
+ end
1436
+
1437
+ let!(:photek) do
1438
+ Band.create!(name: "Photek", likes: 1)
1439
+ end
1440
+
1441
+ let!(:maniacs) do
1442
+ Band.create!(name: "10,000 Maniacs", likes: 1, sales: "1E2")
1443
+ end
1444
+
1445
+ let(:criteria) do
1446
+ Band.all.tap do |crit|
1447
+ crit.documents = [ depeche, tool, photek, maniacs ]
1448
+ end
1449
+ end
1450
+
1451
+ context "when the field is aliased" do
1452
+
1453
+ let!(:expensive) do
1454
+ Product.create!(price: 100000)
1455
+ end
1456
+
1457
+ let!(:cheap) do
1458
+ Product.create!(price: 1)
1459
+ end
1460
+
1461
+ let(:criteria) do
1462
+ Product.all.tap do |crit|
1463
+ crit.documents = [ expensive, cheap ]
1464
+ end
1465
+ end
1466
+
1467
+ context "when using alias_attribute" do
1468
+
1469
+ let(:plucked) do
1470
+ context.pluck(:price)
1471
+ end
1472
+
1473
+ it "uses the aliases" do
1474
+ expect(plucked).to eq([ 100000, 1 ])
1475
+ end
1476
+ end
1477
+ end
1478
+
1479
+ context "when the criteria matches" do
1480
+
1481
+ context "when there are no duplicate values" do
1482
+
1483
+ let!(:plucked) do
1484
+ context.pluck(:name)
1485
+ end
1486
+
1487
+ it "returns the values" do
1488
+ expect(plucked).to contain_exactly("10,000 Maniacs", "Depeche Mode", "Tool", "Photek")
1489
+ end
1490
+
1491
+ context "when subsequently executing the criteria without a pluck" do
1492
+
1493
+ it "does not limit the fields" do
1494
+ expect(context.first.likes).to eq(3)
1495
+ end
1496
+ end
1497
+
1498
+ context 'when the field is a subdocument' do
1499
+
1500
+ context 'when a top-level field and a subdocument field are plucked' do
1501
+ let(:criteria) do
1502
+ Band.where(name: 'FKA Twigs').tap do |crit|
1503
+ crit.documents = [
1504
+ Band.create!(name: 'FKA Twigs'),
1505
+ Band.create!(name: 'FKA Twigs', records: [ Record.new(name: 'LP1') ])
1506
+ ]
1507
+ end
1508
+ end
1509
+
1510
+ let(:embedded_pluck) do
1511
+ context.pluck(:name, 'records.name')
1512
+ end
1513
+
1514
+ let(:expected) do
1515
+ [
1516
+ ["FKA Twigs", []],
1517
+ ['FKA Twigs', ["LP1"]]
1518
+ ]
1519
+ end
1520
+
1521
+ it 'returns the list of top-level field and subdocument values' do
1522
+ expect(embedded_pluck).to eq(expected)
1523
+ end
1524
+ end
1525
+
1526
+ context 'when only a subdocument field is plucked' do
1527
+
1528
+ let(:criteria) do
1529
+ Band.where(name: 'FKA Twigs').tap do |crit|
1530
+ crit.documents = [
1531
+ Band.create!(name: 'FKA Twigs'),
1532
+ Band.create!(name: 'FKA Twigs', records: [ Record.new(name: 'LP1') ])
1533
+ ]
1534
+ end
1535
+ end
1536
+
1537
+ let(:embedded_pluck) do
1538
+ context.pluck('records.name')
1539
+ end
1540
+
1541
+ let(:expected) do
1542
+ [
1543
+ [],
1544
+ ["LP1"]
1545
+ ]
1546
+ end
1547
+
1548
+ it 'returns the list of subdocument values' do
1549
+ expect(embedded_pluck).to eq(expected)
1550
+ end
1551
+ end
1552
+ end
1553
+ end
1554
+
1555
+ context "when plucking multi-fields" do
1556
+
1557
+ let(:plucked) do
1558
+ context.pluck(:name, :likes)
1559
+ end
1560
+
1561
+ it "returns the values" do
1562
+ expect(plucked).to contain_exactly(["10,000 Maniacs", 1], ["Depeche Mode", 3], ["Tool", 3], ["Photek", 1])
1563
+ end
1564
+ end
1565
+
1566
+ context "when there are duplicate values" do
1567
+
1568
+ let(:plucked) do
1569
+ context.pluck(:likes)
1570
+ end
1571
+
1572
+ it "returns the duplicates" do
1573
+ expect(plucked).to contain_exactly(1, 3, 3, 1)
1574
+ end
1575
+ end
1576
+ end
1577
+
1578
+ context "when plucking an aliased field" do
1579
+
1580
+ let(:plucked) do
1581
+ context.pluck(:id)
1582
+ end
1583
+
1584
+ it "returns the field values" do
1585
+ expect(plucked).to eq([ depeche.id, tool.id, photek.id, maniacs.id ])
1586
+ end
1587
+ end
1588
+
1589
+ context "when plucking existent and non-existent fields" do
1590
+
1591
+ let(:plucked) do
1592
+ context.pluck(:id, :fooz)
1593
+ end
1594
+
1595
+ it "returns nil for the field that doesnt exist" do
1596
+ expect(plucked).to eq([[depeche.id, nil], [tool.id, nil], [photek.id, nil], [maniacs.id, nil] ])
1597
+ end
1598
+ end
1599
+
1600
+ context "when plucking a field that doesnt exist" do
1601
+
1602
+ context "when pluck one field" do
1603
+
1604
+ let(:plucked) do
1605
+ context.pluck(:foo)
1606
+ end
1607
+
1608
+ it "returns an array with nil values" do
1609
+ expect(plucked).to eq([nil, nil, nil, nil])
1610
+ end
1611
+ end
1612
+
1613
+ context "when pluck multiple fields" do
1614
+
1615
+ let(:plucked) do
1616
+ context.pluck(:foo, :bar)
1617
+ end
1618
+
1619
+ it "returns an array of arrays with nil values" do
1620
+ expect(plucked).to eq([[nil, nil], [nil, nil], [nil, nil], [nil, nil]])
1621
+ end
1622
+ end
1623
+ end
1624
+
1625
+ context 'when plucking a localized field' do
1626
+ with_default_i18n_configs
1627
+
1628
+ before do
1629
+ I18n.locale = :en
1630
+ d = Dictionary.create!(description: 'english-text')
1631
+ I18n.locale = :de
1632
+ d.description = 'deutsch-text'
1633
+ d.save!
1634
+ end
1635
+
1636
+ let(:criteria) do
1637
+ Dictionary.all.tap do |crit|
1638
+ crit.documents = [ Dictionary.first ]
1639
+ end
1640
+ end
1641
+
1642
+ context 'when plucking the entire field' do
1643
+
1644
+ let(:plucked) do
1645
+ context.pluck(:description)
1646
+ end
1647
+
1648
+ let(:plucked_translations) do
1649
+ context.pluck(:description_translations)
1650
+ end
1651
+
1652
+ let(:plucked_translations_both) do
1653
+ context.pluck(:description_translations, :description)
1654
+ end
1655
+
1656
+ it 'returns the demongoized translations' do
1657
+ expect(plucked.first).to eq('deutsch-text')
1658
+ end
1659
+
1660
+ it 'returns the full translations hash to _translations' do
1661
+ expect(plucked_translations.first).to eq({"de"=>"deutsch-text", "en"=>"english-text"})
1662
+ end
1663
+
1664
+ it 'returns both' do
1665
+ expect(plucked_translations_both.first).to eq([{"de"=>"deutsch-text", "en"=>"english-text"}, "deutsch-text"])
1666
+ end
1667
+ end
1668
+
1669
+ context 'when plucking a specific locale' do
1670
+
1671
+ let(:plucked) do
1672
+ context.pluck(:'description.de')
1673
+ end
1674
+
1675
+ it 'returns the specific translations' do
1676
+ expect(plucked.first).to eq('deutsch-text')
1677
+ end
1678
+ end
1679
+
1680
+ context 'when plucking a specific locale from _translations field' do
1681
+
1682
+ let(:plucked) do
1683
+ context.pluck(:'description_translations.de')
1684
+ end
1685
+
1686
+ it 'returns the specific translations' do
1687
+ expect(plucked.first).to eq('deutsch-text')
1688
+ end
1689
+ end
1690
+
1691
+ context 'when fallbacks are enabled with a locale list' do
1692
+ require_fallbacks
1693
+ with_default_i18n_configs
1694
+
1695
+ before do
1696
+ I18n.fallbacks[:he] = [ :en ]
1697
+ end
1698
+
1699
+ let(:plucked) do
1700
+ context.pluck(:description).first
1701
+ end
1702
+
1703
+ it "correctly uses the fallback" do
1704
+ I18n.locale = :en
1705
+ Dictionary.create!(description: 'english-text')
1706
+ I18n.locale = :he
1707
+ plucked.should == "english-text"
1708
+ end
1709
+ end
1710
+
1711
+ context "when the localized field is embedded" do
1712
+ with_default_i18n_configs
1713
+
1714
+ before do
1715
+ p = Passport.new
1716
+ I18n.locale = :en
1717
+ p.name = "Neil"
1718
+ I18n.locale = :he
1719
+ p.name = "Nissim"
1720
+
1721
+ Person.create!(passport: p, employer_id: 12345)
1722
+ end
1723
+
1724
+ let(:plucked) do
1725
+ Person.where(employer_id: 12345).pluck("pass.name").first
1726
+ end
1727
+
1728
+ let(:plucked_translations) do
1729
+ Person.where(employer_id: 12345).pluck("pass.name_translations").first
1730
+ end
1731
+
1732
+ let(:plucked_translations_field) do
1733
+ Person.where(employer_id: 12345).pluck("pass.name_translations.en").first
1734
+ end
1735
+
1736
+ it "returns the translation for the current locale" do
1737
+ expect(plucked).to eq("Nissim")
1738
+ end
1739
+
1740
+ it "returns the full _translation hash" do
1741
+ expect(plucked_translations).to eq({ "en" => "Neil", "he" => "Nissim" })
1742
+ end
1743
+
1744
+ it "returns the translation for the requested locale" do
1745
+ expect(plucked_translations_field).to eq("Neil")
1746
+ end
1747
+ end
1748
+ end
1749
+
1750
+ context 'when plucking a field to be demongoized' do
1751
+
1752
+ let(:plucked) do
1753
+ Band.where(name: maniacs.name).pluck(:sales)
1754
+ end
1755
+
1756
+ with_config_values :map_big_decimal_to_decimal128, true, false do
1757
+
1758
+ it "demongoizes the field" do
1759
+ expect(plucked.first).to be_a(BigDecimal)
1760
+ expect(plucked.first).to eq(BigDecimal("1E2"))
1761
+ end
1762
+ end
1763
+ end
1764
+
1765
+ context "when plucking an embedded field" do
1766
+ let(:label) { Label.new(sales: "1E2") }
1767
+ let!(:band) { Band.create!(label: label) }
1768
+
1769
+ let(:plucked) do
1770
+ Band.where(_id: band.id).tap do |crit|
1771
+ crit.documents = [ band ]
1772
+ end.pluck("label.sales")
1773
+ end
1774
+
1775
+ it "demongoizes the field" do
1776
+ expect(plucked).to eq([ BigDecimal("1E2") ])
1777
+ end
1778
+ end
1779
+
1780
+ context "when plucking an embeds_many field" do
1781
+ let(:label) { Label.new(sales: "1E2") }
1782
+ let!(:band) { Band.create!(labels: [label]) }
1783
+
1784
+ let(:plucked) { Band.where(_id: band.id).pluck("labels.sales") }
1785
+
1786
+ it "demongoizes the field" do
1787
+ expect(plucked.first).to eq([ BigDecimal("1E2") ])
1788
+ end
1789
+ end
1790
+
1791
+ context "when plucking a nonexistent embedded field" do
1792
+ let(:label) { Label.new(sales: "1E2") }
1793
+ let!(:band) { Band.create!(label: label) }
1794
+
1795
+ let(:plucked) do
1796
+ Band.where(_id: band.id).tap do |crit|
1797
+ crit.documents = [ band ]
1798
+ end.pluck("label.qwerty")
1799
+ end
1800
+
1801
+ it "returns nil" do
1802
+ expect(plucked.first).to eq(nil)
1803
+ end
1804
+ end
1805
+
1806
+ context "when plucking deeply nested arrays/embedded associations" do
1807
+
1808
+ let(:criteria) do
1809
+ Person.all.tap do |crit|
1810
+ crit.documents = [
1811
+ Person.create!(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 2 } } ]))) ]),
1812
+ Person.create!(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 2 } } ]))) ]),
1813
+ Person.create!(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 3 } } ]))) ]),
1814
+ ]
1815
+ end
1816
+ end
1817
+
1818
+ let(:plucked) do
1819
+ context.pluck("addresses.code.deepest.array.y.z")
1820
+ end
1821
+
1822
+ it "returns the correct hash" do
1823
+ expect(plucked).to eq([
1824
+ [ [ 1, 2 ] ], [ [ 1, 2 ] ], [ [ 1, 3 ] ]
1825
+ ])
1826
+ end
1827
+ end
1828
+ end
1829
+
1830
+ describe "#pick" do
1831
+
1832
+ let(:depeche) do
1833
+ Band.create!(name: "Depeche Mode", likes: 3)
1834
+ end
1835
+
1836
+ let(:tool) do
1837
+ Band.create!(name: "Tool", likes: 3)
1838
+ end
1839
+
1840
+ let(:criteria) do
1841
+ Band.all.tap do |crit|
1842
+ crit.documents = [ depeche, tool ]
1843
+ end
1844
+ end
1845
+
1846
+ let(:context) do
1847
+ described_class.new(criteria)
1848
+ end
1849
+
1850
+ context "when picking a field" do
1851
+
1852
+ let(:picked) do
1853
+ context.pick(:name)
1854
+ end
1855
+
1856
+ it "returns one element" do
1857
+ expect(picked).to eq("Depeche Mode")
1858
+ end
1859
+ end
1860
+
1861
+ context "when picking multiple fields" do
1862
+
1863
+ let(:picked) do
1864
+ context.pick(:name, :likes)
1865
+ end
1866
+
1867
+ it "returns an array" do
1868
+ expect(picked).to eq([ "Depeche Mode", 3 ])
1869
+ end
1870
+ end
1871
+
1872
+ context "when no documents to pick" do
1873
+
1874
+ let(:criteria) do
1875
+ Band.all.tap do |crit|
1876
+ crit.documents = []
1877
+ end
1878
+ end
1879
+
1880
+ let(:picked) do
1881
+ context.pick(:name)
1882
+ end
1883
+
1884
+ it "returns nil" do
1885
+ expect(picked).to be_nil
1886
+ end
1887
+ end
1888
+ end
1889
+
1890
+ describe "#tally" do
1891
+ let(:fans1) { [ Fanatic.new(age:1), Fanatic.new(age:2) ] }
1892
+ let(:fans2) { [ Fanatic.new(age:1), Fanatic.new(age:2) ] }
1893
+ let(:fans3) { [ Fanatic.new(age:1), Fanatic.new(age:3) ] }
1894
+
1895
+ let(:genres1) { [ { x: 1, y: { z: 1 } }, { x: 2, y: { z: 2 } }, { y: 3 } ]}
1896
+ let(:genres2) { [ { x: 1, y: { z: 1 } }, { x: 2, y: { z: 2 } }, { y: 4 } ]}
1897
+ let(:genres3) { [ { x: 1, y: { z: 1 } }, { x: 3, y: { z: 3 } }, { y: 5 } ]}
1898
+
1899
+ let(:label1) { Label.new(name: "Atlantic") }
1900
+ let(:label2) { Label.new(name: "Atlantic") }
1901
+ let(:label3) { Label.new(name: "Columbia") }
1902
+
1903
+
1904
+ let(:band1) { Band.new(origin: "tally", name: "Depeche Mode", years: 30, sales: "1E2", label: label1, genres: genres1) }
1905
+ let(:band2) { Band.new(origin: "tally", name: "New Order", years: 30, sales: "2E3", label: label2, genres: genres2) }
1906
+ let(:band3) { Band.new(origin: "tally", name: "10,000 Maniacs", years: 30, sales: "1E2", label: label3, genres: genres3) }
1907
+ let(:band4) { Band.new(origin: "tally2", fanatics: fans1, genres: [1, 2]) }
1908
+ let(:band5) { Band.new(origin: "tally2", fanatics: fans2, genres: [1, 2]) }
1909
+ let(:band6) { Band.new(origin: "tally2", fanatics: fans3, genres: [1, 3]) }
1910
+
1911
+ let(:criteria) do
1912
+ Band.where(origin: "tally").all.tap do |crit|
1913
+ crit.documents = [ band1, band2, band3 ]
1914
+ end
1915
+ end
1916
+
1917
+ let(:criteria2) do
1918
+ Band.where(origin: "tally2").tap do |crit|
1919
+ crit.documents = [ band4, band5, band6 ]
1920
+ end
1921
+ end
1922
+
1923
+ let(:context) do
1924
+ described_class.new(criteria)
1925
+ end
1926
+
1927
+ let(:context2) do
1928
+ described_class.new(criteria2)
1929
+ end
1930
+
1931
+ context "when tallying a string" do
1932
+ let(:tally) do
1933
+ context.tally(:name)
1934
+ end
1935
+
1936
+ it "returns the correct hash" do
1937
+ expect(tally).to eq("Depeche Mode" => 1, "New Order" => 1, "10,000 Maniacs" => 1)
1938
+ end
1939
+ end
1940
+
1941
+ context "using an aliased field" do
1942
+ let(:tally) do
1943
+ context.tally(:years)
1944
+ end
1945
+
1946
+ it "returns the correct hash" do
1947
+ expect(tally).to eq(30 => 3)
1948
+ end
1949
+ end
1950
+
1951
+ context "when tallying a demongoizable field" do
1952
+ let(:tally) do
1953
+ context.tally(:sales)
1954
+ end
1955
+
1956
+ it "returns the correct hash" do
1957
+ expect(tally).to eq(BigDecimal("1E2") => 2, BigDecimal("2E3") => 1)
1958
+ end
1959
+ end
1960
+
1961
+ context "when tallying a localized field" do
1962
+ with_default_i18n_configs
1963
+
1964
+ let(:d1) { Dictionary.new(description: 'en1') }
1965
+ let(:d2) { Dictionary.new(description: 'en1') }
1966
+ let(:d3) { Dictionary.new(description: 'en1') }
1967
+ let(:d4) { Dictionary.new(description: 'en2') }
1968
+
1969
+ before do
1970
+ I18n.locale = :en
1971
+ d1
1972
+ d2
1973
+ d3
1974
+ d4
1975
+ I18n.locale = :de
1976
+ d1.description = 'de1'
1977
+ d2.description = 'de1'
1978
+ d3.description = 'de2'
1979
+ d4.description = 'de3'
1980
+ I18n.locale = :en
1981
+ end
1982
+
1983
+ let(:criteria) do
1984
+ Dictionary.all.tap do |crit|
1985
+ crit.documents = [ d1, d2, d3, d4 ]
1986
+ end
1987
+ end
1988
+
1989
+ context "when getting the demongoized field" do
1990
+ let(:tallied) do
1991
+ context.tally(:description)
1992
+ end
1993
+
1994
+ it "returns the translation for the current locale" do
1995
+ expect(tallied).to eq("en1" => 3, "en2" => 1)
1996
+ end
1997
+ end
1998
+
1999
+ context "when getting a specific locale" do
2000
+ let(:tallied) do
2001
+ context.tally("description.de")
2002
+ end
2003
+
2004
+ it "returns the translation for the the specific locale" do
2005
+ expect(tallied).to eq("de1" => 2, "de2" => 1, "de3" => 1)
2006
+ end
2007
+ end
2008
+
2009
+ context "when getting the full hash" do
2010
+ let(:tallied) do
2011
+ context.tally("description_translations")
2012
+ end
2013
+
2014
+ it "returns the correct hash" do
2015
+ expect(tallied).to eq(
2016
+ {"de" => "de1", "en" => "en1" } => 2,
2017
+ {"de" => "de2", "en" => "en1" } => 1,
2018
+ {"de" => "de3", "en" => "en2" } => 1
2019
+ )
2020
+ end
2021
+ end
2022
+ end
2023
+
2024
+ context "when tallying an embedded localized field" do
2025
+ with_default_i18n_configs
2026
+
2027
+ let(:person1) { Person.create!(addresses: [ address1a, address1b ]) }
2028
+ let(:person2) { Person.create!(addresses: [ address2a, address2b ]) }
2029
+
2030
+ let(:address1a) { Address.new(name: "en1") }
2031
+ let(:address1b) { Address.new(name: "en2") }
2032
+ let(:address2a) { Address.new(name: "en1") }
2033
+ let(:address2b) { Address.new(name: "en3") }
2034
+
2035
+ before do
2036
+ I18n.locale = :en
2037
+ address1a
2038
+ address1b
2039
+ address2a
2040
+ address2b
2041
+ I18n.locale = :de
2042
+ address1a.name = "de1"
2043
+ address1b.name = "de2"
2044
+ address2a.name = "de1"
2045
+ address2b.name = "de3"
2046
+ person1
2047
+ person2
2048
+ I18n.locale = :en
2049
+ end
2050
+
2051
+ let(:criteria) do
2052
+ Person.all.tap do |crit|
2053
+ crit.documents = [ person1, person2 ]
2054
+ end
2055
+ end
2056
+
2057
+ context "when getting the demongoized field" do
2058
+ let(:tallied) do
2059
+ context.tally("addresses.name")
2060
+ end
2061
+
2062
+ it "returns the translation for the current locale" do
2063
+ expect(tallied).to eq(
2064
+ [ "en1", "en2" ] => 1,
2065
+ [ "en1", "en3" ] => 1,
2066
+ )
2067
+ end
2068
+ end
2069
+
2070
+ context "when getting a specific locale" do
2071
+ let(:tallied) do
2072
+ context.tally("addresses.name.de")
2073
+ end
2074
+
2075
+ it "returns the translation for the the specific locale" do
2076
+ expect(tallied).to eq(
2077
+ [ "de1", "de2" ] => 1,
2078
+ [ "de1", "de3" ] => 1,
2079
+ )
2080
+ end
2081
+ end
2082
+
2083
+ context "when getting the full hash" do
2084
+ let(:tallied) do
2085
+ context.tally("addresses.name_translations")
2086
+ end
2087
+
2088
+ it "returns the correct hash" do
2089
+ expect(tallied).to eq(
2090
+ [{ "de" => "de1", "en" => "en1" }, { "de" => "de2", "en" => "en2" }] => 1,
2091
+ [{ "de" => "de1", "en" => "en1" }, { "de" => "de3", "en" => "en3" }] => 1,
2092
+ )
2093
+ end
2094
+ end
2095
+ end
2096
+
2097
+ context "when tallying an embedded field" do
2098
+ let(:tally) do
2099
+ context.tally("label.name")
2100
+ end
2101
+
2102
+ it "returns the correct hash" do
2103
+ expect(tally).to eq("Atlantic" => 2, "Columbia" => 1)
2104
+ end
2105
+ end
2106
+
2107
+ context "when tallying an element in an embeds_many field" do
2108
+
2109
+ let(:tally) do
2110
+ context2.tally("fanatics.age")
2111
+ end
2112
+
2113
+ it "returns the correct hash" do
2114
+ expect(tally).to eq(
2115
+ [1, 2] => 2,
2116
+ [1, 3] => 1
2117
+ )
2118
+ end
2119
+ end
2120
+
2121
+ context "when tallying an embeds_many field" do
2122
+
2123
+ let(:tally) do
2124
+ context2.tally("fanatics")
2125
+ end
2126
+
2127
+ it "returns the correct hash" do
2128
+ expect(tally).to eq(
2129
+ fans1 => 1,
2130
+ fans2 => 1,
2131
+ fans3 => 1,
2132
+ )
2133
+ end
2134
+ end
2135
+
2136
+ context "when tallying a field of type array" do
2137
+
2138
+ let(:tally) do
2139
+ context2.tally("genres")
2140
+ end
2141
+
2142
+ it "returns the correct hash" do
2143
+ expect(tally).to eq(
2144
+ [1, 2] => 2,
2145
+ [1, 3] => 1
2146
+ )
2147
+ end
2148
+ end
2149
+
2150
+ context "when tallying an element from an array of hashes" do
2151
+
2152
+ let(:tally) do
2153
+ context.tally("genres.x")
2154
+ end
2155
+
2156
+ it "returns the correct hash without the nil keys" do
2157
+ expect(tally).to eq(
2158
+ [1, 2] => 2,
2159
+ [1, 3] => 1
2160
+ )
2161
+ end
2162
+ end
2163
+
2164
+ context "when tallying an element from an array of hashes; with duplicate" do
2165
+
2166
+ let(:band4) { Band.new(origin: "tally", genres: [ { x: 1 }, {x: 1} ] ) }
2167
+
2168
+ let(:criteria) do
2169
+ Band.where(origin: "tally").all.tap do |crit|
2170
+ crit.documents = [ band1, band2, band3, band4 ]
2171
+ end
2172
+ end
2173
+
2174
+ let(:tally) do
2175
+ context.tally("genres.x")
2176
+ end
2177
+
2178
+ it "returns the correct hash without the nil keys" do
2179
+ expect(tally).to eq(
2180
+ [1, 2] => 2,
2181
+ [1, 3] => 1,
2182
+ [1, 1] => 1,
2183
+ )
2184
+ end
2185
+ end
2186
+
2187
+ context "when tallying an aliased field of type array" do
2188
+
2189
+ let(:person1) { Person.new(array: [ 1, 2 ]) }
2190
+ let(:person2) { Person.new(array: [ 1, 3 ]) }
2191
+
2192
+ let(:criteria) do
2193
+ Person.all.tap do |crit|
2194
+ crit.documents = [ person1, person2 ]
2195
+ end
2196
+ end
2197
+
2198
+ let(:tally) do
2199
+ context.tally("array")
2200
+ end
2201
+
2202
+ it "returns the correct hash" do
2203
+ expect(tally).to eq(
2204
+ [1, 2] => 1,
2205
+ [1, 3] => 1
2206
+ )
2207
+ end
2208
+ end
2209
+
2210
+ context "when going multiple levels deep in arrays" do
2211
+
2212
+ let(:tally) do
2213
+ context.tally("genres.y.z")
2214
+ end
2215
+
2216
+ it "returns the correct hash" do
2217
+ expect(tally).to eq(
2218
+ [1, 2] => 2,
2219
+ [1, 3] => 1
2220
+ )
2221
+ end
2222
+ end
2223
+
2224
+ context "when going multiple levels deep in an array" do
2225
+
2226
+ let(:tally) do
2227
+ context.tally("genres.y.z")
2228
+ end
2229
+
2230
+ it "returns the correct hash" do
2231
+ expect(tally).to eq(
2232
+ [1, 2] => 2,
2233
+ [1, 3] => 1
2234
+ )
2235
+ end
2236
+ end
2237
+
2238
+ context "when tallying deeply nested arrays/embedded associations" do
2239
+
2240
+ let(:person1) { Person.new(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 2 } } ]))) ]) }
2241
+ let(:person2) { Person.new(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 2 } } ]))) ]) }
2242
+ let(:person3) { Person.new(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 3 } } ]))) ]) }
2243
+
2244
+ let(:criteria) do
2245
+ Person.all.tap do |crit|
2246
+ crit.documents = [ person1, person2, person3 ]
2247
+ end
2248
+ end
2249
+
2250
+ let(:tally) do
2251
+ context.tally("addresses.code.deepest.array.y.z")
2252
+ end
2253
+
2254
+ it "returns the correct hash" do
2255
+ expect(tally).to eq(
2256
+ [ [ 1, 2 ] ] => 2,
2257
+ [ [ 1, 3 ] ] => 1
2258
+ )
2259
+ end
2260
+ end
2261
+
2262
+ context "when tallying deeply nested arrays/embedded associations" do
2263
+
2264
+ let(:person1) do
2265
+ Person.new(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 2 } } ]))),
2266
+ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 2 } } ]))) ])
2267
+ end
2268
+
2269
+ let(:person2) do
2270
+ Person.new(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 2 } } ]))),
2271
+ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 2 } } ]))) ])
2272
+ end
2273
+
2274
+ let(:person3) do
2275
+ Person.new(addresses: [ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 3 } } ]))),
2276
+ Address.new(code: Code.new(deepest: Deepest.new(array: [ { y: { z: 1 } }, { y: { z: 3 } } ]))) ])
2277
+ end
2278
+
2279
+ let(:criteria) do
2280
+ Person.all.tap do |crit|
2281
+ crit.documents = [ person1, person2, person3 ]
2282
+ end
2283
+ end
2284
+
2285
+ let(:tally) do
2286
+ context.tally("addresses.code.deepest.array.y.z")
2287
+ end
2288
+
2289
+ it "returns the correct hash" do
2290
+ expect(tally).to eq(
2291
+ [ [ 1, 2 ], [ 1, 2 ] ] => 2,
2292
+ [ [ 1, 3 ], [ 1, 3 ] ] => 1
2293
+ )
2294
+ end
2295
+ end
2296
+
2297
+ context "when some keys are missing" do
2298
+
2299
+ let(:criteria) do
2300
+ Band.where(origin: "tally").all.tap do |crit|
2301
+ crit.documents = [ band1, band2, band3 ]
2302
+ 3.times{ crit.documents << Band.new(origin: "tally") }
2303
+ end
2304
+ end
2305
+
2306
+ let(:tally) do
2307
+ context.tally(:name)
2308
+ end
2309
+
2310
+ it "returns the correct hash" do
2311
+ expect(tally).to eq(
2312
+ "Depeche Mode" => 1,
2313
+ "New Order" => 1,
2314
+ "10,000 Maniacs" => 1,
2315
+ nil => 3
2316
+ )
2317
+ end
2318
+ end
2319
+
2320
+ context "when the first element is an embeds_one" do
2321
+ let(:person1) { Person.create!(name: Name.new(translations: [ Translation.new(language: 1), Translation.new(language: 2) ])) }
2322
+ let(:person2) { Person.create!(name: Name.new(translations: [ Translation.new(language: 1), Translation.new(language: 2) ])) }
2323
+ let(:person3) { Person.create!(name: Name.new(translations: [ Translation.new(language: 1), Translation.new(language: 3) ])) }
2324
+
2325
+ let(:criteria) do
2326
+ Person.all.tap do |crit|
2327
+ crit.documents = [ person1, person2, person3 ]
2328
+ end
2329
+ end
2330
+
2331
+ let(:tally) do
2332
+ context.tally("name.translations.language")
2333
+ end
2334
+
2335
+ it "returns the correct hash" do
2336
+ expect(tally).to eq(
2337
+ [1, 2] => 2,
2338
+ [1, 3] => 1
2339
+ )
2340
+ end
2341
+ end
2342
+ end
2343
+
2344
+ describe '#inc' do
2345
+
2346
+ let(:criteria) do
2347
+ Address.all.tap do |crit|
2348
+ crit.documents = [ Address.new(number: 1),
2349
+ Address.new(number: 2),
2350
+ Address.new(number: 3) ]
2351
+ end
2352
+ end
2353
+
2354
+ let(:context) do
2355
+ described_class.new(criteria)
2356
+ end
2357
+
2358
+ it 'increases each member' do
2359
+ expect(context.inc(number: 10).collect(&:number)).to eql([11, 12, 13])
2360
+ end
2361
+ end
2362
+
2363
+ describe "#skip" do
2364
+
2365
+ let(:hobrecht) do
2366
+ Address.new(street: "hobrecht")
2367
+ end
2368
+
2369
+ let(:friedel) do
2370
+ Address.new(street: "friedel")
2371
+ end
2372
+
2373
+ let(:pfluger) do
2374
+ Address.new(street: "pfluger")
2375
+ end
2376
+
2377
+ let(:criteria) do
2378
+ Address.all.tap do |crit|
2379
+ crit.documents = [ hobrecht, friedel, pfluger ]
2380
+ end
2381
+ end
2382
+
2383
+ let(:context) do
2384
+ described_class.new(criteria)
2385
+ end
2386
+
2387
+ context 'when there is no collation on the criteria' do
2388
+
2389
+ let!(:skip) do
2390
+ context.skip(1)
2391
+ end
2392
+
2393
+ it "returns the context" do
2394
+ expect(skip).to eq(context)
2395
+ end
2396
+
2397
+ context "when asking for all documents" do
2398
+
2399
+ context "when only a skip exists" do
2400
+
2401
+ it "skips the correct number" do
2402
+ expect(context.entries).to eq([ friedel, pfluger ])
2403
+ end
2404
+ end
2405
+
2406
+ context "when a skip and limit exist" do
2407
+
2408
+ before do
2409
+ skip.limit(1)
2410
+ end
2411
+
2412
+ it "applies the skip before the limit" do
2413
+ expect(context.entries).to eq([ friedel ])
2414
+ end
2415
+ end
2416
+ end
2417
+ end
2418
+
2419
+ context 'when there is a collation on the criteria' do
2420
+
2421
+ let(:criteria) do
2422
+ Address.all.tap do |crit|
2423
+ crit.documents = [ hobrecht, friedel, pfluger ]
2424
+ end.collation(locale: 'en_US', strength: 2)
2425
+ end
2426
+
2427
+ it "raises an exception" do
2428
+ expect {
2429
+ context.skip(1)
2430
+ }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
2431
+ end
2432
+ end
2433
+ end
2434
+
2435
+ describe "#sort" do
2436
+
2437
+ let(:hobrecht) do
2438
+ Address.new(street: "hobrecht", number: 9, name: "hobrecht")
2439
+ end
2440
+
2441
+ let(:friedel) do
2442
+ Address.new(street: "friedel", number: 1, name: "friedel")
2443
+ end
2444
+
2445
+ let(:pfluger) do
2446
+ Address.new(street: "pfluger", number: 5, name: "pfluger")
2447
+ end
2448
+
2449
+ let(:criteria) do
2450
+ Address.all.tap do |crit|
2451
+ crit.documents = [ hobrecht, friedel, pfluger ]
2452
+ end
2453
+ end
2454
+
2455
+ let(:context) do
2456
+ described_class.new(criteria)
2457
+ end
2458
+
2459
+ context "when providing a single field sort" do
2460
+
2461
+ context "when the sort is ascending" do
2462
+
2463
+ let!(:sorted) do
2464
+ context.sort(street: 1)
2465
+ end
2466
+
2467
+ it "sorts the documents" do
2468
+ expect(context.entries).to eq([ friedel, hobrecht, pfluger ])
2469
+ end
2470
+
2471
+ it "returns the context" do
2472
+ expect(sorted).to eq(context)
2473
+ end
2474
+ end
2475
+
2476
+ context "when the sort is descending" do
2477
+
2478
+ context "when sorting on a string" do
2479
+
2480
+ let!(:sorted) do
2481
+ context.sort(street: -1)
2482
+ end
2483
+
2484
+ it "sorts the documents" do
2485
+ expect(context.entries).to eq([ pfluger, hobrecht, friedel ])
2486
+ end
2487
+
2488
+ it "returns the context" do
2489
+ expect(sorted).to eq(context)
2490
+ end
2491
+ end
2492
+
2493
+ context "when sorting on a time" do
2494
+
2495
+ before do
2496
+ pfluger.move_in = 30.days.ago
2497
+ hobrecht.move_in = 25.days.ago
2498
+ end
2499
+
2500
+ let!(:sorted) do
2501
+ context.sort(move_in: -1)
2502
+ end
2503
+
2504
+ it "sorts the documents" do
2505
+ expect(context.entries).to eq([ friedel, hobrecht, pfluger ])
2506
+ end
2507
+
2508
+ it "returns the context" do
2509
+ expect(sorted).to eq(context)
2510
+ end
2511
+ end
2512
+ end
2513
+ end
2514
+
2515
+ context "when providing multiple sort fields" do
2516
+
2517
+ let(:lenau) do
2518
+ Address.new(street: "lenau", number: 5, name: "lenau")
2519
+ end
2520
+
2521
+ let(:kampuchea_krom) do
2522
+ Address.new(street: "kampuchea krom", number: 5, name: "kampuchea krom")
2523
+ end
2524
+
2525
+ before do
2526
+ criteria.documents.unshift(lenau)
2527
+ criteria.documents.unshift(kampuchea_krom)
2528
+ end
2529
+
2530
+ context "when the sort is ascending" do
2531
+
2532
+ let!(:sorted) do
2533
+ context.sort(number: 1, street: 1)
2534
+ end
2535
+
2536
+ it "sorts the documents" do
2537
+ expect(context.entries).to eq([ friedel, kampuchea_krom, lenau, pfluger, hobrecht ])
2538
+ end
2539
+
2540
+ it "returns the context" do
2541
+ expect(sorted).to eq(context)
2542
+ end
2543
+ end
2544
+
2545
+ context "when the sort is descending" do
2546
+
2547
+ let!(:sorted) do
2548
+ context.sort(number: -1, street: -1)
2549
+ end
2550
+
2551
+ it "sorts the documents" do
2552
+ expect(context.entries).to eq([ hobrecht, pfluger, lenau, kampuchea_krom, friedel ])
2553
+ end
2554
+
2555
+ it "returns the context" do
2556
+ expect(sorted).to eq(context)
2557
+ end
2558
+ end
2559
+ end
2560
+
2561
+ context "when the field is nil" do
2562
+
2563
+ let!(:sorted) do
2564
+ context.sort(state: 1)
2565
+ end
2566
+
2567
+ it "does not sort the documents" do
2568
+ expect(context.entries).to eq([ hobrecht, friedel, pfluger ])
2569
+ end
2570
+
2571
+ it "returns the context" do
2572
+ expect(sorted).to eq(context)
2573
+ end
2574
+ end
2575
+
2576
+ context "with localized field" do
2577
+
2578
+ let!(:sorted) do
2579
+ context.sort("name.en" => 1)
2580
+ end
2581
+
2582
+ it "sorts the documents" do
2583
+ expect(context.entries).to eq([ friedel, hobrecht, pfluger ])
2584
+ end
2585
+ end
2586
+
2587
+ context 'when there is a collation on the criteria' do
2588
+
2589
+ let(:criteria) do
2590
+ Address.all.tap do |crit|
2591
+ crit.documents = [ hobrecht, friedel, pfluger ]
2592
+ end.collation(locale: 'en_US', strength: 2)
2593
+ end
2594
+
2595
+ it "raises an exception" do
2596
+ expect {
2597
+ context.sort(state: 1)
2598
+ }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
2599
+ end
2600
+ end
2601
+ end
2602
+
2603
+ describe "#update" do
2604
+
2605
+ let(:person) do
2606
+ Person.create!
2607
+ end
2608
+
2609
+ let(:hobrecht) do
2610
+ person.addresses.create!(street: "hobrecht")
2611
+ end
2612
+
2613
+ let(:friedel) do
2614
+ person.addresses.create!(street: "friedel")
2615
+ end
2616
+
2617
+ let(:pfluger) do
2618
+ person.addresses.create!(street: "pfluger")
2619
+ end
2620
+
2621
+ context "when the documents are embedded one level" do
2622
+
2623
+ let(:criteria) do
2624
+ Address.any_in(street: [ "hobrecht", "friedel" ]).tap do |crit|
2625
+ crit.documents = [ hobrecht, friedel, pfluger ]
2626
+ end
2627
+ end
2628
+
2629
+ let(:context) do
2630
+ described_class.new(criteria)
2631
+ end
2632
+
2633
+ context "when attributes are provided" do
2634
+
2635
+ before do
2636
+ context.update(number: 5)
2637
+ end
2638
+
2639
+ it "updates the first matching document" do
2640
+ expect(hobrecht.number).to eq(5)
2641
+ end
2642
+
2643
+ it "does not update the last matching document" do
2644
+ expect(friedel.number).to be_nil
2645
+ end
2646
+
2647
+ it "does not update non matching docs" do
2648
+ expect(pfluger.number).to be_nil
2649
+ end
2650
+
2651
+ context "when reloading the embedded documents" do
2652
+
2653
+ it "updates the first matching document" do
2654
+ expect(hobrecht.reload.number).to eq(5)
2655
+ end
2656
+
2657
+ it "updates the last matching document" do
2658
+ expect(friedel.reload.number).to be_nil
2659
+ end
2660
+
2661
+ it "does not update non matching docs" do
2662
+ expect(pfluger.reload.number).to be_nil
2663
+ end
2664
+ end
2665
+ end
2666
+
2667
+ context "when no attributes are provided" do
2668
+
2669
+ it "returns false" do
2670
+ expect(context.update).to be false
2671
+ end
2672
+ end
2673
+ end
2674
+
2675
+ context "when the documents are embedded multiple levels" do
2676
+
2677
+ let!(:home) do
2678
+ hobrecht.locations.create!(name: "home")
2679
+ end
2680
+
2681
+ let!(:work) do
2682
+ hobrecht.locations.create!(name: "work")
2683
+ end
2684
+
2685
+ let(:criteria) do
2686
+ Location.where(name: "work").tap do |crit|
2687
+ crit.documents = [ home, work ]
2688
+ end
2689
+ end
2690
+
2691
+ let(:context) do
2692
+ described_class.new(criteria)
2693
+ end
2694
+
2695
+ context "when attributes are provided" do
2696
+
2697
+ before do
2698
+ context.update(number: 5)
2699
+ end
2700
+
2701
+ it "updates the first matching document" do
2702
+ expect(work.number).to eq(5)
2703
+ end
2704
+
2705
+ it "does not update non matching docs" do
2706
+ expect(home.number).to be_nil
2707
+ end
2708
+
2709
+ context "when reloading the embedded documents" do
2710
+
2711
+ it "updates the first matching document" do
2712
+ expect(work.reload.number).to eq(5)
2713
+ end
2714
+
2715
+ it "does not update non matching docs" do
2716
+ expect(home.reload.number).to be_nil
2717
+ end
2718
+ end
2719
+ end
2720
+
2721
+ context "when no attributes are provided" do
2722
+
2723
+ it "returns false" do
2724
+ expect(context.update).to be false
2725
+ end
2726
+ end
2727
+ end
2728
+
2729
+ context 'when there is a collation on the criteria' do
2730
+
2731
+ let(:criteria) do
2732
+ Address.all.collation(locale: 'en_US', strength: 2)
2733
+ end
2734
+
2735
+ it "raises an exception" do
2736
+ expect {
2737
+ context.update
2738
+ }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
2739
+ end
2740
+ end
2741
+ end
2742
+
2743
+ describe "#update_all" do
2744
+
2745
+ let(:person) do
2746
+ Person.create!
2747
+ end
2748
+
2749
+ let(:hobrecht) do
2750
+ person.addresses.create!(street: "hobrecht")
2751
+ end
2752
+
2753
+ let(:friedel) do
2754
+ person.addresses.create!(street: "friedel")
2755
+ end
2756
+
2757
+ let(:pfluger) do
2758
+ person.addresses.create!(street: "pfluger")
2759
+ end
2760
+
2761
+ context 'when there is a collation on the criteria' do
2762
+
2763
+ let(:criteria) do
2764
+ Address.all.collation(locale: 'en_US', strength: 2)
2765
+ end
2766
+
2767
+ it "raises an exception" do
2768
+ expect {
2769
+ context.update_all({})
2770
+ }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
2771
+ end
2772
+ end
2773
+
2774
+ context "when the documents are empty" do
2775
+
2776
+ let(:person_two) do
2777
+ Person.create!
2778
+ end
2779
+
2780
+ let(:criteria) do
2781
+ Address.all
2782
+ end
2783
+
2784
+ let(:context) do
2785
+ described_class.new(criteria)
2786
+ end
2787
+
2788
+ it "returns false" do
2789
+ expect(context.update_all({})).to be false
2790
+ end
2791
+ end
2792
+
2793
+ context "when the documents are embedded one level" do
2794
+
2795
+ let(:criteria) do
2796
+ Address.any_in(street: [ "hobrecht", "friedel" ]).tap do |crit|
2797
+ crit.documents = [ hobrecht, friedel, pfluger ]
2798
+ end
2799
+ end
2800
+
2801
+ let(:context) do
2802
+ described_class.new(criteria)
2803
+ end
2804
+
2805
+ context "when providing aliased fields" do
2806
+
2807
+ before do
2808
+ context.update_all(suite: "10B")
2809
+ end
2810
+
2811
+ it "updates the first matching document" do
2812
+ expect(hobrecht.suite).to eq("10B")
2813
+ end
2814
+
2815
+ it "updates the last matching document" do
2816
+ expect(friedel.suite).to eq("10B")
2817
+ end
2818
+
2819
+ it "does not update non matching docs" do
2820
+ expect(pfluger.suite).to be_nil
2821
+ end
2822
+ end
2823
+
2824
+ context "when attributes are provided" do
2825
+
2826
+ before do
2827
+ context.update_all(number: 5)
2828
+ end
2829
+
2830
+ it "updates the first matching document" do
2831
+ expect(hobrecht.number).to eq(5)
2832
+ end
2833
+
2834
+ it "updates the last matching document" do
2835
+ expect(friedel.number).to eq(5)
2836
+ end
2837
+
2838
+ it "does not update non matching docs" do
2839
+ expect(pfluger.number).to be_nil
2840
+ end
2841
+
2842
+ context "when reloading the embedded documents" do
2843
+
2844
+ it "updates the first matching document" do
2845
+ expect(hobrecht.reload.number).to eq(5)
2846
+ end
2847
+
2848
+ it "updates the last matching document" do
2849
+ expect(friedel.reload.number).to eq(5)
2850
+ end
2851
+
2852
+ it "does not update non matching docs" do
2853
+ expect(pfluger.reload.number).to be_nil
2854
+ end
2855
+ end
2856
+
2857
+ context "when updating the documents a second time" do
2858
+
2859
+ before do
2860
+ context.update_all(number: 5)
2861
+ end
2862
+
2863
+ it "does not error on the update" do
2864
+ expect(hobrecht.number).to eq(5)
2865
+ end
2866
+ end
2867
+ end
2868
+
2869
+ context "when no attributes are provided" do
2870
+
2871
+ it "returns false" do
2872
+ expect(context.update_all).to be false
2873
+ end
2874
+ end
2875
+ end
2876
+
2877
+ context "when the documents are embedded multiple levels" do
2878
+
2879
+ let!(:home) do
2880
+ hobrecht.locations.create!(name: "home")
2881
+ end
2882
+
2883
+ let!(:work) do
2884
+ hobrecht.locations.create!(name: "work")
2885
+ end
2886
+
2887
+ let(:criteria) do
2888
+ Location.where(name: "work").tap do |crit|
2889
+ crit.documents = [ home, work ]
2890
+ end
2891
+ end
2892
+
2893
+ let(:context) do
2894
+ described_class.new(criteria)
2895
+ end
2896
+
2897
+ context "when attributes are provided" do
2898
+
2899
+ before do
2900
+ context.update_all(number: 5)
2901
+ end
2902
+
2903
+ it "updates the first matching document" do
2904
+ expect(work.number).to eq(5)
2905
+ end
2906
+
2907
+ it "does not update non matching docs" do
2908
+ expect(home.number).to be_nil
2909
+ end
2910
+
2911
+ context "when reloading the embedded documents" do
2912
+
2913
+ it "updates the first matching document" do
2914
+ expect(work.reload.number).to eq(5)
2915
+ end
2916
+
2917
+ it "does not update non matching docs" do
2918
+ expect(home.reload.number).to be_nil
2919
+ end
2920
+ end
2921
+ end
2922
+
2923
+ context "when no attributes are provided" do
2924
+
2925
+ it "returns false" do
2926
+ expect(context.update_all).to be false
2927
+ end
2928
+ end
2929
+ end
2930
+ end
2931
+ end