mongoid 6.4.8 → 7.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,170 @@
1
+ # encoding: utf-8
2
+ module Mongoid
3
+ module Association
4
+ module Referenced
5
+
6
+ # This module handles the behaviour for synchronizing foreign keys between
7
+ # both sides of a many to many relations.
8
+ module Syncable
9
+
10
+ # Is the document able to be synced on the inverse side? This is only if
11
+ # the key has changed and the relation bindings have not been run.
12
+ #
13
+ # @example Are the foreign keys syncable?
14
+ # document._syncable?(association)
15
+ #
16
+ # @param [ Association ] association The association metadata.
17
+ #
18
+ # @return [ true, false ] If we can sync.
19
+ #
20
+ # @since 2.1.0
21
+ def _syncable?(association)
22
+ !_synced?(association.foreign_key) && send(association.foreign_key_check)
23
+ end
24
+
25
+ # Get the synced foreign keys.
26
+ #
27
+ # @example Get the synced foreign keys.
28
+ # document._synced
29
+ #
30
+ # @return [ Hash ] The synced foreign keys.
31
+ #
32
+ # @since 2.1.0
33
+ def _synced
34
+ @_synced ||= {}
35
+ end
36
+
37
+ # Has the document been synced for the foreign key?
38
+ #
39
+ # @example Has the document been synced?
40
+ # document._synced?
41
+ #
42
+ # @param [ String ] foreign_key The foreign key.
43
+ #
44
+ # @return [ true, false ] If we can sync.
45
+ #
46
+ # @since 2.1.0
47
+ def _synced?(foreign_key)
48
+ !!_synced[foreign_key]
49
+ end
50
+
51
+ # Update the inverse keys on destroy.
52
+ #
53
+ # @example Update the inverse keys.
54
+ # document.remove_inverse_keys(association)
55
+ #
56
+ # @param [ Association ] association The association.
57
+ #
58
+ # @return [ Object ] The updated values.
59
+ #
60
+ # @since 2.2.1
61
+ def remove_inverse_keys(association)
62
+ foreign_keys = send(association.foreign_key)
63
+ unless foreign_keys.nil? || foreign_keys.empty?
64
+ association.criteria(self, foreign_keys).pull(association.inverse_foreign_key => _id)
65
+ end
66
+ end
67
+
68
+ # Update the inverse keys for the relation.
69
+ #
70
+ # @example Update the inverse keys
71
+ # document.update_inverse_keys(association)
72
+ #
73
+ # @param [ Association ] association The document association.
74
+ #
75
+ # @return [ Object ] The updated values.
76
+ #
77
+ # @since 2.1.0
78
+ def update_inverse_keys(association)
79
+ if changes.has_key?(association.foreign_key)
80
+ old, new = changes[association.foreign_key]
81
+ adds, subs = new - (old || []), (old || []) - new
82
+
83
+ # If we are autosaving we don't want a duplicate to get added - the
84
+ # $addToSet would run previously and then the $pushAll from the
85
+ # inverse on the autosave would cause this. We delete each id from
86
+ # what's in memory in case a mix of id addition and object addition
87
+ # had occurred.
88
+ if association.autosave?
89
+ send(association.name).in_memory.each do |doc|
90
+ adds.delete_one(doc._id)
91
+ end
92
+ end
93
+
94
+ unless adds.empty?
95
+ association.criteria(self, adds).without_options.add_to_set(association.inverse_foreign_key => _id)
96
+ end
97
+ unless subs.empty?
98
+ association.criteria(self, subs).without_options.pull(association.inverse_foreign_key => _id)
99
+ end
100
+ end
101
+ end
102
+
103
+ module ClassMethods
104
+
105
+ # Set up the syncing of many to many foreign keys.
106
+ #
107
+ # @example Set up the syncing.
108
+ # Person._synced(association)
109
+ #
110
+ # @param [ Association ] association The association metadata.
111
+ #
112
+ # @since 2.1.0
113
+ def _synced(association)
114
+ unless association.forced_nil_inverse?
115
+ synced_save(association)
116
+ synced_destroy(association)
117
+ end
118
+ end
119
+
120
+ private
121
+
122
+ # Set up the sync of inverse keys that needs to happen on a save.
123
+ #
124
+ # If the foreign key field has changed and the document is not
125
+ # synced, $addToSet the new ids, $pull the ones no longer in the
126
+ # array from the inverse side.
127
+ #
128
+ # @example Set up the save syncing.
129
+ # Person.synced_save(association)
130
+ #
131
+ # @param [ Association ] association The relation association.
132
+ #
133
+ # @return [ Class ] The class getting set up.
134
+ #
135
+ # @since 2.1.0
136
+ def synced_save(association)
137
+ set_callback(
138
+ :save,
139
+ :after,
140
+ if: ->(doc) { doc._syncable?(association) }
141
+ ) do |doc|
142
+ doc.update_inverse_keys(association)
143
+ end
144
+ self
145
+ end
146
+
147
+ # Set up the sync of inverse keys that needs to happen on a destroy.
148
+ #
149
+ # @example Set up the destroy syncing.
150
+ # Person.synced_destroy(association)
151
+ #
152
+ # @param [ Association ] association The association metadata.
153
+ #
154
+ # @return [ Class ] The class getting set up.
155
+ #
156
+ # @since 2.2.1
157
+ def synced_destroy(association)
158
+ set_callback(
159
+ :destroy,
160
+ :after
161
+ ) do |doc|
162
+ doc.remove_inverse_keys(association)
163
+ end
164
+ self
165
+ end
166
+ end
167
+ end
168
+ end
169
+ end
170
+ end
@@ -1,62 +1,66 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid
3
- module Relations
3
+ module Association
4
4
 
