mongoid 7.0.13 → 9.1.1

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 (1421) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +41 -41
  3. data/LICENSE +1 -1
  4. data/README.md +29 -25
  5. data/Rakefile +150 -40
  6. data/lib/config/locales/en.yml +364 -108
  7. data/lib/mongoid/association/accessors.rb +205 -107
  8. data/lib/mongoid/association/bindable.rb +102 -56
  9. data/lib/mongoid/association/builders.rb +24 -26
  10. data/lib/mongoid/association/constrainable.rb +8 -11
  11. data/lib/mongoid/association/depending.rb +65 -59
  12. data/lib/mongoid/association/eager.rb +203 -0
  13. data/lib/mongoid/association/eager_load/discriminated_inclusion.rb +88 -0
  14. data/lib/mongoid/association/eager_load/embedded_distributor.rb +151 -0
  15. data/lib/mongoid/association/eager_load/inclusion.rb +173 -0
  16. data/lib/mongoid/association/eager_load/inclusion_tree.rb +96 -0
  17. data/lib/mongoid/association/eager_load/lookup_pipeline.rb +121 -0
  18. data/lib/mongoid/association/eager_load/polymorphic_preloader.rb +61 -0
  19. data/lib/mongoid/association/eager_load/polymorphic_targets.rb +151 -0
  20. data/lib/mongoid/association/eager_loadable.rb +135 -15
  21. data/lib/mongoid/association/embedded/batchable.rb +87 -75
  22. data/lib/mongoid/association/embedded/cyclic.rb +16 -25
  23. data/lib/mongoid/association/embedded/eager.rb +18 -0
  24. data/lib/mongoid/association/embedded/embedded_in/binding.rb +30 -14
  25. data/lib/mongoid/association/embedded/embedded_in/buildable.rb +10 -7
  26. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +39 -32
  27. data/lib/mongoid/association/embedded/embedded_in.rb +37 -46
  28. data/lib/mongoid/association/embedded/embeds_many/binding.rb +7 -11
  29. data/lib/mongoid/association/embedded/embeds_many/buildable.rb +11 -7
  30. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +309 -243
  31. data/lib/mongoid/association/embedded/embeds_many.rb +32 -56
  32. data/lib/mongoid/association/embedded/embeds_one/binding.rb +6 -11
  33. data/lib/mongoid/association/embedded/embeds_one/buildable.rb +25 -11
  34. data/lib/mongoid/association/embedded/embeds_one/proxy.rb +145 -53
  35. data/lib/mongoid/association/embedded/embeds_one.rb +30 -50
  36. data/lib/mongoid/association/embedded.rb +3 -0
  37. data/lib/mongoid/association/macros.rb +97 -56
  38. data/lib/mongoid/association/many.rb +99 -36
  39. data/lib/mongoid/association/marshalable.rb +5 -7
  40. data/lib/mongoid/association/nested/many.rb +86 -47
  41. data/lib/mongoid/association/nested/nested_buildable.rb +58 -21
  42. data/lib/mongoid/association/nested/one.rb +81 -36
  43. data/lib/mongoid/association/nested.rb +4 -5
  44. data/lib/mongoid/association/one.rb +5 -14
  45. data/lib/mongoid/association/options.rb +48 -53
  46. data/lib/mongoid/association/proxy.rb +65 -54
  47. data/lib/mongoid/association/referenced/auto_save.rb +59 -20
  48. data/lib/mongoid/association/referenced/belongs_to/binding.rb +21 -22
  49. data/lib/mongoid/association/referenced/belongs_to/buildable.rb +32 -11
  50. data/lib/mongoid/association/referenced/belongs_to/eager.rb +47 -6
  51. data/lib/mongoid/association/referenced/belongs_to/proxy.rb +35 -42
  52. data/lib/mongoid/association/referenced/belongs_to.rb +77 -77
  53. data/lib/mongoid/association/referenced/counter_cache.rb +24 -35
  54. data/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb +26 -15
  55. data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +12 -7
  56. data/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb +3 -4
  57. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +202 -133
  58. data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +81 -83
  59. data/lib/mongoid/association/referenced/has_many/binding.rb +3 -7
  60. data/lib/mongoid/association/referenced/has_many/buildable.rb +15 -8
  61. data/lib/mongoid/association/referenced/has_many/eager.rb +3 -4
  62. data/lib/mongoid/association/referenced/has_many/enumerable.rb +601 -456
  63. data/lib/mongoid/association/referenced/has_many/proxy.rb +344 -216
  64. data/lib/mongoid/association/referenced/has_many.rb +58 -74
  65. data/lib/mongoid/association/referenced/has_many_through/eager.rb +99 -0
  66. data/lib/mongoid/association/referenced/has_many_through/proxy.rb +68 -0
  67. data/lib/mongoid/association/referenced/has_many_through.rb +201 -0
  68. data/lib/mongoid/association/referenced/has_one/binding.rb +4 -10
  69. data/lib/mongoid/association/referenced/has_one/buildable.rb +29 -17
  70. data/lib/mongoid/association/referenced/has_one/eager.rb +4 -6
  71. data/lib/mongoid/association/referenced/has_one/proxy.rb +51 -47
  72. data/lib/mongoid/association/referenced/has_one.rb +45 -55
  73. data/lib/mongoid/association/referenced/has_one_through/eager.rb +65 -0
  74. data/lib/mongoid/association/referenced/has_one_through/proxy.rb +30 -0
  75. data/lib/mongoid/association/referenced/has_one_through.rb +167 -0
  76. data/lib/mongoid/association/referenced/syncable.rb +44 -62
  77. data/lib/mongoid/association/referenced/with_polymorphic_criteria.rb +41 -0
  78. data/lib/mongoid/association/referenced.rb +4 -0
  79. data/lib/mongoid/association/reflections.rb +11 -12
  80. data/lib/mongoid/association/relatable.rb +173 -146
  81. data/lib/mongoid/association.rb +35 -40
  82. data/lib/mongoid/atomic/modifiers.rb +91 -74
  83. data/lib/mongoid/atomic/paths/embedded/many.rb +25 -11
  84. data/lib/mongoid/atomic/paths/embedded/one.rb +6 -10
  85. data/lib/mongoid/atomic/paths/embedded.rb +5 -8
  86. data/lib/mongoid/atomic/paths/root.rb +5 -10
  87. data/lib/mongoid/atomic/paths.rb +4 -3
  88. data/lib/mongoid/atomic.rb +41 -89
  89. data/lib/mongoid/atomic_update_preparer.rb +89 -0
  90. data/lib/mongoid/attributes/dynamic.rb +12 -27
  91. data/lib/mongoid/attributes/embedded.rb +34 -0
  92. data/lib/mongoid/attributes/nested.rb +22 -25
  93. data/lib/mongoid/attributes/processing.rb +51 -34
  94. data/lib/mongoid/attributes/projector.rb +107 -0
  95. data/lib/mongoid/attributes/readonly.rb +15 -17
  96. data/lib/mongoid/attributes.rb +119 -126
  97. data/lib/mongoid/cacheable.rb +30 -14
  98. data/lib/mongoid/changeable.rb +196 -99
  99. data/lib/mongoid/clients/factory.rb +73 -17
  100. data/lib/mongoid/clients/options.rb +140 -19
  101. data/lib/mongoid/clients/sessions.rb +239 -87
  102. data/lib/mongoid/clients/storage_options.rb +42 -17
  103. data/lib/mongoid/clients/validators/storage.rb +6 -25
  104. data/lib/mongoid/clients/validators.rb +3 -2
  105. data/lib/mongoid/clients.rb +44 -22
  106. data/lib/mongoid/collection_configurable.rb +63 -0
  107. data/lib/mongoid/composable.rb +40 -39
  108. data/lib/mongoid/config/defaults.rb +46 -0
  109. data/lib/mongoid/config/encryption.rb +228 -0
  110. data/lib/mongoid/config/environment.rb +26 -13
  111. data/lib/mongoid/config/introspection.rb +150 -0
  112. data/lib/mongoid/config/options.rb +37 -26
  113. data/lib/mongoid/config/validators/async_query_executor.rb +31 -0
  114. data/lib/mongoid/config/validators/client.rb +20 -37
  115. data/lib/mongoid/config/validators/option.rb +5 -7
  116. data/lib/mongoid/config/validators.rb +5 -3
  117. data/lib/mongoid/config.rb +418 -60
  118. data/lib/mongoid/contextual/aggregable/memory.rb +45 -28
  119. data/lib/mongoid/contextual/aggregable/mongo.rb +49 -47
  120. data/lib/mongoid/contextual/aggregable/none.rb +64 -0
  121. data/lib/mongoid/contextual/aggregable.rb +16 -0
  122. data/lib/mongoid/contextual/atomic.rb +126 -45
  123. data/lib/mongoid/contextual/command.rb +3 -6
  124. data/lib/mongoid/contextual/map_reduce.rb +23 -49
  125. data/lib/mongoid/contextual/memory.rb +448 -121
  126. data/lib/mongoid/contextual/mongo/documents_loader.rb +177 -0
  127. data/lib/mongoid/contextual/mongo.rb +707 -351
  128. data/lib/mongoid/contextual/none.rb +249 -33
  129. data/lib/mongoid/contextual/queryable.rb +6 -6
  130. data/lib/mongoid/contextual.rb +40 -15
  131. data/lib/mongoid/copyable.rb +45 -21
  132. data/lib/mongoid/criteria/findable.rb +65 -27
  133. data/lib/mongoid/criteria/includable.rb +97 -45
  134. data/lib/mongoid/criteria/inspectable.rb +6 -6
  135. data/lib/mongoid/criteria/marshalable.rb +15 -10
  136. data/lib/mongoid/criteria/modifiable.rb +8 -28
  137. data/lib/mongoid/criteria/options.rb +2 -5
  138. data/lib/mongoid/criteria/permission.rb +40 -37
  139. data/lib/mongoid/criteria/queryable/aggregable.rb +13 -24
  140. data/lib/mongoid/criteria/queryable/expandable.rb +64 -0
  141. data/lib/mongoid/criteria/queryable/extensions/array.rb +14 -50
  142. data/lib/mongoid/criteria/queryable/extensions/big_decimal.rb +29 -13
  143. data/lib/mongoid/criteria/queryable/extensions/boolean.rb +6 -9
  144. data/lib/mongoid/criteria/queryable/extensions/date.rb +13 -16
  145. data/lib/mongoid/criteria/queryable/extensions/date_time.rb +14 -18
  146. data/lib/mongoid/criteria/queryable/extensions/hash.rb +5 -44
  147. data/lib/mongoid/criteria/queryable/extensions/nil_class.rb +23 -24
  148. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +31 -29
  149. data/lib/mongoid/criteria/queryable/extensions/object.rb +15 -37
  150. data/lib/mongoid/criteria/queryable/extensions/range.rb +52 -22
  151. data/lib/mongoid/criteria/queryable/extensions/regexp.rb +22 -25
  152. data/lib/mongoid/criteria/queryable/extensions/set.rb +8 -10
  153. data/lib/mongoid/criteria/queryable/extensions/string.rb +24 -41
  154. data/lib/mongoid/criteria/queryable/extensions/symbol.rb +10 -28
  155. data/lib/mongoid/criteria/queryable/extensions/time.rb +11 -14
  156. data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +12 -20
  157. data/lib/mongoid/criteria/queryable/extensions.rb +19 -24
  158. data/lib/mongoid/criteria/queryable/key.rb +49 -30
  159. data/lib/mongoid/criteria/queryable/macroable.rb +3 -6
  160. data/lib/mongoid/criteria/queryable/mergeable.rb +203 -46
  161. data/lib/mongoid/criteria/queryable/optional.rb +32 -81
  162. data/lib/mongoid/criteria/queryable/options.rb +24 -29
  163. data/lib/mongoid/criteria/queryable/pipeline.rb +11 -23
  164. data/lib/mongoid/criteria/queryable/selectable.rb +558 -242
  165. data/lib/mongoid/criteria/queryable/selector.rb +134 -43
  166. data/lib/mongoid/criteria/queryable/smash.rb +41 -21
  167. data/lib/mongoid/criteria/queryable/storable.rb +219 -0
  168. data/lib/mongoid/criteria/queryable.rb +41 -27
  169. data/lib/mongoid/criteria/scopable.rb +37 -30
  170. data/lib/mongoid/criteria/translator.rb +43 -0
  171. data/lib/mongoid/criteria.rb +257 -152
  172. data/lib/mongoid/deprecable.rb +78 -0
  173. data/lib/mongoid/deprecation.rb +25 -0
  174. data/lib/mongoid/document.rb +277 -171
  175. data/lib/mongoid/encryptable.rb +98 -0
  176. data/lib/mongoid/equality.rb +29 -31
  177. data/lib/mongoid/errors/ambiguous_relationship.rb +6 -9
  178. data/lib/mongoid/errors/attribute_not_loaded.rb +31 -0
  179. data/lib/mongoid/errors/callback.rb +3 -6
  180. data/lib/mongoid/errors/config_redactor.rb +43 -0
  181. data/lib/mongoid/errors/create_collection_failure.rb +31 -0
  182. data/lib/mongoid/errors/criteria_argument_required.rb +16 -0
  183. data/lib/mongoid/errors/delete_restriction.rb +11 -16
  184. data/lib/mongoid/errors/document_not_destroyed.rb +5 -10
  185. data/lib/mongoid/errors/document_not_found.rb +37 -27
  186. data/lib/mongoid/errors/drop_collection_failure.rb +25 -0
  187. data/lib/mongoid/errors/empty_config_file.rb +23 -0
  188. data/lib/mongoid/errors/immutable_attribute.rb +24 -0
  189. data/lib/mongoid/errors/in_memory_collation_not_supported.rb +3 -6
  190. data/lib/mongoid/errors/in_memory_regexp_timeout.rb +26 -0
  191. data/lib/mongoid/errors/invalid_around_callback.rb +16 -0
  192. data/lib/mongoid/errors/invalid_async_query_executor.rb +23 -0
  193. data/lib/mongoid/errors/invalid_auto_encryption_configuration.rb +30 -0
  194. data/lib/mongoid/errors/invalid_collection.rb +3 -3
  195. data/lib/mongoid/errors/invalid_config_file.rb +23 -0
  196. data/lib/mongoid/errors/invalid_config_option.rb +5 -8
  197. data/lib/mongoid/errors/invalid_dependent_strategy.rb +9 -13
  198. data/lib/mongoid/errors/invalid_discriminator_key_target.rb +23 -0
  199. data/lib/mongoid/errors/invalid_dot_dollar_assignment.rb +21 -0
  200. data/lib/mongoid/errors/invalid_elem_match_operator.rb +29 -0
  201. data/lib/mongoid/errors/invalid_estimated_count_criteria.rb +24 -0
  202. data/lib/mongoid/errors/invalid_estimated_count_scoping.rb +24 -0
  203. data/lib/mongoid/errors/invalid_expression_operator.rb +24 -0
  204. data/lib/mongoid/errors/invalid_field.rb +10 -11
  205. data/lib/mongoid/errors/invalid_field_operator.rb +29 -0
  206. data/lib/mongoid/errors/invalid_field_option.rb +4 -7
  207. data/lib/mongoid/errors/invalid_field_type.rb +24 -0
  208. data/lib/mongoid/errors/invalid_find.rb +3 -6
  209. data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +20 -0
  210. data/lib/mongoid/errors/invalid_includes.rb +5 -8
  211. data/lib/mongoid/errors/invalid_index.rb +3 -6
  212. data/lib/mongoid/errors/invalid_options.rb +5 -8
  213. data/lib/mongoid/errors/invalid_path.rb +3 -6
  214. data/lib/mongoid/errors/invalid_persistence_option.rb +6 -11
  215. data/lib/mongoid/errors/invalid_query.rb +36 -0
  216. data/lib/mongoid/errors/invalid_relation.rb +15 -22
  217. data/lib/mongoid/errors/invalid_relation_option.rb +8 -11
  218. data/lib/mongoid/errors/invalid_scope.rb +3 -6
  219. data/lib/mongoid/errors/invalid_session_nesting.rb +14 -0
  220. data/lib/mongoid/errors/invalid_set_polymorphic_relation.rb +8 -9
  221. data/lib/mongoid/errors/invalid_storage_options.rb +4 -7
  222. data/lib/mongoid/errors/invalid_time.rb +3 -6
  223. data/lib/mongoid/errors/invalid_transaction_nesting.rb +14 -0
  224. data/lib/mongoid/errors/inverse_not_found.rb +4 -7
  225. data/lib/mongoid/errors/mixed_client_configuration.rb +4 -7
  226. data/lib/mongoid/errors/mixed_relations.rb +3 -5
  227. data/lib/mongoid/errors/mongoid_error.rb +13 -23
  228. data/lib/mongoid/errors/nested_attributes_metadata_not_found.rb +4 -7
  229. data/lib/mongoid/errors/no_client_config.rb +5 -8
  230. data/lib/mongoid/errors/no_client_database.rb +5 -8
  231. data/lib/mongoid/errors/no_client_hosts.rb +5 -8
  232. data/lib/mongoid/errors/no_clients_config.rb +3 -6
  233. data/lib/mongoid/errors/no_default_client.rb +4 -7
  234. data/lib/mongoid/errors/no_encryption_schema.rb +28 -0
  235. data/lib/mongoid/errors/no_environment.rb +3 -6
  236. data/lib/mongoid/errors/no_map_reduce_output.rb +3 -6
  237. data/lib/mongoid/errors/no_metadata.rb +3 -6
  238. data/lib/mongoid/errors/no_parent.rb +3 -6
  239. data/lib/mongoid/errors/readonly_association.rb +29 -0
  240. data/lib/mongoid/errors/readonly_attribute.rb +4 -7
  241. data/lib/mongoid/errors/readonly_document.rb +4 -9
  242. data/lib/mongoid/errors/rollback.rb +13 -0
  243. data/lib/mongoid/errors/scope_overwrite.rb +4 -4
  244. data/lib/mongoid/errors/sessions_not_supported.rb +14 -0
  245. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +4 -3
  246. data/lib/mongoid/errors/transaction_error.rb +22 -0
  247. data/lib/mongoid/errors/transactions_not_supported.rb +14 -0
  248. data/lib/mongoid/errors/unknown_attribute.rb +4 -7
  249. data/lib/mongoid/errors/unknown_model.rb +3 -6
  250. data/lib/mongoid/errors/unrecognized_model_alias.rb +53 -0
  251. data/lib/mongoid/errors/unrecognized_resolver.rb +27 -0
  252. data/lib/mongoid/errors/unregistered_class.rb +47 -0
  253. data/lib/mongoid/errors/unsaved_document.rb +4 -4
  254. data/lib/mongoid/errors/unsupported_isolation_level.rb +22 -0
  255. data/lib/mongoid/errors/unsupported_javascript.rb +3 -6
  256. data/lib/mongoid/errors/validations.rb +5 -5
  257. data/lib/mongoid/errors.rb +82 -51
  258. data/lib/mongoid/evolvable.rb +4 -7
  259. data/lib/mongoid/extensions/array.rb +32 -55
  260. data/lib/mongoid/extensions/big_decimal.rb +45 -24
  261. data/lib/mongoid/extensions/binary.rb +42 -0
  262. data/lib/mongoid/extensions/boolean.rb +12 -8
  263. data/lib/mongoid/extensions/bson_document.rb +28 -0
  264. data/lib/mongoid/extensions/date.rb +38 -30
  265. data/lib/mongoid/extensions/date_time.rb +8 -22
  266. data/lib/mongoid/extensions/decimal128.rb +4 -8
  267. data/lib/mongoid/extensions/false_class.rb +11 -13
  268. data/lib/mongoid/extensions/float.rb +15 -19
  269. data/lib/mongoid/extensions/hash.rb +31 -116
  270. data/lib/mongoid/extensions/integer.rb +17 -21
  271. data/lib/mongoid/extensions/module.rb +6 -7
  272. data/lib/mongoid/extensions/nil_class.rb +6 -7
  273. data/lib/mongoid/extensions/object.rb +42 -84
  274. data/lib/mongoid/extensions/object_id.rb +5 -11
  275. data/lib/mongoid/extensions/range.rb +49 -25
  276. data/lib/mongoid/extensions/raw_value.rb +31 -0
  277. data/lib/mongoid/extensions/regexp.rb +16 -10
  278. data/lib/mongoid/extensions/set.rb +27 -15
  279. data/lib/mongoid/extensions/string.rb +40 -67
  280. data/lib/mongoid/extensions/symbol.rb +11 -26
  281. data/lib/mongoid/extensions/time.rb +49 -47
  282. data/lib/mongoid/extensions/time_with_zone.rb +30 -12
  283. data/lib/mongoid/extensions/true_class.rb +11 -13
  284. data/lib/mongoid/extensions/vector.rb +64 -0
  285. data/lib/mongoid/extensions.rb +28 -41
  286. data/lib/mongoid/factory.rb +205 -28
  287. data/lib/mongoid/field_readable.rb +69 -0
  288. data/lib/mongoid/fields/encrypted.rb +47 -0
  289. data/lib/mongoid/fields/foreign_key.rb +48 -35
  290. data/lib/mongoid/fields/localized.rb +33 -21
  291. data/lib/mongoid/fields/standard.rb +31 -48
  292. data/lib/mongoid/fields/validators/macro.rb +48 -45
  293. data/lib/mongoid/fields/validators.rb +3 -2
  294. data/lib/mongoid/fields.rb +499 -117
  295. data/lib/mongoid/findable.rb +174 -64
  296. data/lib/mongoid/identifiable.rb +28 -0
  297. data/lib/mongoid/indexable/specification.rb +76 -39
  298. data/lib/mongoid/indexable/validators/options.rb +36 -43
  299. data/lib/mongoid/indexable.rb +57 -58
  300. data/lib/mongoid/inspectable.rb +37 -15
  301. data/lib/mongoid/interceptable.rb +235 -66
  302. data/lib/mongoid/loadable.rb +146 -0
  303. data/lib/mongoid/loggable.rb +12 -18
  304. data/lib/mongoid/matchable.rb +7 -154
  305. data/lib/mongoid/matcher/all.rb +34 -0
  306. data/lib/mongoid/matcher/and.rb +32 -0
  307. data/lib/mongoid/matcher/bits.rb +65 -0
  308. data/lib/mongoid/matcher/bits_all_clear.rb +39 -0
  309. data/lib/mongoid/matcher/bits_all_set.rb +39 -0
  310. data/lib/mongoid/matcher/bits_any_clear.rb +39 -0
  311. data/lib/mongoid/matcher/bits_any_set.rb +39 -0
  312. data/lib/mongoid/matcher/elem_match.rb +49 -0
  313. data/lib/mongoid/matcher/elem_match_expression.rb +29 -0
  314. data/lib/mongoid/matcher/eq.rb +23 -0
  315. data/lib/mongoid/matcher/eq_impl.rb +79 -0
  316. data/lib/mongoid/matcher/eq_impl_with_regexp.rb +33 -0
  317. data/lib/mongoid/matcher/exists.rb +27 -0
  318. data/lib/mongoid/matcher/expression.rb +43 -0
  319. data/lib/mongoid/matcher/expression_operator.rb +31 -0
  320. data/lib/mongoid/matcher/field_expression.rb +74 -0
  321. data/lib/mongoid/matcher/field_operator.rb +76 -0
  322. data/lib/mongoid/matcher/gt.rb +29 -0
  323. data/lib/mongoid/matcher/gte.rb +29 -0
  324. data/lib/mongoid/matcher/in.rb +37 -0
  325. data/lib/mongoid/matcher/lt.rb +29 -0
  326. data/lib/mongoid/matcher/lte.rb +29 -0
  327. data/lib/mongoid/matcher/mod.rb +29 -0
  328. data/lib/mongoid/matcher/ne.rb +28 -0
  329. data/lib/mongoid/matcher/nin.rb +23 -0
  330. data/lib/mongoid/matcher/nor.rb +34 -0
  331. data/lib/mongoid/matcher/not.rb +42 -0
  332. data/lib/mongoid/matcher/or.rb +32 -0
  333. data/lib/mongoid/matcher/regex.rb +61 -0
  334. data/lib/mongoid/matcher/regexp_budget.rb +372 -0
  335. data/lib/mongoid/matcher/size.rb +39 -0
  336. data/lib/mongoid/matcher/type.rb +119 -0
  337. data/lib/mongoid/matcher.rb +141 -0
  338. data/lib/mongoid/model_resolver.rb +155 -0
  339. data/lib/mongoid/persistable/creatable.rb +50 -36
  340. data/lib/mongoid/persistable/deletable.rb +18 -30
  341. data/lib/mongoid/persistable/destroyable.rb +38 -14
  342. data/lib/mongoid/persistable/incrementable.rb +8 -10
  343. data/lib/mongoid/persistable/logical.rb +7 -10
  344. data/lib/mongoid/persistable/maxable.rb +35 -0
  345. data/lib/mongoid/persistable/minable.rb +35 -0
  346. data/lib/mongoid/persistable/multipliable.rb +34 -0
  347. data/lib/mongoid/persistable/poppable.rb +5 -9
  348. data/lib/mongoid/persistable/pullable.rb +6 -12
  349. data/lib/mongoid/persistable/pushable.rb +8 -14
  350. data/lib/mongoid/persistable/renamable.rb +11 -10
  351. data/lib/mongoid/persistable/savable.rb +16 -11
  352. data/lib/mongoid/persistable/settable.rb +7 -18
  353. data/lib/mongoid/persistable/unsettable.rb +11 -12
  354. data/lib/mongoid/persistable/updatable.rb +113 -35
  355. data/lib/mongoid/persistable/upsertable.rb +37 -12
  356. data/lib/mongoid/persistable.rb +169 -60
  357. data/lib/mongoid/persistence_context.rb +189 -75
  358. data/lib/mongoid/pluckable.rb +130 -0
  359. data/lib/mongoid/positional.rb +7 -15
  360. data/lib/mongoid/railtie.rb +36 -32
  361. data/lib/mongoid/railties/bson_object_id_serializer.rb +46 -0
  362. data/lib/mongoid/railties/console_sandbox.rb +40 -0
  363. data/lib/mongoid/railties/controller_runtime.rb +34 -20
  364. data/lib/mongoid/railties/database.rake +53 -29
  365. data/lib/mongoid/reloadable.rb +56 -45
  366. data/lib/mongoid/scopable.rb +50 -79
  367. data/lib/mongoid/search_indexable.rb +538 -0
  368. data/lib/mongoid/selectable.rb +8 -19
  369. data/lib/mongoid/serializable.rb +46 -40
  370. data/lib/mongoid/shardable.rb +102 -23
  371. data/lib/mongoid/stateful.rb +67 -26
  372. data/lib/mongoid/stringified_symbol.rb +61 -0
  373. data/lib/mongoid/tasks/database.rake +46 -15
  374. data/lib/mongoid/tasks/database.rb +204 -31
  375. data/lib/mongoid/tasks/encryption.rake +59 -0
  376. data/lib/mongoid/tasks/encryption.rb +103 -0
  377. data/lib/mongoid/threaded/lifecycle.rb +13 -34
  378. data/lib/mongoid/threaded.rb +353 -108
  379. data/lib/mongoid/timestamps/created/short.rb +4 -3
  380. data/lib/mongoid/timestamps/created.rb +15 -7
  381. data/lib/mongoid/timestamps/short.rb +2 -1
  382. data/lib/mongoid/timestamps/timeless.rb +127 -29
  383. data/lib/mongoid/timestamps/updated/short.rb +4 -3
  384. data/lib/mongoid/timestamps/updated.rb +6 -9
  385. data/lib/mongoid/timestamps.rb +7 -7
  386. data/lib/mongoid/touchable.rb +176 -40
  387. data/lib/mongoid/traversable.rb +309 -106
  388. data/lib/mongoid/utils.rb +52 -0
  389. data/lib/mongoid/validatable/associated.rb +115 -22
  390. data/lib/mongoid/validatable/format.rb +2 -2
  391. data/lib/mongoid/validatable/length.rb +2 -2
  392. data/lib/mongoid/validatable/localizable.rb +3 -6
  393. data/lib/mongoid/validatable/macros.rb +25 -14
  394. data/lib/mongoid/validatable/numericality.rb +40 -0
  395. data/lib/mongoid/validatable/presence.rb +15 -21
  396. data/lib/mongoid/validatable/queryable.rb +9 -4
  397. data/lib/mongoid/validatable/uniqueness.rb +40 -53
  398. data/lib/mongoid/validatable.rb +41 -52
  399. data/lib/mongoid/version.rb +6 -2
  400. data/lib/mongoid/warnings.rb +40 -0
  401. data/lib/mongoid.rb +105 -44
  402. data/lib/rails/generators/mongoid/config/config_generator.rb +18 -5
  403. data/lib/rails/generators/mongoid/config/templates/mongoid.rb +24 -0
  404. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +51 -56
  405. data/lib/rails/generators/mongoid/model/model_generator.rb +10 -8
  406. data/lib/rails/generators/mongoid_generator.rb +17 -7
  407. data/lib/rails/mongoid.rb +4 -32
  408. data/spec/README.md +19 -4
  409. data/spec/config/mongoid.yml +17 -1
  410. data/spec/config/mongoid_with_schema_map_uuid.yml +27 -0
  411. data/spec/integration/active_job_spec.rb +35 -0
  412. data/spec/integration/app_spec.rb +327 -144
  413. data/spec/integration/associations/belongs_to_spec.rb +165 -0
  414. data/spec/integration/associations/embedded_dirty_spec.rb +57 -0
  415. data/spec/integration/associations/embedded_spec.rb +241 -67
  416. data/spec/integration/associations/embeds_many_spec.rb +347 -0
  417. data/spec/integration/associations/embeds_one_spec.rb +62 -0
  418. data/spec/integration/associations/foreign_key_spec.rb +107 -0
  419. data/spec/integration/associations/foreign_key_spec_models.rb +64 -0
  420. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +161 -0
  421. data/spec/integration/associations/has_many_spec.rb +184 -0
  422. data/spec/integration/associations/has_one_spec.rb +322 -0
  423. data/spec/integration/associations/nested_attributes_assignment_spec.rb +113 -0
  424. data/spec/integration/associations/reverse_population_spec.rb +34 -0
  425. data/spec/integration/associations/reverse_population_spec_models.rb +36 -0
  426. data/spec/integration/associations/scope_option_spec.rb +100 -0
  427. data/spec/integration/atomic/modifiers_spec.rb +137 -0
  428. data/spec/integration/bson_regexp_raw_spec.rb +19 -0
  429. data/spec/integration/caching_spec.rb +68 -0
  430. data/spec/integration/callbacks_models.rb +149 -3
  431. data/spec/integration/callbacks_spec.rb +422 -34
  432. data/spec/integration/contextual/empty_spec.rb +141 -0
  433. data/spec/integration/criteria/alias_query_spec.rb +91 -0
  434. data/spec/integration/criteria/date_field_spec.rb +1 -2
  435. data/spec/integration/criteria/default_scope_spec.rb +78 -0
  436. data/spec/integration/criteria/logical_spec.rb +105 -0
  437. data/spec/integration/criteria/range_spec.rb +349 -0
  438. data/spec/integration/criteria/raw_value_spec.rb +538 -0
  439. data/spec/integration/criteria/time_with_zone_spec.rb +135 -15
  440. data/spec/integration/discriminator_key_spec.rb +392 -0
  441. data/spec/integration/discriminator_value_spec.rb +204 -0
  442. data/spec/integration/document_spec.rb +32 -3
  443. data/spec/integration/dots_and_dollars_spec.rb +273 -0
  444. data/spec/integration/encryption_spec.rb +248 -0
  445. data/spec/integration/i18n_fallbacks_spec.rb +47 -0
  446. data/spec/integration/isolation_state_spec.rb +241 -0
  447. data/spec/integration/matcher_examples_spec.rb +743 -0
  448. data/spec/integration/matcher_operator_data/all.yml +140 -0
  449. data/spec/integration/matcher_operator_data/and.yml +93 -0
  450. data/spec/integration/matcher_operator_data/bits_all_clear.yml +144 -0
  451. data/spec/integration/matcher_operator_data/bits_all_set.yml +144 -0
  452. data/spec/integration/matcher_operator_data/bits_any_clear.yml +144 -0
  453. data/spec/integration/matcher_operator_data/bits_any_set.yml +144 -0
  454. data/spec/integration/matcher_operator_data/comment.yml +22 -0
  455. data/spec/integration/matcher_operator_data/elem_match.yml +409 -0
  456. data/spec/integration/matcher_operator_data/elem_match_expr.yml +213 -0
  457. data/spec/integration/matcher_operator_data/eq.yml +191 -0
  458. data/spec/integration/matcher_operator_data/exists.yml +213 -0
  459. data/spec/integration/matcher_operator_data/generic_op.yml +17 -0
  460. data/spec/integration/matcher_operator_data/gt.yml +132 -0
  461. data/spec/integration/matcher_operator_data/gt_types.yml +63 -0
  462. data/spec/integration/matcher_operator_data/gte.yml +132 -0
  463. data/spec/integration/matcher_operator_data/gte_types.yml +15 -0
  464. data/spec/integration/matcher_operator_data/implicit.yml +331 -0
  465. data/spec/integration/matcher_operator_data/implicit_traversal.yml +112 -0
  466. data/spec/integration/matcher_operator_data/in.yml +210 -0
  467. data/spec/integration/matcher_operator_data/invalid_op.yml +59 -0
  468. data/spec/integration/matcher_operator_data/invalid_syntax.yml +39 -0
  469. data/spec/integration/matcher_operator_data/lt.yml +132 -0
  470. data/spec/integration/matcher_operator_data/lt_types.yml +15 -0
  471. data/spec/integration/matcher_operator_data/lte.yml +132 -0
  472. data/spec/integration/matcher_operator_data/lte_types.yml +15 -0
  473. data/spec/integration/matcher_operator_data/mod.yml +55 -0
  474. data/spec/integration/matcher_operator_data/multiple.yml +29 -0
  475. data/spec/integration/matcher_operator_data/ne.yml +150 -0
  476. data/spec/integration/matcher_operator_data/ne_types.yml +15 -0
  477. data/spec/integration/matcher_operator_data/nin.yml +114 -0
  478. data/spec/integration/matcher_operator_data/nor.yml +126 -0
  479. data/spec/integration/matcher_operator_data/not.yml +196 -0
  480. data/spec/integration/matcher_operator_data/or.yml +137 -0
  481. data/spec/integration/matcher_operator_data/regex.yml +195 -0
  482. data/spec/integration/matcher_operator_data/regex_options.yml +72 -0
  483. data/spec/integration/matcher_operator_data/size.yml +171 -0
  484. data/spec/integration/matcher_operator_data/type.yml +64 -0
  485. data/spec/integration/matcher_operator_data/type_array.yml +15 -0
  486. data/spec/integration/matcher_operator_data/type_binary.yml +18 -0
  487. data/spec/integration/matcher_operator_data/type_boolean.yml +39 -0
  488. data/spec/integration/matcher_operator_data/type_code.yml +26 -0
  489. data/spec/integration/matcher_operator_data/type_code_with_scope.yml +26 -0
  490. data/spec/integration/matcher_operator_data/type_date.yml +39 -0
  491. data/spec/integration/matcher_operator_data/type_db_pointer.yml +19 -0
  492. data/spec/integration/matcher_operator_data/type_decimal.yml +37 -0
  493. data/spec/integration/matcher_operator_data/type_double.yml +15 -0
  494. data/spec/integration/matcher_operator_data/type_int32.yml +33 -0
  495. data/spec/integration/matcher_operator_data/type_int64.yml +33 -0
  496. data/spec/integration/matcher_operator_data/type_max_key.yml +17 -0
  497. data/spec/integration/matcher_operator_data/type_min_key.yml +17 -0
  498. data/spec/integration/matcher_operator_data/type_null.yml +23 -0
  499. data/spec/integration/matcher_operator_data/type_object.yml +23 -0
  500. data/spec/integration/matcher_operator_data/type_object_id.yml +25 -0
  501. data/spec/integration/matcher_operator_data/type_regex.yml +44 -0
  502. data/spec/integration/matcher_operator_data/type_string.yml +15 -0
  503. data/spec/integration/matcher_operator_data/type_symbol.yml +32 -0
  504. data/spec/integration/matcher_operator_data/type_timestamp.yml +25 -0
  505. data/spec/integration/matcher_operator_data/type_undefined.yml +17 -0
  506. data/spec/integration/matcher_operator_spec.rb +113 -0
  507. data/spec/integration/matcher_regexp_timeout_spec.rb +215 -0
  508. data/spec/integration/matcher_spec.rb +237 -0
  509. data/spec/integration/persistence/collection_options_spec.rb +34 -0
  510. data/spec/integration/persistence/range_field_spec.rb +373 -0
  511. data/spec/integration/query_operator_guard_spec.rb +89 -0
  512. data/spec/integration/server_query_spec.rb +148 -0
  513. data/spec/integration/shardable_spec.rb +148 -0
  514. data/spec/integration/stringified_symbol_field_spec.rb +189 -0
  515. data/spec/lite_spec_helper.rb +56 -19
  516. data/spec/mongoid/association/accessors_spec.rb +375 -273
  517. data/spec/mongoid/association/auto_save_spec.rb +215 -106
  518. data/spec/mongoid/association/builders_spec.rb +124 -74
  519. data/spec/mongoid/association/constrainable_spec.rb +11 -21
  520. data/spec/mongoid/association/counter_cache_spec.rb +101 -131
  521. data/spec/mongoid/association/depending_spec.rb +454 -402
  522. data/spec/mongoid/association/eager_loadable_spec.rb +47 -0
  523. data/spec/mongoid/association/eager_spec.rb +471 -60
  524. data/spec/mongoid/association/embedded/cyclic_spec.rb +40 -49
  525. data/spec/mongoid/association/embedded/dirty_spec.rb +19 -21
  526. data/spec/mongoid/association/embedded/embedded_in/binding_spec.rb +39 -53
  527. data/spec/mongoid/association/embedded/embedded_in/buildable_spec.rb +58 -8
  528. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +208 -158
  529. data/spec/mongoid/association/embedded/embedded_in_spec.rb +46 -134
  530. data/spec/mongoid/association/embedded/embeds_many/binding_spec.rb +11 -13
  531. data/spec/mongoid/association/embedded/embeds_many/buildable_spec.rb +137 -35
  532. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +1716 -1327
  533. data/spec/mongoid/association/embedded/embeds_many_models.rb +191 -2
  534. data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +68 -0
  535. data/spec/mongoid/association/embedded/embeds_many_spec.rb +77 -119
  536. data/spec/mongoid/association/embedded/embeds_one/binding_spec.rb +17 -22
  537. data/spec/mongoid/association/embedded/embeds_one/buildable_spec.rb +44 -24
  538. data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +234 -287
  539. data/spec/mongoid/association/embedded/embeds_one_dnl_models.rb +2 -0
  540. data/spec/mongoid/association/embedded/embeds_one_models.rb +27 -1
  541. data/spec/mongoid/association/embedded/embeds_one_query_spec.rb +28 -0
  542. data/spec/mongoid/association/embedded/embeds_one_spec.rb +50 -134
  543. data/spec/mongoid/association/fallback_spec.rb +425 -0
  544. data/spec/mongoid/association/macros_spec.rb +250 -308
  545. data/spec/mongoid/association/nested/many_spec.rb +53 -72
  546. data/spec/mongoid/association/nested/one_spec.rb +68 -85
  547. data/spec/mongoid/association/options_spec.rb +189 -309
  548. data/spec/mongoid/association/polymorphic_spec.rb +36 -51
  549. data/spec/mongoid/association/referenced/belongs_to/binding_spec.rb +58 -75
  550. data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +131 -40
  551. data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +280 -52
  552. data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +345 -370
  553. data/spec/mongoid/association/referenced/belongs_to_models.rb +13 -0
  554. data/spec/mongoid/association/referenced/belongs_to_query_spec.rb +57 -0
  555. data/spec/mongoid/association/referenced/belongs_to_spec.rb +171 -329
  556. data/spec/mongoid/association/referenced/has_and_belongs_to_many/binding_spec.rb +45 -56
  557. data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +66 -29
  558. data/spec/mongoid/association/referenced/has_and_belongs_to_many/eager_spec.rb +75 -36
  559. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_persistence_spec.rb +28 -8
  560. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +1151 -1283
  561. data/spec/mongoid/association/referenced/has_and_belongs_to_many_models.rb +69 -2
  562. data/spec/mongoid/association/referenced/has_and_belongs_to_many_query_spec.rb +39 -0
  563. data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +176 -166
  564. data/spec/mongoid/association/referenced/has_many/binding_spec.rb +32 -43
  565. data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +161 -24
  566. data/spec/mongoid/association/referenced/has_many/eager_spec.rb +169 -67
  567. data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +1021 -689
  568. data/spec/mongoid/association/referenced/has_many/proxy_query_spec.rb +1 -1
  569. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +1743 -1955
  570. data/spec/mongoid/association/referenced/has_many_models.rb +85 -1
  571. data/spec/mongoid/association/referenced/has_many_query_spec.rb +37 -0
  572. data/spec/mongoid/association/referenced/has_many_spec.rb +83 -178
  573. data/spec/mongoid/association/referenced/has_many_through/eager_spec.rb +58 -0
  574. data/spec/mongoid/association/referenced/has_many_through/proxy_spec.rb +62 -0
  575. data/spec/mongoid/association/referenced/has_many_through_spec.rb +399 -0
  576. data/spec/mongoid/association/referenced/has_one/binding_spec.rb +28 -37
  577. data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +74 -28
  578. data/spec/mongoid/association/referenced/has_one/eager_spec.rb +60 -42
  579. data/spec/mongoid/association/referenced/has_one/proxy_spec.rb +380 -333
  580. data/spec/mongoid/association/referenced/has_one_models.rb +73 -0
  581. data/spec/mongoid/association/referenced/has_one_query_spec.rb +37 -0
  582. data/spec/mongoid/association/referenced/has_one_spec.rb +61 -185
  583. data/spec/mongoid/association/referenced/has_one_through/eager_spec.rb +58 -0
  584. data/spec/mongoid/association/referenced/has_one_through/proxy_spec.rb +49 -0
  585. data/spec/mongoid/association/referenced/has_one_through_spec.rb +211 -0
  586. data/spec/mongoid/association/reflections_spec.rb +16 -23
  587. data/spec/mongoid/association/syncable_spec.rb +132 -138
  588. data/spec/mongoid/association_spec.rb +59 -65
  589. data/spec/mongoid/atomic/modifiers_spec.rb +217 -180
  590. data/spec/mongoid/atomic/paths/embedded/many_spec.rb +41 -51
  591. data/spec/mongoid/atomic/paths/embedded/one_spec.rb +37 -48
  592. data/spec/mongoid/atomic/paths/root_spec.rb +11 -14
  593. data/spec/mongoid/atomic/paths_spec.rb +133 -128
  594. data/spec/mongoid/atomic_spec.rb +187 -154
  595. data/spec/mongoid/atomic_update_preparer_spec.rb +83 -0
  596. data/spec/mongoid/attributes/dynamic_spec.rb +5 -6
  597. data/spec/mongoid/attributes/embedded_spec.rb +118 -0
  598. data/spec/mongoid/attributes/nested_spec.rb +1604 -1737
  599. data/spec/mongoid/attributes/nested_spec_models.rb +48 -0
  600. data/spec/mongoid/attributes/projector_data/embedded.yml +105 -0
  601. data/spec/mongoid/attributes/projector_data/fields.yml +93 -0
  602. data/spec/mongoid/attributes/projector_spec.rb +37 -0
  603. data/spec/mongoid/attributes/readonly_spec.rb +106 -108
  604. data/spec/mongoid/attributes_spec.rb +1217 -715
  605. data/spec/mongoid/cacheable_spec.rb +78 -45
  606. data/spec/mongoid/changeable_spec.rb +914 -638
  607. data/spec/mongoid/clients/factory_spec.rb +314 -137
  608. data/spec/mongoid/clients/options_spec.rb +205 -76
  609. data/spec/mongoid/clients/sessions_spec.rb +73 -116
  610. data/spec/mongoid/clients/transactions_spec.rb +1128 -186
  611. data/spec/mongoid/clients/transactions_spec_models.rb +251 -0
  612. data/spec/mongoid/clients_spec.rb +433 -312
  613. data/spec/mongoid/collection_configurable_spec.rb +156 -0
  614. data/spec/mongoid/composable_spec.rb +4 -8
  615. data/spec/mongoid/config/defaults_spec.rb +125 -0
  616. data/spec/mongoid/config/encryption_spec.rb +377 -0
  617. data/spec/mongoid/config/environment_spec.rb +129 -49
  618. data/spec/mongoid/config/introspection_spec.rb +119 -0
  619. data/spec/mongoid/config/options_spec.rb +36 -23
  620. data/spec/mongoid/config_spec.rb +497 -148
  621. data/spec/mongoid/contextual/aggregable/memory_spec.rb +507 -161
  622. data/spec/mongoid/contextual/aggregable/memory_table.yml +88 -0
  623. data/spec/mongoid/contextual/aggregable/memory_table_spec.rb +60 -0
  624. data/spec/mongoid/contextual/aggregable/mongo_spec.rb +242 -158
  625. data/spec/mongoid/contextual/aggregable/none_spec.rb +47 -0
  626. data/spec/mongoid/contextual/atomic_spec.rb +510 -229
  627. data/spec/mongoid/contextual/map_reduce_spec.rb +114 -153
  628. data/spec/mongoid/contextual/memory_spec.rb +1947 -563
  629. data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +193 -0
  630. data/spec/mongoid/contextual/mongo_spec.rb +3317 -1268
  631. data/spec/mongoid/contextual/none_spec.rb +98 -89
  632. data/spec/mongoid/contextual/short_circuit_spec.rb +139 -0
  633. data/spec/mongoid/copyable_spec.rb +769 -191
  634. data/spec/mongoid/copyable_spec_models.rb +30 -0
  635. data/spec/mongoid/criteria/findable_spec.rb +565 -506
  636. data/spec/mongoid/criteria/includable_spec.rb +2417 -0
  637. data/spec/mongoid/criteria/includable_spec_models.rb +64 -0
  638. data/spec/mongoid/criteria/inspectable_spec.rb +13 -13
  639. data/spec/mongoid/criteria/marshalable_spec.rb +29 -12
  640. data/spec/mongoid/criteria/modifiable_spec.rb +388 -530
  641. data/spec/mongoid/criteria/options_spec.rb +4 -6
  642. data/spec/mongoid/criteria/queryable/aggregable_spec.rb +114 -140
  643. data/spec/mongoid/criteria/queryable/expandable_spec.rb +59 -0
  644. data/spec/mongoid/criteria/queryable/extensions/array_spec.rb +132 -198
  645. data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +199 -25
  646. data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +86 -116
  647. data/spec/mongoid/criteria/queryable/extensions/date_spec.rb +69 -67
  648. data/spec/mongoid/criteria/queryable/extensions/date_time_spec.rb +121 -113
  649. data/spec/mongoid/criteria/queryable/extensions/float_spec.rb +96 -32
  650. data/spec/mongoid/criteria/queryable/extensions/hash_spec.rb +105 -152
  651. data/spec/mongoid/criteria/queryable/extensions/integer_spec.rb +97 -33
  652. data/spec/mongoid/criteria/queryable/extensions/nil_class_spec.rb +27 -37
  653. data/spec/mongoid/criteria/queryable/extensions/numeric_spec.rb +79 -15
  654. data/spec/mongoid/criteria/queryable/extensions/object_spec.rb +34 -53
  655. data/spec/mongoid/criteria/queryable/extensions/range_spec.rb +259 -191
  656. data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +18 -34
  657. data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +16 -33
  658. data/spec/mongoid/criteria/queryable/extensions/set_spec.rb +14 -18
  659. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +91 -189
  660. data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +34 -100
  661. data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +99 -103
  662. data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +78 -85
  663. data/spec/mongoid/criteria/queryable/key_spec.rb +34 -35
  664. data/spec/mongoid/criteria/queryable/mergeable_spec.rb +150 -21
  665. data/spec/mongoid/criteria/queryable/optional_spec.rb +375 -954
  666. data/spec/mongoid/criteria/queryable/options_spec.rb +175 -123
  667. data/spec/mongoid/criteria/queryable/pipeline_spec.rb +67 -86
  668. data/spec/mongoid/criteria/queryable/queryable_spec.rb +43 -52
  669. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +2101 -414
  670. data/spec/mongoid/criteria/queryable/selectable_shared_examples.rb +35 -0
  671. data/spec/mongoid/criteria/queryable/selectable_spec.rb +1283 -3140
  672. data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +754 -0
  673. data/spec/mongoid/criteria/queryable/selector_spec.rb +404 -317
  674. data/spec/mongoid/criteria/queryable/smash_spec.rb +8 -10
  675. data/spec/mongoid/criteria/queryable/storable_spec.rb +249 -0
  676. data/spec/mongoid/criteria/queryable_mql_spec.rb +92 -0
  677. data/spec/mongoid/criteria/scopable_spec.rb +212 -118
  678. data/spec/mongoid/criteria/translator_spec.rb +132 -0
  679. data/spec/mongoid/criteria_projection_spec.rb +389 -0
  680. data/spec/mongoid/criteria_spec.rb +1942 -2301
  681. data/spec/mongoid/document_fields_spec.rb +187 -14
  682. data/spec/mongoid/document_persistence_context_spec.rb +80 -6
  683. data/spec/mongoid/document_query_spec.rb +87 -0
  684. data/spec/mongoid/document_spec.rb +394 -538
  685. data/spec/mongoid/encryptable_spec.rb +61 -0
  686. data/spec/mongoid/equality_spec.rb +77 -103
  687. data/spec/mongoid/errors/ambiguous_relationship_spec.rb +10 -10
  688. data/spec/mongoid/errors/attribute_not_loaded_spec.rb +29 -0
  689. data/spec/mongoid/errors/callback_spec.rb +10 -10
  690. data/spec/mongoid/errors/config_redactor_spec.rb +112 -0
  691. data/spec/mongoid/errors/delete_restriction_spec.rb +9 -9
  692. data/spec/mongoid/errors/document_not_destroyed_spec.rb +9 -9
  693. data/spec/mongoid/errors/document_not_found_spec.rb +108 -32
  694. data/spec/mongoid/errors/invalid_collection_spec.rb +11 -12
  695. data/spec/mongoid/errors/invalid_config_file_spec.rb +29 -0
  696. data/spec/mongoid/errors/invalid_config_option_spec.rb +10 -10
  697. data/spec/mongoid/errors/invalid_field_option_spec.rb +9 -9
  698. data/spec/mongoid/errors/invalid_field_spec.rb +13 -11
  699. data/spec/mongoid/errors/invalid_field_type_spec.rb +53 -0
  700. data/spec/mongoid/errors/invalid_find_spec.rb +10 -10
  701. data/spec/mongoid/errors/invalid_includes_spec.rb +11 -12
  702. data/spec/mongoid/errors/invalid_index_spec.rb +10 -10
  703. data/spec/mongoid/errors/invalid_options_spec.rb +10 -10
  704. data/spec/mongoid/errors/invalid_path_spec.rb +9 -9
  705. data/spec/mongoid/errors/invalid_relation_spec.rb +17 -15
  706. data/spec/mongoid/errors/invalid_scope_spec.rb +10 -10
  707. data/spec/mongoid/errors/invalid_set_polymorphic_relation_spec.rb +5 -5
  708. data/spec/mongoid/errors/invalid_storage_options_spec.rb +10 -10
  709. data/spec/mongoid/errors/invalid_time_spec.rb +10 -10
  710. data/spec/mongoid/errors/inverse_not_found_spec.rb +11 -11
  711. data/spec/mongoid/errors/mixed_client_configuration_spec.rb +42 -11
  712. data/spec/mongoid/errors/mixed_relations_spec.rb +10 -10
  713. data/spec/mongoid/errors/mongoid_error_spec.rb +23 -23
  714. data/spec/mongoid/errors/nested_attributes_metadata_not_found_spec.rb +10 -10
  715. data/spec/mongoid/errors/no_client_config_spec.rb +11 -11
  716. data/spec/mongoid/errors/no_client_database_spec.rb +40 -11
  717. data/spec/mongoid/errors/no_client_hosts_spec.rb +37 -11
  718. data/spec/mongoid/errors/no_clients_config_spec.rb +9 -9
  719. data/spec/mongoid/errors/no_environment_spec.rb +10 -10
  720. data/spec/mongoid/errors/no_map_reduce_output_spec.rb +10 -10
  721. data/spec/mongoid/errors/no_metadata_spec.rb +9 -9
  722. data/spec/mongoid/errors/no_parent_spec.rb +10 -10
  723. data/spec/mongoid/errors/readonly_association_spec.rb +34 -0
  724. data/spec/mongoid/errors/readonly_attribute_spec.rb +9 -9
  725. data/spec/mongoid/errors/readonly_document_spec.rb +9 -9
  726. data/spec/mongoid/errors/scope_overwrite_spec.rb +11 -11
  727. data/spec/mongoid/errors/too_many_nested_attribute_records_spec.rb +11 -11
  728. data/spec/mongoid/errors/unknown_attribute_spec.rb +9 -9
  729. data/spec/mongoid/errors/unsaved_document_spec.rb +10 -10
  730. data/spec/mongoid/errors/unsupported_javascript_spec.rb +11 -11
  731. data/spec/mongoid/errors/validations_spec.rb +13 -13
  732. data/spec/mongoid/extensions/array_spec.rb +92 -377
  733. data/spec/mongoid/extensions/big_decimal_spec.rb +640 -217
  734. data/spec/mongoid/extensions/binary_spec.rb +50 -23
  735. data/spec/mongoid/extensions/boolean_spec.rb +73 -107
  736. data/spec/mongoid/extensions/date_class_mongoize_spec.rb +329 -0
  737. data/spec/mongoid/extensions/date_spec.rb +62 -173
  738. data/spec/mongoid/extensions/date_time_spec.rb +42 -95
  739. data/spec/mongoid/extensions/decimal128_spec.rb +14 -17
  740. data/spec/mongoid/extensions/false_class_spec.rb +13 -24
  741. data/spec/mongoid/extensions/float_spec.rb +66 -100
  742. data/spec/mongoid/extensions/hash_spec.rb +79 -165
  743. data/spec/mongoid/extensions/integer_spec.rb +63 -91
  744. data/spec/mongoid/extensions/module_spec.rb +15 -17
  745. data/spec/mongoid/extensions/nil_class_spec.rb +4 -4
  746. data/spec/mongoid/extensions/object_id_spec.rb +161 -246
  747. data/spec/mongoid/extensions/object_spec.rb +36 -238
  748. data/spec/mongoid/extensions/range_spec.rb +259 -69
  749. data/spec/mongoid/extensions/raw_value_spec.rb +65 -0
  750. data/spec/mongoid/extensions/regexp_spec.rb +56 -41
  751. data/spec/mongoid/extensions/set_spec.rb +110 -14
  752. data/spec/mongoid/extensions/string_spec.rb +139 -237
  753. data/spec/mongoid/extensions/stringified_symbol_spec.rb +71 -0
  754. data/spec/mongoid/extensions/symbol_spec.rb +19 -62
  755. data/spec/mongoid/extensions/time_spec.rb +394 -192
  756. data/spec/mongoid/extensions/time_with_zone_spec.rb +111 -176
  757. data/spec/mongoid/extensions/true_class_spec.rb +13 -24
  758. data/spec/mongoid/extensions/vector_spec.rb +175 -0
  759. data/spec/mongoid/factory_spec.rb +378 -80
  760. data/spec/mongoid/fields/foreign_key_spec.rb +387 -267
  761. data/spec/mongoid/fields/localized_spec.rb +197 -196
  762. data/spec/mongoid/fields/standard_spec.rb +45 -58
  763. data/spec/mongoid/fields_spec.rb +1448 -525
  764. data/spec/mongoid/findable_spec.rb +659 -230
  765. data/spec/mongoid/indexable/specification_spec.rb +145 -40
  766. data/spec/mongoid/indexable_spec.rb +551 -188
  767. data/spec/mongoid/inspectable_spec.rb +126 -18
  768. data/spec/mongoid/interceptable_spec.rb +1431 -573
  769. data/spec/mongoid/interceptable_spec_models.rb +244 -0
  770. data/spec/mongoid/loadable_spec.rb +86 -0
  771. data/spec/mongoid/loading_spec.rb +114 -0
  772. data/spec/mongoid/loggable_spec.rb +5 -5
  773. data/spec/mongoid/matcher/extract_attribute_data/numeric_keys.yml +104 -0
  774. data/spec/mongoid/matcher/extract_attribute_data/traversal.yml +239 -0
  775. data/spec/mongoid/matcher/extract_attribute_spec.rb +33 -0
  776. data/spec/mongoid/matcher/regexp_budget_spec.rb +570 -0
  777. data/spec/mongoid/model_resolver_spec.rb +167 -0
  778. data/spec/mongoid/mongoizable_spec.rb +277 -0
  779. data/spec/mongoid/monkey_patches_spec.rb +215 -0
  780. data/spec/mongoid/persistable/creatable_spec.rb +256 -209
  781. data/spec/mongoid/persistable/deletable_spec.rb +369 -80
  782. data/spec/mongoid/persistable/destroyable_spec.rb +439 -80
  783. data/spec/mongoid/persistable/incrementable_spec.rb +112 -77
  784. data/spec/mongoid/persistable/logical_spec.rb +92 -51
  785. data/spec/mongoid/persistable/maxable_spec.rb +148 -0
  786. data/spec/mongoid/persistable/minable_spec.rb +148 -0
  787. data/spec/mongoid/persistable/multipliable_spec.rb +212 -0
  788. data/spec/mongoid/persistable/poppable_spec.rb +87 -47
  789. data/spec/mongoid/persistable/pullable_spec.rb +161 -82
  790. data/spec/mongoid/persistable/pushable_spec.rb +188 -110
  791. data/spec/mongoid/persistable/renamable_spec.rb +91 -51
  792. data/spec/mongoid/persistable/savable_spec.rb +373 -155
  793. data/spec/mongoid/persistable/settable_spec.rb +164 -134
  794. data/spec/mongoid/persistable/unsettable_spec.rb +94 -58
  795. data/spec/mongoid/persistable/updatable_spec.rb +235 -267
  796. data/spec/mongoid/persistable/upsertable_spec.rb +150 -38
  797. data/spec/mongoid/persistable_spec.rb +198 -128
  798. data/spec/mongoid/persistence_context_spec.rb +133 -137
  799. data/spec/mongoid/positional_spec.rb +69 -78
  800. data/spec/mongoid/railties/bson_object_id_serializer_spec.rb +27 -0
  801. data/spec/mongoid/railties/console_sandbox_spec.rb +43 -0
  802. data/spec/mongoid/relations/proxy_spec.rb +8 -6
  803. data/spec/mongoid/reloadable_spec.rb +552 -123
  804. data/spec/mongoid/scopable_spec.rb +522 -455
  805. data/spec/mongoid/search_indexable_spec.rb +838 -0
  806. data/spec/mongoid/selectable_spec.rb +41 -53
  807. data/spec/mongoid/serializable_spec.rb +394 -319
  808. data/spec/mongoid/shardable_models.rb +76 -0
  809. data/spec/mongoid/shardable_spec.rb +245 -41
  810. data/spec/mongoid/stateful_spec.rb +170 -45
  811. data/spec/mongoid/tasks/database_rake_spec.rb +284 -133
  812. data/spec/mongoid/tasks/database_spec.rb +245 -51
  813. data/spec/mongoid/tasks/encryption_spec.rb +189 -0
  814. data/spec/mongoid/threaded_spec.rb +118 -85
  815. data/spec/mongoid/timestamps/created/short_spec.rb +15 -16
  816. data/spec/mongoid/timestamps/created_spec.rb +36 -14
  817. data/spec/mongoid/timestamps/timeless_spec.rb +230 -47
  818. data/spec/mongoid/timestamps/updated/short_spec.rb +26 -30
  819. data/spec/mongoid/timestamps/updated_spec.rb +23 -27
  820. data/spec/mongoid/timestamps_spec.rb +408 -34
  821. data/spec/mongoid/timestamps_spec_models.rb +67 -0
  822. data/spec/mongoid/touchable_spec.rb +1235 -136
  823. data/spec/mongoid/touchable_spec_models.rb +178 -0
  824. data/spec/mongoid/traversable_spec.rb +1188 -79
  825. data/spec/mongoid/validatable/associated_spec.rb +86 -89
  826. data/spec/mongoid/validatable/format_spec.rb +26 -33
  827. data/spec/mongoid/validatable/length_spec.rb +64 -85
  828. data/spec/mongoid/validatable/numericality_spec.rb +124 -14
  829. data/spec/mongoid/validatable/presence_spec.rb +168 -215
  830. data/spec/mongoid/validatable/uniqueness_spec.rb +702 -826
  831. data/spec/mongoid/validatable_spec.rb +76 -105
  832. data/spec/mongoid/warnings_spec.rb +32 -0
  833. data/spec/mongoid_spec.rb +67 -30
  834. data/spec/rails/controller_extension/controller_runtime_spec.rb +59 -60
  835. data/spec/rails/mongoid_spec.rb +38 -139
  836. data/spec/shared/CANDIDATE.md +28 -0
  837. data/spec/shared/bin/s3-copy +45 -0
  838. data/spec/shared/bin/s3-upload +69 -0
  839. data/spec/shared/lib/mrss/cluster_config.rb +16 -41
  840. data/spec/shared/lib/mrss/constraints.rb +44 -59
  841. data/spec/shared/lib/mrss/docker_runner.rb +42 -20
  842. data/spec/shared/lib/mrss/eg_config_utils.rb +20 -0
  843. data/spec/shared/lib/mrss/event_subscriber.rb +210 -0
  844. data/spec/shared/lib/mrss/lite_constraints.rb +48 -1
  845. data/spec/shared/lib/mrss/release/candidate.rb +287 -0
  846. data/spec/shared/lib/mrss/release/product_data.rb +144 -0
  847. data/spec/shared/lib/mrss/server_version_registry.rb +18 -24
  848. data/spec/shared/lib/mrss/session_registry.rb +69 -0
  849. data/spec/shared/lib/mrss/session_registry_legacy.rb +60 -0
  850. data/spec/shared/lib/mrss/spec_organizer.rb +48 -2
  851. data/spec/shared/lib/mrss/utils.rb +28 -6
  852. data/spec/shared/lib/tasks/candidate.rake +64 -0
  853. data/spec/shared/share/Dockerfile.erb +114 -96
  854. data/spec/shared/share/haproxy-1.conf +16 -0
  855. data/spec/shared/share/haproxy-2.conf +17 -0
  856. data/spec/shared/shlib/config.sh +27 -0
  857. data/spec/shared/shlib/distro.sh +12 -1
  858. data/spec/shared/shlib/server.sh +148 -65
  859. data/spec/shared/shlib/set_env.sh +56 -80
  860. data/spec/spec_helper.rb +104 -72
  861. data/spec/support/authorization.rb +2 -0
  862. data/spec/support/client_registry.rb +9 -0
  863. data/spec/support/constraints.rb +37 -22
  864. data/spec/support/crypt/models.rb +206 -0
  865. data/spec/support/crypt.rb +77 -0
  866. data/spec/support/expectations.rb +38 -17
  867. data/spec/support/feature_sandbox.rb +69 -0
  868. data/spec/support/helpers.rb +11 -0
  869. data/spec/support/immutable_ids.rb +118 -0
  870. data/spec/support/macros.rb +106 -1
  871. data/spec/support/models/account.rb +38 -0
  872. data/spec/support/models/acolyte.rb +20 -0
  873. data/spec/support/models/actor.rb +21 -0
  874. data/spec/support/models/actress.rb +4 -0
  875. data/spec/support/models/address.rb +88 -0
  876. data/spec/support/models/address_component.rb +8 -0
  877. data/spec/support/models/address_number.rb +9 -0
  878. data/spec/support/models/agency.rb +8 -0
  879. data/spec/support/models/agent.rb +19 -0
  880. data/spec/support/models/album.rb +20 -0
  881. data/spec/support/models/alert.rb +10 -0
  882. data/spec/support/models/animal.rb +28 -0
  883. data/spec/support/models/answer.rb +9 -0
  884. data/spec/support/models/appointment.rb +10 -0
  885. data/spec/support/models/armrest.rb +9 -0
  886. data/spec/support/models/article.rb +14 -0
  887. data/spec/support/models/artist.rb +95 -0
  888. data/spec/support/models/artwork.rb +7 -0
  889. data/spec/support/models/audible_sound.rb +3 -0
  890. data/spec/support/models/audio.rb +8 -0
  891. data/spec/support/models/augmentation.rb +25 -0
  892. data/spec/support/models/author.rb +9 -0
  893. data/spec/support/models/baby.rb +7 -0
  894. data/spec/support/models/band.rb +46 -0
  895. data/spec/support/models/bar.rb +13 -0
  896. data/spec/support/models/basic.rb +8 -0
  897. data/spec/support/models/bed.rb +3 -0
  898. data/spec/support/models/big_palette.rb +4 -0
  899. data/spec/support/models/birthday.rb +14 -0
  900. data/spec/support/models/bolt.rb +7 -0
  901. data/spec/support/models/bomb.rb +7 -0
  902. data/spec/support/models/book.rb +20 -0
  903. data/spec/support/models/breed.rb +7 -0
  904. data/spec/support/models/browser.rb +8 -0
  905. data/spec/support/models/building.rb +10 -0
  906. data/spec/support/models/building_address.rb +10 -0
  907. data/spec/support/models/bus.rb +10 -0
  908. data/spec/support/models/business.rb +8 -0
  909. data/spec/support/models/callback_test.rb +12 -0
  910. data/spec/support/models/canvas.rb +27 -0
  911. data/spec/support/models/car.rb +3 -0
  912. data/spec/support/models/cat.rb +9 -0
  913. data/spec/support/models/catalog.rb +24 -0
  914. data/spec/support/models/category.rb +11 -0
  915. data/spec/support/models/child.rb +7 -0
  916. data/spec/support/models/child_doc.rb +24 -0
  917. data/spec/support/models/church.rb +9 -0
  918. data/spec/support/models/circle.rb +5 -0
  919. data/spec/support/models/circuit.rb +7 -0
  920. data/spec/support/models/circus.rb +12 -0
  921. data/spec/support/models/code.rb +17 -0
  922. data/spec/support/models/coding/pull_request.rb +11 -0
  923. data/spec/support/models/coding.rb +3 -0
  924. data/spec/support/models/comment.rb +18 -0
  925. data/spec/support/models/company.rb +9 -0
  926. data/spec/support/models/consumption_period.rb +9 -0
  927. data/spec/support/models/contextable_item.rb +8 -0
  928. data/spec/support/models/contractor.rb +8 -0
  929. data/spec/support/models/cookie.rb +8 -0
  930. data/spec/support/models/country_code.rb +12 -0
  931. data/spec/support/models/courier_job.rb +7 -0
  932. data/spec/support/models/cover.rb +10 -0
  933. data/spec/support/models/crate.rb +12 -0
  934. data/spec/support/models/customer.rb +10 -0
  935. data/spec/support/models/customer_address.rb +11 -0
  936. data/spec/support/models/deed.rb +8 -0
  937. data/spec/support/models/definition.rb +12 -0
  938. data/spec/support/models/delegating_patient.rb +15 -0
  939. data/spec/support/models/description.rb +13 -0
  940. data/spec/support/models/dictionary.rb +19 -0
  941. data/spec/support/models/division.rb +13 -0
  942. data/spec/support/models/doctor.rb +14 -0
  943. data/spec/support/models/dog.rb +10 -0
  944. data/spec/support/models/dokument.rb +9 -0
  945. data/spec/support/models/draft.rb +11 -0
  946. data/spec/support/models/dragon.rb +7 -0
  947. data/spec/support/models/driver.rb +10 -0
  948. data/spec/support/models/drug.rb +9 -0
  949. data/spec/support/models/dungeon.rb +7 -0
  950. data/spec/support/models/edit.rb +8 -0
  951. data/spec/support/models/email.rb +9 -0
  952. data/spec/support/models/employer.rb +7 -0
  953. data/spec/support/models/entry.rb +9 -0
  954. data/spec/support/models/eraser.rb +3 -0
  955. data/spec/support/models/even.rb +10 -0
  956. data/spec/support/models/event.rb +24 -0
  957. data/spec/support/models/exhibition.rb +7 -0
  958. data/spec/support/models/exhibitor.rb +9 -0
  959. data/spec/support/models/explosion.rb +7 -0
  960. data/spec/support/models/eye.rb +11 -0
  961. data/spec/support/models/eye_bowl.rb +11 -0
  962. data/spec/support/models/face.rb +10 -0
  963. data/spec/support/models/fanatic.rb +9 -0
  964. data/spec/support/models/favorite.rb +15 -0
  965. data/spec/support/models/filesystem.rb +8 -0
  966. data/spec/support/models/fire_hydrant.rb +9 -0
  967. data/spec/support/models/firefox.rb +6 -0
  968. data/spec/support/models/fish.rb +9 -0
  969. data/spec/support/models/folder.rb +8 -0
  970. data/spec/support/models/folder_item.rb +10 -0
  971. data/spec/support/models/fruits.rb +41 -0
  972. data/spec/support/models/game.rb +21 -0
  973. data/spec/support/models/ghost.rb +9 -0
  974. data/spec/support/models/guitar.rb +4 -0
  975. data/spec/support/models/hole.rb +12 -0
  976. data/spec/support/models/home.rb +7 -0
  977. data/spec/support/models/house.rb +9 -0
  978. data/spec/support/models/html_writer.rb +5 -0
  979. data/spec/support/models/id_key.rb +9 -0
  980. data/spec/support/models/idnodef.rb +7 -0
  981. data/spec/support/models/image.rb +24 -0
  982. data/spec/support/models/implant.rb +27 -0
  983. data/spec/support/models/instrument.rb +8 -0
  984. data/spec/support/models/item.rb +11 -0
  985. data/spec/support/models/jar.rb +9 -0
  986. data/spec/support/models/kaleidoscope.rb +9 -0
  987. data/spec/support/models/kangaroo.rb +7 -0
  988. data/spec/support/models/label.rb +53 -0
  989. data/spec/support/models/language.rb +8 -0
  990. data/spec/support/models/lat_lng.rb +23 -0
  991. data/spec/support/models/league.rb +14 -0
  992. data/spec/support/models/learner.rb +4 -0
  993. data/spec/support/models/line_item.rb +9 -0
  994. data/spec/support/models/location.rb +11 -0
  995. data/spec/support/models/login.rb +10 -0
  996. data/spec/support/models/manufacturer.rb +9 -0
  997. data/spec/support/models/meat.rb +7 -0
  998. data/spec/support/models/membership.rb +8 -0
  999. data/spec/support/models/message.rb +13 -0
  1000. data/spec/support/models/minim.rb +6 -0
  1001. data/spec/support/models/mixed_drink.rb +7 -0
  1002. data/spec/support/models/modifier_models.rb +25 -0
  1003. data/spec/support/models/mop.rb +24 -0
  1004. data/spec/support/models/movie.rb +16 -0
  1005. data/spec/support/models/my_hash.rb +4 -0
  1006. data/spec/support/models/name.rb +36 -0
  1007. data/spec/support/models/name_only.rb +8 -0
  1008. data/spec/support/models/node.rb +8 -0
  1009. data/spec/support/models/note.rb +20 -0
  1010. data/spec/support/models/nut.rb +7 -0
  1011. data/spec/support/models/odd.rb +10 -0
  1012. data/spec/support/models/order.rb +13 -0
  1013. data/spec/support/models/ordered_post.rb +14 -0
  1014. data/spec/support/models/ordered_preference.rb +9 -0
  1015. data/spec/support/models/oscar.rb +17 -0
  1016. data/spec/support/models/other_owner_object.rb +4 -0
  1017. data/spec/support/models/override.rb +11 -0
  1018. data/spec/support/models/ownable.rb +9 -0
  1019. data/spec/support/models/owner.rb +11 -0
  1020. data/spec/support/models/pack.rb +5 -0
  1021. data/spec/support/models/page.rb +19 -0
  1022. data/spec/support/models/page_question.rb +7 -0
  1023. data/spec/support/models/palette.rb +10 -0
  1024. data/spec/support/models/parent.rb +8 -0
  1025. data/spec/support/models/parent_doc.rb +9 -0
  1026. data/spec/support/models/passport.rb +23 -0
  1027. data/spec/support/models/patient.rb +12 -0
  1028. data/spec/support/models/pdf_writer.rb +5 -0
  1029. data/spec/support/models/pencil.rb +3 -0
  1030. data/spec/support/models/person.rb +237 -0
  1031. data/spec/support/models/pet.rb +26 -0
  1032. data/spec/support/models/pet_owner.rb +9 -0
  1033. data/spec/support/models/phone.rb +13 -0
  1034. data/spec/support/models/piano.rb +4 -0
  1035. data/spec/support/models/pizza.rb +10 -0
  1036. data/spec/support/models/player.rb +40 -0
  1037. data/spec/support/models/post.rb +53 -0
  1038. data/spec/support/models/post_genre.rb +9 -0
  1039. data/spec/support/models/powerup.rb +25 -0
  1040. data/spec/support/models/preference.rb +12 -0
  1041. data/spec/support/models/princess.rb +13 -0
  1042. data/spec/support/models/product.rb +23 -0
  1043. data/spec/support/models/profile.rb +19 -0
  1044. data/spec/support/models/pronunciation.rb +8 -0
  1045. data/spec/support/models/pub.rb +9 -0
  1046. data/spec/support/models/publication/encyclopedia.rb +11 -0
  1047. data/spec/support/models/publication/review.rb +13 -0
  1048. data/spec/support/models/publication.rb +4 -0
  1049. data/spec/support/models/purchase.rb +7 -0
  1050. data/spec/support/models/purchased_item.rb +11 -0
  1051. data/spec/support/models/purse.rb +9 -0
  1052. data/spec/support/models/question.rb +11 -0
  1053. data/spec/support/models/quiz.rb +10 -0
  1054. data/spec/support/models/rating.rb +11 -0
  1055. data/spec/support/models/record.rb +54 -0
  1056. data/spec/support/models/registry.rb +8 -0
  1057. data/spec/support/models/role.rb +10 -0
  1058. data/spec/support/models/root_category.rb +7 -0
  1059. data/spec/support/models/sandbox/app/models/app_models_message.rb +3 -0
  1060. data/spec/support/models/sandbox/lib/models/lib_models_message.rb +3 -0
  1061. data/spec/support/models/sandbox/sandbox_message.rb +3 -0
  1062. data/spec/support/models/sandbox/sandbox_user.rb +3 -0
  1063. data/spec/support/models/sandbox/subdir/sandbox_comment.rb +3 -0
  1064. data/spec/support/models/sandwich.rb +12 -0
  1065. data/spec/support/models/scheduler.rb +9 -0
  1066. data/spec/support/models/school.rb +14 -0
  1067. data/spec/support/models/scribe.rb +8 -0
  1068. data/spec/support/models/sealer.rb +7 -0
  1069. data/spec/support/models/seat.rb +24 -0
  1070. data/spec/support/models/seo.rb +12 -0
  1071. data/spec/support/models/series.rb +8 -0
  1072. data/spec/support/models/server.rb +16 -0
  1073. data/spec/support/models/service.rb +25 -0
  1074. data/spec/support/models/shape.rb +15 -0
  1075. data/spec/support/models/shelf.rb +8 -0
  1076. data/spec/support/models/shield.rb +19 -0
  1077. data/spec/support/models/shipment_address.rb +5 -0
  1078. data/spec/support/models/shipping_container.rb +8 -0
  1079. data/spec/support/models/shipping_pack.rb +5 -0
  1080. data/spec/support/models/shirt.rb +11 -0
  1081. data/spec/support/models/shop.rb +9 -0
  1082. data/spec/support/models/short_agent.rb +6 -0
  1083. data/spec/support/models/short_quiz.rb +8 -0
  1084. data/spec/support/models/simple.rb +8 -0
  1085. data/spec/support/models/slave.rb +9 -0
  1086. data/spec/support/models/song.rb +10 -0
  1087. data/spec/support/models/sound.rb +8 -0
  1088. data/spec/support/models/spacer.rb +7 -0
  1089. data/spec/support/models/square.rb +6 -0
  1090. data/spec/support/models/staff.rb +9 -0
  1091. data/spec/support/models/store_as_dup_test1.rb +8 -0
  1092. data/spec/support/models/store_as_dup_test2.rb +8 -0
  1093. data/spec/support/models/store_as_dup_test3.rb +8 -0
  1094. data/spec/support/models/store_as_dup_test4.rb +8 -0
  1095. data/spec/support/models/strategy.rb +5 -0
  1096. data/spec/support/models/student.rb +14 -0
  1097. data/spec/support/models/sub_item.rb +5 -0
  1098. data/spec/support/models/subscription.rb +8 -0
  1099. data/spec/support/models/survey.rb +8 -0
  1100. data/spec/support/models/symptom.rb +9 -0
  1101. data/spec/support/models/system_role.rb +7 -0
  1102. data/spec/support/models/tag.rb +11 -0
  1103. data/spec/support/models/target.rb +8 -0
  1104. data/spec/support/models/template.rb +8 -0
  1105. data/spec/support/models/thing.rb +12 -0
  1106. data/spec/support/models/threadlocker.rb +7 -0
  1107. data/spec/support/models/title.rb +5 -0
  1108. data/spec/support/models/tool.rb +11 -0
  1109. data/spec/support/models/topping.rb +8 -0
  1110. data/spec/support/models/toy.rb +9 -0
  1111. data/spec/support/models/track.rb +41 -0
  1112. data/spec/support/models/translation.rb +8 -0
  1113. data/spec/support/models/tree.rb +11 -0
  1114. data/spec/support/models/truck.rb +7 -0
  1115. data/spec/support/models/user.rb +23 -0
  1116. data/spec/support/models/user_account.rb +13 -0
  1117. data/spec/support/models/validation_callback.rb +13 -0
  1118. data/spec/support/models/vehicle.rb +18 -0
  1119. data/spec/support/models/version.rb +8 -0
  1120. data/spec/support/models/vertex.rb +8 -0
  1121. data/spec/support/models/vet_visit.rb +8 -0
  1122. data/spec/support/models/video.rb +16 -0
  1123. data/spec/support/models/video_game.rb +3 -0
  1124. data/spec/support/models/washer.rb +7 -0
  1125. data/spec/support/models/weapon.rb +25 -0
  1126. data/spec/support/models/wiki_page.rb +17 -0
  1127. data/spec/support/models/word.rb +18 -0
  1128. data/spec/support/models/word_origin.rb +13 -0
  1129. data/spec/support/models/writer.rb +14 -0
  1130. data/spec/support/rails_mock.rb +27 -0
  1131. data/spec/support/schema_maps/schema_map_aws.json +17 -0
  1132. data/spec/support/schema_maps/schema_map_aws_key_alt_names.json +12 -0
  1133. data/spec/support/schema_maps/schema_map_azure.json +17 -0
  1134. data/spec/support/schema_maps/schema_map_azure_key_alt_names.json +12 -0
  1135. data/spec/support/schema_maps/schema_map_gcp.json +17 -0
  1136. data/spec/support/schema_maps/schema_map_gcp_key_alt_names.json +12 -0
  1137. data/spec/support/schema_maps/schema_map_kmip.json +17 -0
  1138. data/spec/support/schema_maps/schema_map_kmip_key_alt_names.json +12 -0
  1139. data/spec/support/schema_maps/schema_map_local.json +18 -0
  1140. data/spec/support/schema_maps/schema_map_local_key_alt_names.json +12 -0
  1141. data/spec/support/shared/time.rb +34 -0
  1142. data/spec/support/sinatra_mock.rb +9 -0
  1143. data/spec/support/spec_config.rb +45 -14
  1144. metadata +1341 -780
  1145. checksums.yaml.gz.sig +0 -0
  1146. data/lib/mongoid/association/referenced/eager.rb +0 -162
  1147. data/lib/mongoid/association/referenced/has_one/nested_builder.rb +0 -126
  1148. data/lib/mongoid/contextual/geo_near.rb +0 -267
  1149. data/lib/mongoid/criteria/queryable/forwardable.rb +0 -65
  1150. data/lib/mongoid/errors/eager_load.rb +0 -22
  1151. data/lib/mongoid/errors/invalid_session_use.rb +0 -24
  1152. data/lib/mongoid/errors/invalid_storage_parent.rb +0 -26
  1153. data/lib/mongoid/errors/invalid_value.rb +0 -16
  1154. data/lib/mongoid/matchable/all.rb +0 -28
  1155. data/lib/mongoid/matchable/and.rb +0 -30
  1156. data/lib/mongoid/matchable/default.rb +0 -119
  1157. data/lib/mongoid/matchable/elem_match.rb +0 -34
  1158. data/lib/mongoid/matchable/eq.rb +0 -22
  1159. data/lib/mongoid/matchable/exists.rb +0 -23
  1160. data/lib/mongoid/matchable/gt.rb +0 -23
  1161. data/lib/mongoid/matchable/gte.rb +0 -23
  1162. data/lib/mongoid/matchable/in.rb +0 -24
  1163. data/lib/mongoid/matchable/lt.rb +0 -23
  1164. data/lib/mongoid/matchable/lte.rb +0 -23
  1165. data/lib/mongoid/matchable/ne.rb +0 -21
  1166. data/lib/mongoid/matchable/nin.rb +0 -22
  1167. data/lib/mongoid/matchable/nor.rb +0 -37
  1168. data/lib/mongoid/matchable/or.rb +0 -33
  1169. data/lib/mongoid/matchable/regexp.rb +0 -27
  1170. data/lib/mongoid/matchable/size.rb +0 -21
  1171. data/lib/mongoid/query_cache.rb +0 -331
  1172. data/lib/support/ruby_version.rb +0 -26
  1173. data/spec/app/models/account.rb +0 -36
  1174. data/spec/app/models/acolyte.rb +0 -17
  1175. data/spec/app/models/actor.rb +0 -18
  1176. data/spec/app/models/actress.rb +0 -2
  1177. data/spec/app/models/address.rb +0 -79
  1178. data/spec/app/models/address_component.rb +0 -5
  1179. data/spec/app/models/address_number.rb +0 -6
  1180. data/spec/app/models/agency.rb +0 -5
  1181. data/spec/app/models/agent.rb +0 -16
  1182. data/spec/app/models/album.rb +0 -18
  1183. data/spec/app/models/alert.rb +0 -7
  1184. data/spec/app/models/animal.rb +0 -26
  1185. data/spec/app/models/answer.rb +0 -6
  1186. data/spec/app/models/appointment.rb +0 -7
  1187. data/spec/app/models/article.rb +0 -12
  1188. data/spec/app/models/artist.rb +0 -87
  1189. data/spec/app/models/artwork.rb +0 -4
  1190. data/spec/app/models/audio.rb +0 -5
  1191. data/spec/app/models/augmentation.rb +0 -11
  1192. data/spec/app/models/author.rb +0 -6
  1193. data/spec/app/models/baby.rb +0 -4
  1194. data/spec/app/models/band.rb +0 -31
  1195. data/spec/app/models/bar.rb +0 -10
  1196. data/spec/app/models/basic.rb +0 -6
  1197. data/spec/app/models/bed.rb +0 -1
  1198. data/spec/app/models/big_palette.rb +0 -2
  1199. data/spec/app/models/birthday.rb +0 -13
  1200. data/spec/app/models/bomb.rb +0 -4
  1201. data/spec/app/models/book.rb +0 -16
  1202. data/spec/app/models/breed.rb +0 -4
  1203. data/spec/app/models/browser.rb +0 -6
  1204. data/spec/app/models/building.rb +0 -6
  1205. data/spec/app/models/building_address.rb +0 -7
  1206. data/spec/app/models/bus.rb +0 -7
  1207. data/spec/app/models/business.rb +0 -5
  1208. data/spec/app/models/callback_test.rb +0 -9
  1209. data/spec/app/models/canvas.rb +0 -25
  1210. data/spec/app/models/car.rb +0 -1
  1211. data/spec/app/models/cat.rb +0 -8
  1212. data/spec/app/models/category.rb +0 -8
  1213. data/spec/app/models/child.rb +0 -4
  1214. data/spec/app/models/child_doc.rb +0 -22
  1215. data/spec/app/models/church.rb +0 -6
  1216. data/spec/app/models/circle.rb +0 -3
  1217. data/spec/app/models/circuit.rb +0 -4
  1218. data/spec/app/models/circus.rb +0 -7
  1219. data/spec/app/models/code.rb +0 -11
  1220. data/spec/app/models/comment.rb +0 -16
  1221. data/spec/app/models/company.rb +0 -5
  1222. data/spec/app/models/consumption_period.rb +0 -7
  1223. data/spec/app/models/contextable_item.rb +0 -5
  1224. data/spec/app/models/contractor.rb +0 -5
  1225. data/spec/app/models/cookie.rb +0 -6
  1226. data/spec/app/models/country_code.rb +0 -8
  1227. data/spec/app/models/courier_job.rb +0 -4
  1228. data/spec/app/models/customer.rb +0 -11
  1229. data/spec/app/models/customer_address.rb +0 -12
  1230. data/spec/app/models/definition.rb +0 -9
  1231. data/spec/app/models/delegating_patient.rb +0 -16
  1232. data/spec/app/models/description.rb +0 -11
  1233. data/spec/app/models/dictionary.rb +0 -10
  1234. data/spec/app/models/division.rb +0 -10
  1235. data/spec/app/models/doctor.rb +0 -12
  1236. data/spec/app/models/dog.rb +0 -7
  1237. data/spec/app/models/dokument.rb +0 -6
  1238. data/spec/app/models/draft.rb +0 -9
  1239. data/spec/app/models/dragon.rb +0 -4
  1240. data/spec/app/models/driver.rb +0 -7
  1241. data/spec/app/models/drug.rb +0 -6
  1242. data/spec/app/models/dungeon.rb +0 -4
  1243. data/spec/app/models/edit.rb +0 -5
  1244. data/spec/app/models/email.rb +0 -6
  1245. data/spec/app/models/employer.rb +0 -5
  1246. data/spec/app/models/entry.rb +0 -6
  1247. data/spec/app/models/eraser.rb +0 -1
  1248. data/spec/app/models/even.rb +0 -7
  1249. data/spec/app/models/event.rb +0 -22
  1250. data/spec/app/models/exhibition.rb +0 -4
  1251. data/spec/app/models/exhibitor.rb +0 -6
  1252. data/spec/app/models/explosion.rb +0 -4
  1253. data/spec/app/models/eye.rb +0 -9
  1254. data/spec/app/models/eye_bowl.rb +0 -9
  1255. data/spec/app/models/face.rb +0 -8
  1256. data/spec/app/models/favorite.rb +0 -6
  1257. data/spec/app/models/filesystem.rb +0 -5
  1258. data/spec/app/models/fire_hydrant.rb +0 -6
  1259. data/spec/app/models/firefox.rb +0 -4
  1260. data/spec/app/models/fish.rb +0 -7
  1261. data/spec/app/models/folder.rb +0 -7
  1262. data/spec/app/models/folder_item.rb +0 -9
  1263. data/spec/app/models/fruits.rb +0 -34
  1264. data/spec/app/models/game.rb +0 -19
  1265. data/spec/app/models/ghost.rb +0 -7
  1266. data/spec/app/models/home.rb +0 -4
  1267. data/spec/app/models/house.rb +0 -6
  1268. data/spec/app/models/html_writer.rb +0 -3
  1269. data/spec/app/models/id_key.rb +0 -6
  1270. data/spec/app/models/image.rb +0 -22
  1271. data/spec/app/models/implant.rb +0 -16
  1272. data/spec/app/models/item.rb +0 -8
  1273. data/spec/app/models/jar.rb +0 -7
  1274. data/spec/app/models/kaleidoscope.rb +0 -6
  1275. data/spec/app/models/kangaroo.rb +0 -4
  1276. data/spec/app/models/label.rb +0 -47
  1277. data/spec/app/models/language.rb +0 -5
  1278. data/spec/app/models/lat_lng.rb +0 -15
  1279. data/spec/app/models/league.rb +0 -11
  1280. data/spec/app/models/learner.rb +0 -2
  1281. data/spec/app/models/line_item.rb +0 -6
  1282. data/spec/app/models/location.rb +0 -8
  1283. data/spec/app/models/login.rb +0 -8
  1284. data/spec/app/models/manufacturer.rb +0 -7
  1285. data/spec/app/models/meat.rb +0 -4
  1286. data/spec/app/models/membership.rb +0 -4
  1287. data/spec/app/models/message.rb +0 -11
  1288. data/spec/app/models/minim.rb +0 -7
  1289. data/spec/app/models/mixed_drink.rb +0 -4
  1290. data/spec/app/models/movie.rb +0 -13
  1291. data/spec/app/models/my_hash.rb +0 -2
  1292. data/spec/app/models/name.rb +0 -24
  1293. data/spec/app/models/node.rb +0 -5
  1294. data/spec/app/models/note.rb +0 -17
  1295. data/spec/app/models/odd.rb +0 -7
  1296. data/spec/app/models/ordered_post.rb +0 -11
  1297. data/spec/app/models/ordered_preference.rb +0 -6
  1298. data/spec/app/models/oscar.rb +0 -14
  1299. data/spec/app/models/other_owner_object.rb +0 -2
  1300. data/spec/app/models/override.rb +0 -16
  1301. data/spec/app/models/ownable.rb +0 -6
  1302. data/spec/app/models/owner.rb +0 -6
  1303. data/spec/app/models/pack.rb +0 -3
  1304. data/spec/app/models/page.rb +0 -16
  1305. data/spec/app/models/page_question.rb +0 -4
  1306. data/spec/app/models/palette.rb +0 -7
  1307. data/spec/app/models/parent.rb +0 -5
  1308. data/spec/app/models/parent_doc.rb +0 -6
  1309. data/spec/app/models/passport.rb +0 -5
  1310. data/spec/app/models/patient.rb +0 -9
  1311. data/spec/app/models/pdf_writer.rb +0 -3
  1312. data/spec/app/models/pencil.rb +0 -1
  1313. data/spec/app/models/person.rb +0 -210
  1314. data/spec/app/models/pet.rb +0 -23
  1315. data/spec/app/models/pet_owner.rb +0 -6
  1316. data/spec/app/models/phone.rb +0 -9
  1317. data/spec/app/models/pizza.rb +0 -7
  1318. data/spec/app/models/player.rb +0 -35
  1319. data/spec/app/models/post.rb +0 -50
  1320. data/spec/app/models/post_genre.rb +0 -6
  1321. data/spec/app/models/powerup.rb +0 -11
  1322. data/spec/app/models/preference.rb +0 -9
  1323. data/spec/app/models/princess.rb +0 -10
  1324. data/spec/app/models/product.rb +0 -17
  1325. data/spec/app/models/profile.rb +0 -5
  1326. data/spec/app/models/pronunciation.rb +0 -5
  1327. data/spec/app/models/pub.rb +0 -6
  1328. data/spec/app/models/purchase.rb +0 -4
  1329. data/spec/app/models/question.rb +0 -8
  1330. data/spec/app/models/quiz.rb +0 -7
  1331. data/spec/app/models/rating.rb +0 -8
  1332. data/spec/app/models/record.rb +0 -52
  1333. data/spec/app/models/registry.rb +0 -4
  1334. data/spec/app/models/role.rb +0 -7
  1335. data/spec/app/models/root_category.rb +0 -4
  1336. data/spec/app/models/sandwich.rb +0 -9
  1337. data/spec/app/models/scheduler.rb +0 -7
  1338. data/spec/app/models/seo.rb +0 -7
  1339. data/spec/app/models/series.rb +0 -4
  1340. data/spec/app/models/server.rb +0 -13
  1341. data/spec/app/models/service.rb +0 -22
  1342. data/spec/app/models/shape.rb +0 -12
  1343. data/spec/app/models/shelf.rb +0 -5
  1344. data/spec/app/models/shipment_address.rb +0 -3
  1345. data/spec/app/models/shipping_container.rb +0 -5
  1346. data/spec/app/models/shipping_pack.rb +0 -3
  1347. data/spec/app/models/shop.rb +0 -6
  1348. data/spec/app/models/short_agent.rb +0 -4
  1349. data/spec/app/models/short_quiz.rb +0 -5
  1350. data/spec/app/models/simple.rb +0 -5
  1351. data/spec/app/models/slave.rb +0 -6
  1352. data/spec/app/models/song.rb +0 -8
  1353. data/spec/app/models/sound.rb +0 -5
  1354. data/spec/app/models/square.rb +0 -4
  1355. data/spec/app/models/staff.rb +0 -7
  1356. data/spec/app/models/store_as_dup_test1.rb +0 -5
  1357. data/spec/app/models/store_as_dup_test2.rb +0 -5
  1358. data/spec/app/models/store_as_dup_test3.rb +0 -7
  1359. data/spec/app/models/store_as_dup_test4.rb +0 -7
  1360. data/spec/app/models/strategy.rb +0 -3
  1361. data/spec/app/models/sub_item.rb +0 -3
  1362. data/spec/app/models/subscription.rb +0 -5
  1363. data/spec/app/models/survey.rb +0 -5
  1364. data/spec/app/models/symptom.rb +0 -6
  1365. data/spec/app/models/tag.rb +0 -8
  1366. data/spec/app/models/target.rb +0 -5
  1367. data/spec/app/models/template.rb +0 -5
  1368. data/spec/app/models/thing.rb +0 -9
  1369. data/spec/app/models/title.rb +0 -3
  1370. data/spec/app/models/tool.rb +0 -8
  1371. data/spec/app/models/topping.rb +0 -5
  1372. data/spec/app/models/track.rb +0 -38
  1373. data/spec/app/models/translation.rb +0 -5
  1374. data/spec/app/models/tree.rb +0 -9
  1375. data/spec/app/models/truck.rb +0 -3
  1376. data/spec/app/models/user.rb +0 -21
  1377. data/spec/app/models/user_account.rb +0 -10
  1378. data/spec/app/models/validation_callback.rb +0 -10
  1379. data/spec/app/models/vehicle.rb +0 -11
  1380. data/spec/app/models/version.rb +0 -5
  1381. data/spec/app/models/vertex.rb +0 -6
  1382. data/spec/app/models/vet_visit.rb +0 -5
  1383. data/spec/app/models/video.rb +0 -13
  1384. data/spec/app/models/video_game.rb +0 -1
  1385. data/spec/app/models/weapon.rb +0 -11
  1386. data/spec/app/models/wiki_page.rb +0 -14
  1387. data/spec/app/models/word.rb +0 -15
  1388. data/spec/app/models/word_origin.rb +0 -11
  1389. data/spec/app/models/writer.rb +0 -11
  1390. data/spec/integration/matchable_spec.rb +0 -680
  1391. data/spec/mongoid/contextual/geo_near_spec.rb +0 -469
  1392. data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +0 -58
  1393. data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +0 -58
  1394. data/spec/mongoid/criteria/queryable/forwardable_spec.rb +0 -87
  1395. data/spec/mongoid/errors/eager_load_spec.rb +0 -29
  1396. data/spec/mongoid/extensions_spec.rb +0 -29
  1397. data/spec/mongoid/matchable/all_spec.rb +0 -31
  1398. data/spec/mongoid/matchable/and_spec.rb +0 -187
  1399. data/spec/mongoid/matchable/default_spec.rb +0 -137
  1400. data/spec/mongoid/matchable/elem_match_spec.rb +0 -106
  1401. data/spec/mongoid/matchable/eq_spec.rb +0 -48
  1402. data/spec/mongoid/matchable/exists_spec.rb +0 -57
  1403. data/spec/mongoid/matchable/gt_spec.rb +0 -86
  1404. data/spec/mongoid/matchable/gte_spec.rb +0 -84
  1405. data/spec/mongoid/matchable/in_spec.rb +0 -49
  1406. data/spec/mongoid/matchable/lt_spec.rb +0 -85
  1407. data/spec/mongoid/matchable/lte_spec.rb +0 -85
  1408. data/spec/mongoid/matchable/ne_spec.rb +0 -46
  1409. data/spec/mongoid/matchable/nin_spec.rb +0 -48
  1410. data/spec/mongoid/matchable/nor_spec.rb +0 -209
  1411. data/spec/mongoid/matchable/or_spec.rb +0 -131
  1412. data/spec/mongoid/matchable/regexp_spec.rb +0 -59
  1413. data/spec/mongoid/matchable/size_spec.rb +0 -25
  1414. data/spec/mongoid/matchable_spec.rb +0 -853
  1415. data/spec/mongoid/query_cache_spec.rb +0 -618
  1416. data/spec/support/cluster_config.rb +0 -158
  1417. data/spec/support/session_registry.rb +0 -50
  1418. data.tar.gz.sig +0 -0
  1419. metadata.gz.sig +0 -1
  1420. /data/spec/{app → support}/models/array_field.rb +0 -0
  1421. /data/spec/{app → support}/models/updatable.rb +0 -0
