mongoid 9.0.12 → 9.1.0

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