mongoid 9.0.11 → 9.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (997) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -8
  3. data/README.md +5 -11
  4. data/Rakefile +35 -38
  5. data/lib/config/locales/en.yml +11 -0
  6. data/lib/mongoid/association/accessors.rb +19 -26
  7. data/lib/mongoid/association/bindable.rb +43 -45
  8. data/lib/mongoid/association/builders.rb +18 -15
  9. data/lib/mongoid/association/constrainable.rb +3 -5
  10. data/lib/mongoid/association/depending.rb +34 -38
  11. data/lib/mongoid/association/eager.rb +47 -13
  12. data/lib/mongoid/association/eager_loadable.rb +152 -24
  13. data/lib/mongoid/association/embedded/batchable.rb +28 -30
  14. data/lib/mongoid/association/embedded/cyclic.rb +8 -11
  15. data/lib/mongoid/association/embedded/eager.rb +1 -6
  16. data/lib/mongoid/association/embedded/embedded_in/binding.rb +6 -9
  17. data/lib/mongoid/association/embedded/embedded_in/buildable.rb +2 -3
  18. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +2 -0
  19. data/lib/mongoid/association/embedded/embedded_in.rb +25 -22
  20. data/lib/mongoid/association/embedded/embeds_many/binding.rb +0 -2
  21. data/lib/mongoid/association/embedded/embeds_many/buildable.rb +2 -3
  22. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +1 -1
  23. data/lib/mongoid/association/embedded/embeds_many.rb +24 -21
  24. data/lib/mongoid/association/embedded/embeds_one/binding.rb +0 -2
  25. data/lib/mongoid/association/embedded/embeds_one/buildable.rb +2 -6
  26. data/lib/mongoid/association/embedded/embeds_one.rb +20 -18
  27. data/lib/mongoid/association/embedded.rb +0 -1
  28. data/lib/mongoid/association/macros.rb +3 -7
  29. data/lib/mongoid/association/many.rb +75 -3
  30. data/lib/mongoid/association/marshalable.rb +0 -3
  31. data/lib/mongoid/association/nested/many.rb +18 -13
  32. data/lib/mongoid/association/nested/nested_buildable.rb +6 -9
  33. data/lib/mongoid/association/nested/one.rb +4 -7
  34. data/lib/mongoid/association/nested.rb +1 -3
  35. data/lib/mongoid/association/one.rb +0 -3
  36. data/lib/mongoid/association/options.rb +8 -7
  37. data/lib/mongoid/association/referenced/auto_save.rb +0 -2
  38. data/lib/mongoid/association/referenced/belongs_to/binding.rb +11 -15
  39. data/lib/mongoid/association/referenced/belongs_to/buildable.rb +23 -5
  40. data/lib/mongoid/association/referenced/belongs_to/eager.rb +1 -3
  41. data/lib/mongoid/association/referenced/belongs_to.rb +48 -43
  42. data/lib/mongoid/association/referenced/counter_cache.rb +5 -10
  43. data/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb +0 -2
  44. data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +8 -4
  45. data/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb +0 -3
  46. data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +52 -47
  47. data/lib/mongoid/association/referenced/has_many/binding.rb +0 -2
  48. data/lib/mongoid/association/referenced/has_many/buildable.rb +11 -5
  49. data/lib/mongoid/association/referenced/has_many/eager.rb +0 -3
  50. data/lib/mongoid/association/referenced/has_many/enumerable.rb +107 -38
  51. data/lib/mongoid/association/referenced/has_many/proxy.rb +10 -6
  52. data/lib/mongoid/association/referenced/has_many.rb +30 -24
  53. data/lib/mongoid/association/referenced/has_one/binding.rb +0 -2
  54. data/lib/mongoid/association/referenced/has_one/buildable.rb +11 -9
  55. data/lib/mongoid/association/referenced/has_one/eager.rb +0 -3
  56. data/lib/mongoid/association/referenced/has_one.rb +26 -21
  57. data/lib/mongoid/association/referenced/syncable.rb +30 -34
  58. data/lib/mongoid/association/referenced.rb +0 -1
  59. data/lib/mongoid/association/reflections.rb +0 -3
  60. data/lib/mongoid/association/relatable.rb +69 -66
  61. data/lib/mongoid/association.rb +12 -15
  62. data/lib/mongoid/atomic/modifiers.rb +65 -25
  63. data/lib/mongoid/atomic/paths/embedded/many.rb +4 -7
  64. data/lib/mongoid/atomic/paths/embedded/one.rb +2 -4
  65. data/lib/mongoid/atomic/paths/embedded.rb +3 -6
  66. data/lib/mongoid/atomic/paths/root.rb +1 -4
  67. data/lib/mongoid/atomic/paths.rb +2 -3
  68. data/lib/mongoid/atomic.rb +0 -3
  69. data/lib/mongoid/attributes/dynamic.rb +6 -7
  70. data/lib/mongoid/attributes/nested.rb +8 -10
  71. data/lib/mongoid/attributes/projector.rb +7 -20
  72. data/lib/mongoid/attributes/readonly.rb +6 -11
  73. data/lib/mongoid/attributes.rb +18 -34
  74. data/lib/mongoid/cacheable.rb +24 -6
  75. data/lib/mongoid/clients/factory.rb +12 -15
  76. data/lib/mongoid/clients/options.rb +2 -5
  77. data/lib/mongoid/clients/sessions.rb +50 -60
  78. data/lib/mongoid/clients/storage_options.rb +1 -4
  79. data/lib/mongoid/clients/validators/storage.rb +2 -3
  80. data/lib/mongoid/clients/validators.rb +1 -2
  81. data/lib/mongoid/clients.rb +8 -11
  82. data/lib/mongoid/collection_configurable.rb +10 -14
  83. data/lib/mongoid/composable.rb +30 -34
  84. data/lib/mongoid/config/defaults.rb +7 -4
  85. data/lib/mongoid/config/encryption.rb +1 -3
  86. data/lib/mongoid/config/environment.rb +7 -16
  87. data/lib/mongoid/config/introspection.rb +14 -16
  88. data/lib/mongoid/config/options.rb +16 -10
  89. data/lib/mongoid/config/validators/async_query_executor.rb +3 -6
  90. data/lib/mongoid/config/validators/client.rb +12 -15
  91. data/lib/mongoid/config/validators/option.rb +3 -5
  92. data/lib/mongoid/config/validators.rb +3 -4
  93. data/lib/mongoid/config.rb +132 -42
  94. data/lib/mongoid/contextual/aggregable/memory.rb +2 -4
  95. data/lib/mongoid/contextual/aggregable/mongo.rb +20 -22
  96. data/lib/mongoid/contextual/aggregable/none.rb +2 -4
  97. data/lib/mongoid/contextual/aggregable.rb +5 -7
  98. data/lib/mongoid/contextual/atomic.rb +20 -24
  99. data/lib/mongoid/contextual/command.rb +0 -3
  100. data/lib/mongoid/contextual/map_reduce.rb +12 -16
  101. data/lib/mongoid/contextual/memory.rb +72 -65
  102. data/lib/mongoid/contextual/mongo/documents_loader.rb +2 -3
  103. data/lib/mongoid/contextual/mongo.rb +169 -85
  104. data/lib/mongoid/contextual/none.rb +9 -9
  105. data/lib/mongoid/contextual/queryable.rb +1 -4
  106. data/lib/mongoid/contextual.rb +18 -7
  107. data/lib/mongoid/copyable.rb +7 -9
  108. data/lib/mongoid/criteria/findable.rb +5 -7
  109. data/lib/mongoid/criteria/includable.rb +37 -8
  110. data/lib/mongoid/criteria/inspectable.rb +2 -5
  111. data/lib/mongoid/criteria/marshalable.rb +4 -8
  112. data/lib/mongoid/criteria/modifiable.rb +3 -11
  113. data/lib/mongoid/criteria/options.rb +0 -3
  114. data/lib/mongoid/criteria/permission.rb +34 -37
  115. data/lib/mongoid/criteria/queryable/aggregable.rb +9 -10
  116. data/lib/mongoid/criteria/queryable/expandable.rb +4 -9
  117. data/lib/mongoid/criteria/queryable/extensions/array.rb +7 -10
  118. data/lib/mongoid/criteria/queryable/extensions/big_decimal.rb +3 -5
  119. data/lib/mongoid/criteria/queryable/extensions/boolean.rb +1 -4
  120. data/lib/mongoid/criteria/queryable/extensions/date.rb +2 -6
  121. data/lib/mongoid/criteria/queryable/extensions/date_time.rb +3 -7
  122. data/lib/mongoid/criteria/queryable/extensions/hash.rb +1 -4
  123. data/lib/mongoid/criteria/queryable/extensions/nil_class.rb +20 -11
  124. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +15 -11
  125. data/lib/mongoid/criteria/queryable/extensions/object.rb +7 -8
  126. data/lib/mongoid/criteria/queryable/extensions/range.rb +6 -9
  127. data/lib/mongoid/criteria/queryable/extensions/regexp.rb +10 -12
  128. data/lib/mongoid/criteria/queryable/extensions/set.rb +4 -6
  129. data/lib/mongoid/criteria/queryable/extensions/string.rb +5 -9
  130. data/lib/mongoid/criteria/queryable/extensions/symbol.rb +3 -7
  131. data/lib/mongoid/criteria/queryable/extensions/time.rb +2 -6
  132. data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +2 -12
  133. data/lib/mongoid/criteria/queryable/extensions.rb +18 -21
  134. data/lib/mongoid/criteria/queryable/key.rb +12 -18
  135. data/lib/mongoid/criteria/queryable/macroable.rb +0 -3
  136. data/lib/mongoid/criteria/queryable/mergeable.rb +31 -39
  137. data/lib/mongoid/criteria/queryable/optional.rb +17 -18
  138. data/lib/mongoid/criteria/queryable/options.rb +20 -9
  139. data/lib/mongoid/criteria/queryable/pipeline.rb +6 -9
  140. data/lib/mongoid/criteria/queryable/selectable.rb +131 -193
  141. data/lib/mongoid/criteria/queryable/selector.rb +20 -29
  142. data/lib/mongoid/criteria/queryable/smash.rb +1 -6
  143. data/lib/mongoid/criteria/queryable/storable.rb +20 -39
  144. data/lib/mongoid/criteria/queryable.rb +25 -15
  145. data/lib/mongoid/criteria/scopable.rb +5 -11
  146. data/lib/mongoid/criteria/translator.rb +5 -8
  147. data/lib/mongoid/criteria.rb +109 -54
  148. data/lib/mongoid/deprecable.rb +1 -4
  149. data/lib/mongoid/deprecation.rb +2 -6
  150. data/lib/mongoid/document.rb +1 -1
  151. data/lib/mongoid/encryptable.rb +1 -2
  152. data/lib/mongoid/equality.rb +4 -5
  153. data/lib/mongoid/errors/ambiguous_relationship.rb +2 -5
  154. data/lib/mongoid/errors/attribute_not_loaded.rb +1 -4
  155. data/lib/mongoid/errors/callback.rb +1 -4
  156. data/lib/mongoid/errors/create_collection_failure.rb +8 -11
  157. data/lib/mongoid/errors/criteria_argument_required.rb +2 -5
  158. data/lib/mongoid/errors/delete_restriction.rb +1 -4
  159. data/lib/mongoid/errors/document_not_destroyed.rb +1 -4
  160. data/lib/mongoid/errors/document_not_found.rb +16 -21
  161. data/lib/mongoid/errors/drop_collection_failure.rb +7 -10
  162. data/lib/mongoid/errors/empty_config_file.rb +1 -4
  163. data/lib/mongoid/errors/immutable_attribute.rb +1 -4
  164. data/lib/mongoid/errors/in_memory_collation_not_supported.rb +1 -4
  165. data/lib/mongoid/errors/invalid_async_query_executor.rb +2 -5
  166. data/lib/mongoid/errors/invalid_auto_encryption_configuration.rb +2 -5
  167. data/lib/mongoid/errors/invalid_collection.rb +1 -3
  168. data/lib/mongoid/errors/invalid_config_file.rb +1 -4
  169. data/lib/mongoid/errors/invalid_config_option.rb +2 -5
  170. data/lib/mongoid/errors/invalid_dependent_strategy.rb +6 -10
  171. data/lib/mongoid/errors/invalid_discriminator_key_target.rb +2 -4
  172. data/lib/mongoid/errors/invalid_dot_dollar_assignment.rb +1 -4
  173. data/lib/mongoid/errors/invalid_elem_match_operator.rb +5 -9
  174. data/lib/mongoid/errors/invalid_estimated_count_criteria.rb +1 -4
  175. data/lib/mongoid/errors/invalid_estimated_count_scoping.rb +1 -4
  176. data/lib/mongoid/errors/invalid_expression_operator.rb +3 -7
  177. data/lib/mongoid/errors/invalid_field.rb +2 -5
  178. data/lib/mongoid/errors/invalid_field_operator.rb +5 -9
  179. data/lib/mongoid/errors/invalid_field_option.rb +2 -5
  180. data/lib/mongoid/errors/invalid_field_type.rb +1 -4
  181. data/lib/mongoid/errors/invalid_find.rb +1 -4
  182. data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +1 -4
  183. data/lib/mongoid/errors/invalid_includes.rb +3 -6
  184. data/lib/mongoid/errors/invalid_index.rb +1 -4
  185. data/lib/mongoid/errors/invalid_options.rb +1 -4
  186. data/lib/mongoid/errors/invalid_path.rb +1 -4
  187. data/lib/mongoid/errors/invalid_persistence_option.rb +4 -7
  188. data/lib/mongoid/errors/invalid_query.rb +6 -11
  189. data/lib/mongoid/errors/invalid_relation.rb +10 -13
  190. data/lib/mongoid/errors/invalid_relation_option.rb +4 -7
  191. data/lib/mongoid/errors/invalid_scope.rb +1 -4
  192. data/lib/mongoid/errors/invalid_session_nesting.rb +0 -3
  193. data/lib/mongoid/errors/invalid_set_polymorphic_relation.rb +2 -4
  194. data/lib/mongoid/errors/invalid_storage_options.rb +1 -4
  195. data/lib/mongoid/errors/invalid_time.rb +1 -4
  196. data/lib/mongoid/errors/invalid_transaction_nesting.rb +0 -3
  197. data/lib/mongoid/errors/inverse_not_found.rb +1 -4
  198. data/lib/mongoid/errors/mixed_client_configuration.rb +1 -4
  199. data/lib/mongoid/errors/mixed_relations.rb +1 -3
  200. data/lib/mongoid/errors/mongoid_error.rb +7 -11
  201. data/lib/mongoid/errors/nested_attributes_metadata_not_found.rb +1 -4
  202. data/lib/mongoid/errors/no_client_config.rb +1 -4
  203. data/lib/mongoid/errors/no_client_database.rb +1 -4
  204. data/lib/mongoid/errors/no_client_hosts.rb +1 -4
  205. data/lib/mongoid/errors/no_clients_config.rb +1 -4
  206. data/lib/mongoid/errors/no_default_client.rb +1 -4
  207. data/lib/mongoid/errors/no_environment.rb +1 -4
  208. data/lib/mongoid/errors/no_map_reduce_output.rb +1 -4
  209. data/lib/mongoid/errors/no_metadata.rb +1 -4
  210. data/lib/mongoid/errors/no_parent.rb +1 -4
  211. data/lib/mongoid/errors/readonly_attribute.rb +1 -4
  212. data/lib/mongoid/errors/readonly_document.rb +1 -4
  213. data/lib/mongoid/errors/rollback.rb +0 -2
  214. data/lib/mongoid/errors/scope_overwrite.rb +2 -4
  215. data/lib/mongoid/errors/sessions_not_supported.rb +0 -3
  216. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +1 -3
  217. data/lib/mongoid/errors/transaction_error.rb +0 -3
  218. data/lib/mongoid/errors/transactions_not_supported.rb +0 -3
  219. data/lib/mongoid/errors/unknown_attribute.rb +1 -4
  220. data/lib/mongoid/errors/unknown_model.rb +1 -4
  221. data/lib/mongoid/errors/unregistered_class.rb +1 -1
  222. data/lib/mongoid/errors/unsaved_document.rb +1 -3
  223. data/lib/mongoid/errors/unsupported_isolation_level.rb +22 -0
  224. data/lib/mongoid/errors/unsupported_javascript.rb +1 -4
  225. data/lib/mongoid/errors/validations.rb +3 -5
  226. data/lib/mongoid/errors.rb +72 -72
  227. data/lib/mongoid/evolvable.rb +0 -3
  228. data/lib/mongoid/extensions/array.rb +5 -7
  229. data/lib/mongoid/extensions/big_decimal.rb +6 -7
  230. data/lib/mongoid/extensions/binary.rb +3 -6
  231. data/lib/mongoid/extensions/boolean.rb +4 -7
  232. data/lib/mongoid/extensions/bson_document.rb +28 -0
  233. data/lib/mongoid/extensions/date.rb +10 -14
  234. data/lib/mongoid/extensions/date_time.rb +2 -6
  235. data/lib/mongoid/extensions/decimal128.rb +1 -5
  236. data/lib/mongoid/extensions/false_class.rb +3 -5
  237. data/lib/mongoid/extensions/float.rb +5 -10
  238. data/lib/mongoid/extensions/hash.rb +5 -10
  239. data/lib/mongoid/extensions/integer.rb +5 -10
  240. data/lib/mongoid/extensions/module.rb +1 -4
  241. data/lib/mongoid/extensions/nil_class.rb +0 -1
  242. data/lib/mongoid/extensions/object.rb +9 -14
  243. data/lib/mongoid/extensions/object_id.rb +2 -6
  244. data/lib/mongoid/extensions/range.rb +14 -15
  245. data/lib/mongoid/extensions/raw_value.rb +0 -3
  246. data/lib/mongoid/extensions/regexp.rb +3 -6
  247. data/lib/mongoid/extensions/set.rb +3 -4
  248. data/lib/mongoid/extensions/string.rb +8 -12
  249. data/lib/mongoid/extensions/symbol.rb +3 -7
  250. data/lib/mongoid/extensions/time.rb +24 -26
  251. data/lib/mongoid/extensions/time_with_zone.rb +4 -7
  252. data/lib/mongoid/extensions/true_class.rb +3 -5
  253. data/lib/mongoid/extensions.rb +26 -26
  254. data/lib/mongoid/factory.rb +5 -9
  255. data/lib/mongoid/fields/encrypted.rb +0 -1
  256. data/lib/mongoid/fields/foreign_key.rb +9 -10
  257. data/lib/mongoid/fields/localized.rb +12 -12
  258. data/lib/mongoid/fields/standard.rb +5 -7
  259. data/lib/mongoid/fields/validators/macro.rb +23 -34
  260. data/lib/mongoid/fields/validators.rb +1 -2
  261. data/lib/mongoid/fields.rb +151 -58
  262. data/lib/mongoid/findable.rb +82 -53
  263. data/lib/mongoid/indexable/specification.rb +20 -6
  264. data/lib/mongoid/indexable/validators/options.rb +33 -40
  265. data/lib/mongoid/indexable.rb +26 -25
  266. data/lib/mongoid/inspectable.rb +4 -7
  267. data/lib/mongoid/interceptable.rb +41 -40
  268. data/lib/mongoid/loadable.rb +17 -18
  269. data/lib/mongoid/loggable.rb +4 -8
  270. data/lib/mongoid/matchable.rb +0 -2
  271. data/lib/mongoid/matcher/all.rb +2 -5
  272. data/lib/mongoid/matcher/and.rb +0 -3
  273. data/lib/mongoid/matcher/bits.rb +13 -8
  274. data/lib/mongoid/matcher/bits_all_clear.rb +1 -3
  275. data/lib/mongoid/matcher/bits_all_set.rb +1 -3
  276. data/lib/mongoid/matcher/bits_any_clear.rb +1 -3
  277. data/lib/mongoid/matcher/bits_any_set.rb +1 -3
  278. data/lib/mongoid/matcher/elem_match.rb +13 -15
  279. data/lib/mongoid/matcher/elem_match_expression.rb +0 -3
  280. data/lib/mongoid/matcher/eq.rb +0 -3
  281. data/lib/mongoid/matcher/eq_impl.rb +12 -17
  282. data/lib/mongoid/matcher/eq_impl_with_regexp.rb +4 -7
  283. data/lib/mongoid/matcher/exists.rb +1 -4
  284. data/lib/mongoid/matcher/expression.rb +5 -10
  285. data/lib/mongoid/matcher/expression_operator.rb +1 -3
  286. data/lib/mongoid/matcher/field_expression.rb +10 -13
  287. data/lib/mongoid/matcher/field_operator.rb +4 -6
  288. data/lib/mongoid/matcher/gt.rb +0 -3
  289. data/lib/mongoid/matcher/gte.rb +0 -3
  290. data/lib/mongoid/matcher/in.rb +9 -12
  291. data/lib/mongoid/matcher/lt.rb +0 -3
  292. data/lib/mongoid/matcher/lte.rb +0 -3
  293. data/lib/mongoid/matcher/mod.rb +4 -8
  294. data/lib/mongoid/matcher/ne.rb +0 -3
  295. data/lib/mongoid/matcher/nin.rb +0 -3
  296. data/lib/mongoid/matcher/nor.rb +1 -6
  297. data/lib/mongoid/matcher/not.rb +0 -3
  298. data/lib/mongoid/matcher/or.rb +0 -3
  299. data/lib/mongoid/matcher/regex.rb +11 -14
  300. data/lib/mongoid/matcher/size.rb +2 -5
  301. data/lib/mongoid/matcher/type.rb +32 -37
  302. data/lib/mongoid/matcher.rb +7 -15
  303. data/lib/mongoid/model_resolver.rb +1 -0
  304. data/lib/mongoid/persistable/creatable.rb +23 -7
  305. data/lib/mongoid/persistable/deletable.rb +6 -7
  306. data/lib/mongoid/persistable/destroyable.rb +2 -6
  307. data/lib/mongoid/persistable/incrementable.rb +1 -3
  308. data/lib/mongoid/persistable/logical.rb +3 -5
  309. data/lib/mongoid/persistable/maxable.rb +2 -4
  310. data/lib/mongoid/persistable/minable.rb +2 -4
  311. data/lib/mongoid/persistable/multipliable.rb +1 -3
  312. data/lib/mongoid/persistable/poppable.rb +2 -4
  313. data/lib/mongoid/persistable/pullable.rb +3 -5
  314. data/lib/mongoid/persistable/pushable.rb +5 -7
  315. data/lib/mongoid/persistable/renamable.rb +1 -3
  316. data/lib/mongoid/persistable/savable.rb +0 -3
  317. data/lib/mongoid/persistable/settable.rb +4 -13
  318. data/lib/mongoid/persistable/unsettable.rb +1 -3
  319. data/lib/mongoid/persistable/updatable.rb +16 -24
  320. data/lib/mongoid/persistable/upsertable.rb +8 -10
  321. data/lib/mongoid/persistable.rb +28 -27
  322. data/lib/mongoid/persistence_context.rb +34 -41
  323. data/lib/mongoid/pluckable.rb +0 -2
  324. data/lib/mongoid/positional.rb +5 -11
  325. data/lib/mongoid/railtie.rb +16 -22
  326. data/lib/mongoid/railties/console_sandbox.rb +6 -8
  327. data/lib/mongoid/railties/controller_runtime.rb +7 -11
  328. data/lib/mongoid/railties/database.rake +42 -44
  329. data/lib/mongoid/scopable.rb +23 -31
  330. data/lib/mongoid/search_indexable.rb +330 -0
  331. data/lib/mongoid/selectable.rb +1 -3
  332. data/lib/mongoid/serializable.rb +6 -8
  333. data/lib/mongoid/shardable.rb +8 -17
  334. data/lib/mongoid/stateful.rb +5 -10
  335. data/lib/mongoid/stringified_symbol.rb +5 -9
  336. data/lib/mongoid/tasks/database.rake +31 -32
  337. data/lib/mongoid/tasks/database.rb +28 -30
  338. data/lib/mongoid/tasks/encryption.rb +10 -15
  339. data/lib/mongoid/threaded/lifecycle.rb +6 -9
  340. data/lib/mongoid/threaded.rb +132 -32
  341. data/lib/mongoid/timestamps/created/short.rb +2 -3
  342. data/lib/mongoid/timestamps/created.rb +1 -2
  343. data/lib/mongoid/timestamps/short.rb +0 -1
  344. data/lib/mongoid/timestamps/timeless.rb +8 -13
  345. data/lib/mongoid/timestamps/updated/short.rb +2 -3
  346. data/lib/mongoid/timestamps/updated.rb +2 -5
  347. data/lib/mongoid/timestamps.rb +4 -6
  348. data/lib/mongoid/touchable.rb +19 -11
  349. data/lib/mongoid/traversable.rb +3 -2
  350. data/lib/mongoid/validatable/associated.rb +1 -5
  351. data/lib/mongoid/validatable/format.rb +0 -2
  352. data/lib/mongoid/validatable/length.rb +0 -2
  353. data/lib/mongoid/validatable/localizable.rb +0 -3
  354. data/lib/mongoid/validatable/macros.rb +0 -2
  355. data/lib/mongoid/validatable/numericality.rb +24 -3
  356. data/lib/mongoid/validatable/presence.rb +8 -10
  357. data/lib/mongoid/validatable/queryable.rb +0 -3
  358. data/lib/mongoid/validatable/uniqueness.rb +12 -14
  359. data/lib/mongoid/validatable.rb +15 -20
  360. data/lib/mongoid/version.rb +1 -1
  361. data/lib/mongoid/warnings.rb +10 -11
  362. data/lib/mongoid.rb +30 -34
  363. data/lib/rails/generators/mongoid/config/config_generator.rb +2 -4
  364. data/lib/rails/generators/mongoid/config/templates/mongoid.rb +4 -5
  365. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +2 -2
  366. data/lib/rails/generators/mongoid/model/model_generator.rb +5 -8
  367. data/lib/rails/generators/mongoid_generator.rb +6 -10
  368. data/lib/rails/mongoid.rb +1 -3
  369. data/spec/integration/active_job_spec.rb +1 -2
  370. data/spec/integration/app_spec.rb +142 -178
  371. data/spec/integration/associations/belongs_to_spec.rb +30 -28
  372. data/spec/integration/associations/embedded_dirty_spec.rb +0 -1
  373. data/spec/integration/associations/embedded_spec.rb +41 -43
  374. data/spec/integration/associations/embeds_many_spec.rb +46 -29
  375. data/spec/integration/associations/embeds_one_spec.rb +2 -1
  376. data/spec/integration/associations/foreign_key_spec.rb +10 -11
  377. data/spec/integration/associations/foreign_key_spec_models.rb +8 -9
  378. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +16 -13
  379. data/spec/integration/associations/has_many_spec.rb +17 -18
  380. data/spec/integration/associations/has_one_spec.rb +42 -45
  381. data/spec/integration/associations/nested_attributes_assignment_spec.rb +36 -39
  382. data/spec/integration/associations/reverse_population_spec.rb +4 -5
  383. data/spec/integration/associations/reverse_population_spec_models.rb +0 -1
  384. data/spec/integration/associations/scope_option_spec.rb +18 -20
  385. data/spec/integration/atomic/modifiers_spec.rb +72 -52
  386. data/spec/integration/bson_regexp_raw_spec.rb +2 -3
  387. data/spec/integration/caching_spec.rb +68 -0
  388. data/spec/integration/callbacks_models.rb +11 -9
  389. data/spec/integration/callbacks_spec.rb +78 -80
  390. data/spec/integration/contextual/empty_spec.rb +19 -20
  391. data/spec/integration/criteria/alias_query_spec.rb +12 -19
  392. data/spec/integration/criteria/date_field_spec.rb +1 -2
  393. data/spec/integration/criteria/default_scope_spec.rb +23 -18
  394. data/spec/integration/criteria/logical_spec.rb +18 -19
  395. data/spec/integration/criteria/range_spec.rb +77 -86
  396. data/spec/integration/criteria/raw_value_spec.rb +122 -109
  397. data/spec/integration/criteria/time_with_zone_spec.rb +33 -24
  398. data/spec/integration/discriminator_key_spec.rb +85 -85
  399. data/spec/integration/discriminator_value_spec.rb +70 -73
  400. data/spec/integration/document_spec.rb +9 -10
  401. data/spec/integration/dots_and_dollars_spec.rb +104 -119
  402. data/spec/integration/encryption_spec.rb +8 -11
  403. data/spec/integration/i18n_fallbacks_spec.rb +7 -8
  404. data/spec/integration/isolation_state_spec.rb +241 -0
  405. data/spec/integration/matcher_examples_spec.rb +145 -168
  406. data/spec/integration/matcher_operator_data/in.yml +6 -6
  407. data/spec/integration/matcher_operator_spec.rb +16 -26
  408. data/spec/integration/matcher_spec.rb +31 -32
  409. data/spec/integration/persistence/collection_options_spec.rb +0 -2
  410. data/spec/integration/persistence/range_field_spec.rb +24 -2
  411. data/spec/integration/server_query_spec.rb +43 -37
  412. data/spec/integration/shardable_spec.rb +10 -11
  413. data/spec/integration/stringified_symbol_field_spec.rb +28 -43
  414. data/spec/lite_spec_helper.rb +11 -12
  415. data/spec/mongoid/association/accessors_spec.rb +197 -285
  416. data/spec/mongoid/association/auto_save_spec.rb +62 -93
  417. data/spec/mongoid/association/builders_spec.rb +121 -74
  418. data/spec/mongoid/association/constrainable_spec.rb +9 -22
  419. data/spec/mongoid/association/counter_cache_spec.rb +71 -104
  420. data/spec/mongoid/association/depending_spec.rb +166 -208
  421. data/spec/mongoid/association/eager_spec.rb +416 -80
  422. data/spec/mongoid/association/embedded/cyclic_spec.rb +36 -48
  423. data/spec/mongoid/association/embedded/dirty_spec.rb +15 -20
  424. data/spec/mongoid/association/embedded/embedded_in/binding_spec.rb +36 -54
  425. data/spec/mongoid/association/embedded/embedded_in/buildable_spec.rb +8 -15
  426. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +135 -175
  427. data/spec/mongoid/association/embedded/embedded_in_spec.rb +25 -127
  428. data/spec/mongoid/association/embedded/embeds_many/binding_spec.rb +9 -14
  429. data/spec/mongoid/association/embedded/embeds_many/buildable_spec.rb +38 -51
  430. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +1151 -1340
  431. data/spec/mongoid/association/embedded/embeds_many_models.rb +17 -17
  432. data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +11 -11
  433. data/spec/mongoid/association/embedded/embeds_many_spec.rb +25 -138
  434. data/spec/mongoid/association/embedded/embeds_one/binding_spec.rb +15 -23
  435. data/spec/mongoid/association/embedded/embeds_one/buildable_spec.rb +18 -26
  436. data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +204 -273
  437. data/spec/mongoid/association/embedded/embeds_one_dnl_models.rb +0 -1
  438. data/spec/mongoid/association/embedded/embeds_one_models.rb +0 -1
  439. data/spec/mongoid/association/embedded/embeds_one_query_spec.rb +5 -6
  440. data/spec/mongoid/association/embedded/embeds_one_spec.rb +28 -143
  441. data/spec/mongoid/association/macros_spec.rb +213 -300
  442. data/spec/mongoid/association/nested/many_spec.rb +51 -73
  443. data/spec/mongoid/association/nested/one_spec.rb +56 -80
  444. data/spec/mongoid/association/options_spec.rb +187 -310
  445. data/spec/mongoid/association/polymorphic_spec.rb +35 -53
  446. data/spec/mongoid/association/referenced/belongs_to/binding_spec.rb +55 -76
  447. data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +36 -41
  448. data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +81 -101
  449. data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +258 -345
  450. data/spec/mongoid/association/referenced/belongs_to_models.rb +4 -3
  451. data/spec/mongoid/association/referenced/belongs_to_query_spec.rb +3 -4
  452. data/spec/mongoid/association/referenced/belongs_to_spec.rb +91 -294
  453. data/spec/mongoid/association/referenced/has_and_belongs_to_many/binding_spec.rb +42 -56
  454. data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +41 -32
  455. data/spec/mongoid/association/referenced/has_and_belongs_to_many/eager_spec.rb +36 -42
  456. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_persistence_spec.rb +8 -9
  457. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +771 -975
  458. data/spec/mongoid/association/referenced/has_and_belongs_to_many_models.rb +9 -9
  459. data/spec/mongoid/association/referenced/has_and_belongs_to_many_query_spec.rb +5 -6
  460. data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +59 -177
  461. data/spec/mongoid/association/referenced/has_many/binding_spec.rb +29 -43
  462. data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +53 -53
  463. data/spec/mongoid/association/referenced/has_many/eager_spec.rb +104 -67
  464. data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +431 -675
  465. data/spec/mongoid/association/referenced/has_many/proxy_query_spec.rb +1 -2
  466. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +173 -11
  467. data/spec/mongoid/association/referenced/has_many_models.rb +1 -2
  468. data/spec/mongoid/association/referenced/has_many_query_spec.rb +3 -4
  469. data/spec/mongoid/association/referenced/has_many_spec.rb +44 -188
  470. data/spec/mongoid/association/referenced/has_one/binding_spec.rb +26 -38
  471. data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +28 -29
  472. data/spec/mongoid/association/referenced/has_one/eager_spec.rb +29 -42
  473. data/spec/mongoid/association/referenced/has_one/proxy_spec.rb +270 -332
  474. data/spec/mongoid/association/referenced/has_one_models.rb +1 -2
  475. data/spec/mongoid/association/referenced/has_one_query_spec.rb +3 -4
  476. data/spec/mongoid/association/referenced/has_one_spec.rb +40 -188
  477. data/spec/mongoid/association/reflections_spec.rb +14 -24
  478. data/spec/mongoid/association/syncable_spec.rb +102 -125
  479. data/spec/mongoid/association_spec.rb +47 -70
  480. data/spec/mongoid/atomic/modifiers_spec.rb +184 -197
  481. data/spec/mongoid/atomic/paths/embedded/many_spec.rb +39 -52
  482. data/spec/mongoid/atomic/paths/embedded/one_spec.rb +35 -49
  483. data/spec/mongoid/atomic/paths/root_spec.rb +9 -15
  484. data/spec/mongoid/atomic/paths_spec.rb +81 -117
  485. data/spec/mongoid/atomic_spec.rb +156 -171
  486. data/spec/mongoid/attributes/dynamic_spec.rb +5 -6
  487. data/spec/mongoid/attributes/nested_spec.rb +1219 -1692
  488. data/spec/mongoid/attributes/nested_spec_models.rb +8 -9
  489. data/spec/mongoid/attributes/projector_spec.rb +2 -6
  490. data/spec/mongoid/attributes/readonly_spec.rb +73 -97
  491. data/spec/mongoid/attributes_spec.rb +669 -826
  492. data/spec/mongoid/cacheable_spec.rb +74 -44
  493. data/spec/mongoid/changeable_spec.rb +591 -733
  494. data/spec/mongoid/clients/factory_spec.rb +122 -154
  495. data/spec/mongoid/clients/options_spec.rb +40 -59
  496. data/spec/mongoid/clients/sessions_spec.rb +25 -44
  497. data/spec/mongoid/clients/transactions_spec.rb +33 -50
  498. data/spec/mongoid/clients/transactions_spec_models.rb +3 -4
  499. data/spec/mongoid/clients_spec.rb +265 -355
  500. data/spec/mongoid/collection_configurable_spec.rb +9 -12
  501. data/spec/mongoid/composable_spec.rb +2 -9
  502. data/spec/mongoid/config/defaults_spec.rb +24 -31
  503. data/spec/mongoid/config/encryption_spec.rb +54 -57
  504. data/spec/mongoid/config/environment_spec.rb +30 -40
  505. data/spec/mongoid/config/introspection_spec.rb +42 -37
  506. data/spec/mongoid/config/options_spec.rb +14 -21
  507. data/spec/mongoid/config_spec.rb +145 -195
  508. data/spec/mongoid/contextual/aggregable/memory_spec.rb +98 -135
  509. data/spec/mongoid/contextual/aggregable/memory_table_spec.rb +4 -7
  510. data/spec/mongoid/contextual/aggregable/mongo_spec.rb +149 -187
  511. data/spec/mongoid/contextual/aggregable/none_spec.rb +14 -16
  512. data/spec/mongoid/contextual/atomic_spec.rb +223 -271
  513. data/spec/mongoid/contextual/map_reduce_spec.rb +110 -138
  514. data/spec/mongoid/contextual/memory_spec.rb +706 -854
  515. data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +12 -13
  516. data/spec/mongoid/contextual/mongo_spec.rb +1008 -1213
  517. data/spec/mongoid/contextual/none_spec.rb +54 -59
  518. data/spec/mongoid/contextual/short_circuit_spec.rb +139 -0
  519. data/spec/mongoid/copyable_spec.rb +254 -300
  520. data/spec/mongoid/copyable_spec_models.rb +0 -1
  521. data/spec/mongoid/criteria/findable_spec.rb +322 -387
  522. data/spec/mongoid/criteria/includable_spec.rb +421 -372
  523. data/spec/mongoid/criteria/includable_spec_models.rb +16 -7
  524. data/spec/mongoid/criteria/inspectable_spec.rb +11 -14
  525. data/spec/mongoid/criteria/marshalable_spec.rb +11 -14
  526. data/spec/mongoid/criteria/modifiable_spec.rb +366 -512
  527. data/spec/mongoid/criteria/options_spec.rb +2 -7
  528. data/spec/mongoid/criteria/queryable/aggregable_spec.rb +112 -141
  529. data/spec/mongoid/criteria/queryable/expandable_spec.rb +8 -11
  530. data/spec/mongoid/criteria/queryable/extensions/array_spec.rb +127 -184
  531. data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +123 -60
  532. data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +84 -117
  533. data/spec/mongoid/criteria/queryable/extensions/date_spec.rb +44 -65
  534. data/spec/mongoid/criteria/queryable/extensions/date_time_spec.rb +86 -108
  535. data/spec/mongoid/criteria/queryable/extensions/float_spec.rb +94 -33
  536. data/spec/mongoid/criteria/queryable/extensions/hash_spec.rb +103 -138
  537. data/spec/mongoid/criteria/queryable/extensions/integer_spec.rb +95 -34
  538. data/spec/mongoid/criteria/queryable/extensions/nil_class_spec.rb +25 -38
  539. data/spec/mongoid/criteria/queryable/extensions/numeric_spec.rb +26 -28
  540. data/spec/mongoid/criteria/queryable/extensions/object_spec.rb +32 -54
  541. data/spec/mongoid/criteria/queryable/extensions/range_spec.rb +136 -130
  542. data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +16 -24
  543. data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +15 -24
  544. data/spec/mongoid/criteria/queryable/extensions/set_spec.rb +12 -19
  545. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +46 -63
  546. data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +28 -42
  547. data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +73 -96
  548. data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +71 -92
  549. data/spec/mongoid/criteria/queryable/key_spec.rb +32 -36
  550. data/spec/mongoid/criteria/queryable/mergeable_spec.rb +44 -49
  551. data/spec/mongoid/criteria/queryable/optional_spec.rb +373 -486
  552. data/spec/mongoid/criteria/queryable/options_spec.rb +172 -123
  553. data/spec/mongoid/criteria/queryable/pipeline_spec.rb +65 -87
  554. data/spec/mongoid/criteria/queryable/queryable_spec.rb +41 -53
  555. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +576 -680
  556. data/spec/mongoid/criteria/queryable/selectable_shared_examples.rb +7 -11
  557. data/spec/mongoid/criteria/queryable/selectable_spec.rb +807 -998
  558. data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +154 -191
  559. data/spec/mongoid/criteria/queryable/selector_spec.rb +294 -390
  560. data/spec/mongoid/criteria/queryable/smash_spec.rb +6 -11
  561. data/spec/mongoid/criteria/queryable/storable_spec.rb +54 -81
  562. data/spec/mongoid/criteria/queryable_mql_spec.rb +92 -0
  563. data/spec/mongoid/criteria/scopable_spec.rb +102 -138
  564. data/spec/mongoid/criteria/translator_spec.rb +59 -60
  565. data/spec/mongoid/criteria_projection_spec.rb +81 -99
  566. data/spec/mongoid/criteria_spec.rb +935 -950
  567. data/spec/mongoid/document_fields_spec.rb +28 -30
  568. data/spec/mongoid/document_persistence_context_spec.rb +13 -15
  569. data/spec/mongoid/document_query_spec.rb +9 -12
  570. data/spec/mongoid/document_spec.rb +317 -409
  571. data/spec/mongoid/equality_spec.rb +69 -102
  572. data/spec/mongoid/errors/ambiguous_relationship_spec.rb +8 -11
  573. data/spec/mongoid/errors/attribute_not_loaded_spec.rb +7 -10
  574. data/spec/mongoid/errors/callback_spec.rb +8 -11
  575. data/spec/mongoid/errors/delete_restriction_spec.rb +5 -8
  576. data/spec/mongoid/errors/document_not_destroyed_spec.rb +7 -10
  577. data/spec/mongoid/errors/document_not_found_spec.rb +51 -61
  578. data/spec/mongoid/errors/invalid_collection_spec.rb +9 -13
  579. data/spec/mongoid/errors/invalid_config_file_spec.rb +8 -11
  580. data/spec/mongoid/errors/invalid_config_option_spec.rb +8 -11
  581. data/spec/mongoid/errors/invalid_field_option_spec.rb +7 -10
  582. data/spec/mongoid/errors/invalid_field_spec.rb +10 -11
  583. data/spec/mongoid/errors/invalid_field_type_spec.rb +9 -12
  584. data/spec/mongoid/errors/invalid_find_spec.rb +8 -11
  585. data/spec/mongoid/errors/invalid_includes_spec.rb +9 -13
  586. data/spec/mongoid/errors/invalid_index_spec.rb +8 -11
  587. data/spec/mongoid/errors/invalid_options_spec.rb +8 -11
  588. data/spec/mongoid/errors/invalid_path_spec.rb +7 -10
  589. data/spec/mongoid/errors/invalid_relation_spec.rb +15 -16
  590. data/spec/mongoid/errors/invalid_scope_spec.rb +8 -11
  591. data/spec/mongoid/errors/invalid_set_polymorphic_relation_spec.rb +3 -6
  592. data/spec/mongoid/errors/invalid_storage_options_spec.rb +8 -11
  593. data/spec/mongoid/errors/invalid_time_spec.rb +8 -11
  594. data/spec/mongoid/errors/inverse_not_found_spec.rb +9 -12
  595. data/spec/mongoid/errors/mixed_client_configuration_spec.rb +9 -12
  596. data/spec/mongoid/errors/mixed_relations_spec.rb +8 -11
  597. data/spec/mongoid/errors/mongoid_error_spec.rb +21 -24
  598. data/spec/mongoid/errors/nested_attributes_metadata_not_found_spec.rb +6 -9
  599. data/spec/mongoid/errors/no_client_config_spec.rb +7 -10
  600. data/spec/mongoid/errors/no_client_database_spec.rb +9 -12
  601. data/spec/mongoid/errors/no_client_hosts_spec.rb +9 -12
  602. data/spec/mongoid/errors/no_clients_config_spec.rb +7 -10
  603. data/spec/mongoid/errors/no_environment_spec.rb +8 -11
  604. data/spec/mongoid/errors/no_map_reduce_output_spec.rb +8 -11
  605. data/spec/mongoid/errors/no_metadata_spec.rb +7 -10
  606. data/spec/mongoid/errors/no_parent_spec.rb +7 -10
  607. data/spec/mongoid/errors/readonly_attribute_spec.rb +7 -10
  608. data/spec/mongoid/errors/readonly_document_spec.rb +6 -9
  609. data/spec/mongoid/errors/scope_overwrite_spec.rb +9 -12
  610. data/spec/mongoid/errors/too_many_nested_attribute_records_spec.rb +9 -12
  611. data/spec/mongoid/errors/unknown_attribute_spec.rb +5 -8
  612. data/spec/mongoid/errors/unsaved_document_spec.rb +8 -11
  613. data/spec/mongoid/errors/unsupported_javascript_spec.rb +9 -12
  614. data/spec/mongoid/errors/validations_spec.rb +11 -14
  615. data/spec/mongoid/extensions/array_spec.rb +70 -92
  616. data/spec/mongoid/extensions/big_decimal_spec.rb +233 -313
  617. data/spec/mongoid/extensions/binary_spec.rb +21 -32
  618. data/spec/mongoid/extensions/boolean_spec.rb +52 -75
  619. data/spec/mongoid/extensions/date_class_mongoize_spec.rb +43 -54
  620. data/spec/mongoid/extensions/date_spec.rb +26 -38
  621. data/spec/mongoid/extensions/date_time_spec.rb +22 -32
  622. data/spec/mongoid/extensions/decimal128_spec.rb +12 -18
  623. data/spec/mongoid/extensions/false_class_spec.rb +10 -17
  624. data/spec/mongoid/extensions/float_spec.rb +40 -61
  625. data/spec/mongoid/extensions/hash_spec.rb +46 -64
  626. data/spec/mongoid/extensions/integer_spec.rb +40 -61
  627. data/spec/mongoid/extensions/module_spec.rb +13 -18
  628. data/spec/mongoid/extensions/nil_class_spec.rb +2 -5
  629. data/spec/mongoid/extensions/object_id_spec.rb +159 -247
  630. data/spec/mongoid/extensions/object_spec.rb +31 -47
  631. data/spec/mongoid/extensions/range_spec.rb +112 -115
  632. data/spec/mongoid/extensions/raw_value_spec.rb +10 -12
  633. data/spec/mongoid/extensions/regexp_spec.rb +23 -36
  634. data/spec/mongoid/extensions/set_spec.rb +38 -49
  635. data/spec/mongoid/extensions/string_spec.rb +118 -167
  636. data/spec/mongoid/extensions/stringified_symbol_spec.rb +29 -43
  637. data/spec/mongoid/extensions/symbol_spec.rb +12 -20
  638. data/spec/mongoid/extensions/time_spec.rb +160 -202
  639. data/spec/mongoid/extensions/time_with_zone_spec.rb +79 -105
  640. data/spec/mongoid/extensions/true_class_spec.rb +10 -17
  641. data/spec/mongoid/factory_spec.rb +125 -148
  642. data/spec/mongoid/fields/foreign_key_spec.rb +124 -169
  643. data/spec/mongoid/fields/localized_spec.rb +140 -185
  644. data/spec/mongoid/fields/standard_spec.rb +43 -59
  645. data/spec/mongoid/fields_spec.rb +751 -620
  646. data/spec/mongoid/findable_spec.rb +260 -228
  647. data/spec/mongoid/indexable/specification_spec.rb +42 -58
  648. data/spec/mongoid/indexable_spec.rb +260 -264
  649. data/spec/mongoid/inspectable_spec.rb +45 -55
  650. data/spec/mongoid/interceptable_spec.rb +784 -922
  651. data/spec/mongoid/interceptable_spec_models.rb +35 -35
  652. data/spec/mongoid/loading_spec.rb +47 -43
  653. data/spec/mongoid/loggable_spec.rb +3 -6
  654. data/spec/mongoid/matcher/extract_attribute_spec.rb +2 -6
  655. data/spec/mongoid/mongoizable_spec.rb +81 -90
  656. data/spec/mongoid/monkey_patches_spec.rb +4 -0
  657. data/spec/mongoid/persistable/creatable_spec.rb +181 -220
  658. data/spec/mongoid/persistable/deletable_spec.rb +139 -179
  659. data/spec/mongoid/persistable/destroyable_spec.rb +135 -164
  660. data/spec/mongoid/persistable/incrementable_spec.rb +65 -84
  661. data/spec/mongoid/persistable/logical_spec.rb +48 -61
  662. data/spec/mongoid/persistable/maxable_spec.rb +45 -44
  663. data/spec/mongoid/persistable/minable_spec.rb +45 -44
  664. data/spec/mongoid/persistable/multipliable_spec.rb +54 -69
  665. data/spec/mongoid/persistable/poppable_spec.rb +47 -60
  666. data/spec/mongoid/persistable/pullable_spec.rb +79 -103
  667. data/spec/mongoid/persistable/pushable_spec.rb +106 -131
  668. data/spec/mongoid/persistable/renamable_spec.rb +52 -65
  669. data/spec/mongoid/persistable/savable_spec.rb +152 -189
  670. data/spec/mongoid/persistable/settable_spec.rb +121 -145
  671. data/spec/mongoid/persistable/unsettable_spec.rb +56 -73
  672. data/spec/mongoid/persistable/updatable_spec.rb +185 -232
  673. data/spec/mongoid/persistable/upsertable_spec.rb +66 -77
  674. data/spec/mongoid/persistable_spec.rb +132 -139
  675. data/spec/mongoid/persistence_context_spec.rb +31 -89
  676. data/spec/mongoid/positional_spec.rb +67 -79
  677. data/spec/mongoid/railties/bson_object_id_serializer_spec.rb +0 -3
  678. data/spec/mongoid/railties/console_sandbox_spec.rb +0 -1
  679. data/spec/mongoid/relations/proxy_spec.rb +0 -1
  680. data/spec/mongoid/reloadable_spec.rb +250 -145
  681. data/spec/mongoid/scopable_spec.rb +319 -398
  682. data/spec/mongoid/search_indexable_spec.rb +446 -41
  683. data/spec/mongoid/selectable_spec.rb +36 -51
  684. data/spec/mongoid/serializable_spec.rb +269 -312
  685. data/spec/mongoid/shardable_models.rb +5 -5
  686. data/spec/mongoid/shardable_spec.rb +44 -46
  687. data/spec/mongoid/stateful_spec.rb +55 -75
  688. data/spec/mongoid/tasks/database_rake_spec.rb +138 -141
  689. data/spec/mongoid/tasks/database_spec.rb +80 -80
  690. data/spec/mongoid/tasks/encryption_spec.rb +22 -20
  691. data/spec/mongoid/threaded_spec.rb +57 -83
  692. data/spec/mongoid/timestamps/created/short_spec.rb +12 -16
  693. data/spec/mongoid/timestamps/created_spec.rb +16 -20
  694. data/spec/mongoid/timestamps/timeless_spec.rb +31 -43
  695. data/spec/mongoid/timestamps/updated/short_spec.rb +21 -28
  696. data/spec/mongoid/timestamps/updated_spec.rb +18 -25
  697. data/spec/mongoid/timestamps_spec.rb +171 -188
  698. data/spec/mongoid/timestamps_spec_models.rb +12 -13
  699. data/spec/mongoid/touchable_spec.rb +330 -294
  700. data/spec/mongoid/touchable_spec_models.rb +35 -37
  701. data/spec/mongoid/traversable_spec.rb +435 -471
  702. data/spec/mongoid/validatable/associated_spec.rb +42 -60
  703. data/spec/mongoid/validatable/format_spec.rb +24 -34
  704. data/spec/mongoid/validatable/length_spec.rb +62 -86
  705. data/spec/mongoid/validatable/numericality_spec.rb +114 -23
  706. data/spec/mongoid/validatable/presence_spec.rb +149 -203
  707. data/spec/mongoid/validatable/uniqueness_spec.rb +632 -830
  708. data/spec/mongoid/validatable_spec.rb +71 -103
  709. data/spec/mongoid/warnings_spec.rb +5 -9
  710. data/spec/mongoid_spec.rb +25 -41
  711. data/spec/rails/controller_extension/controller_runtime_spec.rb +45 -49
  712. data/spec/rails/mongoid_spec.rb +15 -15
  713. data/spec/shared/lib/mrss/cluster_config.rb +5 -40
  714. data/spec/shared/lib/mrss/constraints.rb +7 -46
  715. data/spec/shared/lib/mrss/docker_runner.rb +10 -21
  716. data/spec/shared/lib/mrss/eg_config_utils.rb +0 -31
  717. data/spec/shared/lib/mrss/release/candidate.rb +18 -12
  718. data/spec/shared/lib/mrss/server_version_registry.rb +1 -5
  719. data/spec/shared/share/Dockerfile.erb +6 -8
  720. data/spec/shared/shlib/server.sh +0 -50
  721. data/spec/shared/shlib/set_env.sh +4 -10
  722. data/spec/spec_helper.rb +54 -31
  723. data/spec/support/authorization.rb +0 -1
  724. data/spec/support/client_registry.rb +1 -2
  725. data/spec/support/constraints.rb +8 -25
  726. data/spec/support/crypt/models.rb +7 -8
  727. data/spec/support/crypt.rb +6 -9
  728. data/spec/support/expectations.rb +21 -7
  729. data/spec/support/feature_sandbox.rb +1 -4
  730. data/spec/support/helpers.rb +0 -1
  731. data/spec/support/immutable_ids.rb +20 -21
  732. data/spec/support/macros.rb +1 -3
  733. data/spec/support/models/account.rb +4 -5
  734. data/spec/support/models/acolyte.rb +5 -5
  735. data/spec/support/models/actor.rb +3 -3
  736. data/spec/support/models/actress.rb +0 -1
  737. data/spec/support/models/address.rb +11 -11
  738. data/spec/support/models/address_component.rb +1 -1
  739. data/spec/support/models/address_number.rb +1 -1
  740. data/spec/support/models/agency.rb +1 -1
  741. data/spec/support/models/agent.rb +1 -1
  742. data/spec/support/models/album.rb +1 -2
  743. data/spec/support/models/alert.rb +1 -1
  744. data/spec/support/models/animal.rb +4 -5
  745. data/spec/support/models/answer.rb +1 -1
  746. data/spec/support/models/appointment.rb +2 -2
  747. data/spec/support/models/armrest.rb +0 -1
  748. data/spec/support/models/array_field.rb +0 -1
  749. data/spec/support/models/article.rb +0 -1
  750. data/spec/support/models/artist.rb +16 -12
  751. data/spec/support/models/artwork.rb +1 -1
  752. data/spec/support/models/audible_sound.rb +0 -1
  753. data/spec/support/models/audio.rb +2 -2
  754. data/spec/support/models/augmentation.rb +1 -2
  755. data/spec/support/models/author.rb +1 -1
  756. data/spec/support/models/baby.rb +1 -1
  757. data/spec/support/models/band.rb +0 -1
  758. data/spec/support/models/bar.rb +2 -2
  759. data/spec/support/models/basic.rb +0 -1
  760. data/spec/support/models/bed.rb +0 -1
  761. data/spec/support/models/big_palette.rb +0 -1
  762. data/spec/support/models/birthday.rb +3 -5
  763. data/spec/support/models/bolt.rb +0 -1
  764. data/spec/support/models/bomb.rb +1 -1
  765. data/spec/support/models/book.rb +1 -1
  766. data/spec/support/models/breed.rb +1 -1
  767. data/spec/support/models/browser.rb +1 -2
  768. data/spec/support/models/building.rb +0 -1
  769. data/spec/support/models/building_address.rb +1 -1
  770. data/spec/support/models/bus.rb +1 -1
  771. data/spec/support/models/business.rb +2 -2
  772. data/spec/support/models/callback_test.rb +1 -1
  773. data/spec/support/models/canvas.rb +5 -6
  774. data/spec/support/models/car.rb +0 -1
  775. data/spec/support/models/cat.rb +0 -2
  776. data/spec/support/models/catalog.rb +0 -1
  777. data/spec/support/models/category.rb +1 -1
  778. data/spec/support/models/child.rb +1 -1
  779. data/spec/support/models/child_doc.rb +5 -6
  780. data/spec/support/models/church.rb +1 -1
  781. data/spec/support/models/circle.rb +0 -1
  782. data/spec/support/models/circuit.rb +2 -2
  783. data/spec/support/models/circus.rb +0 -1
  784. data/spec/support/models/code.rb +2 -1
  785. data/spec/support/models/coding/pull_request.rb +0 -1
  786. data/spec/support/models/coding.rb +0 -1
  787. data/spec/support/models/comment.rb +0 -1
  788. data/spec/support/models/company.rb +0 -1
  789. data/spec/support/models/consumption_period.rb +0 -1
  790. data/spec/support/models/contextable_item.rb +1 -1
  791. data/spec/support/models/contractor.rb +1 -1
  792. data/spec/support/models/cookie.rb +0 -1
  793. data/spec/support/models/country_code.rb +2 -3
  794. data/spec/support/models/courier_job.rb +2 -2
  795. data/spec/support/models/cover.rb +0 -1
  796. data/spec/support/models/crate.rb +0 -1
  797. data/spec/support/models/customer.rb +0 -1
  798. data/spec/support/models/customer_address.rb +0 -1
  799. data/spec/support/models/deed.rb +1 -1
  800. data/spec/support/models/definition.rb +1 -1
  801. data/spec/support/models/delegating_patient.rb +0 -1
  802. data/spec/support/models/description.rb +0 -1
  803. data/spec/support/models/dictionary.rb +1 -1
  804. data/spec/support/models/division.rb +2 -2
  805. data/spec/support/models/doctor.rb +0 -1
  806. data/spec/support/models/dog.rb +2 -2
  807. data/spec/support/models/dokument.rb +1 -1
  808. data/spec/support/models/draft.rb +0 -1
  809. data/spec/support/models/dragon.rb +1 -1
  810. data/spec/support/models/driver.rb +2 -2
  811. data/spec/support/models/drug.rb +1 -1
  812. data/spec/support/models/dungeon.rb +1 -1
  813. data/spec/support/models/edit.rb +1 -1
  814. data/spec/support/models/email.rb +1 -1
  815. data/spec/support/models/employer.rb +1 -2
  816. data/spec/support/models/entry.rb +1 -1
  817. data/spec/support/models/eraser.rb +0 -1
  818. data/spec/support/models/even.rb +1 -1
  819. data/spec/support/models/event.rb +3 -4
  820. data/spec/support/models/exhibition.rb +1 -1
  821. data/spec/support/models/exhibitor.rb +1 -1
  822. data/spec/support/models/explosion.rb +1 -1
  823. data/spec/support/models/eye.rb +0 -1
  824. data/spec/support/models/eye_bowl.rb +2 -3
  825. data/spec/support/models/face.rb +2 -3
  826. data/spec/support/models/fanatic.rb +1 -1
  827. data/spec/support/models/favorite.rb +1 -1
  828. data/spec/support/models/filesystem.rb +1 -1
  829. data/spec/support/models/fire_hydrant.rb +1 -1
  830. data/spec/support/models/firefox.rb +0 -1
  831. data/spec/support/models/fish.rb +0 -1
  832. data/spec/support/models/folder.rb +0 -2
  833. data/spec/support/models/folder_item.rb +1 -3
  834. data/spec/support/models/fruits.rb +9 -5
  835. data/spec/support/models/game.rb +3 -4
  836. data/spec/support/models/ghost.rb +0 -1
  837. data/spec/support/models/guitar.rb +0 -1
  838. data/spec/support/models/hole.rb +0 -1
  839. data/spec/support/models/home.rb +1 -1
  840. data/spec/support/models/house.rb +2 -2
  841. data/spec/support/models/html_writer.rb +0 -1
  842. data/spec/support/models/id_key.rb +3 -3
  843. data/spec/support/models/idnodef.rb +0 -1
  844. data/spec/support/models/image.rb +0 -1
  845. data/spec/support/models/implant.rb +1 -2
  846. data/spec/support/models/instrument.rb +2 -3
  847. data/spec/support/models/item.rb +2 -2
  848. data/spec/support/models/jar.rb +1 -2
  849. data/spec/support/models/kaleidoscope.rb +1 -1
  850. data/spec/support/models/kangaroo.rb +1 -1
  851. data/spec/support/models/label.rb +2 -2
  852. data/spec/support/models/language.rb +1 -1
  853. data/spec/support/models/lat_lng.rb +0 -1
  854. data/spec/support/models/league.rb +1 -1
  855. data/spec/support/models/learner.rb +0 -1
  856. data/spec/support/models/line_item.rb +1 -1
  857. data/spec/support/models/location.rb +1 -1
  858. data/spec/support/models/login.rb +1 -2
  859. data/spec/support/models/manufacturer.rb +0 -1
  860. data/spec/support/models/meat.rb +1 -1
  861. data/spec/support/models/membership.rb +1 -1
  862. data/spec/support/models/message.rb +1 -2
  863. data/spec/support/models/minim.rb +0 -1
  864. data/spec/support/models/mixed_drink.rb +1 -1
  865. data/spec/support/models/modifier_models.rb +25 -0
  866. data/spec/support/models/mop.rb +0 -1
  867. data/spec/support/models/movie.rb +1 -1
  868. data/spec/support/models/my_hash.rb +1 -2
  869. data/spec/support/models/name.rb +4 -5
  870. data/spec/support/models/name_only.rb +0 -1
  871. data/spec/support/models/node.rb +1 -1
  872. data/spec/support/models/note.rb +3 -3
  873. data/spec/support/models/nut.rb +0 -1
  874. data/spec/support/models/odd.rb +1 -1
  875. data/spec/support/models/order.rb +1 -1
  876. data/spec/support/models/ordered_post.rb +1 -1
  877. data/spec/support/models/ordered_preference.rb +1 -1
  878. data/spec/support/models/oscar.rb +1 -1
  879. data/spec/support/models/other_owner_object.rb +0 -1
  880. data/spec/support/models/override.rb +3 -11
  881. data/spec/support/models/ownable.rb +1 -1
  882. data/spec/support/models/owner.rb +1 -1
  883. data/spec/support/models/pack.rb +0 -1
  884. data/spec/support/models/page.rb +3 -3
  885. data/spec/support/models/page_question.rb +1 -1
  886. data/spec/support/models/palette.rb +2 -2
  887. data/spec/support/models/parent.rb +2 -2
  888. data/spec/support/models/parent_doc.rb +1 -1
  889. data/spec/support/models/passport.rb +0 -1
  890. data/spec/support/models/patient.rb +2 -2
  891. data/spec/support/models/pdf_writer.rb +0 -1
  892. data/spec/support/models/pencil.rb +0 -1
  893. data/spec/support/models/person.rb +32 -28
  894. data/spec/support/models/pet.rb +1 -1
  895. data/spec/support/models/pet_owner.rb +1 -1
  896. data/spec/support/models/phone.rb +1 -2
  897. data/spec/support/models/piano.rb +0 -1
  898. data/spec/support/models/pizza.rb +1 -1
  899. data/spec/support/models/player.rb +5 -5
  900. data/spec/support/models/post.rb +12 -12
  901. data/spec/support/models/post_genre.rb +1 -1
  902. data/spec/support/models/powerup.rb +1 -2
  903. data/spec/support/models/preference.rb +2 -2
  904. data/spec/support/models/princess.rb +1 -1
  905. data/spec/support/models/product.rb +3 -3
  906. data/spec/support/models/profile.rb +2 -1
  907. data/spec/support/models/pronunciation.rb +1 -1
  908. data/spec/support/models/pub.rb +2 -2
  909. data/spec/support/models/publication/encyclopedia.rb +0 -1
  910. data/spec/support/models/publication/review.rb +0 -1
  911. data/spec/support/models/publication.rb +0 -1
  912. data/spec/support/models/purchase.rb +1 -1
  913. data/spec/support/models/purchased_item.rb +1 -1
  914. data/spec/support/models/question.rb +2 -2
  915. data/spec/support/models/quiz.rb +1 -1
  916. data/spec/support/models/rating.rb +1 -1
  917. data/spec/support/models/record.rb +2 -3
  918. data/spec/support/models/registry.rb +1 -1
  919. data/spec/support/models/role.rb +1 -1
  920. data/spec/support/models/root_category.rb +1 -1
  921. data/spec/support/models/sandbox/app/models/app_models_message.rb +0 -1
  922. data/spec/support/models/sandbox/lib/models/lib_models_message.rb +0 -1
  923. data/spec/support/models/sandbox/sandbox_message.rb +0 -1
  924. data/spec/support/models/sandbox/sandbox_user.rb +0 -1
  925. data/spec/support/models/sandbox/subdir/sandbox_comment.rb +0 -1
  926. data/spec/support/models/sandwich.rb +1 -1
  927. data/spec/support/models/scheduler.rb +0 -1
  928. data/spec/support/models/school.rb +0 -1
  929. data/spec/support/models/scribe.rb +1 -1
  930. data/spec/support/models/sealer.rb +0 -1
  931. data/spec/support/models/seat.rb +0 -1
  932. data/spec/support/models/seo.rb +1 -1
  933. data/spec/support/models/series.rb +1 -1
  934. data/spec/support/models/server.rb +1 -1
  935. data/spec/support/models/service.rb +2 -2
  936. data/spec/support/models/shape.rb +3 -3
  937. data/spec/support/models/shelf.rb +1 -1
  938. data/spec/support/models/shield.rb +2 -2
  939. data/spec/support/models/shipment_address.rb +0 -1
  940. data/spec/support/models/shipping_container.rb +1 -1
  941. data/spec/support/models/shipping_pack.rb +0 -1
  942. data/spec/support/models/shirt.rb +0 -1
  943. data/spec/support/models/shop.rb +2 -2
  944. data/spec/support/models/short_agent.rb +0 -1
  945. data/spec/support/models/short_quiz.rb +1 -1
  946. data/spec/support/models/simple.rb +1 -1
  947. data/spec/support/models/slave.rb +1 -1
  948. data/spec/support/models/song.rb +1 -2
  949. data/spec/support/models/sound.rb +2 -2
  950. data/spec/support/models/spacer.rb +0 -1
  951. data/spec/support/models/square.rb +0 -1
  952. data/spec/support/models/staff.rb +0 -1
  953. data/spec/support/models/store_as_dup_test1.rb +2 -2
  954. data/spec/support/models/store_as_dup_test2.rb +1 -1
  955. data/spec/support/models/store_as_dup_test3.rb +2 -2
  956. data/spec/support/models/store_as_dup_test4.rb +1 -1
  957. data/spec/support/models/strategy.rb +0 -1
  958. data/spec/support/models/student.rb +1 -2
  959. data/spec/support/models/sub_item.rb +0 -1
  960. data/spec/support/models/subscription.rb +2 -2
  961. data/spec/support/models/survey.rb +2 -2
  962. data/spec/support/models/symptom.rb +2 -2
  963. data/spec/support/models/system_role.rb +2 -3
  964. data/spec/support/models/tag.rb +2 -2
  965. data/spec/support/models/target.rb +1 -1
  966. data/spec/support/models/template.rb +1 -1
  967. data/spec/support/models/thing.rb +1 -1
  968. data/spec/support/models/threadlocker.rb +0 -1
  969. data/spec/support/models/title.rb +0 -1
  970. data/spec/support/models/tool.rb +3 -3
  971. data/spec/support/models/topping.rb +1 -1
  972. data/spec/support/models/toy.rb +0 -1
  973. data/spec/support/models/track.rb +1 -1
  974. data/spec/support/models/translation.rb +1 -1
  975. data/spec/support/models/tree.rb +2 -3
  976. data/spec/support/models/truck.rb +0 -1
  977. data/spec/support/models/updatable.rb +0 -1
  978. data/spec/support/models/user.rb +3 -4
  979. data/spec/support/models/user_account.rb +3 -3
  980. data/spec/support/models/validation_callback.rb +4 -4
  981. data/spec/support/models/vehicle.rb +2 -3
  982. data/spec/support/models/version.rb +1 -1
  983. data/spec/support/models/vertex.rb +0 -1
  984. data/spec/support/models/vet_visit.rb +1 -1
  985. data/spec/support/models/video.rb +2 -2
  986. data/spec/support/models/video_game.rb +0 -1
  987. data/spec/support/models/washer.rb +0 -1
  988. data/spec/support/models/weapon.rb +1 -2
  989. data/spec/support/models/wiki_page.rb +2 -3
  990. data/spec/support/models/word.rb +2 -2
  991. data/spec/support/models/word_origin.rb +1 -2
  992. data/spec/support/models/writer.rb +3 -3
  993. data/spec/support/rails_mock.rb +7 -12
  994. data/spec/support/shared/time.rb +0 -1
  995. data/spec/support/sinatra_mock.rb +4 -2
  996. data/spec/support/spec_config.rb +19 -18
  997. metadata +14 -2