@@ -1,15 +1,16 @@
1
- require "spec_helper"
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require_relative '../has_and_belongs_to_many_models'
2
5
 
3
6
  describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
7
+ config_override :raise_not_found_error, true
4
8
 
5
- around(:each) do |example|
6
- original_raise_not_found_error = Mongoid.raise_not_found_error
7
- original_preferences_association = Person.relations["preferences"]
8
- Mongoid.raise_not_found_error = true
9
+ around do |example|
10
+ original_preferences_association = Person.relations['preferences']
9
11
  Person.has_and_belongs_to_many :preferences, autosave: true
10
12
  example.run
11
- Mongoid.raise_not_found_error = original_raise_not_found_error
12
- Person.relations["preferences"] = original_preferences_association
13
+ Person.relations['preferences'] = original_preferences_association
13
14
  end
14
15
 
15
16
  after(:all) do
@@ -17,18 +18,14 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
17
18
  Person.reset_callbacks(:destroy)
18
19
  end
19
20
 
20
- [ :<<, :push ].each do |method|
21
-
21
+ %i[<< push].each do |method|
22
22
  describe "##{method}" do
23
-
24
- context "when the inverse_of is nil" do
25
-
23
+ context 'when the inverse_of is nil' do
26
24
  let!(:article) do
