mongoid 7.0.13 → 9.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (1421) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +41 -41
  3. data/LICENSE +1 -1
  4. data/README.md +29 -25
  5. data/Rakefile +150 -40
  6. data/lib/config/locales/en.yml +364 -108
  7. data/lib/mongoid/association/accessors.rb +205 -107
  8. data/lib/mongoid/association/bindable.rb +102 -56
  9. data/lib/mongoid/association/builders.rb +24 -26
  10. data/lib/mongoid/association/constrainable.rb +8 -11
  11. data/lib/mongoid/association/depending.rb +65 -59
  12. data/lib/mongoid/association/eager.rb +203 -0
  13. data/lib/mongoid/association/eager_load/discriminated_inclusion.rb +88 -0
  14. data/lib/mongoid/association/eager_load/embedded_distributor.rb +151 -0
  15. data/lib/mongoid/association/eager_load/inclusion.rb +173 -0
  16. data/lib/mongoid/association/eager_load/inclusion_tree.rb +96 -0
  17. data/lib/mongoid/association/eager_load/lookup_pipeline.rb +121 -0
  18. data/lib/mongoid/association/eager_load/polymorphic_preloader.rb +61 -0
  19. data/lib/mongoid/association/eager_load/polymorphic_targets.rb +151 -0
  20. data/lib/mongoid/association/eager_loadable.rb +135 -15
  21. data/lib/mongoid/association/embedded/batchable.rb +87 -75
  22. data/lib/mongoid/association/embedded/cyclic.rb +16 -25
  23. data/lib/mongoid/association/embedded/eager.rb +18 -0
  24. data/lib/mongoid/association/embedded/embedded_in/binding.rb +30 -14
  25. data/lib/mongoid/association/embedded/embedded_in/buildable.rb +10 -7
  26. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +39 -32
  27. data/lib/mongoid/association/embedded/embedded_in.rb +37 -46
  28. data/lib/mongoid/association/embedded/embeds_many/binding.rb +7 -11
  29. data/lib/mongoid/association/embedded/embeds_many/buildable.rb +11 -7
  30. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +309 -243
  31. data/lib/mongoid/association/embedded/embeds_many.rb +32 -56
  32. data/lib/mongoid/association/embedded/embeds_one/binding.rb +6 -11
  33. data/lib/mongoid/association/embedded/embeds_one/buildable.rb +25 -11
  34. data/lib/mongoid/association/embedded/embeds_one/proxy.rb +145 -53
  35. data/lib/mongoid/association/embedded/embeds_one.rb +30 -50
  36. data/lib/mongoid/association/embedded.rb +3 -0
  37. data/lib/mongoid/association/macros.rb +97 -56
  38. data/lib/mongoid/association/many.rb +99 -36
  39. data/lib/mongoid/association/marshalable.rb +5 -7
  40. data/lib/mongoid/association/nested/many.rb +86 -47
  41. data/lib/mongoid/association/nested/nested_buildable.rb +58 -21
  42. data/lib/mongoid/association/nested/one.rb +81 -36
  43. data/lib/mongoid/association/nested.rb +4 -5
  44. data/lib/mongoid/association/one.rb +5 -14
  45. data/lib/mongoid/association/options.rb +48 -53
  46. data/lib/mongoid/association/proxy.rb +65 -54
  47. data/lib/mongoid/association/referenced/auto_save.rb +59 -20
  48. data/lib/mongoid/association/referenced/belongs_to/binding.rb +21 -22
  49. data/lib/mongoid/association/referenced/belongs_to/buildable.rb +32 -11
  50. data/lib/mongoid/association/referenced/belongs_to/eager.rb +47 -6
  51. data/lib/mongoid/association/referenced/belongs_to/proxy.rb +35 -42
  52. data/lib/mongoid/association/referenced/belongs_to.rb +77 -77
  53. data/lib/mongoid/association/referenced/counter_cache.rb +24 -35
  54. data/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb +26 -15
  55. data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +12 -7
  56. data/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb +3 -4
  57. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +202 -133
  58. data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +81 -83
  59. data/lib/mongoid/association/referenced/has_many/binding.rb +3 -7
  60. data/lib/mongoid/association/referenced/has_many/buildable.rb +15 -8
  61. data/lib/mongoid/association/referenced/has_many/eager.rb +3 -4
  62. data/lib/mongoid/association/referenced/has_many/enumerable.rb +601 -456
  63. data/lib/mongoid/association/referenced/has_many/proxy.rb +344 -216
  64. data/lib/mongoid/association/referenced/has_many.rb +58 -74
  65. data/lib/mongoid/association/referenced/has_many_through/eager.rb +99 -0
  66. data/lib/mongoid/association/referenced/has_many_through/proxy.rb +68 -0
  67. data/lib/mongoid/association/referenced/has_many_through.rb +201 -0
  68. data/lib/mongoid/association/referenced/has_one/binding.rb +4 -10
  69. data/lib/mongoid/association/referenced/has_one/buildable.rb +29 -17
  70. data/lib/mongoid/association/referenced/has_one/eager.rb +4 -6
  71. data/lib/mongoid/association/referenced/has_one/proxy.rb +51 -47
  72. data/lib/mongoid/association/referenced/has_one.rb +45 -55
  73. data/lib/mongoid/association/referenced/has_one_through/eager.rb +65 -0
  74. data/lib/mongoid/association/referenced/has_one_through/proxy.rb +30 -0
  75. data/lib/mongoid/association/referenced/has_one_through.rb +167 -0
  76. data/lib/mongoid/association/referenced/syncable.rb +44 -62
  77. data/lib/mongoid/association/referenced/with_polymorphic_criteria.rb +41 -0
  78. data/lib/mongoid/association/referenced.rb +4 -0
  79. data/lib/mongoid/association/reflections.rb +11 -12
  80. data/lib/mongoid/association/relatable.rb +173 -146
  81. data/lib/mongoid/association.rb +35 -40
  82. data/lib/mongoid/atomic/modifiers.rb +91 -74
  83. data/lib/mongoid/atomic/paths/embedded/many.rb +25 -11
  84. data/lib/mongoid/atomic/paths/embedded/one.rb +6 -10
  85. data/lib/mongoid/atomic/paths/embedded.rb +5 -8
  86. data/lib/mongoid/atomic/paths/root.rb +5 -10
  87. data/lib/mongoid/atomic/paths.rb +4 -3
  88. data/lib/mongoid/atomic.rb +41 -89
  89. data/lib/mongoid/atomic_update_preparer.rb +89 -0
  90. data/lib/mongoid/attributes/dynamic.rb +12 -27
  91. data/lib/mongoid/attributes/embedded.rb +34 -0
  92. data/lib/mongoid/attributes/nested.rb +22 -25
  93. data/lib/mongoid/attributes/processing.rb +51 -34
  94. data/lib/mongoid/attributes/projector.rb +107 -0
  95. data/lib/mongoid/attributes/readonly.rb +15 -17
  96. data/lib/mongoid/attributes.rb +119 -126
  97. data/lib/mongoid/cacheable.rb +30 -14
  98. data/lib/mongoid/changeable.rb +196 -99
  99. data/lib/mongoid/clients/factory.rb +73 -17
  100. data/lib/mongoid/clients/options.rb +140 -19
  101. data/lib/mongoid/clients/sessions.rb +239 -87
  102. data/lib/mongoid/clients/storage_options.rb +42 -17
  103. data/lib/mongoid/clients/validators/storage.rb +6 -25
  104. data/lib/mongoid/clients/validators.rb +3 -2
  105. data/lib/mongoid/clients.rb +44 -22
  106. data/lib/mongoid/collection_configurable.rb +63 -0
  107. data/lib/mongoid/composable.rb +40 -39
  108. data/lib/mongoid/config/defaults.rb +46 -0
  109. data/lib/mongoid/config/encryption.rb +228 -0
  110. data/lib/mongoid/config/environment.rb +26 -13
  111. data/lib/mongoid/config/introspection.rb +150 -0
  112. data/lib/mongoid/config/options.rb +37 -26
  113. data/lib/mongoid/config/validators/async_query_executor.rb +31 -0
  114. data/lib/mongoid/config/validators/client.rb +20 -37
  115. data/lib/mongoid/config/validators/option.rb +5 -7
  116. data/lib/mongoid/config/validators.rb +5 -3
  117. data/lib/mongoid/config.rb +418 -60
  118. data/lib/mongoid/contextual/aggregable/memory.rb +45 -28
  119. data/lib/mongoid/contextual/aggregable/mongo.rb +49 -47
  120. data/lib/mongoid/contextual/aggregable/none.rb +64 -0
  121. data/lib/mongoid/contextual/aggregable.rb +16 -0
  122. data/lib/mongoid/contextual/atomic.rb +126 -45
  123. data/lib/mongoid/contextual/command.rb +3 -6
  124. data/lib/mongoid/contextual/map_reduce.rb +23 -49
  125. data/lib/mongoid/contextual/memory.rb +448 -121
  126. data/lib/mongoid/contextual/mongo/documents_loader.rb +177 -0
  127. data/lib/mongoid/contextual/mongo.rb +707 -351
  128. data/lib/mongoid/contextual/none.rb +249 -33
  129. data/lib/mongoid/contextual/queryable.rb +6 -6
  130. data/lib/mongoid/contextual.rb +40 -15
  131. data/lib/mongoid/copyable.rb +45 -21
  132. data/lib/mongoid/criteria/findable.rb +65 -27
  133. data/lib/mongoid/criteria/includable.rb +97 -45
  134. data/lib/mongoid/criteria/inspectable.rb +6 -6
  135. data/lib/mongoid/criteria/marshalable.rb +15 -10
  136. data/lib/mongoid/criteria/modifiable.rb +8 -28
  137. data/lib/mongoid/criteria/options.rb +2 -5
  138. data/lib/mongoid/criteria/permission.rb +40 -37
  139. data/lib/mongoid/criteria/queryable/aggregable.rb +13 -24
  140. data/lib/mongoid/criteria/queryable/expandable.rb +64 -0
  141. data/lib/mongoid/criteria/queryable/extensions/array.rb +14 -50
  142. data/lib/mongoid/criteria/queryable/extensions/big_decimal.rb +29 -13
  143. data/lib/mongoid/criteria/queryable/extensions/boolean.rb +6 -9
  144. data/lib/mongoid/criteria/queryable/extensions/date.rb +13 -16
  145. data/lib/mongoid/criteria/queryable/extensions/date_time.rb +14 -18
  146. data/lib/mongoid/criteria/queryable/extensions/hash.rb +5 -44
  147. data/lib/mongoid/criteria/queryable/extensions/nil_class.rb +23 -24
  148. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +31 -29
  149. data/lib/mongoid/criteria/queryable/extensions/object.rb +15 -37
  150. data/lib/mongoid/criteria/queryable/extensions/range.rb +52 -22
  151. data/lib/mongoid/criteria/queryable/extensions/regexp.rb +22 -25
  152. data/lib/mongoid/criteria/queryable/extensions/set.rb +8 -10
  153. data/lib/mongoid/criteria/queryable/extensions/string.rb +24 -41
  154. data/lib/mongoid/criteria/queryable/extensions/symbol.rb +10 -28
  155. data/lib/mongoid/criteria/queryable/extensions/time.rb +11 -14
  156. data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +12 -20
  157. data/lib/mongoid/criteria/queryable/extensions.rb +19 -24
  158. data/lib/mongoid/criteria/queryable/key.rb +49 -30
  159. data/lib/mongoid/criteria/queryable/macroable.rb +3 -6
  160. data/lib/mongoid/criteria/queryable/mergeable.rb +203 -46
  161. data/lib/mongoid/criteria/queryable/optional.rb +32 -81
  162. data/lib/mongoid/criteria/queryable/options.rb +24 -29
  163. data/lib/mongoid/criteria/queryable/pipeline.rb +11 -23
  164. data/lib/mongoid/criteria/queryable/selectable.rb +558 -242
  165. data/lib/mongoid/criteria/queryable/selector.rb +134 -43
  166. data/lib/mongoid/criteria/queryable/smash.rb +41 -21
  167. data/lib/mongoid/criteria/queryable/storable.rb +219 -0
  168. data/lib/mongoid/criteria/queryable.rb +41 -27
  169. data/lib/mongoid/criteria/scopable.rb +37 -30
  170. data/lib/mongoid/criteria/translator.rb +43 -0
  171. data/lib/mongoid/criteria.rb +257 -152
  172. data/lib/mongoid/deprecable.rb +78 -0
  173. data/lib/mongoid/deprecation.rb +25 -0
  174. data/lib/mongoid/document.rb +277 -171
  175. data/lib/mongoid/encryptable.rb +98 -0
  176. data/lib/mongoid/equality.rb +29 -31
  177. data/lib/mongoid/errors/ambiguous_relationship.rb +6 -9
  178. data/lib/mongoid/errors/attribute_not_loaded.rb +31 -0
  179. data/lib/mongoid/errors/callback.rb +3 -6
  180. data/lib/mongoid/errors/config_redactor.rb +43 -0
  181. data/lib/mongoid/errors/create_collection_failure.rb +31 -0
  182. data/lib/mongoid/errors/criteria_argument_required.rb +16 -0
  183. data/lib/mongoid/errors/delete_restriction.rb +11 -16
  184. data/lib/mongoid/errors/document_not_destroyed.rb +5 -10
  185. data/lib/mongoid/errors/document_not_found.rb +37 -27
  186. data/lib/mongoid/errors/drop_collection_failure.rb +25 -0
  187. data/lib/mongoid/errors/empty_config_file.rb +23 -0
  188. data/lib/mongoid/errors/immutable_attribute.rb +24 -0
  189. data/lib/mongoid/errors/in_memory_collation_not_supported.rb +3 -6
  190. data/lib/mongoid/errors/in_memory_regexp_timeout.rb +26 -0
  191. data/lib/mongoid/errors/invalid_around_callback.rb +16 -0
  192. data/lib/mongoid/errors/invalid_async_query_executor.rb +23 -0
  193. data/lib/mongoid/errors/invalid_auto_encryption_configuration.rb +30 -0
  194. data/lib/mongoid/errors/invalid_collection.rb +3 -3
  195. data/lib/mongoid/errors/invalid_config_file.rb +23 -0
  196. data/lib/mongoid/errors/invalid_config_option.rb +5 -8
  197. data/lib/mongoid/errors/invalid_dependent_strategy.rb +9 -13
  198. data/lib/mongoid/errors/invalid_discriminator_key_target.rb +23 -0
  199. data/lib/mongoid/errors/invalid_dot_dollar_assignment.rb +21 -0
  200. data/lib/mongoid/errors/invalid_elem_match_operator.rb +29 -0
  201. data/lib/mongoid/errors/invalid_estimated_count_criteria.rb +24 -0
  202. data/lib/mongoid/errors/invalid_estimated_count_scoping.rb +24 -0
  203. data/lib/mongoid/errors/invalid_expression_operator.rb +24 -0
  204. data/lib/mongoid/errors/invalid_field.rb +10 -11
  205. data/lib/mongoid/errors/invalid_field_operator.rb +29 -0
  206. data/lib/mongoid/errors/invalid_field_option.rb +4 -7
  207. data/lib/mongoid/errors/invalid_field_type.rb +24 -0
  208. data/lib/mongoid/errors/invalid_find.rb +3 -6
  209. data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +20 -0
  210. data/lib/mongoid/errors/invalid_includes.rb +5 -8
  211. data/lib/mongoid/errors/invalid_index.rb +3 -6
  212. data/lib/mongoid/errors/invalid_options.rb +5 -8
  213. data/lib/mongoid/errors/invalid_path.rb +3 -6
  214. data/lib/mongoid/errors/invalid_persistence_option.rb +6 -11
  215. data/lib/mongoid/errors/invalid_query.rb +36 -0
  216. data/lib/mongoid/errors/invalid_relation.rb +15 -22
  217. data/lib/mongoid/errors/invalid_relation_option.rb +8 -11
  218. data/lib/mongoid/errors/invalid_scope.rb +3 -6
  219. data/lib/mongoid/errors/invalid_session_nesting.rb +14 -0
  220. data/lib/mongoid/errors/invalid_set_polymorphic_relation.rb +8 -9
  221. data/lib/mongoid/errors/invalid_storage_options.rb +4 -7
  222. data/lib/mongoid/errors/invalid_time.rb +3 -6
  223. data/lib/mongoid/errors/invalid_transaction_nesting.rb +14 -0
  224. data/lib/mongoid/errors/inverse_not_found.rb +4 -7
  225. data/lib/mongoid/errors/mixed_client_configuration.rb +4 -7
  226. data/lib/mongoid/errors/mixed_relations.rb +3 -5
  227. data/lib/mongoid/errors/mongoid_error.rb +13 -23
  228. data/lib/mongoid/errors/nested_attributes_metadata_not_found.rb +4 -7
  229. data/lib/mongoid/errors/no_client_config.rb +5 -8
  230. data/lib/mongoid/errors/no_client_database.rb +5 -8
  231. data/lib/mongoid/errors/no_client_hosts.rb +5 -8
  232. data/lib/mongoid/errors/no_clients_config.rb +3 -6
  233. data/lib/mongoid/errors/no_default_client.rb +4 -7
  234. data/lib/mongoid/errors/no_encryption_schema.rb +28 -0
  235. data/lib/mongoid/errors/no_environment.rb +3 -6
  236. data/lib/mongoid/errors/no_map_reduce_output.rb +3 -6
  237. data/lib/mongoid/errors/no_metadata.rb +3 -6
  238. data/lib/mongoid/errors/no_parent.rb +3 -6
  239. data/lib/mongoid/errors/readonly_association.rb +29 -0
  240. data/lib/mongoid/errors/readonly_attribute.rb +4 -7
  241. data/lib/mongoid/errors/readonly_document.rb +4 -9
  242. data/lib/mongoid/errors/rollback.rb +13 -0
  243. data/lib/mongoid/errors/scope_overwrite.rb +4 -4
  244. data/lib/mongoid/errors/sessions_not_supported.rb +14 -0
  245. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +4 -3
  246. data/lib/mongoid/errors/transaction_error.rb +22 -0
  247. data/lib/mongoid/errors/transactions_not_supported.rb +14 -0
  248. data/lib/mongoid/errors/unknown_attribute.rb +4 -7
  249. data/lib/mongoid/errors/unknown_model.rb +3 -6
  250. data/lib/mongoid/errors/unrecognized_model_alias.rb +53 -0
  251. data/lib/mongoid/errors/unrecognized_resolver.rb +27 -0
  252. data/lib/mongoid/errors/unregistered_class.rb +47 -0
  253. data/lib/mongoid/errors/unsaved_document.rb +4 -4
  254. data/lib/mongoid/errors/unsupported_isolation_level.rb +22 -0
  255. data/lib/mongoid/errors/unsupported_javascript.rb +3 -6
  256. data/lib/mongoid/errors/validations.rb +5 -5
  257. data/lib/mongoid/errors.rb +82 -51
  258. data/lib/mongoid/evolvable.rb +4 -7
  259. data/lib/mongoid/extensions/array.rb +32 -55
  260. data/lib/mongoid/extensions/big_decimal.rb +45 -24
  261. data/lib/mongoid/extensions/binary.rb +42 -0
  262. data/lib/mongoid/extensions/boolean.rb +12 -8
  263. data/lib/mongoid/extensions/bson_document.rb +28 -0
  264. data/lib/mongoid/extensions/date.rb +38 -30
  265. data/lib/mongoid/extensions/date_time.rb +8 -22
  266. data/lib/mongoid/extensions/decimal128.rb +4 -8
  267. data/lib/mongoid/extensions/false_class.rb +11 -13
  268. data/lib/mongoid/extensions/float.rb +15 -19
  269. data/lib/mongoid/extensions/hash.rb +31 -116
  270. data/lib/mongoid/extensions/integer.rb +17 -21
  271. data/lib/mongoid/extensions/module.rb +6 -7
  272. data/lib/mongoid/extensions/nil_class.rb +6 -7
  273. data/lib/mongoid/extensions/object.rb +42 -84
  274. data/lib/mongoid/extensions/object_id.rb +5 -11
  275. data/lib/mongoid/extensions/range.rb +49 -25
  276. data/lib/mongoid/extensions/raw_value.rb +31 -0
  277. data/lib/mongoid/extensions/regexp.rb +16 -10
  278. data/lib/mongoid/extensions/set.rb +27 -15
  279. data/lib/mongoid/extensions/string.rb +40 -67
  280. data/lib/mongoid/extensions/symbol.rb +11 -26
  281. data/lib/mongoid/extensions/time.rb +49 -47
  282. data/lib/mongoid/extensions/time_with_zone.rb +30 -12
  283. data/lib/mongoid/extensions/true_class.rb +11 -13
  284. data/lib/mongoid/extensions/vector.rb +64 -0
  285. data/lib/mongoid/extensions.rb +28 -41
  286. data/lib/mongoid/factory.rb +205 -28
  287. data/lib/mongoid/field_readable.rb +69 -0
  288. data/lib/mongoid/fields/encrypted.rb +47 -0
  289. data/lib/mongoid/fields/foreign_key.rb +48 -35
  290. data/lib/mongoid/fields/localized.rb +33 -21
  291. data/lib/mongoid/fields/standard.rb +31 -48
  292. data/lib/mongoid/fields/validators/macro.rb +48 -45
  293. data/lib/mongoid/fields/validators.rb +3 -2
  294. data/lib/mongoid/fields.rb +499 -117
  295. data/lib/mongoid/findable.rb +174 -64
  296. data/lib/mongoid/identifiable.rb +28 -0
  297. data/lib/mongoid/indexable/specification.rb +76 -39
  298. data/lib/mongoid/indexable/validators/options.rb +36 -43
  299. data/lib/mongoid/indexable.rb +57 -58
  300. data/lib/mongoid/inspectable.rb +37 -15
  301. data/lib/mongoid/interceptable.rb +235 -66
  302. data/lib/mongoid/loadable.rb +146 -0
  303. data/lib/mongoid/loggable.rb +12 -18
  304. data/lib/mongoid/matchable.rb +7 -154
  305. data/lib/mongoid/matcher/all.rb +34 -0
  306. data/lib/mongoid/matcher/and.rb +32 -0
  307. data/lib/mongoid/matcher/bits.rb +65 -0
  308. data/lib/mongoid/matcher/bits_all_clear.rb +39 -0
  309. data/lib/mongoid/matcher/bits_all_set.rb +39 -0
  310. data/lib/mongoid/matcher/bits_any_clear.rb +39 -0
  311. data/lib/mongoid/matcher/bits_any_set.rb +39 -0
  312. data/lib/mongoid/matcher/elem_match.rb +49 -0
  313. data/lib/mongoid/matcher/elem_match_expression.rb +29 -0
  314. data/lib/mongoid/matcher/eq.rb +23 -0
  315. data/lib/mongoid/matcher/eq_impl.rb +79 -0
  316. data/lib/mongoid/matcher/eq_impl_with_regexp.rb +33 -0
  317. data/lib/mongoid/matcher/exists.rb +27 -0
  318. data/lib/mongoid/matcher/expression.rb +43 -0
  319. data/lib/mongoid/matcher/expression_operator.rb +31 -0
  320. data/lib/mongoid/matcher/field_expression.rb +74 -0
  321. data/lib/mongoid/matcher/field_operator.rb +76 -0
  322. data/lib/mongoid/matcher/gt.rb +29 -0
  323. data/lib/mongoid/matcher/gte.rb +29 -0
  324. data/lib/mongoid/matcher/in.rb +37 -0
  325. data/lib/mongoid/matcher/lt.rb +29 -0
  326. data/lib/mongoid/matcher/lte.rb +29 -0
  327. data/lib/mongoid/matcher/mod.rb +29 -0
  328. data/lib/mongoid/matcher/ne.rb +28 -0
  329. data/lib/mongoid/matcher/nin.rb +23 -0
  330. data/lib/mongoid/matcher/nor.rb +34 -0
  331. data/lib/mongoid/matcher/not.rb +42 -0
  332. data/lib/mongoid/matcher/or.rb +32 -0
  333. data/lib/mongoid/matcher/regex.rb +61 -0
  334. data/lib/mongoid/matcher/regexp_budget.rb +372 -0
  335. data/lib/mongoid/matcher/size.rb +39 -0
  336. data/lib/mongoid/matcher/type.rb +119 -0
  337. data/lib/mongoid/matcher.rb +141 -0
  338. data/lib/mongoid/model_resolver.rb +155 -0
  339. data/lib/mongoid/persistable/creatable.rb +50 -36
  340. data/lib/mongoid/persistable/deletable.rb +18 -30
  341. data/lib/mongoid/persistable/destroyable.rb +38 -14
  342. data/lib/mongoid/persistable/incrementable.rb +8 -10
  343. data/lib/mongoid/persistable/logical.rb +7 -10
  344. data/lib/mongoid/persistable/maxable.rb +35 -0
  345. data/lib/mongoid/persistable/minable.rb +35 -0
  346. data/lib/mongoid/persistable/multipliable.rb +34 -0
  347. data/lib/mongoid/persistable/poppable.rb +5 -9
  348. data/lib/mongoid/persistable/pullable.rb +6 -12
  349. data/lib/mongoid/persistable/pushable.rb +8 -14
  350. data/lib/mongoid/persistable/renamable.rb +11 -10
  351. data/lib/mongoid/persistable/savable.rb +16 -11
  352. data/lib/mongoid/persistable/settable.rb +7 -18
  353. data/lib/mongoid/persistable/unsettable.rb +11 -12
  354. data/lib/mongoid/persistable/updatable.rb +113 -35
  355. data/lib/mongoid/persistable/upsertable.rb +37 -12
  356. data/lib/mongoid/persistable.rb +169 -60
  357. data/lib/mongoid/persistence_context.rb +189 -75
  358. data/lib/mongoid/pluckable.rb +130 -0
  359. data/lib/mongoid/positional.rb +7 -15
  360. data/lib/mongoid/railtie.rb +36 -32
  361. data/lib/mongoid/railties/bson_object_id_serializer.rb +46 -0
  362. data/lib/mongoid/railties/console_sandbox.rb +40 -0
  363. data/lib/mongoid/railties/controller_runtime.rb +34 -20
  364. data/lib/mongoid/railties/database.rake +53 -29
  365. data/lib/mongoid/reloadable.rb +56 -45
  366. data/lib/mongoid/scopable.rb +50 -79
  367. data/lib/mongoid/search_indexable.rb +538 -0
  368. data/lib/mongoid/selectable.rb +8 -19
  369. data/lib/mongoid/serializable.rb +46 -40
  370. data/lib/mongoid/shardable.rb +102 -23
  371. data/lib/mongoid/stateful.rb +67 -26
  372. data/lib/mongoid/stringified_symbol.rb +61 -0
  373. data/lib/mongoid/tasks/database.rake +46 -15
  374. data/lib/mongoid/tasks/database.rb +204 -31
  375. data/lib/mongoid/tasks/encryption.rake +59 -0
  376. data/lib/mongoid/tasks/encryption.rb +103 -0
  377. data/lib/mongoid/threaded/lifecycle.rb +13 -34
  378. data/lib/mongoid/threaded.rb +353 -108
  379. data/lib/mongoid/timestamps/created/short.rb +4 -3
  380. data/lib/mongoid/timestamps/created.rb +15 -7
  381. data/lib/mongoid/timestamps/short.rb +2 -1
  382. data/lib/mongoid/timestamps/timeless.rb +127 -29
  383. data/lib/mongoid/timestamps/updated/short.rb +4 -3
  384. data/lib/mongoid/timestamps/updated.rb +6 -9
  385. data/lib/mongoid/timestamps.rb +7 -7
  386. data/lib/mongoid/touchable.rb +176 -40
  387. data/lib/mongoid/traversable.rb +309 -106
  388. data/lib/mongoid/utils.rb +52 -0
  389. data/lib/mongoid/validatable/associated.rb +115 -22
  390. data/lib/mongoid/validatable/format.rb +2 -2
  391. data/lib/mongoid/validatable/length.rb +2 -2
  392. data/lib/mongoid/validatable/localizable.rb +3 -6
  393. data/lib/mongoid/validatable/macros.rb +25 -14
  394. data/lib/mongoid/validatable/numericality.rb +40 -0
  395. data/lib/mongoid/validatable/presence.rb +15 -21
  396. data/lib/mongoid/validatable/queryable.rb +9 -4
  397. data/lib/mongoid/validatable/uniqueness.rb +40 -53
  398. data/lib/mongoid/validatable.rb +41 -52
  399. data/lib/mongoid/version.rb +6 -2
  400. data/lib/mongoid/warnings.rb +40 -0
  401. data/lib/mongoid.rb +105 -44
  402. data/lib/rails/generators/mongoid/config/config_generator.rb +18 -5
  403. data/lib/rails/generators/mongoid/config/templates/mongoid.rb +24 -0
  404. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +51 -56
  405. data/lib/rails/generators/mongoid/model/model_generator.rb +10 -8
  406. data/lib/rails/generators/mongoid_generator.rb +17 -7
  407. data/lib/rails/mongoid.rb +4 -32
  408. data/spec/README.md +19 -4
  409. data/spec/config/mongoid.yml +17 -1
  410. data/spec/config/mongoid_with_schema_map_uuid.yml +27 -0
  411. data/spec/integration/active_job_spec.rb +35 -0
  412. data/spec/integration/app_spec.rb +327 -144
  413. data/spec/integration/associations/belongs_to_spec.rb +165 -0
  414. data/spec/integration/associations/embedded_dirty_spec.rb +57 -0
  415. data/spec/integration/associations/embedded_spec.rb +241 -67
  416. data/spec/integration/associations/embeds_many_spec.rb +347 -0
  417. data/spec/integration/associations/embeds_one_spec.rb +62 -0
  418. data/spec/integration/associations/foreign_key_spec.rb +107 -0
  419. data/spec/integration/associations/foreign_key_spec_models.rb +64 -0
  420. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +161 -0
  421. data/spec/integration/associations/has_many_spec.rb +184 -0
  422. data/spec/integration/associations/has_one_spec.rb +322 -0
  423. data/spec/integration/associations/nested_attributes_assignment_spec.rb +113 -0
  424. data/spec/integration/associations/reverse_population_spec.rb +34 -0
  425. data/spec/integration/associations/reverse_population_spec_models.rb +36 -0
  426. data/spec/integration/associations/scope_option_spec.rb +100 -0
  427. data/spec/integration/atomic/modifiers_spec.rb +137 -0
  428. data/spec/integration/bson_regexp_raw_spec.rb +19 -0
  429. data/spec/integration/caching_spec.rb +68 -0
  430. data/spec/integration/callbacks_models.rb +149 -3
  431. data/spec/integration/callbacks_spec.rb +422 -34
  432. data/spec/integration/contextual/empty_spec.rb +141 -0
  433. data/spec/integration/criteria/alias_query_spec.rb +91 -0
  434. data/spec/integration/criteria/date_field_spec.rb +1 -2
  435. data/spec/integration/criteria/default_scope_spec.rb +78 -0
  436. data/spec/integration/criteria/logical_spec.rb +105 -0
  437. data/spec/integration/criteria/range_spec.rb +349 -0
  438. data/spec/integration/criteria/raw_value_spec.rb +538 -0
  439. data/spec/integration/criteria/time_with_zone_spec.rb +135 -15
  440. data/spec/integration/discriminator_key_spec.rb +392 -0
  441. data/spec/integration/discriminator_value_spec.rb +204 -0
  442. data/spec/integration/document_spec.rb +32 -3
  443. data/spec/integration/dots_and_dollars_spec.rb +273 -0
  444. data/spec/integration/encryption_spec.rb +248 -0
  445. data/spec/integration/i18n_fallbacks_spec.rb +47 -0
  446. data/spec/integration/isolation_state_spec.rb +241 -0
  447. data/spec/integration/matcher_examples_spec.rb +743 -0
  448. data/spec/integration/matcher_operator_data/all.yml +140 -0
  449. data/spec/integration/matcher_operator_data/and.yml +93 -0
  450. data/spec/integration/matcher_operator_data/bits_all_clear.yml +144 -0
  451. data/spec/integration/matcher_operator_data/bits_all_set.yml +144 -0
  452. data/spec/integration/matcher_operator_data/bits_any_clear.yml +144 -0
  453. data/spec/integration/matcher_operator_data/bits_any_set.yml +144 -0
  454. data/spec/integration/matcher_operator_data/comment.yml +22 -0
  455. data/spec/integration/matcher_operator_data/elem_match.yml +409 -0
  456. data/spec/integration/matcher_operator_data/elem_match_expr.yml +213 -0
  457. data/spec/integration/matcher_operator_data/eq.yml +191 -0
  458. data/spec/integration/matcher_operator_data/exists.yml +213 -0
  459. data/spec/integration/matcher_operator_data/generic_op.yml +17 -0
  460. data/spec/integration/matcher_operator_data/gt.yml +132 -0
  461. data/spec/integration/matcher_operator_data/gt_types.yml +63 -0
  462. data/spec/integration/matcher_operator_data/gte.yml +132 -0
  463. data/spec/integration/matcher_operator_data/gte_types.yml +15 -0
  464. data/spec/integration/matcher_operator_data/implicit.yml +331 -0
  465. data/spec/integration/matcher_operator_data/implicit_traversal.yml +112 -0
  466. data/spec/integration/matcher_operator_data/in.yml +210 -0
  467. data/spec/integration/matcher_operator_data/invalid_op.yml +59 -0
  468. data/spec/integration/matcher_operator_data/invalid_syntax.yml +39 -0
  469. data/spec/integration/matcher_operator_data/lt.yml +132 -0
  470. data/spec/integration/matcher_operator_data/lt_types.yml +15 -0
  471. data/spec/integration/matcher_operator_data/lte.yml +132 -0
  472. data/spec/integration/matcher_operator_data/lte_types.yml +15 -0
  473. data/spec/integration/matcher_operator_data/mod.yml +55 -0
  474. data/spec/integration/matcher_operator_data/multiple.yml +29 -0
  475. data/spec/integration/matcher_operator_data/ne.yml +150 -0
  476. data/spec/integration/matcher_operator_data/ne_types.yml +15 -0
  477. data/spec/integration/matcher_operator_data/nin.yml +114 -0
  478. data/spec/integration/matcher_operator_data/nor.yml +126 -0
  479. data/spec/integration/matcher_operator_data/not.yml +196 -0
  480. data/spec/integration/matcher_operator_data/or.yml +137 -0
  481. data/spec/integration/matcher_operator_data/regex.yml +195 -0
  482. data/spec/integration/matcher_operator_data/regex_options.yml +72 -0
  483. data/spec/integration/matcher_operator_data/size.yml +171 -0
  484. data/spec/integration/matcher_operator_data/type.yml +64 -0
  485. data/spec/integration/matcher_operator_data/type_array.yml +15 -0
  486. data/spec/integration/matcher_operator_data/type_binary.yml +18 -0
  487. data/spec/integration/matcher_operator_data/type_boolean.yml +39 -0
  488. data/spec/integration/matcher_operator_data/type_code.yml +26 -0
  489. data/spec/integration/matcher_operator_data/type_code_with_scope.yml +26 -0
  490. data/spec/integration/matcher_operator_data/type_date.yml +39 -0
  491. data/spec/integration/matcher_operator_data/type_db_pointer.yml +19 -0
  492. data/spec/integration/matcher_operator_data/type_decimal.yml +37 -0
  493. data/spec/integration/matcher_operator_data/type_double.yml +15 -0
  494. data/spec/integration/matcher_operator_data/type_int32.yml +33 -0
  495. data/spec/integration/matcher_operator_data/type_int64.yml +33 -0
  496. data/spec/integration/matcher_operator_data/type_max_key.yml +17 -0
  497. data/spec/integration/matcher_operator_data/type_min_key.yml +17 -0
  498. data/spec/integration/matcher_operator_data/type_null.yml +23 -0
  499. data/spec/integration/matcher_operator_data/type_object.yml +23 -0
  500. data/spec/integration/matcher_operator_data/type_object_id.yml +25 -0
  501. data/spec/integration/matcher_operator_data/type_regex.yml +44 -0
  502. data/spec/integration/matcher_operator_data/type_string.yml +15 -0
  503. data/spec/integration/matcher_operator_data/type_symbol.yml +32 -0
  504. data/spec/integration/matcher_operator_data/type_timestamp.yml +25 -0
  505. data/spec/integration/matcher_operator_data/type_undefined.yml +17 -0
  506. data/spec/integration/matcher_operator_spec.rb +113 -0
  507. data/spec/integration/matcher_regexp_timeout_spec.rb +215 -0
  508. data/spec/integration/matcher_spec.rb +237 -0
  509. data/spec/integration/persistence/collection_options_spec.rb +34 -0
  510. data/spec/integration/persistence/range_field_spec.rb +373 -0
  511. data/spec/integration/query_operator_guard_spec.rb +89 -0
  512. data/spec/integration/server_query_spec.rb +148 -0
  513. data/spec/integration/shardable_spec.rb +148 -0
  514. data/spec/integration/stringified_symbol_field_spec.rb +189 -0
  515. data/spec/lite_spec_helper.rb +56 -19
  516. data/spec/mongoid/association/accessors_spec.rb +375 -273
  517. data/spec/mongoid/association/auto_save_spec.rb +215 -106
  518. data/spec/mongoid/association/builders_spec.rb +124 -74
  519. data/spec/mongoid/association/constrainable_spec.rb +11 -21
  520. data/spec/mongoid/association/counter_cache_spec.rb +101 -131
  521. data/spec/mongoid/association/depending_spec.rb +454 -402
  522. data/spec/mongoid/association/eager_loadable_spec.rb +47 -0
  523. data/spec/mongoid/association/eager_spec.rb +471 -60
  524. data/spec/mongoid/association/embedded/cyclic_spec.rb +40 -49
  525. data/spec/mongoid/association/embedded/dirty_spec.rb +19 -21
  526. data/spec/mongoid/association/embedded/embedded_in/binding_spec.rb +39 -53
  527. data/spec/mongoid/association/embedded/embedded_in/buildable_spec.rb +58 -8
  528. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +208 -158
  529. data/spec/mongoid/association/embedded/embedded_in_spec.rb +46 -134
  530. data/spec/mongoid/association/embedded/embeds_many/binding_spec.rb +11 -13
  531. data/spec/mongoid/association/embedded/embeds_many/buildable_spec.rb +137 -35
  532. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +1716 -1327
  533. data/spec/mongoid/association/embedded/embeds_many_models.rb +191 -2
  534. data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +68 -0
  535. data/spec/mongoid/association/embedded/embeds_many_spec.rb +77 -119
  536. data/spec/mongoid/association/embedded/embeds_one/binding_spec.rb +17 -22
  537. data/spec/mongoid/association/embedded/embeds_one/buildable_spec.rb +44 -24
  538. data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +234 -287
  539. data/spec/mongoid/association/embedded/embeds_one_dnl_models.rb +2 -0
  540. data/spec/mongoid/association/embedded/embeds_one_models.rb +27 -1
  541. data/spec/mongoid/association/embedded/embeds_one_query_spec.rb +28 -0
  542. data/spec/mongoid/association/embedded/embeds_one_spec.rb +50 -134
  543. data/spec/mongoid/association/fallback_spec.rb +425 -0
  544. data/spec/mongoid/association/macros_spec.rb +250 -308
  545. data/spec/mongoid/association/nested/many_spec.rb +53 -72
  546. data/spec/mongoid/association/nested/one_spec.rb +68 -85
  547. data/spec/mongoid/association/options_spec.rb +189 -309
  548. data/spec/mongoid/association/polymorphic_spec.rb +36 -51
  549. data/spec/mongoid/association/referenced/belongs_to/binding_spec.rb +58 -75
  550. data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +131 -40
  551. data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +280 -52
  552. data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +345 -370
  553. data/spec/mongoid/association/referenced/belongs_to_models.rb +13 -0
  554. data/spec/mongoid/association/referenced/belongs_to_query_spec.rb +57 -0
  555. data/spec/mongoid/association/referenced/belongs_to_spec.rb +171 -329
  556. data/spec/mongoid/association/referenced/has_and_belongs_to_many/binding_spec.rb +45 -56
  557. data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +66 -29
  558. data/spec/mongoid/association/referenced/has_and_belongs_to_many/eager_spec.rb +75 -36
  559. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_persistence_spec.rb +28 -8
  560. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +1151 -1283
  561. data/spec/mongoid/association/referenced/has_and_belongs_to_many_models.rb +69 -2
  562. data/spec/mongoid/association/referenced/has_and_belongs_to_many_query_spec.rb +39 -0
  563. data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +176 -166
  564. data/spec/mongoid/association/referenced/has_many/binding_spec.rb +32 -43
  565. data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +161 -24
  566. data/spec/mongoid/association/referenced/has_many/eager_spec.rb +169 -67
  567. data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +1021 -689
  568. data/spec/mongoid/association/referenced/has_many/proxy_query_spec.rb +1 -1
  569. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +1743 -1955
  570. data/spec/mongoid/association/referenced/has_many_models.rb +85 -1
  571. data/spec/mongoid/association/referenced/has_many_query_spec.rb +37 -0
  572. data/spec/mongoid/association/referenced/has_many_spec.rb +83 -178
  573. data/spec/mongoid/association/referenced/has_many_through/eager_spec.rb +58 -0
  574. data/spec/mongoid/association/referenced/has_many_through/proxy_spec.rb +62 -0
  575. data/spec/mongoid/association/referenced/has_many_through_spec.rb +399 -0
  576. data/spec/mongoid/association/referenced/has_one/binding_spec.rb +28 -37
  577. data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +74 -28
  578. data/spec/mongoid/association/referenced/has_one/eager_spec.rb +60 -42
  579. data/spec/mongoid/association/referenced/has_one/proxy_spec.rb +380 -333
  580. data/spec/mongoid/association/referenced/has_one_models.rb +73 -0
  581. data/spec/mongoid/association/referenced/has_one_query_spec.rb +37 -0
  582. data/spec/mongoid/association/referenced/has_one_spec.rb +61 -185
  583. data/spec/mongoid/association/referenced/has_one_through/eager_spec.rb +58 -0
  584. data/spec/mongoid/association/referenced/has_one_through/proxy_spec.rb +49 -0
  585. data/spec/mongoid/association/referenced/has_one_through_spec.rb +211 -0
  586. data/spec/mongoid/association/reflections_spec.rb +16 -23
  587. data/spec/mongoid/association/syncable_spec.rb +132 -138
  588. data/spec/mongoid/association_spec.rb +59 -65
  589. data/spec/mongoid/atomic/modifiers_spec.rb +217 -180
  590. data/spec/mongoid/atomic/paths/embedded/many_spec.rb +41 -51
  591. data/spec/mongoid/atomic/paths/embedded/one_spec.rb +37 -48
  592. data/spec/mongoid/atomic/paths/root_spec.rb +11 -14
  593. data/spec/mongoid/atomic/paths_spec.rb +133 -128
  594. data/spec/mongoid/atomic_spec.rb +187 -154
  595. data/spec/mongoid/atomic_update_preparer_spec.rb +83 -0
  596. data/spec/mongoid/attributes/dynamic_spec.rb +5 -6
  597. data/spec/mongoid/attributes/embedded_spec.rb +118 -0
  598. data/spec/mongoid/attributes/nested_spec.rb +1604 -1737
  599. data/spec/mongoid/attributes/nested_spec_models.rb +48 -0
  600. data/spec/mongoid/attributes/projector_data/embedded.yml +105 -0
  601. data/spec/mongoid/attributes/projector_data/fields.yml +93 -0
  602. data/spec/mongoid/attributes/projector_spec.rb +37 -0
  603. data/spec/mongoid/attributes/readonly_spec.rb +106 -108
  604. data/spec/mongoid/attributes_spec.rb +1217 -715
  605. data/spec/mongoid/cacheable_spec.rb +78 -45
  606. data/spec/mongoid/changeable_spec.rb +914 -638
  607. data/spec/mongoid/clients/factory_spec.rb +314 -137
  608. data/spec/mongoid/clients/options_spec.rb +205 -76
  609. data/spec/mongoid/clients/sessions_spec.rb +73 -116
  610. data/spec/mongoid/clients/transactions_spec.rb +1128 -186
  611. data/spec/mongoid/clients/transactions_spec_models.rb +251 -0
  612. data/spec/mongoid/clients_spec.rb +433 -312
  613. data/spec/mongoid/collection_configurable_spec.rb +156 -0
  614. data/spec/mongoid/composable_spec.rb +4 -8
  615. data/spec/mongoid/config/defaults_spec.rb +125 -0
  616. data/spec/mongoid/config/encryption_spec.rb +377 -0
  617. data/spec/mongoid/config/environment_spec.rb +129 -49
  618. data/spec/mongoid/config/introspection_spec.rb +119 -0
  619. data/spec/mongoid/config/options_spec.rb +36 -23
  620. data/spec/mongoid/config_spec.rb +497 -148
  621. data/spec/mongoid/contextual/aggregable/memory_spec.rb +507 -161
  622. data/spec/mongoid/contextual/aggregable/memory_table.yml +88 -0
  623. data/spec/mongoid/contextual/aggregable/memory_table_spec.rb +60 -0
  624. data/spec/mongoid/contextual/aggregable/mongo_spec.rb +242 -158
  625. data/spec/mongoid/contextual/aggregable/none_spec.rb +47 -0
  626. data/spec/mongoid/contextual/atomic_spec.rb +510 -229
  627. data/spec/mongoid/contextual/map_reduce_spec.rb +114 -153
  628. data/spec/mongoid/contextual/memory_spec.rb +1947 -563
  629. data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +193 -0
  630. data/spec/mongoid/contextual/mongo_spec.rb +3317 -1268
  631. data/spec/mongoid/contextual/none_spec.rb +98 -89
  632. data/spec/mongoid/contextual/short_circuit_spec.rb +139 -0
  633. data/spec/mongoid/copyable_spec.rb +769 -191
  634. data/spec/mongoid/copyable_spec_models.rb +30 -0
  635. data/spec/mongoid/criteria/findable_spec.rb +565 -506
  636. data/spec/mongoid/criteria/includable_spec.rb +2417 -0
  637. data/spec/mongoid/criteria/includable_spec_models.rb +64 -0
  638. data/spec/mongoid/criteria/inspectable_spec.rb +13 -13
  639. data/spec/mongoid/criteria/marshalable_spec.rb +29 -12
  640. data/spec/mongoid/criteria/modifiable_spec.rb +388 -530
  641. data/spec/mongoid/criteria/options_spec.rb +4 -6
  642. data/spec/mongoid/criteria/queryable/aggregable_spec.rb +114 -140
  643. data/spec/mongoid/criteria/queryable/expandable_spec.rb +59 -0
  644. data/spec/mongoid/criteria/queryable/extensions/array_spec.rb +132 -198
  645. data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +199 -25
  646. data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +86 -116
  647. data/spec/mongoid/criteria/queryable/extensions/date_spec.rb +69 -67
  648. data/spec/mongoid/criteria/queryable/extensions/date_time_spec.rb +121 -113
  649. data/spec/mongoid/criteria/queryable/extensions/float_spec.rb +96 -32
  650. data/spec/mongoid/criteria/queryable/extensions/hash_spec.rb +105 -152
  651. data/spec/mongoid/criteria/queryable/extensions/integer_spec.rb +97 -33
  652. data/spec/mongoid/criteria/queryable/extensions/nil_class_spec.rb +27 -37
  653. data/spec/mongoid/criteria/queryable/extensions/numeric_spec.rb +79 -15
  654. data/spec/mongoid/criteria/queryable/extensions/object_spec.rb +34 -53
  655. data/spec/mongoid/criteria/queryable/extensions/range_spec.rb +259 -191
  656. data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +18 -34
  657. data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +16 -33
  658. data/spec/mongoid/criteria/queryable/extensions/set_spec.rb +14 -18
  659. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +91 -189
  660. data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +34 -100
  661. data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +99 -103
  662. data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +78 -85
  663. data/spec/mongoid/criteria/queryable/key_spec.rb +34 -35
  664. data/spec/mongoid/criteria/queryable/mergeable_spec.rb +150 -21
  665. data/spec/mongoid/criteria/queryable/optional_spec.rb +375 -954
  666. data/spec/mongoid/criteria/queryable/options_spec.rb +175 -123
  667. data/spec/mongoid/criteria/queryable/pipeline_spec.rb +67 -86
  668. data/spec/mongoid/criteria/queryable/queryable_spec.rb +43 -52
  669. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +2101 -414
  670. data/spec/mongoid/criteria/queryable/selectable_shared_examples.rb +35 -0
  671. data/spec/mongoid/criteria/queryable/selectable_spec.rb +1283 -3140
  672. data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +754 -0
  673. data/spec/mongoid/criteria/queryable/selector_spec.rb +404 -317
  674. data/spec/mongoid/criteria/queryable/smash_spec.rb +8 -10
  675. data/spec/mongoid/criteria/queryable/storable_spec.rb +249 -0
  676. data/spec/mongoid/criteria/queryable_mql_spec.rb +92 -0
  677. data/spec/mongoid/criteria/scopable_spec.rb +212 -118
  678. data/spec/mongoid/criteria/translator_spec.rb +132 -0
  679. data/spec/mongoid/criteria_projection_spec.rb +389 -0
  680. data/spec/mongoid/criteria_spec.rb +1942 -2301
  681. data/spec/mongoid/document_fields_spec.rb +187 -14
  682. data/spec/mongoid/document_persistence_context_spec.rb +80 -6
  683. data/spec/mongoid/document_query_spec.rb +87 -0
  684. data/spec/mongoid/document_spec.rb +394 -538
  685. data/spec/mongoid/encryptable_spec.rb +61 -0
  686. data/spec/mongoid/equality_spec.rb +77 -103
  687. data/spec/mongoid/errors/ambiguous_relationship_spec.rb +10 -10
  688. data/spec/mongoid/errors/attribute_not_loaded_spec.rb +29 -0
  689. data/spec/mongoid/errors/callback_spec.rb +10 -10
  690. data/spec/mongoid/errors/config_redactor_spec.rb +112 -0
  691. data/spec/mongoid/errors/delete_restriction_spec.rb +9 -9
  692. data/spec/mongoid/errors/document_not_destroyed_spec.rb +9 -9
  693. data/spec/mongoid/errors/document_not_found_spec.rb +108 -32
  694. data/spec/mongoid/errors/invalid_collection_spec.rb +11 -12
  695. data/spec/mongoid/errors/invalid_config_file_spec.rb +29 -0
  696. data/spec/mongoid/errors/invalid_config_option_spec.rb +10 -10
  697. data/spec/mongoid/errors/invalid_field_option_spec.rb +9 -9
  698. data/spec/mongoid/errors/invalid_field_spec.rb +13 -11
  699. data/spec/mongoid/errors/invalid_field_type_spec.rb +53 -0
  700. data/spec/mongoid/errors/invalid_find_spec.rb +10 -10
  701. data/spec/mongoid/errors/invalid_includes_spec.rb +11 -12
  702. data/spec/mongoid/errors/invalid_index_spec.rb +10 -10
  703. data/spec/mongoid/errors/invalid_options_spec.rb +10 -10
  704. data/spec/mongoid/errors/invalid_path_spec.rb +9 -9
  705. data/spec/mongoid/errors/invalid_relation_spec.rb +17 -15
  706. data/spec/mongoid/errors/invalid_scope_spec.rb +10 -10
  707. data/spec/mongoid/errors/invalid_set_polymorphic_relation_spec.rb +5 -5
  708. data/spec/mongoid/errors/invalid_storage_options_spec.rb +10 -10
  709. data/spec/mongoid/errors/invalid_time_spec.rb +10 -10
  710. data/spec/mongoid/errors/inverse_not_found_spec.rb +11 -11
  711. data/spec/mongoid/errors/mixed_client_configuration_spec.rb +42 -11
  712. data/spec/mongoid/errors/mixed_relations_spec.rb +10 -10
  713. data/spec/mongoid/errors/mongoid_error_spec.rb +23 -23
  714. data/spec/mongoid/errors/nested_attributes_metadata_not_found_spec.rb +10 -10
  715. data/spec/mongoid/errors/no_client_config_spec.rb +11 -11
  716. data/spec/mongoid/errors/no_client_database_spec.rb +40 -11
  717. data/spec/mongoid/errors/no_client_hosts_spec.rb +37 -11
  718. data/spec/mongoid/errors/no_clients_config_spec.rb +9 -9
  719. data/spec/mongoid/errors/no_environment_spec.rb +10 -10
  720. data/spec/mongoid/errors/no_map_reduce_output_spec.rb +10 -10
  721. data/spec/mongoid/errors/no_metadata_spec.rb +9 -9
  722. data/spec/mongoid/errors/no_parent_spec.rb +10 -10
  723. data/spec/mongoid/errors/readonly_association_spec.rb +34 -0
  724. data/spec/mongoid/errors/readonly_attribute_spec.rb +9 -9
  725. data/spec/mongoid/errors/readonly_document_spec.rb +9 -9
  726. data/spec/mongoid/errors/scope_overwrite_spec.rb +11 -11
  727. data/spec/mongoid/errors/too_many_nested_attribute_records_spec.rb +11 -11
  728. data/spec/mongoid/errors/unknown_attribute_spec.rb +9 -9
  729. data/spec/mongoid/errors/unsaved_document_spec.rb +10 -10
  730. data/spec/mongoid/errors/unsupported_javascript_spec.rb +11 -11
  731. data/spec/mongoid/errors/validations_spec.rb +13 -13
  732. data/spec/mongoid/extensions/array_spec.rb +92 -377
  733. data/spec/mongoid/extensions/big_decimal_spec.rb +640 -217
  734. data/spec/mongoid/extensions/binary_spec.rb +50 -23
  735. data/spec/mongoid/extensions/boolean_spec.rb +73 -107
  736. data/spec/mongoid/extensions/date_class_mongoize_spec.rb +329 -0
  737. data/spec/mongoid/extensions/date_spec.rb +62 -173
  738. data/spec/mongoid/extensions/date_time_spec.rb +42 -95
  739. data/spec/mongoid/extensions/decimal128_spec.rb +14 -17
  740. data/spec/mongoid/extensions/false_class_spec.rb +13 -24
  741. data/spec/mongoid/extensions/float_spec.rb +66 -100
  742. data/spec/mongoid/extensions/hash_spec.rb +79 -165
  743. data/spec/mongoid/extensions/integer_spec.rb +63 -91
  744. data/spec/mongoid/extensions/module_spec.rb +15 -17
  745. data/spec/mongoid/extensions/nil_class_spec.rb +4 -4
  746. data/spec/mongoid/extensions/object_id_spec.rb +161 -246
  747. data/spec/mongoid/extensions/object_spec.rb +36 -238
  748. data/spec/mongoid/extensions/range_spec.rb +259 -69
  749. data/spec/mongoid/extensions/raw_value_spec.rb +65 -0
  750. data/spec/mongoid/extensions/regexp_spec.rb +56 -41
  751. data/spec/mongoid/extensions/set_spec.rb +110 -14
  752. data/spec/mongoid/extensions/string_spec.rb +139 -237
  753. data/spec/mongoid/extensions/stringified_symbol_spec.rb +71 -0
  754. data/spec/mongoid/extensions/symbol_spec.rb +19 -62
  755. data/spec/mongoid/extensions/time_spec.rb +394 -192
  756. data/spec/mongoid/extensions/time_with_zone_spec.rb +111 -176
  757. data/spec/mongoid/extensions/true_class_spec.rb +13 -24
  758. data/spec/mongoid/extensions/vector_spec.rb +175 -0
  759. data/spec/mongoid/factory_spec.rb +378 -80
  760. data/spec/mongoid/fields/foreign_key_spec.rb +387 -267
  761. data/spec/mongoid/fields/localized_spec.rb +197 -196
  762. data/spec/mongoid/fields/standard_spec.rb +45 -58
  763. data/spec/mongoid/fields_spec.rb +1448 -525
  764. data/spec/mongoid/findable_spec.rb +659 -230
  765. data/spec/mongoid/indexable/specification_spec.rb +145 -40
  766. data/spec/mongoid/indexable_spec.rb +551 -188
  767. data/spec/mongoid/inspectable_spec.rb +126 -18
  768. data/spec/mongoid/interceptable_spec.rb +1431 -573
  769. data/spec/mongoid/interceptable_spec_models.rb +244 -0
  770. data/spec/mongoid/loadable_spec.rb +86 -0
  771. data/spec/mongoid/loading_spec.rb +114 -0
  772. data/spec/mongoid/loggable_spec.rb +5 -5
  773. data/spec/mongoid/matcher/extract_attribute_data/numeric_keys.yml +104 -0
  774. data/spec/mongoid/matcher/extract_attribute_data/traversal.yml +239 -0
  775. data/spec/mongoid/matcher/extract_attribute_spec.rb +33 -0
  776. data/spec/mongoid/matcher/regexp_budget_spec.rb +570 -0
  777. data/spec/mongoid/model_resolver_spec.rb +167 -0
  778. data/spec/mongoid/mongoizable_spec.rb +277 -0
  779. data/spec/mongoid/monkey_patches_spec.rb +215 -0
  780. data/spec/mongoid/persistable/creatable_spec.rb +256 -209
  781. data/spec/mongoid/persistable/deletable_spec.rb +369 -80
  782. data/spec/mongoid/persistable/destroyable_spec.rb +439 -80
  783. data/spec/mongoid/persistable/incrementable_spec.rb +112 -77
  784. data/spec/mongoid/persistable/logical_spec.rb +92 -51
  785. data/spec/mongoid/persistable/maxable_spec.rb +148 -0
  786. data/spec/mongoid/persistable/minable_spec.rb +148 -0
  787. data/spec/mongoid/persistable/multipliable_spec.rb +212 -0
  788. data/spec/mongoid/persistable/poppable_spec.rb +87 -47
  789. data/spec/mongoid/persistable/pullable_spec.rb +161 -82
  790. data/spec/mongoid/persistable/pushable_spec.rb +188 -110
  791. data/spec/mongoid/persistable/renamable_spec.rb +91 -51
  792. data/spec/mongoid/persistable/savable_spec.rb +373 -155
  793. data/spec/mongoid/persistable/settable_spec.rb +164 -134
  794. data/spec/mongoid/persistable/unsettable_spec.rb +94 -58
  795. data/spec/mongoid/persistable/updatable_spec.rb +235 -267
  796. data/spec/mongoid/persistable/upsertable_spec.rb +150 -38
  797. data/spec/mongoid/persistable_spec.rb +198 -128
  798. data/spec/mongoid/persistence_context_spec.rb +133 -137
  799. data/spec/mongoid/positional_spec.rb +69 -78
  800. data/spec/mongoid/railties/bson_object_id_serializer_spec.rb +27 -0
  801. data/spec/mongoid/railties/console_sandbox_spec.rb +43 -0
  802. data/spec/mongoid/relations/proxy_spec.rb +8 -6
  803. data/spec/mongoid/reloadable_spec.rb +552 -123
  804. data/spec/mongoid/scopable_spec.rb +522 -455
  805. data/spec/mongoid/search_indexable_spec.rb +838 -0
  806. data/spec/mongoid/selectable_spec.rb +41 -53
  807. data/spec/mongoid/serializable_spec.rb +394 -319
  808. data/spec/mongoid/shardable_models.rb +76 -0
  809. data/spec/mongoid/shardable_spec.rb +245 -41
  810. data/spec/mongoid/stateful_spec.rb +170 -45
  811. data/spec/mongoid/tasks/database_rake_spec.rb +284 -133
  812. data/spec/mongoid/tasks/database_spec.rb +245 -51
  813. data/spec/mongoid/tasks/encryption_spec.rb +189 -0
  814. data/spec/mongoid/threaded_spec.rb +118 -85
  815. data/spec/mongoid/timestamps/created/short_spec.rb +15 -16
  816. data/spec/mongoid/timestamps/created_spec.rb +36 -14
  817. data/spec/mongoid/timestamps/timeless_spec.rb +230 -47
  818. data/spec/mongoid/timestamps/updated/short_spec.rb +26 -30
  819. data/spec/mongoid/timestamps/updated_spec.rb +23 -27
  820. data/spec/mongoid/timestamps_spec.rb +408 -34
  821. data/spec/mongoid/timestamps_spec_models.rb +67 -0
  822. data/spec/mongoid/touchable_spec.rb +1235 -136
  823. data/spec/mongoid/touchable_spec_models.rb +178 -0
  824. data/spec/mongoid/traversable_spec.rb +1188 -79
  825. data/spec/mongoid/validatable/associated_spec.rb +86 -89
  826. data/spec/mongoid/validatable/format_spec.rb +26 -33
  827. data/spec/mongoid/validatable/length_spec.rb +64 -85
  828. data/spec/mongoid/validatable/numericality_spec.rb +124 -14
  829. data/spec/mongoid/validatable/presence_spec.rb +168 -215
  830. data/spec/mongoid/validatable/uniqueness_spec.rb +702 -826
  831. data/spec/mongoid/validatable_spec.rb +76 -105
  832. data/spec/mongoid/warnings_spec.rb +32 -0
  833. data/spec/mongoid_spec.rb +67 -30
  834. data/spec/rails/controller_extension/controller_runtime_spec.rb +59 -60
  835. data/spec/rails/mongoid_spec.rb +38 -139
  836. data/spec/shared/CANDIDATE.md +28 -0
  837. data/spec/shared/bin/s3-copy +45 -0
  838. data/spec/shared/bin/s3-upload +69 -0
  839. data/spec/shared/lib/mrss/cluster_config.rb +16 -41
  840. data/spec/shared/lib/mrss/constraints.rb +44 -59
  841. data/spec/shared/lib/mrss/docker_runner.rb +42 -20
  842. data/spec/shared/lib/mrss/eg_config_utils.rb +20 -0
  843. data/spec/shared/lib/mrss/event_subscriber.rb +210 -0
  844. data/spec/shared/lib/mrss/lite_constraints.rb +48 -1
  845. data/spec/shared/lib/mrss/release/candidate.rb +287 -0
  846. data/spec/shared/lib/mrss/release/product_data.rb +144 -0
  847. data/spec/shared/lib/mrss/server_version_registry.rb +18 -24
  848. data/spec/shared/lib/mrss/session_registry.rb +69 -0
  849. data/spec/shared/lib/mrss/session_registry_legacy.rb +60 -0
  850. data/spec/shared/lib/mrss/spec_organizer.rb +48 -2
  851. data/spec/shared/lib/mrss/utils.rb +28 -6
  852. data/spec/shared/lib/tasks/candidate.rake +64 -0
  853. data/spec/shared/share/Dockerfile.erb +114 -96
  854. data/spec/shared/share/haproxy-1.conf +16 -0
  855. data/spec/shared/share/haproxy-2.conf +17 -0
  856. data/spec/shared/shlib/config.sh +27 -0
  857. data/spec/shared/shlib/distro.sh +12 -1
  858. data/spec/shared/shlib/server.sh +148 -65
  859. data/spec/shared/shlib/set_env.sh +56 -80
  860. data/spec/spec_helper.rb +104 -72
  861. data/spec/support/authorization.rb +2 -0
  862. data/spec/support/client_registry.rb +9 -0
  863. data/spec/support/constraints.rb +37 -22
  864. data/spec/support/crypt/models.rb +206 -0
  865. data/spec/support/crypt.rb +77 -0
  866. data/spec/support/expectations.rb +38 -17
  867. data/spec/support/feature_sandbox.rb +69 -0
  868. data/spec/support/helpers.rb +11 -0
  869. data/spec/support/immutable_ids.rb +118 -0
  870. data/spec/support/macros.rb +106 -1
  871. data/spec/support/models/account.rb +38 -0
  872. data/spec/support/models/acolyte.rb +20 -0
  873. data/spec/support/models/actor.rb +21 -0
  874. data/spec/support/models/actress.rb +4 -0
  875. data/spec/support/models/address.rb +88 -0
  876. data/spec/support/models/address_component.rb +8 -0
  877. data/spec/support/models/address_number.rb +9 -0
  878. data/spec/support/models/agency.rb +8 -0
  879. data/spec/support/models/agent.rb +19 -0
  880. data/spec/support/models/album.rb +20 -0
  881. data/spec/support/models/alert.rb +10 -0
  882. data/spec/support/models/animal.rb +28 -0
  883. data/spec/support/models/answer.rb +9 -0
  884. data/spec/support/models/appointment.rb +10 -0
  885. data/spec/support/models/armrest.rb +9 -0
  886. data/spec/support/models/article.rb +14 -0
  887. data/spec/support/models/artist.rb +95 -0
  888. data/spec/support/models/artwork.rb +7 -0
  889. data/spec/support/models/audible_sound.rb +3 -0
  890. data/spec/support/models/audio.rb +8 -0
  891. data/spec/support/models/augmentation.rb +25 -0
  892. data/spec/support/models/author.rb +9 -0
  893. data/spec/support/models/baby.rb +7 -0
  894. data/spec/support/models/band.rb +46 -0
  895. data/spec/support/models/bar.rb +13 -0
  896. data/spec/support/models/basic.rb +8 -0
  897. data/spec/support/models/bed.rb +3 -0
  898. data/spec/support/models/big_palette.rb +4 -0
  899. data/spec/support/models/birthday.rb +14 -0
  900. data/spec/support/models/bolt.rb +7 -0
  901. data/spec/support/models/bomb.rb +7 -0
  902. data/spec/support/models/book.rb +20 -0
  903. data/spec/support/models/breed.rb +7 -0
  904. data/spec/support/models/browser.rb +8 -0
  905. data/spec/support/models/building.rb +10 -0
  906. data/spec/support/models/building_address.rb +10 -0
  907. data/spec/support/models/bus.rb +10 -0
  908. data/spec/support/models/business.rb +8 -0
  909. data/spec/support/models/callback_test.rb +12 -0
  910. data/spec/support/models/canvas.rb +27 -0
  911. data/spec/support/models/car.rb +3 -0
  912. data/spec/support/models/cat.rb +9 -0
  913. data/spec/support/models/catalog.rb +24 -0
  914. data/spec/support/models/category.rb +11 -0
  915. data/spec/support/models/child.rb +7 -0
  916. data/spec/support/models/child_doc.rb +24 -0
  917. data/spec/support/models/church.rb +9 -0
  918. data/spec/support/models/circle.rb +5 -0
  919. data/spec/support/models/circuit.rb +7 -0
  920. data/spec/support/models/circus.rb +12 -0
  921. data/spec/support/models/code.rb +17 -0
  922. data/spec/support/models/coding/pull_request.rb +11 -0
  923. data/spec/support/models/coding.rb +3 -0
  924. data/spec/support/models/comment.rb +18 -0
  925. data/spec/support/models/company.rb +9 -0
  926. data/spec/support/models/consumption_period.rb +9 -0
  927. data/spec/support/models/contextable_item.rb +8 -0
  928. data/spec/support/models/contractor.rb +8 -0
  929. data/spec/support/models/cookie.rb +8 -0
  930. data/spec/support/models/country_code.rb +12 -0
  931. data/spec/support/models/courier_job.rb +7 -0
  932. data/spec/support/models/cover.rb +10 -0
  933. data/spec/support/models/crate.rb +12 -0
  934. data/spec/support/models/customer.rb +10 -0
  935. data/spec/support/models/customer_address.rb +11 -0
  936. data/spec/support/models/deed.rb +8 -0
  937. data/spec/support/models/definition.rb +12 -0
  938. data/spec/support/models/delegating_patient.rb +15 -0
  939. data/spec/support/models/description.rb +13 -0
  940. data/spec/support/models/dictionary.rb +19 -0
  941. data/spec/support/models/division.rb +13 -0
  942. data/spec/support/models/doctor.rb +14 -0
  943. data/spec/support/models/dog.rb +10 -0
  944. data/spec/support/models/dokument.rb +9 -0
  945. data/spec/support/models/draft.rb +11 -0
  946. data/spec/support/models/dragon.rb +7 -0
  947. data/spec/support/models/driver.rb +10 -0
  948. data/spec/support/models/drug.rb +9 -0
  949. data/spec/support/models/dungeon.rb +7 -0
  950. data/spec/support/models/edit.rb +8 -0
  951. data/spec/support/models/email.rb +9 -0
  952. data/spec/support/models/employer.rb +7 -0
  953. data/spec/support/models/entry.rb +9 -0
  954. data/spec/support/models/eraser.rb +3 -0
  955. data/spec/support/models/even.rb +10 -0
  956. data/spec/support/models/event.rb +24 -0
  957. data/spec/support/models/exhibition.rb +7 -0
  958. data/spec/support/models/exhibitor.rb +9 -0
  959. data/spec/support/models/explosion.rb +7 -0
  960. data/spec/support/models/eye.rb +11 -0
  961. data/spec/support/models/eye_bowl.rb +11 -0
  962. data/spec/support/models/face.rb +10 -0
  963. data/spec/support/models/fanatic.rb +9 -0
  964. data/spec/support/models/favorite.rb +15 -0
  965. data/spec/support/models/filesystem.rb +8 -0
  966. data/spec/support/models/fire_hydrant.rb +9 -0
  967. data/spec/support/models/firefox.rb +6 -0
  968. data/spec/support/models/fish.rb +9 -0
  969. data/spec/support/models/folder.rb +8 -0
  970. data/spec/support/models/folder_item.rb +10 -0
  971. data/spec/support/models/fruits.rb +41 -0
  972. data/spec/support/models/game.rb +21 -0
  973. data/spec/support/models/ghost.rb +9 -0
  974. data/spec/support/models/guitar.rb +4 -0
  975. data/spec/support/models/hole.rb +12 -0
  976. data/spec/support/models/home.rb +7 -0
  977. data/spec/support/models/house.rb +9 -0
  978. data/spec/support/models/html_writer.rb +5 -0
  979. data/spec/support/models/id_key.rb +9 -0
  980. data/spec/support/models/idnodef.rb +7 -0
  981. data/spec/support/models/image.rb +24 -0
  982. data/spec/support/models/implant.rb +27 -0
  983. data/spec/support/models/instrument.rb +8 -0
  984. data/spec/support/models/item.rb +11 -0
  985. data/spec/support/models/jar.rb +9 -0
  986. data/spec/support/models/kaleidoscope.rb +9 -0
  987. data/spec/support/models/kangaroo.rb +7 -0
  988. data/spec/support/models/label.rb +53 -0
  989. data/spec/support/models/language.rb +8 -0
  990. data/spec/support/models/lat_lng.rb +23 -0
  991. data/spec/support/models/league.rb +14 -0
  992. data/spec/support/models/learner.rb +4 -0
  993. data/spec/support/models/line_item.rb +9 -0
  994. data/spec/support/models/location.rb +11 -0
  995. data/spec/support/models/login.rb +10 -0
  996. data/spec/support/models/manufacturer.rb +9 -0
  997. data/spec/support/models/meat.rb +7 -0
  998. data/spec/support/models/membership.rb +8 -0
  999. data/spec/support/models/message.rb +13 -0
  1000. data/spec/support/models/minim.rb +6 -0
  1001. data/spec/support/models/mixed_drink.rb +7 -0
  1002. data/spec/support/models/modifier_models.rb +25 -0
  1003. data/spec/support/models/mop.rb +24 -0
  1004. data/spec/support/models/movie.rb +16 -0
  1005. data/spec/support/models/my_hash.rb +4 -0
  1006. data/spec/support/models/name.rb +36 -0
  1007. data/spec/support/models/name_only.rb +8 -0
  1008. data/spec/support/models/node.rb +8 -0
  1009. data/spec/support/models/note.rb +20 -0
  1010. data/spec/support/models/nut.rb +7 -0
  1011. data/spec/support/models/odd.rb +10 -0
  1012. data/spec/support/models/order.rb +13 -0
  1013. data/spec/support/models/ordered_post.rb +14 -0
  1014. data/spec/support/models/ordered_preference.rb +9 -0
  1015. data/spec/support/models/oscar.rb +17 -0
  1016. data/spec/support/models/other_owner_object.rb +4 -0
  1017. data/spec/support/models/override.rb +11 -0
  1018. data/spec/support/models/ownable.rb +9 -0
  1019. data/spec/support/models/owner.rb +11 -0
  1020. data/spec/support/models/pack.rb +5 -0
  1021. data/spec/support/models/page.rb +19 -0
  1022. data/spec/support/models/page_question.rb +7 -0
  1023. data/spec/support/models/palette.rb +10 -0
  1024. data/spec/support/models/parent.rb +8 -0
  1025. data/spec/support/models/parent_doc.rb +9 -0
  1026. data/spec/support/models/passport.rb +23 -0
  1027. data/spec/support/models/patient.rb +12 -0
  1028. data/spec/support/models/pdf_writer.rb +5 -0
  1029. data/spec/support/models/pencil.rb +3 -0
  1030. data/spec/support/models/person.rb +237 -0
  1031. data/spec/support/models/pet.rb +26 -0
  1032. data/spec/support/models/pet_owner.rb +9 -0
  1033. data/spec/support/models/phone.rb +13 -0
  1034. data/spec/support/models/piano.rb +4 -0
  1035. data/spec/support/models/pizza.rb +10 -0
  1036. data/spec/support/models/player.rb +40 -0
  1037. data/spec/support/models/post.rb +53 -0
  1038. data/spec/support/models/post_genre.rb +9 -0
  1039. data/spec/support/models/powerup.rb +25 -0
  1040. data/spec/support/models/preference.rb +12 -0
  1041. data/spec/support/models/princess.rb +13 -0
  1042. data/spec/support/models/product.rb +23 -0
  1043. data/spec/support/models/profile.rb +19 -0
  1044. data/spec/support/models/pronunciation.rb +8 -0
  1045. data/spec/support/models/pub.rb +9 -0
  1046. data/spec/support/models/publication/encyclopedia.rb +11 -0
  1047. data/spec/support/models/publication/review.rb +13 -0
  1048. data/spec/support/models/publication.rb +4 -0
  1049. data/spec/support/models/purchase.rb +7 -0
  1050. data/spec/support/models/purchased_item.rb +11 -0
  1051. data/spec/support/models/purse.rb +9 -0
  1052. data/spec/support/models/question.rb +11 -0
  1053. data/spec/support/models/quiz.rb +10 -0
  1054. data/spec/support/models/rating.rb +11 -0
  1055. data/spec/support/models/record.rb +54 -0
  1056. data/spec/support/models/registry.rb +8 -0
  1057. data/spec/support/models/role.rb +10 -0
  1058. data/spec/support/models/root_category.rb +7 -0
  1059. data/spec/support/models/sandbox/app/models/app_models_message.rb +3 -0
  1060. data/spec/support/models/sandbox/lib/models/lib_models_message.rb +3 -0
  1061. data/spec/support/models/sandbox/sandbox_message.rb +3 -0
  1062. data/spec/support/models/sandbox/sandbox_user.rb +3 -0
  1063. data/spec/support/models/sandbox/subdir/sandbox_comment.rb +3 -0
  1064. data/spec/support/models/sandwich.rb +12 -0
  1065. data/spec/support/models/scheduler.rb +9 -0
  1066. data/spec/support/models/school.rb +14 -0
  1067. data/spec/support/models/scribe.rb +8 -0
  1068. data/spec/support/models/sealer.rb +7 -0
  1069. data/spec/support/models/seat.rb +24 -0
  1070. data/spec/support/models/seo.rb +12 -0
  1071. data/spec/support/models/series.rb +8 -0
  1072. data/spec/support/models/server.rb +16 -0
  1073. data/spec/support/models/service.rb +25 -0
  1074. data/spec/support/models/shape.rb +15 -0
  1075. data/spec/support/models/shelf.rb +8 -0
  1076. data/spec/support/models/shield.rb +19 -0
  1077. data/spec/support/models/shipment_address.rb +5 -0
  1078. data/spec/support/models/shipping_container.rb +8 -0
  1079. data/spec/support/models/shipping_pack.rb +5 -0
  1080. data/spec/support/models/shirt.rb +11 -0
  1081. data/spec/support/models/shop.rb +9 -0
  1082. data/spec/support/models/short_agent.rb +6 -0
  1083. data/spec/support/models/short_quiz.rb +8 -0
  1084. data/spec/support/models/simple.rb +8 -0
  1085. data/spec/support/models/slave.rb +9 -0
  1086. data/spec/support/models/song.rb +10 -0
  1087. data/spec/support/models/sound.rb +8 -0
  1088. data/spec/support/models/spacer.rb +7 -0
  1089. data/spec/support/models/square.rb +6 -0
  1090. data/spec/support/models/staff.rb +9 -0
  1091. data/spec/support/models/store_as_dup_test1.rb +8 -0
  1092. data/spec/support/models/store_as_dup_test2.rb +8 -0
  1093. data/spec/support/models/store_as_dup_test3.rb +8 -0
  1094. data/spec/support/models/store_as_dup_test4.rb +8 -0
  1095. data/spec/support/models/strategy.rb +5 -0
  1096. data/spec/support/models/student.rb +14 -0
  1097. data/spec/support/models/sub_item.rb +5 -0
  1098. data/spec/support/models/subscription.rb +8 -0
  1099. data/spec/support/models/survey.rb +8 -0
  1100. data/spec/support/models/symptom.rb +9 -0
  1101. data/spec/support/models/system_role.rb +7 -0
  1102. data/spec/support/models/tag.rb +11 -0
  1103. data/spec/support/models/target.rb +8 -0
  1104. data/spec/support/models/template.rb +8 -0
  1105. data/spec/support/models/thing.rb +12 -0
  1106. data/spec/support/models/threadlocker.rb +7 -0
  1107. data/spec/support/models/title.rb +5 -0
  1108. data/spec/support/models/tool.rb +11 -0
  1109. data/spec/support/models/topping.rb +8 -0
  1110. data/spec/support/models/toy.rb +9 -0
  1111. data/spec/support/models/track.rb +41 -0
  1112. data/spec/support/models/translation.rb +8 -0
  1113. data/spec/support/models/tree.rb +11 -0
  1114. data/spec/support/models/truck.rb +7 -0
  1115. data/spec/support/models/user.rb +23 -0
  1116. data/spec/support/models/user_account.rb +13 -0
  1117. data/spec/support/models/validation_callback.rb +13 -0
  1118. data/spec/support/models/vehicle.rb +18 -0
  1119. data/spec/support/models/version.rb +8 -0
  1120. data/spec/support/models/vertex.rb +8 -0
  1121. data/spec/support/models/vet_visit.rb +8 -0
  1122. data/spec/support/models/video.rb +16 -0
  1123. data/spec/support/models/video_game.rb +3 -0
  1124. data/spec/support/models/washer.rb +7 -0
  1125. data/spec/support/models/weapon.rb +25 -0
  1126. data/spec/support/models/wiki_page.rb +17 -0
  1127. data/spec/support/models/word.rb +18 -0
  1128. data/spec/support/models/word_origin.rb +13 -0
  1129. data/spec/support/models/writer.rb +14 -0
  1130. data/spec/support/rails_mock.rb +27 -0
  1131. data/spec/support/schema_maps/schema_map_aws.json +17 -0
  1132. data/spec/support/schema_maps/schema_map_aws_key_alt_names.json +12 -0
  1133. data/spec/support/schema_maps/schema_map_azure.json +17 -0
  1134. data/spec/support/schema_maps/schema_map_azure_key_alt_names.json +12 -0
  1135. data/spec/support/schema_maps/schema_map_gcp.json +17 -0
  1136. data/spec/support/schema_maps/schema_map_gcp_key_alt_names.json +12 -0
  1137. data/spec/support/schema_maps/schema_map_kmip.json +17 -0
  1138. data/spec/support/schema_maps/schema_map_kmip_key_alt_names.json +12 -0
  1139. data/spec/support/schema_maps/schema_map_local.json +18 -0
  1140. data/spec/support/schema_maps/schema_map_local_key_alt_names.json +12 -0
  1141. data/spec/support/shared/time.rb +34 -0
  1142. data/spec/support/sinatra_mock.rb +9 -0
  1143. data/spec/support/spec_config.rb +45 -14
  1144. metadata +1341 -780
  1145. checksums.yaml.gz.sig +0 -0
  1146. data/lib/mongoid/association/referenced/eager.rb +0 -162
  1147. data/lib/mongoid/association/referenced/has_one/nested_builder.rb +0 -126
  1148. data/lib/mongoid/contextual/geo_near.rb +0 -267
  1149. data/lib/mongoid/criteria/queryable/forwardable.rb +0 -65
  1150. data/lib/mongoid/errors/eager_load.rb +0 -22
  1151. data/lib/mongoid/errors/invalid_session_use.rb +0 -24
  1152. data/lib/mongoid/errors/invalid_storage_parent.rb +0 -26
  1153. data/lib/mongoid/errors/invalid_value.rb +0 -16
  1154. data/lib/mongoid/matchable/all.rb +0 -28
  1155. data/lib/mongoid/matchable/and.rb +0 -30
  1156. data/lib/mongoid/matchable/default.rb +0 -119
  1157. data/lib/mongoid/matchable/elem_match.rb +0 -34
  1158. data/lib/mongoid/matchable/eq.rb +0 -22
  1159. data/lib/mongoid/matchable/exists.rb +0 -23
  1160. data/lib/mongoid/matchable/gt.rb +0 -23
  1161. data/lib/mongoid/matchable/gte.rb +0 -23
  1162. data/lib/mongoid/matchable/in.rb +0 -24
  1163. data/lib/mongoid/matchable/lt.rb +0 -23
  1164. data/lib/mongoid/matchable/lte.rb +0 -23
  1165. data/lib/mongoid/matchable/ne.rb +0 -21
  1166. data/lib/mongoid/matchable/nin.rb +0 -22
  1167. data/lib/mongoid/matchable/nor.rb +0 -37
  1168. data/lib/mongoid/matchable/or.rb +0 -33
  1169. data/lib/mongoid/matchable/regexp.rb +0 -27
  1170. data/lib/mongoid/matchable/size.rb +0 -21
  1171. data/lib/mongoid/query_cache.rb +0 -331
  1172. data/lib/support/ruby_version.rb +0 -26
  1173. data/spec/app/models/account.rb +0 -36
  1174. data/spec/app/models/acolyte.rb +0 -17
  1175. data/spec/app/models/actor.rb +0 -18
  1176. data/spec/app/models/actress.rb +0 -2
  1177. data/spec/app/models/address.rb +0 -79
  1178. data/spec/app/models/address_component.rb +0 -5
  1179. data/spec/app/models/address_number.rb +0 -6
  1180. data/spec/app/models/agency.rb +0 -5
  1181. data/spec/app/models/agent.rb +0 -16
  1182. data/spec/app/models/album.rb +0 -18
  1183. data/spec/app/models/alert.rb +0 -7
  1184. data/spec/app/models/animal.rb +0 -26
  1185. data/spec/app/models/answer.rb +0 -6
  1186. data/spec/app/models/appointment.rb +0 -7
  1187. data/spec/app/models/article.rb +0 -12
  1188. data/spec/app/models/artist.rb +0 -87
  1189. data/spec/app/models/artwork.rb +0 -4
  1190. data/spec/app/models/audio.rb +0 -5
  1191. data/spec/app/models/augmentation.rb +0 -11
  1192. data/spec/app/models/author.rb +0 -6
  1193. data/spec/app/models/baby.rb +0 -4
  1194. data/spec/app/models/band.rb +0 -31
  1195. data/spec/app/models/bar.rb +0 -10
  1196. data/spec/app/models/basic.rb +0 -6
  1197. data/spec/app/models/bed.rb +0 -1
  1198. data/spec/app/models/big_palette.rb +0 -2
  1199. data/spec/app/models/birthday.rb +0 -13
  1200. data/spec/app/models/bomb.rb +0 -4
  1201. data/spec/app/models/book.rb +0 -16
  1202. data/spec/app/models/breed.rb +0 -4
  1203. data/spec/app/models/browser.rb +0 -6
  1204. data/spec/app/models/building.rb +0 -6
  1205. data/spec/app/models/building_address.rb +0 -7
  1206. data/spec/app/models/bus.rb +0 -7
  1207. data/spec/app/models/business.rb +0 -5
  1208. data/spec/app/models/callback_test.rb +0 -9
  1209. data/spec/app/models/canvas.rb +0 -25
  1210. data/spec/app/models/car.rb +0 -1
  1211. data/spec/app/models/cat.rb +0 -8
  1212. data/spec/app/models/category.rb +0 -8
  1213. data/spec/app/models/child.rb +0 -4
  1214. data/spec/app/models/child_doc.rb +0 -22
  1215. data/spec/app/models/church.rb +0 -6
  1216. data/spec/app/models/circle.rb +0 -3
  1217. data/spec/app/models/circuit.rb +0 -4
  1218. data/spec/app/models/circus.rb +0 -7
  1219. data/spec/app/models/code.rb +0 -11
  1220. data/spec/app/models/comment.rb +0 -16
  1221. data/spec/app/models/company.rb +0 -5
  1222. data/spec/app/models/consumption_period.rb +0 -7
  1223. data/spec/app/models/contextable_item.rb +0 -5
  1224. data/spec/app/models/contractor.rb +0 -5
  1225. data/spec/app/models/cookie.rb +0 -6
  1226. data/spec/app/models/country_code.rb +0 -8
  1227. data/spec/app/models/courier_job.rb +0 -4
  1228. data/spec/app/models/customer.rb +0 -11
  1229. data/spec/app/models/customer_address.rb +0 -12
  1230. data/spec/app/models/definition.rb +0 -9
  1231. data/spec/app/models/delegating_patient.rb +0 -16
  1232. data/spec/app/models/description.rb +0 -11
  1233. data/spec/app/models/dictionary.rb +0 -10
  1234. data/spec/app/models/division.rb +0 -10
  1235. data/spec/app/models/doctor.rb +0 -12
  1236. data/spec/app/models/dog.rb +0 -7
  1237. data/spec/app/models/dokument.rb +0 -6
  1238. data/spec/app/models/draft.rb +0 -9
  1239. data/spec/app/models/dragon.rb +0 -4
  1240. data/spec/app/models/driver.rb +0 -7
  1241. data/spec/app/models/drug.rb +0 -6
  1242. data/spec/app/models/dungeon.rb +0 -4
  1243. data/spec/app/models/edit.rb +0 -5
  1244. data/spec/app/models/email.rb +0 -6
  1245. data/spec/app/models/employer.rb +0 -5
  1246. data/spec/app/models/entry.rb +0 -6
  1247. data/spec/app/models/eraser.rb +0 -1
  1248. data/spec/app/models/even.rb +0 -7
  1249. data/spec/app/models/event.rb +0 -22
  1250. data/spec/app/models/exhibition.rb +0 -4
  1251. data/spec/app/models/exhibitor.rb +0 -6
  1252. data/spec/app/models/explosion.rb +0 -4
  1253. data/spec/app/models/eye.rb +0 -9
  1254. data/spec/app/models/eye_bowl.rb +0 -9
  1255. data/spec/app/models/face.rb +0 -8
  1256. data/spec/app/models/favorite.rb +0 -6
  1257. data/spec/app/models/filesystem.rb +0 -5
  1258. data/spec/app/models/fire_hydrant.rb +0 -6
  1259. data/spec/app/models/firefox.rb +0 -4
  1260. data/spec/app/models/fish.rb +0 -7
  1261. data/spec/app/models/folder.rb +0 -7
  1262. data/spec/app/models/folder_item.rb +0 -9
  1263. data/spec/app/models/fruits.rb +0 -34
  1264. data/spec/app/models/game.rb +0 -19
  1265. data/spec/app/models/ghost.rb +0 -7
  1266. data/spec/app/models/home.rb +0 -4
  1267. data/spec/app/models/house.rb +0 -6
  1268. data/spec/app/models/html_writer.rb +0 -3
  1269. data/spec/app/models/id_key.rb +0 -6
  1270. data/spec/app/models/image.rb +0 -22
  1271. data/spec/app/models/implant.rb +0 -16
  1272. data/spec/app/models/item.rb +0 -8
  1273. data/spec/app/models/jar.rb +0 -7
  1274. data/spec/app/models/kaleidoscope.rb +0 -6
  1275. data/spec/app/models/kangaroo.rb +0 -4
  1276. data/spec/app/models/label.rb +0 -47
  1277. data/spec/app/models/language.rb +0 -5
  1278. data/spec/app/models/lat_lng.rb +0 -15
  1279. data/spec/app/models/league.rb +0 -11
  1280. data/spec/app/models/learner.rb +0 -2
  1281. data/spec/app/models/line_item.rb +0 -6
  1282. data/spec/app/models/location.rb +0 -8
  1283. data/spec/app/models/login.rb +0 -8
  1284. data/spec/app/models/manufacturer.rb +0 -7
  1285. data/spec/app/models/meat.rb +0 -4
  1286. data/spec/app/models/membership.rb +0 -4
  1287. data/spec/app/models/message.rb +0 -11
  1288. data/spec/app/models/minim.rb +0 -7
  1289. data/spec/app/models/mixed_drink.rb +0 -4
  1290. data/spec/app/models/movie.rb +0 -13
  1291. data/spec/app/models/my_hash.rb +0 -2
  1292. data/spec/app/models/name.rb +0 -24
  1293. data/spec/app/models/node.rb +0 -5
  1294. data/spec/app/models/note.rb +0 -17
  1295. data/spec/app/models/odd.rb +0 -7
  1296. data/spec/app/models/ordered_post.rb +0 -11
  1297. data/spec/app/models/ordered_preference.rb +0 -6
  1298. data/spec/app/models/oscar.rb +0 -14
  1299. data/spec/app/models/other_owner_object.rb +0 -2
  1300. data/spec/app/models/override.rb +0 -16
  1301. data/spec/app/models/ownable.rb +0 -6
  1302. data/spec/app/models/owner.rb +0 -6
  1303. data/spec/app/models/pack.rb +0 -3
  1304. data/spec/app/models/page.rb +0 -16
  1305. data/spec/app/models/page_question.rb +0 -4
  1306. data/spec/app/models/palette.rb +0 -7
  1307. data/spec/app/models/parent.rb +0 -5
  1308. data/spec/app/models/parent_doc.rb +0 -6
  1309. data/spec/app/models/passport.rb +0 -5
  1310. data/spec/app/models/patient.rb +0 -9
  1311. data/spec/app/models/pdf_writer.rb +0 -3
  1312. data/spec/app/models/pencil.rb +0 -1
  1313. data/spec/app/models/person.rb +0 -210
  1314. data/spec/app/models/pet.rb +0 -23
  1315. data/spec/app/models/pet_owner.rb +0 -6
  1316. data/spec/app/models/phone.rb +0 -9
  1317. data/spec/app/models/pizza.rb +0 -7
  1318. data/spec/app/models/player.rb +0 -35
  1319. data/spec/app/models/post.rb +0 -50
  1320. data/spec/app/models/post_genre.rb +0 -6
  1321. data/spec/app/models/powerup.rb +0 -11
  1322. data/spec/app/models/preference.rb +0 -9
  1323. data/spec/app/models/princess.rb +0 -10
  1324. data/spec/app/models/product.rb +0 -17
  1325. data/spec/app/models/profile.rb +0 -5
  1326. data/spec/app/models/pronunciation.rb +0 -5
  1327. data/spec/app/models/pub.rb +0 -6
  1328. data/spec/app/models/purchase.rb +0 -4
  1329. data/spec/app/models/question.rb +0 -8
  1330. data/spec/app/models/quiz.rb +0 -7
  1331. data/spec/app/models/rating.rb +0 -8
  1332. data/spec/app/models/record.rb +0 -52
  1333. data/spec/app/models/registry.rb +0 -4
  1334. data/spec/app/models/role.rb +0 -7
  1335. data/spec/app/models/root_category.rb +0 -4
  1336. data/spec/app/models/sandwich.rb +0 -9
  1337. data/spec/app/models/scheduler.rb +0 -7
  1338. data/spec/app/models/seo.rb +0 -7
  1339. data/spec/app/models/series.rb +0 -4
  1340. data/spec/app/models/server.rb +0 -13
  1341. data/spec/app/models/service.rb +0 -22
  1342. data/spec/app/models/shape.rb +0 -12
  1343. data/spec/app/models/shelf.rb +0 -5
  1344. data/spec/app/models/shipment_address.rb +0 -3
  1345. data/spec/app/models/shipping_container.rb +0 -5
  1346. data/spec/app/models/shipping_pack.rb +0 -3
  1347. data/spec/app/models/shop.rb +0 -6
  1348. data/spec/app/models/short_agent.rb +0 -4
  1349. data/spec/app/models/short_quiz.rb +0 -5
  1350. data/spec/app/models/simple.rb +0 -5
  1351. data/spec/app/models/slave.rb +0 -6
  1352. data/spec/app/models/song.rb +0 -8
  1353. data/spec/app/models/sound.rb +0 -5
  1354. data/spec/app/models/square.rb +0 -4
  1355. data/spec/app/models/staff.rb +0 -7
  1356. data/spec/app/models/store_as_dup_test1.rb +0 -5
  1357. data/spec/app/models/store_as_dup_test2.rb +0 -5
  1358. data/spec/app/models/store_as_dup_test3.rb +0 -7
  1359. data/spec/app/models/store_as_dup_test4.rb +0 -7
  1360. data/spec/app/models/strategy.rb +0 -3
  1361. data/spec/app/models/sub_item.rb +0 -3
  1362. data/spec/app/models/subscription.rb +0 -5
  1363. data/spec/app/models/survey.rb +0 -5
  1364. data/spec/app/models/symptom.rb +0 -6
  1365. data/spec/app/models/tag.rb +0 -8
  1366. data/spec/app/models/target.rb +0 -5
  1367. data/spec/app/models/template.rb +0 -5
  1368. data/spec/app/models/thing.rb +0 -9
  1369. data/spec/app/models/title.rb +0 -3
  1370. data/spec/app/models/tool.rb +0 -8
  1371. data/spec/app/models/topping.rb +0 -5
  1372. data/spec/app/models/track.rb +0 -38
  1373. data/spec/app/models/translation.rb +0 -5
  1374. data/spec/app/models/tree.rb +0 -9
  1375. data/spec/app/models/truck.rb +0 -3
  1376. data/spec/app/models/user.rb +0 -21
  1377. data/spec/app/models/user_account.rb +0 -10
  1378. data/spec/app/models/validation_callback.rb +0 -10
  1379. data/spec/app/models/vehicle.rb +0 -11
  1380. data/spec/app/models/version.rb +0 -5
  1381. data/spec/app/models/vertex.rb +0 -6
  1382. data/spec/app/models/vet_visit.rb +0 -5
  1383. data/spec/app/models/video.rb +0 -13
  1384. data/spec/app/models/video_game.rb +0 -1
  1385. data/spec/app/models/weapon.rb +0 -11
  1386. data/spec/app/models/wiki_page.rb +0 -14
  1387. data/spec/app/models/word.rb +0 -15
  1388. data/spec/app/models/word_origin.rb +0 -11
  1389. data/spec/app/models/writer.rb +0 -11
  1390. data/spec/integration/matchable_spec.rb +0 -680
  1391. data/spec/mongoid/contextual/geo_near_spec.rb +0 -469
  1392. data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +0 -58
  1393. data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +0 -58
  1394. data/spec/mongoid/criteria/queryable/forwardable_spec.rb +0 -87
  1395. data/spec/mongoid/errors/eager_load_spec.rb +0 -29
  1396. data/spec/mongoid/extensions_spec.rb +0 -29
  1397. data/spec/mongoid/matchable/all_spec.rb +0 -31
  1398. data/spec/mongoid/matchable/and_spec.rb +0 -187
  1399. data/spec/mongoid/matchable/default_spec.rb +0 -137
  1400. data/spec/mongoid/matchable/elem_match_spec.rb +0 -106
  1401. data/spec/mongoid/matchable/eq_spec.rb +0 -48
  1402. data/spec/mongoid/matchable/exists_spec.rb +0 -57
  1403. data/spec/mongoid/matchable/gt_spec.rb +0 -86
  1404. data/spec/mongoid/matchable/gte_spec.rb +0 -84
  1405. data/spec/mongoid/matchable/in_spec.rb +0 -49
  1406. data/spec/mongoid/matchable/lt_spec.rb +0 -85
  1407. data/spec/mongoid/matchable/lte_spec.rb +0 -85
  1408. data/spec/mongoid/matchable/ne_spec.rb +0 -46
  1409. data/spec/mongoid/matchable/nin_spec.rb +0 -48
  1410. data/spec/mongoid/matchable/nor_spec.rb +0 -209
  1411. data/spec/mongoid/matchable/or_spec.rb +0 -131
  1412. data/spec/mongoid/matchable/regexp_spec.rb +0 -59
  1413. data/spec/mongoid/matchable/size_spec.rb +0 -25
  1414. data/spec/mongoid/matchable_spec.rb +0 -853
  1415. data/spec/mongoid/query_cache_spec.rb +0 -618
  1416. data/spec/support/cluster_config.rb +0 -158
  1417. data/spec/support/session_registry.rb +0 -50
  1418. data.tar.gz.sig +0 -0
  1419. metadata.gz.sig +0 -1
  1420. /data/spec/{app → support}/models/array_field.rb +0 -0
  1421. /data/spec/{app → support}/models/updatable.rb +0 -0