@@ -1,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
@@ -2268,53 +2162,52 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2268
2162
  end
2269
2163
 
2270
2164
  %i[ delete_all destroy_all ].each do |method|
2271
- describe "\##{method}" do
2165
+ describe "##{method}" do
2272
2166
  context 'when the relation is not polymorphic' do
2273
2167
  context 'when conditions are provided' do
2274
2168
  let(:person) { Person.create! }
2275
2169
 
2276
2170
  let!(:preference_one) do
2277
- person.preferences.create!(name: "Testing")
2171
+ person.preferences.create!(name: 'Testing')
2278
2172
  end
2279
2173
 
2280
2174
  let!(:preference_two) do
2281
- person.preferences.create!(name: "Test")
2175
+ person.preferences.create!(name: 'Test')
2282
2176
  end
2283
2177
 
2284
2178
  let!(:deleted) do
2285
2179
  person.preferences.send(
2286
- method,
2287
- { name: "Testing" }
2180
+ method,
2181
+ { name: 'Testing' }
2288
2182
  )
2289
2183
  end
2290
2184
 
2291
- it "removes the correct preferences" do
2185
+ it 'removes the correct preferences' do
2292
2186
  expect(person.preferences.count).to eq(1)
2293
2187
  end
2294
2188
 
2295
- it "deletes the documents from the database" do
2296
- 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)
2297
2191
  end