27
- Article.create
25
+ Article.create!
28
26
  end
29
27
 
30
- context "when the child document is new" do
31
-
28
+ context 'when the child document is new' do
32
29
  let(:preference) do
33
30
  Preference.new
34
31
  end
@@ -37,26 +34,24 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
37
34
  article.preferences.send(method, preference)
38
35
  end
39
36
 
40
- it "persists the child document" do
37
+ it 'persists the child document' do
41
38
  expect(preference).to be_persisted
42
39
  end
43
40
  end
44
41
 
45
- context "when the child document is not new" do
46
-
42
+ context 'when the child document is not new' do
47
43
  let(:preference) do
48
- Preference.create
44
+ Preference.create!
49
45
  end
50
46
 
51
- it "does not persist the child document" do
52
- expect(preference).to receive(:save).never
47
+ it 'does not persist the child document' do
48
+ expect(preference).not_to receive(:save)
53
49
  article.preferences.send(method, preference)
54
50
  end
55
51
  end
56
52
  end
57
53
 
58
- context "when the parent is a new record" do
59
-
54
+ context 'when the parent is a new record' do
60
55
  let(:person) do
61
56
  Person.new
62
57
  end
@@ -69,15 +64,14 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
69
64
  person.preferences.send(method, preference)
70
65
  end
