mongoid 6.4.2 → 7.0.3

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 (318) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/config/locales/en.yml +17 -0
  4. data/lib/mongoid/association/accessors.rb +339 -0
  5. data/lib/mongoid/{relations/binding.rb → association/bindable.rb} +32 -52
  6. data/lib/mongoid/association/builders.rb +92 -0
  7. data/lib/mongoid/{relations/constraint.rb → association/constrainable.rb} +11 -22
  8. data/lib/mongoid/association/depending.rb +137 -0
  9. data/lib/mongoid/{relations/eager.rb → association/eager_loadable.rb} +11 -11
  10. data/lib/mongoid/{relations → association}/embedded/batchable.rb +19 -19
  11. data/lib/mongoid/association/embedded/cyclic.rb +109 -0
  12. data/lib/mongoid/association/embedded/embedded_in/binding.rb +56 -0
  13. data/lib/mongoid/{relations/builders/embedded/in.rb → association/embedded/embedded_in/buildable.rb} +12 -6
  14. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +121 -0
  15. data/lib/mongoid/association/embedded/embedded_in.rb +154 -0
  16. data/lib/mongoid/{relations/bindings/embedded/many.rb → association/embedded/embeds_many/binding.rb} +11 -9
  17. data/lib/mongoid/{relations/builders/embedded/many.rb → association/embedded/embeds_many/buildable.rb} +13 -7
  18. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +552 -0
  19. data/lib/mongoid/association/embedded/embeds_many.rb +210 -0
  20. data/lib/mongoid/{relations/bindings/embedded/one.rb → association/embedded/embeds_one/binding.rb} +12 -10
  21. data/lib/mongoid/{relations/builders/embedded/one.rb → association/embedded/embeds_one/buildable.rb} +13 -7
  22. data/lib/mongoid/association/embedded/embeds_one/proxy.rb +130 -0
  23. data/lib/mongoid/association/embedded/embeds_one.rb +173 -0
  24. data/lib/mongoid/association/embedded.rb +4 -0
  25. data/lib/mongoid/association/macros.rb +204 -0
  26. data/lib/mongoid/{relations → association}/many.rb +19 -49
  27. data/lib/mongoid/{relations → association}/marshalable.rb +6 -4
  28. data/lib/mongoid/association/nested/many.rb +200 -0
  29. data/lib/mongoid/association/nested/nested_buildable.rb +72 -0
  30. data/lib/mongoid/association/nested/one.rb +127 -0
  31. data/lib/mongoid/association/nested.rb +15 -0
  32. data/lib/mongoid/{relations → association}/one.rb +6 -6
  33. data/lib/mongoid/association/options.rb +152 -0
  34. data/lib/mongoid/{relations → association}/proxy.rb +31 -58
  35. data/lib/mongoid/association/referenced/auto_save.rb +79 -0
  36. data/lib/mongoid/association/referenced/belongs_to/binding.rb +87 -0
  37. data/lib/mongoid/association/referenced/belongs_to/buildable.rb +46 -0
  38. data/lib/mongoid/association/referenced/belongs_to/eager.rb +36 -0
  39. data/lib/mongoid/association/referenced/belongs_to/proxy.rb +136 -0
  40. data/lib/mongoid/association/referenced/belongs_to.rb +248 -0
  41. data/lib/mongoid/association/referenced/counter_cache.rb +163 -0
  42. data/lib/mongoid/association/referenced/eager.rb +162 -0
  43. data/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb +71 -0
  44. data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +40 -0
  45. data/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb +52 -0
  46. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +312 -0
  47. data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +290 -0
  48. data/lib/mongoid/{relations/bindings/referenced/many.rb → association/referenced/has_many/binding.rb} +6 -5
  49. data/lib/mongoid/association/referenced/has_many/buildable.rb +38 -0
  50. data/lib/mongoid/association/referenced/has_many/eager.rb +43 -0
  51. data/lib/mongoid/association/referenced/has_many/enumerable.rb +510 -0
  52. data/lib/mongoid/association/referenced/has_many/proxy.rb +578 -0
  53. data/lib/mongoid/association/referenced/has_many.rb +275 -0
  54. data/lib/mongoid/{relations/bindings/referenced/one.rb → association/referenced/has_one/binding.rb} +11 -8
  55. data/lib/mongoid/association/referenced/has_one/buildable.rb +60 -0
  56. data/lib/mongoid/association/referenced/has_one/eager.rb +35 -0
  57. data/lib/mongoid/{relations/builders/nested_attributes/one.rb → association/referenced/has_one/nested_builder.rb} +9 -9
  58. data/lib/mongoid/association/referenced/has_one/proxy.rb +113 -0
  59. data/lib/mongoid/association/referenced/has_one.rb +204 -0
  60. data/lib/mongoid/association/referenced/syncable.rb +170 -0
  61. data/lib/mongoid/association/referenced.rb +7 -0
  62. data/lib/mongoid/{relations → association}/reflections.rb +21 -17
  63. data/lib/mongoid/association/relatable.rb +509 -0
  64. data/lib/mongoid/{relations.rb → association.rb} +57 -56
  65. data/lib/mongoid/atomic/paths/embedded/many.rb +1 -1
  66. data/lib/mongoid/atomic/paths/embedded/one.rb +1 -1
  67. data/lib/mongoid/atomic.rb +4 -4
  68. data/lib/mongoid/attributes/dynamic.rb +2 -2
  69. data/lib/mongoid/attributes/nested.rb +22 -11
  70. data/lib/mongoid/attributes/processing.rb +2 -2
  71. data/lib/mongoid/attributes/readonly.rb +2 -4
  72. data/lib/mongoid/attributes.rb +22 -13
  73. data/lib/mongoid/changeable.rb +1 -1
  74. data/lib/mongoid/clients/options.rb +7 -5
  75. data/lib/mongoid/composable.rb +4 -4
  76. data/lib/mongoid/config.rb +1 -0
  77. data/lib/mongoid/contextual/atomic.rb +1 -1
  78. data/lib/mongoid/contextual/geo_near.rb +1 -1
  79. data/lib/mongoid/contextual/memory.rb +21 -3
  80. data/lib/mongoid/contextual/mongo.rb +10 -8
  81. data/lib/mongoid/copyable.rb +7 -6
  82. data/lib/mongoid/criteria/includable.rb +14 -14
  83. data/lib/mongoid/criteria/modifiable.rb +8 -14
  84. data/lib/mongoid/criteria/options.rb +2 -2
  85. data/lib/mongoid/criteria/queryable/extensions/string.rb +1 -1
  86. data/lib/mongoid/criteria/queryable/pipeline.rb +10 -5
  87. data/lib/mongoid/criteria/queryable/selectable.rb +37 -7
  88. data/lib/mongoid/criteria.rb +2 -2
  89. data/lib/mongoid/document.rb +15 -6
  90. data/lib/mongoid/errors/invalid_dependent_strategy.rb +32 -0
  91. data/lib/mongoid/errors/invalid_relation_option.rb +29 -0
  92. data/lib/mongoid/errors/unknown_model.rb +25 -0
  93. data/lib/mongoid/errors.rb +3 -0
  94. data/lib/mongoid/extensions/array.rb +5 -5
  95. data/lib/mongoid/extensions/big_decimal.rb +1 -1
  96. data/lib/mongoid/extensions/hash.rb +5 -2
  97. data/lib/mongoid/extensions/object.rb +4 -4
  98. data/lib/mongoid/extensions/range.rb +1 -0
  99. data/lib/mongoid/extensions/regexp.rb +1 -0
  100. data/lib/mongoid/extensions.rb +0 -4
  101. data/lib/mongoid/factory.rb +13 -3
  102. data/lib/mongoid/fields/foreign_key.rb +5 -5
  103. data/lib/mongoid/fields/standard.rb +2 -14
  104. data/lib/mongoid/fields/validators/macro.rb +1 -1
  105. data/lib/mongoid/fields.rb +3 -3
  106. data/lib/mongoid/indexable.rb +4 -1
  107. data/lib/mongoid/interceptable.rb +5 -5
  108. data/lib/mongoid/matchable/and.rb +1 -1
  109. data/lib/mongoid/matchable/elem_match.rb +9 -3
  110. data/lib/mongoid/matchable/eq.rb +22 -0
  111. data/lib/mongoid/matchable/ne.rb +1 -1
  112. data/lib/mongoid/matchable.rb +3 -1
  113. data/lib/mongoid/persistable/deletable.rb +7 -6
  114. data/lib/mongoid/persistable/incrementable.rb +1 -1
  115. data/lib/mongoid/persistable/logical.rb +1 -1
  116. data/lib/mongoid/persistable/settable.rb +57 -12
  117. data/lib/mongoid/persistable.rb +4 -5
  118. data/lib/mongoid/persistence_context.rb +20 -5
  119. data/lib/mongoid/query_cache.rb +8 -4
  120. data/lib/mongoid/railtie.rb +17 -0
  121. data/lib/mongoid/railties/controller_runtime.rb +86 -0
  122. data/lib/mongoid/scopable.rb +3 -3
  123. data/lib/mongoid/serializable.rb +4 -4
  124. data/lib/mongoid/shardable.rb +1 -1
  125. data/lib/mongoid/threaded.rb +36 -0
  126. data/lib/mongoid/touchable.rb +102 -0
  127. data/lib/mongoid/traversable.rb +3 -3
  128. data/lib/mongoid/validatable/presence.rb +2 -2
  129. data/lib/mongoid/validatable/uniqueness.rb +4 -4
  130. data/lib/mongoid/validatable.rb +8 -8
  131. data/lib/mongoid/version.rb +1 -1
  132. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +3 -0
  133. data/spec/app/models/animal.rb +2 -1
  134. data/spec/app/models/bomb.rb +1 -1
  135. data/spec/app/models/message.rb +1 -1
  136. data/spec/app/models/minim.rb +7 -0
  137. data/spec/app/models/person.rb +5 -2
  138. data/spec/app/models/shipment_address.rb +1 -0
  139. data/spec/app/models/store_as_dup_test3.rb +7 -0
  140. data/spec/app/models/store_as_dup_test4.rb +7 -0
  141. data/spec/app/models/updatable.rb +7 -0
  142. data/spec/app/models/vertex.rb +6 -0
  143. data/spec/app/models/wiki_page.rb +1 -1
  144. data/spec/config/mongoid.yml +13 -3
  145. data/spec/integration/associations/belongs_to_spec.rb +13 -0
  146. data/spec/lite_spec_helper.rb +56 -0
  147. data/spec/mongoid/{relations → association}/accessors_spec.rb +40 -1
  148. data/spec/mongoid/{relations → association}/auto_save_spec.rb +60 -12
  149. data/spec/mongoid/{relations → association}/builders_spec.rb +1 -1
  150. data/spec/mongoid/association/constrainable_spec.rb +115 -0
  151. data/spec/mongoid/{relations → association}/counter_cache_spec.rb +1 -1
  152. data/spec/mongoid/association/depending_spec.rb +866 -0
  153. data/spec/mongoid/{relations → association}/eager_spec.rb +12 -12
  154. data/spec/mongoid/{relations → association/embedded}/cyclic_spec.rb +1 -1
  155. data/spec/mongoid/{relations/bindings/embedded/in_spec.rb → association/embedded/embedded_in/binding_spec.rb} +13 -13
  156. data/spec/mongoid/{relations/builders/embedded/in_spec.rb → association/embedded/embedded_in/buildable_spec.rb} +9 -9
  157. data/spec/mongoid/{relations/embedded/in_spec.rb → association/embedded/embedded_in/proxy_spec.rb} +5 -77
  158. data/spec/mongoid/association/embedded/embedded_in_spec.rb +843 -0
  159. data/spec/mongoid/{relations/bindings/embedded/many_spec.rb → association/embedded/embeds_many/binding_spec.rb} +3 -3
  160. data/spec/mongoid/{relations/builders/embedded/many_spec.rb → association/embedded/embeds_many/buildable_spec.rb} +17 -45
  161. data/spec/mongoid/{relations/embedded/many_spec.rb → association/embedded/embeds_many/proxy_spec.rb} +124 -182
  162. data/spec/mongoid/association/embedded/embeds_many_spec.rb +852 -0
  163. data/spec/mongoid/{relations/bindings/embedded/one_spec.rb → association/embedded/embeds_one/binding_spec.rb} +4 -4
  164. data/spec/mongoid/{relations/builders/embedded/one_spec.rb → association/embedded/embeds_one/buildable_spec.rb} +14 -34
  165. data/spec/mongoid/{relations/embedded/one_spec.rb → association/embedded/embeds_one/proxy_spec.rb} +39 -84
  166. data/spec/mongoid/association/embedded/embeds_one_spec.rb +908 -0
  167. data/spec/mongoid/{relations → association}/macros_spec.rb +148 -73
  168. data/spec/mongoid/{relations/builders/nested_attributes → association/nested}/many_spec.rb +16 -19
  169. data/spec/mongoid/{relations/builders/nested_attributes → association/nested}/one_spec.rb +17 -20
  170. data/spec/mongoid/association/options_spec.rb +1321 -0
  171. data/spec/mongoid/{relations → association}/polymorphic_spec.rb +66 -34
  172. data/spec/mongoid/{relations/bindings/referenced/in_spec.rb → association/referenced/belongs_to/binding_spec.rb} +7 -7
  173. data/spec/mongoid/{relations/builders/referenced/in_spec.rb → association/referenced/belongs_to/buildable_spec.rb} +46 -79
  174. data/spec/mongoid/{relations/eager/belongs_to_spec.rb → association/referenced/belongs_to/eager_spec.rb} +33 -14
  175. data/spec/mongoid/{relations/referenced/in_spec.rb → association/referenced/belongs_to/proxy_spec.rb} +57 -91
  176. data/spec/mongoid/association/referenced/belongs_to_spec.rb +2006 -0
  177. data/spec/mongoid/{relations/bindings/referenced/many_to_many_spec.rb → association/referenced/has_and_belongs_to_many/binding_spec.rb} +5 -5
  178. data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +121 -0
  179. data/spec/mongoid/{relations/eager/has_and_belongs_to_many_spec.rb → association/referenced/has_and_belongs_to_many/eager_spec.rb} +26 -7
  180. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_persistence_spec.rb +56 -0
  181. data/spec/mongoid/{relations/referenced/many_to_many_spec.rb → association/referenced/has_and_belongs_to_many/proxy_spec.rb} +107 -98
  182. data/spec/mongoid/association/referenced/has_and_belongs_to_many_models.rb +26 -0
  183. data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +1027 -0
  184. data/spec/mongoid/{relations/bindings/referenced/many_spec.rb → association/referenced/has_many/binding_spec.rb} +5 -5
  185. data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +119 -0
  186. data/spec/mongoid/{relations/eager/has_many_spec.rb → association/referenced/has_many/eager_spec.rb} +26 -11
  187. data/spec/mongoid/{relations/targets → association/referenced/has_many}/enumerable_spec.rb +122 -1
  188. data/spec/mongoid/association/referenced/has_many/proxy_query_spec.rb +23 -0
  189. data/spec/mongoid/{relations/referenced/many_spec.rb → association/referenced/has_many/proxy_spec.rb} +28 -93
  190. data/spec/mongoid/association/referenced/has_many_models.rb +37 -0
  191. data/spec/mongoid/association/referenced/has_many_spec.rb +1225 -0
  192. data/spec/mongoid/{relations/bindings/referenced/one_spec.rb → association/referenced/has_one/binding_spec.rb} +4 -4
  193. data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +113 -0
  194. data/spec/mongoid/{relations/eager/has_one_spec.rb → association/referenced/has_one/eager_spec.rb} +10 -10
  195. data/spec/mongoid/{relations/referenced/one_spec.rb → association/referenced/has_one/proxy_spec.rb} +9 -109
  196. data/spec/mongoid/association/referenced/has_one_models.rb +48 -0
  197. data/spec/mongoid/association/referenced/has_one_spec.rb +1350 -0
  198. data/spec/mongoid/{relations → association}/reflections_spec.rb +1 -12
  199. data/spec/mongoid/{relations/synchronization_spec.rb → association/syncable_spec.rb} +4 -2
  200. data/spec/mongoid/{relations_spec.rb → association_spec.rb} +1 -1
  201. data/spec/mongoid/atomic/modifiers_spec.rb +2 -2
  202. data/spec/mongoid/atomic_spec.rb +4 -4
  203. data/spec/mongoid/attributes/nested_spec.rb +29 -11
  204. data/spec/mongoid/attributes/readonly_spec.rb +80 -125
  205. data/spec/mongoid/attributes_spec.rb +38 -2
  206. data/spec/mongoid/clients/factory_spec.rb +24 -18
  207. data/spec/mongoid/clients/options_spec.rb +58 -44
  208. data/spec/mongoid/clients/sessions_spec.rb +1 -1
  209. data/spec/mongoid/clients/transactions_spec.rb +369 -0
  210. data/spec/mongoid/clients_spec.rb +68 -8
  211. data/spec/mongoid/config_spec.rb +27 -1
  212. data/spec/mongoid/contextual/memory_spec.rb +19 -0
  213. data/spec/mongoid/contextual/mongo_spec.rb +33 -5
  214. data/spec/mongoid/copyable_spec.rb +90 -6
  215. data/spec/mongoid/copyable_spec_models.rb +17 -0
  216. data/spec/mongoid/criteria/modifiable_spec.rb +183 -60
  217. data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +3 -3
  218. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +43 -0
  219. data/spec/mongoid/criteria/queryable/pipeline_spec.rb +12 -0
  220. data/spec/mongoid/criteria/queryable/selectable_spec.rb +42 -3
  221. data/spec/mongoid/criteria/queryable/selector_spec.rb +2 -2
  222. data/spec/mongoid/criteria/scopable_spec.rb +81 -0
  223. data/spec/mongoid/criteria_spec.rb +33 -18
  224. data/spec/mongoid/document_spec.rb +83 -4
  225. data/spec/mongoid/extensions/array_spec.rb +11 -15
  226. data/spec/mongoid/extensions/big_decimal_spec.rb +9 -9
  227. data/spec/mongoid/extensions/hash_spec.rb +18 -1
  228. data/spec/mongoid/extensions/object_spec.rb +7 -11
  229. data/spec/mongoid/extensions/range_spec.rb +7 -0
  230. data/spec/mongoid/extensions/regexp_spec.rb +23 -0
  231. data/spec/mongoid/factory_spec.rb +19 -1
  232. data/spec/mongoid/fields/foreign_key_spec.rb +24 -32
  233. data/spec/mongoid/fields_spec.rb +2 -2
  234. data/spec/mongoid/findable_spec.rb +1 -1
  235. data/spec/mongoid/indexable_spec.rb +18 -8
  236. data/spec/mongoid/interceptable_spec.rb +22 -1
  237. data/spec/mongoid/matchable/elem_match_spec.rb +20 -0
  238. data/spec/mongoid/matchable/eq_spec.rb +48 -0
  239. data/spec/mongoid/persistable/incrementable_spec.rb +6 -6
  240. data/spec/mongoid/persistable/savable_spec.rb +2 -2
  241. data/spec/mongoid/persistable/settable_spec.rb +95 -10
  242. data/spec/mongoid/persistable_spec.rb +21 -6
  243. data/spec/mongoid/persistence_context_spec.rb +1 -1
  244. data/spec/mongoid/query_cache_spec.rb +61 -22
  245. data/spec/mongoid/relations/proxy_spec.rb +124 -124
  246. data/spec/mongoid/scopable_spec.rb +13 -0
  247. data/spec/mongoid/shardable_spec.rb +32 -12
  248. data/spec/mongoid/threaded_spec.rb +68 -0
  249. data/spec/mongoid/{relations/touchable_spec.rb → touchable_spec.rb} +40 -1
  250. data/spec/mongoid/validatable/associated_spec.rb +1 -1
  251. data/spec/mongoid/validatable/presence_spec.rb +7 -6
  252. data/spec/mongoid/validatable_spec.rb +1 -1
  253. data/spec/rails/controller_extension/controller_runtime_spec.rb +110 -0
  254. data/spec/spec_helper.rb +51 -25
  255. data/spec/support/constraints.rb +101 -0
  256. data/spec/support/macros.rb +20 -0
  257. data/spec/support/spec_config.rb +39 -0
  258. metadata +602 -582
  259. checksums.yaml.gz.sig +0 -2
  260. data/lib/mongoid/relations/accessors.rb +0 -267
  261. data/lib/mongoid/relations/auto_save.rb +0 -94
  262. data/lib/mongoid/relations/bindings/embedded/in.rb +0 -59
  263. data/lib/mongoid/relations/bindings/referenced/in.rb +0 -65
  264. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +0 -70
  265. data/lib/mongoid/relations/bindings.rb +0 -9
  266. data/lib/mongoid/relations/builder.rb +0 -57
  267. data/lib/mongoid/relations/builders/nested_attributes/many.rb +0 -199
  268. data/lib/mongoid/relations/builders/referenced/in.rb +0 -26
  269. data/lib/mongoid/relations/builders/referenced/many.rb +0 -26
  270. data/lib/mongoid/relations/builders/referenced/many_to_many.rb +0 -39
  271. data/lib/mongoid/relations/builders/referenced/one.rb +0 -26
  272. data/lib/mongoid/relations/builders.rb +0 -106
  273. data/lib/mongoid/relations/cascading/delete.rb +0 -44
  274. data/lib/mongoid/relations/cascading/destroy.rb +0 -43
  275. data/lib/mongoid/relations/cascading/nullify.rb +0 -35
  276. data/lib/mongoid/relations/cascading/restrict.rb +0 -39
  277. data/lib/mongoid/relations/cascading.rb +0 -56
  278. data/lib/mongoid/relations/conversions.rb +0 -34
  279. data/lib/mongoid/relations/counter_cache.rb +0 -160
  280. data/lib/mongoid/relations/cyclic.rb +0 -107
  281. data/lib/mongoid/relations/eager/base.rb +0 -153
  282. data/lib/mongoid/relations/eager/belongs_to.rb +0 -31
  283. data/lib/mongoid/relations/eager/has_and_belongs_to_many.rb +0 -47
  284. data/lib/mongoid/relations/eager/has_many.rb +0 -38
  285. data/lib/mongoid/relations/eager/has_one.rb +0 -30
  286. data/lib/mongoid/relations/embedded/in.rb +0 -241
  287. data/lib/mongoid/relations/embedded/many.rb +0 -683
  288. data/lib/mongoid/relations/embedded/one.rb +0 -235
  289. data/lib/mongoid/relations/macros.rb +0 -367
  290. data/lib/mongoid/relations/metadata.rb +0 -1179
  291. data/lib/mongoid/relations/nested_builder.rb +0 -74
  292. data/lib/mongoid/relations/options.rb +0 -49
  293. data/lib/mongoid/relations/polymorphic.rb +0 -39
  294. data/lib/mongoid/relations/referenced/in.rb +0 -304
  295. data/lib/mongoid/relations/referenced/many.rb +0 -812
  296. data/lib/mongoid/relations/referenced/many_to_many.rb +0 -479
  297. data/lib/mongoid/relations/referenced/one.rb +0 -290
  298. data/lib/mongoid/relations/synchronization.rb +0 -169
  299. data/lib/mongoid/relations/targets/enumerable.rb +0 -493
  300. data/lib/mongoid/relations/targets.rb +0 -2
  301. data/lib/mongoid/relations/touchable.rb +0 -97
  302. data/spec/mongoid/fields/internal/foreign_keys/array_spec.rb +0 -184
  303. data/spec/mongoid/fields/internal/foreign_keys/object_spec.rb +0 -201
  304. data/spec/mongoid/relations/builders/referenced/many_spec.rb +0 -137
  305. data/spec/mongoid/relations/builders/referenced/many_to_many_spec.rb +0 -178
  306. data/spec/mongoid/relations/builders/referenced/one_spec.rb +0 -111
  307. data/spec/mongoid/relations/cascading/delete_spec.rb +0 -101
  308. data/spec/mongoid/relations/cascading/destroy_spec.rb +0 -47
  309. data/spec/mongoid/relations/cascading/nullify_spec.rb +0 -32
  310. data/spec/mongoid/relations/cascading/restrict_spec.rb +0 -68
  311. data/spec/mongoid/relations/cascading_spec.rb +0 -355
  312. data/spec/mongoid/relations/constraint_spec.rb +0 -75
  313. data/spec/mongoid/relations/conversions_spec.rb +0 -128
  314. data/spec/mongoid/relations/metadata_spec.rb +0 -1985
  315. data/spec/mongoid/relations/options_spec.rb +0 -35
  316. data.tar.gz.sig +0 -0
  317. metadata.gz.sig +0 -0
  318. /data/spec/mongoid/{relations → association}/embedded/dirty_spec.rb +0 -0