2298
2192
 
2299
- it "returns the number of documents deleted" do
2193
+ it 'returns the number of documents deleted' do
2300
2194
  expect(deleted).to eq(1)
2301
2195
  end
2302
2196
 
2303
- it "removes the ids from the foreign key" do
2197
+ it 'removes the ids from the foreign key' do
2304
2198
  expect(person.preference_ids).to eq([ preference_two.id ])
2305
2199
  end
2306
2200
 
2307
- it "sets the association locally" do
2308
- expect(person.preferences).to eq([preference_two])
2201
+ it 'sets the association locally' do
2202
+ expect(person.preferences).to eq([ preference_two ])
2309
2203
  end
2310
2204
  end
2311
2205
 
2312
- context "when conditions are not provided" do
2313
-
2206
+ context 'when conditions are not provided' do
2314
2207
  let(:person) do
2315
2208
  Person.create!.tap do |person|
2316
- person.preferences.create!(name: "Testing")
2317
- person.preferences.create!(name: "Test")
2209
+ person.preferences.create!(name: 'Testing')
2210
+ person.preferences.create!(name: 'Test')
2318
2211
  end
2319
2212
  end
2320
2213
 
@@ -2322,19 +2215,19 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2322
2215
  person.preferences.send(method)
2323
2216
  end