71
66
 
72
- it "returns an array of loaded documents" do
67
+ it 'returns an array of loaded documents' do
73
68
  expect(result).to eq([ preference ])
74
69
  end
75
70
  end
76
71
 
77
- context "when the parent is not a new record" do
78
-
72
+ context 'when the parent is not a new record' do
79
73
  let(:person) do
80
- Person.create
74
+ Person.create!
81
75
  end
82
76
 
83
77
  let!(:preference) do
@@ -88,15 +82,13 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
88
82
  person.preferences.send(method, preference)
89
83
  end
90
84
 
91
- it "returns an array of loaded documents" do
85
+ it 'returns an array of loaded documents' do
92
86
  expect(result).to eq([ preference ])
93
87
  end
94
88
  end
95
89
 
96
- context "when the relations are not polymorphic" do
97
-
98
- context "when the inverse relation is not defined" do
99
-
90
+ context 'when the relations are not polymorphic' do
91
+ context 'when the inverse relation is not defined' do
100
92
  let(:person) do
101
93
  Person.new
102
94
  end
@@ -109,60 +101,57 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
109
101
  person.houses << house
110
102
  end
111
103
 
112
- it "appends the document to the relation" do
104
+ it 'appends the document to the relation' do
113
105
  expect(person.houses).to eq([ house ])
114
106
  end
115
107
 
116
- it "sets the foreign key on the relation" do
108
+ it 'sets the foreign key on the relation' do
117
109
  expect(person.house_ids).to eq([ house.id ])
118
110
  end
119
111
  end
120
112
 
121
- context "when appending in a parent create block" do
122
-
113
+ context 'when appending in a parent create block' do
123
114
  let!(:preference) do
124
- Preference.create(name: "testing")
115
+ Preference.create!(name: 'testing')
125
116
  end
126
117
 
127
118
  let!(:person) do
128
- Person.create do |doc|
119
+ Person.create! do |doc|
129
120
  doc.preferences << preference
130
121
  end
131
122
  end
132
123
 
133
- it "adds the documents to the relation" do
124
+ it 'adds the documents to the relation' do
134
125
  expect(person.preferences).to eq([ preference ])
135
126
  end
136
127
 
137
- it "sets the foreign key on the relation" do
128
+ it 'sets the foreign key on the relation' do
138
129
  expect(person.preference_ids).to eq([ preference.id ])
139
130
  end
140
131
 
141
- it "sets the foreign key on the inverse relation" do
132
+ it 'sets the foreign key on the inverse relation' do
142
133
  expect(preference.person_ids).to eq([ person.id ])
143
134
  end
144
135
 
145
- it "saves the target" do
136
+ it 'saves the target' do
146
137
  expect(preference).to be_persisted
147
138
  end
148
139
 
149
- it "adds the correct number of documents" do
140
+ it 'adds the correct number of documents' do
150
141
  expect(person.preferences.size).to eq(1)
151
142
  end
152
143
 
153
- it "persists the link" do
144
+ it 'persists the link' do
154
145
  expect(person.reload.preferences).to eq([ preference ])
155
146
  end
156
147
  end
157
148
 
158
- context "when the parent is a new record" do
159
-
149
+ context 'when the parent is a new record' do
160
150
  let(:person) do
161
151
  Person.new
162
152
  end
163
153
 
164
- context "when the child is new" do
165
-
154
+ context 'when the child is new' do
166
155
  let(:preference) do
167
156
  Preference.new
168
157
  end
@@ -171,46 +160,44 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
171
160
  person.preferences.send(method, preference)
172
161
  end
173
162
 
174
- it "adds the documents to the relation" do
163
+ it 'adds the documents to the relation' do
175
164
  expect(person.preferences).to eq([ preference ])
176
165
  end
177
166
 
178
- it "sets the foreign key on the relation" do
167
+ it 'sets the foreign key on the relation' do
179
168
  expect(person.preference_ids).to eq([ preference.id ])
180
169
  end
181
170
 
182
- it "sets the foreign key on the inverse relation" do
171
+ it 'sets the foreign key on the inverse relation' do
183
172
  expect(preference.person_ids).to eq([ person.id ])
184
173
  end
185
174
 
186
- it "does not save the target" do
175
+ it 'does not save the target' do
187
176
  expect(preference).to be_new_record
188
177
  end
189
178
 
190
- it "adds the correct number of documents" do
179
+ it 'adds the correct number of documents' do
191
180
  expect(person.preferences.size).to eq(1)
192
181
  end
193
182
 
194
- context "when appending a second time" do
195
-
183
+ context 'when appending a second time' do
196
184
  before do
197
185
  person.preferences.send(method, preference)
198
186
  end
199
187
 
200
- it "does not allow the document to be added again" do
188
+ it 'does not allow the document to be added again' do
201
189
  expect(person.preferences).to eq([ preference ])
202
190
  end
203
191
 
204
- it "does not allow duplicate ids" do
192
+ it 'does not allow duplicate ids' do
205
193
  expect(person.preference_ids).to eq([ preference.id ])
206
194
  end
207
195
  end
208
196
  end
209
197
 
210
- context "when the child is already persisted" do
211
-
198
+ context 'when the child is already persisted' do
212
199
  let!(:persisted) do
213
- Preference.create(name: "testy")
200
+ Preference.create!(name: 'testy')
214
201
  end
215
202
 
216
203
  let(:preference) do
@@ -219,38 +206,37 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
219
206
 
220
207
  before do
221
208
  person.preferences.send(method, preference)
222
- person.save
209
+ person.save!
223
210
  end
224
211
 
225
- it "adds the documents to the relation" do
212
+ it 'adds the documents to the relation' do
226
213
  expect(person.preferences).to eq([ preference ])
227
214
  end
228
215
 
229
- it "sets the foreign key on the relation" do
216
+ it 'sets the foreign key on the relation' do
230
217
  expect(person.preference_ids).to eq([ preference.id ])
231
218
  end
232
219
 
233
- it "sets the foreign key on the inverse relation" do
220
+ it 'sets the foreign key on the inverse relation' do
234
221
  expect(preference.person_ids).to eq([ person.id ])
235
222
  end
236
223
 
237
- it "saves the target" do
224
+ it 'saves the target' do
238
225
  expect(preference).to be_persisted
239
226
  end
240
227
 
241
- it "adds the correct number of documents" do
228
+ it 'adds the correct number of documents' do
242
229
  expect(person.preferences.size).to eq(1)
243
230
  end
244
231
 
245
- it "persists the link" do
232
+ it 'persists the link' do
246
233
  expect(person.reload.preferences).to eq([ preference ])
247
234
  end
248
235
  end
249
236
 
250
- context "when setting via the associated ids" do
251
-
237
+ context 'when setting via the associated ids' do
252
238
  let!(:persisted) do
253
- Preference.create(name: "testy")
239
+ Preference.create!(name: 'testy')
254
240
  end
255
241
 
256
242
  let(:preference) do
@@ -262,35 +248,34 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
262
248
  end
263
249
 
264
250
  before do
265
- person.save
251
+ person.save!
266
252
  end
267
253
 
268
- it "adds the documents to the relation" do
254
+ it 'adds the documents to the relation' do
269
255
  expect(person.preferences).to eq([ preference ])
270
256
  end
271
257
 
272
- it "sets the foreign key on the relation" do
258
+ it 'sets the foreign key on the relation' do
273
259
  expect(person.preference_ids).to eq([ preference.id ])
274
260
  end
275
261
 
276
- it "sets the foreign key on the inverse relation" do
262
+ it 'sets the foreign key on the inverse relation' do
277
263
  expect(preference.reload.person_ids).to eq([ person.id ])
278
264
  end
279
265
 
280
- it "adds the correct number of documents" do
266
+ it 'adds the correct number of documents' do
281
267
  expect(person.preferences.size).to eq(1)
282
268
  end
283
269
 
284
- it "persists the link" do
270
+ it 'persists the link' do
285
271
  expect(person.reload.preferences).to eq([ preference ])
286
272
  end
287
273
  end
288
274
  end
289
275
 
290
- context "when the parent is not a new record" do
291
-
276
+ context 'when the parent is not a new record' do
292
277
  let(:person) do
293
- Person.create
278
+ Person.create!
294
279
  end
295
280
 
296
281
  let(:preference) do
@@ -301,36 +286,35 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
301
286
  person.preferences.send(method, preference)
302
287
  end
303
288
 
304
- it "adds the documents to the relation" do
289
+ it 'adds the documents to the relation' do
305
290
  expect(person.preferences).to eq([ preference ])
306
291
  end
307
292
 
308
- it "sets the foreign key on the relation" do
293
+ it 'sets the foreign key on the relation' do
309
294
  expect(person.preference_ids).to eq([ preference.id ])
310
295
  end
311
296
 
312
- it "sets the foreign key on the inverse relation" do
297
+ it 'sets the foreign key on the inverse relation' do
313
298
  expect(preference.person_ids).to eq([ person.id ])
314
299
  end
315
300
 
316
- it "sets the base on the inverse relation" do
301
+ it 'sets the base on the inverse relation' do
317
302
  expect(preference.people).to eq([ person ])
318
303
  end
319
304
 
320
- it "sets the same instance on the inverse relation" do
305
+ it 'sets the same instance on the inverse relation' do
321
306
  expect(preference.people.first).to eql(person)
322
307
  end
323
308
 
324
- it "saves the target" do
325
- expect(preference).to_not be_new_record
309
+ it 'saves the target' do
310
+ expect(preference).not_to be_new_record
326
311
  end
327
312
 
328
- it "adds the document to the target" do
313
+ it 'adds the document to the target' do
329
314
  expect(person.preferences.count).to eq(1)
330
315
  end
331
316
 
332
- context "when documents already exist on the relation" do
333
-
317
+ context 'when documents already exist on the relation' do
334
318
  let(:preference_two) do
335
319
  Preference.new
336
320
  end
@@ -339,71 +323,68 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
339
323
  person.preferences.send(method, preference_two)
340
324
  end
341
325
 
342
- it "adds the documents to the relation" do
326
+ it 'adds the documents to the relation' do
343
327
  expect(person.preferences).to eq([ preference, preference_two ])
344
328
  end
345
329
 
346
- it "sets the foreign key on the relation" do
330
+ it 'sets the foreign key on the relation' do
347
331
  expect(person.preference_ids).to eq([ preference.id, preference_two.id ])
348
332
  end
349
333
 
350
- it "sets the foreign key on the inverse relation" do
334
+ it 'sets the foreign key on the inverse relation' do
351
335
  expect(preference_two.person_ids).to eq([ person.id ])
352
336
  end
353
337
 
354
- it "sets the base on the inverse relation" do
338
+ it 'sets the base on the inverse relation' do
355
339
  expect(preference_two.people).to eq([ person ])
356
340
  end
357
341
 
358
- it "sets the same instance on the inverse relation" do
342
+ it 'sets the same instance on the inverse relation' do
359
343
  expect(preference_two.people.first).to eql(person)
360
344
  end
361
345
 
362
- it "saves the target" do
363
- expect(preference).to_not be_new_record
346
+ it 'saves the target' do
347
+ expect(preference).not_to be_new_record
364
348
  end
365
349
 
366
- it "adds the document to the target" do
350
+ it 'adds the document to the target' do
367
351
  expect(person.preferences.count).to eq(2)
368
352
  end
369
353
  end
370
354
  end
371
355
 
372
- context "when both sides have been persisted" do
373
-
356
+ context 'when both sides have been persisted' do
374
357
  let(:person) do
375
- Person.create
358
+ Person.create!
376
359
  end
377
360
 
378
361
  let(:event) do
379
- Event.create
362
+ Event.create!
380
363
  end
381
364
 
382
365
  before do
383
366
  person.administrated_events << event
384
367
  end
385
368
 
386
- it "sets the front side of the relation" do
369
+ it 'sets the front side of the relation' do
387
370
  expect(person.administrated_events).to eq([ event ])
388
371
  end
389
372
 
390
- it "sets the inverse side of the relation" do
373
+ it 'sets the inverse side of the relation' do
391
374
  expect(event.administrators(true)).to eq([ person ])
392
375
  end
393
376
 
394
- context "when reloading" do
395
-
396
- it "sets the front side of the relation" do
377
+ context 'when reloading' do
378
+ it 'sets the front side of the relation' do
397
379
  expect(person.reload.administrated_events).to eq([ event ])
398
380
  end
399
381
 
400
- it "sets the inverse side of the relation" do
382
+ it 'sets the inverse side of the relation' do
401
383
  expect(event.reload.administrators).to eq([ person ])
402
384
  end
403
385
  end
404
386
 
405
- context "when performing a new database query" do
406
-
387
+ context 'when performing a new database query' do
407
388
  let(:loaded_person) do
408
389
  Person.find(person.id)
409
390
  end
@@ -412,74 +393,70 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
412
393
  Event.find(event.id)
413
394
  end
414
395
 
415
- it "sets the front side of the relation" do
396
+ it 'sets the front side of the relation' do
416
397
  expect(loaded_person.administrated_events).to eq([ event ])
417
398
  end
418
399
 
419
- it "sets the inverse side of the relation" do
400
+ it 'sets the inverse side of the relation' do
420
401
  expect(loaded_event.administrators).to eq([ person ])
421
402
  end
422
403
  end
423
404
  end
424
405
 
425
- context "when the relation also includes a has_many relation" do
426
-
406
+ context 'when the relation also includes a has_many relation' do
427
407
  let(:artwork) do
428
- Artwork.create
408
+ Artwork.create!
429
409
  end
430
410
 
431
411
  let(:exhibition) do
432
- Exhibition.create
412
+ Exhibition.create!
433
413
  end
434
414
 
435
415
  let(:exhibitor) do
436
- Exhibitor.create(exhibition: exhibition)
416
+ Exhibitor.create!(exhibition: exhibition)
437
417
  end
438
418
 
439
419
  before do
440
420
  artwork.exhibitors << exhibitor
441
421
  end
442
422
 
443
- it "creates a single artwork object" do
423
+ it 'creates a single artwork object' do
444
424
  expect(Artwork.count).to eq(1)
445
425
  end
446
426
  end
447
427
 
448
- context "when the relation is self referencing" do
449
-
428
+ context 'when the relation is self referencing' do
450
429
  let(:tag_one) do
451
- Tag.create(text: "one")
430
+ Tag.create!(text: 'one')
452
431
  end
453
432
 
454
433
  let(:tag_two) do
455
- Tag.create(text: "two")
434
+ Tag.create!(text: 'two')
456
435
  end
457
436
 
458
437
  before do
459
438
  tag_one.related << tag_two
460
439
  end
461
440
 
462
- it "sets the front side of the relation" do
441
+ it 'sets the front side of the relation' do
463
442
  expect(tag_one.related).to eq([ tag_two ])
464
443
  end
465
444
 
466
- it "sets the inverse side of the relation" do
445
+ it 'sets the inverse side of the relation' do
467
446
  expect(tag_two.related(true)).to eq([ tag_one ])
468
447
  end
469
448
 
470
- context "when reloading" do
471
-
472
- it "sets the front side of the relation" do
449
+ context 'when reloading' do
450
+ it 'sets the front side of the relation' do
473
451
  expect(tag_one.reload.related).to eq([ tag_two ])
474
452
  end
475
453
 
476
- it "sets the inverse side of the relation" do
454
+ it 'sets the inverse side of the relation' do
477
455
  expect(tag_two.reload.related).to eq([ tag_one ])
478
456
  end
479
457
  end
480
458
 
481
- context "when performing a new database query" do
482
-
459
+ context 'when performing a new database query' do
483
460
  let(:loaded_tag_one) do
484
461
  Tag.find(tag_one.id)
485
462
  end
@@ -488,19 +465,18 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
488
465
  Tag.find(tag_two.id)
489
466
  end
490
467
 
491
- it "sets the front side of the relation" do
468
+ it 'sets the front side of the relation' do
492
469
  expect(loaded_tag_one.related).to eq([ tag_two ])
493
470
  end
494
471
 
495
- it "sets the inverse side of the relation" do
472
+ it 'sets the inverse side of the relation' do
496
473
  expect(loaded_tag_two.related).to eq([ tag_one ])
497
474
  end
498
475
  end
499
476
  end
500
477
  end
501
478
 
502
- context "when association has callbacks" do
503
-
479
+ context 'when association has callbacks' do
504
480
  let(:post) do
505
481
  Post.new
506
482
  end
@@ -509,41 +485,37 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
509
485
  Tag.new
510
486
  end
511
487
 
512
- context "when the callback is a before_add" do
513
-
514
- it "executes the callback" do
488
+ context 'when the callback is a before_add' do
489
+ it 'executes the callback' do
515
490
  post.tags.send(method, tag)
516
491
  expect(post.before_add_called).to be true
517
492
  end
518
493
 
519
- context "when errors are raised" do
520
-
494
+ context 'when errors are raised' do
521
495
  before do
522
496
  expect(post).to receive(:before_add_tag).and_raise
523
- begin; post.tags.send(method, tag); rescue; end;
497
+ begin; post.tags.send(method, tag); rescue StandardError; end
524
498
  end
525
499
 
526
- it "does not add the document to the relation" do
500
+ it 'does not add the document to the relation' do
527
501
  expect(post.tags).to be_empty
528
502
  end
529
503
  end
530
504
  end
531
505
 
532
- context "when the callback is an after_add" do
533
-
534
- it "executes the callback" do
506
+ context 'when the callback is an after_add' do
507
+ it 'executes the callback' do
535
508
  post.tags.send(method, tag)
536
509
  expect(post.after_add_called).to be true
537
510
  end
538
511
 
539
- context "when errors are raised" do
540
-
512
+ context 'when errors are raised' do
541
513
  before do
542
514
  expect(post).to receive(:after_add_tag).and_raise
543
- begin; post.tags.send(method, tag); rescue; end
515
+ begin; post.tags.send(method, tag); rescue StandardError; end
544
516
  end
545
517
 
546
- it "adds the document to the relation" do
518
+ it 'adds the document to the relation' do
547
519
  expect(post.tags).to eq([ tag ])
548
520
  end
549
521
  end
@@ -552,60 +524,55 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
552
524
  end
553
525
  end
554
526
 
555
- describe "#=" do
556
-
557
- context "when trying to add duplicate entries" do
558
-
527
+ describe '#=' do
528
+ context 'when trying to add duplicate entries' do
559
529
  let(:person) do
560
530
  Person.new
561
531
  end
562
532
 
563
533
  let(:preference) do
564
- Preference.create(name: "one")
534
+ Preference.create!(name: 'one')
565
535
  end
566
536
 
567
537
  before do
568
538
  person.preferences = [ preference, preference ]
569
539
  end
570
540
 
571
- context "when the document is new" do
572
-
573
- it "does not add the duplicates" do
541
+ context 'when the document is new' do
542
+ it 'does not add the duplicates' do
574
543
  expect(person.preferences).to eq([ preference ])
575
544
  end
576
545
 
577
- it "does not create duplicate keys" do
546
+ it 'does not create duplicate keys' do
578
547
  expect(person.preference_ids).to eq([ preference.id ])
579
548
  end
580
549
  end
581
550
 
582
- context "when the document is persisted" do
583
-
551
+ context 'when the document is persisted' do
584
552
  before do
585
- person.save
553
+ person.save!
586
554
  end
587
555
 
588
- it "does not add the duplicates" do
556
+ it 'does not add the duplicates' do
589
557
  expect(person.preferences).to eq([ preference ])
590
558
  end
591
559
 
592
- it "does not create duplicate keys" do
560
+ it 'does not create duplicate keys' do
593
561
  expect(person.preference_ids).to eq([ preference.id ])
594
562
  end
595
563
 
596
- it "does not add duplicates on the inverse" do
564
+ it 'does not add duplicates on the inverse' do
597
565
  expect(preference.people).to eq([ person ])
598
566
  end
599
567
 
600
- it "does not add duplicate inverse keys" do
568
+ it 'does not add duplicate inverse keys' do
601
569
  expect(preference.person_ids).to eq([ person.id ])
602
570
  end
603
571
 
604
- context "when reloading document from db" do
605
-
572
+ context 'when reloading document from db' do
606
573
  let(:from_db) { Preference.last }
607
574
 
608
- it "does not create duplicate keys" do
575
+ it 'does not create duplicate keys' do
609
576
  person.preferences = [ from_db ]
610
577
  expect(from_db.person_ids).to eq([ person.id ])
611
578
  end
@@ -613,10 +580,8 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
613
580
  end
614
581
  end
615
582
 
616
- context "when the relation is not polymorphic" do
617
-
618
- context "when the parent and relation are new records" do
619
-
583
+ context 'when the relation is not polymorphic' do
584
+ context 'when the parent and relation are new records' do
620
585
  let(:person) do
621
586
  Person.new
622
587
  end
@@ -629,78 +594,75 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
629
594
  person.preferences = [ preference ]
630
595
  end
631
596
 
632
- it "sets the relation" do
597
+ it 'sets the relation' do
633
598
  expect(person.preferences).to eq([ preference ])
634
599
  end
635
600
 
636
- it "sets the foreign key on the relation" do
601
+ it 'sets the foreign key on the relation' do
637
602
  expect(person.preference_ids).to eq([ preference.id ])
638
603
  end
639
604
 
640
- it "sets the foreign key on the inverse relation" do
605
+ it 'sets the foreign key on the inverse relation' do
641
606
  expect(preference.person_ids).to eq([ person.id ])
642
607
  end
643
608
 
644
- it "does not save the target" do
609
+ it 'does not save the target' do
645
610
  expect(preference).to be_new_record
646
611
  end
647
612
  end
648
613
 
649
- context "when the parent is new but the relation exists" do
650
-
614
+ context 'when the parent is new but the relation exists' do
651
615
  let(:person) do
652
616
  Person.new
653
617
  end
654
618
 
655
619
  let!(:preference) do
656
- Preference.create
620
+ Preference.create!
657
621
  end
658
622
 
659
623
  before do
660
624
  person.preferences = [ preference ]
661
625
  end
662
626
 
663
- it "sets the relation" do
627
+ it 'sets the relation' do
664
628
  expect(person.preferences).to eq([ preference ])
665
629
  end
666
630
 
667
- it "sets the foreign key on the relation" do
631
+ it 'sets the foreign key on the relation' do
668
632
  expect(person.preference_ids).to eq([ preference.id ])
669
633
  end
670
634
 
671
- it "sets the foreign key on the inverse relation" do
635
+ it 'sets the foreign key on the inverse relation' do
672
636
  expect(preference.person_ids).to eq([ person.id ])
673
637
  end
674
638
 
675
- context "and the parent is persisted" do
676
-
639
+ context 'and the parent is persisted' do
677
640
  before do
678
641
  person.save!
679
642
  preference.reload
680
643
  end
681
644
 
682
- it "maintains the relation" do
645
+ it 'maintains the relation' do
683
646
  expect(person.preferences).to eq([ preference ])
684
647
  end
685
648
 
686
- it "maintains the foreign key on the relation" do
649
+ it 'maintains the foreign key on the relation' do
687
650
  expect(person.preference_ids).to eq([ preference.id ])
688
651
  end
689
652
 
690
- it "maintains the foreign key on the inverse relation" do
653
+ it 'maintains the foreign key on the inverse relation' do
691
654
  expect(preference.person_ids).to eq([ person.id ])
692
655
  end
693
656
 
694
- it "maintains the base on the inverse relation" do
657
+ it 'maintains the base on the inverse relation' do
695
658
  expect(preference.people.first).to eq(person)
696
659
  end
697
660
  end
698
661
  end
699
662
 
700
- context "when the parent is not a new record" do
701
-
663
+ context 'when the parent is not a new record' do
702
664
  let(:person) do
703
- Person.create
665
+ Person.create!
704
666
  end
705
667
 
706
668
  let(:preference) do
@@ -711,32 +673,31 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
711
673
  person.preferences = [ preference ]
712
674
  end
713
675
 
714
- it "sets the relation" do
676
+ it 'sets the relation' do
715
677
  expect(person.preferences).to eq([ preference ])
716
678
  end
717
679
 
718
- it "sets the foreign key on the relation" do
680
+ it 'sets the foreign key on the relation' do
719
681
  expect(person.preference_ids).to eq([ preference.id ])
720
682
  end
721
683
 
722
- it "sets the foreign key on the inverse relation" do
684
+ it 'sets the foreign key on the inverse relation' do
723
685
  expect(preference.person_ids).to eq([ person.id ])
724
686
  end
725
687
 
726
- it "sets the base on the inverse relation" do
688
+ it 'sets the base on the inverse relation' do
727
689
  expect(preference.people.first).to eq(person)
728
690
  end
729
691
 
730
- it "saves the target" do
692
+ it 'saves the target' do
731
693
  expect(preference).to be_persisted
732
694
  end
733
695
 
734
- it "persists the relation" do
696
+ it 'persists the relation' do
735
697
  person.reload.preferences == [ preference ]
736
698
  end
737
699
 
738
- context "when overwriting an existing relation" do
739
-
700
+ context 'when overwriting an existing relation' do
740
701
  let(:another_preference) do
741
702
  Preference.new
742
703
  end
@@ -745,71 +706,67 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
745
706
  person.preferences = [ another_preference ]
746
707
  end
747
708
 
748
- it "sets the relation" do
709
+ it 'sets the relation' do
749
710
  expect(person.preferences).to eq([ another_preference ])
750
711
  end
751
712
 
752
- it "saves the target" do
713
+ it 'saves the target' do
753
714
  expect(another_preference).to be_persisted
754
715
  end
755
716
 
756
- it "does not leave foreign keys of the previous relation" do
717
+ it 'does not leave foreign keys of the previous relation' do
757
718
  expect(person.preference_ids).to eq([ another_preference.id ])
758
719
  end
759
720
 
760
- it "clears its own key on the foreign relation" do
721
+ it 'clears its own key on the foreign relation' do
761
722
  expect(preference.person_ids).to be_empty
762
723
  end
763
724
 
764
- context "and then overwriting it again with the same value" do
765
-
725
+ context 'and then overwriting it again with the same value' do
766
726
  before do
767
727
  person.preferences = [ another_preference ]
768
728
  end
769
729
 
770
- it "persists the relation between another_preference and person" do
730
+ it 'persists the relation between another_preference and person' do
771
731
  expect(another_preference.reload.people).to eq([ person ])
772
732
  end
773
-
774
733
  end
775
734
 
776
- context "and person reloaded instead of saved" do
777
-
735
+ context 'and person reloaded instead of saved' do
778
736
  before do
779
737
  person.reload
780
738
  another_preference.reload
781
739
  end
782
740
 
783
- it "persists the relation between person and another_preference" do
741
+ it 'persists the relation between person and another_preference' do
784
742
  expect(person.preferences).to eq([ another_preference ])
785
743
  end
786
744
 
787
- it "persists the relation between another_prefrence and person" do
745
+ it 'persists the relation between another_preference and person' do
788
746
  expect(another_preference.people).to eq([ person ])
789
747
  end
790
748
 
791
- it "no longer has any relation between preference and person" do
749
+ it 'no longer has any relation between preference and person' do
792
750
  expect(preference.people).to be_empty
793
751
  end
794
752
  end
795
753
 
796
- context "and person is saved" do
797
-
754
+ context 'and person is saved' do
798
755
  before do
799
- person.save
756
+ person.save!
800
757
  person.reload
801
758
  another_preference.reload
802
759
  end
803
760
 
804
- it "persists the relation between person and another_preference" do
761
+ it 'persists the relation between person and another_preference' do
805
762
  expect(person.preferences).to eq([ another_preference ])
806
763
  end
807
764
 
808
- it "persists the relation between another_prefrence and person" do
765
+ it 'persists the relation between another_preference and person' do
809
766
  expect(another_preference.people).to eq([ person ])
810
767
  end
811
768
 
812
- it "no longer has any relation between preference and person" do
769
+ it 'no longer has any relation between preference and person' do
813
770
  expect(preference.people).to be_empty
814
771
  end
815
772
  end
@@ -818,13 +775,13 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
818
775
  end
819
776
  end
820
777
 
821
- describe "when self-referencing" do
778
+ describe 'when self-referencing' do
822
779
  let!(:parent) do
823
- Vertex.create
780
+ Vertex.create!
824
781
  end
825
782
 
826
783
  let!(:child) do
827
- Vertex.create(parents: [parent])
784
+ Vertex.create!(parents: [ parent ])
828
785
  end
829
786
 
830
787
  before do
@@ -832,62 +789,62 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
832
789
  child.reload
833
790
  end
834
791
 
835
- it "sets the parent" do
792
+ it 'sets the parent' do
836
793
  expect(child.parents).to include(parent)
837
794
  end
838
795
 
839
- it "sets the parent id" do
796
+ it 'sets the parent id' do
840
797
  expect(child.parent_ids).to include(parent.id)
841
798
  end
842
799
 
843
- it "sets the child" do
800
+ it 'sets the child' do
844
801
  expect(parent.children).to include(child)
845
802
  end
846
803
 
847
- it "sets the child id" do
804
+ it 'sets the child id' do
848
805
  expect(parent.child_ids).to include(child.id)
849
806
  end
850
807
 
851
- describe "#update" do
808
+ describe '#update' do
852
809
  before do
853
- child.update(parents: [parent])
810
+ child.update(parents: [ parent ])
854
811
  end
855
812
 
856
- it "sets the parent" do
813
+ it 'sets the parent' do
857
814
  expect(child.parents).to include(parent)
858
815
  end
859
816
 
860
- it "sets the parent id" do
817
+ it 'sets the parent id' do
861
818
  expect(child.parent_ids).to include(parent.id)
862
819
  end
863
820
 
864
- it "sets the child" do
821
+ it 'sets the child' do
865
822
  expect(parent.children).to include(child)