@@ -1,115 +1,118 @@
1
- require "spec_helper"
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require_relative 'interceptable_spec_models'
2
5
 
3
6
  describe Mongoid::Interceptable do
7
+ before do
8
+ # The find and initialize callbacks I added were causing failures
9
+ # because they were causing updates when we were asserting no updates
10
+ # happened.
11
+ Label.reset_callbacks(:initialize)
12
+ Label.reset_callbacks(:find)
13
+ end
4
14
 
5
15
  class TestClass
6
16
  include Mongoid::Interceptable
7
17
 
8
18
  attr_reader :before_save_called, :after_save_called
9
19
 
10
- before_save do |object|
20
+ before_save do |_object|
11
21
  @before_save_called = true
12
22
  end
13
23
 
14
- after_save do |object|
24
+ after_save do |_object|
15
25
  @after_save_called = true
16
26
  end
17
27
  end
18
28
 
19
- describe ".included" do
20
-
29
+ describe '.included' do
21
30
  let(:klass) do
22
31
  TestClass
23
32
  end
24
33
 
25
- it "includes the before_create callback" do
34
+ it 'includes the before_create callback' do
26
35
  expect(klass).to respond_to(:before_create)
27
36
  end
28
37
 
29
- it "includes the after_create callback" do
38
+ it 'includes the after_create callback' do
30
39
  expect(klass).to respond_to(:after_create)
