mongoid 9.0.11 → 9.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (997) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -8
  3. data/README.md +5 -11
  4. data/Rakefile +35 -38
  5. data/lib/config/locales/en.yml +11 -0
  6. data/lib/mongoid/association/accessors.rb +19 -26
  7. data/lib/mongoid/association/bindable.rb +43 -45
  8. data/lib/mongoid/association/builders.rb +18 -15
  9. data/lib/mongoid/association/constrainable.rb +3 -5
  10. data/lib/mongoid/association/depending.rb +34 -38
  11. data/lib/mongoid/association/eager.rb +47 -13
  12. data/lib/mongoid/association/eager_loadable.rb +152 -24
  13. data/lib/mongoid/association/embedded/batchable.rb +28 -30
  14. data/lib/mongoid/association/embedded/cyclic.rb +8 -11
  15. data/lib/mongoid/association/embedded/eager.rb +1 -6
  16. data/lib/mongoid/association/embedded/embedded_in/binding.rb +6 -9
  17. data/lib/mongoid/association/embedded/embedded_in/buildable.rb +2 -3
  18. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +2 -0
  19. data/lib/mongoid/association/embedded/embedded_in.rb +25 -22
  20. data/lib/mongoid/association/embedded/embeds_many/binding.rb +0 -2
  21. data/lib/mongoid/association/embedded/embeds_many/buildable.rb +2 -3
  22. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +1 -1
  23. data/lib/mongoid/association/embedded/embeds_many.rb +24 -21
  24. data/lib/mongoid/association/embedded/embeds_one/binding.rb +0 -2
  25. data/lib/mongoid/association/embedded/embeds_one/buildable.rb +2 -6
  26. data/lib/mongoid/association/embedded/embeds_one.rb +20 -18
  27. data/lib/mongoid/association/embedded.rb +0 -1
  28. data/lib/mongoid/association/macros.rb +3 -7
  29. data/lib/mongoid/association/many.rb +75 -3
  30. data/lib/mongoid/association/marshalable.rb +0 -3
  31. data/lib/mongoid/association/nested/many.rb +18 -13
  32. data/lib/mongoid/association/nested/nested_buildable.rb +6 -9
  33. data/lib/mongoid/association/nested/one.rb +4 -7
  34. data/lib/mongoid/association/nested.rb +1 -3
  35. data/lib/mongoid/association/one.rb +0 -3
  36. data/lib/mongoid/association/options.rb +8 -7
  37. data/lib/mongoid/association/referenced/auto_save.rb +0 -2
  38. data/lib/mongoid/association/referenced/belongs_to/binding.rb +11 -15
  39. data/lib/mongoid/association/referenced/belongs_to/buildable.rb +23 -5
  40. data/lib/mongoid/association/referenced/belongs_to/eager.rb +1 -3
  41. data/lib/mongoid/association/referenced/belongs_to.rb +48 -43
  42. data/lib/mongoid/association/referenced/counter_cache.rb +5 -10
  43. data/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb +0 -2
  44. data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +8 -4
  45. data/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb +0 -3
  46. data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +52 -47
  47. data/lib/mongoid/association/referenced/has_many/binding.rb +0 -2
  48. data/lib/mongoid/association/referenced/has_many/buildable.rb +11 -5
  49. data/lib/mongoid/association/referenced/has_many/eager.rb +0 -3
  50. data/lib/mongoid/association/referenced/has_many/enumerable.rb +107 -38
  51. data/lib/mongoid/association/referenced/has_many/proxy.rb +10 -6
  52. data/lib/mongoid/association/referenced/has_many.rb +30 -24
  53. data/lib/mongoid/association/referenced/has_one/binding.rb +0 -2
  54. data/lib/mongoid/association/referenced/has_one/buildable.rb +11 -9
  55. data/lib/mongoid/association/referenced/has_one/eager.rb +0 -3
  56. data/lib/mongoid/association/referenced/has_one.rb +26 -21
  57. data/lib/mongoid/association/referenced/syncable.rb +30 -34
  58. data/lib/mongoid/association/referenced.rb +0 -1
  59. data/lib/mongoid/association/reflections.rb +0 -3
  60. data/lib/mongoid/association/relatable.rb +69 -66
  61. data/lib/mongoid/association.rb +12 -15
  62. data/lib/mongoid/atomic/modifiers.rb +65 -25
  63. data/lib/mongoid/atomic/paths/embedded/many.rb +4 -7
  64. data/lib/mongoid/atomic/paths/embedded/one.rb +2 -4
  65. data/lib/mongoid/atomic/paths/embedded.rb +3 -6
  66. data/lib/mongoid/atomic/paths/root.rb +1 -4
  67. data/lib/mongoid/atomic/paths.rb +2 -3
  68. data/lib/mongoid/atomic.rb +0 -3
  69. data/lib/mongoid/attributes/dynamic.rb +6 -7
  70. data/lib/mongoid/attributes/nested.rb +8 -10
  71. data/lib/mongoid/attributes/projector.rb +7 -20
  72. data/lib/mongoid/attributes/readonly.rb +6 -11
  73. data/lib/mongoid/attributes.rb +18 -34
  74. data/lib/mongoid/cacheable.rb +24 -6
  75. data/lib/mongoid/clients/factory.rb +12 -15
  76. data/lib/mongoid/clients/options.rb +2 -5
  77. data/lib/mongoid/clients/sessions.rb +50 -60
  78. data/lib/mongoid/clients/storage_options.rb +1 -4
  79. data/lib/mongoid/clients/validators/storage.rb +2 -3
  80. data/lib/mongoid/clients/validators.rb +1 -2
  81. data/lib/mongoid/clients.rb +8 -11
  82. data/lib/mongoid/collection_configurable.rb +10 -14
  83. data/lib/mongoid/composable.rb +30 -34
  84. data/lib/mongoid/config/defaults.rb +7 -4
  85. data/lib/mongoid/config/encryption.rb +1 -3
  86. data/lib/mongoid/config/environment.rb +7 -16
  87. data/lib/mongoid/config/introspection.rb +14 -16
  88. data/lib/mongoid/config/options.rb +16 -10
  89. data/lib/mongoid/config/validators/async_query_executor.rb +3 -6
  90. data/lib/mongoid/config/validators/client.rb +12 -15
  91. data/lib/mongoid/config/validators/option.rb +3 -5
  92. data/lib/mongoid/config/validators.rb +3 -4
  93. data/lib/mongoid/config.rb +132 -42
  94. data/lib/mongoid/contextual/aggregable/memory.rb +2 -4
  95. data/lib/mongoid/contextual/aggregable/mongo.rb +20 -22
  96. data/lib/mongoid/contextual/aggregable/none.rb +2 -4
  97. data/lib/mongoid/contextual/aggregable.rb +5 -7
  98. data/lib/mongoid/contextual/atomic.rb +20 -24
  99. data/lib/mongoid/contextual/command.rb +0 -3
  100. data/lib/mongoid/contextual/map_reduce.rb +12 -16
  101. data/lib/mongoid/contextual/memory.rb +72 -65
  102. data/lib/mongoid/contextual/mongo/documents_loader.rb +2 -3
  103. data/lib/mongoid/contextual/mongo.rb +169 -85
  104. data/lib/mongoid/contextual/none.rb +9 -9
  105. data/lib/mongoid/contextual/queryable.rb +1 -4
  106. data/lib/mongoid/contextual.rb +18 -7
  107. data/lib/mongoid/copyable.rb +7 -9
  108. data/lib/mongoid/criteria/findable.rb +5 -7
  109. data/lib/mongoid/criteria/includable.rb +37 -8
  110. data/lib/mongoid/criteria/inspectable.rb +2 -5
  111. data/lib/mongoid/criteria/marshalable.rb +4 -8
  112. data/lib/mongoid/criteria/modifiable.rb +3 -11
  113. data/lib/mongoid/criteria/options.rb +0 -3
  114. data/lib/mongoid/criteria/permission.rb +34 -37
  115. data/lib/mongoid/criteria/queryable/aggregable.rb +9 -10
  116. data/lib/mongoid/criteria/queryable/expandable.rb +4 -9
  117. data/lib/mongoid/criteria/queryable/extensions/array.rb +7 -10
  118. data/lib/mongoid/criteria/queryable/extensions/big_decimal.rb +3 -5
  119. data/lib/mongoid/criteria/queryable/extensions/boolean.rb +1 -4
  120. data/lib/mongoid/criteria/queryable/extensions/date.rb +2 -6
  121. data/lib/mongoid/criteria/queryable/extensions/date_time.rb +3 -7
  122. data/lib/mongoid/criteria/queryable/extensions/hash.rb +1 -4
  123. data/lib/mongoid/criteria/queryable/extensions/nil_class.rb +20 -11
  124. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +15 -11
  125. data/lib/mongoid/criteria/queryable/extensions/object.rb +7 -8
  126. data/lib/mongoid/criteria/queryable/extensions/range.rb +6 -9
  127. data/lib/mongoid/criteria/queryable/extensions/regexp.rb +10 -12
  128. data/lib/mongoid/criteria/queryable/extensions/set.rb +4 -6
  129. data/lib/mongoid/criteria/queryable/extensions/string.rb +5 -9
  130. data/lib/mongoid/criteria/queryable/extensions/symbol.rb +3 -7
  131. data/lib/mongoid/criteria/queryable/extensions/time.rb +2 -6
  132. data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +2 -12
  133. data/lib/mongoid/criteria/queryable/extensions.rb +18 -21
  134. data/lib/mongoid/criteria/queryable/key.rb +12 -18
  135. data/lib/mongoid/criteria/queryable/macroable.rb +0 -3
  136. data/lib/mongoid/criteria/queryable/mergeable.rb +31 -39
  137. data/lib/mongoid/criteria/queryable/optional.rb +17 -18
  138. data/lib/mongoid/criteria/queryable/options.rb +20 -9
  139. data/lib/mongoid/criteria/queryable/pipeline.rb +6 -9
  140. data/lib/mongoid/criteria/queryable/selectable.rb +131 -193
  141. data/lib/mongoid/criteria/queryable/selector.rb +20 -29
  142. data/lib/mongoid/criteria/queryable/smash.rb +1 -6
  143. data/lib/mongoid/criteria/queryable/storable.rb +20 -39
  144. data/lib/mongoid/criteria/queryable.rb +25 -15
  145. data/lib/mongoid/criteria/scopable.rb +5 -11
  146. data/lib/mongoid/criteria/translator.rb +5 -8
  147. data/lib/mongoid/criteria.rb +109 -54
  148. data/lib/mongoid/deprecable.rb +1 -4
  149. data/lib/mongoid/deprecation.rb +2 -6
  150. data/lib/mongoid/document.rb +1 -1
  151. data/lib/mongoid/encryptable.rb +1 -2
  152. data/lib/mongoid/equality.rb +4 -5
  153. data/lib/mongoid/errors/ambiguous_relationship.rb +2 -5
  154. data/lib/mongoid/errors/attribute_not_loaded.rb +1 -4
  155. data/lib/mongoid/errors/callback.rb +1 -4
  156. data/lib/mongoid/errors/create_collection_failure.rb +8 -11
  157. data/lib/mongoid/errors/criteria_argument_required.rb +2 -5
  158. data/lib/mongoid/errors/delete_restriction.rb +1 -4
  159. data/lib/mongoid/errors/document_not_destroyed.rb +1 -4
  160. data/lib/mongoid/errors/document_not_found.rb +16 -21
  161. data/lib/mongoid/errors/drop_collection_failure.rb +7 -10
  162. data/lib/mongoid/errors/empty_config_file.rb +1 -4
  163. data/lib/mongoid/errors/immutable_attribute.rb +1 -4
  164. data/lib/mongoid/errors/in_memory_collation_not_supported.rb +1 -4
  165. data/lib/mongoid/errors/invalid_async_query_executor.rb +2 -5
  166. data/lib/mongoid/errors/invalid_auto_encryption_configuration.rb +2 -5
  167. data/lib/mongoid/errors/invalid_collection.rb +1 -3
  168. data/lib/mongoid/errors/invalid_config_file.rb +1 -4
  169. data/lib/mongoid/errors/invalid_config_option.rb +2 -5
  170. data/lib/mongoid/errors/invalid_dependent_strategy.rb +6 -10
  171. data/lib/mongoid/errors/invalid_discriminator_key_target.rb +2 -4
  172. data/lib/mongoid/errors/invalid_dot_dollar_assignment.rb +1 -4
  173. data/lib/mongoid/errors/invalid_elem_match_operator.rb +5 -9
  174. data/lib/mongoid/errors/invalid_estimated_count_criteria.rb +1 -4
  175. data/lib/mongoid/errors/invalid_estimated_count_scoping.rb +1 -4
  176. data/lib/mongoid/errors/invalid_expression_operator.rb +3 -7
  177. data/lib/mongoid/errors/invalid_field.rb +2 -5
  178. data/lib/mongoid/errors/invalid_field_operator.rb +5 -9
  179. data/lib/mongoid/errors/invalid_field_option.rb +2 -5
  180. data/lib/mongoid/errors/invalid_field_type.rb +1 -4
  181. data/lib/mongoid/errors/invalid_find.rb +1 -4
  182. data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +1 -4
  183. data/lib/mongoid/errors/invalid_includes.rb +3 -6
  184. data/lib/mongoid/errors/invalid_index.rb +1 -4
  185. data/lib/mongoid/errors/invalid_options.rb +1 -4
  186. data/lib/mongoid/errors/invalid_path.rb +1 -4
  187. data/lib/mongoid/errors/invalid_persistence_option.rb +4 -7
  188. data/lib/mongoid/errors/invalid_query.rb +6 -11
  189. data/lib/mongoid/errors/invalid_relation.rb +10 -13
  190. data/lib/mongoid/errors/invalid_relation_option.rb +4 -7
  191. data/lib/mongoid/errors/invalid_scope.rb +1 -4
  192. data/lib/mongoid/errors/invalid_session_nesting.rb +0 -3
  193. data/lib/mongoid/errors/invalid_set_polymorphic_relation.rb +2 -4
  194. data/lib/mongoid/errors/invalid_storage_options.rb +1 -4
  195. data/lib/mongoid/errors/invalid_time.rb +1 -4
  196. data/lib/mongoid/errors/invalid_transaction_nesting.rb +0 -3
  197. data/lib/mongoid/errors/inverse_not_found.rb +1 -4
  198. data/lib/mongoid/errors/mixed_client_configuration.rb +1 -4
  199. data/lib/mongoid/errors/mixed_relations.rb +1 -3
  200. data/lib/mongoid/errors/mongoid_error.rb +7 -11
  201. data/lib/mongoid/errors/nested_attributes_metadata_not_found.rb +1 -4
  202. data/lib/mongoid/errors/no_client_config.rb +1 -4
  203. data/lib/mongoid/errors/no_client_database.rb +1 -4
  204. data/lib/mongoid/errors/no_client_hosts.rb +1 -4
  205. data/lib/mongoid/errors/no_clients_config.rb +1 -4
  206. data/lib/mongoid/errors/no_default_client.rb +1 -4
  207. data/lib/mongoid/errors/no_environment.rb +1 -4
  208. data/lib/mongoid/errors/no_map_reduce_output.rb +1 -4
  209. data/lib/mongoid/errors/no_metadata.rb +1 -4
  210. data/lib/mongoid/errors/no_parent.rb +1 -4
  211. data/lib/mongoid/errors/readonly_attribute.rb +1 -4
  212. data/lib/mongoid/errors/readonly_document.rb +1 -4
  213. data/lib/mongoid/errors/rollback.rb +0 -2
  214. data/lib/mongoid/errors/scope_overwrite.rb +2 -4
  215. data/lib/mongoid/errors/sessions_not_supported.rb +0 -3
  216. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +1 -3
  217. data/lib/mongoid/errors/transaction_error.rb +0 -3
  218. data/lib/mongoid/errors/transactions_not_supported.rb +0 -3
  219. data/lib/mongoid/errors/unknown_attribute.rb +1 -4
  220. data/lib/mongoid/errors/unknown_model.rb +1 -4
  221. data/lib/mongoid/errors/unregistered_class.rb +1 -1
  222. data/lib/mongoid/errors/unsaved_document.rb +1 -3
  223. data/lib/mongoid/errors/unsupported_isolation_level.rb +22 -0
  224. data/lib/mongoid/errors/unsupported_javascript.rb +1 -4
  225. data/lib/mongoid/errors/validations.rb +3 -5
  226. data/lib/mongoid/errors.rb +72 -72
  227. data/lib/mongoid/evolvable.rb +0 -3
  228. data/lib/mongoid/extensions/array.rb +5 -7
  229. data/lib/mongoid/extensions/big_decimal.rb +6 -7
  230. data/lib/mongoid/extensions/binary.rb +3 -6
  231. data/lib/mongoid/extensions/boolean.rb +4 -7
  232. data/lib/mongoid/extensions/bson_document.rb +28 -0
  233. data/lib/mongoid/extensions/date.rb +10 -14
  234. data/lib/mongoid/extensions/date_time.rb +2 -6
  235. data/lib/mongoid/extensions/decimal128.rb +1 -5
  236. data/lib/mongoid/extensions/false_class.rb +3 -5
  237. data/lib/mongoid/extensions/float.rb +5 -10
  238. data/lib/mongoid/extensions/hash.rb +5 -10
  239. data/lib/mongoid/extensions/integer.rb +5 -10
  240. data/lib/mongoid/extensions/module.rb +1 -4
  241. data/lib/mongoid/extensions/nil_class.rb +0 -1
  242. data/lib/mongoid/extensions/object.rb +9 -14
  243. data/lib/mongoid/extensions/object_id.rb +2 -6
  244. data/lib/mongoid/extensions/range.rb +14 -15
  245. data/lib/mongoid/extensions/raw_value.rb +0 -3
  246. data/lib/mongoid/extensions/regexp.rb +3 -6
  247. data/lib/mongoid/extensions/set.rb +3 -4
  248. data/lib/mongoid/extensions/string.rb +8 -12
  249. data/lib/mongoid/extensions/symbol.rb +3 -7
  250. data/lib/mongoid/extensions/time.rb +24 -26
  251. data/lib/mongoid/extensions/time_with_zone.rb +4 -7
  252. data/lib/mongoid/extensions/true_class.rb +3 -5
  253. data/lib/mongoid/extensions.rb +26 -26
  254. data/lib/mongoid/factory.rb +5 -9
  255. data/lib/mongoid/fields/encrypted.rb +0 -1
  256. data/lib/mongoid/fields/foreign_key.rb +9 -10
  257. data/lib/mongoid/fields/localized.rb +12 -12
  258. data/lib/mongoid/fields/standard.rb +5 -7
  259. data/lib/mongoid/fields/validators/macro.rb +23 -34
  260. data/lib/mongoid/fields/validators.rb +1 -2
  261. data/lib/mongoid/fields.rb +151 -58
  262. data/lib/mongoid/findable.rb +82 -53
  263. data/lib/mongoid/indexable/specification.rb +20 -6
  264. data/lib/mongoid/indexable/validators/options.rb +33 -40
  265. data/lib/mongoid/indexable.rb +26 -25
  266. data/lib/mongoid/inspectable.rb +4 -7
  267. data/lib/mongoid/interceptable.rb +41 -40
  268. data/lib/mongoid/loadable.rb +17 -18
  269. data/lib/mongoid/loggable.rb +4 -8
  270. data/lib/mongoid/matchable.rb +0 -2
  271. data/lib/mongoid/matcher/all.rb +2 -5
  272. data/lib/mongoid/matcher/and.rb +0 -3
  273. data/lib/mongoid/matcher/bits.rb +13 -8
  274. data/lib/mongoid/matcher/bits_all_clear.rb +1 -3
  275. data/lib/mongoid/matcher/bits_all_set.rb +1 -3
  276. data/lib/mongoid/matcher/bits_any_clear.rb +1 -3
  277. data/lib/mongoid/matcher/bits_any_set.rb +1 -3
  278. data/lib/mongoid/matcher/elem_match.rb +13 -15
  279. data/lib/mongoid/matcher/elem_match_expression.rb +0 -3
  280. data/lib/mongoid/matcher/eq.rb +0 -3
  281. data/lib/mongoid/matcher/eq_impl.rb +12 -17
  282. data/lib/mongoid/matcher/eq_impl_with_regexp.rb +4 -7
  283. data/lib/mongoid/matcher/exists.rb +1 -4
  284. data/lib/mongoid/matcher/expression.rb +5 -10
  285. data/lib/mongoid/matcher/expression_operator.rb +1 -3
  286. data/lib/mongoid/matcher/field_expression.rb +10 -13
  287. data/lib/mongoid/matcher/field_operator.rb +4 -6
  288. data/lib/mongoid/matcher/gt.rb +0 -3
  289. data/lib/mongoid/matcher/gte.rb +0 -3
  290. data/lib/mongoid/matcher/in.rb +9 -12
  291. data/lib/mongoid/matcher/lt.rb +0 -3
  292. data/lib/mongoid/matcher/lte.rb +0 -3
  293. data/lib/mongoid/matcher/mod.rb +4 -8
  294. data/lib/mongoid/matcher/ne.rb +0 -3
  295. data/lib/mongoid/matcher/nin.rb +0 -3
  296. data/lib/mongoid/matcher/nor.rb +1 -6
  297. data/lib/mongoid/matcher/not.rb +0 -3
  298. data/lib/mongoid/matcher/or.rb +0 -3
  299. data/lib/mongoid/matcher/regex.rb +11 -14
  300. data/lib/mongoid/matcher/size.rb +2 -5
  301. data/lib/mongoid/matcher/type.rb +32 -37
  302. data/lib/mongoid/matcher.rb +7 -15
  303. data/lib/mongoid/model_resolver.rb +1 -0
  304. data/lib/mongoid/persistable/creatable.rb +23 -7
  305. data/lib/mongoid/persistable/deletable.rb +6 -7
  306. data/lib/mongoid/persistable/destroyable.rb +2 -6
  307. data/lib/mongoid/persistable/incrementable.rb +1 -3
  308. data/lib/mongoid/persistable/logical.rb +3 -5
  309. data/lib/mongoid/persistable/maxable.rb +2 -4
  310. data/lib/mongoid/persistable/minable.rb +2 -4
  311. data/lib/mongoid/persistable/multipliable.rb +1 -3
  312. data/lib/mongoid/persistable/poppable.rb +2 -4
  313. data/lib/mongoid/persistable/pullable.rb +3 -5
  314. data/lib/mongoid/persistable/pushable.rb +5 -7
  315. data/lib/mongoid/persistable/renamable.rb +1 -3
  316. data/lib/mongoid/persistable/savable.rb +0 -3
  317. data/lib/mongoid/persistable/settable.rb +4 -13
  318. data/lib/mongoid/persistable/unsettable.rb +1 -3
  319. data/lib/mongoid/persistable/updatable.rb +16 -24
  320. data/lib/mongoid/persistable/upsertable.rb +8 -10
  321. data/lib/mongoid/persistable.rb +28 -27
  322. data/lib/mongoid/persistence_context.rb +34 -41
  323. data/lib/mongoid/pluckable.rb +0 -2
  324. data/lib/mongoid/positional.rb +5 -11
  325. data/lib/mongoid/railtie.rb +16 -22
  326. data/lib/mongoid/railties/console_sandbox.rb +6 -8
  327. data/lib/mongoid/railties/controller_runtime.rb +7 -11
  328. data/lib/mongoid/railties/database.rake +42 -44
  329. data/lib/mongoid/scopable.rb +23 -31
  330. data/lib/mongoid/search_indexable.rb +330 -0
  331. data/lib/mongoid/selectable.rb +1 -3
  332. data/lib/mongoid/serializable.rb +6 -8
  333. data/lib/mongoid/shardable.rb +8 -17
  334. data/lib/mongoid/stateful.rb +5 -10
  335. data/lib/mongoid/stringified_symbol.rb +5 -9
  336. data/lib/mongoid/tasks/database.rake +31 -32
  337. data/lib/mongoid/tasks/database.rb +28 -30
  338. data/lib/mongoid/tasks/encryption.rb +10 -15
  339. data/lib/mongoid/threaded/lifecycle.rb +6 -9
  340. data/lib/mongoid/threaded.rb +132 -32
  341. data/lib/mongoid/timestamps/created/short.rb +2 -3
  342. data/lib/mongoid/timestamps/created.rb +1 -2
  343. data/lib/mongoid/timestamps/short.rb +0 -1
  344. data/lib/mongoid/timestamps/timeless.rb +8 -13
  345. data/lib/mongoid/timestamps/updated/short.rb +2 -3
  346. data/lib/mongoid/timestamps/updated.rb +2 -5
  347. data/lib/mongoid/timestamps.rb +4 -6
  348. data/lib/mongoid/touchable.rb +19 -11
  349. data/lib/mongoid/traversable.rb +3 -2
  350. data/lib/mongoid/validatable/associated.rb +1 -5
  351. data/lib/mongoid/validatable/format.rb +0 -2
  352. data/lib/mongoid/validatable/length.rb +0 -2
  353. data/lib/mongoid/validatable/localizable.rb +0 -3
  354. data/lib/mongoid/validatable/macros.rb +0 -2
  355. data/lib/mongoid/validatable/numericality.rb +24 -3
  356. data/lib/mongoid/validatable/presence.rb +8 -10
  357. data/lib/mongoid/validatable/queryable.rb +0 -3
  358. data/lib/mongoid/validatable/uniqueness.rb +12 -14
  359. data/lib/mongoid/validatable.rb +15 -20
  360. data/lib/mongoid/version.rb +1 -1
  361. data/lib/mongoid/warnings.rb +10 -11
  362. data/lib/mongoid.rb +30 -34
  363. data/lib/rails/generators/mongoid/config/config_generator.rb +2 -4
  364. data/lib/rails/generators/mongoid/config/templates/mongoid.rb +4 -5
  365. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +2 -2
  366. data/lib/rails/generators/mongoid/model/model_generator.rb +5 -8
  367. data/lib/rails/generators/mongoid_generator.rb +6 -10
  368. data/lib/rails/mongoid.rb +1 -3
  369. data/spec/integration/active_job_spec.rb +1 -2
  370. data/spec/integration/app_spec.rb +142 -178
  371. data/spec/integration/associations/belongs_to_spec.rb +30 -28
  372. data/spec/integration/associations/embedded_dirty_spec.rb +0 -1
  373. data/spec/integration/associations/embedded_spec.rb +41 -43
  374. data/spec/integration/associations/embeds_many_spec.rb +46 -29
  375. data/spec/integration/associations/embeds_one_spec.rb +2 -1
  376. data/spec/integration/associations/foreign_key_spec.rb +10 -11
  377. data/spec/integration/associations/foreign_key_spec_models.rb +8 -9
  378. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +16 -13
  379. data/spec/integration/associations/has_many_spec.rb +17 -18
  380. data/spec/integration/associations/has_one_spec.rb +42 -45
  381. data/spec/integration/associations/nested_attributes_assignment_spec.rb +36 -39
  382. data/spec/integration/associations/reverse_population_spec.rb +4 -5
  383. data/spec/integration/associations/reverse_population_spec_models.rb +0 -1
  384. data/spec/integration/associations/scope_option_spec.rb +18 -20
  385. data/spec/integration/atomic/modifiers_spec.rb +72 -52
  386. data/spec/integration/bson_regexp_raw_spec.rb +2 -3
  387. data/spec/integration/caching_spec.rb +68 -0
  388. data/spec/integration/callbacks_models.rb +11 -9
  389. data/spec/integration/callbacks_spec.rb +78 -80
  390. data/spec/integration/contextual/empty_spec.rb +19 -20
  391. data/spec/integration/criteria/alias_query_spec.rb +12 -19
  392. data/spec/integration/criteria/date_field_spec.rb +1 -2
  393. data/spec/integration/criteria/default_scope_spec.rb +23 -18
  394. data/spec/integration/criteria/logical_spec.rb +18 -19
  395. data/spec/integration/criteria/range_spec.rb +77 -86
  396. data/spec/integration/criteria/raw_value_spec.rb +122 -109
  397. data/spec/integration/criteria/time_with_zone_spec.rb +33 -24
  398. data/spec/integration/discriminator_key_spec.rb +85 -85
  399. data/spec/integration/discriminator_value_spec.rb +70 -73
  400. data/spec/integration/document_spec.rb +9 -10
  401. data/spec/integration/dots_and_dollars_spec.rb +104 -119
  402. data/spec/integration/encryption_spec.rb +8 -11
  403. data/spec/integration/i18n_fallbacks_spec.rb +7 -8
  404. data/spec/integration/isolation_state_spec.rb +241 -0
  405. data/spec/integration/matcher_examples_spec.rb +145 -168
  406. data/spec/integration/matcher_operator_data/in.yml +6 -6
  407. data/spec/integration/matcher_operator_spec.rb +16 -26
  408. data/spec/integration/matcher_spec.rb +31 -32
  409. data/spec/integration/persistence/collection_options_spec.rb +0 -2
  410. data/spec/integration/persistence/range_field_spec.rb +24 -2
  411. data/spec/integration/server_query_spec.rb +43 -37
  412. data/spec/integration/shardable_spec.rb +10 -11
  413. data/spec/integration/stringified_symbol_field_spec.rb +28 -43
  414. data/spec/lite_spec_helper.rb +11 -12
  415. data/spec/mongoid/association/accessors_spec.rb +197 -285
  416. data/spec/mongoid/association/auto_save_spec.rb +62 -93
  417. data/spec/mongoid/association/builders_spec.rb +121 -74
  418. data/spec/mongoid/association/constrainable_spec.rb +9 -22
  419. data/spec/mongoid/association/counter_cache_spec.rb +71 -104
  420. data/spec/mongoid/association/depending_spec.rb +166 -208
  421. data/spec/mongoid/association/eager_spec.rb +416 -80
  422. data/spec/mongoid/association/embedded/cyclic_spec.rb +36 -48
  423. data/spec/mongoid/association/embedded/dirty_spec.rb +15 -20
  424. data/spec/mongoid/association/embedded/embedded_in/binding_spec.rb +36 -54
  425. data/spec/mongoid/association/embedded/embedded_in/buildable_spec.rb +8 -15
  426. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +135 -175
  427. data/spec/mongoid/association/embedded/embedded_in_spec.rb +25 -127
  428. data/spec/mongoid/association/embedded/embeds_many/binding_spec.rb +9 -14
  429. data/spec/mongoid/association/embedded/embeds_many/buildable_spec.rb +38 -51
  430. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +1151 -1340
  431. data/spec/mongoid/association/embedded/embeds_many_models.rb +17 -17
  432. data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +11 -11
  433. data/spec/mongoid/association/embedded/embeds_many_spec.rb +25 -138
  434. data/spec/mongoid/association/embedded/embeds_one/binding_spec.rb +15 -23
  435. data/spec/mongoid/association/embedded/embeds_one/buildable_spec.rb +18 -26
  436. data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +204 -273
  437. data/spec/mongoid/association/embedded/embeds_one_dnl_models.rb +0 -1
  438. data/spec/mongoid/association/embedded/embeds_one_models.rb +0 -1
  439. data/spec/mongoid/association/embedded/embeds_one_query_spec.rb +5 -6
  440. data/spec/mongoid/association/embedded/embeds_one_spec.rb +28 -143
  441. data/spec/mongoid/association/macros_spec.rb +213 -300
  442. data/spec/mongoid/association/nested/many_spec.rb +51 -73
  443. data/spec/mongoid/association/nested/one_spec.rb +56 -80
  444. data/spec/mongoid/association/options_spec.rb +187 -310
  445. data/spec/mongoid/association/polymorphic_spec.rb +35 -53
  446. data/spec/mongoid/association/referenced/belongs_to/binding_spec.rb +55 -76
  447. data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +36 -41
  448. data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +81 -101
  449. data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +258 -345
  450. data/spec/mongoid/association/referenced/belongs_to_models.rb +4 -3
  451. data/spec/mongoid/association/referenced/belongs_to_query_spec.rb +3 -4
  452. data/spec/mongoid/association/referenced/belongs_to_spec.rb +91 -294
  453. data/spec/mongoid/association/referenced/has_and_belongs_to_many/binding_spec.rb +42 -56
  454. data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +41 -32
  455. data/spec/mongoid/association/referenced/has_and_belongs_to_many/eager_spec.rb +36 -42
  456. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_persistence_spec.rb +8 -9
  457. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +771 -975
  458. data/spec/mongoid/association/referenced/has_and_belongs_to_many_models.rb +9 -9
  459. data/spec/mongoid/association/referenced/has_and_belongs_to_many_query_spec.rb +5 -6
  460. data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +59 -177
  461. data/spec/mongoid/association/referenced/has_many/binding_spec.rb +29 -43
  462. data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +53 -53
  463. data/spec/mongoid/association/referenced/has_many/eager_spec.rb +104 -67
  464. data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +431 -675
  465. data/spec/mongoid/association/referenced/has_many/proxy_query_spec.rb +1 -2
  466. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +173 -11
  467. data/spec/mongoid/association/referenced/has_many_models.rb +1 -2
  468. data/spec/mongoid/association/referenced/has_many_query_spec.rb +3 -4
  469. data/spec/mongoid/association/referenced/has_many_spec.rb +44 -188
  470. data/spec/mongoid/association/referenced/has_one/binding_spec.rb +26 -38
  471. data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +28 -29
  472. data/spec/mongoid/association/referenced/has_one/eager_spec.rb +29 -42
  473. data/spec/mongoid/association/referenced/has_one/proxy_spec.rb +270 -332
  474. data/spec/mongoid/association/referenced/has_one_models.rb +1 -2
  475. data/spec/mongoid/association/referenced/has_one_query_spec.rb +3 -4
  476. data/spec/mongoid/association/referenced/has_one_spec.rb +40 -188
  477. data/spec/mongoid/association/reflections_spec.rb +14 -24
  478. data/spec/mongoid/association/syncable_spec.rb +102 -125
  479. data/spec/mongoid/association_spec.rb +47 -70
  480. data/spec/mongoid/atomic/modifiers_spec.rb +184 -197
  481. data/spec/mongoid/atomic/paths/embedded/many_spec.rb +39 -52
  482. data/spec/mongoid/atomic/paths/embedded/one_spec.rb +35 -49
  483. data/spec/mongoid/atomic/paths/root_spec.rb +9 -15
  484. data/spec/mongoid/atomic/paths_spec.rb +81 -117
  485. data/spec/mongoid/atomic_spec.rb +156 -171
  486. data/spec/mongoid/attributes/dynamic_spec.rb +5 -6
  487. data/spec/mongoid/attributes/nested_spec.rb +1219 -1692
  488. data/spec/mongoid/attributes/nested_spec_models.rb +8 -9
  489. data/spec/mongoid/attributes/projector_spec.rb +2 -6
  490. data/spec/mongoid/attributes/readonly_spec.rb +73 -97
  491. data/spec/mongoid/attributes_spec.rb +669 -826
  492. data/spec/mongoid/cacheable_spec.rb +74 -44
  493. data/spec/mongoid/changeable_spec.rb +591 -733
  494. data/spec/mongoid/clients/factory_spec.rb +122 -154
  495. data/spec/mongoid/clients/options_spec.rb +40 -59
  496. data/spec/mongoid/clients/sessions_spec.rb +25 -44
  497. data/spec/mongoid/clients/transactions_spec.rb +33 -50
  498. data/spec/mongoid/clients/transactions_spec_models.rb +3 -4
  499. data/spec/mongoid/clients_spec.rb +265 -355
  500. data/spec/mongoid/collection_configurable_spec.rb +9 -12
  501. data/spec/mongoid/composable_spec.rb +2 -9
  502. data/spec/mongoid/config/defaults_spec.rb +24 -31
  503. data/spec/mongoid/config/encryption_spec.rb +54 -57
  504. data/spec/mongoid/config/environment_spec.rb +30 -40
  505. data/spec/mongoid/config/introspection_spec.rb +42 -37
  506. data/spec/mongoid/config/options_spec.rb +14 -21
  507. data/spec/mongoid/config_spec.rb +145 -195
  508. data/spec/mongoid/contextual/aggregable/memory_spec.rb +98 -135
  509. data/spec/mongoid/contextual/aggregable/memory_table_spec.rb +4 -7
  510. data/spec/mongoid/contextual/aggregable/mongo_spec.rb +149 -187
  511. data/spec/mongoid/contextual/aggregable/none_spec.rb +14 -16
  512. data/spec/mongoid/contextual/atomic_spec.rb +223 -271
  513. data/spec/mongoid/contextual/map_reduce_spec.rb +110 -138
  514. data/spec/mongoid/contextual/memory_spec.rb +706 -854
  515. data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +12 -13
  516. data/spec/mongoid/contextual/mongo_spec.rb +1008 -1213
  517. data/spec/mongoid/contextual/none_spec.rb +54 -59
  518. data/spec/mongoid/contextual/short_circuit_spec.rb +139 -0
  519. data/spec/mongoid/copyable_spec.rb +254 -300
  520. data/spec/mongoid/copyable_spec_models.rb +0 -1
  521. data/spec/mongoid/criteria/findable_spec.rb +322 -387
  522. data/spec/mongoid/criteria/includable_spec.rb +421 -372
  523. data/spec/mongoid/criteria/includable_spec_models.rb +16 -7
  524. data/spec/mongoid/criteria/inspectable_spec.rb +11 -14
  525. data/spec/mongoid/criteria/marshalable_spec.rb +11 -14
  526. data/spec/mongoid/criteria/modifiable_spec.rb +366 -512
  527. data/spec/mongoid/criteria/options_spec.rb +2 -7
  528. data/spec/mongoid/criteria/queryable/aggregable_spec.rb +112 -141
  529. data/spec/mongoid/criteria/queryable/expandable_spec.rb +8 -11
  530. data/spec/mongoid/criteria/queryable/extensions/array_spec.rb +127 -184
  531. data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +123 -60
  532. data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +84 -117
  533. data/spec/mongoid/criteria/queryable/extensions/date_spec.rb +44 -65
  534. data/spec/mongoid/criteria/queryable/extensions/date_time_spec.rb +86 -108
  535. data/spec/mongoid/criteria/queryable/extensions/float_spec.rb +94 -33
  536. data/spec/mongoid/criteria/queryable/extensions/hash_spec.rb +103 -138
  537. data/spec/mongoid/criteria/queryable/extensions/integer_spec.rb +95 -34
  538. data/spec/mongoid/criteria/queryable/extensions/nil_class_spec.rb +25 -38
  539. data/spec/mongoid/criteria/queryable/extensions/numeric_spec.rb +26 -28
  540. data/spec/mongoid/criteria/queryable/extensions/object_spec.rb +32 -54
  541. data/spec/mongoid/criteria/queryable/extensions/range_spec.rb +136 -130
  542. data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +16 -24
  543. data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +15 -24
  544. data/spec/mongoid/criteria/queryable/extensions/set_spec.rb +12 -19
  545. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +46 -63
  546. data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +28 -42
  547. data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +73 -96
  548. data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +71 -92
  549. data/spec/mongoid/criteria/queryable/key_spec.rb +32 -36
  550. data/spec/mongoid/criteria/queryable/mergeable_spec.rb +44 -49
  551. data/spec/mongoid/criteria/queryable/optional_spec.rb +373 -486
  552. data/spec/mongoid/criteria/queryable/options_spec.rb +172 -123
  553. data/spec/mongoid/criteria/queryable/pipeline_spec.rb +65 -87
  554. data/spec/mongoid/criteria/queryable/queryable_spec.rb +41 -53
  555. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +576 -680
  556. data/spec/mongoid/criteria/queryable/selectable_shared_examples.rb +7 -11
  557. data/spec/mongoid/criteria/queryable/selectable_spec.rb +807 -998
  558. data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +154 -191
  559. data/spec/mongoid/criteria/queryable/selector_spec.rb +294 -390
  560. data/spec/mongoid/criteria/queryable/smash_spec.rb +6 -11
  561. data/spec/mongoid/criteria/queryable/storable_spec.rb +54 -81
  562. data/spec/mongoid/criteria/queryable_mql_spec.rb +92 -0
  563. data/spec/mongoid/criteria/scopable_spec.rb +102 -138
  564. data/spec/mongoid/criteria/translator_spec.rb +59 -60
  565. data/spec/mongoid/criteria_projection_spec.rb +81 -99
  566. data/spec/mongoid/criteria_spec.rb +935 -950
  567. data/spec/mongoid/document_fields_spec.rb +28 -30
  568. data/spec/mongoid/document_persistence_context_spec.rb +13 -15
  569. data/spec/mongoid/document_query_spec.rb +9 -12
  570. data/spec/mongoid/document_spec.rb +317 -409
  571. data/spec/mongoid/equality_spec.rb +69 -102
  572. data/spec/mongoid/errors/ambiguous_relationship_spec.rb +8 -11
  573. data/spec/mongoid/errors/attribute_not_loaded_spec.rb +7 -10
  574. data/spec/mongoid/errors/callback_spec.rb +8 -11
  575. data/spec/mongoid/errors/delete_restriction_spec.rb +5 -8
  576. data/spec/mongoid/errors/document_not_destroyed_spec.rb +7 -10
  577. data/spec/mongoid/errors/document_not_found_spec.rb +51 -61
  578. data/spec/mongoid/errors/invalid_collection_spec.rb +9 -13
  579. data/spec/mongoid/errors/invalid_config_file_spec.rb +8 -11
  580. data/spec/mongoid/errors/invalid_config_option_spec.rb +8 -11
  581. data/spec/mongoid/errors/invalid_field_option_spec.rb +7 -10
  582. data/spec/mongoid/errors/invalid_field_spec.rb +10 -11
  583. data/spec/mongoid/errors/invalid_field_type_spec.rb +9 -12
  584. data/spec/mongoid/errors/invalid_find_spec.rb +8 -11
  585. data/spec/mongoid/errors/invalid_includes_spec.rb +9 -13
  586. data/spec/mongoid/errors/invalid_index_spec.rb +8 -11
  587. data/spec/mongoid/errors/invalid_options_spec.rb +8 -11
  588. data/spec/mongoid/errors/invalid_path_spec.rb +7 -10
  589. data/spec/mongoid/errors/invalid_relation_spec.rb +15 -16
  590. data/spec/mongoid/errors/invalid_scope_spec.rb +8 -11
  591. data/spec/mongoid/errors/invalid_set_polymorphic_relation_spec.rb +3 -6
  592. data/spec/mongoid/errors/invalid_storage_options_spec.rb +8 -11
  593. data/spec/mongoid/errors/invalid_time_spec.rb +8 -11
  594. data/spec/mongoid/errors/inverse_not_found_spec.rb +9 -12
  595. data/spec/mongoid/errors/mixed_client_configuration_spec.rb +9 -12
  596. data/spec/mongoid/errors/mixed_relations_spec.rb +8 -11
  597. data/spec/mongoid/errors/mongoid_error_spec.rb +21 -24
  598. data/spec/mongoid/errors/nested_attributes_metadata_not_found_spec.rb +6 -9
  599. data/spec/mongoid/errors/no_client_config_spec.rb +7 -10
  600. data/spec/mongoid/errors/no_client_database_spec.rb +9 -12
  601. data/spec/mongoid/errors/no_client_hosts_spec.rb +9 -12
  602. data/spec/mongoid/errors/no_clients_config_spec.rb +7 -10
  603. data/spec/mongoid/errors/no_environment_spec.rb +8 -11
  604. data/spec/mongoid/errors/no_map_reduce_output_spec.rb +8 -11
  605. data/spec/mongoid/errors/no_metadata_spec.rb +7 -10
  606. data/spec/mongoid/errors/no_parent_spec.rb +7 -10
  607. data/spec/mongoid/errors/readonly_attribute_spec.rb +7 -10
  608. data/spec/mongoid/errors/readonly_document_spec.rb +6 -9
  609. data/spec/mongoid/errors/scope_overwrite_spec.rb +9 -12
  610. data/spec/mongoid/errors/too_many_nested_attribute_records_spec.rb +9 -12
  611. data/spec/mongoid/errors/unknown_attribute_spec.rb +5 -8
  612. data/spec/mongoid/errors/unsaved_document_spec.rb +8 -11
  613. data/spec/mongoid/errors/unsupported_javascript_spec.rb +9 -12
  614. data/spec/mongoid/errors/validations_spec.rb +11 -14
  615. data/spec/mongoid/extensions/array_spec.rb +70 -92
  616. data/spec/mongoid/extensions/big_decimal_spec.rb +233 -313
  617. data/spec/mongoid/extensions/binary_spec.rb +21 -32
  618. data/spec/mongoid/extensions/boolean_spec.rb +52 -75
  619. data/spec/mongoid/extensions/date_class_mongoize_spec.rb +43 -54
  620. data/spec/mongoid/extensions/date_spec.rb +26 -38
  621. data/spec/mongoid/extensions/date_time_spec.rb +22 -32
  622. data/spec/mongoid/extensions/decimal128_spec.rb +12 -18
  623. data/spec/mongoid/extensions/false_class_spec.rb +10 -17
  624. data/spec/mongoid/extensions/float_spec.rb +40 -61
  625. data/spec/mongoid/extensions/hash_spec.rb +46 -64
  626. data/spec/mongoid/extensions/integer_spec.rb +40 -61
  627. data/spec/mongoid/extensions/module_spec.rb +13 -18
  628. data/spec/mongoid/extensions/nil_class_spec.rb +2 -5
  629. data/spec/mongoid/extensions/object_id_spec.rb +159 -247
  630. data/spec/mongoid/extensions/object_spec.rb +31 -47
  631. data/spec/mongoid/extensions/range_spec.rb +112 -115
  632. data/spec/mongoid/extensions/raw_value_spec.rb +10 -12
  633. data/spec/mongoid/extensions/regexp_spec.rb +23 -36
  634. data/spec/mongoid/extensions/set_spec.rb +38 -49
  635. data/spec/mongoid/extensions/string_spec.rb +118 -167
  636. data/spec/mongoid/extensions/stringified_symbol_spec.rb +29 -43
  637. data/spec/mongoid/extensions/symbol_spec.rb +12 -20
  638. data/spec/mongoid/extensions/time_spec.rb +160 -202
  639. data/spec/mongoid/extensions/time_with_zone_spec.rb +79 -105
  640. data/spec/mongoid/extensions/true_class_spec.rb +10 -17
  641. data/spec/mongoid/factory_spec.rb +125 -148
  642. data/spec/mongoid/fields/foreign_key_spec.rb +124 -169
  643. data/spec/mongoid/fields/localized_spec.rb +140 -185
  644. data/spec/mongoid/fields/standard_spec.rb +43 -59
  645. data/spec/mongoid/fields_spec.rb +751 -620
  646. data/spec/mongoid/findable_spec.rb +260 -228
  647. data/spec/mongoid/indexable/specification_spec.rb +42 -58
  648. data/spec/mongoid/indexable_spec.rb +260 -264
  649. data/spec/mongoid/inspectable_spec.rb +45 -55
  650. data/spec/mongoid/interceptable_spec.rb +784 -922
  651. data/spec/mongoid/interceptable_spec_models.rb +35 -35
  652. data/spec/mongoid/loading_spec.rb +47 -43
  653. data/spec/mongoid/loggable_spec.rb +3 -6
  654. data/spec/mongoid/matcher/extract_attribute_spec.rb +2 -6
  655. data/spec/mongoid/mongoizable_spec.rb +81 -90
  656. data/spec/mongoid/monkey_patches_spec.rb +4 -0
  657. data/spec/mongoid/persistable/creatable_spec.rb +181 -220
  658. data/spec/mongoid/persistable/deletable_spec.rb +139 -179
  659. data/spec/mongoid/persistable/destroyable_spec.rb +135 -164
  660. data/spec/mongoid/persistable/incrementable_spec.rb +65 -84
  661. data/spec/mongoid/persistable/logical_spec.rb +48 -61
  662. data/spec/mongoid/persistable/maxable_spec.rb +45 -44
  663. data/spec/mongoid/persistable/minable_spec.rb +45 -44
  664. data/spec/mongoid/persistable/multipliable_spec.rb +54 -69
  665. data/spec/mongoid/persistable/poppable_spec.rb +47 -60
  666. data/spec/mongoid/persistable/pullable_spec.rb +79 -103
  667. data/spec/mongoid/persistable/pushable_spec.rb +106 -131
  668. data/spec/mongoid/persistable/renamable_spec.rb +52 -65
  669. data/spec/mongoid/persistable/savable_spec.rb +152 -189
  670. data/spec/mongoid/persistable/settable_spec.rb +121 -145
  671. data/spec/mongoid/persistable/unsettable_spec.rb +56 -73
  672. data/spec/mongoid/persistable/updatable_spec.rb +185 -232
  673. data/spec/mongoid/persistable/upsertable_spec.rb +66 -77
  674. data/spec/mongoid/persistable_spec.rb +132 -139
  675. data/spec/mongoid/persistence_context_spec.rb +31 -89
  676. data/spec/mongoid/positional_spec.rb +67 -79
  677. data/spec/mongoid/railties/bson_object_id_serializer_spec.rb +0 -3
  678. data/spec/mongoid/railties/console_sandbox_spec.rb +0 -1
  679. data/spec/mongoid/relations/proxy_spec.rb +0 -1
  680. data/spec/mongoid/reloadable_spec.rb +250 -145
  681. data/spec/mongoid/scopable_spec.rb +319 -398
  682. data/spec/mongoid/search_indexable_spec.rb +446 -41
  683. data/spec/mongoid/selectable_spec.rb +36 -51
  684. data/spec/mongoid/serializable_spec.rb +269 -312
  685. data/spec/mongoid/shardable_models.rb +5 -5
  686. data/spec/mongoid/shardable_spec.rb +44 -46
  687. data/spec/mongoid/stateful_spec.rb +55 -75
  688. data/spec/mongoid/tasks/database_rake_spec.rb +138 -141
  689. data/spec/mongoid/tasks/database_spec.rb +80 -80
  690. data/spec/mongoid/tasks/encryption_spec.rb +22 -20
  691. data/spec/mongoid/threaded_spec.rb +57 -83
  692. data/spec/mongoid/timestamps/created/short_spec.rb +12 -16
  693. data/spec/mongoid/timestamps/created_spec.rb +16 -20
  694. data/spec/mongoid/timestamps/timeless_spec.rb +31 -43
  695. data/spec/mongoid/timestamps/updated/short_spec.rb +21 -28
  696. data/spec/mongoid/timestamps/updated_spec.rb +18 -25
  697. data/spec/mongoid/timestamps_spec.rb +171 -188
  698. data/spec/mongoid/timestamps_spec_models.rb +12 -13
  699. data/spec/mongoid/touchable_spec.rb +330 -294
  700. data/spec/mongoid/touchable_spec_models.rb +35 -37
  701. data/spec/mongoid/traversable_spec.rb +435 -471
  702. data/spec/mongoid/validatable/associated_spec.rb +42 -60
  703. data/spec/mongoid/validatable/format_spec.rb +24 -34
  704. data/spec/mongoid/validatable/length_spec.rb +62 -86
  705. data/spec/mongoid/validatable/numericality_spec.rb +114 -23
  706. data/spec/mongoid/validatable/presence_spec.rb +149 -203
  707. data/spec/mongoid/validatable/uniqueness_spec.rb +632 -830
  708. data/spec/mongoid/validatable_spec.rb +71 -103
  709. data/spec/mongoid/warnings_spec.rb +5 -9
  710. data/spec/mongoid_spec.rb +25 -41
  711. data/spec/rails/controller_extension/controller_runtime_spec.rb +45 -49
  712. data/spec/rails/mongoid_spec.rb +15 -15
  713. data/spec/shared/lib/mrss/cluster_config.rb +5 -40
  714. data/spec/shared/lib/mrss/constraints.rb +7 -46
  715. data/spec/shared/lib/mrss/docker_runner.rb +10 -21
  716. data/spec/shared/lib/mrss/eg_config_utils.rb +0 -31
  717. data/spec/shared/lib/mrss/release/candidate.rb +18 -12
  718. data/spec/shared/lib/mrss/server_version_registry.rb +1 -5
  719. data/spec/shared/share/Dockerfile.erb +6 -8
  720. data/spec/shared/shlib/server.sh +0 -50
  721. data/spec/shared/shlib/set_env.sh +4 -10
  722. data/spec/spec_helper.rb +54 -31
  723. data/spec/support/authorization.rb +0 -1
  724. data/spec/support/client_registry.rb +1 -2
  725. data/spec/support/constraints.rb +8 -25
  726. data/spec/support/crypt/models.rb +7 -8
  727. data/spec/support/crypt.rb +6 -9
  728. data/spec/support/expectations.rb +21 -7
  729. data/spec/support/feature_sandbox.rb +1 -4
  730. data/spec/support/helpers.rb +0 -1
  731. data/spec/support/immutable_ids.rb +20 -21
  732. data/spec/support/macros.rb +1 -3
  733. data/spec/support/models/account.rb +4 -5
  734. data/spec/support/models/acolyte.rb +5 -5
  735. data/spec/support/models/actor.rb +3 -3
  736. data/spec/support/models/actress.rb +0 -1
  737. data/spec/support/models/address.rb +11 -11
  738. data/spec/support/models/address_component.rb +1 -1
  739. data/spec/support/models/address_number.rb +1 -1
  740. data/spec/support/models/agency.rb +1 -1
  741. data/spec/support/models/agent.rb +1 -1
  742. data/spec/support/models/album.rb +1 -2
  743. data/spec/support/models/alert.rb +1 -1
  744. data/spec/support/models/animal.rb +4 -5
  745. data/spec/support/models/answer.rb +1 -1
  746. data/spec/support/models/appointment.rb +2 -2
  747. data/spec/support/models/armrest.rb +0 -1
  748. data/spec/support/models/array_field.rb +0 -1
  749. data/spec/support/models/article.rb +0 -1
  750. data/spec/support/models/artist.rb +16 -12
  751. data/spec/support/models/artwork.rb +1 -1
  752. data/spec/support/models/audible_sound.rb +0 -1
  753. data/spec/support/models/audio.rb +2 -2
  754. data/spec/support/models/augmentation.rb +1 -2
  755. data/spec/support/models/author.rb +1 -1
  756. data/spec/support/models/baby.rb +1 -1
  757. data/spec/support/models/band.rb +0 -1
  758. data/spec/support/models/bar.rb +2 -2
  759. data/spec/support/models/basic.rb +0 -1
  760. data/spec/support/models/bed.rb +0 -1
  761. data/spec/support/models/big_palette.rb +0 -1
  762. data/spec/support/models/birthday.rb +3 -5
  763. data/spec/support/models/bolt.rb +0 -1
  764. data/spec/support/models/bomb.rb +1 -1
  765. data/spec/support/models/book.rb +1 -1
  766. data/spec/support/models/breed.rb +1 -1
  767. data/spec/support/models/browser.rb +1 -2
  768. data/spec/support/models/building.rb +0 -1
  769. data/spec/support/models/building_address.rb +1 -1
  770. data/spec/support/models/bus.rb +1 -1
  771. data/spec/support/models/business.rb +2 -2
  772. data/spec/support/models/callback_test.rb +1 -1
  773. data/spec/support/models/canvas.rb +5 -6
  774. data/spec/support/models/car.rb +0 -1
  775. data/spec/support/models/cat.rb +0 -2
  776. data/spec/support/models/catalog.rb +0 -1
  777. data/spec/support/models/category.rb +1 -1
  778. data/spec/support/models/child.rb +1 -1
  779. data/spec/support/models/child_doc.rb +5 -6
  780. data/spec/support/models/church.rb +1 -1
  781. data/spec/support/models/circle.rb +0 -1
  782. data/spec/support/models/circuit.rb +2 -2
  783. data/spec/support/models/circus.rb +0 -1
  784. data/spec/support/models/code.rb +2 -1
  785. data/spec/support/models/coding/pull_request.rb +0 -1
  786. data/spec/support/models/coding.rb +0 -1
  787. data/spec/support/models/comment.rb +0 -1
  788. data/spec/support/models/company.rb +0 -1
  789. data/spec/support/models/consumption_period.rb +0 -1
  790. data/spec/support/models/contextable_item.rb +1 -1
  791. data/spec/support/models/contractor.rb +1 -1
  792. data/spec/support/models/cookie.rb +0 -1
  793. data/spec/support/models/country_code.rb +2 -3
  794. data/spec/support/models/courier_job.rb +2 -2
  795. data/spec/support/models/cover.rb +0 -1
  796. data/spec/support/models/crate.rb +0 -1
  797. data/spec/support/models/customer.rb +0 -1
  798. data/spec/support/models/customer_address.rb +0 -1
  799. data/spec/support/models/deed.rb +1 -1
  800. data/spec/support/models/definition.rb +1 -1
  801. data/spec/support/models/delegating_patient.rb +0 -1
  802. data/spec/support/models/description.rb +0 -1
  803. data/spec/support/models/dictionary.rb +1 -1
  804. data/spec/support/models/division.rb +2 -2
  805. data/spec/support/models/doctor.rb +0 -1
  806. data/spec/support/models/dog.rb +2 -2
  807. data/spec/support/models/dokument.rb +1 -1
  808. data/spec/support/models/draft.rb +0 -1
  809. data/spec/support/models/dragon.rb +1 -1
  810. data/spec/support/models/driver.rb +2 -2
  811. data/spec/support/models/drug.rb +1 -1
  812. data/spec/support/models/dungeon.rb +1 -1
  813. data/spec/support/models/edit.rb +1 -1
  814. data/spec/support/models/email.rb +1 -1
  815. data/spec/support/models/employer.rb +1 -2
  816. data/spec/support/models/entry.rb +1 -1
  817. data/spec/support/models/eraser.rb +0 -1
  818. data/spec/support/models/even.rb +1 -1
  819. data/spec/support/models/event.rb +3 -4
  820. data/spec/support/models/exhibition.rb +1 -1
  821. data/spec/support/models/exhibitor.rb +1 -1
  822. data/spec/support/models/explosion.rb +1 -1
  823. data/spec/support/models/eye.rb +0 -1
  824. data/spec/support/models/eye_bowl.rb +2 -3
  825. data/spec/support/models/face.rb +2 -3
  826. data/spec/support/models/fanatic.rb +1 -1
  827. data/spec/support/models/favorite.rb +1 -1
  828. data/spec/support/models/filesystem.rb +1 -1
  829. data/spec/support/models/fire_hydrant.rb +1 -1
  830. data/spec/support/models/firefox.rb +0 -1
  831. data/spec/support/models/fish.rb +0 -1
  832. data/spec/support/models/folder.rb +0 -2
  833. data/spec/support/models/folder_item.rb +1 -3
  834. data/spec/support/models/fruits.rb +9 -5
  835. data/spec/support/models/game.rb +3 -4
  836. data/spec/support/models/ghost.rb +0 -1
  837. data/spec/support/models/guitar.rb +0 -1
  838. data/spec/support/models/hole.rb +0 -1
  839. data/spec/support/models/home.rb +1 -1
  840. data/spec/support/models/house.rb +2 -2
  841. data/spec/support/models/html_writer.rb +0 -1
  842. data/spec/support/models/id_key.rb +3 -3
  843. data/spec/support/models/idnodef.rb +0 -1
  844. data/spec/support/models/image.rb +0 -1
  845. data/spec/support/models/implant.rb +1 -2
  846. data/spec/support/models/instrument.rb +2 -3
  847. data/spec/support/models/item.rb +2 -2
  848. data/spec/support/models/jar.rb +1 -2
  849. data/spec/support/models/kaleidoscope.rb +1 -1
  850. data/spec/support/models/kangaroo.rb +1 -1
  851. data/spec/support/models/label.rb +2 -2
  852. data/spec/support/models/language.rb +1 -1
  853. data/spec/support/models/lat_lng.rb +0 -1
  854. data/spec/support/models/league.rb +1 -1
  855. data/spec/support/models/learner.rb +0 -1
  856. data/spec/support/models/line_item.rb +1 -1
  857. data/spec/support/models/location.rb +1 -1
  858. data/spec/support/models/login.rb +1 -2
  859. data/spec/support/models/manufacturer.rb +0 -1
  860. data/spec/support/models/meat.rb +1 -1
  861. data/spec/support/models/membership.rb +1 -1
  862. data/spec/support/models/message.rb +1 -2
  863. data/spec/support/models/minim.rb +0 -1
  864. data/spec/support/models/mixed_drink.rb +1 -1
  865. data/spec/support/models/modifier_models.rb +25 -0
  866. data/spec/support/models/mop.rb +0 -1
  867. data/spec/support/models/movie.rb +1 -1
  868. data/spec/support/models/my_hash.rb +1 -2
  869. data/spec/support/models/name.rb +4 -5
  870. data/spec/support/models/name_only.rb +0 -1
  871. data/spec/support/models/node.rb +1 -1
  872. data/spec/support/models/note.rb +3 -3
  873. data/spec/support/models/nut.rb +0 -1
  874. data/spec/support/models/odd.rb +1 -1
  875. data/spec/support/models/order.rb +1 -1
  876. data/spec/support/models/ordered_post.rb +1 -1
  877. data/spec/support/models/ordered_preference.rb +1 -1
  878. data/spec/support/models/oscar.rb +1 -1
  879. data/spec/support/models/other_owner_object.rb +0 -1
  880. data/spec/support/models/override.rb +3 -11
  881. data/spec/support/models/ownable.rb +1 -1
  882. data/spec/support/models/owner.rb +1 -1
  883. data/spec/support/models/pack.rb +0 -1
  884. data/spec/support/models/page.rb +3 -3
  885. data/spec/support/models/page_question.rb +1 -1
  886. data/spec/support/models/palette.rb +2 -2
  887. data/spec/support/models/parent.rb +2 -2
  888. data/spec/support/models/parent_doc.rb +1 -1
  889. data/spec/support/models/passport.rb +0 -1
  890. data/spec/support/models/patient.rb +2 -2
  891. data/spec/support/models/pdf_writer.rb +0 -1
  892. data/spec/support/models/pencil.rb +0 -1
  893. data/spec/support/models/person.rb +32 -28
  894. data/spec/support/models/pet.rb +1 -1
  895. data/spec/support/models/pet_owner.rb +1 -1
  896. data/spec/support/models/phone.rb +1 -2
  897. data/spec/support/models/piano.rb +0 -1
  898. data/spec/support/models/pizza.rb +1 -1
  899. data/spec/support/models/player.rb +5 -5
  900. data/spec/support/models/post.rb +12 -12
  901. data/spec/support/models/post_genre.rb +1 -1
  902. data/spec/support/models/powerup.rb +1 -2
  903. data/spec/support/models/preference.rb +2 -2
  904. data/spec/support/models/princess.rb +1 -1
  905. data/spec/support/models/product.rb +3 -3
  906. data/spec/support/models/profile.rb +2 -1
  907. data/spec/support/models/pronunciation.rb +1 -1
  908. data/spec/support/models/pub.rb +2 -2
  909. data/spec/support/models/publication/encyclopedia.rb +0 -1
  910. data/spec/support/models/publication/review.rb +0 -1
  911. data/spec/support/models/publication.rb +0 -1
  912. data/spec/support/models/purchase.rb +1 -1
  913. data/spec/support/models/purchased_item.rb +1 -1
  914. data/spec/support/models/question.rb +2 -2
  915. data/spec/support/models/quiz.rb +1 -1
  916. data/spec/support/models/rating.rb +1 -1
  917. data/spec/support/models/record.rb +2 -3
  918. data/spec/support/models/registry.rb +1 -1
  919. data/spec/support/models/role.rb +1 -1
  920. data/spec/support/models/root_category.rb +1 -1
  921. data/spec/support/models/sandbox/app/models/app_models_message.rb +0 -1
  922. data/spec/support/models/sandbox/lib/models/lib_models_message.rb +0 -1
  923. data/spec/support/models/sandbox/sandbox_message.rb +0 -1
  924. data/spec/support/models/sandbox/sandbox_user.rb +0 -1
  925. data/spec/support/models/sandbox/subdir/sandbox_comment.rb +0 -1
  926. data/spec/support/models/sandwich.rb +1 -1
  927. data/spec/support/models/scheduler.rb +0 -1
  928. data/spec/support/models/school.rb +0 -1
  929. data/spec/support/models/scribe.rb +1 -1
  930. data/spec/support/models/sealer.rb +0 -1
  931. data/spec/support/models/seat.rb +0 -1
  932. data/spec/support/models/seo.rb +1 -1
  933. data/spec/support/models/series.rb +1 -1
  934. data/spec/support/models/server.rb +1 -1
  935. data/spec/support/models/service.rb +2 -2
  936. data/spec/support/models/shape.rb +3 -3
  937. data/spec/support/models/shelf.rb +1 -1
  938. data/spec/support/models/shield.rb +2 -2
  939. data/spec/support/models/shipment_address.rb +0 -1
  940. data/spec/support/models/shipping_container.rb +1 -1
  941. data/spec/support/models/shipping_pack.rb +0 -1
  942. data/spec/support/models/shirt.rb +0 -1
  943. data/spec/support/models/shop.rb +2 -2
  944. data/spec/support/models/short_agent.rb +0 -1
  945. data/spec/support/models/short_quiz.rb +1 -1
  946. data/spec/support/models/simple.rb +1 -1
  947. data/spec/support/models/slave.rb +1 -1
  948. data/spec/support/models/song.rb +1 -2
  949. data/spec/support/models/sound.rb +2 -2
  950. data/spec/support/models/spacer.rb +0 -1
  951. data/spec/support/models/square.rb +0 -1
  952. data/spec/support/models/staff.rb +0 -1
  953. data/spec/support/models/store_as_dup_test1.rb +2 -2
  954. data/spec/support/models/store_as_dup_test2.rb +1 -1
  955. data/spec/support/models/store_as_dup_test3.rb +2 -2
  956. data/spec/support/models/store_as_dup_test4.rb +1 -1
  957. data/spec/support/models/strategy.rb +0 -1
  958. data/spec/support/models/student.rb +1 -2
  959. data/spec/support/models/sub_item.rb +0 -1
  960. data/spec/support/models/subscription.rb +2 -2
  961. data/spec/support/models/survey.rb +2 -2
  962. data/spec/support/models/symptom.rb +2 -2
  963. data/spec/support/models/system_role.rb +2 -3
  964. data/spec/support/models/tag.rb +2 -2
  965. data/spec/support/models/target.rb +1 -1
  966. data/spec/support/models/template.rb +1 -1
  967. data/spec/support/models/thing.rb +1 -1
  968. data/spec/support/models/threadlocker.rb +0 -1
  969. data/spec/support/models/title.rb +0 -1
  970. data/spec/support/models/tool.rb +3 -3
  971. data/spec/support/models/topping.rb +1 -1
  972. data/spec/support/models/toy.rb +0 -1
  973. data/spec/support/models/track.rb +1 -1
  974. data/spec/support/models/translation.rb +1 -1
  975. data/spec/support/models/tree.rb +2 -3
  976. data/spec/support/models/truck.rb +0 -1
  977. data/spec/support/models/updatable.rb +0 -1
  978. data/spec/support/models/user.rb +3 -4
  979. data/spec/support/models/user_account.rb +3 -3
  980. data/spec/support/models/validation_callback.rb +4 -4
  981. data/spec/support/models/vehicle.rb +2 -3
  982. data/spec/support/models/version.rb +1 -1
  983. data/spec/support/models/vertex.rb +0 -1
  984. data/spec/support/models/vet_visit.rb +1 -1
  985. data/spec/support/models/video.rb +2 -2
  986. data/spec/support/models/video_game.rb +0 -1
  987. data/spec/support/models/washer.rb +0 -1
  988. data/spec/support/models/weapon.rb +1 -2
  989. data/spec/support/models/wiki_page.rb +2 -3
  990. data/spec/support/models/word.rb +2 -2
  991. data/spec/support/models/word_origin.rb +1 -2
  992. data/spec/support/models/writer.rb +3 -3
  993. data/spec/support/rails_mock.rb +7 -12
  994. data/spec/support/shared/time.rb +0 -1
  995. data/spec/support/sinatra_mock.rb +4 -2
  996. data/spec/support/spec_config.rb +19 -18
  997. metadata +14 -2