866
823
  end
867
824
 
868
- it "sets the child id" do
825
+ it 'sets the child id' do
869
826
  expect(parent.child_ids).to include(child.id)
870
827
  end
871
828
 
872
- describe "when reloading" do
829
+ describe 'when reloading' do
873
830
  before do
874
831
  parent.reload
875
832
  child.reload
876
833
  end
877
834
 
878
- it "sets the parent" do
835
+ it 'sets the parent' do
879
836
  expect(child.parents).to include(parent)
880
837
  end
881
838
 
882
- it "sets the parent id" do
839
+ it 'sets the parent id' do
883
840
  expect(child.parent_ids).to include(parent.id)
884
841
  end
885
842
 
886
- it "sets the child" do
843
+ it 'sets the child' do
887
844
  expect(parent.children).to include(child)
888
845
  end
889
846
 
890
- it "sets the child id" do
847
+ it 'sets the child id' do
891
848
  expect(parent.child_ids).to include(child.id)
892
849
  end
893
850
  end
@@ -895,13 +852,9 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
895
852
  end
896
853
 
897
854
  [ nil, [] ].each do |value|
898
-
899
855
  describe "#= #{value}" do
900
-
901
- context "when the relation is not polymorphic" do
902
-
903
- context "when the inverse relation is not defined" do
904
-
856
+ context 'when the relation is not polymorphic' do
857
+ context 'when the inverse relation is not defined' do
905
858
  let(:person) do
906
859
  Person.new
907
860
  end
@@ -915,17 +868,16 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
915
868
  person.houses = value
916
869
  end
917
870
 
918
- it "clears the relation" do
871
+ it 'clears the relation' do
919
872
  expect(person.houses).to be_empty
920
873
  end
921
874
 
922
- it "clears the foreign keys" do
875
+ it 'clears the foreign keys' do
923
876
  expect(person.house_ids).to be_empty
924
877
  end
925
878
  end
926
879
 
927
- context "when the parent is a new record" do
928
-
880
+ context 'when the parent is a new record' do
929
881
  let(:person) do
930
882
  Person.new
931
883
  end
@@ -939,29 +891,27 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
939
891
  person.preferences = value
940
892
  end
941
893
 
942
- it "sets the relation to an empty array" do
894
+ it 'sets the relation to an empty array' do
943
895
  expect(person.preferences).to be_empty
944
896
  end
945
897
 
946
- it "removed the inverse relation" do
898
+ it 'removed the inverse relation' do
947
899
  expect(preference.people).to be_empty
948
900
  end
949
901
 
950
- it "removes the foreign key values" do
902
+ it 'removes the foreign key values' do
951
903
  expect(person.preference_ids).to be_empty
952
904
  end
953
905
 
954
- it "removes the inverse foreign key values" do
906
+ it 'removes the inverse foreign key values' do
955
907
  expect(preference.person_ids).to be_empty
956
908
  end
957
909
  end
958
910
 
959
- context "when the parent is not a new record" do
960
-
961
- context "when the relation has been loaded" do
962
-
911
+ context 'when the parent is not a new record' do
912
+ context 'when the relation has been loaded' do
963
913
  let(:person) do
964
- Person.create
914
+ Person.create!
965
915
  end
966
916
 
967
917
  let(:preference) do
@@ -973,35 +923,34 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
973
923
  person.preferences = value
974
924
  end
975
925
 
976
- it "sets the relation to an empty array" do
926
+ it 'sets the relation to an empty array' do
977
927
  expect(person.preferences).to be_empty
978
928
  end
979
929
 
980
- it "removed the inverse relation" do
930
+ it 'removed the inverse relation' do
981
931
  expect(preference.people).to be_empty
982
932
  end
983
933
 
984
- it "removes the foreign key values" do
934
+ it 'removes the foreign key values' do
985
935
  expect(person.preference_ids).to be_empty
986
936
  end
987
937
 
988
- it "removes the inverse foreign key values" do
938
+ it 'removes the inverse foreign key values' do
989
939
  expect(preference.person_ids).to be_empty
990
940
  end
991
941
 
992
- it "does not delete the target from the database" do
993
- expect(preference).to_not be_destroyed
942
+ it 'does not delete the target from the database' do
943
+ expect(preference).not_to be_destroyed
994
944
  end
995
945
  end
996
946
 
997
- context "when the relation has not been loaded" do
998
-
947
+ context 'when the relation has not been loaded' do
999
948
  let(:preference) do
1000
949
  Preference.new
1001
950
  end
1002
951
 
1003
952
  let(:person) do
1004
- Person.create.tap do |p|
953
+ Person.create!.tap do |p|
1005
954
  p.preferences = [ preference ]
1006
955
  end
1007
956
  end
@@ -1014,11 +963,11 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
1014
963
  from_db.preferences = value
1015
964
  end
1016
965
 
1017
- it "sets the relation to an empty array" do
966
+ it 'sets the relation to an empty array' do
1018
967
  expect(from_db.preferences).to be_empty
1019
968
  end
1020
969
 
1021
- it "removes the foreign key values" do
970
+ it 'removes the foreign key values' do
1022
971
  expect(from_db.preference_ids).to be_empty
1023
972
  end
1024
973
  end
@@ -1027,92 +976,86 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
1027
976
  end
1028
977
  end
1029
978
 
1030
- [ :build, :new ].each do |method|
1031
-
979
+ %i[build new].each do |method|
1032
980
  describe "##{method}" do
1033
-
1034
- context "when the relation is not polymorphic" do
1035
-
1036
- context "when the parent is a new record" do
1037
-
981
+ context 'when the relation is not polymorphic' do
982
+ context 'when the parent is a new record' do
1038
983
  let(:person) do
1039
984
  Person.new
1040
985
  end
1041
986
 
1042
987
  let!(:preference) do
1043
- person.preferences.send(method, name: "settings")
988
+ person.preferences.send(method, name: 'settings')
1044
989
  end
1045
990
 
1046
- it "adds the document to the relation" do
991
+ it 'adds the document to the relation' do
1047
992
  expect(person.preferences).to eq([ preference ])
1048
993
  end
1049
994
 
1050
- it "sets the foreign key on the relation" do
995
+ it 'sets the foreign key on the relation' do
1051
996
  expect(person.preference_ids).to eq([ preference.id ])
1052
997
  end
1053
998
 
1054
- it "sets the inverse foreign key on the relation" do
999
+ it 'sets the inverse foreign key on the relation' do
1055
1000
  expect(preference.person_ids).to eq([ person.id ])
1056
1001
  end
1057
1002
 
1058
- it "sets the attributes" do
1059
- expect(preference.name).to eq("settings")
1003
+ it 'sets the attributes' do
1004
+ expect(preference.name).to eq('settings')
1060
1005
  end
1061
1006
 
1062
- it "does not save the target" do
1007
+ it 'does not save the target' do
1063
1008
  expect(preference).to be_new_record
1064
1009
  end
1065
1010
 
1066
- it "adds the correct number of documents" do
1011
+ it 'adds the correct number of documents' do
1067
1012
  expect(person.preferences.size).to eq(1)
1068
1013
  end
1069
1014
  end
1070
1015
 
1071
- context "when the parent is not a new record" do
1072
-
1016
+ context 'when the parent is not a new record' do
1073
1017
  let(:person) do
1074
- Person.create
1018
+ Person.create!
1075
1019
  end
1076
1020
 
1077
1021
  let!(:preference) do
1078
- person.preferences.send(method, name: "settings")
1022
+ person.preferences.send(method, name: 'settings')
1079
1023
  end
1080
1024
 
1081
- it "adds the document to the relation" do
1025
+ it 'adds the document to the relation' do
1082
1026
  expect(person.preferences).to eq([ preference ])
1083
1027
  end
1084
1028
 
1085
- it "sets the foreign key on the relation" do
1029
+ it 'sets the foreign key on the relation' do
1086
1030
  expect(person.preference_ids).to eq([ preference.id ])
1087
1031
  end
1088
1032
 
1089
- it "sets the inverse foreign key on the relation" do
1033
+ it 'sets the inverse foreign key on the relation' do
1090
1034
  expect(preference.person_ids).to eq([ person.id ])
1091
1035
  end
1092
1036
 
1093
- it "sets the base on the inverse relation" do
1037
+ it 'sets the base on the inverse relation' do
1094
1038
  expect(preference.people).to eq([ person ])
1095
1039
  end
1096
1040
 
1097
- it "sets the attributes" do
1098
- expect(preference.name).to eq("settings")
1041
+ it 'sets the attributes' do
1042
+ expect(preference.name).to eq('settings')
1099
1043
  end
1100
1044
 
1101
- it "does not save the target" do
1045
+ it 'does not save the target' do
1102
1046
  expect(preference).to be_new_record
1103
1047
  end
1104
1048
 
1105
- it "adds the correct number of documents" do
1049
+ it 'adds the correct number of documents' do
1106
1050
  expect(person.preferences.size).to eq(1)
1107
1051
  end
1108
1052
 
1109
- context "when saving the target" do
1110
-
1053
+ context 'when saving the target' do
1111
1054
  before do
1112
- preference.save
1055
+ preference.save!
1113
1056
  end
1114
1057
 
1115
- it "persists the parent keys" do
1058
+ it 'persists the parent keys' do
1116
1059
  expect(person.reload.preference_ids).to eq([ preference.id ])
1117
1060
  end
1118
1061
  end
@@ -1121,93 +1064,86 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
1121
1064
  end
1122
1065
  end
1123
1066
 
1124
- describe "#clear" do
1125
-
1126
- context "when the relation is not polymorphic" do
1127
-
1128
- context "when the parent has been persisted" do
1129
-
1067
+ describe '#clear' do
1068
+ context 'when the relation is not polymorphic' do
1069
+ context 'when the parent has been persisted' do
1130
1070
  let!(:person) do
1131
- Person.create
1071
+ Person.create!
1132
1072
  end
1133
1073
 
1134
- context "when the children are persisted" do
1135
-
1074
+ context 'when the children are persisted' do
1136
1075
  let!(:preference) do
1137
- person.preferences.create(name: "settings")
1076
+ person.preferences.create!(name: 'settings')
1138
1077
  end
1139
1078
 
1140
1079
  let!(:relation) do
1141
1080
  person.preferences.clear
1142
1081
  end
1143
1082
 
1144
- it "clears out the relation" do
1083
+ it 'clears out the relation' do
1145
1084
  expect(person.preferences).to be_empty
1146
1085
  end
1147
1086
 
1148
- it "removes the parent from the inverse relation" do
1149
- expect(preference.people).to_not include(person)
1087
+ it 'removes the parent from the inverse relation' do
1088
+ expect(preference.people).not_to include(person)
1150
1089
  end
1151
1090
 
1152
- it "removes the foreign keys" do
1091
+ it 'removes the foreign keys' do
1153
1092
  expect(person.preference_ids).to be_empty
1154
1093
  end
1155
1094
 
1156
- it "removes the parent key from the inverse" do
1157
- expect(preference.person_ids).to_not include(person.id)
1095
+ it 'removes the parent key from the inverse' do
1096
+ expect(preference.person_ids).not_to include(person.id)
1158
1097
  end
1159
1098
 
1160
- it "does not delete the documents" do
1161
- expect(preference).to_not be_destroyed
1099
+ it 'does not delete the documents' do
1100
+ expect(preference).not_to be_destroyed
1162
1101
  end
1163
1102
 
1164
- it "persists the nullification" do
1103
+ it 'persists the nullification' do
1165
1104
  expect(person.reload.preferences).to be_empty
1166
1105
  end
1167
1106
 
1168
- it "returns the relation" do
1107
+ it 'returns the relation' do
1169
1108
  expect(relation).to be_empty
1170
1109
  end
1171
1110
  end
1172
1111
 
1173
- context "when the children are not persisted" do
1174
-
1112
+ context 'when the children are not persisted' do
1175
1113
  let!(:preference) do
1176
- person.preferences.build(name: "setting")
1114
+ person.preferences.build(name: 'setting')
1177
1115
  end
1178
1116
 
1179
1117
  let!(:relation) do
1180
1118
  person.preferences.clear
1181
1119
  end
1182
1120
 
1183
- it "clears out the relation" do
1121
+ it 'clears out the relation' do
1184
1122
  expect(person.preferences).to be_empty
1185
1123
  end
1186
1124
  end
1187
1125
  end
1188
1126
 
1189
- context "when the parent is not persisted" do
1190
-
1127
+ context 'when the parent is not persisted' do
1191
1128
  let(:person) do
1192
1129
  Person.new
1193
1130
  end
1194
1131
 
1195
1132
  let!(:preference) do
1196
- person.preferences.build(name: "setting")
1133
+ person.preferences.build(name: 'setting')
1197
1134
  end
1198
1135
 
1199
1136
  let!(:relation) do
1200
1137
  person.preferences.clear
1201
1138
  end
1202
1139
 
1203
- it "clears out the relation" do
1140
+ it 'clears out the relation' do
1204
1141
  expect(person.preferences).to be_empty
1205
1142
  end
1206
1143
  end
1207
1144
  end
1208
1145
 
1209
- context "when the association has callbacks" do
1210
-
1146
+ context 'when the association has callbacks' do
1211
1147
  let(:post) do
1212
1148
  Post.new
1213
1149
  end
@@ -1220,61 +1156,55 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
1220
1156
  post.tags << tag
1221
1157
  end
1222
1158
 
1223
- context "when the callback is a before_remove" do
1224
-
1225
- context "when no errors are raised" do
1226
-
1159
+ context 'when the callback is a before_remove' do
1160
+ context 'when no errors are raised' do
1227
1161
  before do
1228
1162
  post.tags.clear
1229
1163
  end
1230
1164
 
1231
- it "executes the callback" do
1165
+ it 'executes the callback' do
1232
1166
  expect(post.before_remove_called).to be true
1233
1167
  end
1234
1168
 
1235
- it "removes the document from the relation" do
1169
+ it 'removes the document from the relation' do
1236
1170
  expect(post.tags).to be_empty
1237
1171
  end
1238
1172
  end
1239
1173
 
1240
- context "when errors are raised" do
1241
-
1174
+ context 'when errors are raised' do
1242
1175
  before do
1243
1176
  expect(post).to receive(:before_remove_tag).and_raise
1244
- begin; post.tags.clear; rescue; end
1177
+ begin; post.tags.clear; rescue StandardError; end
1245
1178
  end
1246
1179
 
1247
- it "does not remove the document from the relation" do
1180
+ it 'does not remove the document from the relation' do
1248
1181
  expect(post.tags).to eq([ tag ])
1249
1182
  end
1250
1183
  end
1251
1184
  end
1252
1185
 
1253
- context "when the callback is an after_remove" do
1254
-
1255
- context "when no errors are raised" do
1256
-
1186
+ context 'when the callback is an after_remove' do
1187
+ context 'when no errors are raised' do
1257
1188
  before do
1258
1189
  post.tags.clear
1259
1190
  end
1260
1191
 
1261
- it "executes the callback" do
1192
+ it 'executes the callback' do
1262
1193
  expect(post.after_remove_called).to be true
1263
1194
  end
1264
1195
 
1265
- it "removes the document from the relation" do
1196
+ it 'removes the document from the relation' do
1266
1197
  expect(post.tags).to be_empty
1267
1198
  end
1268
1199
  end
1269
1200
 
1270
- context "when errors are raised" do
1271
-
1201
+ context 'when errors are raised' do
1272
1202
  before do
1273
1203
  expect(post).to receive(:after_remove_tag).and_raise
1274
- begin; post.tags.clear; rescue; end
1204
+ begin; post.tags.clear; rescue StandardError; end
1275
1205
  end
1276
1206
 
1277
- it "removes the document from the relation" do
1207
+ it 'removes the document from the relation' do
1278
1208
  expect(post.tags).to be_empty
1279
1209
  end
1280
1210
  end
@@ -1282,10 +1212,8 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
1282
1212
  end
1283
1213
  end
1284
1214
 
1285
- describe "#concat" do
1286
-
1287
- context "when the parent is a new record" do
1288
-
1215
+ describe '#concat' do
1216
+ context 'when the parent is a new record' do
1289
1217
  let(:person) do
1290
1218
  Person.new
1291
1219
  end
@@ -1298,15 +1226,14 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
1298
1226
  person.preferences.concat([ preference ])
1299
1227
  end
1300
1228
 
1301
- it "returns an array of loaded documents" do
1229
+ it 'returns an array of loaded documents' do
1302
1230
  expect(result).to eq([ preference ])
1303
1231
  end
1304
1232
  end
1305
1233
 
1306
- context "when the parent is not a new record" do
1307
-
1234
+ context 'when the parent is not a new record' do
1308
1235
  let(:person) do
1309
- Person.create
1236
+ Person.create!
1310
1237
  end
1311
1238
 
1312
1239
  let!(:preference) do
@@ -1317,15 +1244,13 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
1317
1244
  person.preferences.concat([ preference ])
1318
1245
  end
1319
1246
 
1320
- it "returns an array of loaded documents" do
1247
+ it 'returns an array of loaded documents' do
1321
1248
  expect(result).to eq([ preference ])
1322
1249
  end
1323
1250
  end
1324
1251
 
1325
- context "when the relations are not polymorphic" do
1326
-
1327
- context "when the inverse relation is not defined" do
1328
-
1252
+ context 'when the relations are not polymorphic' do
1253
+ context 'when the inverse relation is not defined' do
1329
1254
  let(:person) do
1330
1255
  Person.new
1331
1256
  end
@@ -1338,60 +1263,57 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
1338
1263
  person.houses.concat([ house ])
1339
1264
  end
1340
1265
 
1341
- it "appends the document to the relation" do
1266
+ it 'appends the document to the relation' do
1342
1267
  expect(person.houses).to eq([ house ])
1343
1268
  end
1344
1269
 
1345
- it "sets the foreign key on the relation" do
1270
+ it 'sets the foreign key on the relation' do
1346
1271
  expect(person.house_ids).to eq([ house.id ])
1347
1272
  end
1348
1273
  end
1349
1274
 
1350
- context "when appending in a parent create block" do
1351
-
1275
+ context 'when appending in a parent create block' do
1352
1276
  let!(:preference) do
1353
- Preference.create(name: "testing")
1277
+ Preference.create!(name: 'testing')
1354
1278
  end
1355
1279
 
1356
1280
  let!(:person) do
1357
- Person.create do |doc|
1281
+ Person.create! do |doc|
1358
1282
  doc.preferences.concat([ preference ])
1359
1283
  end
1360
1284
  end
1361
1285
 
1362
- it "adds the documents to the relation" do
1286
+ it 'adds the documents to the relation' do
1363
1287
  expect(person.preferences).to eq([ preference ])
1364
1288
  end
1365
1289
 
1366
- it "sets the foreign key on the relation" do
1290
+ it 'sets the foreign key on the relation' do
1367
1291
  expect(person.preference_ids).to eq([ preference.id ])
1368
1292
  end
1369
1293
 
1370
- it "sets the foreign key on the inverse relation" do
1294
+ it 'sets the foreign key on the inverse relation' do
1371
1295
  expect(preference.person_ids).to eq([ person.id ])
1372
1296
  end
1373
1297
 
1374
- it "saves the target" do
1298
+ it 'saves the target' do
1375
1299
  expect(preference).to be_persisted
1376
1300
  end
1377
1301
 
1378
- it "adds the correct number of documents" do
1302
+ it 'adds the correct number of documents' do
1379
1303
  expect(person.preferences.size).to eq(1)
1380
1304
  end
1381
1305
 
1382
- it "persists the link" do
1306
+ it 'persists the link' do
1383
1307
  expect(person.reload.preferences).to eq([ preference ])
1384
1308
  end
1385
1309
  end
1386
1310
 
1387
- context "when the parent is a new record" do
1388
-
1311
+ context 'when the parent is a new record' do
1389
1312
  let(:person) do
1390
1313
  Person.new
1391
1314
  end
1392
1315
 
1393
- context "when the child is new" do
1394
-
1316
+ context 'when the child is new' do
1395
1317
  let(:preference) do
1396
1318
  Preference.new
1397
1319
  end
@@ -1400,46 +1322,44 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
1400
1322
  person.preferences.concat([ preference ])
1401
1323
  end
1402
1324
 
1403
- it "adds the documents to the relation" do
1325
+ it 'adds the documents to the relation' do
1404
1326
  expect(person.preferences).to eq([ preference ])
1405
1327
  end
1406
1328
 
1407
- it "sets the foreign key on the relation" do
1329
+ it 'sets the foreign key on the relation' do
1408
1330
  expect(person.preference_ids).to eq([ preference.id ])
1409
1331
  end
1410
1332
 
1411
- it "sets the foreign key on the inverse relation" do
1333
+ it 'sets the foreign key on the inverse relation' do
1412
1334
  expect(preference.person_ids).to eq([ person.id ])
1413
1335
  end
1414
1336
 
1415
- it "does not save the target" do
1337
+ it 'does not save the target' do
1416
1338
  expect(preference).to be_new_record
1417
1339
  end
1418
1340
 
1419
- it "adds the correct number of documents" do
1341
+ it 'adds the correct number of documents' do
1420
1342
  expect(person.preferences.size).to eq(1)
1421
1343
  end
1422
1344
 
1423
- context "when appending a second time" do
1424
-
1345
+ context 'when appending a second time' do
1425
1346
  before do
1426
1347
  person.preferences.concat([ preference ])
1427
1348
  end
1428
1349
 
1429
- it "does not allow the document to be added again" do
1350
+ it 'does not allow the document to be added again' do
1430
1351
  expect(person.preferences).to eq([ preference ])
1431
1352
  end
1432
1353
 
1433
- it "does not allow duplicate ids" do
1354
+ it 'does not allow duplicate ids' do
1434
1355
  expect(person.preference_ids).to eq([ preference.id ])
1435
1356
  end
1436
1357
  end
1437
1358
  end
1438
1359
 
1439
- context "when the child is already persisted" do
1440
-
1360
+ context 'when the child is already persisted' do
1441
1361
  let!(:persisted) do
1442
- Preference.create(name: "testy")
1362
+ Preference.create!(name: 'testy')
1443
1363
  end
1444
1364
 
1445
1365
  let(:preference) do
@@ -1448,38 +1368,37 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
1448
1368
 
1449
1369
  before do
1450
1370
  person.preferences.concat([ preference ])
1451
- person.save
1371
+ person.save!
1452
1372
  end
1453
1373
 
1454
- it "adds the documents to the relation" do
1374
+ it 'adds the documents to the relation' do
1455
1375
  expect(person.preferences).to eq([ preference ])
1456
1376
  end
1457
1377
 
1458
- it "sets the foreign key on the relation" do
1378
+ it 'sets the foreign key on the relation' do
1459
1379
  expect(person.preference_ids).to eq([ preference.id ])
1460
1380
  end
1461
1381
 
1462
- it "sets the foreign key on the inverse relation" do
1382
+ it 'sets the foreign key on the inverse relation' do
1463
1383
  expect(preference.person_ids).to eq([ person.id ])
1464
1384
  end
1465
1385
 
1466
- it "saves the target" do
1386
+ it 'saves the target' do
1467
1387
  expect(preference).to be_persisted
1468
1388
  end
1469
1389
 
1470
- it "adds the correct number of documents" do
1390
+ it 'adds the correct number of documents' do
1471
1391
  expect(person.preferences.size).to eq(1)
1472
1392
  end
1473
1393
 
1474
- it "persists the link" do
1394
+ it 'persists the link' do
1475
1395
  expect(person.reload.preferences).to eq([ preference ])
1476
1396
  end
1477
1397
  end
1478
1398
 
1479
- context "when setting via the associated ids" do
1480
-
1399
+ context 'when setting via the associated ids' do
1481
1400
  let!(:persisted) do
1482
- Preference.create(name: "testy")
1401
+ Preference.create!(name: 'testy')
1483
1402
  end
1484
1403
 
1485
1404
  let(:preference) do
@@ -1491,35 +1410,34 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
1491
1410
  end
1492
1411
 
1493
1412
  before do
1494
- person.save
1413
+ person.save!
1495
1414
  end
1496
1415
 
1497
- it "adds the documents to the relation" do
1416
+ it 'adds the documents to the relation' do
1498
1417
  expect(person.preferences).to eq([ preference ])
1499
1418
  end
1500
1419
 
1501
- it "sets the foreign key on the relation" do
1420
+ it 'sets the foreign key on the relation' do
1502
1421
  expect(person.preference_ids).to eq([ preference.id ])
1503
1422
  end
1504
1423
 
1505
- it "sets the foreign key on the inverse relation" do
1424
+ it 'sets the foreign key on the inverse relation' do
1506
1425
  expect(preference.reload.person_ids).to eq([ person.id ])
1507
1426
  end
1508
1427
 
1509
- it "adds the correct number of documents" do
1428
+ it 'adds the correct number of documents' do
1510
1429
  expect(person.preferences.size).to eq(1)
1511
1430
  end
1512
1431
 
1513
- it "persists the link" do
1432
+ it 'persists the link' do
1514
1433
  expect(person.reload.preferences).to eq([ preference ])
1515
1434
  end
1516
1435
  end
1517
1436
  end
1518
1437
 
1519
- context "when the parent is not a new record" do
1520
-
1438
+ context 'when the parent is not a new record' do
1521
1439
  let(:person) do
1522
- Person.create
1440
+ Person.create!
1523
1441
  end
1524
1442
 
1525
1443
  let(:preference) do
@@ -1530,36 +1448,35 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
1530
1448
  person.preferences.concat([ preference ])
1531
1449
  end
1532
1450
 
1533
- it "adds the documents to the relation" do
1451
+ it 'adds the documents to the relation' do
1534
1452
  expect(person.preferences).to eq([ preference ])
1535
1453
  end
1536
1454
 
1537
- it "sets the foreign key on the relation" do
1455
+ it 'sets the foreign key on the relation' do
1538
1456
  expect(person.preference_ids).to eq([ preference.id ])
1539
1457
  end
1540
1458
 
1541
- it "sets the foreign key on the inverse relation" do
1459
+ it 'sets the foreign key on the inverse relation' do
1542
1460
  expect(preference.person_ids).to eq([ person.id ])
1543
1461
  end
1544
1462
 
1545
- it "sets the base on the inverse relation" do
1463
+ it 'sets the base on the inverse relation' do
1546
1464
  expect(preference.people).to eq([ person ])
1547
1465
  end
1548
1466
 
1549
- it "sets the same instance on the inverse relation" do
1467
+ it 'sets the same instance on the inverse relation' do
1550
1468
  expect(preference.people.first).to eql(person)
1551
1469
  end
1552
1470
 
1553
- it "saves the target" do
1554
- expect(preference).to_not be_new_record
1471
+ it 'saves the target' do
1472
+ expect(preference).not_to be_new_record
1555
1473
  end
1556
1474
 
1557
- it "adds the document to the target" do
1475
+ it 'adds the document to the target' do
1558
1476
  expect(person.preferences.count).to eq(1)
1559
1477
  end
1560
1478
 
1561
- context "when documents already exist on the relation" do
1562
-
1479
+ context 'when documents already exist on the relation' do
1563
1480
  let(:preference_two) do
1564
1481
  Preference.new
1565
1482
  end
@@ -1568,71 +1485,68 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
1568
1485
  person.preferences.concat([ preference_two ])
1569
1486
  end
1570
1487
 
1571
- it "adds the documents to the relation" do
1488
+ it 'adds the documents to the relation' do
1572
1489
  expect(person.preferences).to eq([ preference, preference_two ])
1573
1490
  end
1574
1491
 
1575
- it "sets the foreign key on the relation" do
1492
+ it 'sets the foreign key on the relation' do
1576
1493
  expect(person.preference_ids).to eq([ preference.id, preference_two.id ])
1577
1494
  end
1578
1495
 
1579
- it "sets the foreign key on the inverse relation" do
1496
+ it 'sets the foreign key on the inverse relation' do
1580
1497
  expect(preference_two.person_ids).to eq([ person.id ])
1581
1498
  end
1582
1499
 
1583
- it "sets the base on the inverse relation" do
1500
+ it 'sets the base on the inverse relation' do
1584
1501
  expect(preference_two.people).to eq([ person ])
1585
1502
  end
1586
1503
 
1587
- it "sets the same instance on the inverse relation" do
1504
+ it 'sets the same instance on the inverse relation' do
1588
1505
  expect(preference_two.people.first).to eql(person)
1589
1506
  end
1590
1507
 
1591
- it "saves the target" do
1592
- expect(preference).to_not be_new_record
1508
+ it 'saves the target' do
1509
+ expect(preference).not_to be_new_record
1593
1510
  end
1594
1511
 
1595
- it "adds the document to the target" do
1512
+ it 'adds the document to the target' do
1596
1513
  expect(person.preferences.count).to eq(2)
1597
1514
  end
1598
1515
  end
1599
1516
  end
1600
1517
 
1601
- context "when both sides have been persisted" do
1602
-
1518
+ context 'when both sides have been persisted' do
1603
1519
  let(:person) do
1604
- Person.create
1520
+ Person.create!
1605
1521
  end
1606
1522
 
1607
1523
  let(:event) do
1608
- Event.create
1524
+ Event.create!
1609
1525
  end
1610
1526
 
1611
1527
  before do
1612
1528
  person.administrated_events.concat([ event ])
1613
1529
  end
1614
1530
 
1615
- it "sets the front side of the relation" do
1531
+ it 'sets the front side of the relation' do
1616
1532
  expect(person.administrated_events).to eq([ event ])
1617
1533
  end
1618
1534
 
1619
- it "sets the inverse side of the relation" do
1535
+ it 'sets the inverse side of the relation' do
1620
1536
  expect(event.administrators(true)).to eq([ person ])
1621
1537
  end
1622
1538
 
1623
- context "when reloading" do
1624
-
1625
- it "sets the front side of the relation" do
1539
+ context 'when reloading' do
1540
+ it 'sets the front side of the relation' do
1626
1541
  expect(person.reload.administrated_events).to eq([ event ])
1627
1542
  end
1628
1543
 
1629
- it "sets the inverse side of the relation" do
1544
+ it 'sets the inverse side of the relation' do
1630
1545
  expect(event.reload.administrators).to eq([ person ])
1631
1546
  end
1632
1547
  end
1633
1548
 
1634
- context "when performing a new database query" do
1635
-
1549
+ context 'when performing a new database query' do
1636
1550
  let(:loaded_person) do
1637
1551
  Person.find(person.id)
1638
1552
  end
@@ -1641,74 +1555,70 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
1641
1555
  Event.find(event.id)
1642
1556
  end
1643
1557
 
1644
- it "sets the front side of the relation" do
1558
+ it 'sets the front side of the relation' do
1645
1559
  expect(loaded_person.administrated_events).to eq([ event ])
1646
1560
  end
1647
1561
 
1648
- it "sets the inverse side of the relation" do
1562
+ it 'sets the inverse side of the relation' do
1649
1563
  expect(loaded_event.administrators).to eq([ person ])
1650
1564
  end
1651
1565
  end
1652
1566
  end
1653
1567
 
1654
- context "when the relation also includes a has_many relation" do
1655
-
1568
+ context 'when the relation also includes a has_many relation' do
1656
1569
  let(:artwork) do
1657
- Artwork.create
1570
+ Artwork.create!
1658
1571
  end
1659
1572
 
1660
1573
  let(:exhibition) do
1661
- Exhibition.create
1574
+ Exhibition.create!
1662
1575
  end
1663
1576
 
1664
1577
  let(:exhibitor) do
1665
- Exhibitor.create(exhibition: exhibition)
1578
+ Exhibitor.create!(exhibition: exhibition)
1666
1579
  end
1667
1580
 
1668
1581
  before do
1669
1582
  artwork.exhibitors.concat([ exhibitor ])
1670
1583
  end
1671
1584
 
1672
- it "creates a single artwork object" do
1585
+ it 'creates a single artwork object' do
1673
1586
  expect(Artwork.count).to eq(1)
1674
1587
  end
1675
1588
  end
1676
1589
 
1677
- context "when the relation is self referencing" do
1678
-
1590
+ context 'when the relation is self referencing' do
1679
1591
  let(:tag_one) do
1680
- Tag.create(text: "one")
1592
+ Tag.create!(text: 'one')
1681
1593
  end
1682
1594
 
1683
1595
  let(:tag_two) do
1684
- Tag.create(text: "two")
1596
+ Tag.create!(text: 'two')
1685
1597
  end
1686
1598
 
1687
1599
  before do