@@ -0,0 +1,1321 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Association::Options do
4
+
5
+ STORES_FOREIGN_KEY =
6
+ [
7
+ Mongoid::Association::Referenced::HasAndBelongsToMany,
8
+ Mongoid::Association::Referenced::BelongsTo
9
+ ]
10
+
11
+ [
12
+ Mongoid::Association::Embedded::EmbeddedIn,
13
+ Mongoid::Association::Embedded::EmbedsMany,
14
+ Mongoid::Association::Embedded::EmbedsOne,
15
+ Mongoid::Association::Referenced::BelongsTo,
16
+ Mongoid::Association::Referenced::HasMany,
17
+ Mongoid::Association::Referenced::HasOne,
18
+ Mongoid::Association::Referenced::HasAndBelongsToMany
19
+ ].each do |association_class|
20
+
21
+ context "when the association type is #{association_class}" do
22
+
23
+ let(:class_left) do
24
+ class ClassLeft; include Mongoid::Document; end
25
+ ClassLeft
26
+ end
27
+
28
+ let(:class_right) do
29
+ class ClassRight; include Mongoid::Document; end
30
+ ClassRight
31
+ end
32
+
33
+ let(:association) do
34
+ association_class.new(class_left, :name, options)
35
+ end
36
+
37
+ let(:options) do
38
+ {}
39
+ end
40
+
41
+ describe 'the :as option' do
42
+
43
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:as) do
44
+
45
+ context 'when :as is in the options' do
46
+
47
+ let(:options) do
48
+ {
49
+ as: :nameable
50
+ }
51
+ end
52
+
53
+ it 'returns the :as value' do
54
+ expect(association.as).to eq(:nameable)
55
+ end
56
+ end
57
+
58
+ context 'when :as is not in the options' do
59
+
60
+ it 'returns nil' do
61
+ expect(association.as).to be_nil
62
+ end
63
+ end
64
+ end
65
+
66
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:as) do
67
+
68
+ it 'returns nil' do
69
+ expect(association.as).to be_nil
70
+ end
71
+
72
+ context 'when the option is provided' do
73
+
74
+ let(:options) do
75
+ {
76
+ as: :nameable
77
+ }
78
+ end
79
+
80
+ it 'raises a Mongoid::Errors::InvalidRelationOption error' do
81
+ expect {
82
+ association
83
+ }.to raise_exception(Mongoid::Errors::InvalidRelationOption)
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+ describe '#autobuilding?' do
90
+
91
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:autobuild) do
92
+
93
+ context 'when :autobuild is in the options' do
94
+
95
+ context 'when :autobuild is true' do
96
+
97
+ let(:options) do
98
+ {
99
+ autobuild: true
100
+ }
101
+ end
102
+
103
+ it 'returns true' do
104
+ expect(association.autobuilding?).to be(true)
105
+ end
106
+ end
107
+
108
+ context 'when :autobuild is false' do
109
+
110
+ let(:options) do
111
+ {
112
+ autobuild: false
113
+ }
114
+ end
115
+
116
+ it 'returns false' do
117
+ expect(association.autobuilding?).to be(false)
118
+ end
119
+ end
120
+
121
+ context 'when :autobuild is nil' do
122
+
123
+ let(:options) do
124
+ {
125
+ autobuild: nil
126
+ }
127
+ end
128
+
129
+ it 'returns false' do
130
+ expect(association.autobuilding?).to be(false)
131
+ end
132
+ end
133
+ end
134
+
135
+ context 'when :autobuild is not in the options' do
136
+
137
+ it 'returns false' do
138
+ expect(association.autobuilding?).to be(false)
139
+ end
140
+ end
141
+ end
142
+
143
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:autobuild) do
144
+
145
+ it 'returns false' do
146
+ expect(association.autobuilding?).to be(false)
147
+ end
148
+
149
+ context 'when the option is provided' do
150
+
151
+ let(:options) do
152
+ {
153
+ autobuild: false
154
+ }
155
+ end
156
+
157
+ it 'raises a Mongoid::Errors::InvalidRelationOption error' do
158
+ expect {
159
+ association
160
+ }.to raise_exception(Mongoid::Errors::InvalidRelationOption)
161
+ end
162
+ end
163
+ end
164
+ end
165
+
166
+ describe '#autosave?' do
167
+
168
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:autosave) do
169
+
170
+ context 'when :autosave is in the options' do
171
+
172
+ context 'when :autosave is true' do
173
+
174
+ let(:options) do
175
+ {
176
+ autosave: true
177
+ }
178
+ end
179
+
180
+ it 'returns true' do
181
+ expect(association.autosave?).to be(true)
182
+ end
183
+ end
184
+
185
+ context 'when :autosave is false' do
186
+
187
+ let(:options) do
188
+ {
189
+ autosave: false
190
+ }
191
+ end
192
+
193
+ it 'returns false' do
194
+ expect(association.autosave?).to be(false)
195
+ end
196
+ end
197
+
198
+ context 'when :autosave is nil' do
199
+
200
+ let(:options) do
201
+ {
202
+ autosave: nil
203
+ }
204
+ end
205
+
206
+ it 'returns false' do
207
+ expect(association.autosave?).to be(false)
208
+ end
209
+ end
210
+ end
211
+
212
+ context 'when :autosave is not in the options' do
213
+
214
+ it 'returns false' do
215
+ expect(association.autosave?).to be(false)
216
+ end
217
+ end
218
+ end
219
+
220
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:autosave) do
221
+
222
+ it 'returns false' do
223
+ expect(association.autosave?).to be(false)
224
+ end
225
+
226
+ context 'when the option is provided' do
227
+
228
+ let(:options) do
229
+ {
230
+ autosave: false
231
+ }
232
+ end
233
+
234
+ it 'raises a Mongoid::Errors::InvalidRelationOption error' do
235
+ expect {
236
+ association
237
+ }.to raise_exception(Mongoid::Errors::InvalidRelationOption)
238
+ end
239
+ end
240
+ end
241
+ end
242
+
243
+ describe 'the :dependent option' do
244
+
245
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:dependent) do
246
+
247
+ context 'when :dependent is in the options' do
248
+
249
+ let(:options) do
250
+ {
251
+ dependent: :delete_all
252
+ }
253
+ end
254
+
255
+ it 'returns the :dependent value' do
256
+ expect(association.dependent).to eq(options[:dependent])
257
+ end
258
+ end
259
+
260
+ context 'when :dependent is not in the options' do
261
+
262
+ it 'returns nil' do
263
+ expect(association.dependent).to be_nil
264
+ end
265
+ end
266
+ end
267
+
268
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:dependent) do
269
+
270
+ it 'returns nil' do
271
+ expect(association.dependent).to be_nil
272
+ end
273
+
274
+ context 'when the option is provided' do
275
+
276
+ let(:options) do
277
+ {
278
+ dependent: :delete_all
279
+ }
280
+ end
281
+
282
+ it 'raises a Mongoid::Errors::InvalidRelationOption error' do
283
+ expect {
284
+ association
285
+ }.to raise_exception(Mongoid::Errors::InvalidRelationOption)
286
+ end
287
+ end
288
+ end
289
+ end
290
+
291
+ describe 'the :foreign_key option' do
292
+
293
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:foreign_key) do
294
+
295
+ context 'when the :foreign_key option is specified' do
296
+
297
+ let(:options) do
298
+ {
299
+ foreign_key: :some_field
300
+ }
301
+ end
302
+
303
+ it 'returns the foreign_key as a String' do
304
+ expect(association.foreign_key).to eq(options[:foreign_key].to_s)
305
+ end
306
+ end
307
+
308
+ context 'when the association stores the foreign key', if: association_class::VALID_OPTIONS.include?(:foreign_key) &&
309
+ STORES_FOREIGN_KEY.include?(association_class) do
310
+
311
+ context 'when :foreign_key option is not specified' do
312
+
313
+ it 'returns the name followed by the foreign_key_suffix' do
314
+ expect(association.foreign_key).to eq("name#{association.class::FOREIGN_KEY_SUFFIX}")
315
+ end
316
+ end
317
+ end
318
+
319
+ context 'when the association does not store the foreign key', if: association_class::VALID_OPTIONS.include?(:foreign_key) &&
320
+ !STORES_FOREIGN_KEY.include?(association_class) do
321
+
322
+ context 'when :foreign_key option is not specified' do
323
+
324
+ before do
325
+ allow(association).to receive(:inverse).and_return(:other)
326
+ end
327
+
328
+ it 'returns the inverse name followed by the foreign_key_suffix' do
329
+ expect(association.foreign_key).to eq("other#{association.class::FOREIGN_KEY_SUFFIX}")
330
+ end
331
+ end
332
+ end
333
+ end
334
+
335
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:foreign_key) do
336
+
337
+ it 'returns nil' do
338
+ expect(association.dependent).to be_nil
339
+ end
340
+
341
+ context 'when the option is provided' do
342
+
343
+ let(:options) do
344
+ {
345
+ foreign_key: :some_field
346
+ }
347
+ end
348
+
349
+ it 'raises a Mongoid::Errors::InvalidRelationOption error' do
350
+ expect {
351
+ association
352
+ }.to raise_exception(Mongoid::Errors::InvalidRelationOption)
353
+ end
354
+ end
355
+ end
356
+ end
357
+
358
+ describe 'the :primary_key option' do
359
+
360
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:primary_key) do
361
+
362
+ context 'when the option is specified' do
363
+
364
+ let(:options) do
365
+ {
366
+ primary_key: :other_id
367
+ }
368
+ end
369
+
370
+ it 'returns the primary_key from the options as a String' do
371
+ expect(association.primary_key).to eq(options[:primary_key].to_s)
372
+ end
373
+ end
374
+
375
+ context 'when the option is not specified' do
376
+
377
+ it 'returns the default primary key' do
378
+ expect(association.primary_key).to eq(Mongoid::Association::Relatable::PRIMARY_KEY_DEFAULT)
379
+ end
380
+ end
381
+ end
382
+
383
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:primary_key) do
384
+
385
+ it 'returns nil' do
386
+ expect(association.primary_key).to be_nil
387
+ end
388
+
389
+ context 'when the option is provided' do
390
+
391
+ let(:options) do
392
+ {
393
+ primary_key: :other_id
394
+ }
395
+ end
396
+
397
+ it 'raises a Mongoid::Errors::InvalidRelationOption error' do
398
+ expect {
399
+ association
400
+ }.to raise_exception(Mongoid::Errors::InvalidRelationOption)
401
+ end
402
+ end
403
+ end
404
+ end
405
+
406
+ describe 'the :after_add option' do
407
+
408
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:after_add) do
409
+
410
+ context 'when the :after_add option is specified' do
411
+
412
+ let(:options) do
413
+ {
414
+ after_add: :method_name
415
+ }
416
+ end
417
+
418
+ it 'retrieves the after_add method name from the association' do
419
+ expect(association.get_callbacks(:after_add)).to eq(Array(options[:after_add]))
420
+ end
421
+ end
422
+
423
+ context 'when the :after_add option is not specified' do
424
+
425
+ it 'returns nil' do
426
+ expect(association.get_callbacks(:after_add)).to be_empty
427
+ end
428
+ end
429
+ end
430
+
431
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:after_add) do
432
+
433
+
434
+ it 'returns nil' do
435
+ expect(association.get_callbacks(:after_add)).to be_empty
436
+ end
437
+
438
+ context 'when the option is provided' do
439
+
440
+ let(:options) do
441
+ {
442
+ after_add: :method_name
443
+ }
444
+ end
445
+
446
+ it 'raises a Mongoid::Errors::InvalidRelationOption error' do
447
+ expect {
448
+ association
449
+ }.to raise_exception(Mongoid::Errors::InvalidRelationOption)
450
+ end
451
+ end
452
+ end
453
+ end
454
+
455
+ describe 'the :after_remove option' do
456
+
457
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:after_remove) do
458
+
459
+ context 'when the :after_remove option is specified' do
460
+
461
+ let(:options) do
462
+ {
463
+ after_remove: :method_name
464
+ }
465
+ end
466
+
467
+ it 'retrieves the after_remove method name from the association' do
468
+ expect(association.get_callbacks(:after_remove)).to eq(Array(options[:after_remove]))
469
+ end
470
+ end
471
+
472
+ context 'when the :after_remove option is not specified' do
473
+
474
+ it 'returns nil' do
475
+ expect(association.get_callbacks(:after_remove)).to be_empty
476
+ end
477
+ end
478
+ end
479
+
480
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:after_remove) do
481
+
482
+
483
+ it 'returns nil' do
484
+ expect(association.get_callbacks(:after_remove)).to be_empty
485
+ end
486
+
487
+ context 'when the option is provided' do
488
+
489
+ let(:options) do
490
+ {
491
+ after_remove: :method_name
492
+ }
493
+ end
494
+
495
+ it 'raises a Mongoid::Errors::InvalidRelationOption error' do
496
+ expect {
497
+ association
498
+ }.to raise_exception(Mongoid::Errors::InvalidRelationOption)
499
+ end
500
+ end
501
+ end
502
+ end
503
+
504
+ describe 'the :before_add option' do
505
+
506
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:before_add) do
507
+
508
+ context 'when the :before_add option is specified' do
509
+
510
+ let(:options) do
511
+ {
512
+ before_add: :method_name
513
+ }
514
+ end
515
+
516
+ it 'retrieves the before_add method name from the association' do
517
+ expect(association.get_callbacks(:before_add)).to eq(Array(options[:before_add]))
518
+ end
519
+ end
520
+
521
+ context 'when the :before_add option is not specified' do
522
+
523
+ it 'returns nil' do
524
+ expect(association.get_callbacks(:before_add)).to be_empty
525
+ end
526
+ end
527
+ end
528
+
529
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:before_add) do
530
+
531
+
532
+ it 'returns nil' do
533
+ expect(association.get_callbacks(:before_add)).to be_empty
534
+ end
535
+
536
+ context 'when the option is provided' do
537
+
538
+ let(:options) do
539
+ {
540
+ before_add: :method_name
541
+ }
542
+ end
543
+
544
+ it 'raises a Mongoid::Errors::InvalidRelationOption error' do
545
+ expect {
546
+ association
547
+ }.to raise_exception(Mongoid::Errors::InvalidRelationOption)
548
+ end
549
+ end
550
+ end
551
+ end
552
+
553
+ describe 'the :before_remove option' do
554
+
555
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:before_remove) do
556
+
557
+ context 'when the :before_remove option is specified' do
558
+
559
+ let(:options) do
560
+ {
561
+ before_remove: :method_name
562
+ }
563
+ end
564
+
565
+ it 'retrieves the before_remove method name from the association' do
566
+ expect(association.get_callbacks(:before_remove)).to eq(Array(options[:before_remove]))
567
+ end
568
+ end
569
+
570
+ context 'when the :before_remove option is not specified' do
571
+
572
+ it 'returns nil' do
573
+ expect(association.get_callbacks(:before_remove)).to be_empty
574
+ end
575
+ end
576
+ end
577
+
578
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:before_remove) do
579
+
580
+
581
+ it 'returns nil' do
582
+ expect(association.get_callbacks(:before_remove)).to be_empty
583
+ end
584
+
585
+ context 'when the option is provided' do
586
+
587
+ let(:options) do
588
+ {
589
+ before_remove: :method_name
590
+ }
591
+ end
592
+
593
+ it 'raises a Mongoid::Errors::InvalidRelationOption error' do
594
+ expect {
595
+ association
596
+ }.to raise_exception(Mongoid::Errors::InvalidRelationOption)
597
+ end
598
+ end
599
+ end
600
+ end
601
+
602
+
603
+ describe '#indexed?' do
604
+
605
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:index) do
606
+
607
+ context 'when :index is in the options' do
608
+
609
+ context 'when :index is true' do
610
+
611
+ let(:options) do
612
+ {
613
+ index: true
614
+ }
615
+ end
616
+
617
+ it 'returns true' do
618
+ expect(association.indexed?).to be(true)
619
+ end
620
+ end
621
+
622
+ context 'when :index is false' do
623
+
624
+ let(:options) do
625
+ {
626
+ index: false
627
+ }
628
+ end
629
+
630
+ it 'returns false' do
631
+ expect(association.indexed?).to be(false)
632
+ end
633
+ end
634
+
635
+ context 'when :index is nil' do
636
+
637
+ let(:options) do
638
+ {
639
+ index: nil
640
+ }
641
+ end
642
+
643
+ it 'returns false' do
644
+ expect(association.indexed?).to be(false)
645
+ end
646
+ end
647
+ end
648
+
649
+ context 'when :index is not in the options' do
650
+
651
+ it 'returns false' do
652
+ expect(association.indexed?).to be(false)
653
+ end
654
+ end
655
+ end
656
+
657
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:index) do
658
+
659
+ it 'returns false' do
660
+ expect(association.indexed?).to be(false)
661
+ end
662
+
663
+ context 'when the option is provided' do
664
+
665
+ let(:options) do
666
+ {
667
+ index: true
668
+ }
669
+ end
670
+
671
+ it 'raises a Mongoid::Errors::InvalidRelationOption error' do
672
+ expect {
673
+ association
674
+ }.to raise_exception(Mongoid::Errors::InvalidRelationOption)
675
+ end
676
+ end
677
+ end
678
+ end
679
+
680
+ describe 'the :order option' do
681
+
682
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:order) do
683
+
684
+ context 'when :order is in the options' do
685
+
686
+ let(:options) do
687
+ {
688
+ order: :rating.desc
689
+ }
690
+ end
691
+
692
+ it 'returns a Mongoid::Criteria::Queryable::Key' do
693
+ expect(association.order).to be_a(Mongoid::Criteria::Queryable::Key)
694
+ end
695
+ end
696
+
697
+ context 'when :order is not in the options' do
698
+
699
+ it 'returns nil' do
700
+ expect(association.order).to be_nil
701
+ end
702
+ end
703
+ end
704
+
705
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:order) do
706
+
707
+ it 'returns nil' do
708
+ expect(association.order).to be_nil
709
+ end
710
+
711
+ context 'when the option is provided' do
712
+
713
+ let(:options) do
714
+ {
715
+ order: :rating.desc
716
+ }
717
+ end
718
+
719
+ it 'raises a Mongoid::Errors::InvalidRelationOption error' do
720
+ expect {
721
+ association
722
+ }.to raise_exception(Mongoid::Errors::InvalidRelationOption)
723
+ end
724
+ end
725
+ end
726
+ end
727
+
728
+ describe '#polymorphic' do
729
+
730
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:polymorphic) do
731
+
732
+ context 'when :polymorphic is in the options' do
733
+
734
+ context 'when :polymorphic is true' do
735
+
736
+ let(:options) do
737
+ {
738
+ polymorphic: true
739
+ }
740
+ end
741
+
742
+ it 'returns true' do
743
+ expect(association.polymorphic?).to be(true)
744
+ end
745
+ end
746
+
747
+ context 'when :polymorphic is false' do
748
+
749
+ let(:options) do
750
+ {
751
+ polymorphic: false
752
+ }
753
+ end
754
+
755
+ it 'returns false' do
756
+ expect(association.polymorphic?).to be(false)
757
+ end
758
+ end
759
+
760
+ context 'when :polymorphic is nil' do
761
+
762
+ let(:options) do
763
+ {
764
+ polymorphic: nil
765
+ }
766
+ end
767
+
768
+ it 'returns false' do
769
+ expect(association.polymorphic?).to be(false)
770
+ end
771
+ end
772
+ end
773
+
774
+ context 'when :polymorphic is not in the options' do
775
+
776
+ it 'returns false' do
777
+ expect(association.polymorphic?).to be(false)
778
+ end
779
+ end
780
+ end
781
+
782
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:polymorphic) do
783
+
784
+ it 'returns false' do
785
+ expect(association.polymorphic?).to be(false)
786
+ end
787
+
788
+ context 'when the option is provided' do
789
+
790
+ let(:options) do
791
+ {
792
+ polymorphic: true
793
+ }
794
+ end
795
+
796
+ it 'raises a Mongoid::Errors::InvalidRelationOption error' do
797
+ expect {
798
+ association
799
+ }.to raise_exception(Mongoid::Errors::InvalidRelationOption)
800
+ end
801
+ end
802
+ end
803
+ end
804
+
805
+ describe '#cascading_callbacks?' do
806
+
807
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:cascade_callbacks) do
808
+
809
+ context 'when :cascade_callbacks is in the options' do
810
+
811
+ context 'when :cascade_callbacks is true' do
812
+
813
+ let(:options) do
814
+ {
815
+ cascade_callbacks: true
816
+ }
817
+ end
818
+
819
+ it 'returns true' do
820
+ expect(association.cascading_callbacks?).to be(true)
821
+ end
822
+ end
823
+
824
+ context 'when :cascade_callbacks is false' do
825
+
826
+ let(:options) do
827
+ {
828
+ cascade_callbacks: false
829
+ }
830
+ end
831
+
832
+ it 'returns false' do
833
+ expect(association.cascading_callbacks?).to be(false)
834
+ end
835
+ end
836
+
837
+ context 'when :cascade_callbacks is nil' do
838
+
839
+ let(:options) do
840
+ {
841
+ cascade_callbacks: nil
842
+ }
843
+ end
844
+
845
+ it 'returns false' do
846
+ expect(association.cascading_callbacks?).to be(false)
847
+ end
848
+ end
849
+ end
850
+
851
+ context 'when :cascade_callbacks is not in the options' do
852
+
853
+ it 'returns false' do
854
+ expect(association.cascading_callbacks?).to be(false)
855
+ end
856
+ end
857
+ end
858
+
859
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:cascade_callbacks) do
860
+
861
+ it 'returns false' do
862
+ expect(association.cascading_callbacks?).to be(false)
863
+ end
864
+
865
+ context 'when the option is provided' do
866
+
867
+ let(:options) do
868
+ {
869
+ cascade_callbacks: true
870
+ }
871
+ end
872
+
873
+ it 'raises a Mongoid::Errors::InvalidRelationOption error' do
874
+ expect {
875
+ association
876
+ }.to raise_exception(Mongoid::Errors::InvalidRelationOption)
877
+ end
878
+ end
879
+ end
880
+ end
881
+
882
+ describe '#cyclic?' do
883
+
884
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:cyclic) do
885
+
886
+ context 'when :cyclic is in the options' do
887
+
888
+ context 'when :cyclic is true' do
889
+
890
+ let(:options) do
891
+ {
892
+ cyclic: true
893
+ }
894
+ end
895
+
896
+ it 'returns true' do
897
+ expect(association.cyclic?).to be(true)
898
+ end
899
+ end
900
+
901
+ context 'when :cyclic is false' do
902
+
903
+ let(:options) do
904
+ {
905
+ cyclic: false
906
+ }
907
+ end
908
+
909
+ it 'returns false' do
910
+ expect(association.cyclic?).to be(false)
911
+ end
912
+ end
913
+
914
+ context 'when :cyclic is nil' do
915
+
916
+ let(:options) do
917
+ {
918
+ cyclic: nil
919
+ }
920
+ end
921
+
922
+ it 'returns false' do
923
+ expect(association.cyclic?).to be(false)
924
+ end
925
+ end
926
+ end
927
+
928
+ context 'when :cyclic is not in the options' do
929
+
930
+ it 'returns false' do
931
+ expect(association.cyclic?).to be(false)
932
+ end
933
+ end
934
+ end
935
+
936
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:cyclic) do
937
+
938
+ it 'returns false' do
939
+ expect(association.cyclic?).to be(false)
940
+ end
941
+
942
+ context 'when the option is provided' do
943
+
944
+ let(:options) do
945
+ {
946
+ cyclic: true
947
+ }
948
+ end
949
+
950
+ it 'raises a Mongoid::Errors::InvalidRelationOption error' do
951
+ expect {
952
+ association
953
+ }.to raise_exception(Mongoid::Errors::InvalidRelationOption)
954
+ end
955
+ end
956
+ end
957
+ end
958
+
959
+ describe 'the :store_as option' do
960
+
961
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:store_as) do
962
+
963
+ context 'when :store_as is in the options' do
964
+
965
+ let(:options) do
966
+ {
967
+ store_as: :another_name
968
+ }
969
+ end
970
+
971
+ it 'returns the :store_as value' do
972
+ expect(association.store_as).to eq(options[:store_as].to_s)
973
+ end
974
+ end
975
+
976
+ context 'when :store_as is not in the options' do
977
+
978
+ it 'returns the name as a string' do
979
+ expect(association.store_as).to eq("name")
980
+ end
981
+ end
982
+ end
983
+
984
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:store_as) do
985
+
986
+ it 'returns nil' do
987
+ expect(association.store_as).to be_nil
988
+ end
989
+
990
+ context 'when the option is provided' do
991
+
992
+ let(:options) do
993
+ {
994
+ store_as: :another_name
995
+ }
996
+ end
997
+
998
+ it 'raises a Mongoid::Errors::InvalidRelationOption error' do
999
+ expect {
1000
+ association
1001
+ }.to raise_exception(Mongoid::Errors::InvalidRelationOption)
1002
+ end
1003
+ end
1004
+ end
1005
+ end
1006
+
1007
+ describe 'the :class_name option' do
1008
+
1009
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:class_name) do
1010
+
1011
+ context 'when :class_name is in the options' do
1012
+
1013
+ let(:options) do
1014
+ {
1015
+ class_name: :a_class
1016
+ }
1017
+ end
1018
+
1019
+ it 'returns the :class_name value' do
1020
+ expect(association.class_name).to eq(:a_class)
1021
+ end
1022
+ end
1023
+
1024
+ context 'when :class_name is not in the options' do
1025
+
1026
+ it 'returns the name deduced from the association name' do
1027
+ expect(association.class_name).to eq(ActiveSupport::Inflector.classify(:name))
1028
+ end
1029
+ end
1030
+ end
1031
+
1032
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:class_name) do
1033
+
1034
+ it 'returns the name deduced from the association name' do
1035
+ expect(association.class_name).to eq(ActiveSupport::Inflector.classify(:name))
1036
+ end
1037
+
1038
+ context 'when the option is provided' do
1039
+
1040
+ let(:options) do
1041
+ {
1042
+ class_name: :a_class
1043
+ }
1044
+ end
1045
+
1046
+ it 'should support the option' do
1047
+ fail('All association types should support this option')
1048
+ end
1049
+ end
1050
+ end
1051
+ end
1052
+
1053
+ describe '#counter_cached?' do
1054
+
1055
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:counter_cache) do
1056
+
1057
+ context 'when :counter_cache is in the options' do
1058
+
1059
+ context 'when :counter_cache is true' do
1060
+
1061
+ let(:options) do
1062
+ {
1063
+ counter_cache: true
1064
+ }
1065
+ end
1066
+
1067
+ it 'returns true' do
1068
+ expect(association.counter_cached?).to be(true)
1069
+ end
1070
+ end
1071
+
1072
+ context 'when :counter_cache is false' do
1073
+
1074
+ let(:options) do
1075
+ {
1076
+ counter_cache: false
1077
+ }
1078
+ end
1079
+
1080
+ it 'returns false' do
1081
+ expect(association.counter_cached?).to be(false)
1082
+ end
1083
+ end
1084
+
1085
+ context 'when :counter_cache is nil' do
1086
+
1087
+ let(:options) do
1088
+ {
1089
+ counter_cache: nil
1090
+ }
1091
+ end
1092
+
1093
+ it 'returns false' do
1094
+ expect(association.counter_cached?).to be(false)
1095
+ end
1096
+ end
1097
+ end
1098
+
1099
+ context 'when :counter_cache is not in the options' do
1100
+
1101
+ it 'returns false' do
1102
+ expect(association.counter_cached?).to be(false)
1103
+ end
1104
+ end
1105
+ end
1106
+
1107
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:counter_cache) do
1108
+
1109
+ it 'returns false' do
1110
+ expect(association.counter_cached?).to be(false)
1111
+ end
1112
+
1113
+ context 'when the option is provided' do
1114
+
1115
+ let(:options) do
1116
+ {
1117
+ counter_cache: :column_name
1118
+ }
1119
+ end
1120
+
1121
+ it 'raises a Mongoid::Errors::InvalidRelationOption error' do
1122
+ expect {
1123
+ association
1124
+ }.to raise_exception(Mongoid::Errors::InvalidRelationOption)
1125
+ end
1126
+ end
1127
+ end
1128
+ end
1129
+
1130
+ describe '#extension' do
1131
+
1132
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:extend) do
1133
+
1134
+ context 'when a block is passed' do
1135
+
1136
+ let!(:association) do
1137
+ association_class.new(class_left, :name, options) do; end
1138
+ end
1139
+
1140
+ after do
1141
+ Object.send(:remove_const, :ClassLeft)
1142
+ end
1143
+
1144
+ it 'defines an extension module' do
1145
+ expect(ClassLeft::ClassLeftNameRelationExtension).to be_a(Module)
1146
+ end
1147
+
1148
+ it 'returns the extension' do
1149
+ expect(association.extension).to eq(ClassLeft::ClassLeftNameRelationExtension)
1150
+ end
1151
+ end
1152
+
1153
+ context 'when a module name is passed' do
1154
+
1155
+ before do
1156
+ class ClassLeft; end
1157
+ module ClassLeft::Handle; end
1158
+ end
1159
+
1160
+ let(:options) do
1161
+ {
1162
+ extend: ClassLeft::Handle
1163
+ }
1164
+ end
1165
+
1166
+ let!(:association) do
1167
+ association_class.new(class_left, :name, options)
1168
+ end
1169
+
1170
+ it 'returns the extension' do
1171
+ expect(association.extension).to eq(ClassLeft::Handle)
1172
+ end
1173
+ end
1174
+
1175
+ context 'when a block is not passed' do
1176
+
1177
+ it 'does not define an extension module' do
1178
+ expect(defined?(ClassLeft::ClassLeftNameRelationExtension)).to be_nil
1179
+ end
1180
+
1181
+ it 'returns nil' do
1182
+ expect(association.extension).to be_nil
1183
+ end
1184
+ end
1185
+
1186
+
1187
+ end
1188
+
1189
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:extend) do
1190
+
1191
+ it 'should support the option' do
1192
+ fail('All association types should support this option')
1193
+ end
1194
+ end
1195
+ end
1196
+
1197
+ describe 'the :inverse_of option' do
1198
+
1199
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:inverse_of) do
1200
+
1201
+ context 'when :inverse_of is in the options' do
1202
+
1203
+ let(:options) do
1204
+ {
1205
+ inverse_of: :inverse_name
1206
+ }
1207
+ end
1208
+
1209
+ it 'returns the :inverse_of value' do
1210
+ expect(association.inverse_of).to eq(:inverse_name)
1211
+ end
1212
+ end
1213
+
1214
+ context 'when :inverse_of is not in the options' do
1215
+
1216
+ it 'returns nil' do
1217
+ expect(association.inverse_of).to be_nil
1218
+ end
1219
+ end
1220
+ end
1221
+
1222
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:inverse_of) do
1223
+
1224
+ it 'returns nil' do
1225
+ expect(association.inverse_of).to be_nil
1226
+ end
1227
+
1228
+
1229
+ context 'when the option is provided' do
1230
+
1231
+ let(:options) do
1232
+ {
1233
+ inverse_of: :inverse_name
1234
+ }
1235
+ end
1236
+
1237
+ it 'should support the option' do
1238
+ fail('All association types should support this option')
1239
+ end
1240
+ end
1241
+ end
1242
+ end
1243
+
1244
+ describe '#validate?' do
1245
+
1246
+ context 'when the option is supported by the association type', if: association_class::VALID_OPTIONS.include?(:validate) do
1247
+
1248
+ context 'when :validate is in the options' do
1249
+
1250
+ context 'when :validate is true' do
1251
+
1252
+ let(:options) do
1253
+ {
1254
+ validate: true
1255
+ }
1256
+ end
1257
+
1258
+ it 'returns true' do
1259
+ expect(association.send(:validate?)).to be(true)
1260
+ end
1261
+ end
1262
+
1263
+ context 'when :validate is false' do
1264
+
1265
+ let(:options) do
1266
+ {
1267
+ validate: false
1268
+ }
1269
+ end
1270
+
1271
+ it 'returns false' do
1272
+ expect(association.send(:validate?)).to be(false)
1273
+ end
1274
+ end
1275
+
1276
+ context 'when :validate is nil' do
1277
+
1278
+ let(:options) do
1279
+ {
1280
+ validate: nil
1281
+ }
1282
+ end
1283
+
1284
+ it 'returns the validation default' do
1285
+ expect(association.send(:validate?)).to be(association.validation_default)
1286
+ end
1287
+ end
1288
+ end
1289
+
1290
+ context 'when :validate is not in the options' do
1291
+
1292
+ it 'returns the validation default' do
1293
+ expect(association.send(:validate?)).to be(association.validation_default)
1294
+ end
1295
+ end
1296
+ end
1297
+
1298
+ context 'when the option is not supported by the association type', if: !association_class::VALID_OPTIONS.include?(:validate) do
1299
+
1300
+ it 'returns false' do
1301
+ expect(association.send(:validate?)).to be(false)
1302
+ end
1303
+
1304
+
1305
+ context 'when the option is provided' do
1306
+
1307
+ let(:options) do
1308
+ {
1309
+ validate: true
1310
+ }
1311
+ end
1312
+
1313
+ it 'should support the option' do
1314
+ fail('All association types should support this option')
1315
+ end
1316
+ end
1317
+ end
1318
+ end
1319
+ end
1320
+ end
1321
+ end