2324
2217
 
2325
- it "removes the correct preferences" do
2218
+ it 'removes the correct preferences' do
2326
2219
  expect(person.preferences.count).to eq(0)
2327
2220
  end
2328
2221
 
2329
- it "deletes the documents from the database" do
2222
+ it 'deletes the documents from the database' do
2330
2223
  expect(Preference.count).to eq(0)
2331
2224
  end
2332
2225
 
2333
- it "returns the number of documents deleted" do
2226
+ it 'returns the number of documents deleted' do
2334
2227
  expect(deleted).to eq(2)
2335
2228
  end
2336
2229
 
2337
- it "sets the association locally" do
2230
+ it 'sets the association locally' do
2338
2231
  expect(person.preferences).to eq([])
2339
2232
  end
2340
2233
  end
@@ -2342,166 +2235,156 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2342
2235
  end
2343
2236
  end
2344
2237
 
2345
- describe ".embedded?" do
2346
-
2347
- it "returns false" do
2348
- expect(described_class).to_not be_embedded
2238
+ describe '.embedded?' do
2239
+ it 'returns false' do
2240
+ expect(described_class).not_to be_embedded
2349
2241
  end
2350
2242
  end
2351
2243
 
2352
- describe "#exists?" do
2353
-
2244
+ describe '#exists?' do
2354
2245
  let!(:person) do
