mongoid 6.4.8 → 7.0.0.beta

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 (315) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/Rakefile +0 -26
  5. data/lib/config/locales/en.yml +17 -21
  6. data/lib/mongoid.rb +2 -2
  7. data/lib/mongoid/association.rb +150 -0
  8. data/lib/mongoid/association/accessors.rb +339 -0
  9. data/lib/mongoid/{relations/binding.rb → association/bindable.rb} +32 -52
  10. data/lib/mongoid/association/builders.rb +92 -0
  11. data/lib/mongoid/{relations/constraint.rb → association/constrainable.rb} +11 -22
  12. data/lib/mongoid/association/depending.rb +116 -0
  13. data/lib/mongoid/{relations/eager.rb → association/eager_loadable.rb} +11 -11
  14. data/lib/mongoid/association/embedded.rb +4 -0
  15. data/lib/mongoid/{relations → association}/embedded/batchable.rb +27 -53
  16. data/lib/mongoid/association/embedded/cyclic.rb +109 -0
  17. data/lib/mongoid/association/embedded/embedded_in.rb +154 -0
  18. data/lib/mongoid/association/embedded/embedded_in/binding.rb +56 -0
  19. data/lib/mongoid/{relations/builders/embedded/in.rb → association/embedded/embedded_in/buildable.rb} +12 -6
  20. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +121 -0
  21. data/lib/mongoid/association/embedded/embeds_many.rb +210 -0
  22. data/lib/mongoid/{relations/bindings/embedded/many.rb → association/embedded/embeds_many/binding.rb} +11 -9
  23. data/lib/mongoid/{relations/builders/embedded/many.rb → association/embedded/embeds_many/buildable.rb} +13 -7
  24. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +529 -0
  25. data/lib/mongoid/association/embedded/embeds_one.rb +173 -0
  26. data/lib/mongoid/{relations/bindings/embedded/one.rb → association/embedded/embeds_one/binding.rb} +12 -10
  27. data/lib/mongoid/{relations/builders/embedded/one.rb → association/embedded/embeds_one/buildable.rb} +13 -7
  28. data/lib/mongoid/association/embedded/embeds_one/proxy.rb +130 -0
  29. data/lib/mongoid/association/macros.rb +204 -0
  30. data/lib/mongoid/{relations → association}/many.rb +18 -52
  31. data/lib/mongoid/{relations → association}/marshalable.rb +6 -4
  32. data/lib/mongoid/association/nested.rb +15 -0
  33. data/lib/mongoid/association/nested/many.rb +200 -0
  34. data/lib/mongoid/association/nested/nested_buildable.rb +72 -0
  35. data/lib/mongoid/association/nested/one.rb +127 -0
  36. data/lib/mongoid/{relations → association}/one.rb +6 -6
  37. data/lib/mongoid/association/options.rb +152 -0
  38. data/lib/mongoid/{relations → association}/proxy.rb +31 -58
  39. data/lib/mongoid/association/referenced.rb +7 -0
  40. data/lib/mongoid/association/referenced/auto_save.rb +79 -0
  41. data/lib/mongoid/association/referenced/belongs_to.rb +248 -0
  42. data/lib/mongoid/association/referenced/belongs_to/binding.rb +87 -0
  43. data/lib/mongoid/association/referenced/belongs_to/buildable.rb +46 -0
  44. data/lib/mongoid/association/referenced/belongs_to/eager.rb +36 -0
  45. data/lib/mongoid/association/referenced/belongs_to/proxy.rb +136 -0
  46. data/lib/mongoid/association/referenced/counter_cache.rb +163 -0
  47. data/lib/mongoid/association/referenced/eager.rb +159 -0
  48. data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +290 -0
  49. data/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb +71 -0
  50. data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +40 -0
  51. data/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb +52 -0
  52. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +310 -0
  53. data/lib/mongoid/association/referenced/has_many.rb +273 -0
  54. data/lib/mongoid/{relations/bindings/referenced/many.rb → association/referenced/has_many/binding.rb} +6 -5
  55. data/lib/mongoid/association/referenced/has_many/buildable.rb +38 -0
  56. data/lib/mongoid/association/referenced/has_many/eager.rb +43 -0
  57. data/lib/mongoid/association/referenced/has_many/enumerable.rb +479 -0
  58. data/lib/mongoid/association/referenced/has_many/proxy.rb +577 -0
  59. data/lib/mongoid/association/referenced/has_one.rb +204 -0
  60. data/lib/mongoid/{relations/bindings/referenced/one.rb → association/referenced/has_one/binding.rb} +11 -8
  61. data/lib/mongoid/association/referenced/has_one/buildable.rb +60 -0
  62. data/lib/mongoid/association/referenced/has_one/eager.rb +35 -0
  63. data/lib/mongoid/{relations/builders/nested_attributes/one.rb → association/referenced/has_one/nested_builder.rb} +9 -9
  64. data/lib/mongoid/association/referenced/has_one/proxy.rb +113 -0
  65. data/lib/mongoid/association/referenced/syncable.rb +170 -0
  66. data/lib/mongoid/{relations → association}/reflections.rb +21 -17
  67. data/lib/mongoid/association/relatable.rb +415 -0
  68. data/lib/mongoid/association/touchable.rb +97 -0
  69. data/lib/mongoid/atomic.rb +6 -6
  70. data/lib/mongoid/atomic/modifiers.rb +8 -12
  71. data/lib/mongoid/atomic/paths/embedded/many.rb +1 -1
  72. data/lib/mongoid/atomic/paths/embedded/one.rb +1 -1
  73. data/lib/mongoid/attributes.rb +2 -1
  74. data/lib/mongoid/attributes/nested.rb +10 -10
  75. data/lib/mongoid/attributes/processing.rb +2 -2
  76. data/lib/mongoid/attributes/readonly.rb +2 -4
  77. data/lib/mongoid/clients.rb +0 -2
  78. data/lib/mongoid/clients/options.rb +1 -1
  79. data/lib/mongoid/clients/storage_options.rb +0 -1
  80. data/lib/mongoid/composable.rb +3 -4
  81. data/lib/mongoid/config.rb +1 -0
  82. data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
  83. data/lib/mongoid/contextual/atomic.rb +3 -6
  84. data/lib/mongoid/contextual/map_reduce.rb +3 -7
  85. data/lib/mongoid/contextual/memory.rb +5 -10
  86. data/lib/mongoid/contextual/mongo.rb +10 -27
  87. data/lib/mongoid/copyable.rb +6 -6
  88. data/lib/mongoid/criteria.rb +1 -2
  89. data/lib/mongoid/criteria/includable.rb +14 -14
  90. data/lib/mongoid/criteria/modifiable.rb +8 -14
  91. data/lib/mongoid/criteria/queryable/mergeable.rb +1 -3
  92. data/lib/mongoid/criteria/queryable/pipeline.rb +10 -5
  93. data/lib/mongoid/criteria/queryable/selectable.rb +10 -34
  94. data/lib/mongoid/document.rb +6 -6
  95. data/lib/mongoid/errors.rb +3 -1
  96. data/lib/mongoid/errors/invalid_dependent_strategy.rb +32 -0
  97. data/lib/mongoid/errors/invalid_relation_option.rb +29 -0
  98. data/lib/mongoid/errors/unknown_model.rb +25 -0
  99. data/lib/mongoid/extensions/array.rb +5 -5
  100. data/lib/mongoid/extensions/big_decimal.rb +1 -1
  101. data/lib/mongoid/extensions/object.rb +4 -4
  102. data/lib/mongoid/extensions/range.rb +1 -0
  103. data/lib/mongoid/extensions/regexp.rb +0 -1
  104. data/lib/mongoid/extensions/string.rb +1 -3
  105. data/lib/mongoid/factory.rb +4 -3
  106. data/lib/mongoid/fields.rb +1 -1
  107. data/lib/mongoid/fields/foreign_key.rb +5 -5
  108. data/lib/mongoid/fields/standard.rb +2 -14
  109. data/lib/mongoid/fields/validators/macro.rb +1 -1
  110. data/lib/mongoid/indexable.rb +8 -5
  111. data/lib/mongoid/interceptable.rb +5 -5
  112. data/lib/mongoid/matchable.rb +0 -3
  113. data/lib/mongoid/persistable.rb +4 -5
  114. data/lib/mongoid/persistable/creatable.rb +2 -4
  115. data/lib/mongoid/persistable/deletable.rb +9 -10
  116. data/lib/mongoid/persistable/destroyable.rb +5 -1
  117. data/lib/mongoid/persistable/incrementable.rb +1 -1
  118. data/lib/mongoid/persistable/logical.rb +1 -1
  119. data/lib/mongoid/persistable/settable.rb +5 -5
  120. data/lib/mongoid/persistable/updatable.rb +2 -2
  121. data/lib/mongoid/persistable/upsertable.rb +1 -2
  122. data/lib/mongoid/persistence_context.rb +4 -9
  123. data/lib/mongoid/query_cache.rb +18 -65
  124. data/lib/mongoid/railtie.rb +0 -17
  125. data/lib/mongoid/reloadable.rb +1 -1
  126. data/lib/mongoid/scopable.rb +3 -3
  127. data/lib/mongoid/serializable.rb +3 -3
  128. data/lib/mongoid/tasks/database.rb +2 -3
  129. data/lib/mongoid/threaded.rb +0 -74
  130. data/lib/mongoid/traversable.rb +2 -2
  131. data/lib/mongoid/validatable.rb +8 -8
  132. data/lib/mongoid/validatable/presence.rb +2 -2
  133. data/lib/mongoid/validatable/uniqueness.rb +4 -4
  134. data/lib/mongoid/version.rb +1 -1
  135. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +3 -4
  136. data/spec/app/models/animal.rb +2 -1
  137. data/spec/app/models/band.rb +0 -1
  138. data/spec/app/models/bomb.rb +1 -1
  139. data/spec/app/models/message.rb +1 -1
  140. data/spec/app/models/person.rb +5 -2
  141. data/spec/app/models/vertex.rb +6 -0
  142. data/spec/app/models/wiki_page.rb +1 -1
  143. data/spec/config/mongoid.yml +1 -0
  144. data/spec/mongoid/{relations → association}/accessors_spec.rb +1 -1
  145. data/spec/mongoid/{relations → association}/auto_save_spec.rb +60 -12
  146. data/spec/mongoid/{relations → association}/builders_spec.rb +1 -1
  147. data/spec/mongoid/association/constrainable_spec.rb +115 -0
  148. data/spec/mongoid/{relations → association}/counter_cache_spec.rb +1 -1
  149. data/spec/mongoid/association/depending_spec.rb +613 -0
  150. data/spec/mongoid/{relations → association}/eager_spec.rb +12 -12
  151. data/spec/mongoid/{relations → association/embedded}/cyclic_spec.rb +1 -1
  152. data/spec/mongoid/{relations → association}/embedded/dirty_spec.rb +0 -0
  153. data/spec/mongoid/{relations/bindings/embedded/in_spec.rb → association/embedded/embedded_in/binding_spec.rb} +13 -13
  154. data/spec/mongoid/{relations/builders/embedded/in_spec.rb → association/embedded/embedded_in/buildable_spec.rb} +9 -9
  155. data/spec/mongoid/{relations/embedded/in_spec.rb → association/embedded/embedded_in/proxy_spec.rb} +5 -77
  156. data/spec/mongoid/association/embedded/embedded_in_spec.rb +843 -0
  157. data/spec/mongoid/{relations/bindings/embedded/many_spec.rb → association/embedded/embeds_many/binding_spec.rb} +3 -3
  158. data/spec/mongoid/{relations/builders/embedded/many_spec.rb → association/embedded/embeds_many/buildable_spec.rb} +17 -45
  159. data/spec/mongoid/{relations/embedded/many_spec.rb → association/embedded/embeds_many/proxy_spec.rb} +140 -428
  160. data/spec/mongoid/association/embedded/embeds_many_spec.rb +852 -0
  161. data/spec/mongoid/{relations/bindings/embedded/one_spec.rb → association/embedded/embeds_one/binding_spec.rb} +4 -4
  162. data/spec/mongoid/{relations/builders/embedded/one_spec.rb → association/embedded/embeds_one/buildable_spec.rb} +14 -34
  163. data/spec/mongoid/{relations/embedded/one_spec.rb → association/embedded/embeds_one/proxy_spec.rb} +39 -84
  164. data/spec/mongoid/association/embedded/embeds_one_spec.rb +908 -0
  165. data/spec/mongoid/{relations → association}/macros_spec.rb +148 -93
  166. data/spec/mongoid/{relations/builders/nested_attributes → association/nested}/many_spec.rb +16 -19
  167. data/spec/mongoid/{relations/builders/nested_attributes → association/nested}/one_spec.rb +17 -20
  168. data/spec/mongoid/association/options_spec.rb +1321 -0
  169. data/spec/mongoid/{relations → association}/polymorphic_spec.rb +7 -34
  170. data/spec/mongoid/{relations/bindings/referenced/in_spec.rb → association/referenced/belongs_to/binding_spec.rb} +7 -7
  171. data/spec/mongoid/{relations/builders/referenced/in_spec.rb → association/referenced/belongs_to/buildable_spec.rb} +46 -79
  172. data/spec/mongoid/{relations/eager/belongs_to_spec.rb → association/referenced/belongs_to/eager_spec.rb} +9 -9
  173. data/spec/mongoid/{relations/referenced/in_spec.rb → association/referenced/belongs_to/proxy_spec.rb} +57 -91
  174. data/spec/mongoid/association/referenced/belongs_to_spec.rb +1963 -0
  175. data/spec/mongoid/{relations/bindings/referenced/many_to_many_spec.rb → association/referenced/has_and_belongs_to_many/binding_spec.rb} +5 -5
  176. data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +121 -0
  177. data/spec/mongoid/{relations/eager/has_and_belongs_to_many_spec.rb → association/referenced/has_and_belongs_to_many/eager_spec.rb} +5 -5
  178. data/spec/mongoid/{relations/referenced/many_to_many_spec.rb → association/referenced/has_and_belongs_to_many/proxy_spec.rb} +107 -98
  179. data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +1027 -0
  180. data/spec/mongoid/{relations/bindings/referenced/many_spec.rb → association/referenced/has_many/binding_spec.rb} +5 -5
  181. data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +119 -0
  182. data/spec/mongoid/{relations/eager/has_many_spec.rb → association/referenced/has_many/eager_spec.rb} +11 -11
  183. data/spec/mongoid/{relations/targets → association/referenced/has_many}/enumerable_spec.rb +1 -109
  184. data/spec/mongoid/{relations/referenced/many_spec.rb → association/referenced/has_many/proxy_spec.rb} +28 -93
  185. data/spec/mongoid/association/referenced/has_many_spec.rb +1225 -0
  186. data/spec/mongoid/{relations/bindings/referenced/one_spec.rb → association/referenced/has_one/binding_spec.rb} +4 -4
  187. data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +113 -0
  188. data/spec/mongoid/{relations/eager/has_one_spec.rb → association/referenced/has_one/eager_spec.rb} +10 -10
  189. data/spec/mongoid/{relations/referenced/one_spec.rb → association/referenced/has_one/proxy_spec.rb} +9 -109
  190. data/spec/mongoid/association/referenced/has_one_spec.rb +1244 -0
  191. data/spec/mongoid/{relations → association}/reflections_spec.rb +1 -12
  192. data/spec/mongoid/{relations/synchronization_spec.rb → association/syncable_spec.rb} +4 -2
  193. data/spec/mongoid/{relations → association}/touchable_spec.rb +19 -1
  194. data/spec/mongoid/{relations_spec.rb → association_spec.rb} +1 -1
  195. data/spec/mongoid/atomic/modifiers_spec.rb +17 -17
  196. data/spec/mongoid/atomic_spec.rb +17 -17
  197. data/spec/mongoid/attributes/nested_spec.rb +14 -12
  198. data/spec/mongoid/attributes/readonly_spec.rb +80 -125
  199. data/spec/mongoid/clients/factory_spec.rb +28 -52
  200. data/spec/mongoid/clients/options_spec.rb +65 -69
  201. data/spec/mongoid/config_spec.rb +24 -0
  202. data/spec/mongoid/contextual/geo_near_spec.rb +0 -1
  203. data/spec/mongoid/contextual/mongo_spec.rb +4 -112
  204. data/spec/mongoid/criteria/modifiable_spec.rb +183 -60
  205. data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +3 -3
  206. data/spec/mongoid/criteria/queryable/pipeline_spec.rb +12 -0
  207. data/spec/mongoid/criteria/queryable/selectable_spec.rb +6 -74
  208. data/spec/mongoid/criteria/queryable/selector_spec.rb +2 -2
  209. data/spec/mongoid/criteria/scopable_spec.rb +0 -81
  210. data/spec/mongoid/criteria_spec.rb +16 -19
  211. data/spec/mongoid/document_spec.rb +2 -56
  212. data/spec/mongoid/extensions/array_spec.rb +11 -15
  213. data/spec/mongoid/extensions/big_decimal_spec.rb +9 -9
  214. data/spec/mongoid/extensions/object_spec.rb +7 -11
  215. data/spec/mongoid/extensions/range_spec.rb +7 -0
  216. data/spec/mongoid/extensions/regexp_spec.rb +0 -23
  217. data/spec/mongoid/extensions/string_spec.rb +7 -35
  218. data/spec/mongoid/factory_spec.rb +18 -11
  219. data/spec/mongoid/fields/foreign_key_spec.rb +24 -32
  220. data/spec/mongoid/fields_spec.rb +2 -2
  221. data/spec/mongoid/findable_spec.rb +1 -1
  222. data/spec/mongoid/indexable_spec.rb +18 -8
  223. data/spec/mongoid/interceptable_spec.rb +21 -2
  224. data/spec/mongoid/matchable_spec.rb +1 -26
  225. data/spec/mongoid/persistable/deletable_spec.rb +0 -19
  226. data/spec/mongoid/persistable/destroyable_spec.rb +0 -19
  227. data/spec/mongoid/persistable/incrementable_spec.rb +6 -6
  228. data/spec/mongoid/persistable/savable_spec.rb +3 -3
  229. data/spec/mongoid/persistable/settable_spec.rb +1 -35
  230. data/spec/mongoid/persistable/updatable_spec.rb +2 -2
  231. data/spec/mongoid/persistable_spec.rb +16 -16
  232. data/spec/mongoid/persistence_context_spec.rb +0 -14
  233. data/spec/mongoid/positional_spec.rb +10 -10
  234. data/spec/mongoid/query_cache_spec.rb +18 -87
  235. data/spec/mongoid/relations/proxy_spec.rb +124 -124
  236. data/spec/mongoid/scopable_spec.rb +0 -13
  237. data/spec/mongoid/threaded_spec.rb +0 -68
  238. data/spec/mongoid/validatable/associated_spec.rb +1 -1
  239. data/spec/mongoid/validatable/presence_spec.rb +7 -6
  240. data/spec/mongoid/validatable_spec.rb +1 -1
  241. data/spec/spec_helper.rb +7 -83
  242. metadata +586 -611
  243. metadata.gz.sig +2 -5
  244. data/lib/mongoid/clients/sessions.rb +0 -113
  245. data/lib/mongoid/errors/invalid_session_use.rb +0 -24
  246. data/lib/mongoid/matchable/nor.rb +0 -37
  247. data/lib/mongoid/railties/controller_runtime.rb +0 -86
  248. data/lib/mongoid/relations.rb +0 -148
  249. data/lib/mongoid/relations/accessors.rb +0 -267
  250. data/lib/mongoid/relations/auto_save.rb +0 -94
  251. data/lib/mongoid/relations/bindings.rb +0 -9
  252. data/lib/mongoid/relations/bindings/embedded/in.rb +0 -59
  253. data/lib/mongoid/relations/bindings/referenced/in.rb +0 -65
  254. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +0 -70
  255. data/lib/mongoid/relations/builder.rb +0 -57
  256. data/lib/mongoid/relations/builders.rb +0 -106
  257. data/lib/mongoid/relations/builders/nested_attributes/many.rb +0 -199
  258. data/lib/mongoid/relations/builders/referenced/in.rb +0 -26
  259. data/lib/mongoid/relations/builders/referenced/many.rb +0 -26
  260. data/lib/mongoid/relations/builders/referenced/many_to_many.rb +0 -39
  261. data/lib/mongoid/relations/builders/referenced/one.rb +0 -26
  262. data/lib/mongoid/relations/cascading.rb +0 -56
  263. data/lib/mongoid/relations/cascading/delete.rb +0 -44
  264. data/lib/mongoid/relations/cascading/destroy.rb +0 -43
  265. data/lib/mongoid/relations/cascading/nullify.rb +0 -35
  266. data/lib/mongoid/relations/cascading/restrict.rb +0 -39
  267. data/lib/mongoid/relations/conversions.rb +0 -34
  268. data/lib/mongoid/relations/counter_cache.rb +0 -160
  269. data/lib/mongoid/relations/cyclic.rb +0 -107
  270. data/lib/mongoid/relations/eager/base.rb +0 -153
  271. data/lib/mongoid/relations/eager/belongs_to.rb +0 -31
  272. data/lib/mongoid/relations/eager/has_and_belongs_to_many.rb +0 -47
  273. data/lib/mongoid/relations/eager/has_many.rb +0 -38
  274. data/lib/mongoid/relations/eager/has_one.rb +0 -30
  275. data/lib/mongoid/relations/embedded/in.rb +0 -241
  276. data/lib/mongoid/relations/embedded/many.rb +0 -683
  277. data/lib/mongoid/relations/embedded/one.rb +0 -235
  278. data/lib/mongoid/relations/macros.rb +0 -367
  279. data/lib/mongoid/relations/metadata.rb +0 -1179
  280. data/lib/mongoid/relations/nested_builder.rb +0 -74
  281. data/lib/mongoid/relations/options.rb +0 -49
  282. data/lib/mongoid/relations/polymorphic.rb +0 -39
  283. data/lib/mongoid/relations/referenced/in.rb +0 -304
  284. data/lib/mongoid/relations/referenced/many.rb +0 -812
  285. data/lib/mongoid/relations/referenced/many_to_many.rb +0 -479
  286. data/lib/mongoid/relations/referenced/one.rb +0 -290
  287. data/lib/mongoid/relations/synchronization.rb +0 -169
  288. data/lib/mongoid/relations/targets.rb +0 -2
  289. data/lib/mongoid/relations/targets/enumerable.rb +0 -493
  290. data/lib/mongoid/relations/touchable.rb +0 -97
  291. data/spec/app/models/array_field.rb +0 -7
  292. data/spec/app/models/delegating_patient.rb +0 -16
  293. data/spec/integration/document_spec.rb +0 -22
  294. data/spec/mongoid/clients/sessions_spec.rb +0 -334
  295. data/spec/mongoid/fields/internal/foreign_keys/array_spec.rb +0 -184
  296. data/spec/mongoid/fields/internal/foreign_keys/object_spec.rb +0 -201
  297. data/spec/mongoid/matchable/nor_spec.rb +0 -209
  298. data/spec/mongoid/relations/builders/referenced/many_spec.rb +0 -137
  299. data/spec/mongoid/relations/builders/referenced/many_to_many_spec.rb +0 -178
  300. data/spec/mongoid/relations/builders/referenced/one_spec.rb +0 -111
  301. data/spec/mongoid/relations/cascading/delete_spec.rb +0 -101
  302. data/spec/mongoid/relations/cascading/destroy_spec.rb +0 -47
  303. data/spec/mongoid/relations/cascading/nullify_spec.rb +0 -32
  304. data/spec/mongoid/relations/cascading/restrict_spec.rb +0 -68
  305. data/spec/mongoid/relations/cascading_spec.rb +0 -355
  306. data/spec/mongoid/relations/constraint_spec.rb +0 -75
  307. data/spec/mongoid/relations/conversions_spec.rb +0 -128
  308. data/spec/mongoid/relations/metadata_spec.rb +0 -1985
  309. data/spec/mongoid/relations/options_spec.rb +0 -35
  310. data/spec/rails/controller_extension/controller_runtime_spec.rb +0 -110
  311. data/spec/support/cluster_config.rb +0 -158
  312. data/spec/support/constraints.rb +0 -101
  313. data/spec/support/macros.rb +0 -20
  314. data/spec/support/session_registry.rb +0 -50
  315. data/spec/support/spec_config.rb +0 -42