5
5
  # The reflections module provides convenience methods that can retrieve
6
6
  # useful information about associations.
7
7
  module Reflections
8
8
  extend ActiveSupport::Concern
9
9
 
10
- # Returns the relation metadata for the supplied name.
10
+ # Returns the association metadata for the supplied name.
11
11
  #
12
- # @example Find relation metadata by name.
12
+ # @example Find relation association by name.
13
13
  # person.reflect_on_association(:addresses)
14
14
  #
15
15
  # @param [ String, Symbol ] name The name of the relation to find.
16
16
  #
17
- # @return [ Metadata ] The matching relation metadata.
17
+ # @return [ Association ] The matching association metadata.
18
18
  def reflect_on_association(name)
19
19
  self.class.reflect_on_association(name)
20
20
  end
21
21
 
22
- # Returns all relation metadata for the supplied macros.
22
+ # Returns all association metadata for the supplied macros.
23
23
  #
24
- # @example Find multiple relation metadata by macro.
24
+ # @example Find multiple association metadata by macro.
25
25
  # person.reflect_on_all_associations(:embeds_many)
26
26
  #
27
27
  # @param [ Array<Symbol> ] macros The relation macros.
28
28
  #
29
- # @return [ Array<Metadata> ] The matching relation metadata.
30
- def reflect_on_all_associations(*macros)
29
+ # @return [ Array<Association> ] The matching association metadata.
30
+ def reflect_on_all_association(*macros)
31
31
  self.class.reflect_on_all_associations(*macros)
32
32
  end
33
33
 
34
34
  module ClassMethods
35
35
 
36
- # Returns the relation metadata for the supplied name.
36
+ # Returns the association metadata for the supplied name.
37
37
  #
38
- # @example Find relation metadata by name.
38
+ # @example Find association metadata by name.
39
39
  # Person.reflect_on_association(:addresses)
40
40
  #
41
41
  # @param [ String, Symbol ] name The name of the relation to find.
42
42
  #
43
- # @return [ Metadata ] The matching relation metadata.
43
+ # @return [ Association ] The matching association metadata.
44
44
  def reflect_on_association(name)
45
45
  relations[name.to_s]
46
46
  end
47
47
 
48
- # Returns all relation metadata for the supplied macros.
48
+ # Returns all association metadata for the supplied macros.
49
49
  #
50
- # @example Find multiple relation metadata by macro.
50
+ # @example Find multiple association metadata by macro.
51
51
  # Person.reflect_on_all_associations(:embeds_many)
52
52
  #
53
53
  # @param [ Array<Symbol> ] macros The relation macros.
54
54
  #
55
- # @return [ Array<Metadata> ] The matching relation metadata.
55
+ # @return [ Array<Association> ] The matching association metadata.
56
56
  def reflect_on_all_associations(*macros)
57
- association_reflections = relations.values
58
- association_reflections.select! { |reflection| macros.include?(reflection.macro) } unless macros.empty?
59
- association_reflections
57
+ all_associations = relations.values
58
+ unless macros.empty?
59
+ all_associations.select! do |reflection|
60
+ macros.include?(Association::MACRO_MAPPING.key(reflection.class))
61
+ end
62
+ end
63
+ all_associations
60
64
  end
