mongoid 8.1.5 → 9.0.0

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