@@ -62,8 +62,8 @@ module Mongoid
62
62
  # @since 3.0.0
63
63
  def relation_or_fk_missing?(doc, attr, value)
64
64
  return true if value.blank? && doc.send(attr).blank?
65
- metadata = doc.relations[attr.to_s]
66
- metadata.stores_foreign_key? && doc.send(metadata.foreign_key).blank?
65
+ association = doc.relations[attr.to_s]
66
+ association.stores_foreign_key? && doc.send(association.foreign_key).blank?
67
67
  end
68
68
 
69
69
  # For guarding against false values.
@@ -224,9 +224,9 @@ module Mongoid
224
224
  #
225
225
  # @since 2.4.4
226
226
  def to_validate(document, attribute, value)
227
- metadata = document.relations[attribute.to_s]
228
- if metadata && metadata.stores_foreign_key?
229
- [ metadata.foreign_key, value && value._id ]
227
+ association = document.relations[attribute.to_s]
228
+ if association && association.stores_foreign_key?
229
+ [ association.foreign_key, value && value._id ]
230
230
  else
231
231
  [ attribute, value ]
232
232
  end
@@ -246,7 +246,7 @@ module Mongoid
246
246
  # @since 2.4.10
247
247
  def validate_embedded(document, attribute, value)
248
248
  return if skip_validation?(document)