31
40
  end
32
41
 
33
- it "includes the before_destroy callback" do
42
+ it 'includes the before_destroy callback' do
34
43
  expect(klass).to respond_to(:before_destroy)
35
44
  end
36
45
 
37
- it "includes the after_destroy callback" do
46
+ it 'includes the after_destroy callback' do
38
47
  expect(klass).to respond_to(:after_destroy)
39
48
  end
40
49
 
41
- it "includes the before_save callback" do
50
+ it 'includes the before_save callback' do
42
51
  expect(klass).to respond_to(:before_save)
43
52
  end
44
53
 
45
- it "includes the after_save callback" do
54
+ it 'includes the after_save callback' do
46
55
  expect(klass).to respond_to(:after_save)
47
56
  end
48
57
 
49
- it "includes the before_update callback" do
58
+ it 'includes the before_update callback' do
50
59
  expect(klass).to respond_to(:before_update)
51
60
  end
52
61
 
53
- it "includes the after_update callback" do
62
+ it 'includes the after_update callback' do
54
63
  expect(klass).to respond_to(:after_update)
55
64
  end
56
65
 
57
- it "includes the before_validation callback" do
66
+ it 'includes the before_validation callback' do
58
67
  expect(klass).to respond_to(:before_validation)
59
68
  end