2355
2246
  Person.create!
2356
2247
  end
2357
2248
 
2358
- context "when documents exist in the database" do
2359
-
2249
+ context 'when documents exist in the database' do
2360
2250
  before do
2361
2251
  person.preferences.create!
2362
2252
  end
2363
2253
 
2364
- it "returns true" do
2254
+ it 'returns true' do
2365
2255
  expect(person.preferences.exists?).to be true
2366
2256
  end
2367
2257
  end
2368
2258
 
2369
- context "when no documents exist in the database" do
2370
-
2259
+ context 'when no documents exist in the database' do
2371
2260
  before do
2372
2261
  person.preferences.build
2373
2262
  end
2374
2263
 
2375
- it "returns false" do
2264
+ it 'returns false' do
2376
2265
  expect(person.preferences.exists?).to be false
2377
2266
  end
2378
2267
  end
2379
2268
  end
2380
2269
 
2381
- describe "#find" do
2382
-
2383
- context "when the relation is not polymorphic" do
2384
-
2270
+ describe '#find' do
2271
+ context 'when the relation is not polymorphic' do
2385
2272
  let(:person) do
2386
2273
  Person.create!
2387
2274
  end
2388
2275
 
2389
2276
  let!(:preference_one) do
2390
- person.preferences.create!(name: "Test")
2277
+ person.preferences.create!(name: 'Test')
2391
2278
  end
2392
2279
 
2393
2280
  let!(:preference_two) do
2394
- person.preferences.create!(name: "OMG I has relations")
2281
+ person.preferences.create!(name: 'OMG I has relations')
2395
2282
  end
2396
2283
 
2397
2284
  let!(:unrelated_pref) do
2398
- Preference.create!(name: "orphan annie")
2285
+ Preference.create!(name: 'orphan annie')
2399
2286
  end
2400
2287
 
2401
2288
  let!(:unrelated_pref_two) do
2402
- Preference.create!(name: "orphan two")
2289
+ Preference.create!(name: 'orphan two')
2403
2290
  end
2404
2291
 
2405
- context "when providing an id" do
2406
-
2407
- context "when the id matches" do
2408
-
2292
+ context 'when providing an id' do
2293
+ context 'when the id matches' do
2409
2294
  let(:preference) do
2410
2295
  person.preferences.find(preference_one.id)
2411
2296
  end
2412
2297
 
2413
- it "returns the matching document" do
2298
+ it 'returns the matching document' do
2414
2299
  expect(preference).to eq(preference_one)
2415
2300
  end
2416
2301
  end
2417
2302
 
2418
- context "when the id matches an unreferenced document" do
2419
-
2303
+ context 'when the id matches an unreferenced document' do
2420
2304
  let(:preference) do
2421
2305
  person.preferences.find(unrelated_pref.id)
2422
2306
  end
2423
2307
 
2424
- it "raises an error" do
2425
- expect {
2308
+ it 'raises an error' do
2309
+ expect do
2426
2310
  preference
2427
- }.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\)/)
2428
2313
  end
2429
2314
  end
2430
2315
 
2431
- context "when the id does not match" do
2432
-
2433
- 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
2434
2318
  config_override :raise_not_found_error, true
2435
2319
 
2436
- it "raises an error" do
2437
- expect {
2320
+ it 'raises an error' do
2321
+ expect do
2438
2322
  person.preferences.find(BSON::ObjectId.new)
2439
- }.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\)/)
2440
2325
  end
2441
2326
  end
2442
2327
 
2443
- context "when config set not to raise error" do
2328
+ context 'when config set not to raise error' do
2444
2329
  config_override :raise_not_found_error, false
2445
2330
 
2446
2331
  let(:preference) do
2447
2332
  person.preferences.find(BSON::ObjectId.new)
2448
2333
  end
2449
2334
 
2450
- it "returns nil" do
2335
+ it 'returns nil' do
2451
2336
  expect(preference).to be_nil
2452
2337
  end
2453
2338
  end
2454
2339
  end
2455
2340
  end
2456
2341
 
2457
- context "when providing an array of ids" do
2458
-
2459
- context "when the ids match" do
2460
-
2342
+ context 'when providing an array of ids' do
2343
+ context 'when the ids match' do
2461
2344
  let(:preferences) do
2462
2345
  person.preferences.find([ preference_one.id, preference_two.id ])
2463
2346
  end
2464
2347
 
2465
- it "returns the matching documents" do
2348
+ it 'returns the matching documents' do
2466
2349
  expect(preferences).to eq([ preference_one, preference_two ])
2467
2350
  end
2468
2351
  end
2469
2352
 
2470
- context "when the ids match unreferenced documents" do
2471
-
2353
+ context 'when the ids match unreferenced documents' do
2472
2354
  let(:preferences) do
2473
2355
  person.preferences.find(
2474
- [ unrelated_pref.id, unrelated_pref_two.id ]
2356
+ [ unrelated_pref.id, unrelated_pref_two.id ]
2475
2357
  )
2476
2358
  end
2477
2359
 
2478
- it "raises an error" do
2479
- expect {
2360
+ it 'raises an error' do
2361
+ expect do
2480
2362
  preferences
2481
- }.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\)/)
2482
2365
  end
2483
2366
  end
2484
2367
 
2485
- context "when the ids do not match" do
2486
-
2487
- 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
2488
2370
  config_override :raise_not_found_error, true
2489
2371
 
2490
- it "raises an error" do
2491
- expect {
2372
+ it 'raises an error' do
2373
+ expect do
2492
2374
  person.preferences.find([ BSON::ObjectId.new ])
2493
- }.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\)/)
2494
2377
  end