1688
1600
  tag_one.related.concat([ tag_two ])
1689
1601
  end
1690
1602
 
1691
- it "sets the front side of the relation" do
1603
+ it 'sets the front side of the relation' do
1692
1604
  expect(tag_one.related).to eq([ tag_two ])
1693
1605
  end
1694
1606
 
1695
- it "sets the inverse side of the relation" do
1607
+ it 'sets the inverse side of the relation' do
1696
1608
  expect(tag_two.related(true)).to eq([ tag_one ])
1697
1609
  end
1698
1610
 
1699
- context "when reloading" do
1700
-
1701
- it "sets the front side of the relation" do
1611
+ context 'when reloading' do
1612
+ it 'sets the front side of the relation' do
1702
1613
  expect(tag_one.reload.related).to eq([ tag_two ])
1703
1614
  end
1704
1615
 
1705
- it "sets the inverse side of the relation" do
1616
+ it 'sets the inverse side of the relation' do
1706
1617
  expect(tag_two.reload.related).to eq([ tag_one ])
1707
1618
  end
1708
1619
  end
1709
1620
 
1710
- context "when performing a new database query" do
1711
-
1621
+ context 'when performing a new database query' do
1712
1622
  let(:loaded_tag_one) do
1713
1623
  Tag.find(tag_one.id)
1714
1624
  end
@@ -1717,11 +1627,11 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
1717
1627
  Tag.find(tag_two.id)
1718
1628
  end
1719
1629
 
1720
- it "sets the front side of the relation" do
1630
+ it 'sets the front side of the relation' do
1721
1631
  expect(loaded_tag_one.related).to eq([ tag_two ])
1722
1632
  end
1723
1633
 
1724
- it "sets the inverse side of the relation" do
1634
+ it 'sets the inverse side of the relation' do
1725
1635
  expect(loaded_tag_two.related).to eq([ tag_one ])
1726
1636
  end
1727
1637
  end
@@ -1729,16 +1639,13 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
1729
1639
  end
1730
1640
  end
1731
1641
 
1732
- describe "#count" do
1733
-
1642
+ describe '#count' do
1734
1643
  let(:person) do
1735
- Person.create
1644
+ Person.create!
1736
1645
  end
1737
1646
 
1738
- context "when nothing exists on the relation" do
1739
-
1740
- context "when the document is destroyed" do
1741
-
1647
+ context 'when nothing exists on the relation' do
1648
+ context 'when the document is destroyed' do
1742
1649
  before do
1743
1650
  Meat.create!
1744
1651
  end
@@ -1747,160 +1654,110 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
1747
1654
  Sandwich.create!
1748
1655
  end
1749
1656
 
1750
- it "returns zero" do
1657
+ it 'returns zero' do
1751
1658
  sandwich.destroy
1752
1659
  expect(sandwich.meats.count).to eq(0)
1753
1660
  end
1754
1661
  end
1755
1662
  end
1756
1663
 
1757
- describe "#any?" do
1758
-
1759
- let(:person) do
1760
- Person.create
1761
- end
1762
-
1763
- context "when nothing exists on the relation" do
1764
-
1765
- context "when no document is added" do
1766
-
1767
- let!(:sandwich) do
1768
- Sandwich.create!
1769
- end
1770
-
1771
- it "returns false" do
1772
- expect(sandwich.meats.any?).to be false
1773
- end
1774
- end
1775
-
1776
- context "when the document is destroyed" do
1777
-
1778
- before do
1779
- Meat.create!
1780
- end
1781
-
1782
- let!(:sandwich) do
1783
- Sandwich.create!
1784
- end
1785
-
1786
- it "returns false" do
1787
- sandwich.destroy
1788
- expect(sandwich.meats.any?).to be false
1789
- end
1790
- end
1791
- end
1792
- end
1793
-
1794
- context "when documents have been persisted" do
1795
-
1664
+ context 'when documents have been persisted' do
1796
1665
  let!(:preference) do
1797
- person.preferences.create(name: "setting")
1666
+ person.preferences.create!(name: 'setting')
1798
1667
  end
1799
1668
 
1800
- it "returns the number of persisted documents" do
1669
+ it 'returns the number of persisted documents' do
1801
1670
  expect(person.preferences.count).to eq(1)
1802
1671
  end
1803
1672
  end
1804
1673
 
1805
- context "when appending to a loaded relation" do
1806
-
1674
+ context 'when appending to a loaded relation' do
1807
1675
  let!(:preference) do
1808
- person.preferences.create(name: "setting")
1676
+ person.preferences.create!(name: 'setting')
1809
1677
  end
1810
1678
 
1811
1679
  before do
1812
1680
  person.preferences.count
1813
- person.preferences << Preference.create(name: "two")
1681
+ person.preferences << Preference.create!(name: 'two')
1814
1682
  end
1815
1683
 
1816
- it "returns the number of persisted documents" do
1684
+ it 'returns the number of persisted documents' do
1817
1685
  expect(person.preferences.count).to eq(2)
1818
1686
  end
1819
1687
  end
1820
1688
 
1821
- context "when documents have not been persisted" do
1822
-
1689
+ context 'when documents have not been persisted' do
1823
1690
  let!(:preference) do
1824
- person.preferences.build(name: "settings")
1691
+ person.preferences.build(name: 'settings')
1825
1692
  end
1826
1693
 
1827
- it "returns 0" do
1694
+ it 'returns 0' do
1828
1695
  expect(person.preferences.count).to eq(0)
1829
1696
  end
1830
1697
  end
1831
1698
 
1832
- context "when new documents exist in the database" do
1833
-
1834
- context "when the documents are part of the relation" do
1835
-
1699
+ context 'when new documents exist in the database' do
1700
+ context 'when the documents are part of the relation' do
1836
1701
  before do
1837
- Preference.create(person_ids: [ person.id ])
1702
+ Preference.create!(person_ids: [ person.id ])
1838
1703
  end
1839
1704
 
1840
- it "returns the count from the db" do
1705
+ it 'returns the count from the db' do
1841
1706
  expect(person.reload.preferences.count).to eq(1)
1842
1707
  end
1843
1708
  end
1844
1709
 
1845
- context "when the documents are not part of the relation" do
1846
-
1710
+ context 'when the documents are not part of the relation' do
1847
1711
  before do
1848
- Preference.create
1712
+ Preference.create!
1849
1713
  end
1850
1714
 
1851
- it "returns the count from the db" do
1715
+ it 'returns the count from the db' do
1852
1716
  expect(person.preferences.count).to eq(0)
1853
1717
  end
1854
1718
  end
1855
1719
  end
1856
1720
 
1857
- context "when the inverse relation is not defined" do
1858
-
1859
- context "when documents have been persisted" do
1860
-
1721
+ context 'when the inverse relation is not defined' do
1722
+ context 'when documents have been persisted' do
1861
1723
  let!(:house) do
1862
- person.houses.create(name: "Wayne Manor")
1724
+ person.houses.create!(name: 'Wayne Manor')
1863
1725
  end
1864
1726
 
1865
- it "returns the number of persisted documents" do
1727
+ it 'returns the number of persisted documents' do
1866
1728
  expect(person.houses.count).to eq(1)
1867
1729
  end
1868
1730
  end
1869
1731
 
1870
- context "when documents have not been persisted" do
1871
-
1732
+ context 'when documents have not been persisted' do
1872
1733
  let!(:house) do
1873
- person.houses.build(name: "Ryugyong Hotel")
1734
+ person.houses.build(name: 'Ryugyong Hotel')
1874
1735
  end
1875
1736
 
1876
- it "returns 0" do
1737
+ it 'returns 0' do
1877
1738
  expect(person.preferences.count).to eq(0)
1878
1739
  end
1879
1740
  end
1880
1741
  end
1881
1742
  end
1882
1743
 
1883
- describe "#any?" do
1884
-
1744
+ describe '#any?' do
1885
1745
  let(:sandwich) do
1886
- Sandwich.create
1746
+ Sandwich.create!
1887
1747
  end
1888
1748
 
1889
- context "when nothing exists on the relation" do
1890
-
1891
- context "when no document is added" do
1892
-
1749
+ context 'when nothing exists on the relation' do
1750
+ context 'when no document is added' do
1893
1751
  let!(:sandwich) do
1894
1752
  Sandwich.create!
1895
1753
  end
1896
1754
 
1897
- it "returns false" do
1755
+ it 'returns false' do
1898
1756
  expect(sandwich.meats.any?).to be false
1899
1757
  end
1900
1758
  end
1901
1759
 
1902
- context "when the document is destroyed" do
1903
-
1760
+ context 'when the document is destroyed' do
1904
1761
  before do
1905
1762
  Meat.create!
1906
1763
  end
@@ -1909,15 +1766,14 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
1909
1766
  Sandwich.create!
1910
1767
  end
1911
1768
 
1912
- it "returns false" do
1769
+ it 'returns false' do
1913
1770
  sandwich.destroy
1914
1771
  expect(sandwich.meats.any?).to be false
1915
1772
  end
1916
1773
  end
1917
1774
  end
1918
1775
 
1919
- context "when appending to a relation and _loaded/_unloaded are empty" do
1920
-
1776
+ context 'when appending to a relation and _loaded/_unloaded are empty' do
1921
1777
  let!(:sandwich) do
1922
1778
  Sandwich.create!
1923
1779
  end
@@ -1926,138 +1782,144 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
1926
1782
  sandwich.meats << Meat.new
1927
1783
  end
1928
1784
 
1929
- it "returns true" do
1785
+ it 'returns true' do
1930
1786
  expect(sandwich.meats.any?).to be true
1931
1787
  end
1932
1788
  end
1933
1789
 
1934
- context "when appending to a relation in a transaction" do
1790
+ context 'when appending to a relation in a transaction' do
1935
1791
  require_transaction_support
1936
1792
 
1793
+ # for some reason this test fails intermittently on any sharded
1794
+ # topology (sharded or load-balanced). The error is:
1795
+ #
1796
+ # [13388:StaleConfig]: Transaction <id>...eats is not currently
1797
+ # known and needs to be recovered
1798
+ #
1799
+ # It will fail on one run, succeed on the next, fail on the next,
1800
+ # etc. Tested on both db versions 7 and 8, with both sharded and
1801
+ # load-balanced topologies. For now, we'll just test this on a
1802
+ # replica set.
1803
+ require_topology :replica_set
1804
+
1805
+ # this also fails on server version 4.0, even with replica set,
1806
+ # so we'll just skip it
1807
+ min_server_version '5.0'
1808
+
1937
1809
  let!(:sandwich) do
1938
1810
  Sandwich.create!
1939
1811
  end
1940
1812
 
1941
- it "returns true" do
1942
- sandwich.with_session do |session|
1943
- session.with_transaction do
1944
- expect{ sandwich.meats << Meat.new }.to_not raise_error
1945
- expect(sandwich.meats.any?).to be true
1946
- end
1813
+ it 'returns true' do
1814
+ sandwich.transaction do
1815
+ expect { sandwich.meats << Meat.new }.not_to raise_error
1816
+ expect(sandwich.meats.any?).to be true
1947
1817
  end
1948
1818
  end
1949
1819
  end
1950
1820
 
1951
- context "when documents have been persisted" do
1952
-
1821
+ context 'when documents have been persisted' do
1953
1822
  let!(:meat) do
1954
- sandwich.meats.create
1823
+ sandwich.meats.create!
1955
1824
  end
1956
1825
 
1957
- it "returns true" do
1826
+ it 'returns true' do
1958
1827
  expect(sandwich.meats.any?).to be true
1959
1828
  end
1960
1829
  end
1961
1830
 
1962
- context "when documents have not been persisted" do
1963
-
1831
+ context 'when documents have not been persisted' do
1964
1832
  let!(:meat) do
1965
1833
  sandwich.meats.build
1966
1834
  end
1967
1835
 
1968
- it "returns false" do
1836
+ it 'returns false' do
1969
1837
  expect(sandwich.meats.any?).to be true
1970
1838
  end
1971
1839
  end
1972
1840
 
1973
- context "when new documents exist in the database" do
1841
+ context 'when new documents exist in the database' do
1974
1842
  before do
1975
- Meat.create(sandwiches: [sandwich])
1843
+ Meat.create!(sandwiches: [ sandwich ])
1976
1844
  end
1977
1845
 
1978
- it "returns true" do
1846
+ it 'returns true' do
1979
1847
  expect(sandwich.meats.any?).to be true
1980
1848
  end
1981
1849
  end
1982
1850
  end
1983
1851
 
1984
- [ :create, :create! ].each do |method|
1985
-
1852
+ %i[create create!].each do |method|
1986
1853
  describe "##{method}" do
1987
-
1988
- context "when the relation is not polymorphic" do
1989
-
1990
- context "when using string keys" do
1991
-
1854
+ context 'when the relation is not polymorphic' do
1855
+ context 'when using string keys' do
1992
1856
  let(:agent) do
1993
- Agent.create(number: "007")
1857
+ Agent.create!(number: '007')
1994
1858
  end
1995
1859
 
1996
1860
  before do
1997
- agent.accounts.create(name: "testing again")
1861
+ agent.accounts.send(method, name: 'test again')
1998
1862
  end
1999
1863
 
2000
- it "does not convert the string key to an object id" do
2001
- expect(agent.account_ids).to eq([ "testing-again" ])
1864
+ it 'does not convert the string key to an object id' do
1865
+ expect(agent.account_ids).to eq([ 'test-again' ])
2002
1866
  end
2003
1867
  end
2004
1868
 
2005
- context "when the parent is a new record" do
2006
-
1869
+ context 'when the parent is a new record' do
2007
1870
  let(:person) do
2008
1871
  Person.new
2009
1872
  end
2010
1873
 
2011
- it "raises an unsaved document error" do
2012
- expect {
2013
- person.preferences.send(method, name: "Testing")
2014
- }.to raise_error(Mongoid::Errors::UnsavedDocument)
1874
+ it 'raises an unsaved document error' do
1875
+ expect do
1876
+ person.preferences.send(method, name: 'Testing')
1877
+ end.to raise_error(Mongoid::Errors::UnsavedDocument)
2015
1878
  end
2016
1879
  end
2017
1880
 
2018
- context "when the parent is not a new record" do
2019
-
1881
+ context 'when the parent is not a new record' do
2020
1882
  let(:person) do
2021
1883
  Person.send(method)
2022
1884
  end
2023
1885
 
2024
1886
  let!(:preference) do
2025
- person.preferences.send(method, name: "Testing")
1887
+ person.preferences.send(method, name: 'Testing')
2026
1888
  end
2027
1889
 
2028
- it "sets the foreign key on the relation" do
1890
+ it 'sets the foreign key on the relation' do
2029
1891
  expect(person.preference_ids).to eq([ preference.id ])
2030
1892
  end
2031
1893
 
2032
- it "sets the foreign key on the inverse relation" do
1894
+ it 'sets the foreign key on the inverse relation' do
2033
1895
  expect(preference.person_ids).to eq([ person.id ])
2034
1896
  end
2035
1897
 
2036
- it "adds the document" do
1898
+ it 'adds the document' do
2037
1899
  expect(person.preferences).to eq([ preference ])
2038
1900
  end
2039
1901
 
2040
- it "sets the base on the inverse relation" do
1902
+ it 'sets the base on the inverse relation' do
2041
1903
  expect(preference.people).to eq([ person ])
2042
1904
  end
2043
1905
 
2044
- it "sets the attributes" do
2045
- expect(preference.name).to eq("Testing")
1906
+ it 'sets the attributes' do
1907
+ expect(preference.name).to eq('Testing')
2046
1908
  end
2047
1909
 
2048
- it "saves the target" do
1910
+ it 'saves the target' do
2049
1911
  expect(preference).to be_persisted
2050
1912
  end
2051
1913
 
2052
- it "adds the document to the target" do
1914
+ it 'adds the document to the target' do
2053
1915
  expect(person.preferences.count).to eq(1)
2054
1916
  end
2055
1917
 
2056
- it "does not duplicate documents" do
1918
+ it 'does not duplicate documents' do
2057
1919
  expect(person.reload.preferences.count).to eq(1)
2058
1920
  end
2059
1921
 
2060
- it "does not duplicate ids" do
1922
+ it 'does not duplicate ids' do
2061
1923
  expect(person.reload.preference_ids.count).to eq(1)
2062
1924
  end
2063
1925
  end
@@ -2065,341 +1927,294 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2065
1927
  end
2066
1928
  end
2067
1929
 
2068
- describe "#create!" do
2069
-
2070
- context "when validation fails" do
2071
-
1930
+ describe '#create!' do
1931
+ context 'when validation fails' do
2072
1932
  let(:person) do
2073
- Person.create
1933
+ Person.create!
2074
1934
  end
2075
1935
 
2076
- context "when the relation is not polymorphic" do
2077
-
2078
- it "raises an error" do
2079
- expect {
2080
- person.preferences.create!(name: "a")
2081
- }.to raise_error(Mongoid::Errors::Validations)
1936
+ context 'when the relation is not polymorphic' do
1937
+ it 'raises an error' do
1938
+ expect do
1939
+ person.preferences.create!(name: 'a')
1940
+ end.to raise_error(Mongoid::Errors::Validations)
2082
1941
  end
2083
1942
  end
2084
1943
  end
2085
1944
  end
2086
1945
 
2087
- describe "#delete" do
2088
-
2089
- let(:person) do
2090
- Person.create
2091
- end
2092
-
2093
- let(:preference_one) do
2094
- Preference.create(name: "Testing")
2095
- end
2096
-
2097
- let(:preference_two) do
2098
- Preference.create(name: "Test")
2099
- end
2100
-
2101
- before do
2102
- person.preferences << [ preference_one, preference_two ]
2103
- end
2104
-
2105
- context "when the document exists" do
2106
-
2107
- let!(:deleted) do
2108
- person.preferences.delete(preference_one)
2109
- end
2110
-
2111
- it "removes the document from the relation" do
2112
- expect(person.preferences).to eq([ preference_two ])
2113
- end
2114
-
2115
- it "returns the document" do
2116
- expect(deleted).to eq(preference_one)
2117
- end
2118
-
2119
- it "removes the document key from the foreign key" do
2120
- expect(person.preference_ids).to eq([ preference_two.id ])
2121
- end
2122
-
2123
- it "removes the inverse reference" do
2124
- expect(deleted.reload.people).to be_empty
2125
- end
1946
+ %i[ delete delete_one ].each do |method|
1947
+ describe "##{method}" do
1948
+ let(:person) { Person.create! }
1949
+ let(:preference_one) { Preference.create!(name: 'Testing') }
1950
+ let(:preference_two) { Preference.create!(name: 'Test') }
2126
1951
 
2127
- it "removes the base id from the inverse keys" do
2128
- expect(deleted.reload.person_ids).to be_empty
1952
+ before do
1953
+ person.preferences << [ preference_one, preference_two ]
2129
1954
  end
2130
1955
 
2131
- context "and person and preferences are reloaded" do
2132
-
2133
- before do
2134
- person.reload
2135
- preference_one.reload
2136
- preference_two.reload
1956
+ context 'when the document exists' do
1957
+ let!(:deleted) do
1958
+ person.preferences.send(method, preference_one)
2137
1959
  end
2138
1960
 
2139
- it "nullifies the deleted preference" do
1961
+ it 'removes the document from the relation' do
2140
1962
  expect(person.preferences).to eq([ preference_two ])
2141
1963
  end
2142
1964
 
2143
- it "retains the ids for one preference" do
2144
- expect(person.preference_ids).to eq([ preference_two.id ])
1965
+ it 'returns the document' do
1966
+ expect(deleted).to eq(preference_one)
2145
1967
  end
2146
- end
2147
- end
2148
-
2149
- context "when the document does not exist" do
2150
-
2151
- let!(:deleted) do
2152
- person.preferences.delete(Preference.new)
2153
- end
2154
-
2155
- it "returns nil" do
2156
- expect(deleted).to be_nil
2157
- end
2158
-
2159
- it "does not modify the relation" do
2160
- expect(person.preferences).to eq([ preference_one, preference_two ])
2161
- end
2162
-
2163
- it "does not modify the keys" do
2164
- expect(person.preference_ids).to eq([ preference_one.id, preference_two.id ])
2165
- end
2166
- end
2167
-
2168
- context "when :dependent => :nullify is set" do
2169
-
2170
- context "when :inverse_of is set" do
2171
1968
 
2172
- let(:event) do
2173
- Event.create
1969
+ it 'removes the document key from the foreign key' do
1970
+ expect(person.preference_ids).to eq([ preference_two.id ])
2174
1971
  end
2175
1972
 
2176
- before do
2177
- person.administrated_events << [ event ]
1973
+ it 'removes the inverse reference' do
1974
+ expect(deleted.reload.people).to be_empty
2178
1975
  end
2179
1976
 
2180
- it "deletes the document" do
2181
- expect(event.delete).to be true
1977
+ it 'removes the base id from the inverse keys' do
1978
+ expect(deleted.reload.person_ids).to be_empty
2182
1979
  end
2183
- end
2184
- end
2185
-
2186
- context "when the relationships are self referencing" do
2187
1980
 
2188
- let(:tag_one) do
2189
- Tag.create(text: "one")
2190
- end
1981
+ context 'and person and preferences are reloaded' do
1982
+ before do
1983
+ person.reload
1984
+ preference_one.reload
1985
+ preference_two.reload
1986
+ end
2191
1987
 
2192
- let(:tag_two) do
2193
- Tag.create(text: "two")
2194
- end
1988
+ it 'nullifies the deleted preference' do
1989
+ expect(person.preferences).to eq([ preference_two ])
1990
+ end
2195
1991
 
2196
- before do
2197
- tag_one.related << tag_two
1992
+ it 'retains the ids for one preference' do
1993
+ expect(person.preference_ids).to eq([ preference_two.id ])
1994
+ end
1995
+ end
2198
1996
  end
2199
1997
 
2200
- context "when deleting without reloading" do
2201
-
1998
+ context 'when the document does not exist' do
2202
1999
  let!(:deleted) do
2203
- tag_one.related.delete(tag_two)
2000
+ person.preferences.send(method, Preference.new)
2204
2001
  end
2205
2002
 
2206
- it "deletes the document from the relation" do
2207
- expect(tag_one.related).to be_empty
2003
+ it 'returns nil' do
2004
+ expect(deleted).to be_nil
2208
2005
  end
2209
2006
 
2210
- it "deletes the foreign key from the relation" do
2211
- expect(tag_one.related_ids).to be_empty
2007
+ it 'does not modify the relation' do
2008
+ expect(person.preferences).to eq([ preference_one, preference_two ])
2212
2009
  end
2213
2010
 
2214
- it "removes the reference from the inverse" do
2215
- expect(deleted.related).to be_empty
2216
- end
2217
-
2218
- it "removes the foreign keys from the inverse" do
2219
- expect(deleted.related_ids).to be_empty
2011
+ it 'does not modify the keys' do
2012
+ expect(person.preference_ids).to eq([ preference_one.id, preference_two.id ])
2220
2013
  end
2221
2014
  end
2222
2015
 
2223
- context "when deleting with reloading" do
2224
-
2225
- context "when deleting from the front side" do
2226
-
2227
- let(:reloaded) do
2228
- tag_one.reload
2229
- end
2230
-
2231
- let!(:deleted) do
2232
- reloaded.related.delete(tag_two)
2233
- end
2016
+ context 'when :dependent => :nullify is set' do
2017
+ context 'when :inverse_of is set' do
2018
+ let(:event) { Event.create! }
2234
2019
 
2235
- it "deletes the document from the relation" do
2236
- expect(reloaded.related).to be_empty
2020
+ before do
2021
+ person.administrated_events << [ event ]
2237
2022
  end
2238
2023
 
2239
- it "deletes the foreign key from the relation" do
2240
- expect(reloaded.related_ids).to be_empty
2024
+ it 'deletes the document' do
2025
+ expect(event.delete).to be true
2241
2026
  end
2027
+ end
2028
+ end
2242
2029
 
2243
- it "removes the reference from the inverse" do
2244
- expect(deleted.related).to be_empty
2245
- end
2030
+ context 'when the relationships are self referencing' do
2031
+ let(:tag_one) { Tag.create!(text: 'one') }
2032
+ let(:tag_two) { Tag.create!(text: 'two') }
2246
2033
 
2247
- it "removes the foreign keys from the inverse" do
2248
- expect(deleted.related_ids).to be_empty
2249
- end
2034
+ before do
2035
+ tag_one.related << tag_two
2250
2036
  end
2251
2037
 
2252
- context "when deleting from the inverse side" do
2038
+ context 'when deleting without reloading' do
2039
+ let!(:deleted) { tag_one.related.send(method, tag_two) }
2253
2040
 
2254
- let(:reloaded) do
2255
- tag_two.reload
2041
+ it 'deletes the document from the relation' do
2042
+ expect(tag_one.related).to be_empty
2256
2043
  end
2257
2044
 
2258
- let!(:deleted) do
2259
- reloaded.related.delete(tag_one)
2260
- end
2261
-
2262
- it "deletes the document from the relation" do
2263
- expect(reloaded.related).to be_empty
2045
+ it 'deletes the foreign key from the relation' do
2046
+ expect(tag_one.related_ids).to be_empty
2264
2047
  end
2265
2048
 
2266
- it "deletes the foreign key from the relation" do
2267
- expect(reloaded.related_ids).to be_empty
2049
+ it 'removes the reference from the inverse' do
2050
+ expect(deleted.related).to be_empty
2268
2051
  end
2269
2052
 
2270
- it "removes the foreign keys from the inverse" do
2053
+ it 'removes the foreign keys from the inverse' do
2271
2054
  expect(deleted.related_ids).to be_empty
2272
2055
  end
2273
2056
  end
2274
- end
2275
- end
2276
2057
 
2277
- context "when the association has callbacks" do
2058
+ context 'when deleting with reloading' do
2059
+ context 'when deleting from the front side' do
2060
+ let(:reloaded) { tag_one.reload }
2061
+ let!(:deleted) { reloaded.related.send(method, tag_two) }
2278
2062
 
2279
- let(:post) do
2280
- Post.new
2281
- end
2063
+ it 'deletes the document from the relation' do
2064
+ expect(reloaded.related).to be_empty
2065
+ end
2282
2066
 
2283
- let(:tag) do
2284
- Tag.new
2285
- end
2067
+ it 'deletes the foreign key from the relation' do
2068
+ expect(reloaded.related_ids).to be_empty
2069
+ end
2286
2070
 
2287
- before do
2288
- post.tags << tag
2289
- end
2071
+ it 'removes the reference from the inverse' do
2072
+ expect(deleted.related).to be_empty
2073
+ end
2290
2074
 
2291
- context "when the callback is a before_remove" do
2075
+ it 'removes the foreign keys from the inverse' do
2076
+ expect(deleted.related_ids).to be_empty
2077
+ end
2078
+ end
2292
2079
 
2293
- context "when there are no errors" do
2080
+ context 'when deleting from the inverse side' do
2081
+ let(:reloaded) { tag_two.reload }
2082
+ let!(:deleted) { reloaded.related.send(method, tag_one) }
2294
2083
 
2295
- before do
2296
- post.tags.delete tag
2297
- end
2084
+ it 'deletes the document from the relation' do
2085
+ expect(reloaded.related).to be_empty
2086
+ end
2298
2087
 
2299
- it "executes the callback" do
2300
- expect(post.before_remove_called).to be true
2301
- end
2088
+ it 'deletes the foreign key from the relation' do
2089
+ expect(reloaded.related_ids).to be_empty
2090
+ end
2302
2091
 
2303
- it "removes the document from the relation" do
2304
- expect(post.tags).to be_empty
2092
+ it 'removes the foreign keys from the inverse' do
2093
+ expect(deleted.related_ids).to be_empty
2094
+ end
2305
2095
  end
2306
2096
  end
2097
+ end
2307
2098
 
2308
- context "when errors are raised" do
2309
-
2310
- before do
2311
- expect(post).to receive(:before_remove_tag).and_raise
2312
- begin; post.tags.delete(tag); rescue; end
2313
- end
2099
+ context 'when the association has callbacks' do
2100
+ let(:post) { Post.new }
2101
+ let(:tag) { Tag.new }
2314
2102
 
2315
- it "does not remove the document from the relation" do
2316
- expect(post.tags).to eq([ tag ])
2317
- end
2103
+ before do
2104
+ post.tags << tag
2318
2105
  end
2319
- end
2320
2106
 
2321
- context "when the callback is an after_remove" do
2107
+ context 'when the callback is a before_remove' do
2108
+ context 'when there are no errors' do
2109
+ before do
2110
+ post.tags.send(method, tag)
2111
+ end
2322
2112
 
2323
- context "when no errors are raised" do
2113
+ it 'executes the callback' do
2114
+ expect(post.before_remove_called).to be true
2115
+ end
2324
2116
 
2325
- before do
2326
- post.tags.delete(tag)
2117
+ it 'removes the document from the relation' do
2118
+ expect(post.tags).to be_empty
2119
+ end
2327
2120
  end
2328
2121
 
2329
- it "executes the callback" do
2330
- expect(post.after_remove_called).to be true
2331
- end
2122
+ context 'when errors are raised' do
2123
+ before do
2124
+ expect(post).to receive(:before_remove_tag).and_raise
2125
+ begin; post.tags.send(method, tag); rescue StandardError; end
2126
+ end
2332
2127
 
2333
- it "removes the document from the relation" do
2334
- expect(post.tags).to be_empty
2128
+ it 'does not remove the document from the relation' do
2129
+ expect(post.tags).to eq([ tag ])
2130
+ end
2335
2131
  end
2336
2132
  end
2337
2133
 
2338
- context "when errors are raised" do
2134
+ context 'when the callback is an after_remove' do
2135
+ context 'when no errors are raised' do
2136
+ before do
2137
+ post.tags.send(method, tag)
2138
+ end
2339
2139
 
2340
- before do
2341
- expect(post).to receive(:after_remove_tag).and_raise
2342
- begin; post.tags.delete(tag); rescue; end
2140
+ it 'executes the callback' do
2141
+ expect(post.after_remove_called).to be true
2142
+ end
2143
+
2144
+ it 'removes the document from the relation' do
2145
+ expect(post.tags).to be_empty
2146
+ end
2343
2147
  end
2344
2148
 
2345
- it "removes the document from the relation" do
2346
- expect(post.tags).to be_empty
2149
+ context 'when errors are raised' do
2150
+ before do
2151
+ expect(post).to receive(:after_remove_tag).and_raise
2152
+ begin; post.tags.send(method, tag); rescue StandardError; end
2153
+ end
2154
+
2155
+ it 'removes the document from the relation' do
2156
+ expect(post.tags).to be_empty
2157
+ end
2347
2158
  end
2348
2159
  end
2349
2160
  end
2350
2161
  end
2351
2162
  end
2352
2163
 
2353
- [ :delete_all, :destroy_all ].each do |method|
2164
+ # Whether the conditions carry a pattern decides which branch of remove_all
2165
+ # runs, so anything that has to behave the same on both is exercised twice.
2166
+ conditions_variants = [
2167
+ [ 'no regular expression', nil ],
2168
+ [ 'a regular expression', { name: /Test/ } ]
2169
+ ].freeze
2354
2170
 
2171
+ %i[ delete_all destroy_all ].each do |method|
2355
2172
  describe "##{method}" do
2356
-
2357
- context "when the relation is not polymorphic" do
2358
-
2359
- context "when conditions are provided" do
2360
-
2361
- let(:person) do
2362
- Person.create
2363
- end
2173
+ context 'when the relation is not polymorphic' do
2174
+ context 'when conditions are provided' do
2175
+ let(:person) { Person.create! }
2364
2176
 
2365
2177
  let!(:preference_one) do
2366
- person.preferences.create(name: "Testing")
2178
+ person.preferences.create!(name: 'Testing')
2367
2179
  end
2368
2180
 
2369
2181
  let!(:preference_two) do
2370
- person.preferences.create(name: "Test")
2182
+ person.preferences.create!(name: 'Test')
2371
2183
  end
2372
2184
 
2373
2185
  let!(:deleted) do
2374
2186
  person.preferences.send(
2375
- method,
2376
- { name: "Testing" }
2187
+ method,
2188
+ { name: 'Testing' }
2377
2189
  )
2378
2190
  end
2379
2191
 
2380
- it "removes the correct preferences" do
2192
+ it 'removes the correct preferences' do
2381
2193
  expect(person.preferences.count).to eq(1)
2382
2194
  end
2383
2195
 