60
69
 
61
- it "includes the after_validation callback" do
70
+ it 'includes the after_validation callback' do
62
71
  expect(klass).to respond_to(:after_validation)
63
72
  end
64
73
 
65
- it "includes the after_initialize callback" do
74
+ it 'includes the after_initialize callback' do
66
75
  expect(klass).to respond_to(:after_initialize)
67
76
  end
68
77
 
69
- it "includes the after_build callback" do
78
+ it 'includes the after_build callback' do
70
79
  expect(klass).to respond_to(:after_build)
71
80
  end
72
81
  end
73
82
 
74
- describe ".after_find" do
75
-
83
+ describe '.after_find' do
76
84
  let!(:player) do
77
- Player.create
85
+ Player.create!
78
86
  end
79
87
 
80
- context "when the callback is on a root document" do
81
-
82
- context "when when the document is instantiated" do
83
-
84
- it "does not execute the callback" do
88
+ context 'when the callback is on a root document' do
89
+ context 'when when the document is instantiated' do
90
+ it 'does not execute the callback' do
85
91
  expect(player.impressions).to eq(0)
86
92
  end
87
93
  end
88
94
 
89
- context "when the document is found via #find" do
90
-
95
+ context 'when the document is found via #find' do
91
96
  let(:from_db) do
92
97
  Player.find(player.id)
93
98
  end
94
99
 
95
- it "executes the callback" do
100
+ it 'executes the callback' do
96
101
  expect(from_db.impressions).to eq(1)
97
102
  end
98
103
  end
99
104
 
100
- context "when the document is found in a criteria" do
101
-
105
+ context 'when the document is found in a criteria' do
102
106
  let(:from_db) do
103
107
  Player.where(id: player.id).first
104
108
  end
105
109
 
106
- it "executes the callback" do
110
+ it 'executes the callback' do
107
111
  expect(from_db.impressions).to eq(1)
108
112
  end
109
113
  end
110
114
 
111
- context "when the document is reloaded" do
112
-
115
+ context 'when the document is reloaded' do
113
116
  let(:from_db) do
114
117
  Player.find(player.id)
115
118
  end
@@ -118,106 +121,99 @@ describe Mongoid::Interceptable do
118
121
  from_db.reload
119
122
  end
120
123
 
121
- it "executes the callback" do
124
+ it 'executes the callback' do
122
125
  expect(from_db.impressions).to eq(1)
123
126
  end
124
127
  end
125
128
  end
126
129
 
127
- context "when the callback is on an embedded document" do
128
-
130
+ context 'when the callback is on an embedded document' do
129
131
  let!(:implant) do
130
- player.implants.create
132
+ player.implants.create!
131
133
  end
132
134
 
133
- context "when when the document is instantiated" do
134
-
135
- it "does not execute the callback" do
135
+ context 'when when the document is instantiated' do
136
+ it 'does not execute the callback' do
136
137
  expect(implant.impressions).to eq(0)
137
138
  end
138
139
  end
139
140
 
140
- context "when the document is found via #find" do
141
-
141
+ context 'when the document is found via #find' do
142
142
  let(:from_db) do
143
143
  Player.find(player.id).implants.first
144
144
  end
145
145
 
146
- it "executes the callback" do
146
+ it 'executes the callback' do
147
147
  expect(from_db.impressions).to eq(1)
148
148
  end
149
149
  end
150
150
 
151
- context "when the document is found in a criteria" do
152
-
151
+ context 'when the document is found in a criteria' do
153
152
  let(:from_db) do
154
153
  Player.find(player.id).implants.find(implant.id)