2495
2378
  end
2496
2379
 
2497
- context "when config set not to raise error" do
2380
+ context 'when config set not to raise error' do
2498
2381
  config_override :raise_not_found_error, false
2499
2382
 
2500
2383
  let(:preferences) do
2501
2384
  person.preferences.find([ BSON::ObjectId.new ])
2502
2385
  end
2503
2386
 
2504
- it "returns an empty array" do
2387
+ it 'returns an empty array' do
2505
2388
  expect(preferences).to be_empty
2506
2389
  end
2507
2390
  end
@@ -2510,140 +2393,129 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2510
2393
  end
2511
2394
  end
2512
2395
 
2513
- describe "#find_or_create_by" do
2514
-
2515
- context "when the relation is not polymorphic" do
2516
-
2396
+ describe '#find_or_create_by' do
2397
+ context 'when the relation is not polymorphic' do
2517
2398
  let(:person) do
2518
2399
  Person.create!
2519
2400
  end
2520
2401
 
2521
2402
  let!(:preference) do
2522
- person.preferences.create!(name: "Testing")
2403
+ person.preferences.create!(name: 'Testing')
2523
2404
  end
2524
2405
 
2525
- context "when the document exists" do
2526
-
2406
+ context 'when the document exists' do
2527
2407
  let(:found) do
2528
- person.preferences.find_or_create_by(name: "Testing")
2408
+ person.preferences.find_or_create_by(name: 'Testing')
2529
2409
  end
2530
2410
 
2531
- it "returns the document" do
2411
+ it 'returns the document' do
2532
2412
  expect(found).to eq(preference)
2533
2413
  end
2534
2414
  end
2535
2415
 
2536
- context "when the document does not exist" do
2537
-
2416
+ context 'when the document does not exist' do
2538
2417
  let(:found) do
2539
- person.preferences.find_or_create_by(name: "Test")
2418
+ person.preferences.find_or_create_by(name: 'Test')
2540
2419
  end
2541
2420
 
2542
- it "sets the new document attributes" do
2543
- expect(found.name).to eq("Test")
2421
+ it 'sets the new document attributes' do
2422
+ expect(found.name).to eq('Test')
2544
2423
  end
2545
2424
 
2546
- it "returns a newly persisted document" do
2425
+ it 'returns a newly persisted document' do
2547
2426
  expect(found).to be_persisted
2548
2427
  end
2549
2428
  end
2550
2429
  end
2551
2430
  end
2552
2431
 
2553
- describe "#find_or_create_by!" do
2554
-
2555
- context "when the relation is not polymorphic" do
2556
-
2432
+ describe '#find_or_create_by!' do
2433
+ context 'when the relation is not polymorphic' do
2557
2434
  let(:person) do
2558
2435
  Person.create!
2559
2436
  end
2560
2437
 
2561
2438
  let!(:preference) do
2562
- person.preferences.create!(name: "Testing")
2439
+ person.preferences.create!(name: 'Testing')
2563
2440
  end
2564
2441
 
2565
- context "when the document exists" do
2566
-
2442
+ context 'when the document exists' do
2567
2443
  let(:found) do
2568
- person.preferences.find_or_create_by!(name: "Testing")
2444
+ person.preferences.find_or_create_by!(name: 'Testing')
2569
2445
  end
2570
2446
 
2571
- it "returns the document" do
2447
+ it 'returns the document' do
2572
2448
  expect(found).to eq(preference)
2573
2449
  end
2574
2450
  end
2575
2451
 
2576
- context "when the document does not exist" do
2577
-
2452
+ context 'when the document does not exist' do
2578
2453
  let(:found) do
2579
- person.preferences.find_or_create_by!(name: "Test")
2454
+ person.preferences.find_or_create_by!(name: 'Test')
2580
2455
  end
2581
2456
 
2582
- it "sets the new document attributes" do
2583
- expect(found.name).to eq("Test")
2457
+ it 'sets the new document attributes' do
2458
+ expect(found.name).to eq('Test')
2584
2459
  end
2585
2460
 
2586
- it "returns a newly persisted document" do
2461
+ it 'returns a newly persisted document' do
2587
2462
  expect(found).to be_persisted
2588
2463
  end
2589
2464
 
2590
- context "when validation fails" do
2591
-
2592
- it "raises an error" do
2593
- expect {
2594
- person.preferences.find_or_create_by!(name: "A")
2595
- }.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)
2596
2470
  end
2597
2471
  end
2598
2472
  end
2599
2473
  end
2600
2474
  end
2601
2475
 
2602
- describe "#find_or_initialize_by" do
2603
-
2604
- context "when the relation is not polymorphic" do
2605
-
2476
+ describe '#find_or_initialize_by' do
2477
+ context 'when the relation is not polymorphic' do
2606
2478
  let(:person) do
2607
2479
  Person.create!
2608
2480
  end
2609
2481
 
2610
2482
  let!(:preference) do
2611
- person.preferences.create!(name: "Testing")
2483
+ person.preferences.create!(name: 'Testing')
2612
2484
  end
2613
2485
 
2614
- context "when the document exists" do
2615
-
2486
+ context 'when the document exists' do
2616
2487
  let(:found) do
2617
- person.preferences.find_or_initialize_by(name: "Testing")
2488
+ person.preferences.find_or_initialize_by(name: 'Testing')
2618
2489
  end
2619
2490
 
2620
- it "returns the document" do
2491
+ it 'returns the document' do
2621
2492
  expect(found).to eq(preference)
2622
2493
  end
2623
2494
  end
2624
2495
 
2625
- context "when the document does not exist" do
2626
-
2496
+ context 'when the document does not exist' do
2627
2497
  let(:found) do
2628
- person.preferences.find_or_initialize_by(name: "Test")
2498
+ person.preferences.find_or_initialize_by(name: 'Test')
2629
2499
  end
2630
2500
 
2631
- it "sets the new document attributes" do
2632
- expect(found.name).to eq("Test")
2501
+ it 'sets the new document attributes' do
2502
+ expect(found.name).to eq('Test')
2633
2503
  end
2634
2504
 
2635
- it "returns a non persisted document" do
2636
- expect(found).to_not be_persisted
2505
+ it 'returns a non persisted document' do
2506
+ expect(found).not_to be_persisted
2637
2507
  end
2638
2508
  end
2639
2509
  end
2640
2510
  end
2641
2511
 
2642
- describe "#max" do
2643
-
2512
+ describe '#max' do
2644
2513
  let(:person) do
2645
2514
  Person.create!
2646
2515
  end
2516
+ let(:max) do
2517
+ person.preferences.max_by(&:ranking)
2518
+ end
2647
2519
 
2648
2520
  let(:preference_one) do
2649
2521
  Preference.create!(ranking: 5)
@@ -2657,22 +2529,18 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2657
2529
  person.preferences.push(preference_one, preference_two)
2658
2530
  end
2659
2531
 
2660
- let(:max) do
2661
- person.preferences.max do |a,b|
2662
- a.ranking <=> b.ranking
2663
- end
2664
- end
2665
-
2666
- 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
2667
2533
  expect(max).to eq(preference_two)
2668
2534
  end
2669
2535
  end
2670
2536
 
2671
- describe "#max_by" do
2672
-
2537
+ describe '#max_by' do
2673
2538
  let(:person) do
2674
2539
  Person.create!
2675
2540
  end
2541
+ let(:max) do
2542
+ person.preferences.max_by(&:ranking)
2543
+ end
2676
2544
 
2677
2545
  let(:preference_one) do
2678
2546
  Preference.create!(ranking: 5)
@@ -2686,108 +2554,95 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2686
2554
  person.preferences.push(preference_one, preference_two)
2687
2555
  end
2688
2556
 
2689
- let(:max) do
2690
- person.preferences.max_by(&:ranking)
2691
- end
2692
-
2693
- 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
2694
2558
  expect(max).to eq(preference_two)
2695
2559
  end
2696
2560
  end
2697
2561
 
2698
- describe "#method_missing" do
2699
-
2562
+ describe '#method_missing' do
2700
2563
  let!(:person) do
2701
2564
  Person.create!
2702
2565
  end
2703
2566
 
2704
2567
  let!(:preference_one) do
2705
- person.preferences.create!(name: "First", value: "Posting")
2568
+ person.preferences.create!(name: 'First', value: 'Posting')
2706
2569
  end
2707
2570
 
2708
2571
  let!(:preference_two) do
2709
- person.preferences.create!(name: "Second", value: "Testing")
2572
+ person.preferences.create!(name: 'Second', value: 'Testing')
2710
2573
  end
2711
2574
 
2712
2575
  let!(:unrelated) do
2713
- Preference.create!(name: "Third")
2576
+ Preference.create!(name: 'Third')
2714
2577
  end
2715
2578
 
2716
- context "when providing a single criteria" do
2717
-
2579
+ context 'when providing a single criteria' do
2718
2580
  let(:preferences) do
2719
- person.preferences.where(name: "First")
2581
+ person.preferences.where(name: 'First')
2720
2582
  end
2721
2583
 
2722
- it "applies the criteria to the documents" do
2584
+ it 'applies the criteria to the documents' do
2723
2585
  expect(preferences).to eq([ preference_one ])
2724
2586
  end
2725
2587
 
2726
2588
  context 'when providing a collation' do
2727
-
2728
2589
  let(:preferences) do
2729
- 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
2730
2591
  end
2731
2592
 
2732
- it "applies the collation option to the query" do
2593
+ it 'applies the collation option to the query' do
2733
2594
  expect(preferences).to eq([ preference_one ])
2734
2595
  end
2735
2596
  end
2736
2597
  end
2737
2598
 
2738
- context "when providing a criteria on id" do
2739
-
2599
+ context 'when providing a criteria on id' do
2740
2600
  let(:preferences) do
2741
2601
  person.preferences.where(_id: unrelated.id)
2742
2602
  end
2743
2603
 
2744
- it "does not return unrelated documents" do
2604
+ it 'does not return unrelated documents' do
2745
2605
  expect(preferences).to be_empty
2746
2606
  end
2747
2607
  end
2748
2608
 
2749
- context "when providing a criteria class method" do
2750
-
2609
+ context 'when providing a criteria class method' do
2751
2610
  let(:preferences) do
2752
2611
  person.preferences.posting
2753
2612
  end
2754
2613
 
2755
- it "applies the criteria to the documents" do
2614
+ it 'applies the criteria to the documents' do
2756
2615
  expect(preferences).to eq([ preference_one ])
2757
2616
  end
2758
2617
  end
2759
2618
 
2760
- context "when chaining criteria" do
2761
-
2619
+ context 'when chaining criteria' do
2762
2620
  let(:preferences) do
2763
- person.preferences.posting.where(:name.in => [ "First" ])
2621
+ person.preferences.posting.where(:name.in => [ 'First' ])
2764
2622
  end
2765
2623
 
2766
- it "applies the criteria to the documents" do
2624
+ it 'applies the criteria to the documents' do
2767
2625
  expect(preferences).to eq([ preference_one ])
2768
2626
  end
2769
2627
  end
2770
2628
 
2771
- context "when delegating methods" do
2772
-
2773
- describe "#distinct" do
2774
-
2629
+ context 'when delegating methods' do
2630
+ describe '#distinct' do
2775
2631
  let(:values) do
2776
2632
  person.preferences.distinct(:name)
2777
2633
  end
2778
2634
 
2779
- it "returns the distinct values for the fields" do
2780
- expect(values).to include("First")
2781
- 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')
2782
2638
  end
2783
2639
 
2784
- context "when the inverse relation is not defined" do
2785
-
2640
+ context 'when the inverse relation is not defined' do
2786
2641
  let!(:house) do
2787
- person.houses.create!(name: "Wayne Manor")
2642
+ person.houses.create!(name: 'Wayne Manor')
2788
2643
  end
2789
2644
 
2790
- it "returns the distinct values for the fields" do
2645
+ it 'returns the distinct values for the fields' do
2791
2646
  expect(person.houses.distinct(:name)).to eq([ house.name ])
2792
2647
  end
2793
2648
  end
@@ -2795,11 +2650,13 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2795
2650
  end
2796
2651
  end
2797
2652
 
2798
- describe "#min" do
2799
-
2653
+ describe '#min' do
2800
2654
  let(:person) do
2801
2655
  Person.create!
2802
2656
  end
2657
+ let(:min) do
2658
+ person.preferences.min_by(&:ranking)
2659
+ end
2803
2660
 
2804
2661
  let(:preference_one) do
2805
2662
  Preference.create!(ranking: 5)
@@ -2813,22 +2670,18 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2813
2670
  person.preferences.push(preference_one, preference_two)
2814
2671
  end
2815
2672
 
2816
- let(:min) do
2817
- person.preferences.min do |a, b|
2818
- a.ranking <=> b.ranking
2819
- end
2820
- end
2821
-
2822
- it "returns the min value of the supplied field" do
2673
+ it 'returns the min value of the supplied field' do
2823
2674
  expect(min).to eq(preference_one)
2824
2675
  end
2825
2676
  end
2826
2677
 
2827
- describe "#min_by" do
2828
-
2678
+ describe '#min_by' do
2829
2679
  let(:person) do
2830
2680
  Person.create!
2831
2681
  end
2682
+ let(:min) do
2683
+ person.preferences.min_by(&:ranking)
2684
+ end
2832
2685
 
2833
2686
  let(:preference_one) do
2834
2687
  Preference.create!(ranking: 5)
@@ -2842,71 +2695,64 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2842
2695
  person.preferences.push(preference_one, preference_two)
2843
2696
  end
2844
2697
 
2845
- let(:min) do
2846
- person.preferences.min_by(&:ranking)
2847
- end
2848
-
2849
- it "returns the min value of the supplied field" do
2698
+ it 'returns the min value of the supplied field' do
2850
2699
  expect(min).to eq(preference_one)
2851
2700
  end
2852
2701
  end
2853
2702
 
2854
- describe "#nil?" do
2855
-
2856
- it "returns false" do
2857
- 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
2858
2706
  end
2859
2707
  end
2860
2708
 
2861
- describe "#nullify_all" do
2862
-
2709
+ describe '#nullify_all' do
2863
2710
  let(:person) do
2864
2711
  Person.create!
2865
2712
  end
2866
2713
 
2867
2714
  let!(:preference_one) do
2868
- person.preferences.create!(name: "One")
2715
+ person.preferences.create!(name: 'One')
2869
2716
  end
2870
2717
 
2871
2718
  let!(:preference_two) do
2872
- person.preferences.create!(name: "Two")
2719
+ person.preferences.create!(name: 'Two')
2873
2720
  end
2874
2721
 
2875
2722
  before do
2876
2723
  person.preferences.nullify_all
2877
2724
  end
2878
2725
 
2879
- it "removes the foreign key from the base document" do
2726
+ it 'removes the foreign key from the base document' do
2880
2727
  [ preference_one, preference_two ].each do |preference|
2881
- expect(person.preference_ids).to_not include(preference.id)
2728
+ expect(person.preference_ids).not_to include(preference.id)
2882
2729
  end
2883
2730
  end
2884
2731
 
2885
- it "removes the foreign key from the target documents" do
2732
+ it 'removes the foreign key from the target documents' do
2886
2733
  [ preference_one, preference_two ].each do |preference|
2887
- expect(preference.person_ids).to_not include(person.id)
2734
+ expect(preference.person_ids).not_to include(person.id)
2888
2735
  end
2889
2736
  end
2890
2737
 
2891
- it "removes the reference from the base document" do
2738
+ it 'removes the reference from the base document' do
2892
2739
  [ preference_one, preference_two ].each do |preference|