249
- relation = document._parent.send(document.metadata_name)
249
+ relation = document._parent.send(document.association_name)
250
250
  criteria = create_criteria(relation, document, attribute, value)
251
251
  criteria = criteria.merge(options[:conditions].call) if options[:conditions]
252
252
  add_error(document, attribute, value) if criteria.count > 1
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid
3
- VERSION = "6.4.8"
3
+ VERSION = "7.0.0.beta"
4
4
  end
@@ -11,10 +11,6 @@ development:
11
11
  hosts:
12
12
  - localhost:27017
13
13
  options:
14
- # Note that all options listed below are Ruby driver client options (the mongo gem).
15
- # Please refer to the driver documentation of the version of the mongo gem you are using
16
- # for the most up-to-date list of options.
17
- #
18
14
  # Change the default write concern. (default = { w: 1 })
19
15
  # write:
20
16
  # w: 1
@@ -149,6 +145,9 @@ development:
149
145
  # Application name that is printed to the mongodb logs upon establishing a
150
146
  # connection in server versions >= 3.4. Note that the name cannot exceed 128 bytes.
151
147
  # app_name: MyApplicationName
148
+
149
+ # Use background indexes by default if `background` option not specified. (default: false)
150
+ # background_indexing: false
152
151
  test:
153
152
  clients:
154
153
  default:
@@ -9,7 +9,8 @@ class Animal
9
9
  field :tags, type: Array
10
10
 
11
11
  embedded_in :person
12
- embedded_in :circus
12
+ embedded_in :circus, class_name: 'Circus' # class_name is necessary because ActiveRecord think the singular of Circus
13
+ # is Circu
13
14
 
14
15
  validates_format_of :name, without: /\$\$\$/
15
16
 
@@ -20,7 +20,6 @@ class Band
20
20
 
21
21
  embeds_many :records, cascade_callbacks: true
22
22
  embeds_many :notes, as: :noteable, cascade_callbacks: true, validate: false
23
- embeds_many :labels
24
23
  embeds_one :label, cascade_callbacks: true
25
24
 
26
25
  has_many :same_name, class_name: 'Agent', inverse_of: :same_name
@@ -1,4 +1,4 @@
1
1
  class Bomb
2
2
  include Mongoid::Document
3
- has_one :explosion, dependent: :delete, autobuild: true
3
+ has_one :explosion, dependent: :delete_all, autobuild: true
4
4
  end
@@ -5,7 +5,7 @@ class Message
5
5
  field :priority, type: Integer
6
6
 
7
7
  embedded_in :person
8
- has_and_belongs_to_many :receviers, class_name: "Person", inverse_of: nil
8
+ has_and_belongs_to_many :receivers, class_name: "Person", inverse_of: nil
9
9
 
