mongoid 6.4.8 → 7.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (315) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/Rakefile +0 -26
  5. data/lib/config/locales/en.yml +17 -21
  6. data/lib/mongoid.rb +2 -2
  7. data/lib/mongoid/association.rb +150 -0
  8. data/lib/mongoid/association/accessors.rb +339 -0
  9. data/lib/mongoid/{relations/binding.rb → association/bindable.rb} +32 -52
  10. data/lib/mongoid/association/builders.rb +92 -0
  11. data/lib/mongoid/{relations/constraint.rb → association/constrainable.rb} +11 -22
  12. data/lib/mongoid/association/depending.rb +116 -0
  13. data/lib/mongoid/{relations/eager.rb → association/eager_loadable.rb} +11 -11
  14. data/lib/mongoid/association/embedded.rb +4 -0
  15. data/lib/mongoid/{relations → association}/embedded/batchable.rb +27 -53
  16. data/lib/mongoid/association/embedded/cyclic.rb +109 -0
  17. data/lib/mongoid/association/embedded/embedded_in.rb +154 -0
  18. data/lib/mongoid/association/embedded/embedded_in/binding.rb +56 -0
  19. data/lib/mongoid/{relations/builders/embedded/in.rb → association/embedded/embedded_in/buildable.rb} +12 -6
  20. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +121 -0
  21. data/lib/mongoid/association/embedded/embeds_many.rb +210 -0
  22. data/lib/mongoid/{relations/bindings/embedded/many.rb → association/embedded/embeds_many/binding.rb} +11 -9
  23. data/lib/mongoid/{relations/builders/embedded/many.rb → association/embedded/embeds_many/buildable.rb} +13 -7
  24. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +529 -0
  25. data/lib/mongoid/association/embedded/embeds_one.rb +173 -0
  26. data/lib/mongoid/{relations/bindings/embedded/one.rb → association/embedded/embeds_one/binding.rb} +12 -10
  27. data/lib/mongoid/{relations/builders/embedded/one.rb → association/embedded/embeds_one/buildable.rb} +13 -7
  28. data/lib/mongoid/association/embedded/embeds_one/proxy.rb +130 -0
  29. data/lib/mongoid/association/macros.rb +204 -0
  30. data/lib/mongoid/{relations → association}/many.rb +18 -52
  31. data/lib/mongoid/{relations → association}/marshalable.rb +6 -4
  32. data/lib/mongoid/association/nested.rb +15 -0
  33. data/lib/mongoid/association/nested/many.rb +200 -0
  34. data/lib/mongoid/association/nested/nested_buildable.rb +72 -0
  35. data/lib/mongoid/association/nested/one.rb +127 -0
  36. data/lib/mongoid/{relations → association}/one.rb +6 -6
  37. data/lib/mongoid/association/options.rb +152 -0
  38. data/lib/mongoid/{relations → association}/proxy.rb +31 -58
  39. data/lib/mongoid/association/referenced.rb +7 -0
  40. data/lib/mongoid/association/referenced/auto_save.rb +79 -0
  41. data/lib/mongoid/association/referenced/belongs_to.rb +248 -0
  42. data/lib/mongoid/association/referenced/belongs_to/binding.rb +87 -0
  43. data/lib/mongoid/association/referenced/belongs_to/buildable.rb +46 -0
  44. data/lib/mongoid/association/referenced/belongs_to/eager.rb +36 -0
  45. data/lib/mongoid/association/referenced/belongs_to/proxy.rb +136 -0
  46. data/lib/mongoid/association/referenced/counter_cache.rb +163 -0
  47. data/lib/mongoid/association/referenced/eager.rb +159 -0
  48. data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +290 -0
  49. data/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb +71 -0
  50. data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +40 -0
  51. data/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb +52 -0
  52. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +310 -0
  53. data/lib/mongoid/association/referenced/has_many.rb +273 -0
  54. data/lib/mongoid/{relations/bindings/referenced/many.rb → association/referenced/has_many/binding.rb} +6 -5
  55. data/lib/mongoid/association/referenced/has_many/buildable.rb +38 -0
  56. data/lib/mongoid/association/referenced/has_many/eager.rb +43 -0
  57. data/lib/mongoid/association/referenced/has_many/enumerable.rb +479 -0
  58. data/lib/mongoid/association/referenced/has_many/proxy.rb +577 -0
  59. data/lib/mongoid/association/referenced/has_one.rb +204 -0
  60. data/lib/mongoid/{relations/bindings/referenced/one.rb → association/referenced/has_one/binding.rb} +11 -8
  61. data/lib/mongoid/association/referenced/has_one/buildable.rb +60 -0
  62. data/lib/mongoid/association/referenced/has_one/eager.rb +35 -0
  63. data/lib/mongoid/{relations/builders/nested_attributes/one.rb → association/referenced/has_one/nested_builder.rb} +9 -9
  64. data/lib/mongoid/association/referenced/has_one/proxy.rb +113 -0
  65. data/lib/mongoid/association/referenced/syncable.rb +170 -0
  66. data/lib/mongoid/{relations → association}/reflections.rb +21 -17
  67. data/lib/mongoid/association/relatable.rb +415 -0
  68. data/lib/mongoid/association/touchable.rb +97 -0
  69. data/lib/mongoid/atomic.rb +6 -6
  70. data/lib/mongoid/atomic/modifiers.rb +8 -12
  71. data/lib/mongoid/atomic/paths/embedded/many.rb +1 -1
  72. data/lib/mongoid/atomic/paths/embedded/one.rb +1 -1
  73. data/lib/mongoid/attributes.rb +2 -1
  74. data/lib/mongoid/attributes/nested.rb +10 -10
  75. data/lib/mongoid/attributes/processing.rb +2 -2
  76. data/lib/mongoid/attributes/readonly.rb +2 -4
  77. data/lib/mongoid/clients.rb +0 -2
  78. data/lib/mongoid/clients/options.rb +1 -1
  79. data/lib/mongoid/clients/storage_options.rb +0 -1
  80. data/lib/mongoid/composable.rb +3 -4
  81. data/lib/mongoid/config.rb +1 -0
  82. data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
  83. data/lib/mongoid/contextual/atomic.rb +3 -6
  84. data/lib/mongoid/contextual/map_reduce.rb +3 -7
  85. data/lib/mongoid/contextual/memory.rb +5 -10
  86. data/lib/mongoid/contextual/mongo.rb +10 -27
  87. data/lib/mongoid/copyable.rb +6 -6
  88. data/lib/mongoid/criteria.rb +1 -2
  89. data/lib/mongoid/criteria/includable.rb +14 -14
  90. data/lib/mongoid/criteria/modifiable.rb +8 -14
  91. data/lib/mongoid/criteria/queryable/mergeable.rb +1 -3
  92. data/lib/mongoid/criteria/queryable/pipeline.rb +10 -5
  93. data/lib/mongoid/criteria/queryable/selectable.rb +10 -34
  94. data/lib/mongoid/document.rb +6 -6
  95. data/lib/mongoid/errors.rb +3 -1
  96. data/lib/mongoid/errors/invalid_dependent_strategy.rb +32 -0
  97. data/lib/mongoid/errors/invalid_relation_option.rb +29 -0
  98. data/lib/mongoid/errors/unknown_model.rb +25 -0
  99. data/lib/mongoid/extensions/array.rb +5 -5
  100. data/lib/mongoid/extensions/big_decimal.rb +1 -1
  101. data/lib/mongoid/extensions/object.rb +4 -4
  102. data/lib/mongoid/extensions/range.rb +1 -0
  103. data/lib/mongoid/extensions/regexp.rb +0 -1
  104. data/lib/mongoid/extensions/string.rb +1 -3
  105. data/lib/mongoid/factory.rb +4 -3
  106. data/lib/mongoid/fields.rb +1 -1
  107. data/lib/mongoid/fields/foreign_key.rb +5 -5
  108. data/lib/mongoid/fields/standard.rb +2 -14
  109. data/lib/mongoid/fields/validators/macro.rb +1 -1
  110. data/lib/mongoid/indexable.rb +8 -5
  111. data/lib/mongoid/interceptable.rb +5 -5
  112. data/lib/mongoid/matchable.rb +0 -3
  113. data/lib/mongoid/persistable.rb +4 -5
  114. data/lib/mongoid/persistable/creatable.rb +2 -4
  115. data/lib/mongoid/persistable/deletable.rb +9 -10
  116. data/lib/mongoid/persistable/destroyable.rb +5 -1
  117. data/lib/mongoid/persistable/incrementable.rb +1 -1
  118. data/lib/mongoid/persistable/logical.rb +1 -1
  119. data/lib/mongoid/persistable/settable.rb +5 -5
  120. data/lib/mongoid/persistable/updatable.rb +2 -2
  121. data/lib/mongoid/persistable/upsertable.rb +1 -2
  122. data/lib/mongoid/persistence_context.rb +4 -9
  123. data/lib/mongoid/query_cache.rb +18 -65
  124. data/lib/mongoid/railtie.rb +0 -17
  125. data/lib/mongoid/reloadable.rb +1 -1
  126. data/lib/mongoid/scopable.rb +3 -3
  127. data/lib/mongoid/serializable.rb +3 -3
  128. data/lib/mongoid/tasks/database.rb +2 -3
  129. data/lib/mongoid/threaded.rb +0 -74
  130. data/lib/mongoid/traversable.rb +2 -2
  131. data/lib/mongoid/validatable.rb +8 -8
  132. data/lib/mongoid/validatable/presence.rb +2 -2
  133. data/lib/mongoid/validatable/uniqueness.rb +4 -4
  134. data/lib/mongoid/version.rb +1 -1
  135. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +3 -4
  136. data/spec/app/models/animal.rb +2 -1
  137. data/spec/app/models/band.rb +0 -1
  138. data/spec/app/models/bomb.rb +1 -1
  139. data/spec/app/models/message.rb +1 -1
  140. data/spec/app/models/person.rb +5 -2
  141. data/spec/app/models/vertex.rb +6 -0
  142. data/spec/app/models/wiki_page.rb +1 -1
  143. data/spec/config/mongoid.yml +1 -0
  144. data/spec/mongoid/{relations → association}/accessors_spec.rb +1 -1
  145. data/spec/mongoid/{relations → association}/auto_save_spec.rb +60 -12
  146. data/spec/mongoid/{relations → association}/builders_spec.rb +1 -1
  147. data/spec/mongoid/association/constrainable_spec.rb +115 -0
  148. data/spec/mongoid/{relations → association}/counter_cache_spec.rb +1 -1
  149. data/spec/mongoid/association/depending_spec.rb +613 -0
  150. data/spec/mongoid/{relations → association}/eager_spec.rb +12 -12
  151. data/spec/mongoid/{relations → association/embedded}/cyclic_spec.rb +1 -1
  152. data/spec/mongoid/{relations → association}/embedded/dirty_spec.rb +0 -0
  153. data/spec/mongoid/{relations/bindings/embedded/in_spec.rb → association/embedded/embedded_in/binding_spec.rb} +13 -13
  154. data/spec/mongoid/{relations/builders/embedded/in_spec.rb → association/embedded/embedded_in/buildable_spec.rb} +9 -9
  155. data/spec/mongoid/{relations/embedded/in_spec.rb → association/embedded/embedded_in/proxy_spec.rb} +5 -77
  156. data/spec/mongoid/association/embedded/embedded_in_spec.rb +843 -0
  157. data/spec/mongoid/{relations/bindings/embedded/many_spec.rb → association/embedded/embeds_many/binding_spec.rb} +3 -3
  158. data/spec/mongoid/{relations/builders/embedded/many_spec.rb → association/embedded/embeds_many/buildable_spec.rb} +17 -45
  159. data/spec/mongoid/{relations/embedded/many_spec.rb → association/embedded/embeds_many/proxy_spec.rb} +140 -428
  160. data/spec/mongoid/association/embedded/embeds_many_spec.rb +852 -0
  161. data/spec/mongoid/{relations/bindings/embedded/one_spec.rb → association/embedded/embeds_one/binding_spec.rb} +4 -4
  162. data/spec/mongoid/{relations/builders/embedded/one_spec.rb → association/embedded/embeds_one/buildable_spec.rb} +14 -34
  163. data/spec/mongoid/{relations/embedded/one_spec.rb → association/embedded/embeds_one/proxy_spec.rb} +39 -84
  164. data/spec/mongoid/association/embedded/embeds_one_spec.rb +908 -0
  165. data/spec/mongoid/{relations → association}/macros_spec.rb +148 -93
  166. data/spec/mongoid/{relations/builders/nested_attributes → association/nested}/many_spec.rb +16 -19
  167. data/spec/mongoid/{relations/builders/nested_attributes → association/nested}/one_spec.rb +17 -20
  168. data/spec/mongoid/association/options_spec.rb +1321 -0
  169. data/spec/mongoid/{relations → association}/polymorphic_spec.rb +7 -34
  170. data/spec/mongoid/{relations/bindings/referenced/in_spec.rb → association/referenced/belongs_to/binding_spec.rb} +7 -7
  171. data/spec/mongoid/{relations/builders/referenced/in_spec.rb → association/referenced/belongs_to/buildable_spec.rb} +46 -79
  172. data/spec/mongoid/{relations/eager/belongs_to_spec.rb → association/referenced/belongs_to/eager_spec.rb} +9 -9
  173. data/spec/mongoid/{relations/referenced/in_spec.rb → association/referenced/belongs_to/proxy_spec.rb} +57 -91
  174. data/spec/mongoid/association/referenced/belongs_to_spec.rb +1963 -0
  175. data/spec/mongoid/{relations/bindings/referenced/many_to_many_spec.rb → association/referenced/has_and_belongs_to_many/binding_spec.rb} +5 -5
  176. data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +121 -0
  177. data/spec/mongoid/{relations/eager/has_and_belongs_to_many_spec.rb → association/referenced/has_and_belongs_to_many/eager_spec.rb} +5 -5
  178. data/spec/mongoid/{relations/referenced/many_to_many_spec.rb → association/referenced/has_and_belongs_to_many/proxy_spec.rb} +107 -98
  179. data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +1027 -0
  180. data/spec/mongoid/{relations/bindings/referenced/many_spec.rb → association/referenced/has_many/binding_spec.rb} +5 -5
  181. data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +119 -0
  182. data/spec/mongoid/{relations/eager/has_many_spec.rb → association/referenced/has_many/eager_spec.rb} +11 -11
  183. data/spec/mongoid/{relations/targets → association/referenced/has_many}/enumerable_spec.rb +1 -109
  184. data/spec/mongoid/{relations/referenced/many_spec.rb → association/referenced/has_many/proxy_spec.rb} +28 -93
  185. data/spec/mongoid/association/referenced/has_many_spec.rb +1225 -0
  186. data/spec/mongoid/{relations/bindings/referenced/one_spec.rb → association/referenced/has_one/binding_spec.rb} +4 -4
  187. data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +113 -0
  188. data/spec/mongoid/{relations/eager/has_one_spec.rb → association/referenced/has_one/eager_spec.rb} +10 -10
  189. data/spec/mongoid/{relations/referenced/one_spec.rb → association/referenced/has_one/proxy_spec.rb} +9 -109
  190. data/spec/mongoid/association/referenced/has_one_spec.rb +1244 -0
  191. data/spec/mongoid/{relations → association}/reflections_spec.rb +1 -12
  192. data/spec/mongoid/{relations/synchronization_spec.rb → association/syncable_spec.rb} +4 -2
  193. data/spec/mongoid/{relations → association}/touchable_spec.rb +19 -1
  194. data/spec/mongoid/{relations_spec.rb → association_spec.rb} +1 -1
  195. data/spec/mongoid/atomic/modifiers_spec.rb +17 -17
  196. data/spec/mongoid/atomic_spec.rb +17 -17
  197. data/spec/mongoid/attributes/nested_spec.rb +14 -12
  198. data/spec/mongoid/attributes/readonly_spec.rb +80 -125
  199. data/spec/mongoid/clients/factory_spec.rb +28 -52
  200. data/spec/mongoid/clients/options_spec.rb +65 -69
  201. data/spec/mongoid/config_spec.rb +24 -0
  202. data/spec/mongoid/contextual/geo_near_spec.rb +0 -1
  203. data/spec/mongoid/contextual/mongo_spec.rb +4 -112
  204. data/spec/mongoid/criteria/modifiable_spec.rb +183 -60
  205. data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +3 -3
  206. data/spec/mongoid/criteria/queryable/pipeline_spec.rb +12 -0
  207. data/spec/mongoid/criteria/queryable/selectable_spec.rb +6 -74
  208. data/spec/mongoid/criteria/queryable/selector_spec.rb +2 -2
  209. data/spec/mongoid/criteria/scopable_spec.rb +0 -81
  210. data/spec/mongoid/criteria_spec.rb +16 -19
  211. data/spec/mongoid/document_spec.rb +2 -56
  212. data/spec/mongoid/extensions/array_spec.rb +11 -15
  213. data/spec/mongoid/extensions/big_decimal_spec.rb +9 -9
  214. data/spec/mongoid/extensions/object_spec.rb +7 -11
  215. data/spec/mongoid/extensions/range_spec.rb +7 -0
  216. data/spec/mongoid/extensions/regexp_spec.rb +0 -23
  217. data/spec/mongoid/extensions/string_spec.rb +7 -35
  218. data/spec/mongoid/factory_spec.rb +18 -11
  219. data/spec/mongoid/fields/foreign_key_spec.rb +24 -32
  220. data/spec/mongoid/fields_spec.rb +2 -2
  221. data/spec/mongoid/findable_spec.rb +1 -1
  222. data/spec/mongoid/indexable_spec.rb +18 -8
  223. data/spec/mongoid/interceptable_spec.rb +21 -2
  224. data/spec/mongoid/matchable_spec.rb +1 -26
  225. data/spec/mongoid/persistable/deletable_spec.rb +0 -19
  226. data/spec/mongoid/persistable/destroyable_spec.rb +0 -19
  227. data/spec/mongoid/persistable/incrementable_spec.rb +6 -6
  228. data/spec/mongoid/persistable/savable_spec.rb +3 -3
  229. data/spec/mongoid/persistable/settable_spec.rb +1 -35
  230. data/spec/mongoid/persistable/updatable_spec.rb +2 -2
  231. data/spec/mongoid/persistable_spec.rb +16 -16
  232. data/spec/mongoid/persistence_context_spec.rb +0 -14
  233. data/spec/mongoid/positional_spec.rb +10 -10
  234. data/spec/mongoid/query_cache_spec.rb +18 -87
  235. data/spec/mongoid/relations/proxy_spec.rb +124 -124
  236. data/spec/mongoid/scopable_spec.rb +0 -13
  237. data/spec/mongoid/threaded_spec.rb +0 -68
  238. data/spec/mongoid/validatable/associated_spec.rb +1 -1
  239. data/spec/mongoid/validatable/presence_spec.rb +7 -6
  240. data/spec/mongoid/validatable_spec.rb +1 -1
  241. data/spec/spec_helper.rb +7 -83
  242. metadata +586 -611
  243. metadata.gz.sig +2 -5
  244. data/lib/mongoid/clients/sessions.rb +0 -113
  245. data/lib/mongoid/errors/invalid_session_use.rb +0 -24
  246. data/lib/mongoid/matchable/nor.rb +0 -37
  247. data/lib/mongoid/railties/controller_runtime.rb +0 -86
  248. data/lib/mongoid/relations.rb +0 -148
  249. data/lib/mongoid/relations/accessors.rb +0 -267
  250. data/lib/mongoid/relations/auto_save.rb +0 -94
  251. data/lib/mongoid/relations/bindings.rb +0 -9
  252. data/lib/mongoid/relations/bindings/embedded/in.rb +0 -59
  253. data/lib/mongoid/relations/bindings/referenced/in.rb +0 -65
  254. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +0 -70
  255. data/lib/mongoid/relations/builder.rb +0 -57
  256. data/lib/mongoid/relations/builders.rb +0 -106
  257. data/lib/mongoid/relations/builders/nested_attributes/many.rb +0 -199
  258. data/lib/mongoid/relations/builders/referenced/in.rb +0 -26
  259. data/lib/mongoid/relations/builders/referenced/many.rb +0 -26
  260. data/lib/mongoid/relations/builders/referenced/many_to_many.rb +0 -39
  261. data/lib/mongoid/relations/builders/referenced/one.rb +0 -26
  262. data/lib/mongoid/relations/cascading.rb +0 -56
  263. data/lib/mongoid/relations/cascading/delete.rb +0 -44
  264. data/lib/mongoid/relations/cascading/destroy.rb +0 -43
  265. data/lib/mongoid/relations/cascading/nullify.rb +0 -35
  266. data/lib/mongoid/relations/cascading/restrict.rb +0 -39
  267. data/lib/mongoid/relations/conversions.rb +0 -34
  268. data/lib/mongoid/relations/counter_cache.rb +0 -160
  269. data/lib/mongoid/relations/cyclic.rb +0 -107
  270. data/lib/mongoid/relations/eager/base.rb +0 -153
  271. data/lib/mongoid/relations/eager/belongs_to.rb +0 -31
  272. data/lib/mongoid/relations/eager/has_and_belongs_to_many.rb +0 -47
  273. data/lib/mongoid/relations/eager/has_many.rb +0 -38
  274. data/lib/mongoid/relations/eager/has_one.rb +0 -30
  275. data/lib/mongoid/relations/embedded/in.rb +0 -241
  276. data/lib/mongoid/relations/embedded/many.rb +0 -683
  277. data/lib/mongoid/relations/embedded/one.rb +0 -235
  278. data/lib/mongoid/relations/macros.rb +0 -367
  279. data/lib/mongoid/relations/metadata.rb +0 -1179
  280. data/lib/mongoid/relations/nested_builder.rb +0 -74
  281. data/lib/mongoid/relations/options.rb +0 -49
  282. data/lib/mongoid/relations/polymorphic.rb +0 -39
  283. data/lib/mongoid/relations/referenced/in.rb +0 -304
  284. data/lib/mongoid/relations/referenced/many.rb +0 -812
  285. data/lib/mongoid/relations/referenced/many_to_many.rb +0 -479
  286. data/lib/mongoid/relations/referenced/one.rb +0 -290
  287. data/lib/mongoid/relations/synchronization.rb +0 -169
  288. data/lib/mongoid/relations/targets.rb +0 -2
  289. data/lib/mongoid/relations/targets/enumerable.rb +0 -493
  290. data/lib/mongoid/relations/touchable.rb +0 -97
  291. data/spec/app/models/array_field.rb +0 -7
  292. data/spec/app/models/delegating_patient.rb +0 -16
  293. data/spec/integration/document_spec.rb +0 -22
  294. data/spec/mongoid/clients/sessions_spec.rb +0 -334
  295. data/spec/mongoid/fields/internal/foreign_keys/array_spec.rb +0 -184
  296. data/spec/mongoid/fields/internal/foreign_keys/object_spec.rb +0 -201
  297. data/spec/mongoid/matchable/nor_spec.rb +0 -209
  298. data/spec/mongoid/relations/builders/referenced/many_spec.rb +0 -137
  299. data/spec/mongoid/relations/builders/referenced/many_to_many_spec.rb +0 -178
  300. data/spec/mongoid/relations/builders/referenced/one_spec.rb +0 -111
  301. data/spec/mongoid/relations/cascading/delete_spec.rb +0 -101
  302. data/spec/mongoid/relations/cascading/destroy_spec.rb +0 -47
  303. data/spec/mongoid/relations/cascading/nullify_spec.rb +0 -32
  304. data/spec/mongoid/relations/cascading/restrict_spec.rb +0 -68
  305. data/spec/mongoid/relations/cascading_spec.rb +0 -355
  306. data/spec/mongoid/relations/constraint_spec.rb +0 -75
  307. data/spec/mongoid/relations/conversions_spec.rb +0 -128
  308. data/spec/mongoid/relations/metadata_spec.rb +0 -1985
  309. data/spec/mongoid/relations/options_spec.rb +0 -35
  310. data/spec/rails/controller_extension/controller_runtime_spec.rb +0 -110
  311. data/spec/support/cluster_config.rb +0 -158
  312. data/spec/support/constraints.rb +0 -101
  313. data/spec/support/macros.rb +0 -20
  314. data/spec/support/session_registry.rb +0 -50
  315. data/spec/support/spec_config.rb +0 -42