155
154
  end
156
155
 
157
- it "executes the callback" do
156
+ it 'executes the callback' do
158
157
  expect(from_db.impressions).to eq(1)
159
158
  end
160
159
  end
161
160
  end
162
161
  end
163
162
 
164
- describe ".after_initialize" do
165
-
163
+ describe '.after_initialize' do
166
164
  let(:game) do
167
165
  Game.new
168
166
  end
169
167
 
170
- it "runs after document instantiation" do
171
- expect(game.name).to eq("Testing")
168
+ it 'runs after document instantiation' do
169
+ expect(game.name).to eq('Testing')
172
170
  end
173
171
 
174
172
  context 'when the document is embedded' do
175
-
176
173
  after do
177
174
  Book.destroy_all
178
175
  end
179
176
 
180
177
  let(:book) do
181
178
  book = Book.new({
182
- :pages => [
183
- {
184
- content: "Page 1",
185
- notes: [
186
- { message: "Page 1 / Note A" },
187
- { message: "Page 1 / Note B" }
188
- ]
189
- },
190
- {
191
- content: "Page 2",
192
- notes: [
193
- { message: "Page 2 / Note A" },
194
- { message: "Page 2 / Note B" }
195
- ]
196
- }
197
- ]
198
- })
179
+ pages: [
180
+ {
181
+ content: 'Page 1',
182
+ notes: [
183
+ { message: 'Page 1 / Note A' },
184
+ { message: 'Page 1 / Note B' }
185
+ ]
186
+ },
187
+ {
188
+ content: 'Page 2',
189
+ notes: [
190
+ { message: 'Page 2 / Note A' },
191
+ { message: 'Page 2 / Note B' }
192
+ ]
193
+ }
194
+ ]
195
+ })
199
196
  book.id = '123'
200
- book.save
197
+ book.save!
201
198
  book
202
199
  end
200
+ let(:expected_messages) do
201
+ book.reload.pages.reduce([]) do |messages, p|
202
+ messages + p.notes.reduce([]) do |msgs, n|
203
+ msgs << n.message
204
+ end
205
+ end
206
+ end
203
207
 
204
208
  let(:new_message) do
205
209
  'Note C'
206
210
  end
207
211
 
208
212
  before do
209
- book.pages.each do | page |
213
+ book.pages.each do |page|
210
214
  page.notes.destroy_all
211
215
  page.notes.new(message: new_message)
212
- page.save
213
- end
214
- end
215
-
216
- let(:expected_messages) do
217
- book.reload.pages.reduce([]) do |messages, p|
218
- messages += p.notes.reduce([]) do |msgs, n|
219
- msgs << n.message
220
- end
216
+ page.save!
221
217
  end
222
218
  end
223
219
 
@@ -227,61 +223,55 @@ describe Mongoid::Interceptable do
227
223
  end
228
224
  end
229
225
 
230
- describe ".after_build" do
231
-
226
+ describe '.after_build' do
232
227
  let(:weapon) do
233
228
  Player.new(frags: 5).weapons.build
234
229
  end
235
230
 
236
- it "runs after document build (references_many)" do
237
- expect(weapon.name).to eq("Holy Hand Grenade (5)")
231
+ let(:augmentation) do
232
+ Player.new(frags: 5).build_augmentation
233
+ end
234
+ let(:powerup) do
235
+ Player.new(frags: 5).build_powerup
238
236
  end
239
-
240
237
  let(:implant) do
241
238
  Player.new(frags: 5).implants.build
242
239
  end
243
240
 
244
- it "runs after document build (embeds_many)" do
245
- expect(implant.name).to eq('Cochlear Implant (5)')
246
- end
247
-
248
- let(:powerup) do
249
- Player.new(frags: 5).build_powerup
241
+ it 'runs after document build (references_many)' do
242
+ expect(weapon.name).to eq('Holy Hand Grenade (5)')
250
243
  end
251
244
 
252
- it "runs after document build (references_one)" do
253
- expect(powerup.name).to eq("Quad Damage (5)")
245
+ it 'runs after document build (embeds_many)' do
246
+ expect(implant.name).to eq('Cochlear Implant (5)')
254
247
  end
255
248
 
256
- let(:augmentation) do
257
- Player.new(frags: 5).build_augmentation
249
+ it 'runs after document build (references_one)' do
250
+ expect(powerup.name).to eq('Quad Damage (5)')
258
251
  end
259
252
 
260
- it "runs after document build (embeds_one)" do
261
- expect(augmentation.name).to eq("Infolink (5)")
253
+ it 'runs after document build (embeds_one)' do
254
+ expect(augmentation.name).to eq('Infolink (5)')
262
255
  end
263
256
  end
264
257
 
265
- describe ".before_create" do
266
-
258
+ describe '.before_create' do
267
259
  let(:artist) do
268
- Artist.new(name: "Depeche Mode")
260
+ Artist.new(name: 'Depeche Mode')
269
261
  end
270
262
 
271
- context "callback returns true" do
272
-
263
+ context 'callback returns true' do
273
264
  before do
274
265
  expect(artist).to receive(:before_create_stub).once.and_return(true)
275
- artist.save
266
+ artist.save!
276
267
  end
277
268
 
278
- it "gets saved" do
269
+ it 'gets saved' do
279
270
  expect(artist.persisted?).to be true
280
271
  end
281
272
  end
282
273
 
283
- context "callback aborts the callback chain" do
284
-
274
+ context 'callback aborts the callback chain' do
285
275
  before do
286
276
  Artist.before_create(:before_create_fail_stub)
287
277
  expect(artist).to receive(:before_create_fail_stub).once.and_call_original
@@ -292,37 +282,33 @@ describe Mongoid::Interceptable do
292
282
  Artist.reset_callbacks(:create)
293
283
  end
294
284
 
295
- it "does not get saved" do
285
+ it 'does not get saved' do
296
286
  expect(artist.persisted?).to be false
297
287
  end
298
288
  end
299
289
  end
300
290
 
301
- describe ".before_save" do
302
-
303
- context "when creating" do
304
-
291
+ describe '.before_save' do
292
+ context 'when creating' do
305
293
  let(:artist) do
306
- Artist.new(name: "Depeche Mode")
294
+ Artist.new(name: 'Depeche Mode')
307
295
  end
308
296
 
309
297
  after do
310
298
  artist.delete
311
299
  end
312
300
 
313
- context "when the callback returns true" do
314
-
301
+ context 'when the callback returns true' do
315
302
  before do
316
303
  expect(artist).to receive(:before_save_stub).once.and_return(true)
317
304
  end
318
305
 
319
- it "the save returns true" do
320
- expect(artist.save).to be true
306
+ it 'the save returns true' do
307
+ expect(artist.save!).to be true
321
308
  end
322
309
  end
323
310
 
324
- context "when callback halts the callback chain" do
325
-
311
+ context 'when callback halts the callback chain' do
326
312
  before do
327
313
  Artist.before_save(:before_save_fail_stub)
328
314
  end
@@ -331,18 +317,17 @@ describe Mongoid::Interceptable do
331
317
  Artist.reset_callbacks(:save)
332
318
  end
333
319
 
334
- it "the save returns false" do
320
+ it 'the save returns false' do
335
321
  expect(artist).to receive(:before_save_fail_stub).once.and_call_original
336
322
  expect(artist.save).to be false
337
323
  end
338
324
  end
339
325
  end
340
326
 
341
- context "when updating" do
342
-
327
+ context 'when updating' do
343
328
  let(:artist) do
344
- Artist.create(name: "Depeche Mode").tap do |artist|
345
- artist.name = "The Mountain Goats"
329
+ Artist.create!(name: 'Depeche Mode').tap do |artist|
330
+ artist.name = 'The Mountain Goats'
346
331
  end
347
332
  end
348
333
 
@@ -350,19 +335,17 @@ describe Mongoid::Interceptable do
350
335
  artist.delete
351
336
  end
352
337
 
353
- context "when the callback returns true" do
354
-
338
+ context 'when the callback returns true' do
355
339
  before do
356
340
  expect(artist).to receive(:before_update_stub).once.and_return(true)
357
341
  end
358
342
 
359
- it "the save returns true" do
360
- expect(artist.save).to be true
343
+ it 'the save returns true' do
344
+ expect(artist.save!).to be true
361
345
  end
362
346
  end
363
347
 
364
- context "when the callback halts the callback chain" do
365
-
348
+ context 'when the callback halts the callback chain' do
366
349
  before do
367
350
  Artist.before_update(:before_update_fail_stub)
368
351
  end
@@ -371,41 +354,116 @@ describe Mongoid::Interceptable do
371
354
  Artist.reset_callbacks(:update)
372
355
  end
373
356
 
374
- it "the save returns false" do
357
+ it 'the save returns false' do
375
358
  expect(artist).to receive(:before_update_fail_stub).once.and_call_original
376
359
  expect(artist.save).to be false
377
360
  end
378
361
  end
379
362
  end
380
- end
381
363
 
382
- describe ".before_destroy" do
364
+ context 'with embedded grandchildren' do
365
+ IS = InterceptableSpec
366
+
367
+ context 'when creating' do
368
+ let(:registry) { IS::CallbackRegistry.new(only: %i[ before_save ]) }
369
+
370
+ let(:expected_calls) do
371
+ [
372
+ # the parent
373
+ [ IS::CbParent, :before_save ],
374
+
375
+ # the immediate child of the parent
376
+ [ IS::CbCascadedNode, :before_save ],
377
+
378
+ # the grandchild of the parent
379
+ [ IS::CbCascadedNode, :before_save ]
380
+ ]
381
+ end
382
+
383
+ let!(:parent) do
384
+ parent = IS::CbParent.new(registry)
385
+ child = IS::CbCascadedNode.new(registry)
386
+ grandchild = IS::CbCascadedNode.new(registry)
387
+
388
+ child.cb_cascaded_nodes = [ grandchild ]
389
+ parent.cb_cascaded_nodes = [ child ]
390
+
391
+ parent.tap(&:save)
392
+ end
393
+
394
+ it 'cascades callbacks to grandchildren' do
395
+ expect(registry.calls).to eq expected_calls
396
+ end
397
+ end
398
+
399
+ context 'when updating' do
400
+ let(:registry) { IS::CallbackRegistry.new(only: %i[ before_update ]) }
401
+
402
+ let(:expected_calls) do
403
+ [
404
+ # the parent
405
+ [ IS::CbParent, :before_update ],
406
+
407
+ # the immediate child of the parent
408
+ [ IS::CbCascadedNode, :before_update ],
409
+
410
+ # the grandchild of the parent
411
+ [ IS::CbCascadedNode, :before_update ]
412
+ ]
413
+ end
414
+
415
+ let!(:parent) do
416
+ parent = IS::CbParent.new(nil)
417
+ child = IS::CbCascadedNode.new(nil)
418
+ grandchild = IS::CbCascadedNode.new(nil)
419
+
420
+ child.cb_cascaded_nodes = [ grandchild ]
421
+ parent.cb_cascaded_nodes = [ child ]
383
422
 
423
+ parent.save
424
+
425
+ parent.callback_registry = registry
426
+ child.callback_registry = registry
427
+ grandchild.callback_registry = registry
428
+
429
+ parent.name = 'updated'
430
+ child.name = 'updated'
431
+ grandchild.name = 'updated'
432
+
433
+ parent.tap(&:save)
434
+ end
435
+
436
+ it 'cascades callbacks to grandchildren' do
437
+ expect(registry.calls).to eq expected_calls
438
+ end
439
+ end
440
+ end
441
+ end
442
+
443
+ describe '.before_destroy' do
384
444
  let(:artist) do
385
- Artist.create(name: "Depeche Mode")
445
+ Artist.create!(name: 'Depeche Mode')
386
446
  end
387
447
 
388
448
  before do
389
- artist.name = "The Mountain Goats"
449
+ artist.name = 'The Mountain Goats'
390
450
  end
391
451
 
392
452
  after do
393
453
  artist.delete
394
454
  end
395
455
 
396
- context "when the callback returns true" do
397
-
456
+ context 'when the callback returns true' do
398
457
  before do
399
458
  expect(artist).to receive(:before_destroy_stub).once.and_return(true)
400
459
  end
401
460
 
402
- it "the destroy returns true" do
461
+ it 'the destroy returns true' do
403
462
  expect(artist.destroy).to be true
404
463
  end
405
464
  end
406
465
 
407
- context "when the callback halts the callback chain" do
408
-
466
+ context 'when the callback halts the callback chain' do
409
467
  before do
410
468
  Artist.before_destroy(:before_destroy_fail_stub)
411
469
  end
@@ -414,34 +472,32 @@ describe Mongoid::Interceptable do
414
472
  Artist.reset_callbacks(:destroy)
415
473
  end
416
474
 
417
- it "the destroy returns false" do
475
+ it 'the destroy returns false' do
418
476
  expect(artist).to receive(:before_destroy_fail_stub).once.and_call_original
419
477
  expect(artist.destroy).to be false
420
478
  end
421
479
  end
422
480
 
423
- context "when cascading callbacks" do
424
-
481
+ context 'when cascading callbacks' do
425
482
  let!(:moderat) do
426
- Band.create!(name: "Moderat")
483
+ Band.create!(name: 'Moderat')
427
484
  end
428
485
 
429
486
  let!(:record) do
430
- moderat.records.create(name: "Moderat")
487
+ moderat.records.create!(name: 'Moderat')
431
488
  end
432
489
 
433
490
  before do
434
491
  moderat.destroy
435
492
  end
436
493
 
437
- it "executes the child destroy callbacks" do
494
+ it 'executes the child destroy callbacks' do
438
495
  expect(record.before_destroy_called).to be true
439
496
  end
440
497
  end
441
498
  end
442
499
 
443
- describe "#run_after_callbacks" do
444
-
500
+ describe '#run_after_callbacks' do
445
501
  let(:object) do
446
502
  TestClass.new
447
503
  end
@@ -450,17 +506,16 @@ describe Mongoid::Interceptable do
450
506
  object.run_after_callbacks(:save)
451
507
  end
452
508
 
453
- it "runs the after callbacks" do
509
+ it 'runs the after callbacks' do
454
510
  expect(object.after_save_called).to be true
455
511
  end
456
512
 
457
- it "does not run the before callbacks" do
458
- expect(object.before_save_called).to be nil
513
+ it 'does not run the before callbacks' do
514
+ expect(object.before_save_called).to be_nil
459
515
  end
460
516
  end
461
517
 
462
- describe "#run_before_callbacks" do
463
-
518
+ describe '#run_before_callbacks' do
464
519
  let(:object) do
465
520
  TestClass.new
466
521
  end
@@ -469,21 +524,18 @@ describe Mongoid::Interceptable do
469
524
  object.run_before_callbacks(:save)
470
525
  end
471
526
 
472
- it "runs the before callbacks" do
527
+ it 'runs the before callbacks' do
473
528
  expect(object.before_save_called).to be true
474
529
  end
475
530
 
476
- it "does not run the after callbacks" do
477
- expect(object.after_save_called).to be nil
531
+ it 'does not run the after callbacks' do
532
+ expect(object.after_save_called).to be_nil
478
533
  end
479
534
  end
480
535
 
481
- context "when cascading callbacks" do
482
-
483
- context "when the parent has a custom callback" do
484
-
485
- context "when the child does not have the same callback defined" do
486
-
536
+ context 'when cascading callbacks' do
537
+ context 'when the parent has a custom callback' do
538
+ context 'when the child does not have the same callback defined' do
487
539
  let(:exhibition) do
488
540
  Exhibition.new
489
541
  end
@@ -492,27 +544,32 @@ describe Mongoid::Interceptable do
492
544
  exhibition.exhibitors.build
493
545
  end
494
546
 
495
- context "when running the callbacks directly" do
496
-
547
+ context 'when running the callbacks directly' do
497
548
  before(:all) do
498
549
  Exhibition.define_model_callbacks(:rearrange)
499
- Exhibition.after_rearrange { }
550
+ Exhibition.after_rearrange {}
500
551
  end
501
552
 
502
553
  after(:all) do
503
554
  Exhibition.reset_callbacks(:rearrange)
504
555
  end
505
556
 
506
- it "does not cascade to the child" do
557
+ it 'does not cascade to the child' do
507
558
  expect(exhibition.run_callbacks(:rearrange)).to be true
508
559
  end
509
560
  end
510
561
 
511
- context "when the callbacks get triggered by a destroy" do
512
-
562
+ context 'when the callbacks get triggered by a destroy' do
513
563
  let(:band) do
514
564
  Band.new
515
565
  end
566
+ let(:attributes) do
567
+ {
568
+ records_attributes: {
569
+ '0' => { '_id' => record.id, '_destroy' => true }
570
+ }
571
+ }
572
+ end
516
573
 
517
574
  let!(:record) do
518
575
  band.records.build
@@ -528,27 +585,18 @@ describe Mongoid::Interceptable do
528
585
  after(:all) do
529
586
  # ActiveSupport may raise an error when trying to reset callbacks on all of Band's
530
587
  # descendants, regardless of whether they have a particular callback defined.
531
- begin; Band.reset_callbacks(:rearrange); rescue; end
532
- end
533
-
534
- let(:attributes) do
535
- {
536
- records_attributes: {
537
- "0" => { "_id" => record.id, "_destroy" => true }
538
- }
539
- }
588
+ Band.reset_callbacks(:rearrange); rescue StandardError
540
589
  end
541
590
 
542
- it "does not cascade to the child" do
591
+ it 'does not cascade to the child' do
543
592
  Band.accepts_nested_attributes_for :records, allow_destroy: true
544
- expect(band.update_attributes(attributes)).to be true
593
+ expect(band.update_attributes!(attributes)).to be true
545
594
  end
546
595
  end
547
596
  end
548
597
  end
549
598
 
550
- context "when a document can exist in more than 1 level" do
551
-
599
+ context 'when a document can exist in more than 1 level' do
552
600
  let(:band) do
553
601
  Band.new
554
602
  end
@@ -561,27 +609,39 @@ describe Mongoid::Interceptable do
561
609
  Note.new
562
610
  end
563
611
 
564
- context "when adding the document at multiple levels" do
565
-
612
+ context 'when adding the document at multiple levels' do
566
613
  before do
567
614
  band.notes.push(note)
568
615
  record.notes.push(note)
569
616
  end
570
617
 
571
- context "when saving the root" do
618
+ context 'when saving the root' do
619
+ context 'with prevent_multiple_calls_of_embedded_callbacks enabled' do
620
+ config_override :prevent_multiple_calls_of_embedded_callbacks, true
621
+ config_override :around_callbacks_for_embeds, true
622
+
623
+ it 'executes the callbacks only once for each document' do
624
+ expect(note).to receive(:update_saved).once
625
+ band.save!
626
+ end
627
+ end
572
628
 
573
- it "only executes the callbacks once for each embed" do
574
- expect(note).to receive(:update_saved).twice
575
- band.save
629
+ context 'with prevent_multiple_calls_of_embedded_callbacks disabled' do
630
+ config_override :prevent_multiple_calls_of_embedded_callbacks, false
631
+ config_override :around_callbacks_for_embeds, true
632
+
633
+ it 'executes the callbacks once for each ember' do
634
+ expect(note).to receive(:update_saved).twice
635
+ band.save!
636
+ end
576
637
  end
577
638
  end
578
639
  end
579
640
  end
580
641
 
581
- context "when cascading after initialize" do
582
-
642
+ context 'when cascading after initialize' do
583
643
  let!(:person) do
584
- Person.create
644
+ Person.create!
585
645
  end
586
646
 
587
647
  before do
@@ -589,588 +649,541 @@ describe Mongoid::Interceptable do
589
649
  end
590
650
 
591
651
  it "doesn't cascade the initialize" do
592
- expect_any_instance_of(Service).to receive(:after_initialize_called=).never
652
+ expect_any_instance_of(Service).not_to receive(:after_initialize_called=)
593
653
  expect(Person.find(person.id)).to eq(person)
594
654
  end
595
655
  end
596
656
 
597
- context "when attempting to cascade on a referenced relation" do
598
-
599
- it "raises an error" do
600
- expect {
657
+ context 'when attempting to cascade on a referenced relation' do
658
+ it 'raises an error' do
659
+ expect do
601
660
  Band.has_and_belongs_to_many :tags, cascade_callbacks: true
602
- }.to raise_error(Mongoid::Errors::InvalidRelationOption)
661
+ end.to raise_error(Mongoid::Errors::InvalidRelationOption)
603
662
  end
604
663
  end
605
664
 
606
- context "when the documents are embedded one level" do
607
-
608
- describe "#after_create" do
609
-
610
- context "when the child is new" do
611
-
612
- context "when the parent is new" do
613
-
665
+ context 'when the documents are embedded one level' do
666
+ describe '#after_create' do
667
+ context 'when the child is new' do
668
+ context 'when the parent is new' do
614
669
  let(:band) do
615
- Band.new(name: "Moderat")
670
+ Band.new(name: 'Moderat')
616
671
  end
617
672
 
618
673
  let!(:label) do
619
- band.build_label(name: "Mute")
674
+ band.build_label(name: 'Mute')
620
675
  end
621
676
 
622
677
  before do
623
- band.save
678
+ band.save!
624
679
  end
625
680
 
626
- it "executes the callback" do
681
+ it 'executes the callback' do
627
682
  expect(label.after_create_called).to be true
628
683
  end
629
684
  end
630
685
 
631
- context "when the parent is persisted" do
632
-
686
+ context 'when the parent is persisted' do
633
687
  let(:band) do
634
- Band.create(name: "Moderat")
688
+ Band.create!(name: 'Moderat')
635
689
  end
636
690
 
637
691
  let!(:label) do
638
- band.build_label(name: "Mute")
692
+ band.build_label(name: 'Mute')
639
693
  end
640
694
 
641
695
  before do
642
- band.save
696
+ band.save!
643
697
  end
644
698
 
645
- it "executes the callback" do
699
+ it 'executes the callback' do
646
700
  expect(label.after_create_called).to be true
647
701
  end
648
702
  end
649
703
  end
650
704
 
651
- context "when the child is persisted" do
652
-
705
+ context 'when the child is persisted' do
653
706
  let(:band) do
654
- Band.create(name: "Moderat")
707
+ Band.create!(name: 'Moderat')
655
708
  end
656
709
 
657
710
  let!(:label) do
658
- band.create_label(name: "Mute")
711
+ band.create_label(name: 'Mute')
659
712
  end
660
713
 
661
714
  before do
662
715
  label.after_create_called = false
663
- band.save
716
+ band.save!
664
717
  end
665
718
 
666
- it "does not execute the callback" do
719
+ it 'does not execute the callback' do
667
720
  expect(label.after_create_called).to be false
668
721
  end
669
722
  end
670
723
  end
671
724
 
672
- describe "#after_save" do
673
-
674
- context "when the child is new" do
675
-
676
- context "when the parent is new" do
677
-
725
+ describe '#after_save' do
726
+ context 'when the child is new' do
727
+ context 'when the parent is new' do
678
728
  let(:band) do
679
- Band.new(name: "Moderat")
729
+ Band.new(name: 'Moderat')
680
730
  end
681
731
 
682
732
  let!(:label) do
683
- band.build_label(name: "Mute")
733
+ band.build_label(name: 'Mute')
684
734
  end
685
735
 
686
736
  before do
687
- band.save
737
+ band.save!
688
738
  end
689
739
 
690
- it "executes the callback" do
740
+ it 'executes the callback' do
691
741
  expect(label.after_save_called).to be true
692
742
  end
693
743
  end
694
744
 
695
- context "when the parent is persisted" do
696
-
745
+ context 'when the parent is persisted' do
697
746
  let(:band) do
698
- Band.create(name: "Moderat")
747
+ Band.create!(name: 'Moderat')
699
748
  end
700
749
 
701
750
  let!(:label) do
702
- band.build_label(name: "Mute")
751
+ band.build_label(name: 'Mute')
703
752
  end
704
753
 
705
754
  before do
706
- band.save
755
+ band.save!
707
756
  end
708
757
 
709
- it "executes the callback" do
758
+ it 'executes the callback' do
710
759
  expect(label.after_save_called).to be true
711
760
  end
712
761
  end
713
762
  end
714
763
 
715
- context "when the child is persisted" do
716
-
764
+ context 'when the child is persisted' do
717
765
  let(:band) do
718
- Band.create(name: "Moderat")
766
+ Band.create!(name: 'Moderat')
719
767
  end
720
768
 
721
769
  let!(:label) do
722
- band.create_label(name: "Mute")
770
+ band.create_label(name: 'Mute')
723
771
  end
724
772
 
725
773
  before do
726
- band.save
774
+ band.save!
727
775
  end
728
776
 
729
- it "executes the callback" do
777
+ it 'executes the callback' do
730
778
  expect(label.after_save_called).to be true
731
779
  end
732
780
  end
733
781
  end
734
782
 
735
- describe "#after_update" do
736
-
737
- context "when the child is new" do
738
-
739
- context "when the parent is new" do
740
-
783
+ describe '#after_update' do
784
+ context 'when the child is new' do
785
+ context 'when the parent is new' do
741
786
  let(:band) do
742
- Band.new(name: "Moderat")
787
+ Band.new(name: 'Moderat')
743
788
  end
744
789
 
745
790
  let!(:label) do
746
- band.build_label(name: "Mute")
791
+ band.build_label(name: 'Mute')
747
792
  end
748
793
 
749
794
  before do
750
- band.save
795
+ band.save!
751
796
  end
752
797
 
753
- it "does not execute the callback" do
798
+ it 'does not execute the callback' do
754
799
  expect(label.after_update_called).to be false
755
800
  end
756
801
  end
757
802
 
758
- context "when the parent is persisted" do
759
-
803
+ context 'when the parent is persisted' do
760
804
  let(:band) do
761
- Band.create(name: "Moderat")
805
+ Band.create!(name: 'Moderat')
762
806
  end
763
807
 
764
808
  let!(:label) do
765
- band.build_label(name: "Mute")
809
+ band.build_label(name: 'Mute')
766
810
  end
