mongoid 6.4.8 → 7.0.0

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