@@ -1,6 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Mongoid::Relations::Bindings::Embedded::One do
3
+ describe Mongoid::Association::Embedded::EmbedsOne::Binding do
4
4
 
5
5
  let(:person) do
6
6
  Person.new
@@ -10,14 +10,14 @@ describe Mongoid::Relations::Bindings::Embedded::One do
10
10
  Name.new
11
11
  end
12
12
 
13
- let(:metadata) do
13
+ let(:association) do
14
14
  Person.relations["name"]
15
15
  end
16
16
 
17
17
  describe "#bind" do
18
18
 
19
19
  let(:binding) do
20
- described_class.new(person, target, metadata)
20
+ described_class.new(person, target, association)
21
21
  end
22
22
 
23
23
  context "when the document is bindable" do
@@ -51,7 +51,7 @@ describe Mongoid::Relations::Bindings::Embedded::One do
51
51
  describe "#unbind" do
52
52
 
53
53
  let(:binding) do
54
- described_class.new(person, target, metadata)
54
+ described_class.new(person, target, association)
55
55
  end
56
56
 
57
57
  context "when the document is unbindable" do
@@ -1,29 +1,29 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Mongoid::Relations::Builders::Embedded::One do
3
+ describe Mongoid::Association::Embedded::EmbedsOne::Buildable do
4
4
 