@@ -1,81 +1,72 @@
1
1
  # frozen_string_literal: true
2
- # rubocop:todo all
3
2
 
4
- require "spec_helper"
3
+ require 'spec_helper'
5
4
 
6
5
  describe Mongoid::Fields do
7
-
8
6
  describe "#\{field}_translations" do
9
-
10
7
  let(:product) do
11
8
  Product.new
12
9
  end
13
10
 
14
- context "when the field is localized" do
15
-
16
- context "when translations exist" do
11
+ context 'when the field is localized' do
12
+ context 'when translations exist' do
17
13
  with_default_i18n_configs
18
14
 
19
15
  before do
20
16
  I18n.locale = :en
21
- product.description = "test"
17
+ product.description = 'test'
22
18
  I18n.locale = :de
23
- product.description = "The best"
19
+ product.description = 'The best'
24
20
  end
25
21
 
26
22
  let(:translations) do
27
23
  product.description_translations
28
24
  end
29
25
 
30
- it "returns all the translations" do
26
+ it 'returns all the translations' do
31
27
  expect(translations).to eq(
32
- { "en" => "test", "de" => "The best" }
28
+ { 'en' => 'test', 'de' => 'The best' }
33
29
  )
34
30
  end
35
31
 
36
- it "returns translations as a HashWithIndifferentAccess" do
37
- expect(translations[:en]).to eq("test")
32
+ it 'returns translations as a HashWithIndifferentAccess' do
33
+ expect(translations[:en]).to eq('test')
38
34
  end