61
65
  end
62
66
  end
@@ -0,0 +1,415 @@
1
+ require 'mongoid/association/constrainable'
2
+ require 'mongoid/association/options'
3
+
4
+ module Mongoid
5
+ module Association
6
+
7
+ # This module provides behaviors shared between Association types.
8
+ #
9
+ # @since 7.0
10
+ module Relatable
11
+ include Constrainable
12
+ include Options
13
+
14
+ # The options shared between all association types.
15
+ #
16
+ # @return [ Array<Symbol> ] The shared options.
17
+ #
18
+ # @since 7.0
19
+ SHARED_OPTIONS = [
20
+ :class_name,
21
+ :inverse_of,
22
+ :validate,
23
+ :extend
24
+ ].freeze
25
+
26
+ # The primary key default.
27
+ #
28
+ # @return [ String ] The primary key field default.
29
+ #
30
+ # @since 7.0
31
+ PRIMARY_KEY_DEFAULT = '_id'.freeze
32
+
33
+ # The name of the association.
34
+ #
35
+ # @return [ Symbol ] The name of the relation.
36
+ #
37
+ # @since 7.0
38
+ attr_reader :name
39
+
40
+ # The options on this association.
41
+ #
42
+ # @return [ Hash ] The options.
43
+ #
44
+ # @since 7.0
45
+ attr_reader :options
46
+
47
+ # Initialize the Association.
48
+ #
49
+ # @param [ Class ] _class The class of the model who owns this relation.
50
+ # @param [ Symbol ] name The name of the association.
51
+ # @param [ Hash ] opts The relation options.
52
+ # @param [ Block ] block The optional block.
53
+ #
54
+ # @since 7.0
55
+ def initialize(_class, name, opts = {}, &block)
56
+ @owner_class = _class
57
+ @name = name
58
+ @options = opts
59
+ @extension = nil
60
+ create_extension!(&block)
61
+ validate!
62
+ end
63
+
64
+ # Compare this association to another.
65
+ #
66
+ # @return [ Object ] The object to compare to this association.
67
+ #
68
+ # @since 7.0
69
+ def ==(other)
70
+ relation_class_name == other.relation_class_name &&
71
+ inverse_class_name == other.inverse_class_name &&
72
+ name == other.name &&
73
+ options == other.options
74
+ end
75
+
76
+ # Get the callbacks for a given type.
77
+ #
78
+ # @param [ Symbol ] callback_type The type of callback type.
79
+ #
80
+ # @return [ Array<Proc, Symbol> ] A list of the callbacks, either method
81
+ # names or Procs.
82
+ #
83
+ # @since 7.0
84
+ def get_callbacks(callback_type)
85
+ Array(options[callback_type])
86
+ end
87
+
88
+ # Get the type setter.
89
+ # @note Only relevant for polymorphic relations that take the :as option.
90
+ #
91
+ # @return [ String ] The type setter method.
92
+ #
93
+ # @since 7.0
94
+ def type_setter
95
+ @type_setter ||= type.__setter__
96
+ end
97
+
98
+ # Whether trying to bind an object using this association should raise
99
+ # an error.
100
+ #
101
+ # @param [ Document ] doc The document to be bound.
102
+ #
103
+ # @return [ true, false ] Whether the document can be bound.
104
+ #
105
+ # @since 7.0
106
+ def bindable?(doc); false; end
107
+
108
+ # Get the inverse names.
109
+ #
110
+ # @param [ Object ] other The other model class or model object to use when
111
+ # determining inverses.
112
+ #
113
+ # @return [ Array<Symbol> ] The list of inverse names.
114
+ #
115
+ # @since 7.0
116
+ def inverses(other = nil)
117
+ return [ inverse_of ] if inverse_of
118
+ if polymorphic?
119
+ polymorphic_inverses(other)
120
+ else
121
+ determine_inverses(other)
122
+ end
123
+ end
124
+
125
+ # Get the inverse's association metadata.
126
+ #
127
+ # @param [ Object ] other The other model class or model object to use when
128
+ # determining inverses.
129
+ #
130
+ # @return [ Association ] The inverse's association metadata.
131
+ #
132
+ # @since 7.0
133
+ def inverse_association(other = nil)
134
+ (other || relation_class).relations[inverse(other)]
135
+ end
136
+
137
+ # Get the inverse type.
138
+ #
139
+ # @return [ nil ] Default is nil for an association.
140
+ #
141
+ # @since 7.0
142
+ def inverse_type; end
143
+
144
+ # The class name of the relation object(s).
145
+ #
146
+ # @return [ String ] The relation objects' class name.
147
+ #
148
+ # @since 7.0
149
+ def relation_class_name
150
+ @class_name ||= @options[:class_name] || ActiveSupport::Inflector.classify(name)
151
+ end
152
+ alias :class_name :relation_class_name
153
+
154
+ # The class of the relation object(s).
155
+ #
156
+ # @return [ String ] The relation objects' class.
157
+ #
158
+ # @since 7.0
159
+ def klass
160
+ @klass ||= relation_class_name.constantize
161
+ end
162
+ alias :relation_class :klass
163
+
164
+ # The class name of the object owning this relation.
165
+ #
166
+ # @return [ String ] The owning objects' class name.
167
+ #
168
+ # @since 7.0
169
+ def inverse_class_name
170
+ @inverse_class_name ||= @owner_class.name
171
+ end
172
+
173
+ # The class of the object owning this relation.
174
+ #
175
+ # @return [ String ] The owning objects' class.
176
+ #
177
+ # @since 7.0
178
+ def inverse_class
179
+ @owner_class
180
+ end
181
+ alias :inverse_klass :inverse_class
182
+
183
+ # The foreign key field if this relation stores a foreign key.
184
+ # Otherwise, the primary key.
185
+ #
186
+ # @return [ Symbol, String ] The primary key.
187
+ #
188
+ # @since 7.0
189
+ def key
190
+ stores_foreign_key? ? foreign_key : primary_key
191
+ end
192
+
193
+ # The name of the setter on this object for assigning an associated object.
194
+ #
195
+ # @return [ String ] The setter name.
196
+ #
197
+ # @since 7.0
198
+ def setter
199
+ @setter ||= "#{name}="
200
+ end
201
+
202
+ # The name of the inverse setter method.
203
+ #
204
+ # @return [ String ] The name of the inverse setter.
205
+ #
206
+ # @since 7.0
207
+ def inverse_setter(other = nil)
208
+ @inverse_setter ||= "#{inverses(other).first}=" unless inverses(other).blank?
209
+ end
210
+
211
+ # The name of the foreign key setter method.
212
+ #
213
+ # @return [ String ] The name of the foreign key setter.
214
+ #
215
+ # @since 7.0
216
+ def foreign_key_setter
217
+ # note: You can't check if this association stores foreign key
218
+ # See HasOne and HasMany binding, they referenced foreign_key_setter
219
+ @foreign_key_setter ||= "#{foreign_key}=" if foreign_key
220
+ end
221
+
222
+ # The atomic path for this relation.
223
+ #
224
+ # @return [ Mongoid::Atomic::Paths::Root ] The atomic path object.
225
+ #
226
+ # @since 7.0
227
+ def path(document)
228
+ relation.path(document)
229
+ end
230
+
231
+ # Gets the setter for the field that sets the type of document on a
232
+ # polymorphic relation.
233
+ #
234
+ # @example Get the inverse type setter.
235
+ # association.inverse_type_setter
236
+ #
237
+ # @return [ String ] The name of the setter.
238
+ #
239
+ # @since 7.0
240
+ def inverse_type_setter
241
+ @inverse_type_setter ||= inverse_type.__setter__
242
+ end
243
+
244
+ # Get the name of the method to check if the foreign key has changed.
245
+ #
246
+ # @example Get the foreign key check method.
247
+ # association.foreign_key_check
248
+ #
249
+ # @return [ String ] The foreign key check.
250
+ #
251
+ # @since 7.0
252
+ def foreign_key_check
253
+ @foreign_key_check ||= "#{foreign_key}_changed?" if (stores_foreign_key? && foreign_key)
254
+ end
255
+
256
+ # Create a relation proxy object using the owner and target.
257
+ #
258
+ # @param [ Document ] owner The document this relation hangs off of.
259
+ # @param [ Document, Array<Document> ] target The target (parent) of the
260
+ # relation.
261
+ #
262
+ # @return [ Proxy ]
263
+ #
264
+ # @since 7.0
265
+ def create_relation(owner, target)
266
+ relation.new(owner, target, self)
267
+ end
268
+
269
+ # Whether the dependent method is destructive.
270
+ #
271
+ # @return [ true, false ] If the dependent method is destructive.
272
+ #
273
+ # @since 7.0
274
+ def destructive?
275
+ @destructive ||= !!(dependent && (dependent == :delete_all || dependent == :destroy))
276
+ end
277
+
278
+ # Get the counter cache column name.
279
+ #
280
+ # @return [ String ] The counter cache column name.
281
+ #
282
+ # @since 7.0
283
+ def counter_cache_column_name
284
+ @counter_cache_column_name ||= (@options[:counter_cache].is_a?(String) ||
285
+ @options[:counter_cache].is_a?(Symbol)) ?
286
+ @options[:counter_cache] : "#{inverse || inverse_class_name.demodulize.underscore.pluralize}_count"
287
+ end
288
+
289
+ # Get the extension.
290
+ #
291
+ # @return [ Module ] The extension module, if one has been defined.
292
+ #
293
+ # @since 7.0
294
+ def extension
295
+ @extension ||= @options[:extend]
296
+ end
297
+
298
+ # Get the inverse name.
299
+ #
300
+ # @return [ Symbol ] The inverse name.
301
+ #
302
+ # @since 7.0
303
+ def inverse(other = nil)
304
+ candidates = inverses(other)
305
+ candidates.detect { |c| c } if candidates
306
+ end
307
+
308
+ # Whether the associated object(s) should be validated.
309
+ #
310
+ # @return [ true, false ] If the associated object(s)
311
+ # should be validated.
312
+ #
313
+ # @since 7.0
314
+ def validate?
315
+ @validate ||= if @options[:validate].nil?
316
+ validation_default
317
+ else
318
+ !!@options[:validate]
319
+ end
320
+ end
321
+
322
+ private
323
+
324
+ def setup_index!
325
+ @owner_class.index(index_spec, background: true) if indexed?
326
+ end
327
+
328
+ def define_touchable!
329
+ if touchable?
330
+ Association::Touchable.define_touchable!(self)
331
+ end
332
+ end
333
+
334
+ def define_autosaver!
335
+ if autosave?
336
+ Association::Referenced::AutoSave.define_autosave!(self)
337
+ end
338
+ end
339
+
340
+ def define_builder!
341
+ Association::Builders.define_builder!(self)
342
+ end
343
+
344
+ def define_creator!
345
+ Association::Builders.define_creator!(self)
346
+ end
347
+
348
+ def define_getter!
349
+ Association::Accessors.define_getter!(self)
350
+ end
351
+
352
+ def define_setter!
353
+ Association::Accessors.define_setter!(self)
354
+ end
355
+
356
+ def define_existence_check!
357
+ Association::Accessors.define_existence_check!(self)
358
+ end
359
+
360
+ def define_ids_getter!
361
+ Association::Accessors.define_ids_getter!(self)
362
+ end
363
+
364
+ def define_ids_setter!
365
+ Association::Accessors.define_ids_setter!(self)
366
+ end
367
+
368
+ def define_counter_cache_callbacks!
369
+ if counter_cached?
370
+ Association::Referenced::CounterCache.define_callbacks!(self)
371
+ end
372
+ end
373
+
374
+ def define_dependency!
375
+ if dependent
376
+ Association::Depending.define_dependency!(self)
377
+ end
378
+ end
379
+
380
+ def validate!
381
+ @options.keys.each do |opt|
382
+ unless self.class::VALID_OPTIONS.include?(opt)
383
+ raise Errors::InvalidRelationOption.new(@owner_class, name, opt, self.class::VALID_OPTIONS)
384
+ end
385
+ end
386
+
387
+ [name, "#{name}?".to_sym, "#{name}=".to_sym].each do |n|
388
+ if Mongoid.destructive_fields.include?(n)
389
+ raise Errors::InvalidRelation.new(@owner_class, n)
390
+ end
391
+ end
392
+ end
393
+
394
+ def polymorph!
395
+ if polymorphic?
396
+ @owner_class.polymorphic = true
397
+ end
398
+ end
399
+
400
+ def create_extension!(&block)
401
+ if block
402
+ extension_module_name = "#{@owner_class.to_s.demodulize}#{name.to_s.camelize}RelationExtension"
403
+ silence_warnings do
404
+ @owner_class.const_set(extension_module_name, Module.new(&block))
405
+ end
406
+ @extension = "#{@owner_class}::#{extension_module_name}".constantize
407
+ end
408
+ end
409
+
410
+ def default_inverse
411
+ @default_inverse ||= klass.relations[inverse_klass.name.underscore]
412
+ end
413
+ end
414
+ end
415
+ end