767
811
 
768
812
  before do
769
- band.save
813
+ band.save!
770
814
  end
771
815
 
772
- it "does not execute the callback" do
816
+ it 'does not execute the callback' do
773
817
  expect(label.after_update_called).to be false
774
818
  end
775
819
  end
776
820
  end
777
821
 
778
- context "when the child is persisted" do
779
-
822
+ context 'when the child is persisted' do
780
823
  let(:band) do
781
- Band.create(name: "Moderat")
824
+ Band.create!(name: 'Moderat')
782
825
  end
783
826
 
784
- context "when the child is dirty" do
785
-
827
+ context 'when the child is dirty' do
786
828
  let!(:label) do
787
- band.create_label(name: "Mute")
829
+ band.create_label(name: 'Mute')
788
830
  end
789
831
 
790
832
  before do
791
- label.name = "Nothing"
792
- band.save
833
+ label.name = 'Nothing'
834
+ band.save!
793
835
  end
794
836
 
795
- it "executes the callback" do
837
+ it 'executes the callback' do
796
838
  expect(label.after_update_called).to be true
797
839
  end
798
840
  end
799
841
 
800
- context "when the child is not dirty" do
801
-
842
+ context 'when the child is not dirty' do
802
843
  let!(:label) do
803
- band.build_label(name: "Mute")
844
+ band.build_label(name: 'Mute')
804
845
  end
805
846
 
806
847
  before do
807
- band.save
848
+ band.save!
808
849
  end
809
850
 
810
- it "does not execute the callback" do
851
+ it 'does not execute the callback' do
811
852
  expect(label.after_update_called).to be false
812
853
  end
813
854
  end
814
855
  end
815
856
  end
816
857
 
817
- describe "#after_validation" do
818
-
819
- context "when the child is new" do
820
-
821
- context "when the parent is new" do
822
-
858
+ describe '#after_validation' do
859
+ context 'when the child is new' do
860
+ context 'when the parent is new' do
823
861
  let(:band) do
824
- Band.new(name: "Moderat")
862
+ Band.new(name: 'Moderat')
825
863
  end
826
864
 
827
865
  let!(:label) do
828
- band.build_label(name: "Mute")
866
+ band.build_label(name: 'Mute')
829
867
  end
830
868
 
831
869
  before do
832
- band.save
870
+ band.save!
833
871
  end
834
872
 
835
- it "executes the callback" do
873
+ it 'executes the callback' do
836
874
  expect(label.after_validation_called).to be true
837
875
  end
838
876
  end
839
877
 
840
- context "when the parent is persisted" do
841
-
878
+ context 'when the parent is persisted' do
842
879
  let(:band) do
843
- Band.create(name: "Moderat")
880
+ Band.create!(name: 'Moderat')
844
881
  end
845
882
 
846
883
  let!(:label) do
847
- band.build_label(name: "Mute")
884
+ band.build_label(name: 'Mute')
848
885
  end
849
886
 
850
887
  before do
851
- band.save
888
+ band.save!
852
889
  end
853
890
 
854
- it "executes the callback" do
891
+ it 'executes the callback' do
855
892
  expect(label.after_validation_called).to be true
856
893
  end
857
894
  end
858
895
  end
859
896
 
860
- context "when the child is persisted" do
861
-
897
+ context 'when the child is persisted' do
862
898
  let(:band) do
863
- Band.create(name: "Moderat")
899
+ Band.create!(name: 'Moderat')
864
900
  end
865
901
 
866
902
  let!(:label) do
867
- band.create_label(name: "Mute")
903
+ band.create_label(name: 'Mute')
868
904
  end
869
905
 
870
906
  before do
871
- band.save
907
+ band.save!
872
908
  end
873
909
 
874
- it "executes the callback" do
910
+ it 'executes the callback' do
875
911
  expect(label.after_validation_called).to be true
876
912
  end
877
913
  end
878
914
  end
879
915
 
880
- describe "#before_create" do
881
-
882
- context "when the child is new" do
883
-
884
- context "when the parent is new" do
885
-
916
+ describe '#before_create' do
917
+ context 'when the child is new' do
918
+ context 'when the parent is new' do
886
919
  let(:band) do
887
- Band.new(name: "Moderat")
920
+ Band.new(name: 'Moderat')
888
921
  end
889
922
 
890
923
  let!(:record) do
891
- band.records.build(name: "Moderat")
924
+ band.records.build(name: 'Moderat')
892
925
  end
893
926
 
894
927
  before do
895
- band.save
928
+ band.save!
896
929
  end
897
930
 
898
- it "executes the callback" do
931
+ it 'executes the callback' do
899
932
  expect(record.before_create_called).to be true
900
933
  end
901
934
  end
902
935
 
903
- context "when the parent is persisted" do
904
-
936
+ context 'when the parent is persisted' do
905
937
  let(:band) do
906
- Band.create(name: "Moderat")
938
+ Band.create!(name: 'Moderat')
907
939
  end
908
940
 
909
941
  let!(:record) do
910
- band.records.build(name: "Moderat")
942
+ band.records.build(name: 'Moderat')
911
943
  end
912
944
 
913
945
  before do
914
- band.save
946
+ band.save!
915
947
  end
916
948
 
917
- it "executes the callback" do
949
+ it 'executes the callback' do
918
950
  expect(record.before_create_called).to be true
919
951
  end
920
952
  end
921
953
  end
922
954
 
923
- context "when the child is persisted" do
924
-
955
+ context 'when the child is persisted' do
925
956
  let(:band) do
926
- Band.create(name: "Moderat")
957
+ Band.create!(name: 'Moderat')
927
958
  end
928
959
 
929
960
  let!(:record) do
930
- band.records.create(name: "Moderat")
961
+ band.records.create!(name: 'Moderat')
931
962
  end
932
963
 
933
964
  before do
934
965
  record.before_create_called = false
935
- band.save
966
+ band.save!
936
967
  end
937
968
 
938
- it "does not execute the callback" do
969
+ it 'does not execute the callback' do
939
970
  expect(record.before_create_called).to be false
940
971
  end
941
972
  end
942
973
  end
943
974
 
944
- describe "#before_save" do
945
-
946
- context "when the child is new" do
947
-
948
- context "when the parent is new" do
949
-
975
+ describe '#before_save' do
976
+ context 'when the child is new' do
977
+ context 'when the parent is new' do
950
978
  let(:band) do
951
- Band.new(name: "Moderat")
979
+ Band.new(name: 'Moderat')
952
980
  end
953
981
 
954
982
  let!(:record) do
955
- band.records.build(name: "Moderat")
983
+ band.records.build(name: 'Moderat')
956
984
  end
957
985
 
958
986
  before do
959
- band.save
987
+ band.save!
960
988
  end
961
989
 
962
- it "executes the callback" do
990
+ it 'executes the callback' do
963
991
  expect(record.before_save_called).to be true
964
992
  end
965
993
 
966
- it "persists the change" do
994
+ it 'persists the change' do
967
995
  expect(band.reload.records.first.before_save_called).to be true
968
996
  end
969
997
  end
970
998
 
971
- context "when the parent is persisted" do
972
-
999
+ context 'when the parent is persisted' do
973
1000
  let(:band) do
974
- Band.create(name: "Moderat")
1001
+ Band.create!(name: 'Moderat')
975
1002
  end
976
1003
 
977
1004
  let!(:record) do
978
- band.records.build(name: "Moderat")
1005
+ band.records.build(name: 'Moderat')
979
1006
  end
980
1007
 
981
1008
  before do
982
- band.save
1009
+ band.save!
983
1010
  end
984
1011
 
985
- it "executes the callback" do
1012
+ it 'executes the callback' do
986
1013
  expect(record.before_save_called).to be true
987
1014
  end
988
1015
 
989
- it "persists the change" do
1016
+ it 'persists the change' do
990
1017
  expect(band.reload.records.first.before_save_called).to be true
991
1018
  end
992
1019
  end
993
1020
  end
994
1021
 
995
- context "when the child is persisted" do
996
-
1022
+ context 'when the child is persisted' do
997
1023
  let(:band) do
998
- Band.create(name: "Moderat")
1024
+ Band.create!(name: 'Moderat')
999
1025
  end
1000
1026
 
1001
1027
  let!(:record) do
1002
- band.records.create(name: "Moderat")
1028
+ band.records.create!(name: 'Moderat')
1003
1029
  end
1004
1030
 
1005
1031
  before do
1006
- band.save
1032
+ band.save!
1007
1033
  end
1008
1034
 
1009
- it "executes the callback" do
1035
+ it 'executes the callback' do
1010
1036
  expect(record.before_save_called).to be true
1011
1037
  end
1012
1038
 
1013
- it "persists the change" do
1039
+ it 'persists the change' do
1014
1040
  expect(band.reload.records.first.before_save_called).to be true
1015
1041
  end
1016
1042
  end
1017
1043
 
1018
- context "when the child is created" do
1019
-
1044
+ context 'when the child is created' do
1020
1045
  let!(:band) do
1021
- Band.create
1046
+ Band.create!
1022
1047
  end
1023
1048
 
1024
1049
  let!(:label) do
1025
1050
  band.create_label(name: 'Label')
1026
1051
  end
1027
1052
 
1028
- it "only executes callback once" do
1053
+ it 'only executes callback once' do
1029
1054
  expect(label.before_save_count).to be 1
1030
1055
  end
1031
1056
  end
1032
1057
  end
1033
1058
 
1034
- describe "#before_update" do
1035
-
1036
- context "when the child is new" do
1037
-
1038
- context "when the parent is new" do
1039
-
1059
+ describe '#before_update' do
1060
+ context 'when the child is new' do
1061
+ context 'when the parent is new' do
1040
1062
  let(:band) do
1041
- Band.new(name: "Moderat")
1063
+ Band.new(name: 'Moderat')
1042
1064
  end
1043
1065
 
1044
1066
  let!(:record) do
1045
- band.records.build(name: "Moderat")
1067
+ band.records.build(name: 'Moderat')
1046
1068
  end
1047
1069
 
1048
1070
  before do
1049
- band.save
1071
+ band.save!
1050
1072
  end
1051
1073
 
1052
- it "does not execute the callback" do
1074
+ it 'does not execute the callback' do
1053
1075
  expect(record.before_update_called).to be false
1054
1076
  end
1055
1077
  end
1056
1078
 
1057
- context "when the parent is persisted" do
1058
-
1079
+ context 'when the parent is persisted' do
1059
1080
  let(:band) do
1060
- Band.create(name: "Moderat")
1081
+ Band.create!(name: 'Moderat')
1061
1082
  end
1062
1083
 
1063
1084
  let!(:record) do
1064
- band.records.build(name: "Moderat")
1085
+ band.records.build(name: 'Moderat')
1065
1086
  end
1066
1087
 
1067
1088
  before do
1068
- band.save
1089
+ band.save!
1069
1090
  end
1070
1091
 
1071
- it "does not execute the callback" do
1092
+ it 'does not execute the callback' do
1072
1093
  expect(record.before_update_called).to be false
1073
1094
  end
1074
1095
  end
1075
1096
  end
1076
1097
 
1077
- context "when the child is persisted" do
1078
-
1098
+ context 'when the child is persisted' do
1079
1099
  let(:band) do
1080
- Band.create(name: "Moderat")
1100
+ Band.create!(name: 'Moderat')
1081
1101
  end
1082
1102
 
1083
1103
  let!(:record) do
1084
- band.records.create(name: "Moderat")
1104
+ band.records.create!(name: 'Moderat')
1085
1105
  end
1086
1106
 
1087
- context "when the child is dirty" do
1088
-
1107
+ context 'when the child is dirty' do
1089
1108
  before do
1090
- record.name = "Nothing"
1091
- band.save
1109
+ record.name = 'Nothing'
1110
+ band.save!
1092
1111
  end
1093
1112
 
1094
- it "executes the callback" do
1113
+ it 'executes the callback' do
1095
1114
  expect(record.before_update_called).to be true
1096
1115
  end
1097
1116
 
1098
- it "persists the change" do
1117
+ it 'persists the change' do
1099
1118
  expect(band.reload.records.first.before_update_called).to be true
1100
1119
  end
1101
1120
  end
1102
1121
 
1103
- context "when the child is not dirty" do
1104
-
1122
+ context 'when the child is not dirty' do
1105
1123
  before do
1106
- band.save
1124
+ band.save!
1107
1125
  end
1108
1126
 
1109
- it "does not execute the callback" do
1127
+ it 'does not execute the callback' do
1110
1128
  expect(record.before_update_called).to be false
1111
1129
  end
1112
1130
  end
1113
1131
  end
1114
1132
  end
1115
1133
 
1116
- describe "#before_validation" do
1117
-
1118
- context "when the child is new" do
1119
-
1120
- context "when the parent is new" do
1121
-
1134
+ describe '#before_validation' do
1135
+ context 'when the child is new' do
1136
+ context 'when the parent is new' do
1122
1137
  let(:band) do
1123
- Band.new(name: "Moderat")
1138
+ Band.new(name: 'Moderat')
1124
1139
  end
1125
1140
 
1126
1141
  let!(:record) do
1127
- band.records.build(name: "Moderat")
1142
+ band.records.build(name: 'Moderat')
1128
1143
  end
1129
1144
 
1130
1145
  before do
1131
- band.save
1146
+ band.save!
1132
1147
  end
1133
1148
 
1134
- it "executes the callback" do
1149
+ it 'executes the callback' do
1135
1150
  expect(record.before_validation_called).to be true
1136
1151
  end
1137
1152
 
1138
- it "persists the change" do
1153
+ it 'persists the change' do
1139
1154
  expect(band.reload.records.first.before_validation_called).to be true
1140
1155
  end
1141
1156
  end
1142
1157
 
1143
- context "when the parent is persisted" do
1144
-
1158
+ context 'when the parent is persisted' do
1145
1159
  let(:band) do
1146
- Band.create(name: "Moderat")
1160
+ Band.create!(name: 'Moderat')
1147
1161
  end
1148
1162
 
1149
1163
  let!(:record) do
1150
- band.records.build(name: "Moderat")
1164
+ band.records.build(name: 'Moderat')
1151
1165
  end
1152
1166
 
1153
1167
  before do
1154
- band.save
1168
+ band.save!
1155
1169
  end
1156
1170
 
1157
- it "executes the callback" do
1171
+ it 'executes the callback' do
1158
1172
  expect(record.before_validation_called).to be true
1159
1173
  end
1160
1174
 
1161
- it "persists the change" do
1175
+ it 'persists the change' do
1162
1176
  expect(band.reload.records.first.before_validation_called).to be true
1163
1177
  end
1164
1178
  end
1165
1179
 
1166
1180
  context 'when the parent is updated' do
1167
-
1168
1181
  let(:band) do
1169
- Band.create(name: "Moderat")
1182
+ Band.create!(name: 'Moderat')
1170
1183
  end
1171
1184
 
1172
1185
  before do
1173
- band.update(records: [ { name: 'Black on Both Sides' }])
1186
+ band.update(records: [ { name: 'Black on Both Sides' } ])
1174
1187
  end
1175
1188
 
1176
1189
  it 'executes the callback' do
@@ -1184,13 +1197,12 @@ describe Mongoid::Interceptable do
1184
1197
  end
1185
1198
 
1186
1199
  context 'when the parent is updated' do
1187
-
1188
1200
  let(:band) do
1189
- Band.create(name: "Moderat")
1201
+ Band.create!(name: 'Moderat')
1190
1202
  end
1191
1203
 
1192
1204
  before do
1193
- band.update(records: [ { name: 'Black on Both Sides' }])
1205
+ band.update(records: [ { name: 'Black on Both Sides' } ])
1194
1206
  end
1195
1207
 
1196
1208
  it 'executes the callback' do
@@ -1202,300 +1214,278 @@ describe Mongoid::Interceptable do
1202
1214
  end
1203
1215
  end
1204
1216
 
1205
- context "when the child is persisted" do
1206
-
1217
+ context 'when the child is persisted' do
1207
1218
  let(:band) do
1208
- Band.create(name: "Moderat")
1219
+ Band.create!(name: 'Moderat')
1209
1220
  end
1210
1221
 
1211
1222
  let!(:record) do
1212
- band.records.create(name: "Moderat")
1223
+ band.records.create!(name: 'Moderat')
1213
1224
  end
1214
1225
 
1215
1226
  before do
1216
- band.save
1227
+ band.save!
1217
1228
  end
1218
1229
 
1219
- it "executes the callback" do
1230
+ it 'executes the callback' do
1220
1231
  expect(record.before_validation_called).to be true
1221
1232
  end
1222
1233
 
1223
- it "persists the change" do
1234
+ it 'persists the change' do
1224
1235
  expect(band.reload.records.first.before_validation_called).to be true
1225
1236
  end
1226
1237
  end
1227
1238
  end
1228
1239
  end
1229
1240
 
1230
- context "when the document is embedded multiple levels" do
1231
-
1232
- describe "#before_create" do
1233
-
1234
- context "when the child is new" do
1235
-
1236
- context "when the root is new" do
1237
-
1241
+ context 'when the document is embedded multiple levels' do
1242
+ describe '#before_create' do
1243
+ context 'when the child is new' do
1244
+ context 'when the root is new' do
1238
1245
  let(:band) do
1239
- Band.new(name: "Moderat")
1246
+ Band.new(name: 'Moderat')
1240
1247
  end
1241
1248
 
1242
1249
  let!(:record) do
1243
- band.records.build(name: "Moderat")
1250
+ band.records.build(name: 'Moderat')
1244
1251
  end
1245
1252
 
1246
1253
  let!(:track) do
1247
- record.tracks.build(name: "Berlin")
1254
+ record.tracks.build(name: 'Berlin')
1248
1255
  end
1249
1256
 
1250
1257
  before do
1251
- band.save
1258
+ band.save!
1252
1259
  end
1253
1260
 
1254
- it "executes the callback" do
1261
+ it 'executes the callback' do
1255
1262
  expect(track.before_create_called).to be true
1256
1263
  end
1257
1264
  end
1258
1265
 
1259
- context "when the root is persisted" do
1260
-
1266
+ context 'when the root is persisted' do
1261
1267
  let(:band) do
1262
- Band.create(name: "Moderat")
1268
+ Band.create!(name: 'Moderat')
1263
1269
  end
1264
1270
 
1265
1271
  let!(:record) do
1266
- band.records.build(name: "Moderat")
1272
+ band.records.build(name: 'Moderat')
1267
1273
  end
1268
1274
 
1269
1275
  let!(:track) do
1270
- record.tracks.build(name: "Berlin")
1276
+ record.tracks.build(name: 'Berlin')
1271
1277
  end
1272
1278
 
1273
1279
  before do
1274
- band.save
1280
+ band.save!
1275
1281
  end
1276
1282
 
1277
- it "executes the callback" do
1283
+ it 'executes the callback' do
1278
1284
  expect(track.before_create_called).to be true
1279
1285
  end
1280
1286
  end
1281
1287
  end
1282
1288
 
1283
- context "when the child is persisted" do
1284
-
1289
+ context 'when the child is persisted' do
1285
1290
  let(:band) do
1286
- Band.create(name: "Moderat")
1291
+ Band.create!(name: 'Moderat')
1287
1292
  end
1288
1293
 
1289
1294
  let!(:record) do
1290
- band.records.create(name: "Moderat")
1295
+ band.records.create!(name: 'Moderat')
1291
1296
  end
1292
1297
 
1293
1298
  let!(:track) do
1294
- record.tracks.create(name: "Berlin")
1299
+ record.tracks.create!(name: 'Berlin')
1295
1300
  end
1296
1301
 
1297
1302
  before do
1298
1303
  track.before_create_called = false
1299
- band.save
1304
+ band.save!
1300
1305
  end
1301
1306
 
1302
- it "does not execute the callback" do
1307
+ it 'does not execute the callback' do
1303
1308
  expect(track.before_create_called).to be false
1304
1309
  end
1305
1310
  end
1306
1311
  end
1307
1312
 
1308
- describe "#before_save" do
1309
-
1310
- context "when the child is new" do
1311
-
1312
- context "when the root is new" do
1313
-
1313
+ describe '#before_save' do
1314
+ context 'when the child is new' do
1315
+ context 'when the root is new' do
1314
1316
  let(:band) do
1315
- Band.new(name: "Moderat")
1317
+ Band.new(name: 'Moderat')
1318
+ end
1319
+ let(:reloaded) do
1320
+ band.reload.records.first
1316
1321
  end
1317
1322
 
1318
1323
  let!(:record) do
1319
- band.records.build(name: "Moderat")
1324
+ band.records.build(name: 'Moderat')
1320
1325
  end
1321
1326
 
1322
1327
  let!(:track) do
1323
- record.tracks.build(name: "Berlin")
1328
+ record.tracks.build(name: 'Berlin')
1324
1329
  end
1325
1330
 
1326
1331
  before do
1327
- band.save
1328
- end
1329
-
1330
- let(:reloaded) do
1331
- band.reload.records.first
1332
+ band.save!
1332
1333
  end
1333
1334
 
1334
- it "executes the callback" do
1335
+ it 'executes the callback' do
1335
1336
  expect(track.before_save_called).to be true
1336
1337
  end
1337
1338
 
1338
- it "persists the change" do
1339
+ it 'persists the change' do
1339
1340
  expect(reloaded.tracks.first.before_save_called).to be true
1340
1341
  end
1341
1342
  end
1342
1343
 
1343
- context "when the parent is persisted" do
1344
-
1344
+ context 'when the parent is persisted' do
1345
1345
  let(:band) do
1346
- Band.create(name: "Moderat")
1346
+ Band.create!(name: 'Moderat')
1347
+ end
1348
+ let(:reloaded) do
1349
+ band.reload.records.first
1347
1350
  end
1348
1351
 
1349
1352
  let!(:record) do
1350
- band.records.build(name: "Moderat")
1353
+ band.records.build(name: 'Moderat')
1351
1354
  end
1352
1355
 
1353
1356
  let!(:track) do
1354
- record.tracks.build(name: "Berlin")
1357
+ record.tracks.build(name: 'Berlin')
1355
1358
  end
1356
1359
 
1357
1360
  before do
1358
- band.save
1359
- end
1360
-
1361
- let(:reloaded) do
1362
- band.reload.records.first
1361
+ band.save!
1363
1362
  end
1364
1363
 
1365
- it "executes the callback" do
1364
+ it 'executes the callback' do
1366
1365
  expect(track.before_save_called).to be true
1367
1366
  end
1368
1367
 
1369
- it "persists the change" do
1368
+ it 'persists the change' do
1370
1369
  expect(reloaded.tracks.first.before_save_called).to be true
1371
1370
  end
1372
1371
  end
1373
1372
  end
1374
1373
 
1375
- context "when the child is persisted" do
1376
-
1374
+ context 'when the child is persisted' do
1377
1375
  let(:band) do
1378
- Band.create(name: "Moderat")
1376
+ Band.create!(name: 'Moderat')
1377
+ end
1378
+ let(:reloaded) do
1379
+ band.reload.records.first
1379
1380
  end
1380
1381
 
1381
1382
  let!(:record) do
1382
- band.records.create(name: "Moderat")
1383
+ band.records.create!(name: 'Moderat')
1383
1384
  end
1384
1385
 
1385
1386
  let!(:track) do
1386
- record.tracks.create(name: "Berlin")
1387
+ record.tracks.create!(name: 'Berlin')
1387
1388
  end
1388
1389
 
1389
1390
  before do
1390
- band.save
1391
- end
1392
-
1393
- let(:reloaded) do
1394
- band.reload.records.first
1391
+ band.save!
1395
1392
  end
1396
1393
 
1397
- it "executes the callback" do
1394
+ it 'executes the callback' do
1398
1395
  expect(track.before_save_called).to be true
1399
1396
  end
1400
1397
 
1401
- it "persists the change" do
1398
+ it 'persists the change' do
1402
1399
  expect(reloaded.tracks.first.before_save_called).to be true
1403
1400
  end
1404
1401
  end
1405
1402
  end
1406
1403
 
1407
- describe "#before_update" do
1408
-
1409
- context "when the child is new" do
1410
-
1411
- context "when the parent is new" do
1412
-
1404
+ describe '#before_update' do
1405
+ context 'when the child is new' do
1406
+ context 'when the parent is new' do
1413
1407
  let(:band) do
1414
- Band.new(name: "Moderat")
1408
+ Band.new(name: 'Moderat')
1415
1409
  end
1416
1410
 
1417
1411
  let!(:record) do
1418
- band.records.build(name: "Moderat")
1412
+ band.records.build(name: 'Moderat')
1419
1413
  end
1420
1414
 
1421
1415
  let!(:track) do
1422
- record.tracks.build(name: "Berlin")
1416
+ record.tracks.build(name: 'Berlin')
1423
1417
  end
1424
1418
 
1425
1419
  before do
1426
- band.save
1420
+ band.save!
1427
1421
  end
1428
1422
 
1429
- it "does not execute the callback" do
1423
+ it 'does not execute the callback' do
1430
1424
  expect(track.before_update_called).to be false
1431
1425
  end
1432
1426
  end
1433
1427
 
1434
- context "when the parent is persisted" do
1435
-
1428
+ context 'when the parent is persisted' do
1436
1429
  let(:band) do
1437
- Band.create(name: "Moderat")
1430
+ Band.create!(name: 'Moderat')
1438
1431
  end
1439
1432
 
1440
1433
  let!(:record) do
1441
- band.records.build(name: "Moderat")
1434
+ band.records.build(name: 'Moderat')
1442
1435
  end
1443
1436
 
1444
1437
  let!(:track) do
1445
- record.tracks.build(name: "Berlin")
1438
+ record.tracks.build(name: 'Berlin')
1446
1439
  end
1447
1440
 
1448
1441
  before do
1449
- band.save
1442
+ band.save!
1450
1443
  end
1451
1444
 
1452
- it "does not execute the callback" do
1445
+ it 'does not execute the callback' do
1453
1446
  expect(track.before_update_called).to be false