39
35
  end
40
36
 
41
- context "when translations do not exist" do
42
-
43
- context "when no default is provided" do
44
-
45
- it "returns an empty hash" do
37
+ context 'when translations do not exist' do
38
+ context 'when no default is provided' do
39
+ it 'returns an empty hash' do
46
40
  expect(product.description_translations).to be_empty
47
41
  end
48
42
  end
49
43
 
50
- context "when a default is provided" do
51
-
52
- it "returns the translations with the default" do
44
+ context 'when a default is provided' do
45
+ it 'returns the translations with the default' do
53
46
  expect(product.name_translations).to eq(
54
- { "en" => "no translation" }
47
+ { 'en' => 'no translation' }
55
48
  )
56
49
  end
57
50
  end
58
51
  end
59
52
 
60
- it "should have alias method #\{field}_t" do
53
+ it 'has alias method <field>_t' do
61
54
  expect(product.method(:name_t)).to eq product.method(:name_translations)
62
55
  end
63
56
  end
64
57
 
65
- context "when the field is not localized" do
66
-
67
- it "does not respond to the method" do
68
- expect(product).to_not respond_to(:price_translations)
58
+ context 'when the field is not localized' do
59
+ it 'does not respond to the method' do
60
+ expect(product).not_to respond_to(:price_translations)
69
61
  end