10
10
  has_one :post, as: :posteable
11
11
  end
@@ -85,7 +85,7 @@ class Person
85
85
 
86
86
  has_many \
87
87
  :posts,
88
- dependent: :delete,
88
+ dependent: :delete_all,
89
89
  validate: false do
90
90
  def extension
91
91
  "Testing"
@@ -105,7 +105,7 @@ class Person
105
105
  has_one :account, validate: false
106
106
  has_one :cat, dependent: :nullify, validate: false, primary_key: :username
107
107
  has_one :book, autobuild: true, validate: false
108
- has_one :home, dependent: :delete, validate: false
108
+ has_one :home, dependent: :delete_all, validate: false
109
109
 
110
110
  has_and_belongs_to_many \
111
111
  :administrated_events,
@@ -114,6 +114,9 @@ class Person
114
114
  dependent: :nullify,
115
115
  validate: false
116
116
 
117
+ belongs_to :mother, class_name: 'Person'
118
+ has_many :children, class_name: 'Person'
119
+
117
120
  accepts_nested_attributes_for :addresses
118
121
  accepts_nested_attributes_for :name, update_only: true
119
122
  accepts_nested_attributes_for :pet, allow_destroy: true
@@ -0,0 +1,6 @@
1
+ class Vertex
2
+ include Mongoid::Document
3
+
4
+ has_and_belongs_to_many :parents, inverse_of: :children, class_name: 'Vertex'
5
+ has_and_belongs_to_many :children, inverse_of: :parents, class_name: 'Vertex'
6
+ end
@@ -9,6 +9,6 @@ class WikiPage
9
9
 
10
10
  embeds_many :edits, validate: false
11
11
  has_many :comments, dependent: :destroy, validate: false
12
- has_many :child_pages, class_name: "WikiPage", dependent: :delete, inverse_of: :parent_pages
12
+ has_many :child_pages, class_name: "WikiPage", dependent: :delete_all, inverse_of: :parent_pages
13
13
  belongs_to :parent_pages, class_name: "WikiPage", inverse_of: :child_pages
14
14
  end
@@ -32,6 +32,7 @@ test:
32
32
  log_level: :warn
33
33
  belongs_to_required_by_default: false
34
34
  app_name: 'testing'
35
+ background_indexing: false
35
36
  test_with_max_staleness:
36
37
  clients:
37
38
  default:
@@ -1,6 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Mongoid::Relations::Accessors do
3
+ describe Mongoid::Association::Accessors do
4
4
 
5
5
  describe "\#{getter}?" do
6
6
 
@@ -1,11 +1,12 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Mongoid::Relations::AutoSave do
3
+ describe Mongoid::Association::Referenced::AutoSave do
4
4
 
5
5
  describe ".auto_save" do
6
6
 
7
7
  before(:all) do
8
- Person.autosave(Person.relations["account"].merge!(autosave: true))
8
+ Person.has_many :drugs, validate: false, autosave: true
9
+ Person.has_one :account, validate: false, autosave: true
9
10
  end
10
11
 
11
12
  after(:all) do
@@ -42,13 +43,8 @@ describe Mongoid::Relations::AutoSave do
42
43
 
43
44
  context "when the relation has already had the autosave callback added" do
44
45
 
45
- let(:metadata) do
46
- Person.relations["drugs"].merge!(autosave: true)
47
- end
48
-
49
46
  before do
50
- Person.autosave(metadata)
51
- Person.autosave(metadata)
47
+ Person.has_many :drugs, validate: false, autosave: true
52
48
  end
53
49
 
54
50
  let(:drug) do
@@ -71,6 +67,11 @@ describe Mongoid::Relations::AutoSave do
71
67
  context "when saving a new parent document" do
72
68
 
73
69
  context 'when persistence options are not set on the parent' do
70
+
71
+ before do
72
+ Person.has_many :drugs, validate: false, autosave: true
73
+ end
74
+
74
75
  before do
75
76
  person.drugs << drug
76
77
  person.save
@@ -254,10 +255,6 @@ describe Mongoid::Relations::AutoSave do
254
255
 
255
256
  context "when it has two relations with autosaves" do
256
257
 
257
- before do
258
- Person.autosave(Person.relations["drugs"].merge!(autosave: true))
259
- end
260
-
261
258
  let!(:person) do
262
259
  Person.create(drugs: [percocet], account: account)
263
260
  end
@@ -309,6 +306,57 @@ describe Mongoid::Relations::AutoSave do
309
306
  end
310
307
  end
311
308
  end