5
5
  let(:base) do
6
6
  double
7
7
  end
8
8
 
9
- let(:builder) do
10
- described_class.new(base, metadata, object)
9
+ let(:options) do
10
+ { }
11
11
  end
12
12
 
13
13
  describe "#build" do
14
14
 
15
- context "when provided nil" do
15
+ let(:document) do
16
+ association.build(base, object)
17
+ end
16
18
 
17
- let(:metadata) do
18
- double(klass: Name, name: :name)
19
- end
19
+ let(:association) do
20
+ Mongoid::Association::Embedded::EmbedsOne.new(Person, :name, options)
21
+ end
20
22
 
21
- let(:builder) do
22
- described_class.new(nil, metadata, nil)
23
- end
23
+ context "when provided nil" do
24
24
 
25
- let(:document) do
26
- builder.build
25
+ let(:object) do
26
+ nil
27
27
  end
28
28
 
29
29
  it "returns nil" do
@@ -33,18 +33,10 @@ describe Mongoid::Relations::Builders::Embedded::One do
33
33
 
34
34
  context "when provided a document" do
35
35
 
36
- let(:metadata) do
37
- double(klass: Name, name: :name)
38
- end
39
-
40
36
  let(:object) do
41
37
  Name.new
42
38
  end
43
39
 