70
62
 
71
- it "does not respond to the alias method" do
72
- expect(product).to_not respond_to(:price_t)
63
+ it 'does not respond to the alias method' do
64
+ expect(product).not_to respond_to(:price_t)
73
65
  end
74
66
  end
75
67
  end
76
68
 
77
69
  describe "#\{field}_translations=" do
78
-
79
70
  let(:product) do
80
71
  Product.new
81
72
  end
@@ -84,310 +75,291 @@ describe Mongoid::Fields do
84
75
  Dictionary.new
85
76
  end
86
77
 
87
- context "when the field is localized" do
88
-
89
- context "when the field does not require mongoizations" do
90
-
78
+ context 'when the field is localized' do
79
+ context 'when the field does not require mongoizations' do
91
80
  let(:translations) do
92
- { "en" => "test", "de" => "testing" }
81
+ { 'en' => 'test', 'de' => 'testing' }
93
82
  end
94
83
 
95
84
  before do
96
85
  product.description_translations = translations
97
86
  end
98
87
 
99
- it "sets the raw values of the translations" do
88
+ it 'sets the raw values of the translations' do
100
89
  expect(product.description_translations).to eq(translations)
101
90
  end
102
91
 
103
- context "when saving the new translations" do
104
-
92
+ context 'when saving the new translations' do
105
93
  before do
106
94
  product.save!
107
95
  end
108
96
 
109
- it "persists the changes" do
97
+ it 'persists the changes' do
110
98
  expect(product.reload.description_translations).to eq(translations)
111
99
  end
112
100
 
113
- context "when updating the translations" do
114
-
101
+ context 'when updating the translations' do
115
102
  before do
116
- product.description_translations = { "en" => "overwritten" }
103
+ product.description_translations = { 'en' => 'overwritten' }
117
104
  product.save!
118
105
  end
119
106
 
120
- it "persists the changes" do
107
+ it 'persists the changes' do
121
108
  expect(product.reload.description_translations).to eq(
122
- { "en" => "overwritten" }
109
+ { 'en' => 'overwritten' }
123
110
  )
124
111
  end
125
112
  end
126
113
  end
127
114
  end
128
115
 
129
- context "when the field requires mongoization" do
130
-
116
+ context 'when the field requires mongoization' do
131
117
  let(:translations) do
132
- { "en" => 1, "de" => 2 }
118
+ { 'en' => 1, 'de' => 2 }
133
119
  end
134
120
 
135
121
  before do
136
122
  dictionary.description_translations = translations
137
123
  end
138
124
 
139
- it "sets the mongoized values of the translations" do
125
+ it 'sets the mongoized values of the translations' do
140
126
  expect(dictionary.description_translations).to eq(
141
- { "en" => "1", "de" => "2" }
127
+ { 'en' => '1', 'de' => '2' }
142
128
  )
143
129
  end
144
130
 
145
- context "when saving the new translations" do
146
-
131
+ context 'when saving the new translations' do
147
132
  before do
148
133
  dictionary.save!
149
134
  end
150
135
 
151
- it "persists the changes" do
136
+ it 'persists the changes' do
152
137
  expect(dictionary.reload.description_translations).to eq(
153
- { "en" => "1", "de" => "2" }
138
+ { 'en' => '1', 'de' => '2' }
154
139
  )
155
140
  end
156
141
 
157
- context "when updating the translations" do
158
-
142
+ context 'when updating the translations' do
159
143
  before do
160
- dictionary.description_translations = { "en" => "overwritten" }
144
+ dictionary.description_translations = { 'en' => 'overwritten' }
161
145
  dictionary.save!
162
146
  end
163
147
 
164
- it "persists the changes" do
148
+ it 'persists the changes' do
165
149
  expect(dictionary.reload.description_translations).to eq(
166
- { "en" => "overwritten" }
150
+ { 'en' => 'overwritten' }
167
151
  )
168
152
  end
169
153
  end
170
154
  end
171
155
  end
172
156
 
173
- it "should have alias method #\{field}_t=" do
157
+ it 'has alias method <field>_t=' do
174
158
  expect(product.method(:name_t=)).to eq product.method(:name_translations=)
175
159
  end
176
160
  end
177
161
 
178
- context "when the field is not localized" do
179
-
180
- it "does not respond to the method" do
181
- expect(product).to_not respond_to(:price_translations=)
162
+ context 'when the field is not localized' do
163
+ it 'does not respond to the method' do
164
+ expect(product).not_to respond_to(:price_translations=)
182
165
  end
183
166
 
184
- it "does not respond to the alias method" do
185
- expect(product).to_not respond_to(:price_t=)
167
+ it 'does not respond to the alias method' do
168
+ expect(product).not_to respond_to(:price_t=)
186
169
  end
187
170
  end
188
171
  end
189
172
 
190
- describe "#aliased_fields" do
191
-
173
+ describe '#aliased_fields' do
192
174
  let(:person) do
193
175
  Person.new
194
176
  end
195
177
 
196
- context "when the document is subclassed" do
197
-
198
- it "does not include the child aliases" do
199
- expect(person.aliased_fields.keys).to_not include("spec")
178
+ context 'when the document is subclassed' do
179
+ it 'does not include the child aliases' do
180
+ expect(person.aliased_fields.keys).not_to include('spec')
200
181
  end
201
182
  end
202
183
  end
203
184
 
204
- describe "#attribute_names" do
205
-
206
- context "when the document is a parent class" do
207
-
185
+ describe '#attribute_names' do
186
+ context 'when the document is a parent class' do
208
187
  let(:shape) do
209
188
  Shape.new
210
189
  end
211
190
 
212
- it "includes the _id field" do
213
- expect(shape.attribute_names).to include("_id")
191
+ it 'includes the _id field' do
192
+ expect(shape.attribute_names).to include('_id')
214
193
  end
215
194
 
216
- it "includes the _type field" do
217
- expect(shape.attribute_names).to include("_type")
195
+ it 'includes the _type field' do
196
+ expect(shape.attribute_names).to include('_type')
218
197
  end
219
198
 
220
- it "includes its own fields" do
221
- expect(shape.attribute_names).to include("x")
199
+ it 'includes its own fields' do
200
+ expect(shape.attribute_names).to include('x')
222
201
  end
223
202
 
224
- it "does not return subclass fields" do
225
- expect(shape.attribute_names).to_not include("radius")
203
+ it 'does not return subclass fields' do
204
+ expect(shape.attribute_names).not_to include('radius')
226
205
  end
227
206
  end
228
207
 
229
- context "when the document is a subclass" do
230
-
208
+ context 'when the document is a subclass' do
231
209
  let(:circle) do
232
210
  Circle.new
233
211
  end
234
212
 
235
- it "includes the _id field" do
236
- expect(circle.attribute_names).to include("_id")
213
+ it 'includes the _id field' do
214
+ expect(circle.attribute_names).to include('_id')
237
215
  end
238
216
 
239
- it "includes the _type field" do
240
- expect(circle.attribute_names).to include("_type")
217
+ it 'includes the _type field' do
218
+ expect(circle.attribute_names).to include('_type')
241
219
  end
242
220
 
243
- it "includes the first parent field" do
244
- expect(circle.attribute_names).to include("x")
221
+ it 'includes the first parent field' do
222
+ expect(circle.attribute_names).to include('x')
245
223
  end
246
224
 
247
- it "includes the second parent field" do
248
- expect(circle.attribute_names).to include("y")
225
+ it 'includes the second parent field' do
226
+ expect(circle.attribute_names).to include('y')
249
227
  end
250
228
 
251
- it "includes the child fields" do
252
- expect(circle.attribute_names).to include("radius")
229
+ it 'includes the child fields' do
230
+ expect(circle.attribute_names).to include('radius')
253
231
  end
254
232
  end
255
233
  end
256
234
 
257
- describe ".attribute_names" do
258
-
259
- context "when the class is a parent" do
260
-
261
- it "includes the _id field" do
262
- expect(Shape.attribute_names).to include("_id")
235
+ describe '.attribute_names' do
236
+ context 'when the class is a parent' do
237
+ it 'includes the _id field' do
238
+ expect(Shape.attribute_names).to include('_id')
263
239
  end
264
240
 
265
- it "includes the _type field" do
266
- expect(Shape.attribute_names).to include("_type")
241
+ it 'includes the _type field' do
242
+ expect(Shape.attribute_names).to include('_type')
267
243
  end
268
244
 
269
- it "includes its own fields" do
270
- expect(Shape.attribute_names).to include("x")
245
+ it 'includes its own fields' do
246
+ expect(Shape.attribute_names).to include('x')
271
247
  end
272
248
 
273
- it "does not return subclass fields" do
274
- expect(Shape.attribute_names).to_not include("radius")
249
+ it 'does not return subclass fields' do
250
+ expect(Shape.attribute_names).not_to include('radius')
275
251
  end
276
252
  end
277
253
 
278
- context "when the class is a subclass" do
279
-
280
- it "includes the _id field" do
281
- expect(Circle.attribute_names).to include("_id")
254
+ context 'when the class is a subclass' do
255
+ it 'includes the _id field' do
256
+ expect(Circle.attribute_names).to include('_id')
282
257
  end
283
258
 
284
- it "includes the _type field" do
285
- expect(Circle.attribute_names).to include("_type")
259
+ it 'includes the _type field' do
260
+ expect(Circle.attribute_names).to include('_type')
286
261
  end
287
262
 
288
- it "includes the first parent field" do
289
- expect(Circle.attribute_names).to include("x")
263
+ it 'includes the first parent field' do
264
+ expect(Circle.attribute_names).to include('x')
290
265
  end
291
266
 
292
- it "includes the second parent field" do
293
- expect(Circle.attribute_names).to include("y")
267
+ it 'includes the second parent field' do
268
+ expect(Circle.attribute_names).to include('y')
294
269
  end
295
270
 
296
- it "includes the child fields" do
297
- expect(Circle.attribute_names).to include("radius")
271
+ it 'includes the child fields' do
272
+ expect(Circle.attribute_names).to include('radius')
298
273
  end
299
274
  end
300
275
  end
301
276
 
302
- describe "#field" do
303
-
277
+ describe '#field' do
304
278
  before(:all) do
305
279
  Mongoid::Fields.option :custom do |model, field, value|
306
280
  end
307
281
  end
308
282
 
309
- context "when providing a root Boolean type" do
310
-
283
+ context 'when providing a root Boolean type' do
311
284
  let(:klass) do
312
285
  Class.new do
313
286
  include Mongoid::Document
314
287
  end
315
288
  end
316
289
 
317
- it "converts to Mongoid::Boolean" do
290
+ it 'converts to Mongoid::Boolean' do
318
291
  expect(klass.field(:test, type: Mongoid::Boolean).type).to be(Mongoid::Boolean)
319
292
  end
320
293
  end
321
294
 
322
- context "when using symbol types" do
323
-
295
+ context 'when using symbol types' do
324
296
  let(:klass) do
325
297
  Class.new do
326
298
  include Mongoid::Document
327
299
  end
328
300
  end
329
301
 
330
- it "converts :array to Array" do
302
+ it 'converts :array to Array' do
331
303
  expect(klass.field(:test, type: :array).type).to be(Array)
332
304
  end
333
305
 
334
- it "converts :big_decimal to BigDecimal" do
306
+ it 'converts :big_decimal to BigDecimal' do
335
307
  expect(klass.field(:test, type: :big_decimal).type).to be(BigDecimal)
336
308
  end
337
309
 
338
- it "converts :binary to BSON::Binary" do
310
+ it 'converts :binary to BSON::Binary' do
339
311
  expect(klass.field(:test, type: :binary).type).to be(BSON::Binary)
340
312
  end
341
313
 
342
- it "converts :boolean to Mongoid::Boolean" do
314
+ it 'converts :boolean to Mongoid::Boolean' do
343
315
  expect(klass.field(:test, type: :boolean).type).to be(Mongoid::Boolean)
344
316
  end
345
317
 
346
- it "converts :date to Date" do
318
+ it 'converts :date to Date' do
347
319
  expect(klass.field(:test, type: :date).type).to be(Date)
348
320
  end
349
321
 
350
- it "converts :date_time to DateTime" do
322
+ it 'converts :date_time to DateTime' do
351
323
  expect(klass.field(:test, type: :date_time).type).to be(DateTime)
352
324
  end
353
325
 
354
- it "converts :float to Float" do
326
+ it 'converts :float to Float' do
355
327
  expect(klass.field(:test, type: :float).type).to be(Float)
356
328
  end
357
329
 
358
- it "converts :hash to Hash" do
330
+ it 'converts :hash to Hash' do
359
331
  expect(klass.field(:test, type: :hash).type).to be(Hash)
360
332
  end
361
333
 
362
- it "converts :integer to Integer" do
334
+ it 'converts :integer to Integer' do
363
335
  expect(klass.field(:test, type: :integer).type).to be(Integer)
364
336
  end
365
337
 
366
- it "converts :object_id to BSON::ObjectId" do
338
+ it 'converts :object_id to BSON::ObjectId' do
367
339
  expect(klass.field(:test, type: :object_id).type).to be(BSON::ObjectId)
368
340
  end
369
341
 
370
- it "converts :range to Range" do
342
+ it 'converts :range to Range' do
371
343
  expect(klass.field(:test, type: :range).type).to be(Range)
372
344
  end
373
345
 
374
- it "converts :regexp to Rexegp" do
346
+ it 'converts :regexp to Regexp' do
375
347
  expect(klass.field(:test, type: :regexp).type).to be(Regexp)
376
348
  end
377
349
 