309
+
310
+ context 'when the autosave should be cascaded' do
311
+
312
+ before do
313
+ class King
314
+ include Mongoid::Document
315
+ has_one :peasant, autosave: true
316
+ end
317
+
318
+ class Peasant
319
+ include Mongoid::Document
320
+ belongs_to :king
321
+ has_one :harvest, autosave: true
322
+ end
323
+
324
+ class Harvest
325
+ include Mongoid::Document
326
+ field :season, type: String
327
+ belongs_to :peasant
328
+ end
329
+ end
330
+
331
+ after do
332
+ Object.send(:remove_const, :King)
333
+ Object.send(:remove_const, :Peasant)
334
+ Object.send(:remove_const, :Harvest)
335
+ end
336
+
337
+ let(:king) do
338
+ King.create
339
+ end
340
+
341
+ let(:peasant) do
342
+ Peasant.create
343
+ end
344
+
345
+ let(:harvest) do
346
+ Harvest.create(season: 'Summer')
347
+ end
348
+
349
+ before do
350
+ peasant.harvest = harvest
351
+ king.peasant = peasant
352
+ harvest.season = 'Fall'
353
+ king.save
354
+ end
355
+
356
+ it 'cascades the save' do
357
+ expect(harvest.reload.season).to eq('Fall')
358
+ end
359
+ end
312
360
  end
313
361
  end
314
362
  end
@@ -1,6 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Mongoid::Relations::Builders do
3
+ describe Mongoid::Association::Builders do
4
4
 
5
5
  describe "#build_#\{name}" do
6
6
 
@@ -0,0 +1,115 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Association::Constrainable do
4
+
5
+ describe "#convert_to_foreign_key" do
6
+
7
+ context "when the id's class stores object ids" do
8
+
9
+ before(:all) do
10
+ Person.field(
11
+ :_id,
12
+ type: BSON::ObjectId,
13
+ pre_processed: true,
14
+ default: ->{ BSON::ObjectId.new },
15
+ overwrite: true
16
+ )
17
+ end
18
+
19
+ let(:constrainable) do
20
+ Post.belongs_to :person
21
+ end
22
+
23
+ context "when provided an object id" do
24
+
25
+ let(:object) do
26
+ BSON::ObjectId.new
27
+ end
28
+
29
+ it "returns the object id" do
30
+ expect(constrainable.convert_to_foreign_key(object)).to eq(object)
31
+ end
32
+ end
33
+
34
+ context "when provided a string" do
35
+
36
+ let(:object) do
37
+ BSON::ObjectId.new
38
+ end
39
+
40
+ it "returns an object id from the string" do
41
+ expect(constrainable.convert_to_foreign_key(object.to_s)).to eq(object)
42
+ end
43
+ end
44
+ end
45
+
46
+ context "when the id's class does not store object ids" do
47
+
48
+ let(:constrainable) do
49
+ Alert.belongs_to :account
50
+ end
51
+
52
+ it "returns the object" do
53
+ expect(constrainable.convert_to_foreign_key("testing")).to eq("testing")
54
+ end
55
+ end
56
+
57
+ context 'when the association is polymorphic' do
58
+
59
+ let(:constrainable) do
60
+ Post.relations['posteable']
61
+ end
62
+
63
+ let(:result) do
64
+ constrainable.convert_to_foreign_key(object)
65
+ end
66
+
67
+ context 'when a BSON::ObjectId is passed' do
68
+
69
+ let(:object) do
70
+ BSON::ObjectId.new
71
+ end
72
+
73
+ it 'returns the object id' do
74
+ expect(result).to eq(object)
75
+ end
76
+ end
77
+
78
+ context 'when a string is passed' do
79
+
80
+ context 'when the string represents an ObjectId' do
81
+
82
+ let(:object) do
83
+ BSON::ObjectId.new.to_s
84
+ end
85
+
86
+ it 'returns the object id' do
87
+ expect(result).to eq(BSON::ObjectId.from_string(object))
88
+ end
89
+ end
90
+
91
+ context 'when the string does not represent an ObjectId' do
92
+
93
+ let(:object) do
94
+ 'some-other-string'
95
+ end
96
+
97
+ it 'returns the object' do
98
+ expect(result).to eq(object)
99
+ end
100
+ end
101
+ end
102
+
103
+ context 'when a model object is passed' do
104
+
105
+ let(:object) do
106
+ Post.new
107
+ end
108
+
109
+ it 'returns the id' do
110
+ expect(result).to eq(object.id)
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
@@ -1,6 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Mongoid::Relations::CounterCache do
3
+ describe Mongoid::Association::Referenced::CounterCache do
4
4
 
5
5
  describe "#reset_counters" do
6
6
 