44
- let(:document) do
45
- builder.build
46
- end
47
-
48
40
  it "returns the document" do
49
41
  expect(document).to eq(object)
50
42
  end
@@ -52,18 +44,10 @@ describe Mongoid::Relations::Builders::Embedded::One do
52
44
 
53
45
  context "when no type is in the object" do
54
46
 
55
- let(:metadata) do
56
- double(klass: Name, name: :name)
57
- end
58
-
59
47
  let(:object) do
60
48
  { "first_name" => "Corbin" }
61
49
  end
62
50
 
63
- let(:document) do
64
- builder.build
65
- end
66
-
67
51
  it "creates the correct type of document" do
68
52
  expect(document).to be_a_kind_of(Name)
69
53
  end
@@ -75,18 +59,14 @@ describe Mongoid::Relations::Builders::Embedded::One do
75
59
 
76
60
  context "when a type is in the object" do
77
61
 
78
- let(:metadata) do
79
- double(klass: Writer, name: :writer)
62
+ let(:association) do
63
+ Mongoid::Association::Embedded::EmbedsOne.new(Person, :writer, options)
80
64
  end
81
65
 
82
66
  let(:object) do
83
67
  { "_type" => PdfWriter.name, "speed" => 100 }
84
68
  end
85
69
 
86
- let(:document) do
87
- builder.build
88
- end
89
-
90
70
  it "creates the correct type of document" do
91
71
  expect(document).to be_a_kind_of(PdfWriter)
92
72
  end
@@ -1,6 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Mongoid::Relations::Embedded::One do
3
+ describe Mongoid::Association::Embedded::EmbedsOne::Proxy do
4
4
 
5
5
  describe "#===" do
6
6
 
@@ -12,12 +12,12 @@ describe Mongoid::Relations::Embedded::One do
12
12
  Name.new