378
- it "converts :set to Set" do
350
+ it 'converts :set to Set' do
379
351
  expect(klass.field(:test, type: :set).type).to be(Set)
380
352
  end
381
353
 
382
- it "converts :string to String" do
354
+ it 'converts :string to String' do
383
355
  expect(klass.field(:test, type: :string).type).to be(String)
384
356
  end
385
357
 
386
- it "converts :symbol to Symbol" do
358
+ it 'converts :symbol to Symbol' do
387
359
  expect(klass.field(:test, type: :symbol).type).to be(Symbol)
388
360
  end
389
361
 
390
- it "converts :time to Time" do
362
+ it 'converts :time to Time' do
391
363
  expect(klass.field(:test, type: :time).type).to be(Time)
392
364
  end
393
365
 
@@ -395,7 +367,8 @@ describe Mongoid::Fields do
395
367
  it 'raises InvalidFieldType' do
396
368
  lambda do
397
369
  klass.field(:test, type: :bogus)
398
- end.should raise_error(Mongoid::Errors::InvalidFieldType, /defines a field 'test' with an unknown type value :bogus/)
370
+ end.should raise_error(Mongoid::Errors::InvalidFieldType,
371
+ /defines a field 'test' with an unknown type value :bogus/)
399
372
  end
400
373
  end
401
374
 
@@ -403,42 +376,39 @@ describe Mongoid::Fields do
403
376
  it 'raises InvalidFieldType' do
404
377
  lambda do
405
378
  klass.field(:test, type: 'bogus')
406
- end.should raise_error(Mongoid::Errors::InvalidFieldType, /defines a field 'test' with an unknown type value "bogus"/)
379
+ end.should raise_error(Mongoid::Errors::InvalidFieldType,
380
+ /defines a field 'test' with an unknown type value "bogus"/)
407
381
  end
408
382
  end
409
383
  end
410
384
 
411
- context "when the options are valid" do
412
-
413
- context "when the options are all standard" do
414
-
385
+ context 'when the options are valid' do
386
+ context 'when the options are all standard' do
415
387
  before do
416
388
  Band.field :acceptable, type: Mongoid::Boolean
417
389
  end
418
390
 
419
391
  after do
420
- Band.fields.delete("acceptable")
392
+ Band.fields.delete('acceptable')
421
393
  end
422
394
 
423
- it "adds the field to the model" do
424
- expect(Band.fields["acceptable"]).to_not be_nil
395
+ it 'adds the field to the model' do
396
+ expect(Band.fields['acceptable']).not_to be_nil
425
397
  end
426
398
  end
427
399
 
428
- context "when a custom option is provided" do
429
-
400
+ context 'when a custom option is provided' do
430
401
  before do
431
402
  Band.field :acceptable, type: Mongoid::Boolean, custom: true
432
403
  end
433
404
 
434
- it "adds the field to the model" do
435
- expect(Band.fields["acceptable"]).to_not be_nil
405
+ it 'adds the field to the model' do
406
+ expect(Band.fields['acceptable']).not_to be_nil
436
407
  end
437
408
  end
438
409
  end
439
410
 
440
- context "when the Symbol type is used" do
441
-
411
+ context 'when the Symbol type is used' do
442
412
  before do
443
413
  Mongoid::Warnings.class_eval do
444
414
  @symbol_type_deprecated = false
@@ -446,16 +416,16 @@ describe Mongoid::Fields do
446
416
  end
447
417
 
448
418
  after do
449
- Label.fields.delete("should_warn")
419
+ Label.fields.delete('should_warn')
450
420
  end
451
421
 
452
- it "warns that the BSON symbol type is deprecated" do
422
+ it 'warns that the BSON symbol type is deprecated' do
453
423
  expect(Mongoid.logger).to receive(:warn)
454
424
 
455
425
  Label.field :should_warn, type: Symbol
456
426
  end
457
427
 
458
- it "warns on first use of Symbol type only" do
428
+ it 'warns on first use of Symbol type only' do
459
429
  expect(Mongoid.logger).to receive(:warn).once
460
430
 
461
431
  Label.field :should_warn, type: Symbol
@@ -463,10 +433,10 @@ describe Mongoid::Fields do
463
433
 
464
434
  context 'when using Symbol field type in multiple classes' do
465
435
  after do
466
- Truck.fields.delete("should_warn")
436
+ Truck.fields.delete('should_warn')
467
437
  end
468
438
 
469
- it "warns on first use of Symbol type only" do
439
+ it 'warns on first use of Symbol type only' do
470
440
  expect(Mongoid.logger).to receive(:warn).once
471
441
 
472
442
  Label.field :should_warn, type: Symbol
@@ -475,87 +445,82 @@ describe Mongoid::Fields do
475
445
  end
476
446
  end
477
447
 
478
- context "when the options are not valid" do
479
-
480
- it "raises an error" do
481
- expect {
448
+ context 'when the options are not valid' do
449
+ it 'raises an error' do
450
+ expect do
482
451
  Label.field :unacceptable, bad: true
483
- }.to raise_error(Mongoid::Errors::InvalidFieldOption)
452
+ end.to raise_error(Mongoid::Errors::InvalidFieldOption)
484
453
  end
485
454
  end
486
455
  end
487
456
 
488
- describe "#getter" do
489
-
490
- context "when the field is binary" do
491
-
457
+ describe '#getter' do
458
+ context 'when the field is binary' do
492
459
  let(:binary) do
493
- BSON::Binary.new("testing", :md5)
460
+ BSON::Binary.new('testing', :md5)
494
461
  end
495
462
 
496
463
  let(:registry) do
497
464
  Registry.new(data: binary)
498
465
  end
499
466
 
500
- it "returns the binary data intact" do
467
+ it 'returns the binary data intact' do
501
468
  expect(registry.data).to eq(binary)
502
469
  end
503
470
  end
504
471
 
505
- context "when a field is localized" do
506
-
472
+ context 'when a field is localized' do
507
473
  let(:product) do
508
474
  Product.new
509
475
  end
510
476
 
511
- context "when no locale is set" do
512
-
477
+ context 'when no locale is set' do
513
478
  before do
514
- product.description = "The best"
479
+ product.description = 'The best'
515
480
  end
516
481
 
517
482
  let(:description) do
518
483
  product.description
519
484
  end
520
485
 
521
- it "returns the default locale value" do
522
- expect(description).to eq("The best")
486
+ it 'returns the default locale value' do
487
+ expect(description).to eq('The best')
523
488
  end
524
489
  end
525
490
 
526
- context "when a single locale is set" do
491
+ context 'when a single locale is set' do
527
492
  with_default_i18n_configs
528
493
 
529
494
  before do
530
495
  I18n.locale = :de
531
- product.description = "The best"
496
+ product.description = 'The best'
532
497
  end
533
498
 
534
499
  let(:description) do
535
500
  product.description
536
501
  end
537
502
 
538
- it "returns the set locale value" do
539
- expect(description).to eq("The best")
503
+ it 'returns the set locale value' do
504
+ expect(description).to eq('The best')
540
505
  end
541
506
  end
542
507
 
543
- context "when multiple locales are set" do
508
+ context 'when multiple locales are set' do
544
509
  with_default_i18n_configs
545
510
 
546
511
  before do
547
512
  I18n.locale = :end
548
- product.description = "Cheap drinks"
513
+ product.description = 'Cheap drinks'
549
514
  I18n.locale = :de
550
- product.description = "Cheaper drinks"
515
+ product.description = 'Cheaper drinks'
551
516
  end
552
517
 
553
518
  let(:description) do
554
519
  product.description
555
520
  end
556
521
 
557
- it "returns the current locale value" do
558
- expect(description).to eq("Cheaper drinks")
522
+ it 'returns the current locale value' do
523
+ expect(description).to eq('Cheaper drinks')
559
524
  end
560
525
  end
561
526
  end
@@ -563,20 +528,20 @@ describe Mongoid::Fields do
563
528
  context 'when the field is declared as BSON::Decimal128' do
564
529
  let(:document) { Mop.create!(decimal128_field: BSON::Decimal128.new(Math::PI.to_s)).reload }
565
530
 
566
- shared_context 'BSON::Decimal128 is BigDecimal' do
567
- it 'should return a BigDecimal' do
531
+ shared_examples 'BSON::Decimal128 is BigDecimal' do
532
+ it 'returns a BigDecimal' do
568
533
  expect(document.decimal128_field).to be_a BigDecimal
569
534
  end
570
535
  end
571
536
 
572
- shared_context 'BSON::Decimal128 is BSON::Decimal128' do
573
- it 'should return a BSON::Decimal128' do
537
+ shared_examples 'BSON::Decimal128 is BSON::Decimal128' do
538
+ it 'returns a BSON::Decimal128' do
574
539
  expect(document.decimal128_field).to be_a BSON::Decimal128
575
540
  end
576
541
  end
577
542
 
578
543
  it 'is declared as BSON::Decimal128' do
579
- expect(Mop.fields['decimal128_field'].type).to be == BSON::Decimal128
544
+ expect(Mop.fields['decimal128_field'].type).to eq BSON::Decimal128
580
545
  end
581
546
 
582
547
  context 'when BSON version <= 4' do
@@ -604,28 +569,25 @@ describe Mongoid::Fields do
604
569
  end
605
570
  end
606
571
 
607
- describe "#getter_before_type_cast" do
572
+ describe '#getter_before_type_cast' do
608
573
  let(:person) do
609
574
  Person.new
610
575
  end
611
576
 
612
- context "when the attribute has not been assigned" do
613
-
614
- it "delgates to the getter" do
577
+ context 'when the attribute has not been assigned' do
578
+ it 'delegates to the getter' do
615
579
  expect(person.age_before_type_cast).to eq(person.age)
616
580
  end
617
581
  end
618
582
 
619
- context "when the attribute has been assigned" do
620
-
621
- it "returns the attribute before type cast" do
622
- person.age = "42"
623
- expect(person.age_before_type_cast).to eq("42")
583
+ context 'when the attribute has been assigned' do
584
+ it 'returns the attribute before type cast' do
585
+ person.age = '42'
586
+ expect(person.age_before_type_cast).to eq('42')
624
587
  end
625
588
  end
626
589
 
627
- context "when reloading" do
628
-
590
+ context 'when reloading' do
629
591
  let(:product) do
630
592
  Product.create!(price: '1')
631
593
  end
@@ -634,20 +596,19 @@ describe Mongoid::Fields do
634
596
  product.reload
635
597
  end
636
598
 
637
- it "resets the attributes_before_type_cast to the attributes hash" do
599
+ it 'resets the attributes_before_type_cast to the attributes hash' do
638
600
  expect(product.attributes_before_type_cast).to eq(product.attributes)
639
601
  end
640
602
 
641
- it "the *_before_type_cast method returns the demongoized value" do
603
+ it 'the *_before_type_cast method returns the demongoized value' do
642
604
  expect(product.price_before_type_cast).to eq(1)
643
605
  end
644
606
  end
645
607
 
646
- context "when reloading and writing a demongoizable value" do
647
-
608
+ context 'when reloading and writing a demongoizable value' do
648
609
  let(:product) do
649
610
  Product.create!.tap do |product|
650
- Product.collection.update_one({ _id: product.id }, { :$set => { price: '1' }})
611
+ Product.collection.update_one({ _id: product.id }, { :$set => { price: '1' } })
651
612
  end
652
613
  end
653
614
 
@@ -655,17 +616,16 @@ describe Mongoid::Fields do
655
616
  product.reload
656
617
  end
657
618
 
658
- it "resets the attributes_before_type_cast to the attributes hash" do
619
+ it 'resets the attributes_before_type_cast to the attributes hash' do
659
620
  expect(product.attributes_before_type_cast).to eq(product.attributes)
660
621
  end
661
622
 
662
- it "the *_before_type_cast method returns the mongoized value" do
623
+ it 'the *_before_type_cast method returns the mongoized value' do
663
624
  expect(product.price_before_type_cast).to eq('1')
664
625
  end
665
626
  end
666
627
 
667
- context "when reading from the db" do
668
-
628
+ context 'when reading from the db' do
669
629
  let(:product) do
670
630
  Product.create!(price: '1')
671
631
  end
@@ -674,20 +634,19 @@ describe Mongoid::Fields do
674
634
  Product.find(product.id)
675
635
  end
676
636
 
677
- it "resets the attributes_before_type_cast to the attributes hash" do
637
+ it 'resets the attributes_before_type_cast to the attributes hash' do
678
638
  expect(from_db.attributes_before_type_cast).to eq(from_db.attributes)
679
639
  end
680
640
 
681
- it "the *_before_type_cast method returns the demongoized value" do
641
+ it 'the *_before_type_cast method returns the demongoized value' do
682
642
  expect(from_db.price_before_type_cast).to eq(1)
683
643
  end
684
644
  end
685
645
 
686
- context "when reading from the db after writing a demongoizable value" do
687
-
646
+ context 'when reading from the db after writing a demongoizable value' do
688
647
  let(:product) do
689
648
  Product.create!.tap do |product|
690
- Product.collection.update_one({ _id: product.id }, { :$set => { price: '1' }})
649
+ Product.collection.update_one({ _id: product.id }, { :$set => { price: '1' } })
691
650
  end
692
651
  end
693
652
 
@@ -695,38 +654,37 @@ describe Mongoid::Fields do
695
654
  Product.find(product.id)
696
655
  end
697
656
 
698
- it "resets the attributes_before_type_cast to the attributes hash" do
657
+ it 'resets the attributes_before_type_cast to the attributes hash' do
699
658
  expect(from_db.attributes_before_type_cast).to eq(from_db.attributes)
700
659
  end
701
660
 
702
- it "the *_before_type_cast method returns the mongoized value" do
661
+ it 'the *_before_type_cast method returns the mongoized value' do
703
662
  expect(from_db.price_before_type_cast).to eq('1')
704
663
  end
705
664
  end
706
665
 
707
- context "when making a new model" do
708
-
709
- context "when using new with no options" do
666
+ context 'when making a new model' do
667
+ context 'when using new with no options' do
710
668
  let(:product) { Product.new }
711
669
 
712
- it "sets the attributes_before_type_cast to the attributes hash" do
670
+ it 'sets the attributes_before_type_cast to the attributes hash' do
713
671
  expect(product.attributes_before_type_cast).to eq(product.attributes)
714
672
  end
715
673
  end
716
674
 
717
- context "when using new with options" do
675
+ context 'when using new with options' do
718
676
  let(:product) { Product.new(price: '1') }
719
677
 
720
678
  let(:abtc) do
721
679
  product.attributes.merge('price' => '1')
722
680
  end
723
681
 
724
- it "has the attributes before type cast" do
682
+ it 'has the attributes before type cast' do
725
683
  expect(product.attributes_before_type_cast).to eq(abtc)
726
684
  end
727
685
  end
728
686
 
729
- context "when persisting the model" do
687
+ context 'when persisting the model' do
730
688
  let(:product) { Product.new(price: '1') }
731
689
 
732
690
  let(:abtc) do
@@ -738,53 +696,49 @@ describe Mongoid::Fields do
738
696
  product.save!
739
697
  end
740
698
 
741
- it "resets the attributes_before_type_cast to the attributes" do
699
+ it 'resets the attributes_before_type_cast to the attributes' do
742
700
  expect(product.attributes_before_type_cast).to eq(product.attributes)
743
701
  end
744
702
  end
745
703
 
746
- context "when using create! without options" do
704
+ context 'when using create! without options' do
747
705
  let(:product) { Product.create! }
748
706
 
749
- it "resets the attributes_before_type_cast to the attributes" do
707
+ it 'resets the attributes_before_type_cast to the attributes' do
750
708
  expect(product.attributes_before_type_cast).to eq(product.attributes)
751
709
  end
752
710
  end
753
711
 
754
- context "when using create! with options" do
712
+ context 'when using create! with options' do
755
713
  let(:product) { Product.create!(price: '1') }
756
714
 
757
- it "resets the attributes_before_type_cast to the attributes" do
715
+ it 'resets the attributes_before_type_cast to the attributes' do
758
716
  expect(product.attributes_before_type_cast).to eq(product.attributes)
759
717
  end
760
718
  end
761
719
  end
762
720
  end
763
721
 
764
- describe "#setter=" do
765
-
722
+ describe '#setter=' do
766
723
  let(:product) do
767
724
  Product.new
768
725
  end
769
726
 
770
- context "when setting via the setter" do
771
-
772
- it "returns the set value" do
727
+ context 'when setting via the setter' do
728
+ it 'returns the set value' do
773
729
  expect(product.price = 10).to eq(10)
774
730
  end
775
731
  end
776
732
 
777
- context "when setting via send" do
778
-
779
- it "returns the set value" do
733
+ context 'when setting via send' do
734
+ it 'returns the set value' do
780
735
  expect(product.send(:price=, 10)).to eq(10)
781
736
  end
782
737
  end
783
738
 
784
- context "when the field is binary" do
785
-
739
+ context 'when the field is binary' do
786
740
  let(:binary) do
787
- BSON::Binary.new("testing", :md5)
741
+ BSON::Binary.new('testing', :md5)
788
742
  end
789
743
 
790
744
  let(:registry) do
@@ -795,168 +749,159 @@ describe Mongoid::Fields do
795
749
  registry.data = binary
796
750
  end
797
751
 
798
- it "returns the binary data intact" do
752
+ it 'returns the binary data intact' do
799
753
  expect(registry.data).to eq(binary)
800
754
  end
801
755
  end
802
756
 
803
- context "when the field is an array" do
804
-
757
+ context 'when the field is an array' do
805
758
  before do
806
- product.stores = [ "kadewe", "karstadt" ]
759
+ product.stores = %w[kadewe karstadt]
807
760
  product.save!
808
761
  end
809
762
 
810
- context "when setting the value to nil" do
811
-
763
+ context 'when setting the value to nil' do
812
764
  before do
813
765
  product.stores = nil
814
766
  product.save!
815
767
  end
816
768
 
817
- it "allows the set" do
769
+ it 'allows the set' do
818
770
  expect(product.stores).to be_nil
819
771
  end
820
772
  end
821
773
 
822
- context "when setting any of the values to nil" do
823
-
774
+ context 'when setting any of the values to nil' do
824
775
  before do
825
- product.stores = [ "kadewe", nil ]
776
+ product.stores = [ 'kadewe', nil ]
826
777
  product.save!
827
778
  end
828
779
 
829
- it "allows the set of nil values" do
830
- expect(product.stores).to eq([ "kadewe", nil ])
780
+ it 'allows the set of nil values' do
781
+ expect(product.stores).to eq([ 'kadewe', nil ])
831
782
  end
832
783
 
833
- it "persists the nil values" do
834
- expect(product.reload.stores).to eq([ "kadewe", nil ])
784
+ it 'persists the nil values' do
785
+ expect(product.reload.stores).to eq([ 'kadewe', nil ])
835
786
  end
836
787
  end
837
788
 
838
- context "when reversing the array values" do
839
-
789
+ context 'when reversing the array values' do
840
790
  before do
841
- product.stores = [ "karstadt", "kadewe" ]
791
+ product.stores = %w[karstadt kadewe]
842
792
  product.save!
843
793
  end
844
794
 
845
- it "reverses the values" do
846
- expect(product.stores).to eq([ "karstadt", "kadewe" ])
795
+ it 'reverses the values' do
796
+ expect(product.stores).to eq(%w[karstadt kadewe])
847
797
  end
848
798
 
849
- it "persists the changes" do
850
- expect(product.reload.stores).to eq([ "karstadt", "kadewe" ])
799
+ it 'persists the changes' do
800
+ expect(product.reload.stores).to eq(%w[karstadt kadewe])
851
801
  end
852
802
  end
853
803
  end
854
804
 
855
- context "when a field is localized" do
856
-
857
- context "when no locale is set" do
858
-
805
+ context 'when a field is localized' do
806
+ context 'when no locale is set' do
859
807
  before do
860
- product.description = "Cheap drinks"
808
+ product.description = 'Cheap drinks'
861
809
  end
862
810
 
863
811
  let(:description) do
864
- product.attributes["description"]
812
+ product.attributes['description']
865
813
  end
866
814
 
867
- it "sets the value in the default locale" do
868
- expect(description).to eq({ "en" => "Cheap drinks" })
815
+ it 'sets the value in the default locale' do
816
+ expect(description).to eq({ 'en' => 'Cheap drinks' })
869
817
  end
870
818
  end
871
819
 
872
- context "when a locale is set" do
820
+ context 'when a locale is set' do
873
821
  with_default_i18n_configs
874
822
 
875
823
  before do
876
824
  I18n.locale = :de
877
- product.description = "Cheaper drinks"
825
+ product.description = 'Cheaper drinks'
878
826
  end
879
827
 
880
828
  let(:description) do
881
- product.attributes["description"]
829
+ product.attributes['description']
882
830
  end
883
831
 
884
- it "sets the value in the default locale" do
885
- expect(description).to eq({ "de" => "Cheaper drinks" })
832
+ it 'sets the value in the default locale' do
833
+ expect(description).to eq({ 'de' => 'Cheaper drinks' })
886
834
  end
887
835
  end
888
836
 
889
- context "when having multiple locales" do
837
+ context 'when having multiple locales' do
890
838
  with_default_i18n_configs
891
839
 
892
840
  before do
893
841
  I18n.locale = :en