@@ -0,0 +1,613 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Association::Depending do
4
+
5
+ around(:each) do |example|
6
+ relations_before = Person.relations
7
+ example.run
8
+ Person.relations = relations_before
9
+ end
10
+
11
+ describe "#apply_delete_dependencies!" do
12
+
13
+ let(:band) do
14
+ Band.new
15
+ end
16
+
17
+ context "when the association exists in the list of dependencies" do
18
+
19
+ context "when the association has no dependent strategy" do
20
+
21
+ before do
22
+ band.dependents.push(Band.relations["records"])
23
+ end
24
+
25
+ after do
26
+ band.dependents.delete(Band.relations["records"])
27
+ end
28
+
29
+ it "ignores the dependency" do
30
+ expect(band.apply_delete_dependencies!).to eq([Band.relations["records"]])
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ describe ".define_dependency!" do
37
+
38
+ let(:klass) do
39
+ Class.new.tap { |c| c.send(:include, Mongoid::Document) }
40
+ end
41
+
42
+ context "when the association metadata doesnt exist" do
43
+
44
+ before do
45
+ klass.dependents.push("nothing")
46
+ end
47
+
48
+ it "does not raise an error" do
49
+ expect {
50
+ klass.new.apply_delete_dependencies!
51
+ }.not_to raise_error
52
+ end
53
+ end
54
+
55
+ context "when a dependent option is provided" do
56
+
57
+ let!(:association) do
58
+ klass.has_many :posts, dependent: :destroy
59
+ end
60
+
61
+ after do
62
+ klass.relations.delete(association.name.to_s)
63
+ end
64
+
65
+ it "adds the relation to the dependents" do
66
+ expect(klass.dependents).to include(klass.relations["posts"])
67
+ end
68
+ end
69
+
70
+ context "when no dependent option is provided" do
71
+
72
+ let!(:association) do
73
+ klass.has_many :posts
74
+ end
75
+
76
+ after do
77
+ klass.relations.delete(association.name.to_s)
78
+ end
79
+
80
+ it "does not add a relation to the dependents" do
81
+ expect(klass.dependents).to_not include(association)
82
+ end
83
+ end
84
+
85
+ context 'when the class is defined more than once' do
86
+
87
+ let!(:association) do
88
+ klass.has_many :posts, dependent: :destroy
89
+ klass.has_many :posts, dependent: :destroy
90
+ end
91
+
92
+ it 'only creates the dependency once' do
93
+ expect(klass.dependents.size).to eq(1)
94
+ end
95
+ end
96
+ end
97
+
98
+ [:delete, :destroy].each do |method|
99
+
100
+ describe "##{method}" do
101
+
102
+ context "when cascading removals" do
103
+
104
+ context "when strategy is delete" do
105
+
106
+ let(:person) do
107
+ Person.create
108
+ end
109
+
110
+ let!(:post) do
111
+ person.posts.create(title: "Testing")
112
+ end
113
+
114
+ before do
115
+ person.send(method)
116
+ end
117
+
118
+ it "deletes the associated documents" do
119
+ expect {
120
+ Post.find(post.id)
121
+ }.to raise_error(Mongoid::Errors::DocumentNotFound)
122
+ end
123
+ end
124
+
125
+ context "when strategy is destroy" do
126
+
127
+ let(:person) do
128
+ Person.create
129
+ end
130
+
131
+ let!(:game) do
132
+ person.create_game(name: "Pong")
133
+ end
134
+
135
+ before do
136
+ person.send(method)
137
+ end
138
+
139
+ it "destroys the associated documents" do
140
+ expect {
141
+ Game.find(game.id)
142
+ }.to raise_error(Mongoid::Errors::DocumentNotFound)
143
+ end
144
+ end
145
+
146
+ context "when strategy is nullify" do
147
+
148
+ context "when nullifying a references many" do
149
+
150
+ let(:movie) do
151
+ Movie.create(title: "Bladerunner")
152
+ end
153
+
154
+ let!(:rating) do
155
+ movie.ratings.create(value: 10)
156
+ end
157
+
158
+ let(:from_db) do
159
+ Rating.find(rating.id)
160
+ end
161
+
162
+ before do
163
+ movie.send(method)
164
+ end
165
+
166
+ it "removes the references to the removed document" do
167
+ expect(from_db.ratable_id).to be_nil
168
+ end
169
+ end
170
+
171
+ context "when nullifying a references one" do
172
+
173
+ context "when the relation exists" do
174
+
175
+ let(:book) do
176
+ Book.create(title: "Neuromancer")
177
+ end
178
+
179
+ let!(:rating) do
180
+ book.create_rating(value: 10)
181
+ end
182
+
183
+ let(:from_db) do
184
+ Rating.find(rating.id)
185
+ end
186
+
187
+ before do
188
+ book.send(method)
189
+ end
190
+
191
+ it "removes the references to the removed document" do
192
+ expect(from_db.ratable_id).to be_nil
193
+ end
194
+ end
195
+
196
+ context "when the relation is nil" do
197
+
198
+ let(:book) do
199
+ Book.create(title: "Neuromancer")
200
+ end
201
+
202
+ it "returns nil" do
203
+ expect(book.send(method)).to be true
204
+ end
205
+ end
206
+ end
207
+
208
+ context "when nullifying a many to many" do
209
+
210
+ let(:person) do
211
+ Person.create
212
+ end
213
+
214
+ let!(:preference) do
215
+ person.preferences.create(name: "Setting")
216
+ end
217
+
218
+ let(:from_db) do
219
+ Preference.find(preference.id)
220
+ end
221
+
222
+ before do
223
+ person.send(method)
224
+ end
225
+
226
+ it "removes the references from the removed document" do
227
+ expect(person.preference_ids).to_not include(preference.id)
228
+ end
229
+
230
+ it "removes the references to the removed document" do
231
+ expect(from_db.person_ids).to_not include(person.id)
232
+ end
233
+ end
234
+ end
235
+
236
+ context "when dependent is restrict_with_error" do
237
+
238
+ context "when restricting a references many" do
239
+
240
+ let!(:association) do
241
+ Person.has_many :drugs, dependent: :restrict_with_exception
242
+ end
243
+
244
+ after do
245
+ Person.dependents.delete(association)
246
+ Person.has_many :drugs, validate: false
247
+ end
248
+
249
+ context "when the relation is empty" do
250
+
251
+ let(:person) do
252
+ Person.new drugs: []
253
+ end
254
+
255
+ it "raises no error" do
256
+ expect { person.send(method) }.to_not raise_error
257
+ end
258
+
259
+ it "deletes the parent" do
260
+ person.send(method)
261
+ expect(person).to be_destroyed
262
+ end
263
+ end
264
+
265
+ context "when the relation is not empty" do
266
+
267
+ let(:person) do
268
+ Person.new drugs: [Drug.new]
269
+ end
270
+
271
+ it "raises DeleteRestriction error" do
272
+ expect { person.send(method) }.to raise_error(Mongoid::Errors::DeleteRestriction)
273
+ end
274
+ end
275
+ end
276
+
277
+ context "when restricting a references one" do
278
+
279
+ let!(:association) do
280
+ Person.has_one :account, dependent: :restrict_with_exception
281
+ end
282
+
283
+ after do
284
+ Person.dependents.delete(association)
285
+ Person.has_one :account, validate: false
286
+ end
287
+
288
+ context "when the relation is empty" do
289
+
290
+ let(:person) do
291
+ Person.new account: nil
292
+ end
293
+
294
+ it "raises no error" do
295
+ expect { person.send(method) }.to_not raise_error
296
+ end
297
+
298
+ it "deletes the parent" do
299
+ person.send(method)
300
+ expect(person).to be_destroyed
301
+ end
302
+ end
303
+
304
+ context "when the relation is not empty" do
305
+
306
+ let(:person) do
307
+ Person.new account: Account.new(name: 'test')
308
+ end
309
+
310
+ it "raises DeleteRestriction error" do
311
+ expect { person.send(method) }.to raise_error(Mongoid::Errors::DeleteRestriction)
312
+ end
313
+ end
314
+ end
315
+
316
+ context "when restricting a many to many" do
317
+
318
+ let!(:association) do
319
+ Person.has_and_belongs_to_many :houses, dependent: :restrict_with_exception
320
+ end
321
+
322
+ after do
323
+ Person.dependents.delete(association)
324
+ Person.has_and_belongs_to_many :houses, validate: false
325
+ end
326
+
327
+ context "when the relation is empty" do
328
+
329
+ let(:person) do
330
+ Person.new houses: []
331
+ end
332
+
333
+ it "raises no error" do
334
+ expect { person.send(method) }.to_not raise_error
335
+ end
336
+
337
+ it "deletes the parent" do
338
+ person.send(method)
339
+ expect(person).to be_destroyed
340
+ end
341
+ end
342
+
343
+ context "when the relation is not empty" do
344
+
345
+ let(:person) do
346
+ Person.new houses: [House.new]
347
+ end
348
+
349
+ it "raises DeleteRestriction error" do
350
+ expect { person.send(method) }.to raise_error(Mongoid::Errors::DeleteRestriction)
351
+ end
352
+ end
353
+ end
354
+ end
355
+ end
356
+ end
357
+ end
358
+
359
+ context 'when the strategy is :delete_all' do
360
+
361
+ let(:person) do
362
+ Person.create
363
+ end
364
+
365
+ context "when cascading a has one" do
366
+
367
+ context "when the relation exists" do
368
+
369
+ let!(:home) do
370
+ person.create_home
371
+ end
372
+
373
+ before do
374
+ person.delete
375
+ end
376
+
377
+ it "deletes the relation" do
378
+ expect(home).to be_destroyed
379
+ end
380
+
381
+ it "persists the deletion" do
382
+ expect {
383
+ home.reload
384
+ }.to raise_error(Mongoid::Errors::DocumentNotFound)
385
+ end
386
+ end
387
+
388
+ context "when the relation does not exist" do
389
+
390
+ before do
391
+ person.delete
392
+ end
393
+
394
+ it "deletes the base document" do
395
+ expect(person).to be_destroyed
396
+ end
397
+ end
398
+ end
399
+
400
+ context "when cascading a has many" do
401
+
402
+ context "when the relation has documents" do
403
+
404
+ let!(:post_one) do
405
+ person.posts.create(title: "one")
406
+ end
407
+
408
+ let!(:post_two) do
409
+ person.posts.create(title: "two")
410
+ end
411
+
412
+ context "when the documents are in memory" do
413
+
414
+ before do
415
+ expect(post_one).to receive(:delete).never
416
+ expect(post_two).to receive(:delete).never
417
+ person.delete
418
+ end
419
+
420
+ it "deletes the first document" do
421
+ expect(post_one).to be_destroyed
422
+ end
423
+
424
+ it "deletes the second document" do
425
+ expect(post_two).to be_destroyed
426
+ end
427
+
428
+ it "unbinds the first document" do
429
+ expect(post_one.person).to be_nil
430
+ end
431
+
432
+ it "unbinds the second document" do
433
+ expect(post_two.person).to be_nil
434
+ end
435
+
436
+ it "removes the documents from the relation" do
437
+ expect(person.posts).to be_empty
438
+ end
439
+
440
+ it "persists the first deletion" do
441
+ expect {
442
+ post_one.reload
443
+ }.to raise_error(Mongoid::Errors::DocumentNotFound)
444
+ end
445
+
446
+ it "persists the second deletion" do
447
+ expect {
448
+ post_two.reload
449
+ }.to raise_error(Mongoid::Errors::DocumentNotFound)
450
+ end
451
+ end
452
+ end
453
+ end
454
+ end
455
+
456
+ context 'when the strategy is :destroy' do
457
+
458
+ let!(:association) do
459
+ Person.has_many :destroyable_posts, class_name: "Post", dependent: :destroy
460
+ end
461
+
462
+ after do
463
+ Person.dependents.delete(association)
464
+ end
465
+
466
+ let(:person) do
467
+ Person.new
468
+ end
469
+
470
+ let(:post) do
471
+ Post.new
472
+ end
473
+
474
+ context "when the documents exist" do
475
+
476
+ before do
477
+ expect(post).to receive(:destroy)
478
+ person.destroyable_posts << post
479
+ end
480
+
481
+ it "destroys all documents in the relation" do
482
+ person.delete
483
+ end
484
+ end
485
+
486
+ context "when no documents exist" do
487
+
488
+ before do
489
+ expect(post).to receive(:destroy).never
490
+ end
491
+
492
+ it "it does not destroy the relation" do
493
+ person.delete
494
+ end
495
+ end
496
+ end
497
+
498
+ context 'when the strategy is :nullify' do
499
+
500
+ let!(:association) do
501
+ Person.has_many :nullifyable_posts, class_name: "Post", dependent: :nullify
502
+ end
503
+
504
+ after do
505
+ Person.dependents.delete(association)
506
+ end
507
+
508
+ let(:person) do
509
+ Person.new
510
+ end
511
+
512
+ let(:posts_relation) do
513
+ person.posts
514
+ end
515
+
516
+ before do
517
+ allow(person).to receive(:nullifyable_posts).and_return(posts_relation)
518
+ expect(posts_relation).to receive(:nullify)
519
+ end
520
+
521
+ it "nullifies the relation" do
522
+ person.delete
523
+ end
524
+ end
525
+
526
+ context 'when the strategy is :restrict_with_exception' do
527
+
528
+ let(:person) do
529
+ Person.new
530
+ end
531
+
532
+ let(:post) do
533
+ Post.new
534
+ end
535
+
536
+ let!(:association) do
537
+ Person.has_many :restrictable_posts, class_name: "Post", dependent: :restrict_with_exception
538
+ end
539
+
540
+ after do
541
+ Person.dependents.delete(association)
542
+ end
543
+
544
+ context 'when there are related objects' do
545
+
546
+ before do
547
+ person.restrictable_posts << post
548
+ expect(post).to receive(:delete).never
549
+ expect(post).to receive(:destroy).never
550
+ end
551
+
552
+ it 'raises an exception and leaves the related one intact' do
553
+ expect { person.delete }.to raise_exception(Mongoid::Errors::DeleteRestriction)
554
+ end
555
+ end
556
+
557
+ context 'when there are no related objects' do
558
+
559
+ before do
560
+ expect(post).to receive(:delete).never
561
+ expect(post).to receive(:destroy).never
562
+ end
563
+
564
+ it 'deletes the object and leaves the other one intact' do
565
+ expect(person.delete).to be(true)
566
+ end
567
+ end
568
+ end
569
+
570
+ context 'when the strategy is :restrict_with_error' do
571
+
572
+ let(:person) do
573
+ Person.new
574
+ end
575
+
576
+ let(:post) do
577
+ Post.new
578
+ end
579
+
580
+ let!(:association) do
581
+ Person.has_many :restrictable_posts, class_name: "Post", dependent: :restrict_with_error
582
+ end
583
+
584
+ after do
585
+ Person.dependents.delete(association)
586
+ end
587
+
588
+ context 'when there are related objects' do
589
+
590
+ before do
591
+ person.restrictable_posts << post
592
+ end
593
+
594
+ it 'adds an error to the parent object' do
595
+ expect(person.delete).to be(false)
596
+ expect(person.errors[:restrictable_posts].first).to be(
597
+ Mongoid::Association::Depending::RESTRICT_ERROR_MSG)
598
+ end
599
+ end
600
+
601
+ context 'when there are no related objects' do
602
+
603
+ before do
604
+ expect(post).to receive(:delete).never
605
+ expect(post).to receive(:destroy).never
606
+ end
607
+
608
+ it 'deletes the object and leaves the other one intact' do
609
+ expect(person.delete).to be(true)
610
+ end
611
+ end
612
+ end
613
+ end