1454
1447
  end
1455
1448
  end
1456
1449
  end
1457
1450
 
1458
- context "when the child is persisted" do
1459
-
1451
+ context 'when the child is persisted' do
1460
1452
  let(:band) do
1461
- Band.create(name: "Moderat")
1453
+ Band.create!(name: 'Moderat')
1462
1454
  end
1463
1455
 
1464
1456
  let!(:record) do
1465
- band.records.create(name: "Moderat")
1457
+ band.records.create!(name: 'Moderat')
1466
1458
  end
1467
1459
 
1468
1460
  let!(:track) do
1469
- record.tracks.create(name: "Berlin")
1461
+ record.tracks.create!(name: 'Berlin')
1470
1462
  end
1471
1463
 
1472
- context "when the child is dirty" do
1473
-
1464
+ context 'when the child is dirty' do
1474
1465
  before do
1475
- track.name = "Rusty Nails"
1476
- band.save
1466
+ track.name = 'Rusty Nails'
1467
+ band.save!
1477
1468
  end
1478
1469
 
1479
1470
  let(:reloaded) do
1480
1471
  band.reload.records.first
1481
1472
  end
1482
1473
 
1483
- it "executes the callback" do
1474
+ it 'executes the callback' do
1484
1475
  expect(track.before_update_called).to be true
1485
1476
  end
1486
1477
 
1487
- it "persists the change" do
1478
+ it 'persists the change' do
1488
1479
  expect(reloaded.tracks.first.before_update_called).to be true
1489
1480
  end
1490
1481
  end
1491
1482
 
1492
- context "when the child is not dirty" do
1493
-
1483
+ context 'when the child is not dirty' do
1494
1484
  before do
1495
- band.save
1485
+ band.save!
1496
1486
  end
1497
1487
 
1498
- it "does not execute the callback" do
1488
+ it 'does not execute the callback' do
1499
1489
  expect(track.before_update_called).to be false
1500
1490
  end
1501
1491
  end
@@ -1503,11 +1493,9 @@ describe Mongoid::Interceptable do
1503
1493
  end
1504
1494
 
1505
1495
  describe '#after_destroy' do
1506
-
1507
1496
  context 'when the parent is updated in a child after_destroy callback' do
1508
-
1509
1497
  let!(:person) do
1510
- Person.create!(ordered_posts: [OrderedPost.new])
1498
+ Person.create!(ordered_posts: [ OrderedPost.new ])
1511
1499
  end
1512
1500
 
1513
1501
  before do
@@ -1521,76 +1509,71 @@ describe Mongoid::Interceptable do
1521
1509
  end
1522
1510
  end
1523
1511
 
1524
- describe "#before_validation" do
1525
-
1526
- context "when the child is new" do
1527
-
1528
- context "when the parent is new" do
1529
-
1512
+ describe '#before_validation' do
1513
+ context 'when the child is new' do
1514
+ context 'when the parent is new' do
1530
1515
  let(:band) do
1531
- Band.new(name: "Moderat")
1516
+ Band.new(name: 'Moderat')
1532
1517
  end
1533
1518
 
1534
1519
  let!(:record) do
1535
- band.records.build(name: "Moderat")
1520
+ band.records.build(name: 'Moderat')
1536
1521
  end
1537
1522
 
1538
1523
  let!(:track) do
1539
- record.tracks.build(name: "Berlin")
1524
+ record.tracks.build(name: 'Berlin')
1540
1525
  end
1541
1526
 
1542
1527
  before do
1543
- band.save
1528
+ band.save!
1544
1529
  end
1545
1530
 
1546
- it "executes the callback" do
1531
+ it 'executes the callback' do
1547
1532
  expect(track.before_validation_called).to be true
1548
1533
  end
1549
1534
  end
1550
1535
 
1551
- context "when the parent is persisted" do
1552
-
1536
+ context 'when the parent is persisted' do
1553
1537
  let(:band) do
1554
- Band.create(name: "Moderat")
1538
+ Band.create!(name: 'Moderat')
1555
1539
  end
1556
1540
 
1557
1541
  let!(:record) do
1558
- band.records.build(name: "Moderat")
1542
+ band.records.build(name: 'Moderat')
1559
1543
  end
1560
1544
 
1561
1545
  let!(:track) do
1562
- record.tracks.build(name: "Berlin")
1546
+ record.tracks.build(name: 'Berlin')
1563
1547
  end
1564
1548
 
1565
1549
  before do
1566
- band.save
1550
+ band.save!
1567
1551
  end
1568
1552
 
1569
- it "executes the callback" do
1553
+ it 'executes the callback' do
1570
1554
  expect(track.before_validation_called).to be true
1571
1555
  end
1572
1556
  end
1573
1557
  end
1574
1558
 
1575
- context "when the child is persisted" do
1576
-
1559
+ context 'when the child is persisted' do
1577
1560
  let(:band) do
1578
- Band.create(name: "Moderat")
1561
+ Band.create!(name: 'Moderat')
1579
1562
  end
1580
1563
 
1581
1564
  let!(:record) do
1582
- band.records.create(name: "Moderat")
1565
+ band.records.create!(name: 'Moderat')
1583
1566
  end
1584
1567
 
1585
1568
  let!(:track) do
1586
- record.tracks.create(name: "Berlin")
1569
+ record.tracks.create!(name: 'Berlin')
1587
1570
  end
1588
1571
 
1589
1572
  before do
1590
- band.save
1573
+ band.save!
1591
1574
  end
1592
1575
 
1593
- it "executes the callback" do
1576
+ it 'executes the callback' do
1594
1577
  expect(track.before_validation_called).to be true
1595
1578
  end
1596
1579
  end
@@ -1598,35 +1581,32 @@ describe Mongoid::Interceptable do
1598
1581
  end
1599
1582
  end
1600
1583
 
1601
- context "callback on valid?" do
1602
-
1603
- it "goes in all validation callback in good order" do
1584
+ context 'callback on valid?' do
1585
+ it 'goes in all validation callback in good order' do
1604
1586
  shin = ValidationCallback.new
1605
1587
  shin.valid?
1606
- expect(shin.history).to eq([:before_validation, :validate, :after_validation])
1588
+ expect(shin.history).to eq(%i[before_validation validate after_validation])
1607
1589
  end
1608
1590
  end
1609
1591
 
1610
- context "when creating child documents in callbacks" do
1611
-
1592
+ context 'when creating child documents in callbacks' do
1612
1593
  let(:parent) do
1613
1594
  ParentDoc.new
1614
1595
  end
1615
1596
 
1616
1597
  before do
1617
- parent.save
1598
+ parent.save!
1618
1599
  end
1619
1600
 
1620
- it "does not duplicate the child documents" do
1621
- parent.children.create(position: 1)
1601
+ it 'does not duplicate the child documents' do
1602
+ parent.children.create!(position: 1)
1622
1603
  expect(ParentDoc.find(parent.id).children.size).to eq(1)
1623
1604
  end
1624
1605
  end
1625
1606
 
1626
- context "when callbacks cancel persistence" do
1627
-
1607
+ context 'when callbacks cancel persistence' do
1628
1608
  let(:address) do
1629
- Address.new(street: "123 Sesame")
1609
+ Address.new(street: '123 Sesame')
1630
1610
  end
1631
1611
 
1632
1612
  before(:all) do
@@ -1639,84 +1619,962 @@ describe Mongoid::Interceptable do
1639
1619
  Person.reset_callbacks(:save)
1640
1620
  end
1641
1621
 
1642
- context "when creating a document" do
1643
-
1622
+ context 'when creating a document' do
1644
1623
  let(:person) do
1645
- Person.new(mode: :prevent_save, title: "Associate", addresses: [ address ])
1624
+ Person.new(mode: :prevent_save, title: 'Associate', addresses: [ address ])
1646
1625
  end
1647
1626
 
1648
- it "fails to save" do
1627
+ it 'fails to save' do
1649
1628
  expect(person).to be_valid
1650
1629
  expect(person.save).to be false
1651
1630
  end
1652
1631
 
1653
- it "is a new record" do
1632
+ it 'is a new record' do
1654
1633
  expect(person).to be_a_new_record
1655
- expect { person.save }.not_to change { person.new_record? }
1634
+ expect { person.save }.not_to(change { person.new_record? })
1656
1635
  end
1657
1636
 
1658
- it "is left dirty" do
1637
+ it 'is left dirty' do
1659
1638
  expect(person).to be_changed
1660
- expect { person.save }.not_to change { person.changed? }
1639
+ expect { person.save }.not_to(change { person.changed? })
1661
1640
  end
1662
1641
 
1663
- it "child documents are left dirty" do
1642
+ it 'child documents are left dirty' do
1664
1643
  expect(address).to be_changed
1665
- expect { person.save }.not_to change { address.changed? }
1644
+ expect { person.save }.not_to(change { address.changed? })
1666
1645
  end
1667
1646
  end
1668
1647
 
1669
- context "when updating a document" do
1670
-
1648
+ context 'when updating a document' do
1671
1649
  let(:person) do
1672
- Person.create.tap do |person|
1650
+ Person.create!.tap do |person|
1673
1651
  person.attributes = {
1674
1652
  mode: :prevent_save,
1675
- title: "Associate",
1653
+ title: 'Associate',
1676
1654
  addresses: [ address ]
1677
1655
  }
1678
1656
  end
1679
1657
  end
1680
1658
 
1681
- it "#save returns false" do
1659
+ it '#save returns false' do
1682
1660
  expect(person).to be_valid
1683
1661
  expect(person.save).to be false
1684
1662
  end
1685
1663
 
1686
- it "is a not a new record" do
1687
- expect(person).to_not be_a_new_record
1688
- expect { person.save }.not_to change { person.new_record? }
1664
+ it 'is a not a new record' do
1665
+ expect(person).not_to be_a_new_record
1666
+ expect { person.save }.not_to(change { person.new_record? })
1689
1667
  end
1690
1668
 
1691
- it "is left dirty" do
1669
+ it 'is left dirty' do
1692
1670
  expect(person).to be_changed
1693
- expect { person.save }.not_to change { person.changed? }
1671
+ expect { person.save }.not_to(change { person.changed? })
1694
1672
  end
1695
1673
 
1696
- it "child documents are left dirty" do
1674
+ it 'child documents are left dirty' do
1697
1675
  expect(address).to be_changed
1698
- expect { person.save }.not_to change { address.changed? }
1676
+ expect { person.save }.not_to(change { address.changed? })
1699
1677
  end
1700
1678
  end
1701
1679
  end
1702
1680
 
1703
- context "when loading a model multiple times" do
1704
-
1681
+ context 'when loading a model multiple times' do
1705
1682
  before do
1706
- load "spec/app/models/callback_test.rb"
1707
- load "spec/app/models/callback_test.rb"
1683
+ load File.join(MODELS, 'callback_test.rb')
1684
+ load File.join(MODELS, 'callback_test.rb')
1708
1685
  end
1709
1686
 
1710
1687
  let(:callback) do
1711
1688
  CallbackTest.new
1712
1689
  end
1713
1690
 
1714
- context "when saving the document" do
1715
-
1716
- it "only executes the callbacks once" do
1691
+ context 'when saving the document' do
1692
+ it 'only executes the callbacks once' do
1717
1693
  expect(callback).to receive(:execute).once
1718
1694
  callback.save
1719
1695
  end
1720
1696
  end
1721
1697
  end