894
- product.description = "Cheap drinks"
842
+ product.description = 'Cheap drinks'
895
843
  I18n.locale = :de
896
- product.description = "Cheaper drinks"
844
+ product.description = 'Cheaper drinks'
897
845
  end
898
846
 
899
847
  let(:description) do
900
- product.attributes["description"]
848
+ product.attributes['description']
901
849
  end
902
850
 
903
- it "sets the value in both locales" do
851
+ it 'sets the value in both locales' do
904
852
  expect(description).to eq(
905
- { "de" => "Cheaper drinks", "en" => "Cheap drinks" }
853
+ { 'de' => 'Cheaper drinks', 'en' => 'Cheap drinks' }
906
854
  )
907
855
  end
908
856
  end
909
857
  end
910
858
 
911
- context "when the field needs to be mongoized" do
912
-
859
+ context 'when the field needs to be mongoized' do
913
860
  before do
914
- product.price = "1"
861
+ product.price = '1'
915
862
  product.save!
916
863
  end
917
864
 
918
- it "mongoizes the value" do
865
+ it 'mongoizes the value' do
919
866
  expect(product.price).to eq(1)
920
867
  end
921
868
 
922
- it "stores the value in the mongoized form" do
923
- expect(product.attributes_before_type_cast["price"]).to eq(1)
869
+ it 'stores the value in the mongoized form' do
870
+ expect(product.attributes_before_type_cast['price']).to eq(1)
924
871
  end
925
872
  end
926
873
 
927
- context "when assigning a hash" do
874
+ context 'when assigning a hash' do
928
875
  let(:person) { Person.create!(map: { x: 1 }) }
929
876
 
930
- it "is a BSON::Document" do
877
+ it 'is a BSON::Document' do
931
878
  expect(person.map).to be_a(BSON::Document)
932
879
  end
933
880
 
934
- it "has the correct contents" do
935
- expect(person.map).to eq({ "x" => 1 })
881
+ it 'has the correct contents' do
882
+ expect(person.map).to eq({ 'x' => 1 })
936
883
  end
937
884
  end
938
885
 
939
- context "when loading a hash from the db" do
886
+ context 'when loading a hash from the db' do
940
887
  before do
941
888
  Person.create!(map: { x: 1 })
942
889
  end
943
890
 
944
891
  let(:person) { Person.first }
945
892
 
946
- it "is a BSON::Document" do
893
+ it 'is a BSON::Document' do
947
894
  expect(person.map).to be_a(BSON::Document)
948
895
  end
949
896
 
950
- it "has the correct contents" do
951
- expect(person.map).to eq({ "x" => 1 })
897
+ it 'has the correct contents' do
898
+ expect(person.map).to eq({ 'x' => 1 })
952
899
  end
953
900
  end
954
901
  end
955
902
 
956
- describe "#defaults" do
957
-
958
- context "with defaults specified as a non-primitive" do
959
-
903
+ describe '#defaults' do
904
+ context 'with defaults specified as a non-primitive' do
960
905
  let(:person_one) do
961
906
  Person.new
962
907
  end
@@ -965,157 +910,146 @@ describe Mongoid::Fields do
965
910
  Person.new
966
911
  end
967
912
 
968
- context "when provided a default array" do
969
-
913
+ context 'when provided a default array' do
970
914
  before do
971
915
  Person.field(:array_testing, type: Array, default: [], overwrite: true)
972
916
  end
973
917
 
974
918
  after do
975
- Person.fields.delete("array_testing")
976
- Person.pre_processed_defaults.delete_one("array_testing")
919
+ Person.fields.delete('array_testing')
920
+ Person.pre_processed_defaults.delete_one('array_testing')
977
921
  end
978
922
 