2893
- expect(person.preferences).to_not include(preference)
2740
+ expect(person.preferences).not_to include(preference)
2894
2741
  end
2895
2742
  end
2896
2743
 
2897
- it "removes the reference from the target documents" do
2744
+ it 'removes the reference from the target documents' do
2898
2745
  [ preference_one, preference_two ].each do |preference|
2899
- expect(preference.people).to_not include(person)
2746
+ expect(preference.people).not_to include(person)
2900
2747
  end
2901
2748
  end
2902
2749
 
2903
- it "saves the documents" do
2904
- 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)
2905
2752
  end
2906
2753
  end
2907
2754
 
2908
- describe "#respond_to?" do
2909
-
2755
+ describe '#respond_to?' do
2910
2756
  let(:person) do
2911
2757
  Person.new
2912
2758
  end
@@ -2916,90 +2762,78 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2916
2762
  end
2917
2763
 
2918
2764
  Array.public_instance_methods.sort.each do |method|
2919
-
2920
2765
  context "when checking Array##{method}" do
2921
-
2922
2766
  before do
2923
2767
  expect([].respond_to?(method)).to be true
2924
2768
  end
2925
2769
 
2926
- it "returns true" do
2770
+ it 'returns true' do
2927
2771
  expect(preferences.respond_to?(method)).to be true
2928
2772
  end
2929
2773
  end
2930
2774
  end
2931
2775
 
2932
2776
  Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy.public_instance_methods.sort.each do |method|
2933
-
2934
2777
  context "when checking Proxy##{method}" do
2935
-
2936
- it "returns true" do
2778
+ it 'returns true' do
2937
2779
  expect(preferences.respond_to?(method)).to be true
2938
2780
  end
2939
2781
  end
2940
2782
  end
2941
2783
 
2942
2784
  Preference.scopes.keys.sort.each do |method|
2943
-
2944
2785
  context "when checking scope #{method}" do
2945
-
2946
- it "returns true" do
2786
+ it 'returns true' do
2947
2787
  expect(preferences.respond_to?(method)).to be true
2948
2788
  end
2949
2789
  end
2950
2790
  end
2951
2791
  end
2952
2792
 
2953
- describe "#scoped" do
2954
-
2793
+ describe '#scoped' do
2955
2794
  let(:scoped) do
2956
2795
  person.preferences.scoped
2957
2796
  end
2958
2797
 
2959
2798
  context 'when association is empty' do
2960
-
2961
2799
  let(:person) do
2962
2800
  Person.new
2963
2801
  end
2964
2802
 
2965
- it "returns the relation criteria" do
2803
+ it 'returns the relation criteria' do
2966
2804
  expect(scoped).to be_a(Mongoid::Criteria)
2967
2805
  end
2968
2806
 
2969
- it "returns with an empty selector" do
2970
- expect(scoped.selector).to eq("_id" => { "$in" => [] })
2807
+ it 'returns with an empty selector' do
2808
+ expect(scoped.selector).to eq('_id' => { '$in' => [] })
2971
2809
  end
2972
2810
  end
2973
2811
 
2974
2812
  context 'when association is not empty' do
2975
-
2976
2813
  let(:person) do
2977
2814
  Person.create!(preferences: [
2978
- Preference.new(id: 123),
2979
- ])
2815
+ Preference.new(id: 123)
2816
+ ])
2980
2817
  end
2981
2818
 
2982
- it "returns with a selector including association element ids" do
2983
- 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 ] })
2984
2821
  end
2985
2822
  end
2986
2823
  end
2987
2824
 
2988
- [ :size, :length ].each do |method|
2989
-
2825
+ %i[size length].each do |method|
2990
2826
  describe "##{method}" do
2991
-
2992
2827
  let(:person) do
2993
2828
  Person.create!
2994
2829
  end
2995
2830
 
2996
- context "when documents have been persisted" do
2997
-
2831
+ context 'when documents have been persisted' do
2998
2832
  let!(:preference) do
2999
- person.preferences.create!(name: "Testing")
2833
+ person.preferences.create!(name: 'Testing')
3000
2834
  end
3001
2835
 
3002
- it "returns the number of documents" do
2836
+ it 'returns the number of documents' do
3003
2837
  expect(person.preferences.send(method)).to eq(1)
3004
2838
  end
3005
2839
  end
@@ -3032,238 +2866,225 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3032
2866
  end
3033
2867
  end
3034
2868
 
3035
- context "when documents have not been persisted" do
3036
-
2869
+ context 'when documents have not been persisted' do
3037
2870
  before do
3038
- person.preferences.build(name: "Test")
3039
- person.preferences.create!(name: "Test 2")
2871
+ person.preferences.build(name: 'Test')
2872
+ person.preferences.create!(name: 'Test 2')
3040
2873
  end
3041
2874
 
3042
- it "returns the total number of documents" do
2875
+ it 'returns the total number of documents' do
3043
2876
  expect(person.preferences.send(method)).to eq(2)
3044
2877
  end
3045
2878
  end
3046
2879
  end
3047
2880
  end
3048
2881
 
3049
- describe "#unscoped" do
3050
-
3051
- context "when the relation has no default scope" do
3052
-
2882
+ describe '#unscoped' do
2883
+ context 'when the relation has no default scope' do
3053
2884
  let!(:person) do
3054
2885
  Person.create!
3055
2886
  end
3056
2887
 
3057
2888
  let!(:preference_one) do
3058
- person.preferences.create!(name: "first")
2889
+ person.preferences.create!(name: 'first')
3059
2890
  end
3060
2891
 
3061
2892
  let!(:preference_two) do
3062
- Preference.create!(name: "second")
2893
+ Preference.create!(name: 'second')
3063
2894
  end
3064
2895
 
3065
2896
  let(:unscoped) do
3066
2897
  person.preferences.unscoped
3067
2898
  end
3068
2899
 
3069
- it "returns only the associated documents" do
2900
+ it 'returns only the associated documents' do
3070
2901
  expect(unscoped).to eq([ preference_one ])
3071
2902
  end
3072
2903
  end
3073
2904
 
3074
- context "when the relation has a default scope" do
3075
-
2905
+ context 'when the relation has a default scope' do
3076
2906
  let!(:person) do
3077
2907
  Person.create!
3078
2908
  end
3079
2909
 
3080
2910
  let!(:house_one) do
3081
- person.houses.create!(name: "first")
2911
+ person.houses.create!(name: 'first')
3082
2912
  end
3083
2913
 
3084
2914
  let!(:house_two) do
3085
- House.create!(name: "second")
2915
+ House.create!(name: 'second')
3086
2916
  end
3087
2917
 
3088
2918
  let(:unscoped) do
3089
2919
  person.houses.unscoped
3090
2920
  end
3091
2921
 
3092
- it "only returns associated documents" do
2922
+ it 'only returns associated documents' do
3093
2923
  expect(unscoped).to eq([ house_one ])
3094
2924
  end
3095
2925
 
3096
- it "removes the default scoping options" do
2926
+ it 'removes the default scoping options' do
3097
2927
  expect(unscoped.options).to eq({})
3098
2928
  end
3099
2929
  end
3100
2930
  end
3101
2931
 
3102
- context "when setting the ids directly after the documents" do
3103
-
2932
+ context 'when setting the ids directly after the documents' do
3104
2933
  let!(:person) do
3105
- Person.create!(title: "The Boss")
2934
+ Person.create!(title: 'The Boss')
3106
2935
  end
3107
2936
 
3108
2937
  let!(:girlfriend_house) do
3109
- House.create!(name: "Girlfriend")
2938
+ House.create!(name: 'Girlfriend')
3110
2939
  end
3111
2940
 
3112
2941
  let!(:wife_house) do
3113
- House.create!(name: "Wife")
2942
+ House.create!(name: 'Wife')
3114
2943
  end
3115
2944
 
3116
2945
  let!(:exwife_house) do
3117
- House.create!(name: "Ex-Wife")
2946
+ House.create!(name: 'Ex-Wife')
3118
2947
  end
3119
2948
 
3120
2949
  before do
3121
2950
  person.update_attributes!(
3122
- houses: [ wife_house, exwife_house, girlfriend_house ]
2951
+ houses: [ wife_house, exwife_house, girlfriend_house ]
3123
2952
  )
3124
2953
  person.update_attributes!(house_ids: [ girlfriend_house.id ])
3125
2954
  end
3126
2955
 
3127
- context "when reloading" do
3128
-
3129
- it "properly sets the references" do
2956
+ context 'when reloading' do
2957
+ it 'properly sets the references' do
3130
2958
  expect(person.houses(true)).to eq([ girlfriend_house ])
3131
2959
  end
3132
2960
  end
3133
2961
  end
3134
2962
 
3135
- context "when setting both sides in a single call" do
3136
-
3137
- context "when the documents are new" do
3138
-
2963
+ context 'when setting both sides in a single call' do
2964
+ context 'when the documents are new' do
3139
2965
  let(:user) do
3140
- User.new(name: "testing")
2966
+ User.new(name: 'testing')
3141
2967
  end
3142
2968
 
3143
2969
  let(:business) do
3144
- Business.new(name: "serious", owners: [ user ])
2970
+ Business.new(name: 'serious', owners: [ user ])
3145
2971
  end
3146
2972
 
3147
2973
  before do
3148
2974
  user.businesses = [ business ]
3149
2975
  end
3150
2976
 
3151
- it "sets the businesses" do
2977
+ it 'sets the businesses' do
3152
2978
  expect(user.businesses).to eq([ business ])
3153
2979
  end
3154
2980
 
3155
- it "sets the inverse users" do
2981
+ it 'sets the inverse users' do
3156
2982
  expect(user.businesses.first.owners.first).to eq(user)
3157
2983
  end
3158
2984
 
3159
- it "sets the inverse businesses" do
2985
+ it 'sets the inverse businesses' do
3160
2986
  expect(business.owners).to eq([ user ])
3161
2987
  end
3162
2988
  end
3163
2989
 
3164
- context "when one side is persisted" do
3165
-
2990
+ context 'when one side is persisted' do
3166
2991
  let!(:user) do
3167
- User.new(name: "testing")
2992
+ User.new(name: 'testing')
3168
2993
  end
3169
2994
 
3170
2995
  let!(:business) do
3171
- Business.create!(name: "serious", owners: [ user ])
2996
+ Business.create!(name: 'serious', owners: [ user ])
3172
2997
  end
3173
2998
 
3174
2999
  before do
3175
3000
  user.businesses = [ business ]
3176
3001
  end
3177
3002
 
3178
- it "sets the businesses" do
3003
+ it 'sets the businesses' do
3179
3004
  expect(user.businesses).to eq([ business ])
3180
3005
  end
3181
3006
 
3182
- it "sets the inverse users" do
3007
+ it 'sets the inverse users' do
3183
3008
  expect(user.businesses.first.owners.first).to eq(user)
3184
3009
  end
3185
3010
 
3186
- it "sets the inverse businesses" do
3011
+ it 'sets the inverse businesses' do
3187
3012
  expect(business.owners).to eq([ user ])
3188
3013
  end
3189
3014
 
3190
- context "when reloading" do
3191
-
3015
+ context 'when reloading' do
3192
3016
  before do
3193
3017
  user.reload
3194
3018
  business.reload
3195
3019
  end
3196
3020
 
3197
- it "persists the businesses" do
3021
+ it 'persists the businesses' do
3198
3022
  expect(user.businesses).to eq([ business ])
3199
3023
  end
3200
3024
 
3201
- it "persists the inverse users" do
3025
+ it 'persists the inverse users' do
3202
3026
  expect(user.businesses.first.owners.first).to eq(user)
3203
3027
  end
3204
3028
 
3205
- it "persists the inverse businesses" do
3029
+ it 'persists the inverse businesses' do
3206
3030
  expect(business.owners).to eq([ user ])
3207
3031
  end
3208
3032
  end
3209
3033
  end
3210
3034
 
3211
- context "when the documents are persisted" do
3212
-
3035
+ context 'when the documents are persisted' do
3213
3036
  let(:user) do
3214
- User.create!(name: "tst")
3037
+ User.create!(name: 'tst')
3215
3038
  end
3216
3039
 
3217
3040
  let(:business) do
3218
- Business.create!(name: "srs", owners: [ user ])
3041
+ Business.create!(name: 'srs', owners: [ user ])
3219
3042
  end
3220
3043
 
3221
3044
  before do
3222
3045
  user.businesses = [ business ]
3223
3046
  end
3224
3047
 
3225
- it "sets the businesses" do
3048
+ it 'sets the businesses' do
3226
3049
  expect(user.businesses).to eq([ business ])
3227
3050
  end
3228
3051
 
3229
- it "sets the inverse users" do
3052
+ it 'sets the inverse users' do
3230
3053
  expect(user.businesses.first.owners.first).to eq(user)
3231
3054
  end
3232
3055
 
3233
- it "sets the inverse businesses" do
3056
+ it 'sets the inverse businesses' do
3234
3057
  expect(business.owners).to eq([ user ])
3235
3058
  end
3236
3059
 
3237
- context "when reloading" do
3238
-
3060
+ context 'when reloading' do
3239
3061
  before do
3240
3062
  user.reload
3241
3063
  business.reload
3242
3064
  end
3243
3065
 
3244
- it "persists the businesses" do
3066
+ it 'persists the businesses' do
3245
3067
  expect(user.businesses).to eq([ business ])
3246
3068
  end
3247
3069
 
3248
- it "persists the inverse users" do
3070
+ it 'persists the inverse users' do
3249
3071
  expect(user.businesses.first.owners.first).to eq(user)
3250
3072
  end
3251
3073
 
3252
- it "persists the inverse businesses" do
3074
+ it 'persists the inverse businesses' do
3253
3075
  expect(business.owners).to eq([ user ])
3254
3076
  end
3255
3077
  end
3256
3078
  end
3257
3079
  end
3258
3080
 
3259
- context "when binding the relation multiple times" do
3260
-
3081
+ context 'when binding the relation multiple times' do
3261
3082
  let(:person) do
3262
3083
  Person.create!
3263
3084
  end
3264
3085
 
3265
3086
  let(:preference) do
3266
- person.preferences.create!(name: "testing")
3087
+ person.preferences.create!(name: 'testing')
3267
3088
  end
3268
3089
 
3269
3090
  before do
@@ -3274,13 +3095,12 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3274
3095
  end
3275
3096
  end
3276
3097
 
3277
- it "does not duplicate foreign keys" do
3098
+ it 'does not duplicate foreign keys' do
3278
3099
  expect(person.preference_ids).to eq([ preference.id ])
3279
3100
  end
3280
3101
  end
3281
3102
 
3282
- context "when the association has order criteria" do
3283
-
3103
+ context 'when the association has order criteria' do
3284
3104
  let(:person) do
3285
3105
  Person.create!
3286
3106
  end
@@ -3302,21 +3122,21 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3302
3122
  person.ordered_preferences.push(preference_one, preference_two, preference_three)
3303
3123
  end
3304
3124
 
3305
- it "orders the documents" do
3125
+ it 'orders the documents' do
3306
3126
  expect(person.ordered_preferences(true)).to eq(
3307
- [preference_two, preference_three, preference_one]
3308
- )
3127
+ [ preference_two, preference_three, preference_one ]
3128
+ )
3309
3129
  end
3310
3130
 
3311
- it "chains default criteria with additional" do
3131
+ it 'chains default criteria with additional' do
3312
3132
  expect(person.ordered_preferences.order_by(:name.desc).to_a).to eq(
3313
- [preference_three, preference_two, preference_one]
3314
- )
3133
+ [ preference_three, preference_two,
3134
+ preference_one ]
3135
+ )
3315
3136
  end
3316
3137
  end
3317
3138
 