13
13
  end
14
14
 
15
- let(:metadata) do
15
+ let(:association) do
16
16
  Person.relations["name"]
17
17
  end
18
18
 
19
19
  let(:relation) do
20
- described_class.new(base, target, metadata)
20
+ described_class.new(base, target, association)
21
21
  end
22
22
 
23
23
  context "when the proxied document is same class" do
@@ -168,7 +168,7 @@ describe Mongoid::Relations::Embedded::One do
168
168
  expect(name).to be_persisted
169
169
  end
170
170
 
171
- context "when replacing an exising document" do
171
+ context "when replacing an existing document" do
172
172
 
173
173
  let(:pet_owner) do
174
174
  PetOwner.create
@@ -200,6 +200,29 @@ describe Mongoid::Relations::Embedded::One do
200
200
  end
201
201
  end
202
202
  end
203
+
204
+ context 'when the original document does not need to be unset because it will be replaced by the $set' do
205
+
206
+ let!(:pet_owner) do
207
+ PetOwner.create(pet: pet_one)
208
+ end
209
+
210
+ let(:pet_one) do
211
+ Pet.new(name: 'kika')
212
+ end
213
+
214
+ let(:pet_two) do
215
+ Pet.new(name: 'tiksy')
216
+ end
217
+
218
+ before do
219
+ expect(Mongo::Logger.logger).to receive(:debug?).exactly(2).times
220
+ end
221
+
222
+ it 'does not execute an unnecessary unset for the relation' do
223
+ pet_owner.pet = pet_two
224
+ end
225
+ end
203
226
 
204
227
  context "when setting via the parent attributes" do
205
228
 
@@ -644,29 +667,6 @@ describe Mongoid::Relations::Embedded::One do
644
667
  end
645
668
  end
646
669
 
647
- describe ".builder" do
648
-
649
- let(:base) do
650
- Person.new
651
- end
652
-
653
- let(:target) do
654
- Name.new
655
- end
656
-
657
- let(:metadata) do
658
- Person.relations["name"]
659
- end
660
-
661
- let(:builder_klass) do
662
- Mongoid::Relations::Builders::Embedded::One
663
- end
664
-
665
- it "returns the embedded one builder" do
666
- expect(described_class.builder(base, metadata, target)).to be_a(builder_klass)
667
- end
668
- end
669
-
670
670
  describe "#create_#\{name}" do
671
671
 
672
672
  context "when the parent is a new record" do
@@ -787,6 +787,12 @@ describe Mongoid::Relations::Embedded::One do
787
787
  ShipmentAddress.new
788
788
  end
789
789
 
790
+ after do
791
+ # Note that this is necessary, otherwise all further Address objects will be
792
+ # saved with a "_type" attributes. See Traversable#inherited for why.
793
+ Address.send(:remove_defaults, "_type")
794
+ end
795
+
790
796
  before do
791
797
  courier_job.drop_address = old_child
792
798
  courier_job.update_attribute(:drop_address, new_child)
@@ -807,41 +813,6 @@ describe Mongoid::Relations::Embedded::One do
807
813
  end
808
814
  end
809
815
 
810
- describe ".foreign_key_suffix" do
811
-
812
- it "returns nil" do
813
- expect(described_class.foreign_key_suffix).to be_nil
814
- end
815
- end
816
-
817
- describe ".macro" do
818
-
819
- it "returns embeds_one" do
820
- expect(described_class.macro).to eq(:embeds_one)
821
- end
822
- end
823
-
824
- describe ".nested_builder" do
825
-
826
- let(:nested_builder_klass) do
827
- Mongoid::Relations::Builders::NestedAttributes::One
828
- end
829
-
830
- let(:metadata) do
831
- Person.relations["name"]
832
- end
833
-
834
- let(:attributes) do
835
- {}
836
- end
837
-
838
- it "returns the single nested builder" do
839
- expect(
840
- described_class.nested_builder(metadata, attributes, {})
841
- ).to be_a(nested_builder_klass)
842
- end
843
- end
844
-
845
816
  describe "#respond_to?" do
846
817
 
847
818
  let(:person) do
@@ -871,22 +842,6 @@ describe Mongoid::Relations::Embedded::One do
871
842
  end
872
843
  end
873
844
 
874
- describe ".valid_options" do
875
-
876
- it "returns the valid options" do
877
- expect(described_class.valid_options).to eq(
878
- [ :autobuild, :as, :cascade_callbacks, :cyclic, :store_as ]
879
- )
880
- end
881
- end
882
-
883
- describe ".validation_default" do
884
-
885
- it "returns true" do
886
- expect(described_class.validation_default).to be true
887
- end
888
- end
889
-
890
845
  context "when the embedded document has an array field" do
891
846
 
892
847
  let!(:person) do
@@ -895,9 +850,9 @@ describe Mongoid::Relations::Embedded::One do
895
850
 
896
851
  let!(:name) do
897
852
  person.create_name(
898
- first_name: "Syd",
899
- last_name: "Vicious",
900
- aliases: nil
853
+ first_name: "Syd",
854
+ last_name: "Vicious",
855
+ aliases: nil
901
856
  )
902
857
  end
903
858
 
@@ -980,7 +935,7 @@ describe Mongoid::Relations::Embedded::One do
980
935
 
981
936
  before do
982
937
  person.collection.find(_id: person.id).update_one(
983
- "$pull" => { "addresses" => { _id: address_one.id }}
938
+ "$pull" => { "addresses" => { _id: address_one.id }}
984
939
  )
985
940
  end
986
941
 
@@ -1013,8 +968,8 @@ describe Mongoid::Relations::Embedded::One do
1013
968
 
1014
969
  before do
1015
970
  band.collection.
1016
- find(_id: band.id).
1017
- update_one("$set" => { label: { name: "Mute" }})
971
+ find(_id: band.id).
972
+ update_one("$set" => { label: { name: "Mute" }})
1018
973
  end
1019
974
 
1020
975
  context "when loading the documents" do