2384
- it "deletes the documents from the database" do
2385
- expect(Preference.where(name: "Testing").count).to eq(0)
2196
+ it 'deletes the documents from the database' do
2197
+ expect(Preference.where(name: 'Testing').count).to eq(0)
2386
2198
  end
2387
2199
 
2388
- it "returns the number of documents deleted" do
2200
+ it 'returns the number of documents deleted' do
2389
2201
  expect(deleted).to eq(1)
2390
2202
  end
2391
2203
 
2392
- it "removes the ids from the foreign key" do
2204
+ it 'removes the ids from the foreign key' do
2393
2205
  expect(person.preference_ids).to eq([ preference_two.id ])
2394
2206
  end
2395
- end
2396
2207
 
2397
- context "when conditions are not provided" do
2208
+ it 'sets the association locally' do
2209
+ expect(person.preferences).to eq([ preference_two ])
2210
+ end
2211
+ end
2398
2212
 
2213
+ context 'when conditions are not provided' do
2399
2214
  let(:person) do
2400
- Person.create.tap do |person|
2401
- person.preferences.create(name: "Testing")
2402
- person.preferences.create(name: "Test")
2215
+ Person.create!.tap do |person|
2216
+ person.preferences.create!(name: 'Testing')
2217
+ person.preferences.create!(name: 'Test')
2403
2218
  end
2404
2219
  end
2405
2220
 
@@ -2407,202 +2222,217 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2407
2222
  person.preferences.send(method)
2408
2223
  end
2409
2224
 
2410
- it "removes the correct preferences" do
2225
+ it 'removes the correct preferences' do
2411
2226
  expect(person.preferences.count).to eq(0)
2412
2227
  end
2413
2228
 
2414
- it "deletes the documents from the database" do
2229
+ it 'deletes the documents from the database' do
2415
2230
  expect(Preference.count).to eq(0)
2416
2231
  end
2417
2232
 
2418
- it "returns the number of documents deleted" do
2233
+ it 'returns the number of documents deleted' do
2419
2234
  expect(deleted).to eq(2)
2420
2235
  end
2236
+
2237
+ it 'sets the association locally' do
2238
+ expect(person.preferences).to eq([])
2239
+ end
2240
+ end
2241
+
2242
+ context 'when the base holds the foreign keys' do
2243
+ # unbind_one pulls ids out of the base's foreign key array and marks
2244
+ # the base dirty, so which documents get unbound is visible here.
2245
+ # Only what the association already held is unbound, and that has to
2246
+ # hold whether or not the conditions carry a regular expression:
2247
+ # scanning under a regexp budget loads the association first, and
2248
+ # that load must not turn into an unbind of its own. See the note in
2249
+ # HasMany::Proxy#remove_all_bounded.
2250
+ let!(:person) do
2251
+ Person.create!.tap do |base|
2252
+ base.preferences.create!(name: 'Testing')
2253
+ base.preferences.create!(name: 'Test')
2254
+ end
2255
+ end
2256
+
2257
+ let(:ids) { person.preference_ids.dup }
2258
+
2259
+ conditions_variants.each do |description, conditions|
2260
+ context "when the conditions carry #{description}" do
2261
+ context 'when the association is already in memory' do
2262
+ it 'unbinds the deleted documents' do
2263
+ person.preferences.send(method, conditions)
2264
+ expect(person.preference_ids).to eq([])
2265
+ expect(person).to be_changed
2266
+ end
2267
+ end
2268
+
2269
+ context 'when the association has not been loaded' do
2270
+ let(:cold) { Person.find(person._id) }
2271
+
2272
+ it 'leaves the foreign keys alone' do
2273
+ expect(ids.length).to eq(2)
2274
+ cold.preferences.send(method, conditions)
2275
+ expect(cold.preference_ids).to eq(ids)
2276
+ expect(cold).not_to be_changed
2277
+ end
2278
+ end
2279
+ end
2280
+ end
2421
2281
  end
2422
2282
  end
2423
2283
  end
2424
2284
  end
2425
2285
 
2426
- describe ".embedded?" do
2427
-
2428
- it "returns false" do
2429
- expect(described_class).to_not be_embedded
2286
+ describe '.embedded?' do
2287
+ it 'returns false' do
2288
+ expect(described_class).not_to be_embedded
2430
2289
  end
2431
2290
  end
2432
2291
 
2433
- describe "#exists?" do
2434
-
2292
+ describe '#exists?' do
2435
2293
  let!(:person) do
2436
- Person.create
2294
+ Person.create!
2437
2295
  end
2438
2296
 
2439
- context "when documents exist in the database" do
2440
-
2297
+ context 'when documents exist in the database' do
2441
2298
  before do
2442
- person.preferences.create
2299
+ person.preferences.create!
2443
2300
  end
2444
2301
 
2445
- it "returns true" do
2302
+ it 'returns true' do
2446
2303
  expect(person.preferences.exists?).to be true
2447
2304
  end
2448
2305
  end
2449
2306
 
2450
- context "when no documents exist in the database" do
2451
-
2307
+ context 'when no documents exist in the database' do
2452
2308
  before do
2453
2309
  person.preferences.build
2454
2310
  end
2455
2311
 
2456
- it "returns false" do
2312
+ it 'returns false' do
2457
2313
  expect(person.preferences.exists?).to be false
2458
2314
  end
2459
2315
  end
2460
2316
  end
2461
2317
 
2462
- describe "#find" do
2463
-
2464
- context "when the relation is not polymorphic" do
2465
-
2318
+ describe '#find' do
2319
+ context 'when the relation is not polymorphic' do
2466
2320
  let(:person) do
2467
- Person.create
2321
+ Person.create!
2468
2322
  end
2469
2323
 
2470
2324
  let!(:preference_one) do
2471
- person.preferences.create(name: "Test")
2325
+ person.preferences.create!(name: 'Test')
2472
2326
  end
2473
2327
 
2474
2328
  let!(:preference_two) do
2475
- person.preferences.create(name: "OMG I has relations")
2329
+ person.preferences.create!(name: 'OMG I has relations')
2476
2330
  end
2477
2331
 
2478
2332
  let!(:unrelated_pref) do
2479
- Preference.create(name: "orphan annie")
2333
+ Preference.create!(name: 'orphan annie')
2480
2334
  end
2481
2335
 
2482
2336
  let!(:unrelated_pref_two) do
2483
- Preference.create(name: "orphan two")
2337
+ Preference.create!(name: 'orphan two')
2484
2338
  end
2485
2339
 
2486
- context "when providing an id" do
2487
-
2488
- context "when the id matches" do
2489
-
2340
+ context 'when providing an id' do
2341
+ context 'when the id matches' do
2490
2342
  let(:preference) do
2491
2343
  person.preferences.find(preference_one.id)
2492
2344
  end
2493
2345
 
2494
- it "returns the matching document" do
2346
+ it 'returns the matching document' do
2495
2347
  expect(preference).to eq(preference_one)
2496
2348
  end
2497
2349
  end
2498
2350
 
2499
- context "when the id matches an unreferenced document" do
2500
-
2351
+ context 'when the id matches an unreferenced document' do
2501
2352
  let(:preference) do
2502
2353
  person.preferences.find(unrelated_pref.id)
2503
2354
  end
2504
2355
 
2505
- it "raises an error" do
2506
- expect {
2356
+ it 'raises an error' do
2357
+ expect do
2507
2358
  preference
2508
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
2359
+ end.to raise_error(Mongoid::Errors::DocumentNotFound,
2360
+ /Document\(s\) not found for class Preference with id\(s\)/)
2509
2361
  end
2510
2362
  end
2511
2363
 
2512
- context "when the id does not match" do
2513
-
2514
- context "when config set to raise error" do
2515
-
2516
- before do
2517
- Mongoid.raise_not_found_error = true
2518
- end
2364
+ context 'when the id does not match' do
2365
+ context 'when config set to raise error' do
2366
+ config_override :raise_not_found_error, true
2519
2367
 
2520
- it "raises an error" do
2521
- expect {
2368
+ it 'raises an error' do
2369
+ expect do
2522
2370
  person.preferences.find(BSON::ObjectId.new)
2523
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
2371
+ end.to raise_error(Mongoid::Errors::DocumentNotFound,
2372
+ /Document\(s\) not found for class Preference with id\(s\)/)
2524
2373
  end
2525
2374
  end
2526
2375
 
2527
- context "when config set not to raise error" do
2376
+ context 'when config set not to raise error' do
2377
+ config_override :raise_not_found_error, false
2528
2378
 
2529
2379
  let(:preference) do
2530
2380
  person.preferences.find(BSON::ObjectId.new)
2531
2381
  end
2532
2382
 
2533
- before do
2534
- Mongoid.raise_not_found_error = false
2535
- end
2536
-
2537
- after do
2538
- Mongoid.raise_not_found_error = true
2539
- end
2540
-
2541
- it "returns nil" do
2383
+ it 'returns nil' do
2542
2384
  expect(preference).to be_nil
2543
2385
  end
2544
2386
  end
2545
2387
  end
2546
2388
  end
2547
2389
 
2548
- context "when providing an array of ids" do
2549
-
2550
- context "when the ids match" do
2551
-
2390
+ context 'when providing an array of ids' do
2391
+ context 'when the ids match' do
2552
2392
  let(:preferences) do
2553
2393
  person.preferences.find([ preference_one.id, preference_two.id ])
2554
2394
  end
2555
2395
 
2556
- it "returns the matching documents" do
2396
+ it 'returns the matching documents' do
2557
2397
  expect(preferences).to eq([ preference_one, preference_two ])
2558
2398
  end
2559
2399
  end
2560
2400
 
2561
- context "when the ids matche unreferenced documents" do
2562
-
2401
+ context 'when the ids match unreferenced documents' do
2563
2402
  let(:preferences) do
2564
2403
  person.preferences.find(
2565
- [ unrelated_pref.id, unrelated_pref_two.id ]
2404
+ [ unrelated_pref.id, unrelated_pref_two.id ]
2566
2405
  )
2567
2406
  end
2568
2407
 
2569
- it "raises an error" do
2570
- expect {
2408
+ it 'raises an error' do
2409
+ expect do
2571
2410
  preferences
2572
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
2411
+ end.to raise_error(Mongoid::Errors::DocumentNotFound,
2412
+ /Document\(s\) not found for class Preference with id\(s\)/)
2573
2413
  end
2574
2414
  end
2575
2415
 
2576
- context "when the ids do not match" do
2577
-
2578
- context "when config set to raise error" do
2579
-
2580
- before do
2581
- Mongoid.raise_not_found_error = true
2582
- end
2416
+ context 'when the ids do not match' do
2417
+ context 'when config set to raise error' do
2418
+ config_override :raise_not_found_error, true
2583
2419
 
2584
- it "raises an error" do
2585
- expect {
2420
+ it 'raises an error' do
2421
+ expect do
2586
2422
  person.preferences.find([ BSON::ObjectId.new ])
2587
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
2423
+ end.to raise_error(Mongoid::Errors::DocumentNotFound,
2424
+ /Document\(s\) not found for class Preference with id\(s\)/)
2588
2425
  end
2589
2426
  end
2590
2427
 
2591
- context "when config set not to raise error" do
2428
+ context 'when config set not to raise error' do
2429
+ config_override :raise_not_found_error, false
2592
2430
 
2593
2431
  let(:preferences) do
2594
2432
  person.preferences.find([ BSON::ObjectId.new ])
2595
2433
  end
2596
2434
 
2597
- before do
2598
- Mongoid.raise_not_found_error = false
2599
- end
2600
-
2601
- after do
2602
- Mongoid.raise_not_found_error = true
2603
- end
2604
-
2605
- it "returns an empty array" do
2435
+ it 'returns an empty array' do
2606
2436
  expect(preferences).to be_empty
2607
2437
  end
2608
2438
  end
@@ -2611,285 +2441,256 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2611
2441
  end
2612
2442
  end
2613
2443
 
2614
- describe "#find_or_create_by" do
2615
-
2616
- context "when the relation is not polymorphic" do
2617
-
2444
+ describe '#find_or_create_by' do
2445
+ context 'when the relation is not polymorphic' do
2618
2446
  let(:person) do
2619
- Person.create
2447
+ Person.create!
2620
2448
  end
2621
2449
 
2622
2450
  let!(:preference) do
2623
- person.preferences.create(name: "Testing")
2451
+ person.preferences.create!(name: 'Testing')
2624
2452
  end
2625
2453
 
2626
- context "when the document exists" do
2627
-
2454
+ context 'when the document exists' do
2628
2455
  let(:found) do
2629
- person.preferences.find_or_create_by(name: "Testing")
2456
+ person.preferences.find_or_create_by(name: 'Testing')
2630
2457
  end
2631
2458
 
2632
- it "returns the document" do
2459
+ it 'returns the document' do
2633
2460
  expect(found).to eq(preference)
2634
2461
  end
2635
2462
  end
2636
2463
 
2637
- context "when the document does not exist" do
2638
-
2464
+ context 'when the document does not exist' do
2639
2465
  let(:found) do
2640
- person.preferences.find_or_create_by(name: "Test")
2466
+ person.preferences.find_or_create_by(name: 'Test')
2641
2467
  end
2642
2468
 
2643
- it "sets the new document attributes" do
2644
- expect(found.name).to eq("Test")
2469
+ it 'sets the new document attributes' do
2470
+ expect(found.name).to eq('Test')
2645
2471
  end
2646
2472
 
2647
- it "returns a newly persisted document" do
2473
+ it 'returns a newly persisted document' do
2648
2474
  expect(found).to be_persisted
2649
2475
  end
2650
2476
  end
2651
2477
  end
2652
2478
  end
2653
2479
 
2654
- describe "#find_or_create_by!" do
2655
-
2656
- context "when the relation is not polymorphic" do
2657
-
2480
+ describe '#find_or_create_by!' do
2481
+ context 'when the relation is not polymorphic' do
2658
2482
  let(:person) do
2659
- Person.create
2483
+ Person.create!
2660
2484
  end
2661
2485
 
2662
2486
  let!(:preference) do
2663
- person.preferences.create(name: "Testing")
2487
+ person.preferences.create!(name: 'Testing')
2664
2488
  end
2665
2489
 
2666
- context "when the document exists" do
2667
-
2490
+ context 'when the document exists' do
2668
2491
  let(:found) do
2669
- person.preferences.find_or_create_by!(name: "Testing")
2492
+ person.preferences.find_or_create_by!(name: 'Testing')
2670
2493
  end
2671
2494
 
2672
- it "returns the document" do
2495
+ it 'returns the document' do
2673
2496
  expect(found).to eq(preference)
2674
2497
  end
2675
2498
  end
2676
2499
 
2677
- context "when the document does not exist" do
2678
-
2500
+ context 'when the document does not exist' do
2679
2501
  let(:found) do
2680
- person.preferences.find_or_create_by!(name: "Test")
2502
+ person.preferences.find_or_create_by!(name: 'Test')
2681
2503
  end
2682
2504
 
2683
- it "sets the new document attributes" do
2684
- expect(found.name).to eq("Test")
2505
+ it 'sets the new document attributes' do
2506
+ expect(found.name).to eq('Test')
2685
2507
  end
2686
2508
 
2687
- it "returns a newly persisted document" do
2509
+ it 'returns a newly persisted document' do
2688
2510
  expect(found).to be_persisted
2689
2511
  end
2690
2512
 
2691
- context "when validation fails" do
2692
-
2693
- it "raises an error" do
2694
- expect {
2695
- person.preferences.find_or_create_by!(name: "A")
2696
- }.to raise_error(Mongoid::Errors::Validations)
2513
+ context 'when validation fails' do
2514
+ it 'raises an error' do
2515
+ expect do
2516
+ person.preferences.find_or_create_by!(name: 'A')
2517
+ end.to raise_error(Mongoid::Errors::Validations)
2697
2518
  end
2698
2519
  end
2699
2520
  end
2700
2521
  end
2701
2522
  end
2702
2523
 
2703
- describe "#find_or_initialize_by" do
2704
-
2705
- context "when the relation is not polymorphic" do
2706
-
2524
+ describe '#find_or_initialize_by' do
2525
+ context 'when the relation is not polymorphic' do
2707
2526
  let(:person) do
2708
- Person.create
2527
+ Person.create!
2709
2528
  end
2710
2529
 
2711
2530
  let!(:preference) do
2712
- person.preferences.create(name: "Testing")
2531
+ person.preferences.create!(name: 'Testing')
2713
2532
  end
2714
2533
 
2715
- context "when the document exists" do
2716
-
2534
+ context 'when the document exists' do
2717
2535
  let(:found) do
2718
- person.preferences.find_or_initialize_by(name: "Testing")
2536
+ person.preferences.find_or_initialize_by(name: 'Testing')
2719
2537
  end
2720
2538
 
2721
- it "returns the document" do
2539
+ it 'returns the document' do
2722
2540
  expect(found).to eq(preference)
2723
2541
  end
2724
2542
  end
2725
2543
 
2726
- context "when the document does not exist" do
2727
-
2544
+ context 'when the document does not exist' do
2728
2545
  let(:found) do
2729
- person.preferences.find_or_initialize_by(name: "Test")
2546
+ person.preferences.find_or_initialize_by(name: 'Test')
2730
2547
  end
2731
2548
 
2732
- it "sets the new document attributes" do
2733
- expect(found.name).to eq("Test")
2549
+ it 'sets the new document attributes' do
2550
+ expect(found.name).to eq('Test')
2734
2551
  end
2735
2552
 
2736
- it "returns a non persisted document" do
2737
- expect(found).to_not be_persisted
2553
+ it 'returns a non persisted document' do
2554
+ expect(found).not_to be_persisted
2738
2555
  end
2739
2556
  end
2740
2557
  end
2741
2558
  end
2742
2559
 
2743
- describe "#max" do
2744
-
2560
+ describe '#max' do
2745
2561
  let(:person) do
2746
- Person.create
2562
+ Person.create!
2563
+ end
2564
+ let(:max) do
2565
+ person.preferences.max_by(&:ranking)
2747
2566
  end
2748
2567
 
2749
2568
  let(:preference_one) do
2750
- Preference.create(ranking: 5)
2569
+ Preference.create!(ranking: 5)
2751
2570
  end
2752
2571
 
2753
2572
  let(:preference_two) do
2754
- Preference.create(ranking: 10)
2573
+ Preference.create!(ranking: 10)
2755
2574
  end
2756
2575
 
2757
2576
  before do
2758
2577
  person.preferences.push(preference_one, preference_two)
2759
2578
  end
2760
2579
 
2761
- let(:max) do
2762
- person.preferences.max do |a,b|
2763
- a.ranking <=> b.ranking
2764
- end
2765
- end
2766
-
2767
- it "returns the document with the max value of the supplied field" do
2580
+ it 'returns the document with the max value of the supplied field' do
2768
2581
  expect(max).to eq(preference_two)
2769
2582
  end
2770
2583
  end
2771
2584
 
2772
- describe "#max_by" do
2773
-
2585
+ describe '#max_by' do
2774
2586
  let(:person) do
2775
- Person.create
2587
+ Person.create!
2588
+ end
2589
+ let(:max) do
2590
+ person.preferences.max_by(&:ranking)
2776
2591
  end
2777
2592
 
2778
2593
  let(:preference_one) do
2779
- Preference.create(ranking: 5)
2594
+ Preference.create!(ranking: 5)
2780
2595
  end
2781
2596
 
2782
2597
  let(:preference_two) do
2783
- Preference.create(ranking: 10)
2598
+ Preference.create!(ranking: 10)
2784
2599
  end
2785
2600
 
2786
2601
  before do
2787
2602
  person.preferences.push(preference_one, preference_two)
2788
2603
  end
2789
2604
 
2790
- let(:max) do
2791
- person.preferences.max_by(&:ranking)
2792
- end
2793
-
2794
- it "returns the document with the max value of the supplied field" do
2605
+ it 'returns the document with the max value of the supplied field' do
2795
2606
  expect(max).to eq(preference_two)
2796
2607
  end
2797
2608
  end
2798
2609
 
2799
- describe "#method_missing" do
2800
-
2610
+ describe '#method_missing' do
2801
2611
  let!(:person) do
2802
- Person.create
2612
+ Person.create!
2803
2613
  end
2804
2614
 
2805
2615
  let!(:preference_one) do
2806
- person.preferences.create(name: "First", value: "Posting")
2616
+ person.preferences.create!(name: 'First', value: 'Posting')
2807
2617
  end
2808
2618
 
2809
2619
  let!(:preference_two) do
2810
- person.preferences.create(name: "Second", value: "Testing")
2620
+ person.preferences.create!(name: 'Second', value: 'Testing')
2811
2621
  end
2812
2622
 
2813
2623
  let!(:unrelated) do
2814
- Preference.create(name: "Third")
2624
+ Preference.create!(name: 'Third')
2815
2625
  end
2816
2626
 
2817
- context "when providing a single criteria" do
2818
-
2627
+ context 'when providing a single criteria' do
2819
2628
  let(:preferences) do
2820
- person.preferences.where(name: "First")
2629
+ person.preferences.where(name: 'First')
2821
2630
  end
2822
2631
 
2823
- it "applies the criteria to the documents" do
2632
+ it 'applies the criteria to the documents' do
2824
2633
  expect(preferences).to eq([ preference_one ])
2825
2634
  end
2826
2635
 
2827
2636
  context 'when providing a collation' do
2828
- min_server_version '3.4'
2829
-
2830
2637
  let(:preferences) do
2831
- person.preferences.where(name: "FIRST").collation(locale: 'en_US', strength: 2).to_a
2638
+ person.preferences.where(name: 'FIRST').collation(locale: 'en_US', strength: 2).to_a
2832
2639
  end
2833
2640
 
2834
- it "applies the collation option to the query" do
2641
+ it 'applies the collation option to the query' do
2835
2642
  expect(preferences).to eq([ preference_one ])
2836
2643
  end
2837
2644
  end
2838
2645
  end
2839
2646
 
2840
- context "when providing a criteria on id" do
2841
-
2647
+ context 'when providing a criteria on id' do
2842
2648
  let(:preferences) do
2843
2649
  person.preferences.where(_id: unrelated.id)
2844
2650
  end
2845
2651
 
2846
- it "does not return unrelated documents" do
2652
+ it 'does not return unrelated documents' do
2847
2653
  expect(preferences).to be_empty
2848
2654
  end
2849
2655
  end
2850
2656
 
2851
- context "when providing a criteria class method" do
2852
-
2657
+ context 'when providing a criteria class method' do
2853
2658
  let(:preferences) do
2854
2659
  person.preferences.posting
2855
2660
  end
2856
2661
 
2857
- it "applies the criteria to the documents" do
2662
+ it 'applies the criteria to the documents' do
2858
2663
  expect(preferences).to eq([ preference_one ])
2859
2664
  end
2860
2665
  end
2861
2666
 
2862
- context "when chaining criteria" do
2863
-
2667
+ context 'when chaining criteria' do
2864
2668
  let(:preferences) do
2865
- person.preferences.posting.where(:name.in => [ "First" ])
2669
+ person.preferences.posting.where(:name.in => [ 'First' ])
2866
2670
  end
2867
2671
 
2868
- it "applies the criteria to the documents" do
2672
+ it 'applies the criteria to the documents' do
2869
2673
  expect(preferences).to eq([ preference_one ])
2870
2674
  end
2871
2675
  end
2872
2676
 
2873
- context "when delegating methods" do
2874
-
2875
- describe "#distinct" do
2876
-
2677
+ context 'when delegating methods' do
2678
+ describe '#distinct' do
2877
2679
  let(:values) do
2878
2680
  person.preferences.distinct(:name)
2879
2681
  end
2880
2682
 
2881
- it "returns the distinct values for the fields" do
2882
- expect(values).to include("First")
2883
- expect(values).to include("Second")
2683
+ it 'returns the distinct values for the fields' do
2684
+ expect(values).to include('First')
2685
+ expect(values).to include('Second')
2884
2686
  end
2885
2687
 
2886
- context "when the inverse relation is not defined" do
2887
-
2688
+ context 'when the inverse relation is not defined' do
2888
2689
  let!(:house) do
2889
- person.houses.create(name: "Wayne Manor")
2690
+ person.houses.create!(name: 'Wayne Manor')
2890
2691
  end
2891
2692
 
2892
- it "returns the distinct values for the fields" do
2693
+ it 'returns the distinct values for the fields' do
2893
2694
  expect(person.houses.distinct(:name)).to eq([ house.name ])
2894
2695
  end
2895
2696
  end
@@ -2897,118 +2698,109 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2897
2698
  end
2898
2699
  end
2899
2700
 
2900
- describe "#min" do
2901
-
2701
+ describe '#min' do
2902
2702
  let(:person) do
2903
- Person.create
2703
+ Person.create!
2704
+ end
2705
+ let(:min) do
2706
+ person.preferences.min_by(&:ranking)
2904
2707
  end
2905
2708
 
2906
2709
  let(:preference_one) do
2907
- Preference.create(ranking: 5)
2710
+ Preference.create!(ranking: 5)
2908
2711
  end
2909
2712
 
2910
2713
  let(:preference_two) do
2911
- Preference.create(ranking: 10)
2714
+ Preference.create!(ranking: 10)
2912
2715
  end
2913
2716
 
2914
2717
  before do
2915
2718
  person.preferences.push(preference_one, preference_two)
2916
2719
  end
2917
2720
 
2918
- let(:min) do
2919
- person.preferences.min do |a, b|
2920
- a.ranking <=> b.ranking
2921
- end
2922
- end
2923
-
2924
- it "returns the min value of the supplied field" do
2721
+ it 'returns the min value of the supplied field' do
2925
2722
  expect(min).to eq(preference_one)
2926
2723
  end
2927
2724
  end
2928
2725
 
2929
- describe "#min_by" do
2930
-
2726
+ describe '#min_by' do
2931
2727
  let(:person) do
2932
- Person.create
2728
+ Person.create!
2729
+ end
2730
+ let(:min) do
2731
+ person.preferences.min_by(&:ranking)
2933
2732
  end
2934
2733
 
2935
2734
  let(:preference_one) do
2936
- Preference.create(ranking: 5)
2735
+ Preference.create!(ranking: 5)
2937
2736
  end
2938
2737
 
2939
2738
  let(:preference_two) do
2940
- Preference.create(ranking: 10)
2739
+ Preference.create!(ranking: 10)
2941
2740
  end
2942
2741
 
2943
2742
  before do
2944
2743
  person.preferences.push(preference_one, preference_two)
2945
2744
  end
2946
2745
 
2947
- let(:min) do
2948
- person.preferences.min_by(&:ranking)
2949
- end
2950
-
2951
- it "returns the min value of the supplied field" do
2746
+ it 'returns the min value of the supplied field' do
2952
2747
  expect(min).to eq(preference_one)
2953
2748
  end
2954
2749
  end
2955
2750
 
2956
- describe "#nil?" do
2957
-
2958
- it "returns false" do
2959
- expect(Person.new.preferences).to_not be_nil
2751
+ describe '#nil?' do
2752
+ it 'returns false' do
2753
+ expect(Person.new.preferences).not_to be_nil
2960
2754
  end
2961
2755
  end
2962
2756
 
2963
- describe "#nullify_all" do
2964
-
2757
+ describe '#nullify_all' do
2965
2758
  let(:person) do
2966
- Person.create
2759
+ Person.create!
2967
2760
  end
2968
2761
 
2969
2762
  let!(:preference_one) do
2970
- person.preferences.create(name: "One")
2763
+ person.preferences.create!(name: 'One')
2971
2764
  end
2972
2765
 
2973
2766
  let!(:preference_two) do
2974
- person.preferences.create(name: "Two")
2767
+ person.preferences.create!(name: 'Two')
2975
2768
  end
2976
2769
 
2977
2770
  before do
2978
2771
  person.preferences.nullify_all
2979
2772
  end
2980
2773
 
2981
- it "removes the foreign key from the base document" do
2774
+ it 'removes the foreign key from the base document' do
2982
2775
  [ preference_one, preference_two ].each do |preference|
2983
- expect(person.preference_ids).to_not include(preference.id)
2776
+ expect(person.preference_ids).not_to include(preference.id)
2984
2777
  end
2985
2778
  end
2986
2779
 
2987
- it "removes the foreign key from the target documents" do
2780
+ it 'removes the foreign key from the target documents' do
2988
2781
  [ preference_one, preference_two ].each do |preference|
2989
- expect(preference.person_ids).to_not include(person.id)
2782
+ expect(preference.person_ids).not_to include(person.id)
2990
2783
  end
2991
2784
  end
2992
2785
 
2993
- it "removes the reference from the base document" do
2786
+ it 'removes the reference from the base document' do
2994
2787
  [ preference_one, preference_two ].each do |preference|
2995
- expect(person.preferences).to_not include(preference)
2788
+ expect(person.preferences).not_to include(preference)
2996
2789
  end
2997
2790
  end
2998
2791
 
2999
- it "removes the reference from the target documents" do
2792
+ it 'removes the reference from the target documents' do
3000
2793
  [ preference_one, preference_two ].each do |preference|
3001
- expect(preference.people).to_not include(person)
2794
+ expect(preference.people).not_to include(person)
3002
2795
  end
3003
2796
  end
3004
2797
 
3005
- it "saves the documents" do
3006
- expect(preference_one.reload.people).to_not include(person)
2798
+ it 'saves the documents' do
2799
+ expect(preference_one.reload.people).not_to include(person)
3007
2800
  end
3008
2801
  end
3009
2802
 
3010
- describe "#respond_to?" do
3011
-
2803
+ describe '#respond_to?' do
3012
2804
  let(:person) do
3013
2805
  Person.new
3014
2806
  end
@@ -3017,307 +2809,330 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3017
2809
  person.preferences
3018
2810
  end
3019
2811
 
3020
- Array.public_instance_methods.each do |method|
3021
-
3022
- context "when checking #{method}" do
2812
+ Array.public_instance_methods.sort.each do |method|
2813
+ context "when checking Array##{method}" do
2814
+ before do
2815
+ expect([].respond_to?(method)).to be true
2816
+ end
3023
2817
 
3024
- it "returns true" do
2818
+ it 'returns true' do
3025
2819
  expect(preferences.respond_to?(method)).to be true
3026
2820
  end
3027
2821
  end
3028
2822
  end
3029
2823
 
3030
- Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy.public_instance_methods.each do |method|
3031
-
3032
- context "when checking #{method}" do
3033
-
3034
- it "returns true" do
2824
+ Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy.public_instance_methods.sort.each do |method|
2825
+ context "when checking Proxy##{method}" do
2826
+ it 'returns true' do
3035
2827
  expect(preferences.respond_to?(method)).to be true
3036
2828
  end
3037
2829
  end
3038
2830
  end
3039
2831
 
3040
- Preference.scopes.keys.each do |method|
3041
-
3042
- context "when checking #{method}" do
3043
-
3044
- it "returns true" do
2832
+ Preference.scopes.keys.sort.each do |method|
2833
+ context "when checking scope #{method}" do
2834
+ it 'returns true' do
3045
2835
  expect(preferences.respond_to?(method)).to be true
3046
2836
  end
3047
2837
  end
3048
2838
  end
3049
2839
  end
3050
2840
 
3051
- describe "#scoped" do
3052
-
3053
- let(:person) do
3054
- Person.new
3055
- end
3056
-
2841
+ describe '#scoped' do
3057
2842
  let(:scoped) do
3058
2843
  person.preferences.scoped
3059
2844
  end
3060
2845
 
3061
- it "returns the relation criteria" do
3062
- expect(scoped).to be_a(Mongoid::Criteria)
2846
+ context 'when association is empty' do
2847
+ let(:person) do
2848
+ Person.new
2849
+ end
2850
+
2851
+ it 'returns the relation criteria' do
2852
+ expect(scoped).to be_a(Mongoid::Criteria)
2853
+ end
2854
+
2855
+ it 'returns with an empty selector' do
2856
+ expect(scoped.selector).to eq('_id' => { '$in' => [] })
2857
+ end
3063
2858
  end
3064
2859
 
3065
- it "returns with an empty selector" do
3066
- expect(scoped.selector).to eq({ "$and" => [{ "_id" => { "$in" => [] }}]})
2860
+ context 'when association is not empty' do
2861
+ let(:person) do
2862
+ Person.create!(preferences: [
2863
+ Preference.new(id: 123)
2864
+ ])
2865
+ end
2866
+
2867
+ it 'returns with a selector including association element ids' do
2868
+ expect(scoped.selector).to eq('_id' => { '$in' => [ 123 ] })
2869
+ end
3067
2870
  end
3068
2871
  end
3069
2872
 
3070
- [ :size, :length ].each do |method|
3071
-
2873
+ %i[size length].each do |method|
3072
2874
  describe "##{method}" do
3073
-
3074
2875
  let(:person) do
3075
- Person.create
2876
+ Person.create!
3076
2877
  end
3077
2878
 
3078
- context "when documents have been persisted" do
3079
-
2879
+ context 'when documents have been persisted' do
3080
2880
  let!(:preference) do
3081
- person.preferences.create(name: "Testing")
2881
+ person.preferences.create!(name: 'Testing')
3082
2882
  end
3083
2883
 
3084
- it "returns the number of documents" do
2884
+ it 'returns the number of documents' do
3085
2885
  expect(person.preferences.send(method)).to eq(1)
3086
2886
  end
3087
2887
  end
3088
2888
 
3089
- context "when documents have not been persisted" do
2889
+ # MONGOID-5844
2890
+ #
2891
+ # Specifically, this tests the case where the association is
2892
+ # initialized with a single element (so that Proxy#push does not take
2893
+ # the `concat` route), which causes `reset_unloaded` to be called, which
2894
+ # sets the `_unloaded` Criteria object to match only the specific element
2895
+ # that was given.
2896
+ #
2897
+ # The issue now is that when the events list is updated to be both events,
2898
+ # _unloaded matches one of them already, and the other has previously been
2899
+ # persisted so `new_record?` won't match it. We need to make sure the
2900
+ # `#size` logic properly accounts for this case.
2901
+ context 'when documents have been previously persisted' do
2902
+ let(:person1) { Person.create! }
2903
+ let(:person2) { Person.create! }
2904
+ let(:event1) { Event.create!(administrators: [ person1 ]) }
2905
+ let(:event2) { Event.create!(administrators: [ person2 ]) }
3090
2906
 
3091
2907
  before do
3092
- person.preferences.build(name: "Test")
3093
- person.preferences.create(name: "Test 2")
2908
+ person1.administrated_events = [ event1, event2 ]
2909
+ end
2910
+
2911
+ it 'returns the number of associated documents [MONGOID-5844]' do
2912
+ expect(person1.administrated_events.to_a.size).to eq(2)
2913
+ expect(person1.administrated_events.size).to eq(2)
3094
2914
  end
2915
+ end
3095
2916
 
3096
- it "returns the total number of documents" do
2917
+ context 'when documents have not been persisted' do
2918
+ before do
2919
+ person.preferences.build(name: 'Test')
2920
+ person.preferences.create!(name: 'Test 2')
2921
+ end
2922
+
2923
+ it 'returns the total number of documents' do
3097
2924
  expect(person.preferences.send(method)).to eq(2)
3098
2925
  end
3099
2926
  end
3100
2927
  end
3101
2928
  end
3102
2929
 
3103
- describe "#unscoped" do
3104
-
3105
- context "when the relation has no default scope" do
3106
-
2930
+ describe '#unscoped' do
2931
+ context 'when the relation has no default scope' do
3107
2932
  let!(:person) do
3108
- Person.create
2933
+ Person.create!
3109
2934
  end
3110
2935
 
3111
2936
  let!(:preference_one) do
3112
- person.preferences.create(name: "first")
2937
+ person.preferences.create!(name: 'first')
3113
2938
  end
3114
2939
 
3115
2940
  let!(:preference_two) do
3116
- Preference.create(name: "second")
2941
+ Preference.create!(name: 'second')
3117
2942
  end
3118
2943
 
3119
2944
  let(:unscoped) do
3120
2945
  person.preferences.unscoped
3121
2946
  end
3122
2947
 
3123
- it "returns only the associated documents" do
2948
+ it 'returns only the associated documents' do
3124
2949
  expect(unscoped).to eq([ preference_one ])
3125
2950
  end
3126
2951
  end
3127
2952
 
3128
- context "when the relation has a default scope" do
3129
-
2953
+ context 'when the relation has a default scope' do
3130
2954
  let!(:person) do
3131
- Person.create
2955
+ Person.create!
3132
2956
  end
3133
2957
 
3134
2958
  let!(:house_one) do
3135
- person.houses.create(name: "first")
2959
+ person.houses.create!(name: 'first')
3136
2960
  end
3137
2961
 
3138
2962
  let!(:house_two) do
3139
- House.create(name: "second")
2963
+ House.create!(name: 'second')
3140
2964
  end
3141
2965
 
3142
2966
  let(:unscoped) do
3143
2967
  person.houses.unscoped
3144
2968
  end
3145
2969
 
3146
- it "only returns associated documents" do
2970
+ it 'only returns associated documents' do
3147
2971
  expect(unscoped).to eq([ house_one ])
3148
2972
  end
3149
2973
 
3150
- it "removes the default scoping options" do
2974
+ it 'removes the default scoping options' do
3151
2975
  expect(unscoped.options).to eq({})
3152
2976
  end
3153
2977
  end
3154
2978
  end
3155
2979
 
3156
- context "when setting the ids directly after the documents" do
3157
-
2980
+ context 'when setting the ids directly after the documents' do
3158
2981
  let!(:person) do
3159
- Person.create!(title: "The Boss")
2982
+ Person.create!(title: 'The Boss')
3160
2983
  end
3161
2984
 
3162
2985
  let!(:girlfriend_house) do
3163
- House.create!(name: "Girlfriend")
2986
+ House.create!(name: 'Girlfriend')
3164
2987
  end
3165
2988
 
3166
2989
  let!(:wife_house) do
3167
- House.create!(name: "Wife")
2990
+ House.create!(name: 'Wife')
3168
2991
  end
3169
2992
 
3170
2993
  let!(:exwife_house) do
3171
- House.create!(name: "Ex-Wife")
2994
+ House.create!(name: 'Ex-Wife')
3172
2995
  end
3173
2996
 
3174
2997
  before do
3175
- person.update_attributes(
3176
- houses: [ wife_house, exwife_house, girlfriend_house ]
2998
+ person.update_attributes!(
2999
+ houses: [ wife_house, exwife_house, girlfriend_house ]
3177
3000
  )
3178
- person.update_attributes(house_ids: [ girlfriend_house.id ])
3001
+ person.update_attributes!(house_ids: [ girlfriend_house.id ])
3179
3002
  end
3180
3003
 
3181
- context "when reloading" do
3182
-
3183
- it "properly sets the references" do
3004
+ context 'when reloading' do
3005
+ it 'properly sets the references' do
3184
3006
  expect(person.houses(true)).to eq([ girlfriend_house ])
3185
3007
  end
3186
3008
  end
3187
3009
  end
3188
3010
 
3189
- context "when setting both sides in a single call" do
3190
-
3191
- context "when the documents are new" do
3192
-
3011
+ context 'when setting both sides in a single call' do
3012
+ context 'when the documents are new' do
3193
3013
  let(:user) do
3194
- User.new(name: "testing")
3014
+ User.new(name: 'testing')
3195
3015
  end
3196
3016
 
3197
3017
  let(:business) do
3198
- Business.new(name: "serious", owners: [ user ])
3018
+ Business.new(name: 'serious', owners: [ user ])
3199
3019
  end
3200
3020
 
3201
3021
  before do
3202
3022
  user.businesses = [ business ]
3203
3023
  end
3204
3024
 
3205
- it "sets the businesses" do
3025
+ it 'sets the businesses' do
3206
3026
  expect(user.businesses).to eq([ business ])
3207
3027
  end
3208
3028
 
3209
- it "sets the inverse users" do
3029
+ it 'sets the inverse users' do
3210
3030
  expect(user.businesses.first.owners.first).to eq(user)
3211
3031
  end
3212
3032
 
3213
- it "sets the inverse businesses" do
3033
+ it 'sets the inverse businesses' do
3214
3034
  expect(business.owners).to eq([ user ])
3215
3035
  end
3216
3036
  end
3217
3037
 
3218
- context "when one side is persisted" do
3219
-
3038
+ context 'when one side is persisted' do
3220
3039
  let!(:user) do
3221
- User.new(name: "testing")
3040
+ User.new(name: 'testing')
3222
3041
  end
3223
3042
 
3224
3043
  let!(:business) do
3225
- Business.create(name: "serious", owners: [ user ])
3044
+ Business.create!(name: 'serious', owners: [ user ])
3226
3045
  end
3227
3046
 
3228
3047
  before do
3229
3048
  user.businesses = [ business ]
3230
3049
  end
3231
3050
 
3232
- it "sets the businesses" do
3051
+ it 'sets the businesses' do
3233
3052
  expect(user.businesses).to eq([ business ])
3234
3053
  end
3235
3054
 
3236
- it "sets the inverse users" do
3055
+ it 'sets the inverse users' do
3237
3056
  expect(user.businesses.first.owners.first).to eq(user)
3238
3057
  end
3239
3058
 
3240
- it "sets the inverse businesses" do
3059
+ it 'sets the inverse businesses' do
3241
3060
  expect(business.owners).to eq([ user ])
3242
3061
  end
3243
3062
 
3244
- context "when reloading" do
3245
-
3063
+ context 'when reloading' do
3246
3064
  before do
3247
3065
  user.reload
3248
3066
  business.reload
3249
3067
  end
3250
3068
 
3251
- it "persists the businesses" do
3069
+ it 'persists the businesses' do
3252
3070
  expect(user.businesses).to eq([ business ])
3253
3071
  end
3254
3072
 
3255
- it "persists the inverse users" do
3073
+ it 'persists the inverse users' do
3256
3074
  expect(user.businesses.first.owners.first).to eq(user)
3257
3075
  end
3258
3076
 
3259
- it "persists the inverse businesses" do
3077
+ it 'persists the inverse businesses' do
3260
3078
  expect(business.owners).to eq([ user ])
3261
3079
  end
3262
3080
  end
3263
3081
  end
3264
3082
 
3265
- context "when the documents are persisted" do
3266
-
3083
+ context 'when the documents are persisted' do
3267
3084
  let(:user) do
3268
- User.create(name: "tst")
3085
+ User.create!(name: 'tst')
3269
3086
  end
3270
3087
 
3271
3088
  let(:business) do
3272
- Business.create(name: "srs", owners: [ user ])
3089
+ Business.create!(name: 'srs', owners: [ user ])
3273
3090
  end
3274
3091
 
3275
3092
  before do
3276
3093
  user.businesses = [ business ]
3277
3094
  end
3278
3095
 
3279
- it "sets the businesses" do
3096
+ it 'sets the businesses' do
3280
3097
  expect(user.businesses).to eq([ business ])
3281
3098
  end
3282
3099
 
3283
- it "sets the inverse users" do
3100
+ it 'sets the inverse users' do
3284
3101
  expect(user.businesses.first.owners.first).to eq(user)
3285
3102
  end
3286
3103
 
3287
- it "sets the inverse businesses" do
3104
+ it 'sets the inverse businesses' do
3288
3105
  expect(business.owners).to eq([ user ])
3289
3106
  end
3290
3107
 
3291
- context "when reloading" do
3292
-
3108
+ context 'when reloading' do
3293
3109
  before do
3294
3110
  user.reload
3295
3111
  business.reload
3296
3112
  end
3297
3113
 
3298
- it "persists the businesses" do
3114
+ it 'persists the businesses' do
3299
3115
  expect(user.businesses).to eq([ business ])
3300
3116
  end
3301
3117
 
3302
- it "persists the inverse users" do
3118
+ it 'persists the inverse users' do
3303
3119
  expect(user.businesses.first.owners.first).to eq(user)
3304
3120
  end
3305
3121
 
3306
- it "persists the inverse businesses" do
3122
+ it 'persists the inverse businesses' do
3307
3123
  expect(business.owners).to eq([ user ])
3308
3124
  end
3309
3125
  end
3310
3126
  end
3311
3127
  end
3312
3128
 
3313
- context "when binding the relation multiple times" do
3314
-
3129
+ context 'when binding the relation multiple times' do
3315
3130
  let(:person) do
3316
- Person.create
3131
+ Person.create!
3317
3132
  end
3318
3133
 
3319
3134
  let(:preference) do
3320
- person.preferences.create(name: "testing")
3135
+ person.preferences.create!(name: 'testing')
3321
3136
  end
3322
3137
 
3323
3138
  before do
@@ -3328,27 +3143,26 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3328
3143
  end
3329
3144
  end
3330
3145
 
3331
- it "does not duplicate foreign keys" do
3146
+ it 'does not duplicate foreign keys' do
3332
3147
  expect(person.preference_ids).to eq([ preference.id ])
3333
3148
  end
3334
3149
  end
3335
3150
 
3336
- context "when the association has order criteria" do
3337
-
3151
+ context 'when the association has order criteria' do
3338
3152
  let(:person) do
3339
- Person.create
3153
+ Person.create!
3340
3154
  end
3341
3155
 
3342
3156
  let(:preference_one) do
3343
- OrderedPreference.create(name: 'preference-1', value: 10)
3157
+ OrderedPreference.create!(name: 'preference-1', value: 10)
3344
3158
  end
3345
3159
 
3346
3160
  let(:preference_two) do
3347
- OrderedPreference.create(name: 'preference-2', value: 20)
3161
+ OrderedPreference.create!(name: 'preference-2', value: 20)
3348
3162
  end
3349
3163
 
3350
3164
  let(:preference_three) do
3351
- OrderedPreference.create(name: 'preference-3', value: 20)
3165
+ OrderedPreference.create!(name: 'preference-3', value: 20)
3352
3166
  end
3353
3167
 
3354
3168
  before do
@@ -3356,23 +3170,23 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3356
3170
  person.ordered_preferences.push(preference_one, preference_two, preference_three)
3357
3171
  end
3358
3172
 
3359
- it "orders the documents" do
3173
+ it 'orders the documents' do
3360
3174
  expect(person.ordered_preferences(true)).to eq(
3361
- [preference_two, preference_three, preference_one]
3362
- )
3175
+ [ preference_two, preference_three, preference_one ]
3176
+ )
3363
3177
  end
3364
3178
 
3365
- it "chains default criteria with additional" do
3179
+ it 'chains default criteria with additional' do
3366
3180
  expect(person.ordered_preferences.order_by(:name.desc).to_a).to eq(
3367
- [preference_three, preference_two, preference_one]
3368
- )
3181
+ [ preference_three, preference_two,
3182
+ preference_one ]
3183
+ )
3369
3184
  end
3370
3185
  end
3371
3186
 
3372
- context "when the parent is not a new record and freshly loaded" do
3373
-
3187
+ context 'when the parent is not a new record and freshly loaded' do
3374
3188
  let(:person) do
3375
- Person.create
3189
+ Person.create!
3376
3190
  end
3377
3191
 
3378
3192
  let(:preference) do
@@ -3381,62 +3195,59 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3381
3195
 
3382
3196
  before do
3383
3197
  person.preferences = [ preference ]
3384
- person.save
3198
+ person.save!
3385
3199
  person.reload
3386
3200
  person.preferences = nil
3387
3201
  end
3388
3202
 
3389
- it "sets the relation to an empty array" do
3203
+ it 'sets the relation to an empty array' do
3390
3204
  expect(person.preferences).to be_empty
3391
3205
  end
3392
3206
 
3393
- it "removes the foreign key values" do
3207
+ it 'removes the foreign key values' do
3394
3208
  expect(person.preference_ids).to be_empty
3395
3209
  end
3396
3210
 
3397
- it "does not delete the target from the database" do
3398
- expect {
3211
+ it 'does not delete the target from the database' do
3212
+ expect do
3399
3213
  preference.reload
3400
- }.not_to raise_error
3214
+ end.not_to raise_error
3401
3215
  end
3402
3216
  end
3403
3217
 
3404
- context "when reloading the relation" do
3405
-
3218
+ context 'when reloading the relation' do
3406
3219
  let!(:person) do
3407
- Person.create
3220
+ Person.create!
3408
3221
  end
3409
3222
 
3410
3223
  let!(:preference_one) do
3411
- Preference.create(name: "one")
3224
+ Preference.create!(name: 'one')
3412
3225
  end
3413
3226
 
3414
3227
  let!(:preference_two) do
3415
- Preference.create(name: "two")
3228
+ Preference.create!(name: 'two')
3416
3229
  end
3417
3230
 
3418
3231
  before do
3419
3232
  person.preferences << preference_one
3420
3233
  end
3421
3234
 
3422
- context "when the relation references the same documents" do
3423
-
3235
+ context 'when the relation references the same documents' do
3424
3236
  before do
3425
- Preference.collection.find({ _id: preference_one.id }).
3426
- update_one({ "$set" => { name: "reloaded" }})
3237
+ Preference.collection.find({ _id: preference_one.id })
3238
+ .update_one({ '$set' => { name: 'reloaded' } })
3427
3239
  end
3428
3240
 
3429
3241
  let(:reloaded) do
3430
3242
  person.preferences(true)
3431
3243
  end
3432
3244
 
3433
- it "reloads the document from the database" do
3434
- expect(reloaded.first.name).to eq("reloaded")
3245
+ it 'reloads the document from the database' do
3246
+ expect(reloaded.first.name).to eq('reloaded')
3435
3247
  end
3436
3248
  end
3437
3249
 
3438
- context "when the relation references different documents" do
3439
-
3250
+ context 'when the relation references different documents' do
3440
3251
  before do
3441
3252
  person.preferences << preference_two
3442
3253
  end
@@ -3445,18 +3256,16 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3445
3256
  person.preferences(true)
3446
3257
  end
3447
3258
 
3448
- it "reloads the new document from the database" do
3259
+ it 'reloads the new document from the database' do
3449
3260
  expect(reloaded).to eq([ preference_one, preference_two ])
3450
3261
  end
3451
3262
  end
3452
3263
  end
3453
3264
 
3454
- context "when adding to a relation via a field setter" do
3455
-
3456
- context "when the document is new" do
3457
-
3265
+ context 'when adding to a relation via a field setter' do
3266
+ context 'when the document is new' do
3458
3267
  let!(:person) do
3459
- Person.create(preference_names: "one, two")
3268
+ Person.create!(preference_names: 'one, two')
3460
3269
  end
3461
3270
 
3462
3271
  let(:preference_one) do
@@ -3467,113 +3276,106 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3467
3276
  person.reload.preferences.last
3468
3277
  end
3469
3278
 
3470
- it "persists the first preference" do
3471
- expect(preference_one).to_not be_nil
3279
+ it 'persists the first preference' do
3280
+ expect(preference_one).not_to be_nil
3472
3281
  end
3473
3282
 
3474
- it "sets the first inverse" do
3283
+ it 'sets the first inverse' do
3475
3284
  expect(preference_one.people).to eq([ person ])
3476
3285
  end
3477
3286
 
3478
- it "persists the second preference" do
3479
- expect(preference_two).to_not be_nil
3287
+ it 'persists the second preference' do
3288
+ expect(preference_two).not_to be_nil
3480
3289
  end
3481
3290
 
3482
- it "sets the second inverse keys" do
3291
+ it 'sets the second inverse keys' do
3483
3292
  expect(preference_two.people).to eq([ person ])
3484
3293
  end
3485
3294
  end
3486
3295
  end
3487
3296
 
3488
- context "when changing the order of existing ids" do
3489
-
3297
+ context 'when changing the order of existing ids' do
3490
3298
  let(:person) do
3491
3299
  Person.new
3492
3300
  end
3493
3301
 
3494
3302
  let(:preference_one) do
3495
- Preference.create(name: "one")
3303
+ Preference.create!(name: 'one')
3496
3304
  end
3497
3305
 
3498
3306
  let(:preference_two) do
3499
- Preference.create(name: "two")
3307
+ Preference.create!(name: 'two')
3500
3308
  end
3501
3309
 
3502
3310
  before do
3503
3311
  person.preference_ids = [ preference_one.id, preference_two.id ]
3504
- person.save
3312
+ person.save!
3505
3313
  end
3506
3314
 
3507
- context "and the order is changed" do
3508
-
3315
+ context 'and the order is changed' do
3509
3316
  before do
3510
3317
  person.preference_ids = [ preference_two.id, preference_one.id ]
3511
- person.save
3318
+ person.save!
3512
3319
  end
3513
3320
 
3514
3321
  let(:reloaded) do
3515
3322
  Person.find(person.id)
3516
3323
  end
3517
3324
 
3518
- it "persists the change in id order" do
3325
+ it 'persists the change in id order' do
3519
3326
  expect(reloaded.preference_ids).to eq(
3520
- [ preference_two.id, preference_one.id ]
3521
- )
3327
+ [ preference_two.id, preference_one.id ]
3328
+ )
3522
3329
  end
3523
3330
  end
3524
3331
 
3525
- context "and the order is changed and an element is added" do
3526
-
3332
+ context 'and the order is changed and an element is added' do
3527
3333
  let(:preference_three) do
3528
- Preference.create(name: "three")
3334
+ Preference.create!(name: 'three')
3335
+ end
3336
+ let(:reloaded) do
3337
+ Person.find(person.id)
3529
3338
  end
3530
3339
 
3531
3340
  before do
3532
3341
  person.preference_ids =
3533
- [ preference_two.id, preference_one.id, preference_three.id ]
3534
- person.save
3535
- end
3536
-
3537
- let(:reloaded) do
3538
- Person.find(person.id)
3342
+ [ preference_two.id, preference_one.id, preference_three.id ]
3343
+ person.save!
3539
3344
  end
3540
3345
 
3541
- it "also persists the change in id order" do
3346
+ it 'also persists the change in id order' do
3542
3347
  expect(reloaded.preference_ids).to eq(
3543
- [ preference_two.id, preference_one.id, preference_three.id ]
3544
- )
3348
+ [ preference_two.id, preference_one.id, preference_three.id ]
3349
+ )
3545
3350
  end
3546
3351
  end
3547
3352
 
3548
- context "and the order is changed and an element is removed" do
3549
-
3353
+ context 'and the order is changed and an element is removed' do
3550
3354
  let(:preference_three) do
3551
- Preference.create(name: "three")
3355
+ Preference.create!(name: 'three')
3356
+ end
3357
+ let(:reloaded) do
3358
+ Person.find(person.id)
3552
3359
  end
3553
3360
 
3554
3361
  before do
3555
3362
  person.preference_ids =
3556
- [ preference_one.id, preference_two.id, preference_three.id ]
3557
- person.save
3363
+ [ preference_one.id, preference_two.id, preference_three.id ]
3364
+ person.save!
3558
3365
  person.preference_ids =
3559
- [ preference_three.id, preference_two.id ]
3560
- person.save
3561
- end
3562
-
3563
- let(:reloaded) do
3564
- Person.find(person.id)
3366
+ [ preference_three.id, preference_two.id ]
3367
+ person.save!
3565
3368
  end
3566
3369
 
3567
- it "also persists the change in id order" do
3370
+ it 'also persists the change in id order' do
3568
3371
  expect(reloaded.preference_ids).to eq(
3569
- [ preference_three.id, preference_two.id ]
3570
- )
3372
+ [ preference_three.id, preference_two.id ]
3373
+ )
3571
3374
  end