3318
- context "when the parent is not a new record and freshly loaded" do
3319
-
3139
+ context 'when the parent is not a new record and freshly loaded' do
3320
3140
  let(:person) do
3321
3141
  Person.create!
3322
3142
  end
@@ -3332,57 +3152,54 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3332
3152
  person.preferences = nil
3333
3153
  end
3334
3154
 
3335
- it "sets the relation to an empty array" do
3155
+ it 'sets the relation to an empty array' do
3336
3156
  expect(person.preferences).to be_empty
3337
3157
  end
3338
3158
 
3339
- it "removes the foreign key values" do
3159
+ it 'removes the foreign key values' do
3340
3160
  expect(person.preference_ids).to be_empty
3341
3161
  end
3342
3162
 
3343
- it "does not delete the target from the database" do
3344
- expect {
3163
+ it 'does not delete the target from the database' do
3164
+ expect do
3345
3165
  preference.reload
3346
- }.not_to raise_error
3166
+ end.not_to raise_error
3347
3167
  end
3348
3168
  end
3349
3169
 
3350
- context "when reloading the relation" do
3351
-
3170
+ context 'when reloading the relation' do
3352
3171
  let!(:person) do
3353
3172
  Person.create!
3354
3173
  end
3355
3174
 
3356
3175
  let!(:preference_one) do
3357
- Preference.create!(name: "one")
3176
+ Preference.create!(name: 'one')
3358
3177
  end
3359
3178
 
3360
3179
  let!(:preference_two) do
3361
- Preference.create!(name: "two")
3180
+ Preference.create!(name: 'two')
3362
3181
  end
3363
3182
 
3364
3183
  before do
3365
3184
  person.preferences << preference_one
3366
3185
  end
3367
3186
 
3368
- context "when the relation references the same documents" do
3369
-
3187
+ context 'when the relation references the same documents' do
3370
3188
  before do
3371
- Preference.collection.find({ _id: preference_one.id }).
3372
- update_one({ "$set" => { name: "reloaded" }})
3189
+ Preference.collection.find({ _id: preference_one.id })
3190
+ .update_one({ '$set' => { name: 'reloaded' } })
3373
3191
  end
3374
3192
 
3375
3193
  let(:reloaded) do
3376
3194
  person.preferences(true)
3377
3195
  end
3378
3196
 
3379
- it "reloads the document from the database" do
3380
- expect(reloaded.first.name).to eq("reloaded")
3197
+ it 'reloads the document from the database' do
3198
+ expect(reloaded.first.name).to eq('reloaded')
3381
3199
  end
3382
3200
  end
3383
3201
 
3384
- context "when the relation references different documents" do
3385
-
3202
+ context 'when the relation references different documents' do
3386
3203
  before do
3387
3204
  person.preferences << preference_two
3388
3205
  end
@@ -3391,18 +3208,16 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3391
3208
  person.preferences(true)
3392
3209
  end
3393
3210
 
3394
- it "reloads the new document from the database" do
3211
+ it 'reloads the new document from the database' do
3395
3212
  expect(reloaded).to eq([ preference_one, preference_two ])
3396
3213
  end
3397
3214
  end
3398
3215
  end
3399
3216
 
3400
- context "when adding to a relation via a field setter" do
3401
-
3402
- context "when the document is new" do
3403
-
3217
+ context 'when adding to a relation via a field setter' do
3218
+ context 'when the document is new' do
3404
3219
  let!(:person) do
3405
- Person.create!(preference_names: "one, two")
3220
+ Person.create!(preference_names: 'one, two')
3406
3221
  end
3407
3222
 
3408
3223
  let(:preference_one) do
@@ -3413,36 +3228,35 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3413
3228
  person.reload.preferences.last
3414
3229
  end
3415
3230
 
3416
- it "persists the first preference" do
3417
- expect(preference_one).to_not be_nil
3231
+ it 'persists the first preference' do
3232
+ expect(preference_one).not_to be_nil
3418
3233
  end
3419
3234
 
3420
- it "sets the first inverse" do
3235
+ it 'sets the first inverse' do
3421
3236
  expect(preference_one.people).to eq([ person ])
3422
3237
  end
3423
3238
 
3424
- it "persists the second preference" do
3425
- expect(preference_two).to_not be_nil
3239
+ it 'persists the second preference' do
3240
+ expect(preference_two).not_to be_nil
3426
3241
  end
3427
3242
 
3428
- it "sets the second inverse keys" do
3243
+ it 'sets the second inverse keys' do
3429
3244
  expect(preference_two.people).to eq([ person ])
3430
3245
  end
3431
3246
  end
3432
3247
  end
3433
3248
 
3434
- context "when changing the order of existing ids" do
3435
-
3249
+ context 'when changing the order of existing ids' do
3436
3250
  let(:person) do
3437
3251
  Person.new
3438
3252
  end
3439
3253
 
3440
3254
  let(:preference_one) do
3441
- Preference.create!(name: "one")
3255
+ Preference.create!(name: 'one')
3442
3256
  end
3443
3257
 
3444
3258
  let(:preference_two) do
3445
- Preference.create!(name: "two")
3259
+ Preference.create!(name: 'two')
3446
3260
  end
3447
3261
 
3448
3262
  before do
@@ -3450,8 +3264,7 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3450
3264
  person.save!
3451
3265
  end
3452
3266
 
3453
- context "and the order is changed" do
3454
-
3267
+ context 'and the order is changed' do
3455
3268
  before do
3456
3269
  person.preference_ids = [ preference_two.id, preference_one.id ]
3457
3270
  person.save!
@@ -3461,65 +3274,60 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3461
3274
  Person.find(person.id)
3462
3275
  end
3463
3276
 
3464
- it "persists the change in id order" do
3277
+ it 'persists the change in id order' do
3465
3278
  expect(reloaded.preference_ids).to eq(
3466
- [ preference_two.id, preference_one.id ]
3467
- )
3279
+ [ preference_two.id, preference_one.id ]
3280
+ )
3468
3281
  end
3469
3282
  end
3470
3283
 
3471
- context "and the order is changed and an element is added" do
3472
-
3284
+ context 'and the order is changed and an element is added' do
3473
3285
  let(:preference_three) do
3474
- Preference.create!(name: "three")
3286
+ Preference.create!(name: 'three')
3287
+ end
3288
+ let(:reloaded) do
3289
+ Person.find(person.id)
3475
3290
  end
3476
3291
 
3477
3292
  before do
3478
3293
  person.preference_ids =
3479
- [ preference_two.id, preference_one.id, preference_three.id ]
3294
+ [ preference_two.id, preference_one.id, preference_three.id ]
3480
3295
  person.save!
3481
3296
  end
3482
3297
 
3483
- let(:reloaded) do
3484
- Person.find(person.id)
3485
- end
3486
-
3487
- it "also persists the change in id order" do
3298
+ it 'also persists the change in id order' do
3488
3299
  expect(reloaded.preference_ids).to eq(
3489
- [ preference_two.id, preference_one.id, preference_three.id ]
3490
- )
3300
+ [ preference_two.id, preference_one.id, preference_three.id ]
3301
+ )
3491
3302
  end
3492
3303
  end
3493
3304
 
3494
- context "and the order is changed and an element is removed" do
3495
-
3305
+ context 'and the order is changed and an element is removed' do
3496
3306
  let(:preference_three) do
3497
- Preference.create!(name: "three")
3307
+ Preference.create!(name: 'three')
3308
+ end
3309
+ let(:reloaded) do
3310
+ Person.find(person.id)
3498
3311
  end
3499
3312
 
3500
3313
  before do
3501
3314
  person.preference_ids =
3502
- [ preference_one.id, preference_two.id, preference_three.id ]
3315
+ [ preference_one.id, preference_two.id, preference_three.id ]
3503
3316
  person.save!
3504
3317
  person.preference_ids =
3505
- [ preference_three.id, preference_two.id ]
3318
+ [ preference_three.id, preference_two.id ]
3506
3319
  person.save!
3507
3320
  end
3508
3321
 
3509
- let(:reloaded) do
3510
- Person.find(person.id)
3511
- end
3512
-
3513
- it "also persists the change in id order" do
3322
+ it 'also persists the change in id order' do
3514
3323
  expect(reloaded.preference_ids).to eq(
3515
- [ preference_three.id, preference_two.id ]
3516
- )
3324
+ [ preference_three.id, preference_two.id ]
3325
+ )
3517
3326
  end
3518
3327
  end
3519
3328
  end
3520
3329
 
3521
- context "when adding a document" do
3522
-
3330
+ context 'when adding a document' do
3523
3331
  let(:person) do
3524
3332
  Person.new
3525
3333
  end
@@ -3532,8 +3340,7 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3532
3340
  person.preferences.push(preference_one)
3533
3341
  end
3534
3342
 
3535
- context "when chaining a second add" do
3536
-
3343
+ context 'when chaining a second add' do
3537
3344
  let(:preference_two) do
3538
3345
  Preference.new
3539
3346
  end
@@ -3542,16 +3349,14 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3542
3349
  first_add.push(preference_two)
3543
3350
  end
3544
3351
 
3545
- it "adds both documents" do
3352
+ it 'adds both documents' do
3546
3353
  expect(result).to eq([ preference_one, preference_two ])
3547
3354
  end
3548
3355
  end
3549
3356
  end
3550
3357
 
3551
- context "when setting the relation via the foreign key" do
3552
-
3553
- context "when the relation exists" do
3554
-
3358
+ context 'when setting the relation via the foreign key' do
3359
+ context 'when the relation exists' do
3555
3360
  let!(:person) do
3556
3361
  Person.create!
3557
3362
  end
@@ -3568,14 +3373,13 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3568
3373
  person.preference_ids = [ pref_two.id ]
3569
3374
  end
3570
3375
 
3571
- it "sets the new documents on the relation" do
3376
+ it 'sets the new documents on the relation' do
3572
3377
  expect(person.preferences).to eq([ pref_two ])
3573
3378
  end
3574
3379
  end
3575
3380
  end
3576
3381
 
3577
- context "when using a different primary key" do
3578
-
3382
+ context 'when using a different primary key' do
3579
3383
  let(:dog) do
3580
3384
  Dog.create!(name: 'Doggie')
3581
3385
  end
@@ -3592,72 +3396,67 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3592
3396
  FireHydrant.create!(location: '221B Baker Street')
3593
3397
  end
3594
3398
 
3595
- context "when adding to a one-way many to many" do
3596
-
3399
+ context 'when adding to a one-way many to many' do
3597
3400
  before do
3598
3401
  fire_hydrant.cats.push(cat)
3599
3402
  end
3600
3403
 
3601
- it "adds the pk value to the fk set" do
3602
- 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 ])
3603
3406
  end
3604
3407
  end
3605
3408
 
3606
- context "when adding multiple documents to a one-way many to many" do
3607
-
3409
+ context 'when adding multiple documents to a one-way many to many' do
3608
3410
  before do
3609
- fire_hydrant.cats.push([cat, another_cat])
3411
+ fire_hydrant.cats.push([ cat, another_cat ])
3610
3412
  end
3611
3413
 
3612
- it "adds the pk values to the fk set" do
3613
- 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 ])
3614
3416
  end
3615
3417
  end
3616
3418
 
3617
- context "when adding a new document to a one-way many to many" do
3618
-
3419
+ context 'when adding a new document to a one-way many to many' do
3619
3420
  before do
3620
3421
  fire_hydrant.cats.build name: 'Kitten'
3621
3422
  end
3622
3423
 
3623
- it "adds the pk values to the fk set" do
3624
- 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' ])
3625
3426
  end
3626
3427
  end
3627
3428
 
3628
- context "when adding to a two-way many to many" do
3629
-
3429
+ context 'when adding to a two-way many to many' do
3630
3430
  before do
3631
3431
  fire_hydrant.dogs.push(dog)
3632
3432
  end
3633
3433
 
3634
- it "adds the pk value to the fk set" do
3635
- 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 ])
3636
3436
  end
3637
3437
 
3638
- it "adds the base pk value to the inverse fk set" do
3639
- 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 ])
3640
3440
  end
3641
3441
  end
3642
3442
 
3643
- context "when deleting from a two-way many to many" do
3644
-
3443
+ context 'when deleting from a two-way many to many' do
3645
3444
  before do
3646
3445
  dog.fire_hydrants.push(fire_hydrant)
3647
3446
  fire_hydrant.dogs.delete(dog)
3648
3447
  end
3649
3448
 
3650
- it "removes the pk value from the fk set" do
3449
+ it 'removes the pk value from the fk set' do
3651
3450
  expect(fire_hydrant.dog_ids).to eq([])
3652
3451
  end
3653
3452
 
3654
- 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
3655
3454
  expect(dog.fire_hydrant_ids).to eq([])
3656
3455
  end
3657
3456
  end
3658
3457
  end
3659
3458
 
3660
- context "HABTM" do
3459
+ context 'HABTM' do
3661
3460
  before do
3662
3461
  class Project
3663
3462
  include Mongoid::Document
@@ -3680,18 +3479,18 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3680
3479
  end
3681
3480
  end
3682
3481
 
3683
- it "should assign relation from both sides" do
3482
+ it 'assigns relation from both sides' do
3684
3483
  p1 = Project.create! name: 'Foo'
3685
3484
  p2 = Project.create! name: 'Bar'
3686
3485
  d1 = Distributor.create! name: 'Rock'
3687
3486
  d2 = Distributor.create! name: 'Soul'
3688
3487
 
3689
3488
  p1.distributors << d1
3690
- expect(p1.d_ids).to match_array([d1.id])
3691
- 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)
3692
3491
  d2.projects << p2
3693
- expect(d2.p_ids).to match_array([p2.id])
3694
- 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)
3695
3494
  end
3696
3495
  end
3697
3496
 
@@ -3699,24 +3498,24 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3699
3498
  let!(:contract) { HabtmmContract.create! }
3700
3499
  let!(:signature) { contract.signatures.create! }
3701
3500
 
3702
- it "is nil" do
3703
- expect(signature.favorite_signature).to be nil
3501
+ it 'is nil' do
3502
+ expect(signature.favorite_signature).to be_nil
3704
3503
  end
3705
3504
  end
3706
3505
 
3707
- 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
3708
3507
  let!(:contract) { HabtmmContract.create! }
3709
- let!(:signature) { HabtmmSignature.create!(contracts: [contract]) }
3508
+ let!(:signature) { HabtmmSignature.create!(contracts: [ contract ]) }
3710
3509
 
3711
- it "populates the inverse foreign key" do
3712
- 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 ])
3713
3512
  end
3714
3513
  end
3715
3514
 
3716
- context "when there is a foreign key in the aliased associations" do
3717
- it "has the correct aliases" do
3718
- expect(Dog.aliased_associations["breed_ids"]).to eq("breeds")
3719
- 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')
3720
3519
  end
3721
3520
  end
3722
3521
 
@@ -3729,21 +3528,18 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3729
3528
  # when retrieving a document from the database, the attributes hash is actually
3730
3529
  # a BSON::Document, which applies a transformation to the array before
3731
3530
  # storing it.
3732
- 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
3733
3532
  before do
3734
3533
  HabtmmContract.create!
3735
3534
  HabtmmSignature.create!
3535
+ contract.signature_ids.concat([ signature.id ])
3736
3536
  end
3737
3537
 
3738
3538
  let!(:contract) { HabtmmContract.first }
3739
3539
  let!(:signature) { HabtmmSignature.first }
3740
3540
 
3741
- before do
3742
- contract.signature_ids.concat([signature.id])
3743
- end
3744
-
3745
- it "works on the first attempt" do
3746
- 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 ])
3747
3543
  end
3748
3544
  end
3749
3545
  end