@@ -0,0 +1,908 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Association::Embedded::EmbedsOne do
4
+
5
+ before do
6
+ class Container; include Mongoid::Document; end
7
+ class EmbeddedObject; include Mongoid::Document; end
8
+ end
9
+
10
+ after do
11
+ Container.relations.clear
12
+ EmbeddedObject.relations.clear
13
+ end
14
+
15
+ let(:embeds_one_class) do
16
+ Container
17
+ end
18
+
19
+ let(:name) do
20
+ :embedded_object
21
+ end
22
+
23
+ let(:association) do
24
+ embeds_one_class.embeds_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::Embedded::EmbeddedIn,
36
+ ]
37
+ end
38
+
39
+ it 'returns the relation complements' do
40
+ expect(association.send(: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 'when the :validate option is true' do
72
+
73
+ let(:options) do
74
+ {
75
+ validate: true
76
+ }
77
+ end
78
+
79
+ let(:association) do
80
+ # Note that it is necessary to create the association directly, otherwise the
81
+ # setup! method will be called by the :embeds_one macro
82
+ described_class.new(embeds_one_class, name, options)
83
+ end
84
+
85
+ it 'sets up validation' do
86
+ expect(embeds_one_class).to receive(:validates_associated).with(name).and_call_original
87
+ association.setup!
88
+ end
89
+ end
90
+
91
+ context 'when the :validate option is false' do
92
+
93
+ let(:options) do
94
+ {
95
+ validate: false
96
+ }
97
+ end
98
+
99
+ it 'does not set up validation' do
100
+ expect(embeds_one_class).not_to receive(:validates_associated)
101
+ association.setup!
102
+ end
103
+ end
104
+
105
+ context 'when the :validate option is not provided' do
106
+
107
+ let(:association) do
108
+ # Note that it is necessary to create the association directly, otherwise the
109
+ # setup! method will be called by the :embeds_one macro
110
+ described_class.new(embeds_one_class, name, options)
111
+ end
112
+
113
+ it 'sets up the validation because it uses the validation default (true)' do
114
+ expect(embeds_one_class).to receive(:validates_associated).with(name).and_call_original
115
+ association.setup!
116
+ end
117
+ end
118
+ end
119
+
120
+ describe '#type' do
121
+
122
+ context 'when polymorphic' do
123
+
124
+ let(:options) do
125
+ { as: :containable }
126
+ end
127
+
128
+ it 'returns the as attribute followed by "_type"' do
129
+ expect(association.type).to eq("#{options[:as]}_type")
130
+ end
131
+ end
132
+
133
+ context 'when not polymorphic' do
134
+
135
+ it 'returns nil' do
136
+ expect(association.type).to be_nil
137
+ end
138
+ end
139
+ end
140
+
141
+ describe '#inverse_type' do
142
+
143
+ context 'when polymorphic' do
144
+
145
+ let(:options) do
146
+ { as: :containable }
147
+ end
148
+
149
+ it 'returns nil' do
150
+ expect(association.inverse_type).to be_nil
151
+ end
152
+ end
153
+
154
+ context 'when not polymorphic' do
155
+
156
+ it 'returns nil' do
157
+ expect(association.inverse_type).to be_nil
158
+ end
159
+ end
160
+ end
161
+
162
+ describe '#inverse_type_setter' do
163
+
164
+ context 'when polymorphic' do
165
+
166
+ let(:options) do
167
+ { as: :containable }
168
+ end
169
+
170
+ it 'returns nil' do
171
+ expect(association.inverse_type).to be_nil
172
+ end
173
+ end
174
+
175
+ context 'when not polymorphic' do
176
+
177
+ it 'returns nil' do
178
+ expect(association.inverse_type).to be_nil
179
+ end
180
+ end
181
+ end
182
+
183
+ describe '#embedded?' do
184
+
185
+ it 'returns true' do
186
+ expect(association.embedded?).to be(true)
187
+ end
188
+ end
189
+
190
+ describe '#primary_key' do
191
+
192
+ it 'returns nil' do
193
+ expect(association.primary_key).to be_nil
194
+ end
195
+ end
196
+
197
+ describe '#indexed?' do
198
+
199
+ it 'returns false' do
200
+ expect(association.indexed?).to be(false)
201
+ end
202
+ end
203
+
204
+ describe '#relation' do
205
+
206
+ it 'returns Mongoid::Association::Embedded::EmbedsOne::Proxy' do
207
+ expect(association.relation).to be(Mongoid::Association::Embedded::EmbedsOne::Proxy)
208
+ end
209
+ end
210
+
211
+ describe '#validation_default' do
212
+
213
+ it 'returns true' do
214
+ expect(association.validation_default).to be(true)
215
+ end
216
+ end
217
+
218
+ describe '#name' do
219
+
220
+ it 'returns the name of the relation' do
221
+ expect(association.name).to be(name)
222
+ end
223
+ end
224
+
225
+ describe '#options' do
226
+
227
+ it 'returns the options' do
228
+ expect(association.options).to be(options)
229
+ end
230
+ end
231
+
232
+ describe '#cyclic?' do
233
+
234
+ context 'when :cyclic is specified in the options' do
235
+
236
+ let(:options) do
237
+ { cyclic: true }
238
+ end
239
+
240
+ it 'returns true' do
241
+ expect(association.cyclic?).to be(true)
242
+ end
243
+ end
244
+
245
+ context 'when :cyclic is not specified in the options' do
246
+
247
+ it 'returns false' do
248
+ expect(association.cyclic?).to be(false)
249
+ end
250
+ end
251
+ end
252
+
253
+ describe '#merge!' do
254
+
255
+ end
256
+
257
+ describe '#store_as' do
258
+
259
+ context 'when :store_as is specified in the options' do
260
+
261
+ let(:options) do
262
+ {
263
+ store_as: :baby_kangaroo
264
+ }
265
+ end
266
+
267
+ it 'returns the :store_as option as a String' do
268
+ expect(association.store_as).to eq(options[:store_as].to_s)
269
+ end
270
+ end
271
+
272
+ context 'when :store_as is not specified in the options' do
273
+
274
+ it 'returns name as a String' do
275
+ expect(association.store_as).to eq(name.to_s)
276
+ end
277
+ end
278
+ end
279
+
280
+ describe '#touchable?' do
281
+
282
+ it 'return false' do
283
+ expect(association.send(:touchable?)).to be(false)
284
+ end
285
+ end
286
+
287
+ describe '#order' do
288
+
289
+ it 'returns nil' do
290
+ expect(association.order).to be_nil
291
+ end
292
+ end
293
+
294
+ describe '#as' do
295
+
296
+ context 'when :as is specified in the options' do
297
+
298
+ let(:options) do
299
+ {
300
+ as: :containable
301
+ }
302
+ end
303
+
304
+ it 'returns the :as option' do
305
+ expect(association.as).to eq(options[:as])
306
+ end
307
+ end
308
+
309
+ context 'when :as is not specified in the options' do
310
+
311
+ it 'returns nil' do
312
+ expect(association.as).to be_nil
313
+ end
314
+ end
315
+ end
316
+
317
+ describe '#polymorphic?' do
318
+
319
+ context 'when :as is specified in the options' do
320
+
321
+ let(:options) do
322
+ {
323
+ as: :containable
324
+ }
325
+ end
326
+
327
+ it 'returns true' do
328
+ expect(association.polymorphic?).to be(true)
329
+ end
330
+
331
+ end
332
+
333
+ context 'when :as is not specified in the options' do
334
+
335
+ it 'returns false' do
336
+ expect(association.polymorphic?).to be(false)
337
+ end
338
+ end
339
+ end
340
+
341
+ describe '#type_setter' do
342
+
343
+ context 'when polymorphic' do
344
+
345
+ let(:options) do
346
+ { as: :containable }
347
+ end
348
+
349
+ it 'returns the type followed by = as a String' do
350
+ expect(association.type_setter).to eq("containable_type=")
351
+ end
352
+ end
353
+
354
+ context 'when not polymorphic' do
355
+
356
+ it 'returns nil' do
357
+ expect(association.type).to be_nil
358
+ end
359
+ end
360
+ end
361
+
362
+ describe '#dependent' do
363
+
364
+ it 'returns nil' do
365
+ expect(association.dependent).to be_nil
366
+ end
367
+ end
368
+
369
+ describe '#inverse_type' do
370
+
371
+ it 'returns nil' do
372
+ expect(association.inverse_type).to be_nil
373
+ end
374
+ end
375
+
376
+ describe '#bindable?' do
377
+
378
+ it 'returns false' do
379
+ expect(association.bindable?(Person.new)).to be(false)
380
+ end
381
+ end
382
+
383
+ describe '#inverses' do
384
+
385
+ context 'when polymorphic' do
386
+
387
+ before do
388
+ EmbeddedObject.embedded_in :containable, polymorphic: true
389
+ end
390
+
391
+ let(:options) do
392
+ {
393
+ as: :containable
394
+ }
395
+ end
396
+
397
+ context 'when another object is passed to the method' do
398
+
399
+ let(:instance_of_other_class) do
400
+ EmbeddedObject.new
401
+ end
402
+
403
+ context 'when the relation class has only one relation whose class matches the owning class' do
404
+
405
+ it 'returns the :as attribute of this association' do
406
+ expect(association.inverses(instance_of_other_class)).to match_array([ :containable ])
407
+ end
408
+ end
409
+
410
+ context 'when :inverse_of is specified' do
411
+
412
+ before do
413
+ options.merge!(inverse_of: :inverse_name)
414
+ end
415
+
416
+ it 'returns the :inverse_of value' do
417
+ expect(association.inverses(instance_of_other_class)).to eq([ :inverse_name ])
418
+ end
419
+ end
420
+
421
+ context 'when inverse_of is not specified' do
422
+
423
+ it 'returns the :as attribute of this association' do
424
+ expect(association.inverses(instance_of_other_class)).to match_array([ :containable ])
425
+ end
426
+ end
427
+ end
428
+
429
+ context 'when another object is not passed to the method' do
430
+
431
+ context 'when inverse_of is specified' do
432
+
433
+ before do
434
+ options.merge!(inverse_of: :inverse_name)
435
+ end
436
+
437
+ it 'returns the :inverse_of value' do
438
+ expect(association.inverses).to eq([ :inverse_name ])
439
+ end
440
+ end
441
+
442
+ context 'when inverse_of is not specified' do
443
+
444
+ it 'returns the :as attribute' do
445
+ expect(association.inverses).to eq([ :containable ])
446
+ end
447
+ end
448
+ end
449
+ end
450
+
451
+ context 'when not polymorphic' do
452
+
453
+ before do
454
+ EmbeddedObject.embedded_in :container
455
+ end
456
+
457
+ context 'when inverse_of is specified' do
458
+
459
+ before do
460
+ options.merge!(inverse_of: :inverse_name)
461
+ end
462
+
463
+ it 'returns the :inverse_of value' do
464
+ expect(association.inverses).to eq([ :inverse_name ])
465
+ end
466
+ end
467
+
468
+ context 'when inverse_of is not specified' do
469
+
470
+ it 'uses the inverse class to find the inverse name' do
471
+ expect(association.inverses).to eq([ :container ])
472
+ end
473
+ end
474
+
475
+ context 'when :cyclic is specified' do
476
+
477
+ it 'returns the cyclic inverse name' do
478
+
479
+ end
480
+ end
481
+ end
482
+ end
483
+
484
+ describe '##inverse' do
485
+
486
+ context 'when polymorphic' do
487
+
488
+ before do
489
+ EmbeddedObject.embedded_in :containable, polymorphic: true
490
+ end
491
+
492
+ let(:options) do
493
+ {
494
+ as: :containable
495
+ }
496
+ end
497
+
498
+ context 'when another object is passed to the method' do
499
+
500
+ let(:instance_of_other_class) do
501
+ EmbeddedObject.new
502
+ end
503
+
504
+ context 'when the relation class has only one relation whose class matches the owning class' do
505
+
506
+ it 'returns the :as attribute of this association' do
507
+ expect(association.inverse(instance_of_other_class)).to eq(:containable)
508
+ end
509
+ end
510
+
511
+ context 'when :inverse_of is specified' do
512
+
513
+ before do
514
+ options.merge!(inverse_of: :inverse_name)
515
+ end
516
+
517
+ it 'returns the :inverse_of value' do
518
+ expect(association.inverse(instance_of_other_class)).to eq(:inverse_name)
519
+ end
520
+ end
521
+
522
+ context 'when inverse_of is not specified' do
523
+
524
+ it 'returns the :as attribute of this association' do
525
+ expect(association.inverse(instance_of_other_class)).to eq(:containable)
526
+ end
527
+ end
528
+ end
529
+
530
+ context 'when another object is not passed to the method' do
531
+
532
+ context 'when inverse_of is specified' do
533
+
534
+ before do
535
+ options.merge!(inverse_of: :inverse_name)
536
+ end
537
+
538
+ it 'returns the :inverse_of value' do
539
+ expect(association.inverse).to eq(:inverse_name)
540
+ end
541
+ end
542
+
543
+ context 'when inverse_of is not specified' do
544
+
545
+ it 'returns the :as attribute' do
546
+ expect(association.inverse).to eq(:containable)
547
+ end
548
+ end
549
+ end
550
+ end
551
+
552
+ context 'when not polymorphic' do
553
+
554
+ before do
555
+ EmbeddedObject.embedded_in :container
556
+ end
557
+
558
+ context 'when inverse_of is specified' do
559
+
560
+ before do
561
+ options.merge!(inverse_of: :inverse_name)
562
+ end
563
+
564
+ it 'returns the :inverse_of value' do
565
+ expect(association.inverse).to eq(:inverse_name)
566
+ end
567
+ end
568
+
569
+ context 'when inverse_of is not specified' do
570
+
571
+ it 'uses the inverse class to find the inverse name' do
572
+ expect(association.inverse).to eq(:container)
573
+ end
574
+ end
575
+
576
+ context 'when :cyclic is specified' do
577
+
578
+ it 'returns the cyclic inverse name' do
579
+
580
+ end
581
+ end
582
+ end
583
+ end
584
+
585
+ describe '#inverse_association' do
586
+
587
+ end
588
+
589
+ describe '#autosave' do
590
+
591
+ it 'returns false' do
592
+ expect(association.autosave).to be(false)
593
+ end
594
+ end
595
+
596
+ describe '#relation_class_name' do
597
+
598
+ context 'when the :class_name option is specified' do
599
+
600
+ let(:options) do
601
+ { class_name: 'OtherEmbeddedObject' }
602
+ end
603
+
604
+ it 'returns the class name option' do
605
+ expect(association.relation_class_name).to eq('OtherEmbeddedObject')
606
+ end
607
+ end
608
+
609
+ context 'when the class_name option is not specified' do
610
+
611
+ it 'uses the name of the relation to deduce the class name' do
612
+ expect(association.relation_class_name).to eq('EmbeddedObject')
613
+ end
614
+ end
615
+ end
616
+
617
+ describe '#klass' do
618
+
619
+ context 'when the :class_name option is specified' do
620
+
621
+ let!(:_class) do
622
+ class OtherEmbeddedObject; end
623
+ OtherEmbeddedObject
624
+ end
625
+
626
+ let(:options) do
627
+ { class_name: 'OtherEmbeddedObject' }
628
+ end
629
+
630
+ it 'returns the class name option' do
631
+ expect(association.klass).to eq(_class)
632
+ end
633
+ end
634
+
635
+ context 'when the class_name option is not specified' do
636
+
637
+ it 'uses the name of the relation to deduce the class name' do
638
+ expect(association.klass).to eq(EmbeddedObject)
639
+ end
640
+ end
641
+ end
642
+
643
+ describe '#inverse_class_name' do
644
+
645
+ it 'returns the name of the owner class' do
646
+ expect(association.inverse_class_name).to eq(Container.name)
647
+ end
648
+ end
649
+
650
+ describe '#inverse_class' do
651
+
652
+ it 'returns the owner class' do
653
+ expect(association.inverse_class).to be(Container)
654
+ end
655
+ end
656
+
657
+ describe '#inverse_of' do
658
+
659
+ context 'when :inverse_of is specified in the options' do
660
+
661
+ let(:options) do
662
+ { inverse_of: :objects_list }
663
+ end
664
+
665
+ it 'returns the inverse_of value' do
666
+ expect(association.inverse_of).to eq(options[:inverse_of])
667
+ end
668
+ end
669
+
670
+ context 'when :inverse_of is not specified in the options' do
671
+
672
+ it 'returns nil' do
673
+ expect(association.inverse_of).to be_nil
674
+ end
675
+ end
676
+ end
677
+
678
+ describe '#key' do
679
+
680
+ it 'returns the name of the relation as a string' do
681
+ expect(association.key).to eq(name.to_s)
682
+ end
683
+ end
684
+
685
+ describe '#setter' do
686
+
687
+ it 'returns a string of the name followed by =' do
688
+ expect(association.setter).to eq("#{name}=")
689
+ end
690
+ end
691
+
692
+ describe '#validate?' do
693
+
694
+ context 'when :validate is specified in the options' do
695
+
696
+ context 'when validate is true' do
697
+
698
+ let(:options) do
699
+ { validate: true }
700
+ end
701
+
702
+ it 'returns true' do
703
+ expect(association.send(:validate?)).to be(true)
704
+ end
705
+ end
706
+
707
+ context 'when validate is false' do
708
+
709
+ let(:options) do
710
+ { validate: false }
711
+ end
712
+
713
+ it 'returns false' do
714
+ expect(association.send(:validate?)).to be(false)
715
+ end
716
+ end
717
+ end
718
+
719
+ context 'when :validate is not specified in the options' do
720
+
721
+ it 'returns the validation_default' do
722
+ expect(association.send(:validate?)).to eq(association.validation_default)
723
+ end
724
+ end
725
+ end
726
+
727
+ describe '#autobuilding?' do
728
+
729
+ context 'when :autobuild is specified in the options' do
730
+
731
+ context 'when autobuild is true' do
732
+
733
+ let(:options) do
734
+ { autobuild: true }
735
+ end
736
+
737
+ it 'returns true' do
738
+ expect(association.autobuilding?).to be(true)
739
+ end
740
+ end
741
+
742
+ context 'when autobuild is false' do
743
+
744
+ let(:options) do
745
+ { autobuild: false }
746
+ end
747
+
748
+ it 'returns true' do
749
+ expect(association.autobuilding?).to be(false)
750
+ end
751
+ end
752
+ end
753
+
754
+ context 'when :validate is not specified in the options' do
755
+
756
+ it 'returns false' do
757
+ expect(association.autobuilding?).to be(false)
758
+ end
759
+ end
760
+ end
761
+
762
+ describe '#forced_nil_inverse?' do
763
+
764
+ it 'returns false' do
765
+ expect(association.forced_nil_inverse?).to be(false)
766
+ end
767
+ end
768
+
769
+ describe '#stores_foreign_key?' do
770
+
771
+ it 'returns false' do
772
+ expect(association.stores_foreign_key?).to be(false)
773
+ end
774
+ end
775
+
776
+ describe '#inverse_setter' do
777
+
778
+ context 'when an inverse can be determined' do
779
+
780
+ before do
781
+ EmbeddedObject.embedded_in :container
782
+ end
783
+
784
+ it 'returns the name of the inverse followed by =' do
785
+ expect(association.inverse_setter).to eq('container=')
786
+ end
787
+ end
788
+
789
+ context 'when an inverse cannot be determined' do
790
+
791
+ it 'returns nil' do
792
+ expect(association.inverse_setter).to be_nil
793
+ end
794
+ end
795
+ end
796
+
797
+ describe '#extension' do
798
+
799
+ context 'when a block is passed' do
800
+
801
+ let(:association) do
802
+ embeds_one_class.embeds_one name, options do; end
803
+ end
804
+
805
+ it 'defines an extension module' do
806
+ expect(association.extension).to be_a(Module)
807
+ end
808
+
809
+ it 'returns the extension' do
810
+ expect(association.extension).to eq(
811
+ "#{embeds_one_class.name}::#{embeds_one_class.name}#{name.to_s.camelize}RelationExtension".constantize)
812
+ end
813
+ end
814
+
815
+ context 'when an :extension is not specified in the options' do
816
+
817
+ it 'returns false' do
818
+ expect(association.extension).to be_nil
819
+ end
820
+ end
821
+ end
822
+
823
+ describe '#criteria' do
824
+
825
+ it 'does not respond to the method' do
826
+ expect {
827
+ association.criteria
828
+ }.to raise_exception(NoMethodError)
829
+ end
830
+ end
831
+
832
+ describe '#destructive?' do
833
+
834
+ it 'returns false' do
835
+ expect(association.destructive?).to be(false)
836
+ end
837
+ end
838
+
839
+ describe '#nested_builder' do
840
+
841
+ it 'returns an instance of Association::Nested::One' do
842
+ expect(association.nested_builder({}, {})).to be_a(Mongoid::Association::Nested::One)
843
+ end
844
+ end
845
+
846
+ describe '#cascading_callbacks?' do
847
+
848
+ context 'when cascade_callbacks is specified in the options' do
849
+
850
+ context 'when :cascade_callbacks is true' do
851
+
852
+ let(:options) do
853
+ { cascade_callbacks: true }
854
+ end
855
+
856
+ it 'returns true' do
857
+ expect(association.cascading_callbacks?).to be(true)
858
+ end
859
+ end
860
+
861
+ context 'when :cascade_callbacks is false' do
862
+
863
+ let(:options) do
864
+ { cascade_callbacks: false }
865
+ end
866
+
867
+ it 'returns false' do
868
+ expect(association.cascading_callbacks?).to be(false)
869
+ end
870
+ end
871
+ end
872
+
873
+ context 'when cascade_callbacks is not specified in the options' do
874
+
875
+ it 'returns false' do
876
+ expect(association.cascading_callbacks?).to be(false)
877
+ end
878
+ end
879
+ end
880
+
881
+ describe '#path' do
882
+
883
+ it 'returns an instance of Mongoid::Atomic::Paths::Root' do
884
+ expect(association.path(double( :_parent => true))).to be_a(Mongoid::Atomic::Paths::Embedded::One)
885
+ end
886
+ end
887
+
888
+ describe '#create_relation' do
889
+
890
+ let(:owner) do
891
+ Container.new
892
+ end
893
+
894
+ let(:target) do
895
+ EmbeddedObject.new
896
+ end
897
+
898
+ before do
899
+ association
900
+ EmbeddedObject.embedded_in :container
901
+ end
902
+
903
+ it 'returns an the target (EmbeddedObject)' do
904
+ expect(Mongoid::Association::Embedded::EmbedsOne::Proxy).to receive(:new).and_call_original
905
+ expect(association.create_relation(owner, target)).to be_a(EmbeddedObject)
906
+ end
907
+ end
908
+ end