979
- it "returns an equal object of a different instance" do
980
- expect(person_one.array_testing.object_id).to_not eq(
923
+ it 'returns an equal object of a different instance' do
924
+ expect(person_one.array_testing.object_id).not_to eq(
981
925
  person_two.array_testing.object_id
982
926
  )
983
927
  end
984
928
  end
985
929
 
986
- context "when provided a default hash" do
987
-
930
+ context 'when provided a default hash' do
988
931
  before do
989
932
  Person.field(:hash_testing, type: Hash, default: {}, overwrite: true)
990
933
  end
991
934
 
992
935
  after do
993
- Person.fields.delete("hash_testing")
936
+ Person.fields.delete('hash_testing')
994
937
  end
995
938
 
996
- it "returns an equal object of a different instance" do
997
- expect(person_one.hash_testing.object_id).to_not eq(
939
+ it 'returns an equal object of a different instance' do
940
+ expect(person_one.hash_testing.object_id).not_to eq(
998
941
  person_two.hash_testing.object_id
999
942
  )
1000
943
  end
1001
944
  end
1002
945
 
1003
- context "when provided a default proc" do
1004
-
1005
- context "when the proc has no argument" do
1006
-
946
+ context 'when provided a default proc' do
947
+ context 'when the proc has no argument' do
1007
948
  before do
1008
949
  Person.field(
1009
950
  :generated_testing,
1010
951
  type: Float,
1011
- default: ->{ Time.now.to_f },
952
+ default: -> { Time.now.to_f },
1012
953
  overwrite: true
1013
954
  )
1014
955
  end
1015
956
 
1016
957
  after do
1017
- Person.fields.delete("generated_testing")
1018
- Person.pre_processed_defaults.delete_one("generated_testing")
958
+ Person.fields.delete('generated_testing')
959
+ Person.pre_processed_defaults.delete_one('generated_testing')
1019
960
  end
1020
961
 
1021
- it "returns an equal object of a different instance" do
1022
- expect(person_one.generated_testing.object_id).to_not eq(
962
+ it 'returns an equal object of a different instance' do
963
+ expect(person_one.generated_testing.object_id).not_to eq(
1023
964
  person_two.generated_testing.object_id
1024
965
  )
1025
966
  end
1026
967
  end
1027
968
 
1028
- context "when the proc has to be evaluated on the document" do
1029
-
969
+ context 'when the proc has to be evaluated on the document' do
1030
970
  before do
1031
971
  Person.field(
1032
972
  :rank,
1033
973
  type: Integer,
1034
- default: ->{ title? ? 1 : 2 },
974
+ default: -> { title? ? 1 : 2 },
1035
975
  overwrite: true
1036
976
  )
1037
977
  end
1038
978
 
1039
979
  after do
1040
- Person.fields.delete("rank")
1041
- Person.post_processed_defaults.delete_one("rank")
980
+ Person.fields.delete('rank')
981
+ Person.post_processed_defaults.delete_one('rank')
1042
982
  end
1043
983
 
1044
- it "yields the document to the proc" do
984
+ it 'yields the document to the proc' do
1045
985
  expect(Person.new.rank).to eq(2)
1046
986
  end
1047
987
  end
1048
988
  end
1049
989
  end
1050
990
 
1051
- context "on parent classes" do
1052
-
991
+ context 'on parent classes' do
1053
992
  let(:shape) do
1054
993
  Shape.new
1055
994
  end
1056
995
 
1057
- it "does not return subclass defaults" do
1058
- expect(shape.pre_processed_defaults).to eq([ "_id", "x", "y" ])
1059
- expect(shape.post_processed_defaults).to eq([ "_type" ])
996
+ it 'does not return subclass defaults' do
997
+ expect(shape.pre_processed_defaults).to eq(%w[_id x y])
998
+ expect(shape.post_processed_defaults).to eq([ '_type' ])
1060
999
  end
1061
1000
  end
1062
1001
 
1063
- context "on subclasses" do
1064
-
1002
+ context 'on subclasses' do
1065
1003
  let(:circle) do
1066
1004
  Circle.new
1067
1005
  end
1068
1006
 
1069
- it "has the parent and child defaults" do
1070
- expect(circle.pre_processed_defaults).to eq([ "_id", "x", "y", "radius" ])
1071
- expect(circle.post_processed_defaults).to eq([ "_type" ])
1007
+ it 'has the parent and child defaults' do
1008
+ expect(circle.pre_processed_defaults).to eq(%w[_id x y radius])
1009
+ expect(circle.post_processed_defaults).to eq([ '_type' ])
1072
1010
  end
1073
1011
  end
1074
1012
  end
1075
1013
 
1076
- describe ".field" do
1077
-
1078
- it "returns the generated field" do
1079
- expect(Person.field(:testing)).to eq(Person.fields["testing"])
1014
+ describe '.field' do
1015
+ it 'returns the generated field' do
1016
+ expect(Person.field(:testing)).to eq(Person.fields['testing'])
1080
1017
  end
1081
1018
 
1082
1019
  context "when the field name conflicts with mongoid's internals" do
1083
-
1084
- [:_association, :invalid].each do |meth|
1020
+ %i[_association invalid].each do |meth|
1085
1021
  context "when the field is named #{meth}" do
1086
-
1087
- it "raises an error" do
1088
- expect {
1022
+ it 'raises an error' do
1023
+ expect do
1089
1024
  Person.field(meth)
1090
- }.to raise_error(Mongoid::Errors::InvalidField, /Defining a field named '#{meth}' is not allowed/)
1025
+ end.to raise_error(Mongoid::Errors::InvalidField, /Defining a field named '#{meth}' is not allowed/)
1091
1026
  end
1092
1027
  end
1093
1028
  end
1094
1029
  end
1095
1030
 
1096
- context "when field already exist and validate_duplicate is enable" do
1031
+ context 'when field already exist and validate_duplicate is enable' do
1097
1032
  context 'when exception is enabled' do
1098
1033
  config_override :duplicate_fields_exception, true
1099
1034
 
1100
- it "raises an error" do
1101
- expect {
1035
+ it 'raises an error' do
1036
+ expect do
1102
1037
  Person.field(:title)
1103
- }.to raise_error(Mongoid::Errors::InvalidField)
1038
+ end.to raise_error(Mongoid::Errors::InvalidField)
1104
1039
  end
1105
1040
  end
1106
1041
 
1107
1042
  context 'when exception is disabled' do
1108
1043
  config_override :duplicate_fields_exception, false
1109
1044
  it "doesn't raise an error" do
1110
- expect {
1045
+ expect do
1111
1046
  Class.new(Person)
1112
- }.to_not raise_error
1047
+ end.not_to raise_error
1113
1048
  end
1114
1049
  end
1115
1050
  end
1116
1051
 
1117
- context "when the field is a time" do
1118
-
1052
+ context 'when the field is a time' do
1119
1053
  let!(:time) do
1120
1054
  Time.find_zone('UTC').parse('2023-03-03 10:30:53')
1121
1055
  end
@@ -1124,43 +1058,42 @@ describe Mongoid::Fields do
1124
1058
  Person.new(lunch_time: time.utc)
1125
1059
  end
1126
1060
 
1127
- context "when reading the field" do
1061
+ context 'when reading the field' do
1128
1062
  time_zone_override 'Europe/Berlin'
1129
1063
 
1130
- it "performs the necessary time conversions" do
1064
+ it 'performs the necessary time conversions' do
1131
1065
  expect(person.lunch_time.to_s).to eq('2023-03-03 11:30:53 +0100')
1132
1066
  expect(person.lunch_time.time_zone.name).to eq('Europe/Berlin')
1133
1067
  end
1134
1068
  end
1135
1069
  end
1136
1070
 
1137
- context "when providing no options" do
1138
-
1071
+ context 'when providing no options' do
1139
1072
  before do
1140
1073
  Person.field(:testing, overwrite: true)
1141
1074
  end
1142
1075
 
1143
1076
  let(:person) do
1144
- Person.new(testing: "Test")
1077
+ Person.new(testing: 'Test')
1145
1078
  end
1146
1079
 
1147
- it "adds a reader for the fields defined" do
1148
- expect(person.testing).to eq("Test")
1080
+ it 'adds a reader for the fields defined' do
1081
+ expect(person.testing).to eq('Test')
1149
1082
  end
1150
1083
 
1151
- it "adds a writer for the fields defined" do
1152
- (person.testing = expect("Testy")).to eq("Testy")
1084
+ it 'adds a writer for the fields defined' do
1085
+ (person.testing = expect('Testy')).to eq('Testy')
1153
1086
  end
1154
1087
 
1155
- it "adds an existence method" do
1088
+ it 'adds an existence method' do
1156
1089
  expect(Person.new.testing?).to be false
1157
1090
  end
1158
1091
 
1159
- context "when overwriting an existing field" do
1160
-
1092
+ context 'when overwriting an existing field' do
1161
1093
  before do
1162
1094
  Person.class_eval do
1163
1095
  attr_reader :testing_override_called
1096
+
1164
1097
  def testing=(value)
1165
1098
  @testing_override_called = true
1166
1099
  super
@@ -1169,36 +1102,33 @@ describe Mongoid::Fields do
1169
1102
  person.testing = 'Test'
1170
1103
  end
1171
1104
 
1172
- it "properly overwrites the method" do
1105
+ it 'properly overwrites the method' do
1173
1106
  expect(person.testing_override_called).to be true
1174
1107
  end
1175
1108
  end
1176
1109
  end
1177
1110
 
1178
- context "when the type is an object" do
1179
-
1111
+ context 'when the type is an object' do
1180
1112
  let(:bob) do
1181
1113
  Person.new(reading: 10.023)
1182
1114
  end
1183
1115
 
1184
- it "returns the given value" do
1116
+ it 'returns the given value' do
1185
1117
  expect(bob.reading).to eq(10.023)
1186
1118
  end
1187
1119
  end
1188
1120
 
1189
- context "when type is a boolean" do
1190
-
1121
+ context 'when type is a boolean' do
1191
1122
  let(:person) do
1192
1123
  Person.new(terms: true)
1193
1124
  end
1194
1125
 
1195
- it "adds an accessor method with a question mark" do
1126
+ it 'adds an accessor method with a question mark' do
1196
1127
  expect(person.terms?).to be true
1197
1128
  end
1198
1129
  end
1199
1130
 
1200
- context "when as is specified" do
1201
-
1131
+ context 'when as is specified' do
1202
1132
  let(:person) do
1203
1133
  Person.new(alias: true)
1204
1134
  end
@@ -1207,82 +1137,78 @@ describe Mongoid::Fields do
1207
1137
  Person.field :aliased, as: :alias, type: Mongoid::Boolean, overwrite: true
1208
1138
  end
1209
1139
 
1210
- it "uses the alias to write the attribute" do
1140
+ it 'uses the alias to write the attribute' do
1211
1141
  expect(person.alias = true).to be true
1212
1142
  end
1213
1143
 
1214
- it "uses the alias to read the attribute" do
1144
+ it 'uses the alias to read the attribute' do
1215
1145
  expect(person.alias).to be true
1216
1146
  end
1217
1147
 
1218
- it "uses the alias for the query method" do
1148
+ it 'uses the alias for the query method' do
1219
1149
  expect(person).to be_alias
1220
1150
  end
1221
1151
 
1222
- it "uses the name to write the attribute" do
1152
+ it 'uses the name to write the attribute' do
1223
1153
  expect(person.aliased = true).to be true
1224
1154
  end
1225
1155
 
1226
- it "uses the name to read the attribute" do
1156
+ it 'uses the name to read the attribute' do
1227
1157
  expect(person.aliased).to be true
1228
1158
  end
1229
1159
 
1230
- it "uses the name for the query method" do
1160
+ it 'uses the name for the query method' do
1231
1161
  expect(person).to be_aliased
1232
1162
  end
1233
1163
 
1234
- it "creates dirty methods for the name" do
1164
+ it 'creates dirty methods for the name' do
1235
1165
  expect(person).to respond_to(:aliased_changed?)
1236
1166
  end
1237
1167
 
1238
- it "creates dirty methods for the alias" do
1168
+ it 'creates dirty methods for the alias' do
1239
1169
  expect(person).to respond_to(:alias_changed?)
1240
1170
  end
1241
1171
 
1242
- context "when changing the name" do
1243
-
1172
+ context 'when changing the name' do
1244
1173
  before do
1245
1174
  person.aliased = true
1246
1175
  end
1247
1176
 
1248
- it "sets name_changed?" do
1177
+ it 'sets name_changed?' do
1249
1178
  expect(person.aliased_changed?).to be true
1250
1179
  end
1251
1180
 
1252
- it "sets alias_changed?" do
1181
+ it 'sets alias_changed?' do
1253
1182
  expect(person.alias_changed?).to be true
1254
1183
  end
1255
1184
  end
1256
1185
 
1257
- context "when changing the alias" do
1258
-
1186
+ context 'when changing the alias' do
1259
1187
  before do
1260
1188
  person.alias = true
1261
1189
  end
1262
1190
 
1263
- it "sets name_changed?" do
1191
+ it 'sets name_changed?' do
1264
1192
  expect(person.aliased_changed?).to be true
1265
1193
  end
1266
1194
 
1267
- it "sets alias_changed?" do
1195
+ it 'sets alias_changed?' do
1268
1196
  expect(person.alias_changed?).to be true
1269
1197
  end
1270
1198
  end
1271
1199
 
1272
- context "when defining a criteria" do
1273
-
1200
+ context 'when defining a criteria' do
1274
1201
  let(:criteria) do
1275
- Person.where(alias: "true")
1202
+ Person.where(alias: 'true')
1276
1203
  end
1277
1204
 
1278
- it "properly serializes the aliased field" do
1279
- expect(criteria.selector).to eq({ "aliased" => true })
1205
+ it 'properly serializes the aliased field' do
1206
+ expect(criteria.selector).to eq({ 'aliased' => true })
1280
1207
  end
1281
1208
  end
1282
1209
  end
1283
1210
 
1284
- context "custom options" do
1285
-
1211
+ context 'custom options' do
1286
1212
  let(:handler) do
1287
1213
  proc {}
1288
1214
  end
@@ -1291,78 +1217,72 @@ describe Mongoid::Fields do
1291
1217
  Mongoid::Fields.option :option, &handler
1292
1218
  end
1293
1219
 
1294
- context "when option is provided" do
1295
-
1296
- it "calls the handler with the model" do
1220
+ context 'when option is provided' do
1221
+ it 'calls the handler with the model' do
1297
1222
  User.field :custom, option: true, overwrite: true
1298
- expect(User.fields["custom"].options[:option]).to be_truthy
1223
+ expect(User.fields['custom'].options[:option]).to be_truthy
1299
1224
  end
1300
1225
  end
1301
1226
 
1302
- context "when option is nil" do
1303
-
1304
- it "calls the handler" do
1227
+ context 'when option is nil' do
1228
+ it 'calls the handler' do
1305
1229
  expect(handler).to receive(:call)
1306
1230
  User.field :custom, option: nil, overwrite: true
1307
1231
  end
1308
1232
  end
1309
1233
 
1310
- context "when option is not provided" do
1311
-
1312
- it "does not call the handler" do
1313
- expect(handler).to receive(:call).never
1234
+ context 'when option is not provided' do
1235
+ it 'does not call the handler' do
1236
+ expect(handler).not_to receive(:call)
1314
1237
  User.field :custom, overwrite: true
1315
1238
  end
1316
1239
  end
1317
1240
  end
1318
1241
  end
1319
1242
 
1320
- describe "#fields" do
1321
-
1322
- context "on parent classes" do
1323
-
1243
+ describe '#fields' do
1244
+ context 'on parent classes' do
1324
1245
  let(:shape) do
1325
1246
  Shape.new
1326
1247
  end
1327
1248
 
1328
- it "includes its own fields" do
1329
- expect(shape.fields.keys).to include("x")
1249
+ it 'includes its own fields' do
1250
+ expect(shape.fields.keys).to include('x')
1330
1251
  end
1331
1252
 
1332
- it "does not return subclass fields" do
1333
- expect(shape.fields.keys).to_not include("radius")
1253
+ it 'does not return subclass fields' do
1254
+ expect(shape.fields.keys).not_to include('radius')
1334
1255
  end
1335
1256
 
1336
1257
  it 'includes _type field' do
1337
- expect(shape.fields.keys).to include("_type")
1258
+ expect(shape.fields.keys).to include('_type')
1338
1259
  end
1339
1260
  end
1340
1261
 
1341
- context "on subclasses" do
1342
-
1262
+ context 'on subclasses' do
1343
1263
  let(:circle) do
1344
1264
  Circle.new
1345
1265
  end
1346
1266
 
1347
- it "includes the first parent field" do
1348
- expect(circle.fields.keys).to include("x")
1267
+ it 'includes the first parent field' do
1268
+ expect(circle.fields.keys).to include('x')
1349
1269
  end
1350
1270
 
1351
- it "includes the second parent field" do
1352
- expect(circle.fields.keys).to include("y")
1271
+ it 'includes the second parent field' do
1272
+ expect(circle.fields.keys).to include('y')
1353
1273
  end
1354
1274
 
1355
- it "includes the child fields" do
1356
- expect(circle.fields.keys).to include("radius")
1275
+ it 'includes the child fields' do
1276
+ expect(circle.fields.keys).to include('radius')
1357
1277
  end
1358
1278
 
1359
1279
  it 'includes _type field' do
1360
- expect(circle.fields.keys).to include("_type")
1280
+ expect(circle.fields.keys).to include('_type')
1361
1281
  end
1362
1282
  end
1363
1283
 
1364
- context "on new subclasses" do
1365
- it "all subclasses get the discriminator key" do
1284
+ context 'on new subclasses' do
1285
+ it 'all subclasses get the discriminator key' do
1366
1286
  class DiscriminatorParent
1367
1287
  include Mongoid::Document
1368
1288
  end
@@ -1373,149 +1293,138 @@ describe Mongoid::Fields do
1373
1293
  class DiscriminatorChild2 < DiscriminatorParent
1374
1294
  end
1375
1295
 
1376
- expect(DiscriminatorParent.fields.keys).to include("_type")
1377
- expect(DiscriminatorChild1.fields.keys).to include("_type")
1378
- expect(DiscriminatorChild2.fields.keys).to include("_type")
1296
+ expect(DiscriminatorParent.fields.keys).to include('_type')
1297
+ expect(DiscriminatorChild1.fields.keys).to include('_type')
1298
+ expect(DiscriminatorChild2.fields.keys).to include('_type')
1379
1299
  end
1380
1300
  end
1381
1301
  end
1382
1302
 
1383
- describe ".replace_field" do
1384
-
1303
+ describe '.replace_field' do
1385
1304
  let!(:original) do
1386
- Person.field(:id_test, type: BSON::ObjectId, label: "id")
1305
+ Person.field(:id_test, type: BSON::ObjectId, label: 'id')
1306
+ end
1307
+ let(:new_field) do
1308
+ Person.fields['id_test']
1387
1309
  end
1388
1310
 
1389
1311
  let!(:altered) do
1390
- Person.replace_field("id_test", String)
1312
+ Person.replace_field('id_test', String)
1391
1313
  end
1392
1314
 
1393
1315
  after do
1394
- Person.fields.delete("id_test")
1395
- end
1396
-
1397
- let(:new_field) do
1398
- Person.fields["id_test"]
1316
+ Person.fields.delete('id_test')
1399
1317
  end
1400
1318
 
1401
- it "sets the new type on the field" do
1319
+ it 'sets the new type on the field' do
1402
1320
  expect(new_field.type).to eq(String)
1403
1321
  end
1404
1322
 
1405
- it "keeps the options from the old field" do
1406
- expect(new_field.options[:label]).to eq("id")
1323
+ it 'keeps the options from the old field' do
1324
+ expect(new_field.options[:label]).to eq('id')
1407
1325
  end
1408
1326
  end
1409
1327
 
1410
- context "when sending an include of another module at runtime" do
1411
-
1328
+ context 'when sending an include of another module at runtime' do
1412
1329
  before do
1413
- Basic.send(:include, Ownable)
1330
+ Basic.include Ownable
1414
1331
  end
1415
1332
 
1416
- context "when the class is a parent" do
1417
-
1333
+ context 'when the class is a parent' do
1418
1334
  let(:fields) do
1419
1335
  Basic.fields
1420
1336
  end
1421
1337
 
1422
- it "resets the fields" do
1423
- expect(fields.keys).to include("user_id")
1338
+ it 'resets the fields' do
1339
+ expect(fields.keys).to include('user_id')
1424
1340
  end
1425
1341
  end
1426
1342
 
1427
- context "when the class is a subclass" do
1428
-
1343
+ context 'when the class is a subclass' do
1429
1344
  let(:fields) do
1430
1345
  SubBasic.fields
1431
1346
  end
1432
1347
 
1433
- it "resets the fields" do
1434
- expect(fields.keys).to include("user_id")
1348
+ it 'resets the fields' do
1349
+ expect(fields.keys).to include('user_id')
1435
1350
  end
1436
1351
  end
1437
1352
  end
1438
1353
 
1439
- context "when a setter accesses a field with a default" do
1440
-
1354
+ context 'when a setter accesses a field with a default' do
1441
1355
  let(:person) do
1442
- Person.new(set_on_map_with_default: "testing")
1356
+ Person.new(set_on_map_with_default: 'testing')
1443
1357
  end
1444
1358
 
1445
- it "sets the default value pre process" do
1446
- expect(person.map_with_default).to eq({ "key" => "testing" })
1359
+ it 'sets the default value pre process' do
1360
+ expect(person.map_with_default).to eq({ 'key' => 'testing' })
1447
1361
  end
1448
1362
  end
1449
1363
 
1450
- context "when a field is defined as a big decimal" do
1451
-
1364
+ context 'when a field is defined as a big decimal' do
1452
1365
  context 'when Mongoid.map_big_decimal_to_decimal128 is false' do
1453
1366
  config_override :map_big_decimal_to_decimal128, false
1454
1367
 
1455
1368
  let(:band) do
1456
- Band.new(name: "Tool")
1369
+ Band.new(name: 'Tool')
1457
1370
  end
1458
1371
 
1459
1372
  let(:decimal) do
1460
- BigDecimal("1000000.00")
1373
+ BigDecimal('1000000.00')
1461
1374
  end
1462
1375
 
1463
- context "when setting to a big decimal" do
1464
-
1376
+ context 'when setting to a big decimal' do
1465
1377
  before do
1466
1378
  band.sales = decimal
1467
1379
  end
1468
1380
 
1469
- it "properly persists as a string" do
1470
- expect(band.attributes["sales"]).to eq(decimal.to_s)
1381
+ it 'properly persists as a string' do
1382
+ expect(band.attributes['sales']).to eq(decimal.to_s)
1471
1383
  end
1472
1384
 
1473
- it "returns the proper big decimal" do
1385
+ it 'returns the proper big decimal' do
1474
1386
  expect(band.sales).to eq(decimal)
1475
1387
  end
1476
1388
  end
1477
1389
 
1478
- context "when setting to a string" do
1479
-
1390
+ context 'when setting to a string' do
1480
1391
  before do
1481
1392
  band.sales = decimal.to_s
1482
1393
  end
1483
1394
 
1484
- it "properly persists as a string" do
1485
- expect(band.attributes["sales"]).to eq(decimal.to_s)
1395
+ it 'properly persists as a string' do
1396
+ expect(band.attributes['sales']).to eq(decimal.to_s)
1486
1397
  end
1487
1398
 
1488
- it "returns the proper big decimal" do
1399
+ it 'returns the proper big decimal' do
1489
1400
  expect(band.sales).to eq(decimal)
1490
1401
  end
1491
1402
  end
1492
1403
 
1493
- context "when setting to an integer" do
1494
-
1404
+ context 'when setting to an integer' do
1495
1405
  before do
1496
1406
  band.sales = decimal.to_i
1497
1407
  end
1498
1408
 
1499
- it "properly persists as a string" do
1500
- expect(band.attributes["sales"]).to eq("1000000")
1409
+ it 'properly persists as a string' do
1410
+ expect(band.attributes['sales']).to eq('1000000')
1501
1411
  end
1502
1412
 
1503
- it "returns the proper big decimal" do
1413
+ it 'returns the proper big decimal' do
1504
1414
  expect(band.sales).to eq(decimal)
1505
1415
  end
1506
1416
  end
1507
1417
 
1508
- context "when setting to a float" do
1509
-
1418
+ context 'when setting to a float' do
1510
1419
  before do
1511
1420
  band.sales = decimal.to_f
1512
1421
  end
1513
1422
 
1514
- it "properly persists as a string" do
1515
- expect(band.attributes["sales"]).to eq(decimal.to_s)
1423
+ it 'properly persists as a string' do
1424
+ expect(band.attributes['sales']).to eq(decimal.to_s)
1516
1425
  end
1517
1426
 
1518
- it "returns the proper big decimal" do
1427
+ it 'returns the proper big decimal' do
1519
1428
  expect(band.sales).to eq(decimal)
1520
1429
  end
1521
1430
  end
@@ -1525,120 +1434,113 @@ describe Mongoid::Fields do
1525
1434
  config_override :map_big_decimal_to_decimal128, true
1526
1435
 
1527
1436
  let(:band) do
1528
- Band.new(name: "Tool")
1437
+ Band.new(name: 'Tool')
1529
1438
  end
1530
1439
 
1531
1440
  let(:decimal) do
1532
- BigDecimal("1000000.00")
1441
+ BigDecimal('1000000.00')
1533
1442
  end
1534
1443
 
1535
- context "when setting to a big decimal" do
1536
-
1444
+ context 'when setting to a big decimal' do
1537
1445
  before do
1538
1446
  band.sales = decimal
1539
1447
  end
1540
1448
 
1541
- it "properly persists as a BSON::Decimal128" do
1542
- expect(band.attributes["sales"]).to eq(BSON::Decimal128.new(decimal))
1449
+ it 'properly persists as a BSON::Decimal128' do
1450
+ expect(band.attributes['sales']).to eq(BSON::Decimal128.new(decimal))
1543
1451
  end
1544
1452
 
1545
- it "returns the proper big decimal" do
1453
+ it 'returns the proper big decimal' do
1546
1454
  expect(band.sales).to eq(decimal)
1547
1455
  end
1548
1456
  end
1549
1457
 
1550
- context "when setting to a string" do
1551
-
1458
+ context 'when setting to a string' do
1552
1459
  before do
1553
1460
  band.sales = decimal.to_s
1554
1461
  end
1555
1462
 
1556
- it "persists as a BSON::Decimal128" do
1557
- expect(band.attributes["sales"]).to eq(BSON::Decimal128.new(decimal.to_s))
1463
+ it 'persists as a BSON::Decimal128' do
1464
+ expect(band.attributes['sales']).to eq(BSON::Decimal128.new(decimal.to_s))
1558
1465
  end
1559
1466
 
1560
- it "returns the proper big decimal" do
1467
+ it 'returns the proper big decimal' do
1561
1468
  expect(band.sales).to eq(decimal)
1562
1469
  end
1563
1470
  end
1564
1471
 
1565
- context "when setting to an integer" do
1566
-
1472
+ context 'when setting to an integer' do
1567
1473
  before do
1568
1474
  band.sales = decimal.to_i
1569
1475
  end
1570
1476
 
1571
- it "persists as a BSON::Decimal128" do
1572
- expect(band.attributes["sales"]).to eq(BSON::Decimal128.new(decimal.to_i.to_s))
1477
+ it 'persists as a BSON::Decimal128' do
1478
+ expect(band.attributes['sales']).to eq(BSON::Decimal128.new(decimal.to_i.to_s))
1573
1479
  end
1574
1480
 
1575
- it "returns the proper big decimal" do
1481
+ it 'returns the proper big decimal' do
1576
1482
  expect(band.sales).to eq(decimal)
1577
1483
  end
1578
1484
  end
1579
1485
 
1580
- context "when setting to a float" do
1581
-
1486
+ context 'when setting to a float' do
1582
1487
  before do
1583
1488
  band.sales = decimal.to_f
1584
1489
  end
1585
1490
 
1586
- it "properly persists as a BSON::Decimal128" do
1587
- expect(band.attributes["sales"]).to eq(BSON::Decimal128.new(decimal.to_f.to_s))
1491
+ it 'properly persists as a BSON::Decimal128' do
1492
+ expect(band.attributes['sales']).to eq(BSON::Decimal128.new(decimal.to_f.to_s))
1588
1493
  end
1589
1494
 
1590
- it "returns the proper big decimal" do
1495
+ it 'returns the proper big decimal' do
1591
1496
  expect(band.sales).to eq(decimal)
1592
1497
  end
1593
1498
  end
1594
1499
  end
1595
1500
  end
1596
1501
 
1597
- context "when the field is a hash of arrays" do
1598
-
1502
+ context 'when the field is a hash of arrays' do
1599
1503
  let(:person) do
1600
1504
  Person.create!
1601
1505
  end
1602
1506
 
1603
1507
  let(:map) do
1604
1508
  {
1605
- "stack1" => [ 1, 2, 3, 4 ],
1606
- "stack2" => [ 1, 2, 3, 4 ],
1607
- "stack3" => [ 1, 2, 3, 4 ]
1509
+ 'stack1' => [ 1, 2, 3, 4 ],
1510
+ 'stack2' => [ 1, 2, 3, 4 ],
1511
+ 'stack3' => [ 1, 2, 3, 4 ]
1608
1512
  }
1609
1513
  end
1610
1514
 
1611
1515
  before do
1612
1516
  person.map = map
1613
- person.map["stack1"].reverse!
1517
+ person.map['stack1'].reverse!
1614
1518
  person.save!
1615
1519
  end
1616
1520
 
1617
- it "properly updates the hash" do
1521
+ it 'properly updates the hash' do
1618
1522
  expect(person.map).to eq(
1619
1523
  {
1620
- "stack1" => [ 4, 3, 2, 1 ],
1621
- "stack2" => [ 1, 2, 3, 4 ],
1622
- "stack3" => [ 1, 2, 3, 4 ]
1524
+ 'stack1' => [ 4, 3, 2, 1 ],
1525
+ 'stack2' => [ 1, 2, 3, 4 ],
1526
+ 'stack3' => [ 1, 2, 3, 4 ]
1623
1527
  }
1624
1528
  )
1625
1529
  end
1626
1530
 
1627
- it "persists the changes" do
1531
+ it 'persists the changes' do
1628
1532
  expect(person.reload.map).to eq(
1629
1533
  {
1630
- "stack1" => [ 4, 3, 2, 1 ],
1631
- "stack2" => [ 1, 2, 3, 4 ],
1632
- "stack3" => [ 1, 2, 3, 4 ]
1534
+ 'stack1' => [ 4, 3, 2, 1 ],
1535
+ 'stack2' => [ 1, 2, 3, 4 ],
1536
+ 'stack3' => [ 1, 2, 3, 4 ]
1633
1537
  }
1634
1538
  )
1635
1539
  end
1636
1540
  end
1637
1541
 
1638
- context "when overriding a parent class field" do
1639
-
1640
- context "when the field has a default value" do
1641
-
1542
+ context 'when overriding a parent class field' do
1543
+ context 'when the field has a default value' do
1642
1544
  let!(:canvas) do
1643
1545
  Canvas.new
1644
1546
  end
@@ -1647,25 +1549,23 @@ describe Mongoid::Fields do
1647
1549
  Canvas::Test.new
1648
1550
  end
1649
1551
 
1650
- it "does not override the parent" do
1651
- expect(canvas.foo).to eq("original")
1552
+ it 'does not override the parent' do
1553
+ expect(canvas.foo).to eq('original')
1652
1554
  end
1653
1555
 
1654
- it "overrides the default" do
1655
- expect(test.foo).to eq("overridden")
1556
+ it 'overrides the default' do
1557
+ expect(test.foo).to eq('overridden')
1656
1558
  end
1657
1559
  end
1658
1560
  end
1659
1561
 
1660
- context "when a localized field is a boolean" do
1661
-
1662
- context "when the default is true" do
1663
-
1562
+ context 'when a localized field is a boolean' do
1563
+ context 'when the default is true' do
1664
1564
  let(:definition) do
1665
1565
  Definition.new
1666
1566
  end
1667
1567
 
1668
- it "returns the proper predicate result" do
1568
+ it 'returns the proper predicate result' do
1669
1569
  expect(definition).to be_active
1670
1570
  end
1671
1571
  end
@@ -1676,7 +1576,7 @@ describe Mongoid::Fields do
1676
1576
  let(:shape) { Shape.new }
1677
1577
 
1678
1578
  it 'is correctly set' do
1679
- shape.attributes['_type'].should == 'Shape'
1579
+ shape.attributes['_type'].should eq 'Shape'
1680
1580
  end
1681
1581
  end
1682
1582
 
@@ -1684,98 +1584,114 @@ describe Mongoid::Fields do
1684
1584
  let(:circle) { Circle.new }
1685
1585
 
1686
1586
  it 'is correctly set' do
1687
- circle.attributes['_type'].should == 'Circle'
1587
+ circle.attributes['_type'].should eq 'Circle'
1688
1588
  end
1689
1589
  end
1690
1590
  end
1691
1591
 
1692
1592
  describe '.database_field_name' do
1693
-
1694
1593
  shared_examples_for 'database_field_name' do
1695
1594
  subject { Person.database_field_name(key) }
1696
1595
 
1697
1596
  context 'non-aliased field name' do
1698
1597
  let(:key) { 't' }
1598
+
1699
1599
  it { is_expected.to eq 't' }
1700
1600
  end
1701
1601
 
1702
1602
  context 'aliased field name' do
1703
1603
  let(:key) { 'test' }
1604
+
1704
1605
  it { is_expected.to eq 't' }
1705
1606
  end
1706
1607
 
1707
1608
  context 'non-aliased embeds one relation' do
1708
1609
  let(:key) { 'pass' }
1610
+
1709
1611
  it { is_expected.to eq 'pass' }
1710
1612
  end
1711
1613
 
1712
1614
  context 'aliased embeds one relation' do
1713
1615
  let(:key) { 'passport' }
1616
+
1714
1617
  it { is_expected.to eq 'pass' }
1715
1618
  end
1716
1619
 
1717
1620
  context 'non-aliased embeds many relation' do
1718
1621
  let(:key) { 'mobile_phones' }
1622
+
1719
1623
  it { is_expected.to eq 'mobile_phones' }
1720
1624
  end
1721
1625
 
1722
1626
  context 'aliased embeds many relation' do
1723
1627
  let(:key) { 'phones' }
1628
+
1724
1629
  it { is_expected.to eq 'mobile_phones' }
1725
1630
  end
1726
1631
 
1727
1632
  context 'non-aliased embeds one field' do
1728
1633
  let(:key) { 'pass.exp' }
1634
+
1729
1635
  it { is_expected.to eq 'pass.exp' }
1730
1636
  end
1731
1637
 
1732
1638
  context 'aliased embeds one field' do
1733
1639
  let(:key) { 'passport.expiration_date' }
1640
+
1734
1641
  it { is_expected.to eq 'pass.exp' }
1735
1642
  end
1736
1643
 
1737
1644
  context 'non-aliased embeds many field' do
1738
1645
  let(:key) { 'mobile_phones.landline' }
1646
+
1739
1647
  it { is_expected.to eq 'mobile_phones.landline' }
1740
1648
  end
1741
1649
 
1742
1650
  context 'aliased embeds many field' do
1743
1651
  let(:key) { 'phones.extension' }
1652
+
1744
1653
  it { is_expected.to eq 'mobile_phones.ext' }
1745
1654
  end
1746
1655
 
1747
1656
  context 'aliased multi-level embedded document' do
1748
1657
  let(:key) { 'phones.extension' }
1658
+
1749
1659
  it { is_expected.to eq 'mobile_phones.ext' }
1750
1660
  end
1751
1661
 
1752
1662
  context 'non-aliased multi-level embedded document' do
1753
1663
  let(:key) { 'phones.extension' }
1664
+
1754
1665
  it { is_expected.to eq 'mobile_phones.ext' }
1755
1666
  end
1756
1667
 
1757
1668
  context 'aliased multi-level embedded document field' do
1758
1669
  let(:key) { 'mobile_phones.country_code.code' }
1670
+
1759
1671
  it { is_expected.to eq 'mobile_phones.country_code.code' }
1760
1672
  end
1761
1673
 
1762
1674
  context 'non-aliased multi-level embedded document field' do
1763
1675
  let(:key) { 'phones.country_code.iso_alpha2_code' }
1676
+
1764
1677
  it { is_expected.to eq 'mobile_phones.country_code.iso' }
1765
1678
  end
1766
1679
 
1767
1680
  context 'when field is unknown' do
1768
1681
  let(:key) { 'shenanigans' }
1682
+
1769
1683
  it { is_expected.to eq 'shenanigans' }
1770
1684
  end
1771
1685
 
1772
1686
  context 'when embedded field is unknown' do
1773
1687
  let(:key) { 'phones.bamboozle' }
1688
+
1774
1689
  it { is_expected.to eq 'mobile_phones.bamboozle' }
1775
1690
  end
1776
1691
 
1777
1692
  context 'when multi-level embedded field is unknown' do
1778
1693
  let(:key) { 'phones.bamboozle.brouhaha' }
1694
+
1779
1695
  it { is_expected.to eq 'mobile_phones.bamboozle.brouhaha' }
1780
1696
  end
1781
1697
  end
@@ -1785,322 +1701,537 @@ describe Mongoid::Fields do
1785
1701
 
1786
1702
  context 'non-aliased field name' do
1787
1703
  let(:key) { 't' }
1704
+
1788
1705
  it { is_expected.to eq 't' }
1789
1706
  end
1790
1707
 
1791
1708
  context 'aliased field name' do
1792
1709
  let(:key) { 'test' }
1710
+
1793
1711
  it { is_expected.to eq 't' }
1794
1712
  end
1795
1713
 
1796
1714
  context 'non-aliased embeds one relation' do
1797
1715
  let(:key) { 'pass' }
1716
+
1798
1717
  it { is_expected.to eq 'pass' }
1799
1718
  end
1800
1719
 
1801
1720
  context 'aliased embeds one relation' do
1802
1721
  let(:key) { 'passport' }
1722
+
1803
1723
  it { is_expected.to eq 'pass' }
1804
1724
  end
1805
1725
 
1806
1726
  context 'non-aliased embeds many relation' do
1807
1727
  let(:key) { 'mobile_phones' }
1728
+
1808
1729
  it { is_expected.to eq 'mobile_phones' }
1809
1730
  end
1810
1731
 
1811
1732
  context 'aliased embeds many relation' do
1812
1733
  let(:key) { 'phones' }
1734
+
1813
1735
  it { is_expected.to eq 'mobile_phones' }
1814
1736
  end
1815
1737
 
1816
1738
  context 'non-aliased embeds one field' do
1817
1739
  let(:key) { 'pass.exp' }
1740
+
1818
1741
  it { is_expected.to eq 'pass.exp' }
1819
1742
  end
1820
1743
 
1821
1744
  context 'aliased embeds one field' do
1822
1745
  let(:key) { 'passport.expiration_date' }
1746
+
1823
1747
  it { is_expected.to eq 'passport.expiration_date' }
1824
1748
  end
1825
1749
 
1826
1750
  context 'non-aliased embeds many field' do
1827
1751
  let(:key) { 'mobile_phones.landline' }
1752
+
1828
1753
  it { is_expected.to eq 'mobile_phones.landline' }
1829
1754
  end
1830
1755
 
1831
1756
  context 'aliased embeds many field' do
1832
1757
  let(:key) { 'phones.extension' }
1758
+
1833
1759
  it { is_expected.to eq 'phones.extension' }
1834
1760
  end
1835
1761
 
1836
1762
  context 'aliased multi-level embedded document' do
1837
1763
  let(:key) { 'phones.extension' }
1764
+
1838
1765
  it { is_expected.to eq 'phones.extension' }
1839
1766
  end
1840
1767
 
1841
1768
  context 'non-aliased multi-level embedded document' do
1842
1769
  let(:key) { 'phones.extension' }
1770
+
1843
1771
  it { is_expected.to eq 'phones.extension' }
1844
1772
  end
1845
1773
 
1846
1774
  context 'aliased multi-level embedded document field' do
1847
1775
  let(:key) { 'mobile_phones.country_code.code' }
1776
+
1848
1777
  it { is_expected.to eq 'mobile_phones.country_code.code' }
1849
1778
  end
1850
1779
 
1851
1780
  context 'non-aliased multi-level embedded document field' do
1852
1781
  let(:key) { 'phones.country_code.iso_alpha2_code' }
1782
+
1853
1783
  it { is_expected.to eq 'phones.country_code.iso_alpha2_code' }
1854
1784
  end
1855
1785
 
1856
1786
  context 'when field is unknown' do
1857
1787
  let(:key) { 'shenanigans' }
1788
+
1858
1789
  it { is_expected.to eq 'shenanigans' }
1859
1790
  end
1860
1791
 
1861
1792
  context 'when embedded field is unknown' do
1862
1793
  let(:key) { 'phones.bamboozle' }
1794
+
1863
1795
  it { is_expected.to eq 'phones.bamboozle' }
1864
1796
  end
1865
1797
 
1866
1798
  context 'when multi-level embedded field is unknown' do
1867
1799
  let(:key) { 'phones.bamboozle.brouhaha' }
1800
+
1868
1801
  it { is_expected.to eq 'phones.bamboozle.brouhaha' }
1869
1802
  end
1870
1803
  end
1871
1804
 
1872
1805
  context 'given nil' do
1873
1806
  subject { Person.database_field_name(nil) }
1807
+
1874
1808
  it { is_expected.to eq '' }
1875
1809
  end
1876
1810
 
1877
1811
  context 'given an empty String' do
1878
1812
  subject { Person.database_field_name('') }
1813
+
1879
1814
  it { is_expected.to eq '' }
1880
1815
  end
1881
1816
 
1882
1817
  context 'given a String' do
1883
1818
  subject { Person.database_field_name(key.to_s) }
1819
+
1884
1820
  it_behaves_like 'database_field_name'
1885
1821
  end
1886
1822
 
1887
1823
  context 'given a Symbol' do
1888
1824
  subject { Person.database_field_name(key.to_sym) }
1825
+
1889
1826
  it_behaves_like 'database_field_name'
1890
1827
  end
1891
1828
 
1892
1829
  context 'when getting the database field name of a belongs_to associations' do
1893
-
1894
- context "when the association is the last item" do
1830
+ context 'when the association is the last item' do
1895
1831
  let(:name) do
1896
- Game.database_field_name("person")
1832
+ Game.database_field_name('person')
1897
1833
  end
1898
1834
 
1899
- it "gets the alias" do
1900
- expect(name).to eq("person_id")
1835
+ it 'gets the alias' do
1836
+ expect(name).to eq('person_id')
1901
1837
  end
1902
1838
  end
1903
1839
 
1904
- context "when the association is not the last item" do
1840
+ context 'when the association is not the last item' do
1905
1841
  let(:name) do
1906
- Game.database_field_name("person.name")
1842
+ Game.database_field_name('person.name')
1907
1843
  end
1908
1844
 
1909
- it "gets the alias" do
1910
- expect(name).to eq("person.name")
1845
+ it 'gets the alias' do
1846
+ expect(name).to eq('person.name')
1911
1847
  end
1912
1848
  end
1913
1849
  end
1914
1850
  end
1915
1851
 
1916
- describe "#get_field" do
1917
-
1852
+ describe '#get_field' do
1918
1853
  let(:klass) { Person }
1919
1854
  let(:field) { klass.cleanse_localized_field_names(field_name) }
1920
1855
 
1921
- context "when cleansing a field" do
1922
- let(:field_name) { "employer_id" }
1923
- it "returns the correct field name" do
1856
+ context 'when cleansing a field' do
1857
+ let(:field_name) { 'employer_id' }
1858
+
1859
+ it 'returns the correct field name' do
1924
1860
  expect(field).to eq(field_name)
1925
1861
  end
1926
1862
  end
1927
1863
 
1928
- context "when cleansing a localized field" do
1929
- let(:field_name) { "desc" }
1930
- it "returns the correct field name" do
1864
+ context 'when cleansing a localized field' do
1865
+ let(:field_name) { 'desc' }
1866
+
1867
+ it 'returns the correct field name' do
1931
1868
  expect(field).to eq(field_name)
1932
1869
  end
1933
1870
  end
1934
1871
 
1935
- context "when cleansing a translation field" do
1936
- let(:field_name) { "desc_translations" }
1937
- it "returns the correct field name" do
1938
- expect(field).to eq("desc")
1872
+ context 'when cleansing a translation field' do
1873
+ let(:field_name) { 'desc_translations' }
1874
+
1875
+ it 'returns the correct field name' do
1876
+ expect(field).to eq('desc')
1939
1877
  end
1940
1878
  end
1941
1879
 
1942
- context "when cleansing an existing translation field" do
1943
- let(:field_name) { "localized_translations" }
1944
- it "returns the correct field name" do
1880
+ context 'when cleansing an existing translation field' do
1881
+ let(:field_name) { 'localized_translations' }
1882
+
1883
+ it 'returns the correct field name' do
1945
1884
  expect(field).to eq(field_name)
1946
1885
  end
1947
1886
  end
1948
1887
 
1949
- context "when cleansing an existing translation field with a _translations" do
1950
- let(:field_name) { "localized_translations_translations" }
1951
- it "returns the correct field name" do
1952
- expect(field).to eq("localized_translations")
1888
+ context 'when cleansing an existing translation field with a _translations' do
1889
+ let(:field_name) { 'localized_translations_translations' }
1890
+
1891
+ it 'returns the correct field name' do
1892
+ expect(field).to eq('localized_translations')
1953
1893
  end
1954
1894
  end
1955
1895
 
1956
- context "when cleansing dotted translation field" do
1957
- let(:field_name) { "passport.name_translations.asd" }
1958
- it "returns the correct field name" do
1959
- expect(field).to eq("pass.name.asd")
1896
+ context 'when cleansing dotted translation field' do
1897
+ let(:field_name) { 'passport.name_translations.asd' }
1898
+
1899
+ it 'returns the correct field name' do
1900
+ expect(field).to eq('pass.name.asd')
1960
1901
  end
1961
1902
  end
1962
1903
 
1963
- context "when cleansing dotted translation field as a symbol" do
1964
- let(:field_name) { "passport.name_translations.asd".to_sym }
1965
- it "returns the correct field name" do
1966
- expect(field).to eq("pass.name.asd")
1904
+ context 'when cleansing dotted translation field as a symbol' do
1905
+ let(:field_name) { :'passport.name_translations.asd' }
1906
+
1907
+ it 'returns the correct field name' do
1908
+ expect(field).to eq('pass.name.asd')
1967
1909
  end
1968
1910
  end
1969
1911
 
1970
- context "when cleansing dotted existing translation field" do
1971
- let(:field_name) { "passport.localized_translations.asd" }
1972
- it "returns the correct field name" do
1973
- expect(field).to eq("pass.localized_translations.asd")
1912
+ context 'when cleansing dotted existing translation field' do
1913
+ let(:field_name) { 'passport.localized_translations.asd' }
1914
+
1915
+ it 'returns the correct field name' do
1916
+ expect(field).to eq('pass.localized_translations.asd')
1974
1917
  end
1975
1918
  end
1976
1919
 
1977
- context "when cleansing aliased dotted translation field" do
1978
- let(:field_name) { "pass.name_translations.asd" }
1979
- it "returns the correct field name" do
1980
- expect(field).to eq("pass.name.asd")
1920
+ context 'when cleansing aliased dotted translation field' do
1921
+ let(:field_name) { 'pass.name_translations.asd' }
1922
+
1923
+ it 'returns the correct field name' do
1924
+ expect(field).to eq('pass.name.asd')
1981
1925
  end
1982
1926
  end
1983
1927
  end
1984
1928
 
1985
- describe "localize: :present" do
1986
-
1929
+ describe 'localize: :present' do
1987
1930
  let(:product) do
1988
1931
  Product.new
1989
1932
  end
1990
1933
 
1991
- context "when assigning a non blank value" do
1992
-
1934
+ context 'when assigning a non blank value' do
1993
1935
  before do
1994
- product.title = "hello"
1936
+ product.title = 'hello'
1995
1937
  end
1996
1938
 
1997
- it "assigns the value" do
1998
- expect(product.title).to eq("hello")
1939
+ it 'assigns the value' do
1940
+ expect(product.title).to eq('hello')
1999
1941
  end
2000
1942
 
2001
- it "populates the translations hash" do
2002
- expect(product.title_translations).to eq({ "en" => "hello" })
1943
+ it 'populates the translations hash' do
1944
+ expect(product.title_translations).to eq({ 'en' => 'hello' })
2003
1945
  end
2004
1946
  end
2005
1947
 
2006
- context "when assigning an empty string" do
1948
+ context 'when assigning an empty string' do
2007
1949
  with_default_i18n_configs
2008
1950
 
2009
1951
  before do
2010
1952
  I18n.locale = :en
2011
- product.title = "hello"
1953
+ product.title = 'hello'
2012
1954
  I18n.locale = :de
2013
- product.title = "hello there!"
2014
- product.title = ""
1955
+ product.title = 'hello there!'
1956
+ product.title = ''
2015
1957
  end
2016
1958
 
2017
- it "assigns the value" do
1959
+ it 'assigns the value' do
2018
1960
  expect(product.title).to eq(nil)
2019
1961
  end
2020
1962
 
2021
- it "populates the translations hash" do
2022
- expect(product.title_translations).to eq({ "en" => "hello" })
1963
+ it 'populates the translations hash' do
1964
+ expect(product.title_translations).to eq({ 'en' => 'hello' })
2023
1965
  end
2024
1966
  end
2025
1967
 
2026
- context "when assigning nil" do
1968
+ context 'when assigning nil' do
2027
1969
  with_default_i18n_configs
2028
1970
 
2029
1971
  before do
2030
1972
  I18n.locale = :en
2031
- product.title = "hello"
1973
+ product.title = 'hello'
2032
1974
  I18n.locale = :de
2033
- product.title = "hello there!"
1975
+ product.title = 'hello there!'
2034
1976
  product.title = nil
2035
1977
  end
2036
1978
 
2037
- it "assigns the value" do
1979
+ it 'assigns the value' do
2038
1980
  expect(product.title).to eq(nil)
2039
1981
  end
2040
1982
 
2041
- it "populates the translations hash" do
2042
- expect(product.title_translations).to eq({ "en" => "hello" })
1983
+ it 'populates the translations hash' do
1984
+ expect(product.title_translations).to eq({ 'en' => 'hello' })
2043
1985
  end
2044
1986
  end
2045
1987
 
2046
- context "when assigning an empty array" do
1988
+ context 'when assigning an empty array' do
2047
1989
  with_default_i18n_configs
2048
1990
 
2049
1991
  before do
2050
1992
  I18n.locale = :en
2051
- product.title = "hello"
1993
+ product.title = 'hello'
2052
1994
  I18n.locale = :de
2053
- product.title = "hello there!"
1995
+ product.title = 'hello there!'
2054
1996
  product.title = []
2055
1997
  end
2056
1998
 
2057
- it "assigns the value" do
1999
+ it 'assigns the value' do
2058
2000
  expect(product.title).to eq(nil)
2059
2001
  end
2060
2002
 
2061
- it "populates the translations hash" do
2062
- expect(product.title_translations).to eq({ "en" => "hello" })
2003
+ it 'populates the translations hash' do
2004
+ expect(product.title_translations).to eq({ 'en' => 'hello' })
2063
2005
  end
2064
2006
  end
2065
2007
 
2066
- context "when assigning an empty string first" do
2008
+ context 'when assigning an empty string first' do
2067
2009
  with_default_i18n_configs
2068
2010
 
2069
2011
  before do
2070
- product.title = ""
2012
+ product.title = ''
2071
2013
  end
2072
2014
 
2073
- it "assigns the value" do
2015
+ it 'assigns the value' do
2074
2016
  expect(product.title).to eq(nil)
2075
2017
  end
2076
2018
 
2077
- it "populates the translations hash" do
2019
+ it 'populates the translations hash' do
2078
2020
  expect(product.title_translations).to eq({})
2079
2021
  end
2080
2022
  end
2081
2023
 
2082
- context "when assigning an empty string with only one translation" do
2024
+ context 'when assigning an empty string with only one translation' do
2083
2025
  with_default_i18n_configs
2084
2026
 
2085
2027
  before do
2086
- product.title = "Hello"
2087
- product.title = ""
2028
+ product.title = 'Hello'
2029
+ product.title = ''
2088
2030
  product.save!
2089
2031
  end
2090
2032
 
2091
2033
  let(:from_db) { Product.first }
2092
2034
 
2093
- it "assigns the value" do
2035
+ it 'assigns the value' do
2094
2036
  expect(product.title).to eq(nil)
2095
2037
  end
2096
2038
 
2097
- it "populates the translations hash" do
2039
+ it 'populates the translations hash' do
2098
2040
  expect(product.title_translations).to eq({})
2099
2041
  end
2100
2042
 
2101
- it "round trips an empty hash" do
2043
+ it 'round trips an empty hash' do
2102
2044
  expect(from_db.title_translations).to eq({})
2103
2045
  end
2104
2046
  end
2105
2047
  end
2048
+
2049
+ describe '.vector_field' do
2050
+ let(:model) do
2051
+ Class.new do
2052
+ include Mongoid::Document
2053
+
2054
+ store_in collection: BSON::ObjectId.new.to_s
2055
+ vector_field :embedding, dimensions: 1536
2056
+ end
2057
+ end
2058
+
2059
+ it 'defines an Array field with the given name' do
2060
+ expect(model.fields['embedding'].type).to eq(Array)
2061
+ end
2062
+
2063
+ it 'adds a vector search index spec' do
2064
+ expect(model.search_index_specs).to eq [
2065
+ {
2066
+ type: 'vectorSearch',
2067
+ definition: {
2068
+ fields: [ {
2069
+ type: 'vector',
2070
+ path: 'embedding',
2071
+ numDimensions: 1536,
2072
+ similarity: 'cosine'
2073
+ } ]
2074
+ }
2075
+ }
2076
+ ]
2077
+ end
2078
+
2079
+ it 'adds a vector_search_score field' do
2080
+ expect(model.fields).to have_key('vector_search_score')
2081
+ end
2082
+
2083
+ context 'with a custom similarity' do
2084
+ let(:model) do
2085
+ Class.new do
2086
+ include Mongoid::Document
2087
+
2088
+ store_in collection: BSON::ObjectId.new.to_s
2089
+ vector_field :embedding, dimensions: 768, similarity: 'dotProduct'
2090
+ end
2091
+ end
2092
+
2093
+ it 'uses the given similarity in the index spec' do
2094
+ field_spec = model.search_index_specs.first.dig(:definition, :fields).first
2095
+ expect(field_spec[:similarity]).to eq('dotProduct')
2096
+ end
2097
+ end
2098
+
2099
+ context 'with an explicit index name' do
2100
+ let(:model) do
2101
+ Class.new do
2102
+ include Mongoid::Document
2103
+
2104
+ store_in collection: BSON::ObjectId.new.to_s
2105
+ vector_field :embedding, dimensions: 1536, index: :article_vectors
2106
+ end
2107
+ end
2108
+
2109
+ it 'names the index spec accordingly' do
2110
+ expect(model.search_index_specs.first[:name]).to eq('article_vectors')
2111
+ end
2112
+ end
2113
+
2114
+ context 'when dimensions is omitted' do
2115
+ it 'raises ArgumentError' do
2116
+ expect do
2117
+ Class.new do
2118
+ include Mongoid::Document
2119
+
2120
+ vector_field :embedding
2121
+ end
2122
+ end.to raise_error(ArgumentError, /dimensions/)
2123
+ end
2124
+ end
2125
+ end
2126
+
2127
+ describe '.auto_embed_field' do
2128
+ let(:model) do
2129
+ Class.new do
2130
+ include Mongoid::Document
2131
+
2132
+ store_in collection: BSON::ObjectId.new.to_s
2133
+ auto_embed_field :description, model: 'voyage-4'
2134
+ end
2135
+ end
2136
+
2137
+ it 'registers a vectorSearch index spec with autoEmbed type' do
2138
+ expect(model.search_index_specs).to eq [
2139
+ {
2140
+ type: 'vectorSearch',
2141
+ definition: {
2142
+ fields: [
2143
+ { type: 'autoEmbed', modality: 'text', path: 'description', model: 'voyage-4' }
2144
+ ]
2145
+ }
2146
+ }
2147
+ ]
2148
+ end
2149
+
2150
+ it 'adds a vector_search_score field' do
2151
+ expect(model.fields).to have_key('vector_search_score')
2152
+ end
2153
+
2154
+ it 'defines a String field for the named attribute' do
2155
+ expect(model.fields['description'].type).to eq String
2156
+ end
2157
+
2158
+ context 'with optional numDimensions' do
2159
+ let(:model) do
2160
+ Class.new do
2161
+ include Mongoid::Document
2162
+
2163
+ store_in collection: BSON::ObjectId.new.to_s
2164
+ auto_embed_field :description, model: 'voyage-4', num_dimensions: 512
2165
+ end
2166
+ end
2167
+
2168
+ it 'includes numDimensions in the field spec' do
2169
+ field_spec = model.search_index_specs.first.dig(:definition, :fields).first
2170
+ expect(field_spec[:numDimensions]).to eq 512
2171
+ end
2172
+ end
2173
+
2174
+ context 'with optional quantization' do
2175
+ let(:model) do
2176
+ Class.new do
2177
+ include Mongoid::Document
2178
+
2179
+ store_in collection: BSON::ObjectId.new.to_s
2180
+ auto_embed_field :description, model: 'voyage-4', quantization: 'binary'
2181
+ end
2182
+ end
2183
+
2184
+ it 'includes quantization in the field spec' do
2185
+ field_spec = model.search_index_specs.first.dig(:definition, :fields).first
2186
+ expect(field_spec[:quantization]).to eq 'binary'
2187
+ end
2188
+ end
2189
+
2190
+ context 'with optional similarity' do
2191
+ let(:model) do
2192
+ Class.new do
2193
+ include Mongoid::Document
2194
+
2195
+ store_in collection: BSON::ObjectId.new.to_s
2196
+ auto_embed_field :description, model: 'voyage-4', similarity: 'cosine'
2197
+ end
2198
+ end
2199
+
2200
+ it 'includes similarity in the field spec' do
2201
+ field_spec = model.search_index_specs.first.dig(:definition, :fields).first
2202
+ expect(field_spec[:similarity]).to eq 'cosine'
2203
+ end
2204
+ end
2205
+
2206
+ context 'with a named index' do
2207
+ let(:model) do
2208
+ Class.new do
2209
+ include Mongoid::Document
2210
+
2211
+ store_in collection: BSON::ObjectId.new.to_s
2212
+ auto_embed_field :description, model: 'voyage-4', index: :article_embed
2213
+ end
2214
+ end
2215
+
2216
+ it 'registers the index under the given name' do
2217
+ expect(model.search_index_specs.first[:name]).to eq 'article_embed'
2218
+ end
2219
+ end
2220
+
2221
+ context 'when model: is omitted' do
2222
+ let(:model) do
2223
+ Class.new do
2224
+ include Mongoid::Document
2225
+
2226
+ store_in collection: BSON::ObjectId.new.to_s
2227
+ auto_embed_field :description
2228
+ end
2229
+ end
2230
+
2231
+ it 'defaults model to voyage-4' do
2232
+ field_spec = model.search_index_specs.first.dig(:definition, :fields).first
2233
+ expect(field_spec[:model]).to eq 'voyage-4'
2234
+ end
2235
+ end
2236
+ end
2106
2237
  end