3572
3375
  end
3573
3376
  end
3574
3377
 
3575
- context "when adding a document" do
3576
-
3378
+ context 'when adding a document' do
3577
3379
  let(:person) do
3578
3380
  Person.new
3579
3381
  end
@@ -3586,8 +3388,7 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3586
3388
  person.preferences.push(preference_one)
3587
3389
  end
3588
3390
 
3589
- context "when chaining a second add" do
3590
-
3391
+ context 'when chaining a second add' do
3591
3392
  let(:preference_two) do
3592
3393
  Preference.new
3593
3394
  end
@@ -3596,96 +3397,114 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3596
3397
  first_add.push(preference_two)
3597
3398
  end
3598
3399
 
3599
- it "adds both documents" do
3400
+ it 'adds both documents' do
3600
3401
  expect(result).to eq([ preference_one, preference_two ])
3601
3402
  end
3602
3403
  end
3603
3404
  end
3604
3405
 
3605
- context "when setting the relation via the foreign key" do
3606
-
3607
- context "when the relation exists" do
3608
-
3406
+ context 'when setting the relation via the foreign key' do
3407
+ context 'when the relation exists' do
3609
3408
  let!(:person) do
3610
- Person.create
3409
+ Person.create!
3611
3410
  end
3612
3411
 
3613
3412
  let!(:pref_one) do
3614
- person.preferences.create
3413
+ person.preferences.create!
3615
3414
  end
3616
3415
 
3617
3416
  let!(:pref_two) do
3618
- Preference.create
3417
+ Preference.create!
3619
3418
  end
3620
3419
 
3621
3420
  before do
3622
3421
  person.preference_ids = [ pref_two.id ]
3623
3422
  end
3624
3423
 
3625
- it "sets the new documents on the relation" do
3424
+ it 'sets the new documents on the relation' do
3626
3425
  expect(person.preferences).to eq([ pref_two ])
3627
3426
  end
3628
3427
  end
3629
3428
  end
3630
3429
 
3631
- context "when using a different primary key" do
3632
-
3430
+ context 'when using a different primary key' do
3633
3431
  let(:dog) do
3634
- Dog.create(name: 'Doggie')
3432
+ Dog.create!(name: 'Doggie')
3635
3433
  end
3636
3434
 
3637
3435
  let(:cat) do
3638
- Cat.create(name: 'Kitty')
3436
+ Cat.create!(name: 'Kitty')
3639
3437
  end
3640
3438
 
3641
- let(:fire_hydrant) do
3642
- FireHydrant.create(location: '221B Baker Street')
3439
+ let(:another_cat) do
3440
+ Cat.create!(name: 'Kitten')
3643
3441
  end
3644
3442
 
3645
- context "when adding to a one-way many to many" do
3443
+ let(:fire_hydrant) do
3444
+ FireHydrant.create!(location: '221B Baker Street')
3445
+ end
3646
3446
 
3447
+ context 'when adding to a one-way many to many' do
3647
3448
  before do
3648
3449
  fire_hydrant.cats.push(cat)
3649
3450
  end
3650
3451
 
3651
- it "adds the pk value to the fk set" do
3652
- expect(fire_hydrant.cat_ids).to eq([cat.name])
3452
+ it 'adds the pk value to the fk set' do
3453
+ expect(fire_hydrant.cat_ids).to eq([ cat.name ])
3454
+ end
3455
+ end
3456
+
3457
+ context 'when adding multiple documents to a one-way many to many' do
3458
+ before do
3459
+ fire_hydrant.cats.push([ cat, another_cat ])
3460
+ end
3461
+
3462
+ it 'adds the pk values to the fk set' do
3463
+ expect(fire_hydrant.cat_ids).to eq([ cat.name, another_cat.name ])
3653
3464
  end
3654
3465
  end
3655
3466
 
3656
- context "when adding to a two-way many to many" do
3467
+ context 'when adding a new document to a one-way many to many' do
3468
+ before do
3469
+ fire_hydrant.cats.build name: 'Kitten'
3470
+ end
3471
+
3472
+ it 'adds the pk values to the fk set' do
3473
+ expect(fire_hydrant.cat_ids).to eq([ 'Kitten' ])
3474
+ end
3475
+ end
3657
3476
 
3477
+ context 'when adding to a two-way many to many' do
3658
3478
  before do
3659
3479
  fire_hydrant.dogs.push(dog)
3660
3480
  end
3661
3481
 
3662
- it "adds the pk value to the fk set" do
3663
- expect(fire_hydrant.dog_ids).to eq([dog.name])
3482
+ it 'adds the pk value to the fk set' do
3483
+ expect(fire_hydrant.dog_ids).to eq([ dog.name ])
3664
3484
  end
3665
3485
 
3666
- it "adds the base pk value to the inverse fk set" do
3667
- expect(dog.fire_hydrant_ids).to eq([fire_hydrant.location])
3486
+ it 'adds the base pk value to the inverse fk set' do
3487
+ expect(dog.fire_hydrant_ids).to eq([ fire_hydrant.location ])
3668
3488
  end
3669
3489
  end
3670
3490
 
3671
- context "when deleting from a two-way many to many" do
3672
-
3491
+ context 'when deleting from a two-way many to many' do
3673
3492
  before do
3674
3493
  dog.fire_hydrants.push(fire_hydrant)
3675
3494
  fire_hydrant.dogs.delete(dog)
3676
3495
  end
3677
3496
 
3678
- it "removes the pk value from the fk set" do
3497
+ it 'removes the pk value from the fk set' do
3679
3498
  expect(fire_hydrant.dog_ids).to eq([])
3680
3499
  end
3681
3500
 
3682
- it "removes the base pk value from the inverse fk set" do
3501
+ it 'removes the base pk value from the inverse fk set' do
3683
3502
  expect(dog.fire_hydrant_ids).to eq([])
3684
3503
  end
3685
3504
  end
3686
3505
  end
3687
3506
 
3688
- context "HABTM" do
3507
+ context 'HABTM' do
3689
3508
  before do
3690
3509
  class Project
3691
3510
  include Mongoid::Document
@@ -3708,18 +3527,67 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3708
3527
  end
3709
3528
  end
3710
3529
 
3711
- it "should assign relation from both sides" do
3712
- p1 = Project.create name: 'Foo'
3713
- p2 = Project.create name: 'Bar'
3714
- d1 = Distributor.create name: 'Rock'
3715
- d2 = Distributor.create name: 'Soul'
3530
+ it 'assigns relation from both sides' do
3531
+ p1 = Project.create! name: 'Foo'
3532
+ p2 = Project.create! name: 'Bar'
3533
+ d1 = Distributor.create! name: 'Rock'
3534
+ d2 = Distributor.create! name: 'Soul'
3716
3535
 
3717
3536
  p1.distributors << d1
3718
- expect(p1.d_ids).to match_array([d1.id])
3719
- expect(d1.p_ids).to match_array([p1.id])
3537
+ expect(p1.d_ids).to contain_exactly(d1.id)
3538
+ expect(d1.p_ids).to contain_exactly(p1.id)
3720
3539
  d2.projects << p2
3721
- expect(d2.p_ids).to match_array([p2.id])
3722
- expect(p2.d_ids).to match_array([d2.id])
3540
+ expect(d2.p_ids).to contain_exactly(p2.id)
3541
+ expect(p2.d_ids).to contain_exactly(d2.id)
3542
+ end
3543
+ end
3544
+
3545
+ context "when accesing own _id from parent's foreign key in default" do
3546
+ let!(:contract) { HabtmmContract.create! }
3547
+ let!(:signature) { contract.signatures.create! }
3548
+
3549
+ it 'is nil' do
3550
+ expect(signature.favorite_signature).to be_nil
3551
+ end
3552
+ end
3553
+
3554
+ context 'when setting an association on a model that uses the class_name option' do
3555
+ let!(:contract) { HabtmmContract.create! }
3556
+ let!(:signature) { HabtmmSignature.create!(contracts: [ contract ]) }
3557
+
3558
+ it 'populates the inverse foreign key' do
3559
+ expect(signature.contracts.first.signature_ids).to eq([ signature.id ])
3560
+ end
3561
+ end
3562
+
3563
+ context 'when there is a foreign key in the aliased associations' do
3564
+ it 'has the correct aliases' do
3565
+ expect(Dog.aliased_associations['breed_ids']).to eq('breeds')
3566
+ expect(Breed.aliased_associations['dog_ids']).to eq('dogs')
3567
+ end
3568
+ end
3569
+
3570
+ # This test is for MONGOID-5344 which tests that the initial call to
3571
+ # signature_ids refers to the same array as subsequent calls to signature_ids.
3572
+ # Prior to the change in that ticket, this test broke because the array
3573
+ # returned from write_attribute (which is triggered the first time the
3574
+ # foreign key array is referenced, to set the default), refers to a different
3575
+ # array to the one stored in the attributes hash. This happened because,
3576
+ # when retrieving a document from the database, the attributes hash is actually
3577
+ # a BSON::Document, which applies a transformation to the array before
3578
+ # storing it.
3579
+ context 'when executing concat on foreign key array from the db' do
3580
+ before do
3581
+ HabtmmContract.create!
3582
+ HabtmmSignature.create!
3583
+ contract.signature_ids.concat([ signature.id ])
3584
+ end
3585
+
3586
+ let!(:contract) { HabtmmContract.first }
3587
+ let!(:signature) { HabtmmSignature.first }
3588
+
3589
+ it 'works on the first attempt' do
3590
+ expect(contract.signature_ids).to eq([ signature.id ])
3723
3591
  end
3724
3592
  end
3725
3593
  end