1698
+
1699
+ context 'when creating a parent and embedded child' do
1700
+ let(:registry) { InterceptableSpec::CallbackRegistry.new }
1701
+ let(:parent) do
1702
+ InterceptableSpec::CbParent.new(registry).tap do |parent|
1703
+ parent.cb_children << InterceptableSpec::CbChild.new(registry, cb_parent: parent)
1704
+ end
1705
+ end
1706
+
1707
+ let(:expected) do
1708
+ [
1709
+ [ InterceptableSpec::CbParent, :before_validation ],
1710
+ [ InterceptableSpec::CbChild, :before_validation ],
1711
+ [ InterceptableSpec::CbChild, :after_validation ],
1712
+ [ InterceptableSpec::CbParent, :after_validation ],
1713
+ [ InterceptableSpec::CbParent, :before_save ],
1714
+ [ InterceptableSpec::CbParent, :around_save_open ],
1715
+ [ InterceptableSpec::CbParent, :before_create ],
1716
+ [ InterceptableSpec::CbParent, :around_create_open ],
1717
+ [ InterceptableSpec::CbParent, :around_create_close ],
1718
+ [ InterceptableSpec::CbParent, :after_create ],
1719
+ [ InterceptableSpec::CbParent, :around_save_close ],
1720
+ [ InterceptableSpec::CbParent, :after_save ]
1721
+ ]
1722
+ end
1723
+
1724
+ it 'calls callbacks in the right order' do
1725
+ parent.save!
1726
+ expect(registry.calls).to eq expected
1727
+ end
1728
+ end
1729
+
1730
+ context 'when creating a parent and embedded child with cascading callbacks' do
1731
+ let(:registry) { InterceptableSpec::CallbackRegistry.new }
1732
+ let(:parent) do
1733
+ InterceptableSpec::CbParent.new(registry).tap do |parent|
1734
+ parent.cb_cascaded_children <<
1735
+ InterceptableSpec::CbCascadedChild.new(registry, cb_parent: parent)
1736
+ end
1737
+ end
1738
+
1739
+ context 'with around callbacks' do
1740
+ config_override :around_callbacks_for_embeds, true
1741
+
1742
+ after do
1743
+ Mongoid::Threaded.stack('interceptable').clear
1744
+ end
1745
+
1746
+ let(:stack) { Mongoid::Threaded.stack('interceptable') }
1747
+
1748
+ let(:expected) do
1749
+ [
1750
+ [ InterceptableSpec::CbCascadedChild, :before_validation ],
1751
+ [ InterceptableSpec::CbCascadedChild, :after_validation ],
1752
+ [ InterceptableSpec::CbParent, :before_validation ],
1753
+ [ InterceptableSpec::CbCascadedChild, :before_validation ],
1754
+ [ InterceptableSpec::CbCascadedChild, :after_validation ],
1755
+
1756
+ [ InterceptableSpec::CbParent, :after_validation ],
1757
+ [ InterceptableSpec::CbParent, :before_save ],
1758
+ [ InterceptableSpec::CbParent, :around_save_open ],
1759
+ [ InterceptableSpec::CbParent, :before_create ],
1760
+ [ InterceptableSpec::CbParent, :around_create_open ],
1761
+
1762
+ [ InterceptableSpec::CbCascadedChild, :before_save ],
1763
+ [ InterceptableSpec::CbCascadedChild, :around_save_open ],
1764
+ [ InterceptableSpec::CbCascadedChild, :before_create ],
1765
+ [ InterceptableSpec::CbCascadedChild, :around_create_open ],
1766
+
1767
+ [ InterceptableSpec::CbCascadedChild, :around_create_close ],
1768
+ [ InterceptableSpec::CbCascadedChild, :after_create ],
1769
+ [ InterceptableSpec::CbCascadedChild, :around_save_close ],
1770
+ [ InterceptableSpec::CbCascadedChild, :after_save ],
1771
+
1772
+ [ InterceptableSpec::CbParent, :around_create_close ],
1773
+ [ InterceptableSpec::CbParent, :after_create ],
1774
+ [ InterceptableSpec::CbParent, :around_save_close ],
1775
+ [ InterceptableSpec::CbParent, :after_save ]
1776
+ ]
1777
+ end
1778
+
1779
+ it 'calls callbacks in the right order' do
1780
+ parent.save!
1781
+ expect(registry.calls).to eq expected
1782
+ end
1783
+
1784
+ it 'shows that cascaded callbacks can access Mongoid state' do
1785
+ expect(stack).to be_empty
1786
+ parent.save!
1787
+ expect(stack).not_to be_empty
1788
+ end
1789
+ end
1790
+
1791
+ context 'without around callbacks' do
1792
+ config_override :around_callbacks_for_embeds, false
1793
+
1794
+ let(:expected) do
1795
+ [
1796
+ [ InterceptableSpec::CbCascadedChild, :before_validation ],
1797
+ [ InterceptableSpec::CbCascadedChild, :after_validation ],
1798
+ [ InterceptableSpec::CbParent, :before_validation ],
1799
+ [ InterceptableSpec::CbCascadedChild, :before_validation ],
1800
+ [ InterceptableSpec::CbCascadedChild, :after_validation ],
1801
+
1802
+ [ InterceptableSpec::CbParent, :after_validation ],
1803
+ [ InterceptableSpec::CbParent, :before_save ],
1804
+ [ InterceptableSpec::CbParent, :around_save_open ],
1805
+ [ InterceptableSpec::CbParent, :before_create ],
1806
+ [ InterceptableSpec::CbParent, :around_create_open ],
1807
+
1808
+ [ InterceptableSpec::CbCascadedChild, :before_save ],
1809
+ [ InterceptableSpec::CbCascadedChild, :before_create ],
1810
+
1811
+ [ InterceptableSpec::CbCascadedChild, :after_create ],
1812
+ [ InterceptableSpec::CbCascadedChild, :after_save ],
1813
+
1814
+ [ InterceptableSpec::CbParent, :around_create_close ],
1815
+ [ InterceptableSpec::CbParent, :after_create ],
1816
+ [ InterceptableSpec::CbParent, :around_save_close ],
1817
+ [ InterceptableSpec::CbParent, :after_save ]
1818
+ ]
1819
+ end
1820
+
1821
+ it 'calls callbacks in the right order' do
1822
+ parent.save!
1823
+ expect(registry.calls).to eq expected
1824
+ end
1825
+ end
1826
+ end
1827
+
1828
+ context 'with associations' do
1829
+ context 'has_one' do
1830
+ let(:registry) { InterceptableSpec::CallbackRegistry.new }
1831
+
1832
+ let(:parent) do
1833
+ InterceptableSpec::CbHasOneParent.new(registry).tap do |parent|
1834
+ parent.child = InterceptableSpec::CbHasOneChild.new(registry)
1835
+ end
1836
+ end
1837
+
1838
+ let(:expected) do
1839
+ [
1840
+ [ InterceptableSpec::CbHasOneParent, :before_validation ],
1841
+ [ InterceptableSpec::CbHasOneChild, :before_validation ],
1842
+ [ InterceptableSpec::CbHasOneChild, :after_validation ],
1843
+ [ InterceptableSpec::CbHasOneParent, :after_validation ],
1844
+ [ InterceptableSpec::CbHasOneParent, :before_save ],
1845
+
1846
+ [ InterceptableSpec::CbHasOneParent, :around_save_open ],
1847
+ [ InterceptableSpec::CbHasOneParent, :before_create ],
1848
+ [ InterceptableSpec::CbHasOneParent, :around_create_open ],
1849
+
1850
+ [ InterceptableSpec::CbHasOneParent, :insert_into_database ],
1851
+
1852
+ [ InterceptableSpec::CbHasOneChild, :before_validation ],
1853
+ [ InterceptableSpec::CbHasOneChild, :after_validation ],
1854
+ [ InterceptableSpec::CbHasOneChild, :before_save ],
1855
+ [ InterceptableSpec::CbHasOneChild, :around_save_open ],
1856
+ [ InterceptableSpec::CbHasOneChild, :before_create ],
1857
+ [ InterceptableSpec::CbHasOneChild, :around_create_open ],
1858
+
1859
+ [ InterceptableSpec::CbHasOneChild, :around_create_close ],
1860
+ [ InterceptableSpec::CbHasOneChild, :after_create ],
1861
+ [ InterceptableSpec::CbHasOneChild, :around_save_close ],
1862
+ [ InterceptableSpec::CbHasOneChild, :after_save ],
1863
+
1864
+ [ InterceptableSpec::CbHasOneParent, :around_create_close ],
1865
+ [ InterceptableSpec::CbHasOneParent, :after_create ],
1866
+ [ InterceptableSpec::CbHasOneParent, :around_save_close ],
1867
+ [ InterceptableSpec::CbHasOneParent, :after_save ]
1868
+ ]
1869
+ end
1870
+
1871
+ it 'calls callbacks in the right order' do
1872
+ parent.save!
1873
+ expect(registry.calls).to eq expected
1874
+ end
1875
+ end
1876
+
1877
+ context 'embeds_one' do
1878
+ let(:registry) { InterceptableSpec::CallbackRegistry.new }
1879
+
1880
+ let(:parent) do
1881
+ InterceptableSpec::CbEmbedsOneParent.new(registry).tap do |parent|
1882
+ parent.child = InterceptableSpec::CbEmbedsOneChild.new(registry)
1883
+ end
1884
+ end
1885
+
1886
+ context 'create' do
1887
+ context 'with around callbacks' do
1888
+ config_override :around_callbacks_for_embeds, true
1889
+
1890
+ let(:expected) do
1891
+ [
1892
+ [ InterceptableSpec::CbEmbedsOneChild, :before_validation ],
1893
+ [ InterceptableSpec::CbEmbedsOneChild, :after_validation ],
1894
+ [ InterceptableSpec::CbEmbedsOneParent, :before_validation ],
1895
+ [ InterceptableSpec::CbEmbedsOneChild, :before_validation ],
1896
+ [ InterceptableSpec::CbEmbedsOneChild, :after_validation ],
1897
+ [ InterceptableSpec::CbEmbedsOneParent, :after_validation ],
1898
+
1899
+ [ InterceptableSpec::CbEmbedsOneParent, :before_save ],
1900
+ [ InterceptableSpec::CbEmbedsOneParent, :around_save_open ],
1901
+ [ InterceptableSpec::CbEmbedsOneParent, :before_create ],
1902
+ [ InterceptableSpec::CbEmbedsOneParent, :around_create_open ],
1903
+
1904
+ [ InterceptableSpec::CbEmbedsOneChild, :before_save ],
1905
+ [ InterceptableSpec::CbEmbedsOneChild, :around_save_open ],
1906
+ [ InterceptableSpec::CbEmbedsOneChild, :before_create ],
1907
+ [ InterceptableSpec::CbEmbedsOneChild, :around_create_open ],
1908
+
1909
+ [ InterceptableSpec::CbEmbedsOneParent, :insert_into_database ],
1910
+
1911
+ [ InterceptableSpec::CbEmbedsOneChild, :around_create_close ],
1912
+ [ InterceptableSpec::CbEmbedsOneChild, :after_create ],
1913
+ [ InterceptableSpec::CbEmbedsOneChild, :around_save_close ],
1914
+ [ InterceptableSpec::CbEmbedsOneChild, :after_save ],
1915
+
1916
+ [ InterceptableSpec::CbEmbedsOneParent, :around_create_close ],
1917
+ [ InterceptableSpec::CbEmbedsOneParent, :after_create ],
1918
+ [ InterceptableSpec::CbEmbedsOneParent, :around_save_close ],
1919
+ [ InterceptableSpec::CbEmbedsOneParent, :after_save ]
1920
+ ]
1921
+ end
1922
+
1923
+ it 'calls callbacks in the right order' do
1924
+ parent.save!
1925
+ expect(registry.calls).to eq expected
1926
+ end
1927
+ end
1928
+
1929
+ context 'without around callbacks' do
1930
+ config_override :around_callbacks_for_embeds, false
1931
+
1932
+ let(:expected) do
1933
+ [
1934
+ [ InterceptableSpec::CbEmbedsOneChild, :before_validation ],
1935
+ [ InterceptableSpec::CbEmbedsOneChild, :after_validation ],
1936
+ [ InterceptableSpec::CbEmbedsOneParent, :before_validation ],
1937
+ [ InterceptableSpec::CbEmbedsOneChild, :before_validation ],
1938
+ [ InterceptableSpec::CbEmbedsOneChild, :after_validation ],
1939
+ [ InterceptableSpec::CbEmbedsOneParent, :after_validation ],
1940
+
1941
+ [ InterceptableSpec::CbEmbedsOneParent, :before_save ],
1942
+ [ InterceptableSpec::CbEmbedsOneParent, :around_save_open ],
1943
+ [ InterceptableSpec::CbEmbedsOneParent, :before_create ],
1944
+ [ InterceptableSpec::CbEmbedsOneParent, :around_create_open ],
1945
+
1946
+ [ InterceptableSpec::CbEmbedsOneChild, :before_save ],
1947
+ [ InterceptableSpec::CbEmbedsOneChild, :before_create ],
1948
+
1949
+ [ InterceptableSpec::CbEmbedsOneParent, :insert_into_database ],
1950
+
1951
+ [ InterceptableSpec::CbEmbedsOneChild, :after_create ],
1952
+ [ InterceptableSpec::CbEmbedsOneChild, :after_save ],
1953
+
1954
+ [ InterceptableSpec::CbEmbedsOneParent, :around_create_close ],
1955
+ [ InterceptableSpec::CbEmbedsOneParent, :after_create ],
1956
+ [ InterceptableSpec::CbEmbedsOneParent, :around_save_close ],
1957
+ [ InterceptableSpec::CbEmbedsOneParent, :after_save ]
1958
+ ]
1959
+ end
1960
+
1961
+ it 'calls callbacks in the right order' do
1962
+ parent.save!
1963
+ expect(registry.calls).to eq expected
1964
+ end
1965
+ end
1966
+ end
1967
+
1968
+ context 'update' do
1969
+ context 'with around callbacks' do
1970
+ config_override :around_callbacks_for_embeds, true
1971
+
1972
+ let(:expected) do
1973
+ [
1974
+ [ InterceptableSpec::CbEmbedsOneChild, :before_validation ],
1975
+ [ InterceptableSpec::CbEmbedsOneChild, :after_validation ],
1976
+ [ InterceptableSpec::CbEmbedsOneParent, :before_validation ],
1977
+ [ InterceptableSpec::CbEmbedsOneChild, :before_validation ],
1978
+ [ InterceptableSpec::CbEmbedsOneChild, :after_validation ],
1979
+ [ InterceptableSpec::CbEmbedsOneParent, :after_validation ],
1980
+
1981
+ [ InterceptableSpec::CbEmbedsOneParent, :before_save ],
1982
+ [ InterceptableSpec::CbEmbedsOneParent, :around_save_open ],
1983
+ [ InterceptableSpec::CbEmbedsOneParent, :before_update ],
1984
+ [ InterceptableSpec::CbEmbedsOneParent, :around_update_open ],
1985
+
1986
+ [ InterceptableSpec::CbEmbedsOneChild, :before_save ],
1987
+ [ InterceptableSpec::CbEmbedsOneChild, :around_save_open ],
1988
+ [ InterceptableSpec::CbEmbedsOneChild, :before_update ],
1989
+ [ InterceptableSpec::CbEmbedsOneChild, :around_update_open ],
1990
+
1991
+ [ InterceptableSpec::CbEmbedsOneChild, :around_update_close ],
1992
+ [ InterceptableSpec::CbEmbedsOneChild, :after_update ],
1993
+ [ InterceptableSpec::CbEmbedsOneChild, :around_save_close ],
1994
+ [ InterceptableSpec::CbEmbedsOneChild, :after_save ],
1995
+
1996
+ [ InterceptableSpec::CbEmbedsOneParent, :around_update_close ],
1997
+ [ InterceptableSpec::CbEmbedsOneParent, :after_update ],
1998
+ [ InterceptableSpec::CbEmbedsOneParent, :around_save_close ],
1999
+ [ InterceptableSpec::CbEmbedsOneParent, :after_save ]
2000
+ ]
2001
+ end
2002
+
2003
+ it 'calls callbacks in the right order' do
2004
+ parent.callback_registry = nil
2005
+ parent.child.callback_registry = nil
2006
+ parent.save!
2007
+
2008
+ parent.callback_registry = registry
2009
+ parent.child.callback_registry = registry
2010
+ parent.name = 'name'
2011
+ parent.child.age = 10
2012
+
2013
+ parent.save!
2014
+ expect(registry.calls).to eq expected
2015
+ end
2016
+ end
2017
+
2018
+ context 'without around callbacks' do
2019
+ config_override :around_callbacks_for_embeds, false
2020
+
2021
+ let(:expected) do
2022
+ [
2023
+ [ InterceptableSpec::CbEmbedsOneChild, :before_validation ],
2024
+ [ InterceptableSpec::CbEmbedsOneChild, :after_validation ],
2025
+ [ InterceptableSpec::CbEmbedsOneParent, :before_validation ],
2026
+ [ InterceptableSpec::CbEmbedsOneChild, :before_validation ],
2027
+ [ InterceptableSpec::CbEmbedsOneChild, :after_validation ],
2028
+ [ InterceptableSpec::CbEmbedsOneParent, :after_validation ],
2029
+
2030
+ [ InterceptableSpec::CbEmbedsOneParent, :before_save ],
2031
+ [ InterceptableSpec::CbEmbedsOneParent, :around_save_open ],
2032
+ [ InterceptableSpec::CbEmbedsOneParent, :before_update ],
2033
+ [ InterceptableSpec::CbEmbedsOneParent, :around_update_open ],
2034
+
2035
+ [ InterceptableSpec::CbEmbedsOneChild, :before_save ],
2036
+ [ InterceptableSpec::CbEmbedsOneChild, :before_update ],
2037
+
2038
+ [ InterceptableSpec::CbEmbedsOneChild, :after_update ],
2039
+ [ InterceptableSpec::CbEmbedsOneChild, :after_save ],
2040
+
2041
+ [ InterceptableSpec::CbEmbedsOneParent, :around_update_close ],
2042
+ [ InterceptableSpec::CbEmbedsOneParent, :after_update ],
2043
+ [ InterceptableSpec::CbEmbedsOneParent, :around_save_close ],
2044
+ [ InterceptableSpec::CbEmbedsOneParent, :after_save ]
2045
+ ]
2046
+ end
2047
+
2048
+ it 'calls callbacks in the right order' do
2049
+ parent.callback_registry = nil
2050
+ parent.child.callback_registry = nil
2051
+ parent.save!
2052
+
2053
+ parent.callback_registry = registry
2054
+ parent.child.callback_registry = registry
2055
+ parent.name = 'name'
2056
+ parent.child.age = 10
2057
+
2058
+ parent.save!
2059
+ expect(registry.calls).to eq expected
2060
+ end
2061
+ end
2062
+ end
2063
+ end
2064
+
2065
+ context 'has_many' do
2066
+ let(:registry) { InterceptableSpec::CallbackRegistry.new }
2067
+
2068
+ let(:parent) do
2069
+ InterceptableSpec::CbHasManyParent.new(registry).tap do |parent|
2070
+ parent.children = [
2071
+ InterceptableSpec::CbHasManyChild.new(registry),
2072
+ InterceptableSpec::CbHasManyChild.new(registry)
2073
+ ]
2074
+ end
2075
+ end
2076
+
2077
+ let(:expected) do
2078
+ [
2079
+ [ InterceptableSpec::CbHasManyParent, :before_validation ],
2080
+ [ InterceptableSpec::CbHasManyChild, :before_validation ],
2081
+ [ InterceptableSpec::CbHasManyChild, :after_validation ],
2082
+ [ InterceptableSpec::CbHasManyChild, :before_validation ],
2083
+ [ InterceptableSpec::CbHasManyChild, :after_validation ],
2084
+ [ InterceptableSpec::CbHasManyParent, :after_validation ],
2085
+
2086
+ [ InterceptableSpec::CbHasManyParent, :before_save ],
2087
+ [ InterceptableSpec::CbHasManyParent, :around_save_open ],
2088
+ [ InterceptableSpec::CbHasManyParent, :before_create ],
2089
+ [ InterceptableSpec::CbHasManyParent, :around_create_open ],
2090
+
2091
+ [ InterceptableSpec::CbHasManyParent, :insert_into_database ],
2092
+
2093
+ [ InterceptableSpec::CbHasManyChild, :before_validation ],
2094
+ [ InterceptableSpec::CbHasManyChild, :after_validation ],
2095
+ [ InterceptableSpec::CbHasManyChild, :before_save ],
2096
+ [ InterceptableSpec::CbHasManyChild, :around_save_open ],
2097
+ [ InterceptableSpec::CbHasManyChild, :before_create ],
2098
+ [ InterceptableSpec::CbHasManyChild, :around_create_open ],
2099
+ [ InterceptableSpec::CbHasManyChild, :around_create_close ],
2100
+ [ InterceptableSpec::CbHasManyChild, :after_create ],
2101
+ [ InterceptableSpec::CbHasManyChild, :around_save_close ],
2102
+ [ InterceptableSpec::CbHasManyChild, :after_save ],
2103
+
2104
+ [ InterceptableSpec::CbHasManyChild, :before_validation ],
2105
+ [ InterceptableSpec::CbHasManyChild, :after_validation ],
2106
+ [ InterceptableSpec::CbHasManyChild, :before_save ],
2107
+ [ InterceptableSpec::CbHasManyChild, :around_save_open ],
2108
+ [ InterceptableSpec::CbHasManyChild, :before_create ],
2109
+ [ InterceptableSpec::CbHasManyChild, :around_create_open ],
2110
+ [ InterceptableSpec::CbHasManyChild, :around_create_close ],
2111
+ [ InterceptableSpec::CbHasManyChild, :after_create ],
2112
+ [ InterceptableSpec::CbHasManyChild, :around_save_close ],
2113
+ [ InterceptableSpec::CbHasManyChild, :after_save ],
2114
+
2115
+ [ InterceptableSpec::CbHasManyParent, :around_create_close ],
2116
+ [ InterceptableSpec::CbHasManyParent, :after_create ],
2117
+ [ InterceptableSpec::CbHasManyParent, :around_save_close ],
2118
+ [ InterceptableSpec::CbHasManyParent, :after_save ]
2119
+ ]
2120
+ end
2121
+
2122
+ it 'calls callbacks in the right order' do
2123
+ parent.save!
2124
+ expect(registry.calls).to eq expected
2125
+ end
2126
+ end
2127
+
2128
+ context 'embeds_many' do
2129
+ let(:registry) { InterceptableSpec::CallbackRegistry.new }
2130
+
2131
+ let(:parent) do
2132
+ InterceptableSpec::CbEmbedsManyParent.new(registry).tap do |parent|
2133
+ parent.children = [
2134
+ InterceptableSpec::CbEmbedsManyChild.new(registry),
2135
+ InterceptableSpec::CbEmbedsManyChild.new(registry)
2136
+ ]
2137
+ end
2138
+ end
2139
+
2140
+ context 'with around callbacks' do
2141
+ config_override :around_callbacks_for_embeds, true
2142
+
2143
+ let(:expected) do
2144
+ [
2145
+ [ InterceptableSpec::CbEmbedsManyChild, :before_validation ],
2146
+ [ InterceptableSpec::CbEmbedsManyChild, :after_validation ],
2147
+ [ InterceptableSpec::CbEmbedsManyChild, :before_validation ],
2148
+ [ InterceptableSpec::CbEmbedsManyChild, :after_validation ],
2149
+ [ InterceptableSpec::CbEmbedsManyParent, :before_validation ],
2150
+ [ InterceptableSpec::CbEmbedsManyChild, :before_validation ],
2151
+ [ InterceptableSpec::CbEmbedsManyChild, :after_validation ],
2152
+ [ InterceptableSpec::CbEmbedsManyChild, :before_validation ],
2153
+ [ InterceptableSpec::CbEmbedsManyChild, :after_validation ],
2154
+ [ InterceptableSpec::CbEmbedsManyParent, :after_validation ],
2155
+
2156
+ [ InterceptableSpec::CbEmbedsManyParent, :before_save ],
2157
+ [ InterceptableSpec::CbEmbedsManyParent, :around_save_open ],
2158
+ [ InterceptableSpec::CbEmbedsManyParent, :before_create ],
2159
+ [ InterceptableSpec::CbEmbedsManyParent, :around_create_open ],
2160
+
2161
+ [ InterceptableSpec::CbEmbedsManyChild, :before_save ],
2162
+ [ InterceptableSpec::CbEmbedsManyChild, :around_save_open ],
2163
+ [ InterceptableSpec::CbEmbedsManyChild, :before_save ],
2164
+
2165
+ [ InterceptableSpec::CbEmbedsManyChild, :around_save_open ],
2166
+ [ InterceptableSpec::CbEmbedsManyChild, :before_create ],
2167
+ [ InterceptableSpec::CbEmbedsManyChild, :around_create_open ],
2168
+
2169
+ [ InterceptableSpec::CbEmbedsManyChild, :before_create ],
2170
+ [ InterceptableSpec::CbEmbedsManyChild, :around_create_open ],
2171
+
2172
+ [ InterceptableSpec::CbEmbedsManyParent, :insert_into_database ],
2173
+
2174
+ [ InterceptableSpec::CbEmbedsManyChild, :around_create_close ],
2175
+ [ InterceptableSpec::CbEmbedsManyChild, :after_create ],
2176
+
2177
+ [ InterceptableSpec::CbEmbedsManyChild, :around_create_close ],
2178
+ [ InterceptableSpec::CbEmbedsManyChild, :after_create ],
2179
+
2180
+ [ InterceptableSpec::CbEmbedsManyChild, :around_save_close ],
2181
+ [ InterceptableSpec::CbEmbedsManyChild, :after_save ],
2182
+
2183
+ [ InterceptableSpec::CbEmbedsManyChild, :around_save_close ],
2184
+ [ InterceptableSpec::CbEmbedsManyChild, :after_save ],
2185
+
2186
+ [ InterceptableSpec::CbEmbedsManyParent, :around_create_close ],
2187
+ [ InterceptableSpec::CbEmbedsManyParent, :after_create ],
2188
+ [ InterceptableSpec::CbEmbedsManyParent, :around_save_close ],
2189
+ [ InterceptableSpec::CbEmbedsManyParent, :after_save ]
2190
+ ]
2191
+ end
2192
+
2193
+ it 'calls callbacks in the right order' do
2194
+ parent.save!
2195
+ expect(registry.calls).to eq expected
2196
+ end
2197
+ end
2198
+
2199
+ context 'without around callbacks' do
2200
+ config_override :around_callbacks_for_embeds, false
2201
+
2202
+ let(:expected) do
2203
+ [
2204
+ [ InterceptableSpec::CbEmbedsManyChild, :before_validation ],
2205
+ [ InterceptableSpec::CbEmbedsManyChild, :after_validation ],
2206
+ [ InterceptableSpec::CbEmbedsManyChild, :before_validation ],
2207
+ [ InterceptableSpec::CbEmbedsManyChild, :after_validation ],
2208
+ [ InterceptableSpec::CbEmbedsManyParent, :before_validation ],
2209
+ [ InterceptableSpec::CbEmbedsManyChild, :before_validation ],
2210
+ [ InterceptableSpec::CbEmbedsManyChild, :after_validation ],
2211
+ [ InterceptableSpec::CbEmbedsManyChild, :before_validation ],
2212
+ [ InterceptableSpec::CbEmbedsManyChild, :after_validation ],
2213
+ [ InterceptableSpec::CbEmbedsManyParent, :after_validation ],
2214
+
2215
+ [ InterceptableSpec::CbEmbedsManyParent, :before_save ],
2216
+ [ InterceptableSpec::CbEmbedsManyParent, :around_save_open ],
2217
+ [ InterceptableSpec::CbEmbedsManyParent, :before_create ],
2218
+ [ InterceptableSpec::CbEmbedsManyParent, :around_create_open ],
2219
+
2220
+ [ InterceptableSpec::CbEmbedsManyChild, :before_save ],
2221
+ [ InterceptableSpec::CbEmbedsManyChild, :before_save ],
2222
+
2223
+ [ InterceptableSpec::CbEmbedsManyChild, :before_create ],
2224
+
2225
+ [ InterceptableSpec::CbEmbedsManyChild, :before_create ],
2226
+
2227
+ [ InterceptableSpec::CbEmbedsManyParent, :insert_into_database ],
2228
+
2229
+ [ InterceptableSpec::CbEmbedsManyChild, :after_create ],
2230
+
2231
+ [ InterceptableSpec::CbEmbedsManyChild, :after_create ],
2232
+
2233
+ [ InterceptableSpec::CbEmbedsManyChild, :after_save ],
2234
+
2235
+ [ InterceptableSpec::CbEmbedsManyChild, :after_save ],
2236
+
2237
+ [ InterceptableSpec::CbEmbedsManyParent, :around_create_close ],
2238
+ [ InterceptableSpec::CbEmbedsManyParent, :after_create ],
2239
+ [ InterceptableSpec::CbEmbedsManyParent, :around_save_close ],
2240
+ [ InterceptableSpec::CbEmbedsManyParent, :after_save ]
2241
+ ]
2242
+ end
2243
+
2244
+ it 'calls callbacks in the right order' do
2245
+ parent.save!
2246
+ expect(registry.calls).to eq expected
2247
+ end
2248
+ end
2249
+ end
2250
+ end
2251
+
2252
+ context 'when accessing parent document from callbacks' do
2253
+ shared_examples 'accesses the correct parent' do
2254
+ it 'accesses the correct parent in after_find' do
2255
+ expect(from_db.after_find_player).to eq(player._id)
2256
+ end
2257
+
2258
+ it 'accesses the correct parent in after_initialize' do
2259
+ expect(from_db.after_initialize_player).to eq(player._id)
2260
+ end
2261
+
2262
+ it 'accesses the correct parent in default' do
2263
+ expect(from_db.after_default_player).to eq(player._id)
2264
+ end
2265
+
2266
+ it 'accesses the correct parent in unpersisted after_initialize' do
2267
+ expect(unpersisted.after_initialize_player).to eq(player._id)
2268
+ end
2269
+ end
2270
+
2271
+ context 'when using create methods' do
2272
+ context 'when the child is an embeds_many association' do
2273
+ let!(:player) do
2274
+ Player.create!.tap do |player|
2275
+ player.implants.create!
2276
+ end
2277
+ end
2278
+ let(:from_db) do
2279
+ Player.find(player.id).implants.first
2280
+ end
2281
+
2282
+ let(:unpersisted) { player.implants.first }
2283
+
2284
+ before do
2285
+ # The default is originally set when creating this document, and it is
2286
+ # subsequently persisted to the database. Therefore when we retrieve
2287
+ # this document from the database, this field is already set, and
2288
+ # the default Proc is not called. This unset is needed to allow the
2289
+ # default Proc to be called when the document is retrieved from the
2290
+ # database.
2291
+ Player.find(player.id).implants.first.unset(:after_default_player)
2292
+ end
2293
+
2294
+ include_examples 'accesses the correct parent'
2295
+ end
2296
+
2297
+ context 'when the child is an embeds_one association' do
2298
+ let!(:player) do
2299
+ Player.create!.tap do |player|
2300
+ player.create_augmentation
2301
+ end
2302
+ end
2303
+ let(:from_db) do
2304
+ Player.find(player.id).augmentation
2305
+ end
2306
+
2307
+ let(:unpersisted) { player.augmentation }
2308
+
2309
+ before do
2310
+ Player.find(player.id).augmentation.unset(:after_default_player)
2311
+ end
2312
+
2313
+ include_examples 'accesses the correct parent'
2314
+ end
2315
+
2316
+ context 'when the child is a has_many association' do
2317
+ let!(:player) do
2318
+ Player.create!.tap do |player|
2319
+ player.weapons.create!
2320
+ end
2321
+ end
2322
+ let(:from_db) do
2323
+ Player.find(player.id).weapons.first
2324
+ end
2325
+
2326
+ let(:unpersisted) { player.weapons.first }
2327
+
2328
+ before do
2329
+ Player.find(player.id).weapons.first.unset(:after_default_player)
2330
+ end
2331
+
2332
+ include_examples 'accesses the correct parent'
2333
+ end
2334
+
2335
+ context 'when the child is a has_one association' do
2336
+ let!(:player) do
2337
+ Player.create!.tap do |player|
2338
+ player.create_powerup
2339
+ player.save!
2340
+ end
2341
+ end
2342
+ let(:from_db) do
2343
+ Player.find(player.id).powerup
2344
+ end
2345
+
2346
+ let(:unpersisted) { player.powerup }
2347
+
2348
+ before do
2349
+ Player.find(player.id).powerup.unset(:after_default_player)
2350
+ end
2351
+
2352
+ include_examples 'accesses the correct parent'
2353
+ end
2354
+
2355
+ context 'when the child is a has_and_belongs_to_many association' do
2356
+ let!(:player) do
2357
+ Player.create!.tap do |player|
2358
+ player.shields.create!
2359
+ end
2360
+ end
2361
+ let(:from_db) do
2362
+ Player.find(player.id).shields.first
2363
+ end
2364
+
2365
+ let(:unpersisted) { player.shields.first }
2366
+
2367
+ before do
2368
+ Player.find(player.id).shields.unset(:after_default_player)
2369
+ end
2370
+
2371
+ include_examples 'accesses the correct parent'
2372
+ end
2373
+ end
2374
+
2375
+ context 'when using build methods' do
2376
+ context 'when the child is an embeds_many association' do
2377
+ let!(:player) do
2378
+ Player.create!.tap do |player|
2379
+ player.implants.build
2380
+ player.implants.first.save!
2381
+ end
2382
+ end
2383
+ let(:from_db) do
2384
+ Player.find(player.id).implants.first
2385
+ end
2386
+
2387
+ let(:unpersisted) { player.implants.first }
2388
+
2389
+ before do
2390
+ Player.find(player.id).implants.first.unset(:after_default_player)
2391
+ end
2392
+
2393
+ include_examples 'accesses the correct parent'
2394
+ end
2395
+
2396
+ context 'when the child is an embeds_one association' do
2397
+ let!(:player) do
2398
+ Player.create!.tap do |player|
2399
+ player.build_augmentation
2400
+ player.save!
2401
+ end
2402
+ end
2403
+ let(:from_db) do
2404
+ Player.find(player.id).augmentation
2405
+ end
2406
+
2407
+ let(:unpersisted) { player.augmentation }
2408
+
2409
+ before do
2410
+ Player.find(player.id).augmentation.unset(:after_default_player)
2411
+ end
2412
+
2413
+ include_examples 'accesses the correct parent'
2414
+ end
2415
+
2416
+ context 'when the child is a has_many association' do
2417
+ let!(:player) do
2418
+ Player.create!.tap do |player|
2419
+ player.weapons.build
2420
+ player.weapons.first.save!
2421
+ end
2422
+ end
2423
+ let(:from_db) do
2424
+ Player.find(player.id).weapons.first
2425
+ end
2426
+
2427
+ let(:unpersisted) { player.weapons.first }
2428
+
2429
+ before do
2430
+ Player.find(player.id).weapons.first.unset(:after_default_player)
2431
+ end
2432
+
2433
+ include_examples 'accesses the correct parent'
2434
+ end
2435
+
2436
+ context 'when the child is a has_one association' do
2437
+ let!(:player) do
2438
+ Player.create!.tap do |player|
2439
+ player.build_powerup
2440
+ player.powerup.save!
2441
+ end
2442
+ end
2443
+ let(:from_db) do
2444
+ Player.find(player.id).powerup
2445
+ end
2446
+
2447
+ let(:unpersisted) { player.powerup }
2448
+
2449
+ before do
2450
+ Player.find(player.id).powerup.unset(:after_default_player)
2451
+ end
2452
+
2453
+ include_examples 'accesses the correct parent'
2454
+ end
2455
+
2456
+ context 'when the child is a has_and_belongs_to_many association' do
2457
+ let!(:player) do
2458
+ Player.create!.tap do |player|
2459
+ player.shields.build
2460
+ player.shields.first.save!
2461
+ end
2462
+ end
2463
+ let(:from_db) do
2464
+ Player.find(player.id).shields.first
2465
+ end
2466
+
2467
+ let(:unpersisted) { player.shields.first }
2468
+
2469
+ before do
2470
+ Player.find(player.id).shields.unset(:after_default_player)
2471
+ end
2472
+
2473
+ include_examples 'accesses the correct parent'
2474
+ end
2475
+ end
2476
+ end
2477
+
2478
+ context 'when accessing associations in defaults' do
2479
+ context 'when not using autobuilding' do
2480
+ let(:band) { InterceptableBand.create(name: 'Molejo') }
2481
+ let(:song) { band.songs.create(name: 'Cilada') }
2482
+
2483
+ it 'assigns the default correctly' do
2484
+ expect(song.band_name).to eq('Molejo')
2485
+ end
2486
+ end
2487
+
2488
+ context 'when using autobuilding' do
2489
+ before do
2490
+ InterceptablePlane.create!.tap do |plane|
2491
+ plane.wings.create!
2492
+ end
2493
+ end
2494
+
2495
+ let(:plane) { InterceptablePlane.first }
2496
+ let(:wing) { InterceptableWing.first }
2497
+ let(:engine) { wing.engine }
2498
+
2499
+ it 'sets the defaults correctly' do
2500
+ expect(wing._id).to eq('hello-wing')
2501
+ expect(wing.p_id).to eq(plane._id.to_s)
2502
+ expect(wing.e_id).to eq(engine._id.to_s)
2503
+ expect(engine._id).to eq("hello-engine-#{wing.id}")
2504
+ end
2505
+ end
2506
+ end
2507
+
2508
+ # This case is rather niche. The _ids method used to use the `.only` method
2509
+ # to get only the _ids for an association, which was causing a
2510
+ # MissingAttributeError to be raised when accessing another association. This
2511
+ # was fixed by using `.pluck` over `.only`. Look at MONGOID-5306 for a more
2512
+ # detailed explanation.
2513
+ context 'when accessing _ids in validate and access an association in after_initialize' do
2514
+ it "doesn't raise AttributeNotLoaded" do
2515
+ company = InterceptableCompany.create!
2516
+ InterceptableShop.create!(company: company)
2517
+ user = InterceptableUser.new
2518
+ user.company = company
2519
+ expect do
2520
+ user.save!
2521
+ end.not_to raise_error(Mongoid::Errors::AttributeNotLoaded)
2522
+ end
2523
+ end
2524
+
2525
+ context 'when around callbacks for embedded are disabled' do
2526
+ config_override :around_callbacks_for_embeds, false
2527
+
2528
+ context 'when around callback is defined' do
2529
+ let(:registry) { InterceptableSpec::CallbackRegistry.new }
2530
+
2531
+ let(:parent) do
2532
+ InterceptableSpec::CbEmbedsOneParent.new(registry).tap do |parent|
2533
+ parent.child = InterceptableSpec::CbEmbedsOneChild.new(registry)
2534
+ end
2535
+ end
2536
+
2537
+ before do
2538
+ expect(Mongoid.logger).to receive(:warn).with(/Around callbacks are disabled for embedded documents/).twice.and_call_original
2539
+ expect(Mongoid.logger).to receive(:warn).with(/To enable around callbacks for embedded documents/).twice.and_call_original
2540
+ end
2541
+
2542
+ it 'logs a warning' do
2543
+ parent.save!
2544
+ end
2545
+ end
2546
+ end
2547
+
2548
+ context 'when around callbacks for embedded children are enabled' do
2549
+ config_override :around_callbacks_for_embeds, true
2550
+
2551
+ context 'when around callback is defined without a yield' do
2552
+ class Mother
2553
+ include Mongoid::Document
2554
+
2555
+ embeds_many :daughters, cascade_callbacks: true
2556
+ end
2557
+
2558
+ class Daughter
2559
+ include Mongoid::Document
2560
+
2561
+ embedded_in :mother
2562
+ around_save :log_callback
2563
+
2564
+ private
2565
+
2566
+ def log_callback
2567
+ logger.debug('callback invoked')
2568
+ end
2569
+ end
2570
+
2571
+ let(:mom) { Mother.create(daughters: [ Daughter.new, Daughter.new ]) }
2572
+
2573
+ it 'raises an InvalidAroundCallback error' do
2574
+ expect do
2575
+ mom.save
2576
+ end.to raise_error(Mongoid::Errors::InvalidAroundCallback)
2577
+ end
2578
+ end
2579
+ end
1722
2580
  end