mongoid 5.4.1 → 7.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (1354) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/CHANGELOG.md +30 -30
  4. data/LICENSE +1 -0
  5. data/README.md +33 -19
  6. data/Rakefile +75 -5
  7. data/lib/config/locales/en.yml +180 -64
  8. data/lib/mongoid/association/accessors.rb +402 -0
  9. data/lib/mongoid/association/bindable.rb +179 -0
  10. data/lib/mongoid/association/builders.rb +84 -0
  11. data/lib/mongoid/association/constrainable.rb +43 -0
  12. data/lib/mongoid/association/depending.rb +128 -0
  13. data/lib/mongoid/association/eager_loadable.rb +35 -0
  14. data/lib/mongoid/association/embedded/batchable.rb +358 -0
  15. data/lib/mongoid/association/embedded/cyclic.rb +102 -0
  16. data/lib/mongoid/association/embedded/embedded_in/binding.rb +52 -0
  17. data/lib/mongoid/association/embedded/embedded_in/buildable.rb +38 -0
  18. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +111 -0
  19. data/lib/mongoid/association/embedded/embedded_in.rb +134 -0
  20. data/lib/mongoid/association/embedded/embeds_many/binding.rb +41 -0
  21. data/lib/mongoid/association/embedded/embeds_many/buildable.rb +45 -0
  22. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +549 -0
  23. data/lib/mongoid/association/embedded/embeds_many.rb +183 -0
  24. data/lib/mongoid/association/embedded/embeds_one/binding.rb +43 -0
  25. data/lib/mongoid/association/embedded/embeds_one/buildable.rb +39 -0
  26. data/lib/mongoid/association/embedded/embeds_one/proxy.rb +145 -0
  27. data/lib/mongoid/association/embedded/embeds_one.rb +150 -0
  28. data/lib/mongoid/association/embedded.rb +6 -0
  29. data/lib/mongoid/association/macros.rb +207 -0
  30. data/lib/mongoid/association/many.rb +198 -0
  31. data/lib/mongoid/association/marshalable.rb +31 -0
  32. data/lib/mongoid/association/nested/many.rb +191 -0
  33. data/lib/mongoid/association/nested/nested_buildable.rb +66 -0
  34. data/lib/mongoid/association/nested/one.rb +116 -0
  35. data/lib/mongoid/association/nested.rb +15 -0
  36. data/lib/mongoid/association/one.rb +53 -0
  37. data/lib/mongoid/association/options.rb +122 -0
  38. data/lib/mongoid/association/proxy.rb +189 -0
  39. data/lib/mongoid/association/referenced/auto_save.rb +73 -0
  40. data/lib/mongoid/association/referenced/belongs_to/binding.rb +82 -0
  41. data/lib/mongoid/association/referenced/belongs_to/buildable.rb +48 -0
  42. data/lib/mongoid/association/referenced/belongs_to/eager.rb +73 -0
  43. data/lib/mongoid/association/referenced/belongs_to/proxy.rb +125 -0
  44. data/lib/mongoid/association/referenced/belongs_to.rb +226 -0
  45. data/lib/mongoid/association/referenced/counter_cache.rb +152 -0
  46. data/lib/mongoid/association/referenced/eager.rb +163 -0
  47. data/lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb +72 -0
  48. data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +40 -0
  49. data/lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb +53 -0
  50. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +292 -0
  51. data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +270 -0
  52. data/lib/mongoid/association/referenced/has_many/binding.rb +36 -0
  53. data/lib/mongoid/association/referenced/has_many/buildable.rb +38 -0
  54. data/lib/mongoid/association/referenced/has_many/eager.rb +44 -0
  55. data/lib/mongoid/association/referenced/has_many/enumerable.rb +498 -0
  56. data/lib/mongoid/association/referenced/has_many/proxy.rb +545 -0
  57. data/lib/mongoid/association/referenced/has_many.rb +251 -0
  58. data/lib/mongoid/association/referenced/has_one/binding.rb +42 -0
  59. data/lib/mongoid/association/referenced/has_one/buildable.rb +74 -0
  60. data/lib/mongoid/association/referenced/has_one/eager.rb +36 -0
  61. data/lib/mongoid/association/referenced/has_one/nested_builder.rb +113 -0
  62. data/lib/mongoid/association/referenced/has_one/proxy.rb +111 -0
  63. data/lib/mongoid/association/referenced/has_one.rb +184 -0
  64. data/lib/mongoid/association/referenced/syncable.rb +155 -0
  65. data/lib/mongoid/association/referenced.rb +9 -0
  66. data/lib/mongoid/association/reflections.rb +69 -0
  67. data/lib/mongoid/association/relatable.rb +455 -0
  68. data/lib/mongoid/association.rb +135 -0
  69. data/lib/mongoid/atomic/modifiers.rb +29 -53
  70. data/lib/mongoid/atomic/paths/embedded/many.rb +5 -8
  71. data/lib/mongoid/atomic/paths/embedded/one.rb +5 -8
  72. data/lib/mongoid/atomic/paths/embedded.rb +3 -4
  73. data/lib/mongoid/atomic/paths/root.rb +4 -7
  74. data/lib/mongoid/atomic/paths.rb +2 -1
  75. data/lib/mongoid/atomic.rb +47 -52
  76. data/lib/mongoid/attributes/dynamic.rb +19 -33
  77. data/lib/mongoid/attributes/nested.rb +29 -21
  78. data/lib/mongoid/attributes/processing.rb +11 -26
  79. data/lib/mongoid/attributes/projector.rb +119 -0
  80. data/lib/mongoid/attributes/readonly.rb +24 -7
  81. data/lib/mongoid/attributes.rb +110 -106
  82. data/lib/mongoid/cacheable.rb +33 -0
  83. data/lib/mongoid/changeable.rb +39 -57
  84. data/lib/mongoid/clients/factory.rb +42 -17
  85. data/lib/mongoid/clients/options.rb +62 -258
  86. data/lib/mongoid/clients/sessions.rb +125 -0
  87. data/lib/mongoid/clients/storage_options.rb +9 -81
  88. data/lib/mongoid/clients/validators/storage.rb +2 -9
  89. data/lib/mongoid/clients/validators.rb +2 -1
  90. data/lib/mongoid/clients.rb +13 -73
  91. data/lib/mongoid/composable.rb +33 -10
  92. data/lib/mongoid/config/environment.rb +36 -14
  93. data/lib/mongoid/config/options.rb +20 -23
  94. data/lib/mongoid/config/validators/client.rb +2 -17
  95. data/lib/mongoid/config/validators/option.rb +2 -3
  96. data/lib/mongoid/config/validators.rb +2 -1
  97. data/lib/mongoid/config.rb +112 -39
  98. data/lib/mongoid/contextual/aggregable/memory.rb +17 -12
  99. data/lib/mongoid/contextual/aggregable/mongo.rb +26 -27
  100. data/lib/mongoid/contextual/aggregable/none.rb +65 -0
  101. data/lib/mongoid/contextual/aggregable.rb +17 -0
  102. data/lib/mongoid/contextual/atomic.rb +31 -33
  103. data/lib/mongoid/contextual/command.rb +2 -5
  104. data/lib/mongoid/contextual/geo_near.rb +7 -38
  105. data/lib/mongoid/contextual/map_reduce.rb +12 -36
  106. data/lib/mongoid/contextual/memory.rb +34 -64
  107. data/lib/mongoid/contextual/mongo.rb +228 -113
  108. data/lib/mongoid/contextual/none.rb +51 -23
  109. data/lib/mongoid/contextual/queryable.rb +2 -3
  110. data/lib/mongoid/contextual.rb +7 -9
  111. data/lib/mongoid/copyable.rb +32 -19
  112. data/lib/mongoid/criteria/findable.rb +12 -15
  113. data/lib/mongoid/criteria/includable.rb +21 -28
  114. data/lib/mongoid/criteria/inspectable.rb +2 -3
  115. data/lib/mongoid/criteria/marshalable.rb +4 -7
  116. data/lib/mongoid/criteria/modifiable.rb +38 -24
  117. data/lib/mongoid/criteria/options.rb +24 -0
  118. data/lib/mongoid/criteria/permission.rb +2 -1
  119. data/lib/mongoid/criteria/queryable/aggregable.rb +109 -0
  120. data/lib/mongoid/criteria/queryable/expandable.rb +68 -0
  121. data/lib/mongoid/criteria/queryable/extensions/array.rb +162 -0
  122. data/lib/mongoid/criteria/queryable/extensions/big_decimal.rb +36 -0
  123. data/lib/mongoid/criteria/queryable/extensions/boolean.rb +33 -0
  124. data/lib/mongoid/criteria/queryable/extensions/date.rb +58 -0
  125. data/lib/mongoid/criteria/queryable/extensions/date_time.rb +47 -0
  126. data/lib/mongoid/criteria/queryable/extensions/hash.rb +177 -0
  127. data/lib/mongoid/criteria/queryable/extensions/nil_class.rb +75 -0
  128. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +81 -0
  129. data/lib/mongoid/criteria/queryable/extensions/object.rb +181 -0
  130. data/lib/mongoid/criteria/queryable/extensions/range.rb +94 -0
  131. data/lib/mongoid/criteria/queryable/extensions/regexp.rb +72 -0
  132. data/lib/mongoid/criteria/queryable/extensions/set.rb +33 -0
  133. data/lib/mongoid/criteria/queryable/extensions/string.rb +122 -0
  134. data/lib/mongoid/criteria/queryable/extensions/symbol.rb +72 -0
  135. data/lib/mongoid/criteria/queryable/extensions/time.rb +55 -0
  136. data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +61 -0
  137. data/lib/mongoid/criteria/queryable/extensions.rb +25 -0
  138. data/lib/mongoid/criteria/queryable/key.rb +177 -0
  139. data/lib/mongoid/criteria/queryable/macroable.rb +26 -0
  140. data/lib/mongoid/criteria/queryable/mergeable.rb +413 -0
  141. data/lib/mongoid/criteria/queryable/optional.rb +386 -0
  142. data/lib/mongoid/criteria/queryable/options.rb +136 -0
  143. data/lib/mongoid/criteria/queryable/pipeline.rb +106 -0
  144. data/lib/mongoid/criteria/queryable/selectable.rb +943 -0
  145. data/lib/mongoid/criteria/queryable/selector.rb +202 -0
  146. data/lib/mongoid/criteria/queryable/smash.rb +95 -0
  147. data/lib/mongoid/criteria/queryable/storable.rb +237 -0
  148. data/lib/mongoid/criteria/queryable.rb +85 -0
  149. data/lib/mongoid/criteria/scopable.rb +27 -19
  150. data/lib/mongoid/criteria.rb +93 -76
  151. data/lib/mongoid/document.rb +62 -109
  152. data/lib/mongoid/equality.rb +29 -12
  153. data/lib/mongoid/errors/ambiguous_relationship.rb +5 -6
  154. data/lib/mongoid/errors/callback.rb +2 -3
  155. data/lib/mongoid/errors/criteria_argument_required.rb +18 -0
  156. data/lib/mongoid/errors/delete_restriction.rb +10 -13
  157. data/lib/mongoid/errors/document_not_destroyed.rb +4 -7
  158. data/lib/mongoid/errors/document_not_found.rb +2 -9
  159. data/lib/mongoid/errors/eager_load.rb +6 -5
  160. data/lib/mongoid/errors/empty_config_file.rb +25 -0
  161. data/lib/mongoid/errors/in_memory_collation_not_supported.rb +2 -3
  162. data/lib/mongoid/errors/invalid_collection.rb +2 -1
  163. data/lib/mongoid/errors/invalid_config_file.rb +25 -0
  164. data/lib/mongoid/errors/invalid_config_option.rb +2 -3
  165. data/lib/mongoid/errors/invalid_dependent_strategy.rb +31 -0
  166. data/lib/mongoid/errors/invalid_discriminator_key_target.rb +24 -0
  167. data/lib/mongoid/errors/invalid_elem_match_operator.rb +32 -0
  168. data/lib/mongoid/errors/invalid_estimated_count_criteria.rb +25 -0
  169. data/lib/mongoid/errors/invalid_expression_operator.rb +27 -0
  170. data/lib/mongoid/errors/invalid_field.rb +4 -7
  171. data/lib/mongoid/errors/invalid_field_operator.rb +32 -0
  172. data/lib/mongoid/errors/invalid_field_option.rb +2 -3
  173. data/lib/mongoid/errors/invalid_find.rb +2 -3
  174. data/lib/mongoid/errors/invalid_includes.rb +2 -3
  175. data/lib/mongoid/errors/invalid_index.rb +2 -3
  176. data/lib/mongoid/errors/invalid_options.rb +4 -5
  177. data/lib/mongoid/errors/invalid_path.rb +2 -3
  178. data/lib/mongoid/errors/invalid_persistence_option.rb +26 -0
  179. data/lib/mongoid/errors/invalid_query.rb +40 -0
  180. data/lib/mongoid/errors/invalid_relation.rb +61 -0
  181. data/lib/mongoid/errors/invalid_relation_option.rb +28 -0
  182. data/lib/mongoid/errors/invalid_scope.rb +2 -3
  183. data/lib/mongoid/errors/invalid_session_use.rb +21 -0
  184. data/lib/mongoid/errors/invalid_set_polymorphic_relation.rb +6 -5
  185. data/lib/mongoid/errors/invalid_storage_options.rb +2 -3
  186. data/lib/mongoid/errors/invalid_storage_parent.rb +2 -3
  187. data/lib/mongoid/errors/invalid_time.rb +2 -3
  188. data/lib/mongoid/errors/invalid_value.rb +2 -1
  189. data/lib/mongoid/errors/inverse_not_found.rb +3 -4
  190. data/lib/mongoid/errors/mixed_client_configuration.rb +2 -3
  191. data/lib/mongoid/errors/mixed_relations.rb +2 -3
  192. data/lib/mongoid/errors/mongoid_error.rb +3 -10
  193. data/lib/mongoid/errors/nested_attributes_metadata_not_found.rb +3 -4
  194. data/lib/mongoid/errors/no_client_config.rb +4 -5
  195. data/lib/mongoid/errors/no_client_database.rb +2 -3
  196. data/lib/mongoid/errors/no_client_hosts.rb +2 -3
  197. data/lib/mongoid/errors/no_clients_config.rb +2 -3
  198. data/lib/mongoid/errors/no_default_client.rb +3 -4
  199. data/lib/mongoid/errors/no_environment.rb +2 -3
  200. data/lib/mongoid/errors/no_map_reduce_output.rb +2 -3
  201. data/lib/mongoid/errors/no_metadata.rb +2 -3
  202. data/lib/mongoid/errors/no_parent.rb +2 -3
  203. data/lib/mongoid/errors/readonly_attribute.rb +2 -3
  204. data/lib/mongoid/errors/readonly_document.rb +3 -6
  205. data/lib/mongoid/errors/scope_overwrite.rb +2 -1
  206. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +2 -0
  207. data/lib/mongoid/errors/unknown_attribute.rb +2 -3
  208. data/lib/mongoid/errors/unknown_model.rb +24 -0
  209. data/lib/mongoid/errors/unsaved_document.rb +3 -2
  210. data/lib/mongoid/errors/unsupported_javascript.rb +2 -3
  211. data/lib/mongoid/errors/validations.rb +2 -1
  212. data/lib/mongoid/errors.rb +18 -1
  213. data/lib/mongoid/evolvable.rb +4 -5
  214. data/lib/mongoid/extensions/array.rb +33 -42
  215. data/lib/mongoid/extensions/big_decimal.rb +17 -17
  216. data/lib/mongoid/extensions/boolean.rb +3 -5
  217. data/lib/mongoid/extensions/date.rb +17 -13
  218. data/lib/mongoid/extensions/date_time.rb +4 -12
  219. data/lib/mongoid/extensions/decimal128.rb +3 -6
  220. data/lib/mongoid/extensions/false_class.rb +4 -7
  221. data/lib/mongoid/extensions/float.rb +5 -10
  222. data/lib/mongoid/extensions/hash.rb +57 -38
  223. data/lib/mongoid/extensions/integer.rb +5 -12
  224. data/lib/mongoid/extensions/module.rb +2 -3
  225. data/lib/mongoid/extensions/nil_class.rb +2 -5
  226. data/lib/mongoid/extensions/object.rb +21 -48
  227. data/lib/mongoid/extensions/object_id.rb +2 -7
  228. data/lib/mongoid/extensions/range.rb +3 -11
  229. data/lib/mongoid/extensions/regexp.rb +4 -4
  230. data/lib/mongoid/extensions/set.rb +2 -7
  231. data/lib/mongoid/extensions/string.rb +27 -44
  232. data/lib/mongoid/extensions/symbol.rb +2 -7
  233. data/lib/mongoid/extensions/time.rb +16 -10
  234. data/lib/mongoid/extensions/time_with_zone.rb +25 -7
  235. data/lib/mongoid/extensions/true_class.rb +4 -7
  236. data/lib/mongoid/extensions.rb +18 -11
  237. data/lib/mongoid/factory.rb +60 -10
  238. data/lib/mongoid/fields/foreign_key.rb +8 -27
  239. data/lib/mongoid/fields/localized.rb +2 -11
  240. data/lib/mongoid/fields/standard.rb +8 -50
  241. data/lib/mongoid/fields/validators/macro.rb +50 -14
  242. data/lib/mongoid/fields/validators.rb +2 -1
  243. data/lib/mongoid/fields.rb +118 -67
  244. data/lib/mongoid/findable.rb +60 -24
  245. data/lib/mongoid/indexable/specification.rb +3 -16
  246. data/lib/mongoid/indexable/validators/options.rb +2 -7
  247. data/lib/mongoid/indexable.rb +26 -36
  248. data/lib/mongoid/inspectable.rb +4 -11
  249. data/lib/mongoid/interceptable.rb +27 -57
  250. data/lib/mongoid/loggable.rb +13 -16
  251. data/lib/mongoid/matchable.rb +5 -146
  252. data/lib/mongoid/matcher/all.rb +22 -0
  253. data/lib/mongoid/matcher/and.rb +21 -0
  254. data/lib/mongoid/matcher/bits.rb +41 -0
  255. data/lib/mongoid/matcher/bits_all_clear.rb +20 -0
  256. data/lib/mongoid/matcher/bits_all_set.rb +20 -0
  257. data/lib/mongoid/matcher/bits_any_clear.rb +20 -0
  258. data/lib/mongoid/matcher/bits_any_set.rb +20 -0
  259. data/lib/mongoid/matcher/elem_match.rb +36 -0
  260. data/lib/mongoid/matcher/elem_match_expression.rb +20 -0
  261. data/lib/mongoid/matcher/eq.rb +11 -0
  262. data/lib/mongoid/matcher/eq_impl.rb +67 -0
  263. data/lib/mongoid/matcher/eq_impl_with_regexp.rb +26 -0
  264. data/lib/mongoid/matcher/exists.rb +15 -0
  265. data/lib/mongoid/matcher/expression.rb +35 -0
  266. data/lib/mongoid/matcher/expression_operator.rb +19 -0
  267. data/lib/mongoid/matcher/field_expression.rb +62 -0
  268. data/lib/mongoid/matcher/field_operator.rb +54 -0
  269. data/lib/mongoid/matcher/gt.rb +17 -0
  270. data/lib/mongoid/matcher/gte.rb +17 -0
  271. data/lib/mongoid/matcher/in.rb +25 -0
  272. data/lib/mongoid/matcher/lt.rb +17 -0
  273. data/lib/mongoid/matcher/lte.rb +17 -0
  274. data/lib/mongoid/matcher/mod.rb +17 -0
  275. data/lib/mongoid/matcher/ne.rb +16 -0
  276. data/lib/mongoid/matcher/nin.rb +11 -0
  277. data/lib/mongoid/matcher/nor.rb +25 -0
  278. data/lib/mongoid/matcher/not.rb +29 -0
  279. data/lib/mongoid/matcher/or.rb +21 -0
  280. data/lib/mongoid/matcher/regex.rb +41 -0
  281. data/lib/mongoid/matcher/size.rb +26 -0
  282. data/lib/mongoid/matcher/type.rb +99 -0
  283. data/lib/mongoid/matcher.rb +110 -0
  284. data/lib/mongoid/persistable/creatable.rb +9 -24
  285. data/lib/mongoid/persistable/deletable.rb +16 -29
  286. data/lib/mongoid/persistable/destroyable.rb +12 -15
  287. data/lib/mongoid/persistable/incrementable.rb +7 -8
  288. data/lib/mongoid/persistable/logical.rb +5 -7
  289. data/lib/mongoid/persistable/poppable.rb +3 -6
  290. data/lib/mongoid/persistable/pullable.rb +3 -8
  291. data/lib/mongoid/persistable/pushable.rb +14 -10
  292. data/lib/mongoid/persistable/renamable.rb +10 -8
  293. data/lib/mongoid/persistable/savable.rb +3 -8
  294. data/lib/mongoid/persistable/settable.rb +61 -19
  295. data/lib/mongoid/persistable/unsettable.rb +8 -8
  296. data/lib/mongoid/persistable/updatable.rb +38 -33
  297. data/lib/mongoid/persistable/upsertable.rb +5 -9
  298. data/lib/mongoid/persistable.rb +137 -45
  299. data/lib/mongoid/persistence_context.rb +222 -0
  300. data/lib/mongoid/positional.rb +3 -6
  301. data/lib/mongoid/query_cache.rb +141 -110
  302. data/lib/mongoid/railtie.rb +20 -17
  303. data/lib/mongoid/railties/controller_runtime.rb +88 -0
  304. data/lib/mongoid/railties/database.rake +9 -0
  305. data/lib/mongoid/reloadable.rb +14 -19
  306. data/lib/mongoid/scopable.rb +11 -38
  307. data/lib/mongoid/selectable.rb +8 -17
  308. data/lib/mongoid/serializable.rb +29 -34
  309. data/lib/mongoid/shardable.rb +80 -17
  310. data/lib/mongoid/stateful.rb +4 -10
  311. data/lib/mongoid/stringified_symbol.rb +52 -0
  312. data/lib/mongoid/tasks/database.rake +12 -5
  313. data/lib/mongoid/tasks/database.rb +88 -7
  314. data/lib/mongoid/threaded/lifecycle.rb +2 -21
  315. data/lib/mongoid/threaded.rb +70 -49
  316. data/lib/mongoid/timestamps/created/short.rb +2 -1
  317. data/lib/mongoid/timestamps/created.rb +3 -2
  318. data/lib/mongoid/timestamps/short.rb +2 -1
  319. data/lib/mongoid/timestamps/timeless.rb +6 -9
  320. data/lib/mongoid/timestamps/updated/short.rb +2 -1
  321. data/lib/mongoid/timestamps/updated.rb +3 -4
  322. data/lib/mongoid/timestamps.rb +3 -2
  323. data/lib/mongoid/touchable.rb +132 -0
  324. data/lib/mongoid/traversable.rb +181 -61
  325. data/lib/mongoid/validatable/associated.rb +5 -6
  326. data/lib/mongoid/validatable/format.rb +2 -1
  327. data/lib/mongoid/validatable/length.rb +2 -1
  328. data/lib/mongoid/validatable/localizable.rb +2 -3
  329. data/lib/mongoid/validatable/macros.rb +3 -8
  330. data/lib/mongoid/validatable/presence.rb +10 -15
  331. data/lib/mongoid/validatable/queryable.rb +2 -3
  332. data/lib/mongoid/validatable/uniqueness.rb +26 -40
  333. data/lib/mongoid/validatable.rb +16 -35
  334. data/lib/mongoid/version.rb +3 -2
  335. data/lib/mongoid.rb +35 -17
  336. data/lib/rails/generators/mongoid/config/config_generator.rb +10 -2
  337. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +54 -21
  338. data/lib/rails/generators/mongoid/model/model_generator.rb +3 -2
  339. data/lib/rails/generators/mongoid/model/templates/model.rb.tt +1 -1
  340. data/lib/rails/generators/mongoid_generator.rb +2 -1
  341. data/lib/rails/mongoid.rb +4 -5
  342. data/spec/README.md +33 -0
  343. data/spec/config/mongoid.yml +28 -2
  344. data/spec/integration/app_spec.rb +343 -0
  345. data/spec/integration/associations/belongs_to_spec.rb +15 -0
  346. data/spec/integration/associations/embedded_dirty_spec.rb +29 -0
  347. data/spec/integration/associations/embedded_spec.rb +268 -0
  348. data/spec/integration/associations/embeds_many_spec.rb +206 -0
  349. data/spec/integration/associations/embeds_one_spec.rb +23 -0
  350. data/spec/integration/associations/foreign_key_spec.rb +98 -0
  351. data/spec/integration/associations/foreign_key_spec_models.rb +64 -0
  352. data/spec/integration/associations/has_many_spec.rb +128 -0
  353. data/spec/integration/associations/has_one_spec.rb +176 -0
  354. data/spec/integration/associations/nested_attributes_assignment_spec.rb +115 -0
  355. data/spec/integration/associations/reverse_population_spec.rb +34 -0
  356. data/spec/integration/associations/reverse_population_spec_models.rb +36 -0
  357. data/spec/integration/associations/scope_option_spec.rb +101 -0
  358. data/spec/integration/atomic/modifiers_spec.rb +116 -0
  359. data/spec/integration/bson_regexp_raw_spec.rb +19 -0
  360. data/spec/integration/callbacks_models.rb +61 -0
  361. data/spec/integration/callbacks_spec.rb +228 -0
  362. data/spec/integration/contextual/empty_spec.rb +141 -0
  363. data/spec/integration/criteria/alias_query_spec.rb +161 -0
  364. data/spec/integration/criteria/date_field_spec.rb +40 -0
  365. data/spec/integration/criteria/default_scope_spec.rb +72 -0
  366. data/spec/integration/criteria/logical_spec.rb +124 -0
  367. data/spec/integration/criteria/range_spec.rb +265 -0
  368. data/spec/integration/criteria/time_with_zone_spec.rb +142 -0
  369. data/spec/integration/discriminator_key_spec.rb +353 -0
  370. data/spec/integration/discriminator_value_spec.rb +206 -0
  371. data/spec/integration/document_spec.rb +51 -0
  372. data/spec/integration/i18n_fallbacks_spec.rb +89 -0
  373. data/spec/integration/matcher_examples_spec.rb +758 -0
  374. data/spec/integration/matcher_operator_data/all.yml +140 -0
  375. data/spec/integration/matcher_operator_data/and.yml +93 -0
  376. data/spec/integration/matcher_operator_data/bits_all_clear.yml +159 -0
  377. data/spec/integration/matcher_operator_data/bits_all_set.yml +159 -0
  378. data/spec/integration/matcher_operator_data/bits_any_clear.yml +159 -0
  379. data/spec/integration/matcher_operator_data/bits_any_set.yml +159 -0
  380. data/spec/integration/matcher_operator_data/comment.yml +22 -0
  381. data/spec/integration/matcher_operator_data/elem_match.yml +409 -0
  382. data/spec/integration/matcher_operator_data/elem_match_expr.yml +213 -0
  383. data/spec/integration/matcher_operator_data/eq.yml +191 -0
  384. data/spec/integration/matcher_operator_data/exists.yml +213 -0
  385. data/spec/integration/matcher_operator_data/generic_op.yml +17 -0
  386. data/spec/integration/matcher_operator_data/gt.yml +132 -0
  387. data/spec/integration/matcher_operator_data/gt_types.yml +63 -0
  388. data/spec/integration/matcher_operator_data/gte.yml +132 -0
  389. data/spec/integration/matcher_operator_data/gte_types.yml +15 -0
  390. data/spec/integration/matcher_operator_data/implicit.yml +331 -0
  391. data/spec/integration/matcher_operator_data/implicit_traversal.yml +112 -0
  392. data/spec/integration/matcher_operator_data/in.yml +210 -0
  393. data/spec/integration/matcher_operator_data/invalid_op.yml +59 -0
  394. data/spec/integration/matcher_operator_data/invalid_syntax.yml +39 -0
  395. data/spec/integration/matcher_operator_data/lt.yml +132 -0
  396. data/spec/integration/matcher_operator_data/lt_types.yml +15 -0
  397. data/spec/integration/matcher_operator_data/lte.yml +132 -0
  398. data/spec/integration/matcher_operator_data/lte_types.yml +15 -0
  399. data/spec/integration/matcher_operator_data/mod.yml +55 -0
  400. data/spec/integration/matcher_operator_data/multiple.yml +29 -0
  401. data/spec/integration/matcher_operator_data/ne.yml +150 -0
  402. data/spec/integration/matcher_operator_data/ne_types.yml +15 -0
  403. data/spec/integration/matcher_operator_data/nin.yml +114 -0
  404. data/spec/integration/matcher_operator_data/nor.yml +126 -0
  405. data/spec/integration/matcher_operator_data/not.yml +196 -0
  406. data/spec/integration/matcher_operator_data/or.yml +137 -0
  407. data/spec/integration/matcher_operator_data/regex.yml +174 -0
  408. data/spec/integration/matcher_operator_data/regex_options.yml +72 -0
  409. data/spec/integration/matcher_operator_data/size.yml +174 -0
  410. data/spec/integration/matcher_operator_data/type.yml +70 -0
  411. data/spec/integration/matcher_operator_data/type_array.yml +16 -0
  412. data/spec/integration/matcher_operator_data/type_binary.yml +18 -0
  413. data/spec/integration/matcher_operator_data/type_boolean.yml +39 -0
  414. data/spec/integration/matcher_operator_data/type_code.yml +26 -0
  415. data/spec/integration/matcher_operator_data/type_code_with_scope.yml +26 -0
  416. data/spec/integration/matcher_operator_data/type_date.yml +39 -0
  417. data/spec/integration/matcher_operator_data/type_db_pointer.yml +19 -0
  418. data/spec/integration/matcher_operator_data/type_decimal.yml +40 -0
  419. data/spec/integration/matcher_operator_data/type_double.yml +15 -0
  420. data/spec/integration/matcher_operator_data/type_int32.yml +33 -0
  421. data/spec/integration/matcher_operator_data/type_int64.yml +33 -0
  422. data/spec/integration/matcher_operator_data/type_max_key.yml +17 -0
  423. data/spec/integration/matcher_operator_data/type_min_key.yml +17 -0
  424. data/spec/integration/matcher_operator_data/type_null.yml +23 -0
  425. data/spec/integration/matcher_operator_data/type_object.yml +23 -0
  426. data/spec/integration/matcher_operator_data/type_object_id.yml +25 -0
  427. data/spec/integration/matcher_operator_data/type_regex.yml +44 -0
  428. data/spec/integration/matcher_operator_data/type_string.yml +15 -0
  429. data/spec/integration/matcher_operator_data/type_symbol.yml +32 -0
  430. data/spec/integration/matcher_operator_data/type_timestamp.yml +25 -0
  431. data/spec/integration/matcher_operator_data/type_undefined.yml +17 -0
  432. data/spec/integration/matcher_operator_spec.rb +124 -0
  433. data/spec/integration/matcher_spec.rb +283 -0
  434. data/spec/integration/server_query_spec.rb +141 -0
  435. data/spec/integration/shardable_spec.rb +148 -0
  436. data/spec/integration/stringified_symbol_field_spec.rb +203 -0
  437. data/spec/lite_spec_helper.rb +83 -0
  438. data/spec/mongoid/association/accessors_spec.rb +1067 -0
  439. data/spec/mongoid/association/auto_save_spec.rb +403 -0
  440. data/spec/mongoid/association/builders_spec.rb +255 -0
  441. data/spec/mongoid/association/constrainable_spec.rb +117 -0
  442. data/spec/mongoid/association/counter_cache_spec.rb +452 -0
  443. data/spec/mongoid/association/depending_spec.rb +959 -0
  444. data/spec/mongoid/association/eager_spec.rb +281 -0
  445. data/spec/mongoid/association/embedded/cyclic_spec.rb +180 -0
  446. data/spec/mongoid/association/embedded/dirty_spec.rb +67 -0
  447. data/spec/mongoid/association/embedded/embedded_in/binding_spec.rb +173 -0
  448. data/spec/mongoid/association/embedded/embedded_in/buildable_spec.rb +36 -0
  449. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +559 -0
  450. data/spec/mongoid/association/embedded/embedded_in_spec.rb +903 -0
  451. data/spec/mongoid/association/embedded/embeds_many/binding_spec.rb +56 -0
  452. data/spec/mongoid/association/embedded/embeds_many/buildable_spec.rb +106 -0
  453. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +4652 -0
  454. data/spec/mongoid/association/embedded/embeds_many_models.rb +69 -0
  455. data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +51 -0
  456. data/spec/mongoid/association/embedded/embeds_many_spec.rb +864 -0
  457. data/spec/mongoid/association/embedded/embeds_one/binding_spec.rb +79 -0
  458. data/spec/mongoid/association/embedded/embeds_one/buildable_spec.rb +81 -0
  459. data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +1025 -0
  460. data/spec/mongoid/association/embedded/embeds_one_dnl_models.rb +8 -0
  461. data/spec/mongoid/association/embedded/embeds_one_models.rb +54 -0
  462. data/spec/mongoid/association/embedded/embeds_one_query_spec.rb +28 -0
  463. data/spec/mongoid/association/embedded/embeds_one_spec.rb +956 -0
  464. data/spec/mongoid/association/macros_spec.rb +1116 -0
  465. data/spec/mongoid/association/nested/many_spec.rb +232 -0
  466. data/spec/mongoid/association/nested/one_spec.rb +253 -0
  467. data/spec/mongoid/association/options_spec.rb +1323 -0
  468. data/spec/mongoid/association/polymorphic_spec.rb +162 -0
  469. data/spec/mongoid/association/referenced/belongs_to/binding_spec.rb +243 -0
  470. data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +238 -0
  471. data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +412 -0
  472. data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +1373 -0
  473. data/spec/mongoid/association/referenced/belongs_to_query_spec.rb +57 -0
  474. data/spec/mongoid/association/referenced/belongs_to_spec.rb +2046 -0
  475. data/spec/mongoid/association/referenced/has_and_belongs_to_many/binding_spec.rb +180 -0
  476. data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +148 -0
  477. data/spec/mongoid/association/referenced/has_and_belongs_to_many/eager_spec.rb +196 -0
  478. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_persistence_spec.rb +76 -0
  479. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +3773 -0
  480. data/spec/mongoid/association/referenced/has_and_belongs_to_many_models.rb +67 -0
  481. data/spec/mongoid/association/referenced/has_and_belongs_to_many_query_spec.rb +39 -0
  482. data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +1071 -0
  483. data/spec/mongoid/association/referenced/has_many/binding_spec.rb +155 -0
  484. data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +142 -0
  485. data/spec/mongoid/association/referenced/has_many/eager_spec.rb +290 -0
  486. data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +2232 -0
  487. data/spec/mongoid/association/referenced/has_many/proxy_query_spec.rb +23 -0
  488. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +4092 -0
  489. data/spec/mongoid/association/referenced/has_many_models.rb +95 -0
  490. data/spec/mongoid/association/referenced/has_many_query_spec.rb +37 -0
  491. data/spec/mongoid/association/referenced/has_many_spec.rb +1248 -0
  492. data/spec/mongoid/association/referenced/has_one/binding_spec.rb +133 -0
  493. data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +159 -0
  494. data/spec/mongoid/association/referenced/has_one/eager_spec.rb +193 -0
  495. data/spec/mongoid/association/referenced/has_one/proxy_spec.rb +1142 -0
  496. data/spec/mongoid/association/referenced/has_one_models.rb +97 -0
  497. data/spec/mongoid/association/referenced/has_one_query_spec.rb +37 -0
  498. data/spec/mongoid/association/referenced/has_one_spec.rb +1383 -0
  499. data/spec/mongoid/association/reflections_spec.rb +92 -0
  500. data/spec/mongoid/association/syncable_spec.rb +499 -0
  501. data/spec/mongoid/association_spec.rb +191 -0
  502. data/spec/mongoid/atomic/modifiers_spec.rb +52 -3
  503. data/spec/mongoid/atomic/paths/embedded/many_spec.rb +2 -0
  504. data/spec/mongoid/atomic/paths/embedded/one_spec.rb +2 -0
  505. data/spec/mongoid/atomic/paths/root_spec.rb +2 -0
  506. data/spec/mongoid/atomic/paths_spec.rb +107 -12
  507. data/spec/mongoid/atomic_spec.rb +36 -11
  508. data/spec/mongoid/attributes/dynamic_spec.rb +152 -0
  509. data/spec/mongoid/attributes/nested_spec.rb +115 -91
  510. data/spec/mongoid/attributes/projector_data/embedded.yml +105 -0
  511. data/spec/mongoid/attributes/projector_data/fields.yml +93 -0
  512. data/spec/mongoid/attributes/projector_spec.rb +44 -0
  513. data/spec/mongoid/attributes/readonly_spec.rb +111 -111
  514. data/spec/mongoid/attributes_spec.rb +501 -33
  515. data/spec/mongoid/cacheable_spec.rb +114 -0
  516. data/spec/mongoid/changeable_spec.rb +118 -35
  517. data/spec/mongoid/clients/factory_spec.rb +176 -34
  518. data/spec/mongoid/clients/options_spec.rb +431 -98
  519. data/spec/mongoid/clients/sessions_spec.rb +335 -0
  520. data/spec/mongoid/clients/transactions_spec.rb +415 -0
  521. data/spec/mongoid/clients_spec.rb +318 -114
  522. data/spec/mongoid/composable_spec.rb +9 -0
  523. data/spec/mongoid/config/environment_spec.rb +88 -8
  524. data/spec/mongoid/config/options_spec.rb +22 -3
  525. data/spec/mongoid/config_spec.rb +371 -13
  526. data/spec/mongoid/contextual/aggregable/memory_spec.rb +53 -11
  527. data/spec/mongoid/contextual/aggregable/mongo_spec.rb +122 -20
  528. data/spec/mongoid/contextual/aggregable/none_spec.rb +60 -0
  529. data/spec/mongoid/contextual/atomic_spec.rb +245 -84
  530. data/spec/mongoid/contextual/geo_near_spec.rb +31 -19
  531. data/spec/mongoid/contextual/map_reduce_spec.rb +24 -7
  532. data/spec/mongoid/contextual/memory_spec.rb +44 -23
  533. data/spec/mongoid/contextual/mongo_spec.rb +780 -155
  534. data/spec/mongoid/contextual/none_spec.rb +14 -71
  535. data/spec/mongoid/copyable_spec.rb +297 -23
  536. data/spec/mongoid/copyable_spec_models.rb +47 -0
  537. data/spec/mongoid/criteria/findable_spec.rb +77 -20
  538. data/spec/mongoid/criteria/inspectable_spec.rb +2 -0
  539. data/spec/mongoid/criteria/marshalable_spec.rb +2 -0
  540. data/spec/mongoid/criteria/modifiable_spec.rb +506 -50
  541. data/spec/mongoid/criteria/options_spec.rb +31 -0
  542. data/spec/mongoid/criteria/queryable/aggregable_spec.rb +372 -0
  543. data/spec/mongoid/criteria/queryable/expandable_spec.rb +61 -0
  544. data/spec/mongoid/criteria/queryable/extensions/array_spec.rb +525 -0
  545. data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +61 -0
  546. data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +60 -0
  547. data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +215 -0
  548. data/spec/mongoid/criteria/queryable/extensions/date_spec.rb +332 -0
  549. data/spec/mongoid/criteria/queryable/extensions/date_time_spec.rb +423 -0
  550. data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +60 -0
  551. data/spec/mongoid/criteria/queryable/extensions/float_spec.rb +67 -0
  552. data/spec/mongoid/criteria/queryable/extensions/hash_spec.rb +329 -0
  553. data/spec/mongoid/criteria/queryable/extensions/integer_spec.rb +67 -0
  554. data/spec/mongoid/criteria/queryable/extensions/nil_class_spec.rb +79 -0
  555. data/spec/mongoid/criteria/queryable/extensions/numeric_spec.rb +53 -0
  556. data/spec/mongoid/criteria/queryable/extensions/object_spec.rb +110 -0
  557. data/spec/mongoid/criteria/queryable/extensions/range_spec.rb +370 -0
  558. data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +91 -0
  559. data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +92 -0
  560. data/spec/mongoid/criteria/queryable/extensions/set_spec.rb +41 -0
  561. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +347 -0
  562. data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +169 -0
  563. data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +395 -0
  564. data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +376 -0
  565. data/spec/mongoid/criteria/queryable/key_spec.rb +96 -0
  566. data/spec/mongoid/criteria/queryable/mergeable_spec.rb +182 -0
  567. data/spec/mongoid/criteria/queryable/optional_spec.rb +1801 -0
  568. data/spec/mongoid/criteria/queryable/options_spec.rb +362 -0
  569. data/spec/mongoid/criteria/queryable/pipeline_spec.rb +214 -0
  570. data/spec/mongoid/criteria/queryable/queryable_spec.rb +139 -0
  571. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +2069 -0
  572. data/spec/mongoid/criteria/queryable/selectable_shared_examples.rb +38 -0
  573. data/spec/mongoid/criteria/queryable/selectable_spec.rb +2297 -0
  574. data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +589 -0
  575. data/spec/mongoid/criteria/queryable/selector_spec.rb +883 -0
  576. data/spec/mongoid/criteria/queryable/smash_spec.rb +32 -0
  577. data/spec/mongoid/criteria/queryable/storable_spec.rb +226 -0
  578. data/spec/mongoid/criteria/scopable_spec.rb +210 -0
  579. data/spec/mongoid/criteria_projection_spec.rb +410 -0
  580. data/spec/mongoid/criteria_spec.rb +876 -402
  581. data/spec/mongoid/document_fields_spec.rb +113 -0
  582. data/spec/mongoid/document_persistence_context_spec.rb +32 -0
  583. data/spec/mongoid/document_query_spec.rb +89 -0
  584. data/spec/mongoid/document_spec.rb +214 -104
  585. data/spec/mongoid/equality_spec.rb +144 -41
  586. data/spec/mongoid/errors/ambiguous_relationship_spec.rb +5 -3
  587. data/spec/mongoid/errors/callback_spec.rb +2 -0
  588. data/spec/mongoid/errors/delete_restriction_spec.rb +4 -2
  589. data/spec/mongoid/errors/document_not_destroyed_spec.rb +2 -0
  590. data/spec/mongoid/errors/document_not_found_spec.rb +2 -0
  591. data/spec/mongoid/errors/eager_load_spec.rb +2 -0
  592. data/spec/mongoid/errors/invalid_collection_spec.rb +2 -0
  593. data/spec/mongoid/errors/invalid_config_file_spec.rb +31 -0
  594. data/spec/mongoid/errors/invalid_config_option_spec.rb +2 -0
  595. data/spec/mongoid/errors/invalid_field_option_spec.rb +2 -0
  596. data/spec/mongoid/errors/invalid_field_spec.rb +2 -0
  597. data/spec/mongoid/errors/invalid_find_spec.rb +2 -0
  598. data/spec/mongoid/errors/invalid_includes_spec.rb +2 -0
  599. data/spec/mongoid/errors/invalid_index_spec.rb +2 -0
  600. data/spec/mongoid/errors/invalid_options_spec.rb +4 -2
  601. data/spec/mongoid/errors/invalid_path_spec.rb +2 -0
  602. data/spec/mongoid/errors/invalid_relation_spec.rb +39 -0
  603. data/spec/mongoid/errors/invalid_scope_spec.rb +3 -1
  604. data/spec/mongoid/errors/invalid_set_polymorphic_relation_spec.rb +2 -0
  605. data/spec/mongoid/errors/invalid_storage_options_spec.rb +2 -0
  606. data/spec/mongoid/errors/invalid_time_spec.rb +2 -0
  607. data/spec/mongoid/errors/inverse_not_found_spec.rb +3 -1
  608. data/spec/mongoid/errors/mixed_client_configuration_spec.rb +2 -0
  609. data/spec/mongoid/errors/mixed_relations_spec.rb +2 -0
  610. data/spec/mongoid/errors/mongoid_error_spec.rb +28 -11
  611. data/spec/mongoid/errors/nested_attributes_metadata_not_found_spec.rb +5 -3
  612. data/spec/mongoid/errors/no_client_config_spec.rb +4 -2
  613. data/spec/mongoid/errors/no_client_database_spec.rb +5 -3
  614. data/spec/mongoid/errors/no_client_hosts_spec.rb +5 -3
  615. data/spec/mongoid/errors/no_clients_config_spec.rb +2 -0
  616. data/spec/mongoid/errors/no_environment_spec.rb +2 -0
  617. data/spec/mongoid/errors/no_map_reduce_output_spec.rb +2 -0
  618. data/spec/mongoid/errors/no_metadata_spec.rb +4 -2
  619. data/spec/mongoid/errors/no_parent_spec.rb +3 -1
  620. data/spec/mongoid/errors/readonly_attribute_spec.rb +2 -0
  621. data/spec/mongoid/errors/readonly_document_spec.rb +2 -0
  622. data/spec/mongoid/errors/scope_overwrite_spec.rb +2 -0
  623. data/spec/mongoid/errors/too_many_nested_attribute_records_spec.rb +2 -0
  624. data/spec/mongoid/errors/unknown_attribute_spec.rb +4 -2
  625. data/spec/mongoid/errors/unsaved_document_spec.rb +3 -1
  626. data/spec/mongoid/errors/unsupported_javascript_spec.rb +2 -0
  627. data/spec/mongoid/errors/validations_spec.rb +2 -0
  628. data/spec/mongoid/extensions/array_spec.rb +36 -58
  629. data/spec/mongoid/extensions/big_decimal_spec.rb +323 -19
  630. data/spec/mongoid/extensions/binary_spec.rb +2 -0
  631. data/spec/mongoid/extensions/boolean_spec.rb +16 -0
  632. data/spec/mongoid/extensions/date_class_mongoize_spec.rb +335 -0
  633. data/spec/mongoid/extensions/date_spec.rb +8 -164
  634. data/spec/mongoid/extensions/date_time_spec.rb +19 -66
  635. data/spec/mongoid/extensions/decimal128_spec.rb +3 -1
  636. data/spec/mongoid/extensions/false_class_spec.rb +3 -1
  637. data/spec/mongoid/extensions/float_spec.rb +25 -4
  638. data/spec/mongoid/extensions/hash_spec.rb +138 -1
  639. data/spec/mongoid/extensions/integer_spec.rb +22 -3
  640. data/spec/mongoid/extensions/module_spec.rb +2 -0
  641. data/spec/mongoid/extensions/nil_class_spec.rb +2 -0
  642. data/spec/mongoid/extensions/object_id_spec.rb +2 -0
  643. data/spec/mongoid/extensions/object_spec.rb +27 -31
  644. data/spec/mongoid/extensions/range_spec.rb +9 -0
  645. data/spec/mongoid/extensions/regexp_spec.rb +25 -0
  646. data/spec/mongoid/extensions/set_spec.rb +2 -0
  647. data/spec/mongoid/extensions/string_spec.rb +113 -31
  648. data/spec/mongoid/extensions/stringified_symbol_spec.rb +84 -0
  649. data/spec/mongoid/extensions/symbol_spec.rb +2 -0
  650. data/spec/mongoid/extensions/time_spec.rb +31 -6
  651. data/spec/mongoid/extensions/time_with_zone_spec.rb +35 -6
  652. data/spec/mongoid/extensions/true_class_spec.rb +3 -1
  653. data/spec/mongoid/extensions_spec.rb +16 -2
  654. data/spec/mongoid/factory_spec.rb +304 -15
  655. data/spec/mongoid/fields/foreign_key_spec.rb +26 -32
  656. data/spec/mongoid/fields/localized_spec.rb +6 -4
  657. data/spec/mongoid/fields/standard_spec.rb +2 -0
  658. data/spec/mongoid/fields_spec.rb +420 -38
  659. data/spec/mongoid/findable_spec.rb +98 -19
  660. data/spec/mongoid/indexable/specification_spec.rb +2 -0
  661. data/spec/mongoid/indexable_spec.rb +69 -17
  662. data/spec/mongoid/inspectable_spec.rb +39 -2
  663. data/spec/mongoid/interceptable_spec.rb +273 -114
  664. data/spec/mongoid/interceptable_spec_models.rb +76 -0
  665. data/spec/mongoid/loggable_spec.rb +2 -0
  666. data/spec/mongoid/matcher/extract_attribute_data/numeric_keys.yml +104 -0
  667. data/spec/mongoid/matcher/extract_attribute_data/traversal.yml +239 -0
  668. data/spec/mongoid/matcher/extract_attribute_spec.rb +40 -0
  669. data/spec/mongoid/persistable/creatable_spec.rb +119 -29
  670. data/spec/mongoid/persistable/deletable_spec.rb +307 -11
  671. data/spec/mongoid/persistable/destroyable_spec.rb +225 -12
  672. data/spec/mongoid/persistable/incrementable_spec.rb +29 -13
  673. data/spec/mongoid/persistable/logical_spec.rb +19 -3
  674. data/spec/mongoid/persistable/poppable_spec.rb +19 -3
  675. data/spec/mongoid/persistable/pullable_spec.rb +36 -6
  676. data/spec/mongoid/persistable/pushable_spec.rb +91 -7
  677. data/spec/mongoid/persistable/renamable_spec.rb +18 -2
  678. data/spec/mongoid/persistable/savable_spec.rb +218 -50
  679. data/spec/mongoid/persistable/settable_spec.rb +213 -32
  680. data/spec/mongoid/persistable/unsettable_spec.rb +19 -3
  681. data/spec/mongoid/persistable/updatable_spec.rb +222 -21
  682. data/spec/mongoid/persistable/upsertable_spec.rb +4 -2
  683. data/spec/mongoid/persistable_spec.rb +116 -25
  684. data/spec/mongoid/persistence_context_spec.rb +696 -0
  685. data/spec/mongoid/positional_spec.rb +3 -1
  686. data/spec/mongoid/query_cache_middleware_spec.rb +68 -0
  687. data/spec/mongoid/query_cache_spec.rb +647 -45
  688. data/spec/mongoid/relations/proxy_spec.rb +126 -98
  689. data/spec/mongoid/reloadable_spec.rb +346 -17
  690. data/spec/mongoid/scopable_spec.rb +82 -22
  691. data/spec/mongoid/selectable_spec.rb +8 -6
  692. data/spec/mongoid/serializable_spec.rb +160 -79
  693. data/spec/mongoid/shardable_models.rb +61 -0
  694. data/spec/mongoid/shardable_spec.rb +145 -26
  695. data/spec/mongoid/stateful_spec.rb +3 -1
  696. data/spec/mongoid/tasks/database_rake_spec.rb +15 -13
  697. data/spec/mongoid/tasks/database_spec.rb +3 -1
  698. data/spec/mongoid/threaded_spec.rb +72 -2
  699. data/spec/mongoid/timestamps/created/short_spec.rb +3 -1
  700. data/spec/mongoid/timestamps/created_spec.rb +3 -1
  701. data/spec/mongoid/timestamps/timeless_spec.rb +4 -2
  702. data/spec/mongoid/timestamps/updated/short_spec.rb +5 -3
  703. data/spec/mongoid/timestamps/updated_spec.rb +5 -3
  704. data/spec/mongoid/timestamps_spec.rb +8 -6
  705. data/spec/mongoid/touchable_spec.rb +588 -0
  706. data/spec/mongoid/touchable_spec_models.rb +53 -0
  707. data/spec/mongoid/traversable_spec.rb +1167 -1
  708. data/spec/mongoid/validatable/associated_spec.rb +3 -1
  709. data/spec/mongoid/validatable/format_spec.rb +2 -0
  710. data/spec/mongoid/validatable/length_spec.rb +2 -0
  711. data/spec/mongoid/validatable/numericality_spec.rb +2 -0
  712. data/spec/mongoid/validatable/presence_spec.rb +33 -26
  713. data/spec/mongoid/validatable/uniqueness_spec.rb +119 -81
  714. data/spec/mongoid/validatable_spec.rb +21 -3
  715. data/spec/mongoid_spec.rb +2 -0
  716. data/spec/rails/controller_extension/controller_runtime_spec.rb +112 -0
  717. data/spec/rails/mongoid_spec.rb +4 -2
  718. data/spec/shared/LICENSE +20 -0
  719. data/spec/shared/bin/get-mongodb-download-url +17 -0
  720. data/spec/shared/bin/s3-copy +45 -0
  721. data/spec/shared/bin/s3-upload +69 -0
  722. data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
  723. data/spec/shared/lib/mrss/cluster_config.rb +231 -0
  724. data/spec/shared/lib/mrss/constraints.rb +386 -0
  725. data/spec/shared/lib/mrss/docker_runner.rb +271 -0
  726. data/spec/shared/lib/mrss/event_subscriber.rb +200 -0
  727. data/spec/shared/lib/mrss/lite_constraints.rb +199 -0
  728. data/spec/shared/lib/mrss/server_version_registry.rb +120 -0
  729. data/spec/shared/lib/mrss/session_registry.rb +69 -0
  730. data/spec/shared/lib/mrss/session_registry_legacy.rb +60 -0
  731. data/spec/shared/lib/mrss/spec_organizer.rb +179 -0
  732. data/spec/shared/lib/mrss/utils.rb +15 -0
  733. data/spec/shared/share/Dockerfile.erb +339 -0
  734. data/spec/shared/share/haproxy-1.conf +16 -0
  735. data/spec/shared/share/haproxy-2.conf +17 -0
  736. data/spec/shared/shlib/distro.sh +74 -0
  737. data/spec/shared/shlib/server.sh +379 -0
  738. data/spec/shared/shlib/set_env.sh +132 -0
  739. data/spec/spec_helper.rb +133 -53
  740. data/spec/support/authorization.rb +2 -0
  741. data/spec/support/client_registry.rb +9 -0
  742. data/spec/support/constraints.rb +53 -0
  743. data/spec/support/expectations.rb +21 -3
  744. data/spec/support/helpers.rb +11 -0
  745. data/spec/support/macros.rb +42 -0
  746. data/spec/support/models/account.rb +38 -0
  747. data/spec/support/models/acolyte.rb +19 -0
  748. data/spec/support/models/actor.rb +20 -0
  749. data/spec/support/models/actress.rb +4 -0
  750. data/spec/support/models/address.rb +85 -0
  751. data/spec/support/models/address_component.rb +7 -0
  752. data/spec/support/models/address_number.rb +8 -0
  753. data/spec/support/models/agency.rb +7 -0
  754. data/spec/support/models/agent.rb +18 -0
  755. data/spec/support/models/album.rb +20 -0
  756. data/spec/support/models/alert.rb +9 -0
  757. data/spec/support/models/animal.rb +28 -0
  758. data/spec/support/models/answer.rb +8 -0
  759. data/spec/support/models/appointment.rb +9 -0
  760. data/spec/support/models/armrest.rb +9 -0
  761. data/spec/support/models/array_field.rb +7 -0
  762. data/spec/support/models/article.rb +14 -0
  763. data/spec/support/models/artist.rb +91 -0
  764. data/spec/support/models/artwork.rb +6 -0
  765. data/spec/support/models/audible_sound.rb +3 -0
  766. data/spec/support/models/audio.rb +7 -0
  767. data/spec/support/models/augmentation.rb +13 -0
  768. data/spec/support/models/author.rb +8 -0
  769. data/spec/support/models/baby.rb +6 -0
  770. data/spec/support/models/band.rb +37 -0
  771. data/spec/support/models/bar.rb +12 -0
  772. data/spec/support/models/basic.rb +8 -0
  773. data/spec/support/models/bed.rb +3 -0
  774. data/spec/support/models/big_palette.rb +4 -0
  775. data/spec/support/models/birthday.rb +15 -0
  776. data/spec/support/models/bolt.rb +7 -0
  777. data/spec/support/models/bomb.rb +6 -0
  778. data/spec/support/models/book.rb +18 -0
  779. data/spec/support/models/breed.rb +6 -0
  780. data/spec/support/models/browser.rb +8 -0
  781. data/spec/support/models/building.rb +8 -0
  782. data/spec/support/models/building_address.rb +9 -0
  783. data/spec/support/models/bus.rb +9 -0
  784. data/spec/support/models/business.rb +7 -0
  785. data/spec/support/models/callback_test.rb +11 -0
  786. data/spec/support/models/canvas.rb +27 -0
  787. data/spec/support/models/car.rb +3 -0
  788. data/spec/support/models/cat.rb +10 -0
  789. data/spec/support/models/category.rb +10 -0
  790. data/spec/support/models/child.rb +6 -0
  791. data/spec/support/models/child_doc.rb +24 -0
  792. data/spec/support/models/church.rb +8 -0
  793. data/spec/support/models/circle.rb +5 -0
  794. data/spec/support/models/circuit.rb +6 -0
  795. data/spec/support/models/circus.rb +9 -0
  796. data/spec/support/models/code.rb +13 -0
  797. data/spec/support/models/coding/pull_request.rb +11 -0
  798. data/spec/support/models/coding.rb +3 -0
  799. data/spec/support/models/comment.rb +18 -0
  800. data/spec/support/models/company.rb +7 -0
  801. data/spec/support/models/consumption_period.rb +9 -0
  802. data/spec/support/models/contextable_item.rb +7 -0
  803. data/spec/support/models/contractor.rb +7 -0
  804. data/spec/support/models/cookie.rb +8 -0
  805. data/spec/support/models/country_code.rb +12 -0
  806. data/spec/support/models/courier_job.rb +6 -0
  807. data/spec/support/models/crate.rb +12 -0
  808. data/spec/support/models/customer.rb +10 -0
  809. data/spec/support/models/customer_address.rb +11 -0
  810. data/spec/support/models/deed.rb +7 -0
  811. data/spec/support/models/definition.rb +11 -0
  812. data/spec/support/models/delegating_patient.rb +15 -0
  813. data/spec/support/models/description.rb +13 -0
  814. data/spec/support/models/dictionary.rb +18 -0
  815. data/spec/support/models/division.rb +12 -0
  816. data/spec/support/models/doctor.rb +14 -0
  817. data/spec/support/models/dog.rb +9 -0
  818. data/spec/support/models/dokument.rb +8 -0
  819. data/spec/support/models/draft.rb +11 -0
  820. data/spec/support/models/dragon.rb +6 -0
  821. data/spec/support/models/driver.rb +9 -0
  822. data/spec/support/models/drug.rb +8 -0
  823. data/spec/support/models/dungeon.rb +6 -0
  824. data/spec/support/models/edit.rb +7 -0
  825. data/spec/support/models/email.rb +8 -0
  826. data/spec/support/models/employer.rb +7 -0
  827. data/spec/support/models/entry.rb +8 -0
  828. data/spec/support/models/eraser.rb +3 -0
  829. data/spec/support/models/even.rb +9 -0
  830. data/spec/support/models/event.rb +24 -0
  831. data/spec/support/models/exhibition.rb +6 -0
  832. data/spec/support/models/exhibitor.rb +8 -0
  833. data/spec/support/models/explosion.rb +6 -0
  834. data/spec/support/models/eye.rb +11 -0
  835. data/spec/support/models/eye_bowl.rb +11 -0
  836. data/spec/support/models/face.rb +10 -0
  837. data/spec/support/models/favorite.rb +8 -0
  838. data/spec/support/models/filesystem.rb +7 -0
  839. data/spec/support/models/fire_hydrant.rb +8 -0
  840. data/spec/support/models/firefox.rb +6 -0
  841. data/spec/support/models/fish.rb +9 -0
  842. data/spec/support/models/folder.rb +9 -0
  843. data/spec/support/models/folder_item.rb +11 -0
  844. data/spec/support/models/fruits.rb +36 -0
  845. data/spec/support/models/game.rb +21 -0
  846. data/spec/support/models/ghost.rb +9 -0
  847. data/spec/support/models/guitar.rb +4 -0
  848. data/spec/support/models/hole.rb +12 -0
  849. data/spec/support/models/home.rb +6 -0
  850. data/spec/support/models/house.rb +8 -0
  851. data/spec/support/models/html_writer.rb +5 -0
  852. data/spec/support/models/id_key.rb +8 -0
  853. data/spec/support/models/idnodef.rb +7 -0
  854. data/spec/support/models/image.rb +24 -0
  855. data/spec/support/models/implant.rb +18 -0
  856. data/spec/support/models/instrument.rb +8 -0
  857. data/spec/support/models/item.rb +10 -0
  858. data/spec/support/models/jar.rb +9 -0
  859. data/spec/support/models/kaleidoscope.rb +8 -0
  860. data/spec/support/models/kangaroo.rb +6 -0
  861. data/spec/support/models/label.rb +50 -0
  862. data/spec/support/models/language.rb +7 -0
  863. data/spec/support/models/lat_lng.rb +17 -0
  864. data/spec/support/models/league.rb +13 -0
  865. data/spec/support/models/learner.rb +4 -0
  866. data/spec/support/models/line_item.rb +8 -0
  867. data/spec/support/models/location.rb +10 -0
  868. data/spec/support/models/login.rb +10 -0
  869. data/spec/support/models/manufacturer.rb +9 -0
  870. data/spec/support/models/meat.rb +6 -0
  871. data/spec/support/models/membership.rb +6 -0
  872. data/spec/support/models/message.rb +13 -0
  873. data/spec/support/models/minim.rb +6 -0
  874. data/spec/support/models/mixed_drink.rb +6 -0
  875. data/spec/support/models/mop.rb +24 -0
  876. data/spec/support/models/movie.rb +15 -0
  877. data/spec/support/models/my_hash.rb +4 -0
  878. data/spec/support/models/name.rb +26 -0
  879. data/spec/support/models/name_only.rb +8 -0
  880. data/spec/support/models/node.rb +7 -0
  881. data/spec/support/models/note.rb +19 -0
  882. data/spec/support/models/nut.rb +7 -0
  883. data/spec/support/models/odd.rb +9 -0
  884. data/spec/support/models/order.rb +12 -0
  885. data/spec/support/models/ordered_post.rb +13 -0
  886. data/spec/support/models/ordered_preference.rb +8 -0
  887. data/spec/support/models/oscar.rb +16 -0
  888. data/spec/support/models/other_owner_object.rb +4 -0
  889. data/spec/support/models/override.rb +18 -0
  890. data/spec/support/models/ownable.rb +8 -0
  891. data/spec/support/models/owner.rb +10 -0
  892. data/spec/support/models/pack.rb +5 -0
  893. data/spec/support/models/page.rb +18 -0
  894. data/spec/support/models/page_question.rb +6 -0
  895. data/spec/support/models/palette.rb +9 -0
  896. data/spec/support/models/parent.rb +7 -0
  897. data/spec/support/models/parent_doc.rb +8 -0
  898. data/spec/support/models/passport.rb +13 -0
  899. data/spec/support/models/patient.rb +11 -0
  900. data/spec/support/models/pdf_writer.rb +5 -0
  901. data/spec/support/models/pencil.rb +3 -0
  902. data/spec/support/models/person.rb +230 -0
  903. data/spec/support/models/pet.rb +25 -0
  904. data/spec/support/models/pet_owner.rb +8 -0
  905. data/spec/support/models/phone.rb +13 -0
  906. data/spec/support/models/piano.rb +4 -0
  907. data/spec/support/models/pizza.rb +9 -0
  908. data/spec/support/models/player.rb +37 -0
  909. data/spec/support/models/post.rb +52 -0
  910. data/spec/support/models/post_genre.rb +8 -0
  911. data/spec/support/models/powerup.rb +13 -0
  912. data/spec/support/models/preference.rb +11 -0
  913. data/spec/support/models/princess.rb +12 -0
  914. data/spec/support/models/product.rb +19 -0
  915. data/spec/support/models/profile.rb +17 -0
  916. data/spec/support/models/pronunciation.rb +7 -0
  917. data/spec/support/models/pub.rb +8 -0
  918. data/spec/support/models/publication/encyclopedia.rb +11 -0
  919. data/spec/support/models/publication/review.rb +13 -0
  920. data/spec/support/models/publication.rb +4 -0
  921. data/spec/support/models/purchase.rb +6 -0
  922. data/spec/support/models/purchased_item.rb +10 -0
  923. data/spec/support/models/question.rb +10 -0
  924. data/spec/support/models/quiz.rb +9 -0
  925. data/spec/support/models/rating.rb +10 -0
  926. data/spec/support/models/record.rb +54 -0
  927. data/spec/support/models/registry.rb +6 -0
  928. data/spec/support/models/role.rb +9 -0
  929. data/spec/support/models/root_category.rb +6 -0
  930. data/spec/support/models/sandwich.rb +11 -0
  931. data/spec/support/models/scheduler.rb +9 -0
  932. data/spec/support/models/scribe.rb +7 -0
  933. data/spec/support/models/sealer.rb +7 -0
  934. data/spec/support/models/seat.rb +24 -0
  935. data/spec/support/models/seo.rb +9 -0
  936. data/spec/support/models/series.rb +7 -0
  937. data/spec/support/models/server.rb +15 -0
  938. data/spec/support/models/service.rb +24 -0
  939. data/spec/support/models/shape.rb +14 -0
  940. data/spec/support/models/shelf.rb +7 -0
  941. data/spec/support/models/shipment_address.rb +5 -0
  942. data/spec/support/models/shipping_container.rb +7 -0
  943. data/spec/support/models/shipping_pack.rb +5 -0
  944. data/spec/support/models/shirt.rb +11 -0
  945. data/spec/support/models/shop.rb +8 -0
  946. data/spec/support/models/short_agent.rb +6 -0
  947. data/spec/support/models/short_quiz.rb +7 -0
  948. data/spec/support/models/simple.rb +7 -0
  949. data/spec/support/models/slave.rb +8 -0
  950. data/spec/support/models/song.rb +10 -0
  951. data/spec/support/models/sound.rb +7 -0
  952. data/spec/support/models/spacer.rb +7 -0
  953. data/spec/support/models/square.rb +6 -0
  954. data/spec/support/models/staff.rb +9 -0
  955. data/spec/support/models/store_as_dup_test1.rb +7 -0
  956. data/spec/support/models/store_as_dup_test2.rb +7 -0
  957. data/spec/support/models/store_as_dup_test3.rb +7 -0
  958. data/spec/support/models/store_as_dup_test4.rb +7 -0
  959. data/spec/support/models/strategy.rb +5 -0
  960. data/spec/support/models/sub_item.rb +5 -0
  961. data/spec/support/models/subscription.rb +7 -0
  962. data/spec/support/models/survey.rb +7 -0
  963. data/spec/support/models/symptom.rb +8 -0
  964. data/spec/support/models/system_role.rb +7 -0
  965. data/spec/support/models/tag.rb +10 -0
  966. data/spec/support/models/target.rb +7 -0
  967. data/spec/support/models/template.rb +7 -0
  968. data/spec/support/models/thing.rb +11 -0
  969. data/spec/support/models/threadlocker.rb +7 -0
  970. data/spec/support/models/title.rb +5 -0
  971. data/spec/support/models/tool.rb +10 -0
  972. data/spec/support/models/topping.rb +7 -0
  973. data/spec/support/models/toy.rb +9 -0
  974. data/spec/support/models/track.rb +40 -0
  975. data/spec/support/models/translation.rb +7 -0
  976. data/spec/support/models/tree.rb +11 -0
  977. data/spec/support/models/truck.rb +7 -0
  978. data/spec/support/models/updatable.rb +7 -0
  979. data/spec/support/models/user.rb +23 -0
  980. data/spec/support/models/user_account.rb +12 -0
  981. data/spec/support/models/validation_callback.rb +12 -0
  982. data/spec/support/models/vehicle.rb +18 -0
  983. data/spec/support/models/version.rb +7 -0
  984. data/spec/support/models/vertex.rb +8 -0
  985. data/spec/support/models/vet_visit.rb +7 -0
  986. data/spec/support/models/video.rb +15 -0
  987. data/spec/support/models/video_game.rb +3 -0
  988. data/spec/support/models/washer.rb +7 -0
  989. data/spec/support/models/weapon.rb +13 -0
  990. data/spec/support/models/wiki_page.rb +17 -0
  991. data/spec/support/models/word.rb +17 -0
  992. data/spec/support/models/word_origin.rb +13 -0
  993. data/spec/support/models/writer.rb +13 -0
  994. data/spec/support/shared/time.rb +53 -0
  995. data/spec/support/spec_config.rb +60 -0
  996. data.tar.gz.sig +0 -0
  997. metadata +1347 -813
  998. metadata.gz.sig +0 -0
  999. data/lib/mongoid/clients/thread_options.rb +0 -19
  1000. data/lib/mongoid/extensions/origin/regexp_raw.rb +0 -43
  1001. data/lib/mongoid/matchable/all.rb +0 -27
  1002. data/lib/mongoid/matchable/and.rb +0 -30
  1003. data/lib/mongoid/matchable/default.rb +0 -72
  1004. data/lib/mongoid/matchable/exists.rb +0 -23
  1005. data/lib/mongoid/matchable/gt.rb +0 -21
  1006. data/lib/mongoid/matchable/gte.rb +0 -21
  1007. data/lib/mongoid/matchable/in.rb +0 -24
  1008. data/lib/mongoid/matchable/lt.rb +0 -21
  1009. data/lib/mongoid/matchable/lte.rb +0 -21
  1010. data/lib/mongoid/matchable/ne.rb +0 -21
  1011. data/lib/mongoid/matchable/nin.rb +0 -22
  1012. data/lib/mongoid/matchable/or.rb +0 -33
  1013. data/lib/mongoid/matchable/regexp.rb +0 -27
  1014. data/lib/mongoid/matchable/size.rb +0 -21
  1015. data/lib/mongoid/railties/document.rb +0 -12
  1016. data/lib/mongoid/relations/accessors.rb +0 -264
  1017. data/lib/mongoid/relations/auto_save.rb +0 -86
  1018. data/lib/mongoid/relations/binding.rb +0 -218
  1019. data/lib/mongoid/relations/bindings/embedded/in.rb +0 -59
  1020. data/lib/mongoid/relations/bindings/embedded/many.rb +0 -43
  1021. data/lib/mongoid/relations/bindings/embedded/one.rb +0 -45
  1022. data/lib/mongoid/relations/bindings/referenced/in.rb +0 -65
  1023. data/lib/mongoid/relations/bindings/referenced/many.rb +0 -38
  1024. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +0 -70
  1025. data/lib/mongoid/relations/bindings/referenced/one.rb +0 -44
  1026. data/lib/mongoid/relations/bindings.rb +0 -9
  1027. data/lib/mongoid/relations/builder.rb +0 -57
  1028. data/lib/mongoid/relations/builders/embedded/in.rb +0 -29
  1029. data/lib/mongoid/relations/builders/embedded/many.rb +0 -36
  1030. data/lib/mongoid/relations/builders/embedded/one.rb +0 -30
  1031. data/lib/mongoid/relations/builders/nested_attributes/many.rb +0 -199
  1032. data/lib/mongoid/relations/builders/nested_attributes/one.rb +0 -126
  1033. data/lib/mongoid/relations/builders/referenced/in.rb +0 -26
  1034. data/lib/mongoid/relations/builders/referenced/many.rb +0 -26
  1035. data/lib/mongoid/relations/builders/referenced/many_to_many.rb +0 -39
  1036. data/lib/mongoid/relations/builders/referenced/one.rb +0 -26
  1037. data/lib/mongoid/relations/builders.rb +0 -106
  1038. data/lib/mongoid/relations/cascading/delete.rb +0 -44
  1039. data/lib/mongoid/relations/cascading/destroy.rb +0 -43
  1040. data/lib/mongoid/relations/cascading/nullify.rb +0 -35
  1041. data/lib/mongoid/relations/cascading/restrict.rb +0 -39
  1042. data/lib/mongoid/relations/cascading.rb +0 -56
  1043. data/lib/mongoid/relations/constraint.rb +0 -55
  1044. data/lib/mongoid/relations/conversions.rb +0 -34
  1045. data/lib/mongoid/relations/counter_cache.rb +0 -150
  1046. data/lib/mongoid/relations/cyclic.rb +0 -107
  1047. data/lib/mongoid/relations/eager/base.rb +0 -153
  1048. data/lib/mongoid/relations/eager/belongs_to.rb +0 -31
  1049. data/lib/mongoid/relations/eager/has_and_belongs_to_many.rb +0 -47
  1050. data/lib/mongoid/relations/eager/has_many.rb +0 -38
  1051. data/lib/mongoid/relations/eager/has_one.rb +0 -30
  1052. data/lib/mongoid/relations/eager.rb +0 -41
  1053. data/lib/mongoid/relations/embedded/batchable.rb +0 -382
  1054. data/lib/mongoid/relations/embedded/in.rb +0 -229
  1055. data/lib/mongoid/relations/embedded/many.rb +0 -642
  1056. data/lib/mongoid/relations/embedded/one.rb +0 -222
  1057. data/lib/mongoid/relations/macros.rb +0 -359
  1058. data/lib/mongoid/relations/many.rb +0 -238
  1059. data/lib/mongoid/relations/marshalable.rb +0 -32
  1060. data/lib/mongoid/relations/metadata.rb +0 -1179
  1061. data/lib/mongoid/relations/nested_builder.rb +0 -74
  1062. data/lib/mongoid/relations/one.rb +0 -60
  1063. data/lib/mongoid/relations/options.rb +0 -49
  1064. data/lib/mongoid/relations/polymorphic.rb +0 -39
  1065. data/lib/mongoid/relations/proxy.rb +0 -245
  1066. data/lib/mongoid/relations/referenced/in.rb +0 -295
  1067. data/lib/mongoid/relations/referenced/many.rb +0 -808
  1068. data/lib/mongoid/relations/referenced/many_to_many.rb +0 -472
  1069. data/lib/mongoid/relations/referenced/one.rb +0 -276
  1070. data/lib/mongoid/relations/reflections.rb +0 -64
  1071. data/lib/mongoid/relations/synchronization.rb +0 -169
  1072. data/lib/mongoid/relations/targets/enumerable.rb +0 -473
  1073. data/lib/mongoid/relations/targets.rb +0 -2
  1074. data/lib/mongoid/relations/touchable.rb +0 -94
  1075. data/lib/mongoid/relations.rb +0 -148
  1076. data/lib/support/ruby_version.rb +0 -26
  1077. data/spec/app/models/account.rb +0 -36
  1078. data/spec/app/models/acolyte.rb +0 -17
  1079. data/spec/app/models/actor.rb +0 -18
  1080. data/spec/app/models/actress.rb +0 -2
  1081. data/spec/app/models/address.rb +0 -79
  1082. data/spec/app/models/address_component.rb +0 -5
  1083. data/spec/app/models/address_number.rb +0 -6
  1084. data/spec/app/models/agency.rb +0 -5
  1085. data/spec/app/models/agent.rb +0 -14
  1086. data/spec/app/models/album.rb +0 -14
  1087. data/spec/app/models/alert.rb +0 -7
  1088. data/spec/app/models/animal.rb +0 -25
  1089. data/spec/app/models/answer.rb +0 -6
  1090. data/spec/app/models/appointment.rb +0 -7
  1091. data/spec/app/models/article.rb +0 -12
  1092. data/spec/app/models/artist.rb +0 -66
  1093. data/spec/app/models/artwork.rb +0 -4
  1094. data/spec/app/models/audio.rb +0 -5
  1095. data/spec/app/models/augmentation.rb +0 -11
  1096. data/spec/app/models/author.rb +0 -6
  1097. data/spec/app/models/baby.rb +0 -4
  1098. data/spec/app/models/band.rb +0 -28
  1099. data/spec/app/models/bar.rb +0 -10
  1100. data/spec/app/models/basic.rb +0 -6
  1101. data/spec/app/models/bed.rb +0 -1
  1102. data/spec/app/models/big_palette.rb +0 -2
  1103. data/spec/app/models/birthday.rb +0 -13
  1104. data/spec/app/models/bomb.rb +0 -4
  1105. data/spec/app/models/book.rb +0 -15
  1106. data/spec/app/models/breed.rb +0 -4
  1107. data/spec/app/models/browser.rb +0 -6
  1108. data/spec/app/models/building.rb +0 -6
  1109. data/spec/app/models/building_address.rb +0 -7
  1110. data/spec/app/models/bus.rb +0 -7
  1111. data/spec/app/models/business.rb +0 -5
  1112. data/spec/app/models/callback_test.rb +0 -9
  1113. data/spec/app/models/canvas.rb +0 -25
  1114. data/spec/app/models/car.rb +0 -1
  1115. data/spec/app/models/cat.rb +0 -8
  1116. data/spec/app/models/category.rb +0 -8
  1117. data/spec/app/models/child.rb +0 -4
  1118. data/spec/app/models/child_doc.rb +0 -22
  1119. data/spec/app/models/church.rb +0 -6
  1120. data/spec/app/models/circle.rb +0 -3
  1121. data/spec/app/models/circuit.rb +0 -4
  1122. data/spec/app/models/circus.rb +0 -7
  1123. data/spec/app/models/code.rb +0 -11
  1124. data/spec/app/models/comment.rb +0 -16
  1125. data/spec/app/models/company.rb +0 -5
  1126. data/spec/app/models/consumption_period.rb +0 -7
  1127. data/spec/app/models/contextable_item.rb +0 -5
  1128. data/spec/app/models/contractor.rb +0 -5
  1129. data/spec/app/models/cookie.rb +0 -6
  1130. data/spec/app/models/country_code.rb +0 -8
  1131. data/spec/app/models/courier_job.rb +0 -4
  1132. data/spec/app/models/definition.rb +0 -9
  1133. data/spec/app/models/description.rb +0 -11
  1134. data/spec/app/models/dictionary.rb +0 -10
  1135. data/spec/app/models/division.rb +0 -10
  1136. data/spec/app/models/doctor.rb +0 -12
  1137. data/spec/app/models/dog.rb +0 -7
  1138. data/spec/app/models/dokument.rb +0 -5
  1139. data/spec/app/models/draft.rb +0 -9
  1140. data/spec/app/models/dragon.rb +0 -4
  1141. data/spec/app/models/driver.rb +0 -7
  1142. data/spec/app/models/drug.rb +0 -6
  1143. data/spec/app/models/dungeon.rb +0 -4
  1144. data/spec/app/models/edit.rb +0 -5
  1145. data/spec/app/models/email.rb +0 -6
  1146. data/spec/app/models/employer.rb +0 -5
  1147. data/spec/app/models/entry.rb +0 -6
  1148. data/spec/app/models/eraser.rb +0 -1
  1149. data/spec/app/models/even.rb +0 -7
  1150. data/spec/app/models/event.rb +0 -22
  1151. data/spec/app/models/exhibition.rb +0 -4
  1152. data/spec/app/models/exhibitor.rb +0 -6
  1153. data/spec/app/models/explosion.rb +0 -4
  1154. data/spec/app/models/eye.rb +0 -9
  1155. data/spec/app/models/eye_bowl.rb +0 -9
  1156. data/spec/app/models/face.rb +0 -8
  1157. data/spec/app/models/favorite.rb +0 -6
  1158. data/spec/app/models/filesystem.rb +0 -5
  1159. data/spec/app/models/fire_hydrant.rb +0 -6
  1160. data/spec/app/models/firefox.rb +0 -4
  1161. data/spec/app/models/fish.rb +0 -7
  1162. data/spec/app/models/folder.rb +0 -7
  1163. data/spec/app/models/folder_item.rb +0 -9
  1164. data/spec/app/models/fruits.rb +0 -34
  1165. data/spec/app/models/game.rb +0 -19
  1166. data/spec/app/models/ghost.rb +0 -7
  1167. data/spec/app/models/home.rb +0 -4
  1168. data/spec/app/models/house.rb +0 -6
  1169. data/spec/app/models/html_writer.rb +0 -3
  1170. data/spec/app/models/id_key.rb +0 -6
  1171. data/spec/app/models/image.rb +0 -22
  1172. data/spec/app/models/implant.rb +0 -16
  1173. data/spec/app/models/item.rb +0 -8
  1174. data/spec/app/models/jar.rb +0 -7
  1175. data/spec/app/models/kaleidoscope.rb +0 -6
  1176. data/spec/app/models/kangaroo.rb +0 -4
  1177. data/spec/app/models/label.rb +0 -47
  1178. data/spec/app/models/language.rb +0 -5
  1179. data/spec/app/models/lat_lng.rb +0 -15
  1180. data/spec/app/models/league.rb +0 -11
  1181. data/spec/app/models/learner.rb +0 -2
  1182. data/spec/app/models/line_item.rb +0 -6
  1183. data/spec/app/models/location.rb +0 -8
  1184. data/spec/app/models/login.rb +0 -8
  1185. data/spec/app/models/manufacturer.rb +0 -7
  1186. data/spec/app/models/meat.rb +0 -4
  1187. data/spec/app/models/membership.rb +0 -4
  1188. data/spec/app/models/message.rb +0 -11
  1189. data/spec/app/models/mixed_drink.rb +0 -4
  1190. data/spec/app/models/movie.rb +0 -13
  1191. data/spec/app/models/my_hash.rb +0 -2
  1192. data/spec/app/models/name.rb +0 -24
  1193. data/spec/app/models/node.rb +0 -5
  1194. data/spec/app/models/note.rb +0 -17
  1195. data/spec/app/models/odd.rb +0 -7
  1196. data/spec/app/models/ordered_post.rb +0 -6
  1197. data/spec/app/models/ordered_preference.rb +0 -6
  1198. data/spec/app/models/oscar.rb +0 -15
  1199. data/spec/app/models/override.rb +0 -16
  1200. data/spec/app/models/ownable.rb +0 -6
  1201. data/spec/app/models/owner.rb +0 -6
  1202. data/spec/app/models/pack.rb +0 -3
  1203. data/spec/app/models/page.rb +0 -16
  1204. data/spec/app/models/page_question.rb +0 -4
  1205. data/spec/app/models/palette.rb +0 -7
  1206. data/spec/app/models/parent.rb +0 -5
  1207. data/spec/app/models/parent_doc.rb +0 -6
  1208. data/spec/app/models/passport.rb +0 -5
  1209. data/spec/app/models/patient.rb +0 -9
  1210. data/spec/app/models/pdf_writer.rb +0 -3
  1211. data/spec/app/models/pencil.rb +0 -1
  1212. data/spec/app/models/person.rb +0 -207
  1213. data/spec/app/models/pet.rb +0 -23
  1214. data/spec/app/models/pet_owner.rb +0 -6
  1215. data/spec/app/models/phone.rb +0 -9
  1216. data/spec/app/models/pizza.rb +0 -7
  1217. data/spec/app/models/player.rb +0 -35
  1218. data/spec/app/models/post.rb +0 -50
  1219. data/spec/app/models/post_genre.rb +0 -6
  1220. data/spec/app/models/powerup.rb +0 -11
  1221. data/spec/app/models/preference.rb +0 -9
  1222. data/spec/app/models/princess.rb +0 -8
  1223. data/spec/app/models/product.rb +0 -17
  1224. data/spec/app/models/profile.rb +0 -5
  1225. data/spec/app/models/pronunciation.rb +0 -5
  1226. data/spec/app/models/pub.rb +0 -6
  1227. data/spec/app/models/purchase.rb +0 -4
  1228. data/spec/app/models/question.rb +0 -8
  1229. data/spec/app/models/quiz.rb +0 -7
  1230. data/spec/app/models/rating.rb +0 -8
  1231. data/spec/app/models/record.rb +0 -51
  1232. data/spec/app/models/registry.rb +0 -4
  1233. data/spec/app/models/role.rb +0 -7
  1234. data/spec/app/models/root_category.rb +0 -4
  1235. data/spec/app/models/sandwich.rb +0 -9
  1236. data/spec/app/models/scheduler.rb +0 -7
  1237. data/spec/app/models/seo.rb +0 -7
  1238. data/spec/app/models/series.rb +0 -4
  1239. data/spec/app/models/server.rb +0 -13
  1240. data/spec/app/models/service.rb +0 -22
  1241. data/spec/app/models/shape.rb +0 -12
  1242. data/spec/app/models/shelf.rb +0 -5
  1243. data/spec/app/models/shipment_address.rb +0 -2
  1244. data/spec/app/models/shipping_container.rb +0 -5
  1245. data/spec/app/models/shipping_pack.rb +0 -3
  1246. data/spec/app/models/shop.rb +0 -6
  1247. data/spec/app/models/short_agent.rb +0 -4
  1248. data/spec/app/models/short_quiz.rb +0 -5
  1249. data/spec/app/models/simple.rb +0 -5
  1250. data/spec/app/models/slave.rb +0 -6
  1251. data/spec/app/models/song.rb +0 -8
  1252. data/spec/app/models/sound.rb +0 -5
  1253. data/spec/app/models/square.rb +0 -4
  1254. data/spec/app/models/staff.rb +0 -7
  1255. data/spec/app/models/store_as_dup_test1.rb +0 -5
  1256. data/spec/app/models/store_as_dup_test2.rb +0 -5
  1257. data/spec/app/models/strategy.rb +0 -3
  1258. data/spec/app/models/sub_item.rb +0 -3
  1259. data/spec/app/models/subscription.rb +0 -4
  1260. data/spec/app/models/survey.rb +0 -5
  1261. data/spec/app/models/symptom.rb +0 -6
  1262. data/spec/app/models/tag.rb +0 -8
  1263. data/spec/app/models/target.rb +0 -5
  1264. data/spec/app/models/template.rb +0 -5
  1265. data/spec/app/models/thing.rb +0 -9
  1266. data/spec/app/models/title.rb +0 -3
  1267. data/spec/app/models/tool.rb +0 -8
  1268. data/spec/app/models/topping.rb +0 -5
  1269. data/spec/app/models/track.rb +0 -38
  1270. data/spec/app/models/translation.rb +0 -5
  1271. data/spec/app/models/tree.rb +0 -9
  1272. data/spec/app/models/truck.rb +0 -3
  1273. data/spec/app/models/user.rb +0 -21
  1274. data/spec/app/models/user_account.rb +0 -10
  1275. data/spec/app/models/validation_callback.rb +0 -10
  1276. data/spec/app/models/vehicle.rb +0 -11
  1277. data/spec/app/models/version.rb +0 -5
  1278. data/spec/app/models/vet_visit.rb +0 -5
  1279. data/spec/app/models/video.rb +0 -13
  1280. data/spec/app/models/video_game.rb +0 -1
  1281. data/spec/app/models/weapon.rb +0 -11
  1282. data/spec/app/models/wiki_page.rb +0 -14
  1283. data/spec/app/models/word.rb +0 -15
  1284. data/spec/app/models/word_origin.rb +0 -11
  1285. data/spec/app/models/writer.rb +0 -11
  1286. data/spec/mongoid/extensions/origin/regexp_raw_spec.rb +0 -89
  1287. data/spec/mongoid/fields/internal/foreign_keys/array_spec.rb +0 -184
  1288. data/spec/mongoid/fields/internal/foreign_keys/object_spec.rb +0 -201
  1289. data/spec/mongoid/matchable/all_spec.rb +0 -31
  1290. data/spec/mongoid/matchable/and_spec.rb +0 -187
  1291. data/spec/mongoid/matchable/default_spec.rb +0 -130
  1292. data/spec/mongoid/matchable/exists_spec.rb +0 -57
  1293. data/spec/mongoid/matchable/gt_spec.rb +0 -75
  1294. data/spec/mongoid/matchable/gte_spec.rb +0 -74
  1295. data/spec/mongoid/matchable/in_spec.rb +0 -49
  1296. data/spec/mongoid/matchable/lt_spec.rb +0 -74
  1297. data/spec/mongoid/matchable/lte_spec.rb +0 -74
  1298. data/spec/mongoid/matchable/ne_spec.rb +0 -46
  1299. data/spec/mongoid/matchable/nin_spec.rb +0 -48
  1300. data/spec/mongoid/matchable/or_spec.rb +0 -131
  1301. data/spec/mongoid/matchable/regexp_spec.rb +0 -59
  1302. data/spec/mongoid/matchable/size_spec.rb +0 -25
  1303. data/spec/mongoid/matchable_spec.rb +0 -708
  1304. data/spec/mongoid/railties/document_spec.rb +0 -24
  1305. data/spec/mongoid/relations/accessors_spec.rb +0 -821
  1306. data/spec/mongoid/relations/auto_save_spec.rb +0 -281
  1307. data/spec/mongoid/relations/bindings/embedded/in_spec.rb +0 -171
  1308. data/spec/mongoid/relations/bindings/embedded/many_spec.rb +0 -54
  1309. data/spec/mongoid/relations/bindings/embedded/one_spec.rb +0 -77
  1310. data/spec/mongoid/relations/bindings/referenced/in_spec.rb +0 -241
  1311. data/spec/mongoid/relations/bindings/referenced/many_spec.rb +0 -153
  1312. data/spec/mongoid/relations/bindings/referenced/many_to_many_spec.rb +0 -178
  1313. data/spec/mongoid/relations/bindings/referenced/one_spec.rb +0 -131
  1314. data/spec/mongoid/relations/builders/embedded/in_spec.rb +0 -34
  1315. data/spec/mongoid/relations/builders/embedded/many_spec.rb +0 -132
  1316. data/spec/mongoid/relations/builders/embedded/one_spec.rb +0 -99
  1317. data/spec/mongoid/relations/builders/nested_attributes/many_spec.rb +0 -233
  1318. data/spec/mongoid/relations/builders/nested_attributes/one_spec.rb +0 -250
  1319. data/spec/mongoid/relations/builders/referenced/in_spec.rb +0 -230
  1320. data/spec/mongoid/relations/builders/referenced/many_spec.rb +0 -137
  1321. data/spec/mongoid/relations/builders/referenced/many_to_many_spec.rb +0 -178
  1322. data/spec/mongoid/relations/builders/referenced/one_spec.rb +0 -111
  1323. data/spec/mongoid/relations/builders_spec.rb +0 -226
  1324. data/spec/mongoid/relations/cascading/delete_spec.rb +0 -101
  1325. data/spec/mongoid/relations/cascading/destroy_spec.rb +0 -47
  1326. data/spec/mongoid/relations/cascading/nullify_spec.rb +0 -32
  1327. data/spec/mongoid/relations/cascading/restrict_spec.rb +0 -68
  1328. data/spec/mongoid/relations/cascading_spec.rb +0 -355
  1329. data/spec/mongoid/relations/constraint_spec.rb +0 -75
  1330. data/spec/mongoid/relations/conversions_spec.rb +0 -128
  1331. data/spec/mongoid/relations/counter_cache_spec.rb +0 -389
  1332. data/spec/mongoid/relations/cyclic_spec.rb +0 -178
  1333. data/spec/mongoid/relations/eager/belongs_to_spec.rb +0 -163
  1334. data/spec/mongoid/relations/eager/has_and_belongs_to_many_spec.rb +0 -117
  1335. data/spec/mongoid/relations/eager/has_many_spec.rb +0 -244
  1336. data/spec/mongoid/relations/eager/has_one_spec.rb +0 -163
  1337. data/spec/mongoid/relations/eager_spec.rb +0 -239
  1338. data/spec/mongoid/relations/embedded/dirty_spec.rb +0 -65
  1339. data/spec/mongoid/relations/embedded/in_spec.rb +0 -579
  1340. data/spec/mongoid/relations/embedded/many_spec.rb +0 -4166
  1341. data/spec/mongoid/relations/embedded/one_spec.rb +0 -1067
  1342. data/spec/mongoid/relations/macros_spec.rb +0 -631
  1343. data/spec/mongoid/relations/metadata_spec.rb +0 -1971
  1344. data/spec/mongoid/relations/options_spec.rb +0 -35
  1345. data/spec/mongoid/relations/polymorphic_spec.rb +0 -128
  1346. data/spec/mongoid/relations/referenced/in_spec.rb +0 -1326
  1347. data/spec/mongoid/relations/referenced/many_spec.rb +0 -3806
  1348. data/spec/mongoid/relations/referenced/many_to_many_spec.rb +0 -3589
  1349. data/spec/mongoid/relations/referenced/one_spec.rb +0 -1240
  1350. data/spec/mongoid/relations/reflections_spec.rb +0 -101
  1351. data/spec/mongoid/relations/synchronization_spec.rb +0 -449
  1352. data/spec/mongoid/relations/targets/enumerable_spec.rb +0 -1717
  1353. data/spec/mongoid/relations/touchable_spec.rb +0 -362
  1354. data/spec/mongoid/relations_spec.rb +0 -189
@@ -1,4166 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Mongoid::Relations::Embedded::Many do
4
-
5
- [ :<<, :push ].each do |method|
6
-
7
- describe "##{method}" do
8
-
9
- context "when the parent is a new record" do
10
-
11
- let(:person) do
12
- Person.new
13
- end
14
-
15
- let(:address) do
16
- Address.new
17
- end
18
-
19
- let!(:added) do
20
- person.addresses.send(method, address)
21
- end
22
-
23
- it "appends to the target" do
24
- expect(person.addresses).to eq([ address ])
25
- end
26
-
27
- it "sets the base on the inverse relation" do
28
- expect(address.addressable).to eq(person)
29
- end
30
-
31
- it "sets the same instance on the inverse relation" do
32
- expect(address.addressable).to eql(person)
33
- end
34
-
35
- it "does not save the new document" do
36
- expect(address).to_not be_persisted
37
- end
38
-
39
- it "sets the parent on the child" do
40
- expect(address._parent).to eq(person)
41
- end
42
-
43
- it "sets the metadata on the child" do
44
- expect(address.__metadata).to_not be_nil
45
- end
46
-
47
- it "sets the index on the child" do
48
- expect(address._index).to eq(0)
49
- end
50
-
51
- it "returns the relation" do
52
- expect(added).to eq(person.addresses)
53
- end
54
-
55
- context "with a limiting default scope" do
56
-
57
- context "when the document matches the scope" do
58
-
59
- let(:active) do
60
- Appointment.new
61
- end
62
-
63
- before do
64
- person.appointments.send(method, active)
65
- end
66
-
67
- it "appends to the target" do
68
- expect(person.appointments.target).to eq([ active ])
69
- end
70
-
71
- it "appends to the _unscoped" do
72
- expect(person.appointments.send(:_unscoped)).to eq([ active ])
73
- end
74
- end
75
-
76
- context "when the document does not match the scope" do
77
-
78
- let(:inactive) do
79
- Appointment.new(active: false)
80
- end
81
-
82
- before do
83
- person.appointments.send(method, inactive)
84
- end
85
-
86
- it "doesn't append to the target" do
87
- expect(person.appointments.target).to_not eq([ inactive ])
88
- end
89
-
90
- it "appends to the _unscoped" do
91
- expect(person.appointments.send(:_unscoped)).to eq([ inactive ])
92
- end
93
- end
94
- end
95
- end
96
-
97
- context "when the parent is not a new record" do
98
-
99
- let(:person) do
100
- Person.create
101
- end
102
-
103
- let(:address) do
104
- Address.new
105
- end
106
-
107
- before do
108
- person.addresses.send(method, address)
109
- end
110
-
111
- it "saves the new document" do
112
- expect(address).to be_persisted
113
- end
114
- end
115
-
116
- context "when appending more than one document at once" do
117
-
118
- let(:person) do
119
- Person.create
120
- end
121
-
122
- let(:address_one) do
123
- Address.new
124
- end
125
-
126
- let(:address_two) do
127
- Address.new
128
- end
129
-
130
- let!(:added) do
131
- person.addresses.send(method, [ address_one, address_two ])
132
- end
133
-
134
- it "saves the first document" do
135
- expect(address_one).to be_persisted
136
- end
137
-
138
- it "saves the second document" do
139
- expect(address_two).to be_persisted
140
- end
141
-
142
- it "returns the relation" do
143
- expect(added).to eq(person.addresses)
144
- end
145
- end
146
-
147
- context "when the parent and child have a cyclic relation" do
148
-
149
- context "when the parent is a new record" do
150
-
151
- let(:parent_role) do
152
- Role.new
153
- end
154
-
155
- let(:child_role) do
156
- Role.new
157
- end
158
-
159
- before do
160
- parent_role.child_roles.send(method, child_role)
161
- end
162
-
163
- it "appends to the target" do
164
- expect(parent_role.child_roles).to eq([ child_role ])
165
- end
166
-
167
- it "sets the base on the inverse relation" do
168
- expect(child_role.parent_role).to eq(parent_role)
169
- end
170
-
171
- it "sets the same instance on the inverse relation" do
172
- expect(child_role.parent_role).to eql(parent_role)
173
- end
174
-
175
- it "does not save the new document" do
176
- expect(child_role).to_not be_persisted
177
- end
178
-
179
- it "sets the parent on the child" do
180
- expect(child_role._parent).to eq(parent_role)
181
- end
182
-
183
- it "sets the metadata on the child" do
184
- expect(child_role.__metadata).to_not be_nil
185
- end
186
-
187
- it "sets the index on the child" do
188
- expect(child_role._index).to eq(0)
189
- end
190
- end
191
-
192
- context "when the parent is not a new record" do
193
-
194
- let(:parent_role) do
195
- Role.create(name: "CEO")
196
- end
197
-
198
- let(:child_role) do
199
- Role.new(name: "COO")
200
- end
201
-
202
- before do
203
- parent_role.child_roles.send(method, child_role)
204
- end
205
-
206
- it "saves the new document" do
207
- expect(child_role).to be_persisted
208
- end
209
- end
210
- end
211
-
212
- context "when the child has one sided many to many relation" do
213
- let(:person) do
214
- Person.create
215
- end
216
-
217
- let(:message) do
218
- Message.new
219
- end
220
-
221
- context "assign parent first" do
222
- before do
223
- message.person = person
224
- message.receviers.send(method, person)
225
- end
226
-
227
- it "appends to the relation array" do
228
- expect(message.receviers).to include(person)
229
- end
230
- end
231
-
232
- context "not assign parent" do
233
- before do
234
- message.receviers.send(method, person)
235
- end
236
-
237
- it "appends to the relation array" do
238
- expect(message.receviers).to include(person)
239
- end
240
- end
241
- end
242
- end
243
- end
244
-
245
- describe "#=" do
246
-
247
- context "when the parent is a new record" do
248
-
249
- let(:person) do
250
- Person.new
251
- end
252
-
253
- let(:address) do
254
- Address.new
255
- end
256
-
257
- before do
258
- person.addresses = [ address ]
259
- end
260
-
261
- it "sets the target of the relation" do
262
- expect(person.addresses).to eq([ address ])
263
- end
264
-
265
- it "sets the _unscoped of the relation" do
266
- expect(person.addresses.send(:_unscoped)).to eq([ address ])
267
- end
268
-
269
- it "sets the base on the inverse relation" do
270
- expect(address.addressable).to eq(person)
271
- end
272
-
273
- it "sets the same instance on the inverse relation" do
274
- expect(address.addressable).to eql(person)
275
- end
276
-
277
- it "does not save the target" do
278
- expect(address).to_not be_persisted
279
- end
280
-
281
- it "sets the parent on the child" do
282
- expect(address._parent).to eq(person)
283
- end
284
-
285
- it "sets the metadata on the child" do
286
- expect(address.__metadata).to_not be_nil
287
- end
288
-
289
- it "sets the index on the child" do
290
- expect(address._index).to eq(0)
291
- end
292
- end
293
-
294
- context "when the parent is not a new record" do
295
-
296
- let(:person) do
297
- Person.create
298
- end
299
-
300
- let(:address) do
301
- Address.new
302
- end
303
-
304
- context "when setting directly" do
305
-
306
- before do
307
- person.addresses = [ address ]
308
- end
309
-
310
- it "saves the target" do
311
- expect(address).to be_persisted
312
- end
313
- end
314
-
315
- context "when setting via an overridden method from the parent" do
316
-
317
- let!(:person) do
318
- Person.create
319
- end
320
-
321
- let!(:address) do
322
- person.addresses.create(street: "Alt Treptow")
323
- end
324
-
325
- let!(:new_address) do
326
- Address.new(street: "Tempelhofer Damm")
327
- end
328
-
329
- before do
330
- person.update_attributes(set_addresses: [ new_address ])
331
- end
332
-
333
- it "overwrites the existing addresses" do
334
- expect(person.reload.addresses).to eq([ new_address ])
335
- end
336
- end
337
-
338
- context "when setting via the parent attributes" do
339
-
340
- before do
341
- person.attributes = { addresses: [ address ] }
342
- end
343
-
344
- it "sets the relation" do
345
- expect(person.addresses).to eq([ address ])
346
- end
347
-
348
- it "does not save the target" do
349
- expect(address).to_not be_persisted
350
- end
351
-
352
- context "when setting the relation multiple times" do
353
-
354
- let(:address_two) do
355
- Address.new(street: "kudamm")
356
- end
357
-
358
- before do
359
- person.addresses = [ address_two ]
360
- person.save
361
- end
362
-
363
- it "sets the new documents" do
364
- expect(person.addresses).to eq([ address_two ])
365
- end
366
-
367
- it "persits only the new documents" do
368
- expect(person.reload.addresses).to eq([ address_two ])
369
- end
370
- end
371
- end
372
- end
373
-
374
- context "when setting for inherited docs" do
375
-
376
- context "when the parent collection is already accessed" do
377
-
378
- before do
379
- Person.collection
380
- end
381
-
382
- context "when setting via the subclass" do
383
-
384
- let(:doctor) do
385
- Doctor.new
386
- end
387
-
388
- let(:address_one) do
389
- Address.new(street: "tauentzien")
390
- end
391
-
392
- before do
393
- doctor.addresses = [ address_one ]
394
- doctor.save
395
- end
396
-
397
- it "sets the documents" do
398
- expect(doctor.addresses).to eq([ address_one ])
399
- end
400
-
401
- it "persists the document" do
402
- expect(doctor.reload.addresses).to eq([ address_one ])
403
- end
404
-
405
- context "when setting the relation multiple times" do
406
-
407
- let(:address_two) do
408
- Address.new(street: "kudamm")
409
- end
410
-
411
- before do
412
- doctor.addresses = [ address_two ]
413
- doctor.save
414
- end
415
-
416
- it "sets the new documents" do
417
- expect(doctor.addresses).to eq([ address_two ])
418
- end
419
-
420
- it "persits only the new documents" do
421
- expect(doctor.reload.addresses).to eq([ address_two ])
422
- end
423
- end
424
- end
425
- end
426
- end
427
-
428
- context "when replacing an existing relation" do
429
-
430
- let(:person) do
431
- Person.create(addresses: [
432
- Address.new(street: "1st St"),
433
- Address.new(street: "2nd St")
434
- ])
435
- end
436
-
437
- let(:address) do
438
- Address.new(street: "3rd St")
439
- end
440
-
441
- before do
442
- person.addresses = [ address ]
443
- end
444
-
445
- it "deletes the old documents" do
446
- expect(person.reload.addresses).to eq([ address ])
447
- end
448
- end
449
-
450
- context "when the relation has an unusual name" do
451
-
452
- module MyCompany
453
- module Model
454
- class TrackingId
455
- include Mongoid::Document
456
- include Mongoid::Timestamps
457
- store_in collection: "tracking_ids"
458
- embeds_many \
459
- :validation_history,
460
- class_name: "MyCompany::Model::TrackingIdValidationHistory"
461
- end
462
-
463
- class TrackingIdValidationHistory
464
- include Mongoid::Document
465
- field :old_state, type: String
466
- field :new_state, type: String
467
- field :when_changed, type: DateTime
468
- embedded_in :tracking_id, class_name: "MyCompany::Model::TrackingId"
469
- end
470
- end
471
- end
472
-
473
- let(:tracking_id) do
474
- MyCompany::Model::TrackingId.create
475
- end
476
-
477
- let(:history) do
478
- MyCompany::Model::TrackingIdValidationHistory.new(old_state: "Test")
479
- end
480
-
481
- before do
482
- tracking_id.validation_history << history
483
- end
484
-
485
- it "allows creation of the embedded document" do
486
- expect(tracking_id.validation_history.size).to eq(1)
487
- end
488
-
489
- it "saves the relation" do
490
- expect(history).to be_persisted
491
- end
492
-
493
- it "remains on reload" do
494
- expect(tracking_id.reload.validation_history.size).to eq(1)
495
- end
496
- end
497
-
498
- context "when the relation has address in the name" do
499
-
500
- let(:slave) do
501
- Slave.new(first_name: "Test")
502
- end
503
-
504
- before do
505
- ActiveSupport::Inflector.inflections do |inflect|
506
- inflect.singular("address_numbers", "address_number")
507
- end
508
- slave.address_numbers << AddressNumber.new(country_code: 1)
509
- slave.save
510
- end
511
-
512
- it "requires an inflection to determine the class" do
513
- expect(slave.reload.address_numbers.size).to eq(1)
514
- end
515
- end
516
-
517
- context "when setting the entire tree via a hash" do
518
-
519
- let(:person) do
520
- Person.create
521
- end
522
-
523
- let!(:address_one) do
524
- person.addresses.create(street: "Tauentzienstr")
525
- end
526
-
527
- let!(:address_two) do
528
- person.addresses.create(street: "Kudamm")
529
- end
530
-
531
- let(:attributes) do
532
- person.as_document.dup
533
- end
534
-
535
- context "when the attributes have changed" do
536
-
537
- before do
538
- attributes["addresses"][0]["city"] = "Berlin"
539
- person.update_attributes(attributes)
540
- end
541
-
542
- it "sets the new attributes" do
543
- expect(person.addresses.first.city).to eq("Berlin")
544
- end
545
-
546
- it "persists the changes" do
547
- expect(person.reload.addresses.first.city).to eq("Berlin")
548
- end
549
- end
550
- end
551
-
552
- context "when setting an embedded sub-document tree via a hash" do
553
-
554
- let(:person) do
555
- Person.create
556
- end
557
-
558
- let!(:address_one) do
559
- person.addresses.create(street: "Tauentzienstr")
560
- end
561
-
562
- let!(:location_one) do
563
- person.addresses.first.locations.create(name: "Work")
564
- end
565
-
566
- let(:attributes) do
567
- person.addresses.first.as_document.dup
568
- end
569
-
570
- context "when the attributes have changed" do
571
-
572
- before do
573
- attributes["city"] = "Berlin"
574
- attributes["locations"][0]["name"] = "Home"
575
- person.addresses.first.update_attributes(attributes)
576
- end
577
-
578
- it "sets the new attributes on the address" do
579
- expect(person.addresses.first.city).to eq("Berlin")
580
- end
581
-
582
- it "sets the new attributes on the location" do
583
- expect(person.addresses.first.locations.first.name).to eq("Home")
584
- end
585
-
586
- it "persists the changes to the address" do
587
- expect(person.reload.addresses.first.city).to eq("Berlin")
588
- end
589
-
590
- it "persists the changes to the location" do
591
- expect(person.reload.addresses.first.locations.first.name).to eq("Home")
592
- end
593
-
594
- it "does not persist the locations collection to the person document" do
595
- expect(person.reload[:locations]).to be_nil
596
- end
597
- end
598
- end
599
-
600
- context "when the parent and child have a cyclic relation" do
601
-
602
- context "when the parent is a new record" do
603
-
604
- let(:parent_role) do
605
- Role.new
606
- end
607
-
608
- let(:child_role) do
609
- Role.new
610
- end
611
-
612
- before do
613
- parent_role.child_roles = [ child_role ]
614
- end
615
-
616
- it "sets the target of the relation" do
617
- expect(parent_role.child_roles).to eq([ child_role ])
618
- end
619
-
620
- it "sets the base on the inverse relation" do
621
- expect(child_role.parent_role).to eq(parent_role)
622
- end
623
-
624
- it "sets the same instance on the inverse relation" do
625
- expect(child_role.parent_role).to eql(parent_role)
626
- end
627
-
628
- it "does not save the target" do
629
- expect(child_role).to_not be_persisted
630
- end
631
-
632
- it "sets the parent on the child" do
633
- expect(child_role._parent).to eq(parent_role)
634
- end
635
-
636
- it "sets the metadata on the child" do
637
- expect(child_role.__metadata).to_not be_nil
638
- end
639
-
640
- it "sets the index on the child" do
641
- expect(child_role._index).to eq(0)
642
- end
643
- end
644
-
645
- context "when the parent is not a new record" do
646
-
647
- let(:parent_role) do
648
- Role.create(name: "CTO")
649
- end
650
-
651
- let(:child_role) do
652
- Role.new
653
- end
654
-
655
- before do
656
- parent_role.child_roles = [ child_role ]
657
- end
658
-
659
- it "saves the target" do
660
- expect(child_role).to be_persisted
661
- end
662
- end
663
- end
664
- end
665
-
666
- describe "#= nil" do
667
-
668
- context "when the relationship is polymorphic" do
669
-
670
- context "when the parent is a new record" do
671
-
672
- let(:person) do
673
- Person.new
674
- end
675
-
676
- let(:address) do
677
- Address.new
678
- end
679
-
680
- before do
681
- person.addresses = [ address ]
682
- person.addresses = nil
683
- end
684
-
685
- it "sets the relation to empty" do
686
- expect(person.addresses).to be_empty
687
- end
688
-
689
- it "sets the unscoped to empty" do
690
- expect(person.addresses.send(:_unscoped)).to be_empty
691
- end
692
-
693
- it "removes the inverse relation" do
694
- expect(address.addressable).to be_nil
695
- end
696
- end
697
-
698
- context "when the inverse is already nil" do
699
-
700
- let(:person) do
701
- Person.new
702
- end
703
-
704
- before do
705
- person.addresses = nil
706
- end
707
-
708
- it "sets the relation to empty" do
709
- expect(person.addresses).to be_empty
710
- end
711
- end
712
-
713
- context "when the parent is persisted" do
714
-
715
- let(:person) do
716
- Person.create
717
- end
718
-
719
- let(:address) do
720
- Address.new
721
- end
722
-
723
- context "when setting directly" do
724
-
725
- before do
726
- person.addresses = [ address ]
727
- person.addresses = nil
728
- end
729
-
730
- it "sets the relation to empty" do
731
- expect(person.addresses).to be_empty
732
- end
733
-
734
- it "sets the relation to empty in the database" do
735
- expect(person.reload.addresses).to be_empty
736
- end
737
-
738
- it "removed the inverse relation" do
739
- expect(address.addressable).to be_nil
740
- end
741
-
742
- it "deletes the child document" do
743
- expect(address).to be_destroyed
744
- end
745
- end
746
-
747
- context "when setting via attributes" do
748
-
749
- before do
750
- person.addresses = [ address ]
751
- person.attributes = { addresses: nil }
752
- end
753
-
754
- it "sets the relation to empty" do
755
- expect(person.addresses).to be_empty
756
- end
757
-
758
- it "deletes the child document" do
759
- expect(address).to be_destroyed
760
- end
761
-
762
- context "when saving the parent" do
763
-
764
- before do
765
- person.save
766
- person.reload
767
- end
768
-
769
- it "persists the deletion" do
770
- expect(person.addresses).to be_empty
771
- end
772
- end
773
- end
774
- end
775
-
776
- context "when setting on a reload" do
777
-
778
- let(:person) do
779
- Person.create
780
- end
781
-
782
- let(:address) do
783
- Address.new
784
- end
785
-
786
- let(:reloaded) do
787
- person.reload
788
- end
789
-
790
- before do
791
- person.reload.addresses = [ address ]
792
- person.reload.addresses = nil
793
- end
794
-
795
- it "sets the relation to empty" do
796
- expect(person.addresses).to be_empty
797
- end
798
-
799
- it "sets the relation to empty in the database" do
800
- expect(reloaded.addresses).to be_empty
801
- end
802
- end
803
- end
804
-
805
- context "when the relationship is cyclic" do
806
-
807
- context "when the parent is a new record" do
808
-
809
- let(:parent_role) do
810
- Role.new
811
- end
812
-
813
- let(:child_role) do
814
- Role.new
815
- end
816
-
817
- before do
818
- parent_role.child_roles = [ child_role ]
819
- parent_role.child_roles = nil
820
- end
821
-
822
- it "sets the relation to empty" do
823
- expect(parent_role.child_roles).to be_empty
824
- end
825
-
826
- it "removes the inverse relation" do
827
- expect(child_role.parent_role).to be_nil
828
- end
829
- end
830
-
831
- context "when the inverse is already nil" do
832
-
833
- let(:parent_role) do
834
- Role.new
835
- end
836
-
837
- before do
838
- parent_role.child_roles = nil
839
- end
840
-
841
- it "sets the relation to empty" do
842
- expect(parent_role.child_roles).to be_empty
843
- end
844
- end
845
-
846
- context "when the documents are not new records" do
847
-
848
- let(:parent_role) do
849
- Role.create
850
- end
851
-
852
- let(:child_role) do
853
- Role.new
854
- end
855
-
856
- before do
857
- parent_role.child_roles = [ child_role ]
858
- parent_role.child_roles = nil
859
- end
860
-
861
- it "sets the relation to empty" do
862
- expect(parent_role.child_roles).to be_empty
863
- end
864
-
865
- it "removed the inverse relation" do
866
- expect(child_role.parent_role).to be_nil
867
- end
868
-
869
- it "deletes the child document" do
870
- expect(child_role).to be_destroyed
871
- end
872
- end
873
- end
874
- end
875
-
876
- describe "#as_document" do
877
-
878
- let!(:person) do
879
- Person.create
880
- end
881
-
882
- context "when the relation has no default scope" do
883
-
884
- let!(:address) do
885
- person.addresses.create(street: "one")
886
- end
887
-
888
- let(:document) do
889
- person.reload.addresses.as_document
890
- end
891
-
892
- it "returns the documents as an array of hashes" do
893
- expect(document).to eq([ address.as_document ])
894
- end
895
- end
896
-
897
- context "when the relation has a default scope" do
898
-
899
- context "when the default scope sorts" do
900
-
901
- let(:cough) do
902
- Symptom.new(name: "cough")
903
- end
904
-
905
- let(:headache) do
906
- Symptom.new(name: "headache")
907
- end
908
-
909
- before do
910
- person.symptoms.concat([ headache, cough ])
911
- end
912
-
913
- let(:document) do
914
- person.reload.symptoms.as_document
915
- end
916
-
917
- it "returns the unscoped documents as an array of hashes" do
918
- expect(document).to eq([ headache.as_document, cough.as_document ])
919
- end
920
- end
921
-
922
- context "when the default scope limits" do
923
-
924
- let(:active) do
925
- Appointment.new
926
- end
927
-
928
- let(:inactive) do
929
- Appointment.new(active: false)
930
- end
931
-
932
- before do
933
- person.appointments.concat([ active, inactive ])
934
- end
935
-
936
- let(:document) do
937
- person.reload.appointments.as_document
938
- end
939
-
940
- it "returns the unscoped documents as an array of hashes" do
941
- expect(document).to eq([ active.as_document, inactive.as_document ])
942
- end
943
- end
944
- end
945
- end
946
-
947
- [ :build, :new ].each do |method|
948
-
949
- describe "#build" do
950
-
951
- context "when the relation is not cyclic" do
952
-
953
- let(:person) do
954
- Person.new
955
- end
956
-
957
- let(:address) do
958
- person.addresses.send(method, street: "Bond") do |address|
959
- address.state = "CA"
960
- end
961
- end
962
-
963
- it "appends to the target" do
964
- expect(person.addresses).to eq([ address ])
965
- end
966
-
967
- it "appends to the unscoped" do
968
- expect(person.addresses.send(:_unscoped)).to eq([ address ])
969
- end
970
-
971
- it "sets the base on the inverse relation" do
972
- expect(address.addressable).to eq(person)
973
- end
974
-
975
- it "does not save the new document" do
976
- expect(address).to_not be_persisted
977
- end
978
-
979
- it "sets the parent on the child" do
980
- expect(address._parent).to eq(person)
981
- end
982
-
983
- it "sets the metadata on the child" do
984
- expect(address.__metadata).to_not be_nil
985
- end
986
-
987
- it "sets the index on the child" do
988
- expect(address._index).to eq(0)
989
- end
990
-
991
- it "writes to the attributes" do
992
- expect(address.street).to eq("Bond")
993
- end
994
-
995
- it "calls the passed block" do
996
- expect(address.state).to eq("CA")
997
- end
998
- end
999
-
1000
- context "when the relation is cyclic" do
1001
-
1002
- let(:parent_role) do
1003
- Role.new
1004
- end
1005
-
1006
- let(:child_role) do
1007
- parent_role.child_roles.send(method, name: "CTO")
1008
- end
1009
-
1010
- it "appends to the target" do
1011
- expect(parent_role.child_roles).to eq([ child_role ])
1012
- end
1013
-
1014
- it "sets the base on the inverse relation" do
1015
- expect(child_role.parent_role).to eq(parent_role)
1016
- end
1017
-
1018
- it "does not save the new document" do
1019
- expect(child_role).to_not be_persisted
1020
- end
1021
-
1022
- it "sets the parent on the child" do
1023
- expect(child_role._parent).to eq(parent_role)
1024
- end
1025
-
1026
- it "sets the metadata on the child" do
1027
- expect(child_role.__metadata).to_not be_nil
1028
- end
1029
-
1030
- it "sets the index on the child" do
1031
- expect(child_role._index).to eq(0)
1032
- end
1033
-
1034
- it "writes to the attributes" do
1035
- expect(child_role.name).to eq("CTO")
1036
- end
1037
- end
1038
-
1039
- context "when providing nested attributes" do
1040
-
1041
- let(:person) do
1042
- Person.create
1043
- end
1044
-
1045
- let(:address) do
1046
- person.addresses.send(
1047
- method,
1048
- street: "Bond",
1049
- locations_attributes: { "1" => { "name" => "Home" } }
1050
- )
1051
- end
1052
-
1053
- context "when followed by a save" do
1054
-
1055
- before do
1056
- address.save
1057
- end
1058
-
1059
- let(:location) do
1060
- person.reload.addresses.first.locations.first
1061
- end
1062
-
1063
- it "persists the deeply embedded document" do
1064
- expect(location.name).to eq("Home")
1065
- end
1066
- end
1067
- end
1068
- end
1069
- end
1070
-
1071
- describe ".builder" do
1072
-
1073
- let(:base) do
1074
- Person.new
1075
- end
1076
-
1077
- let(:target) do
1078
- [ address ]
1079
- end
1080
-
1081
- let(:metadata) do
1082
- Person.relations["addresses"]
1083
- end
1084
-
1085
- let(:relation) do
1086
- described_class.new(base, target, metadata)
1087
- end
1088
-
1089
- let(:document) do
1090
- Address.new
1091
- end
1092
-
1093
- it "returns the many builder" do
1094
- expect(
1095
- described_class.builder(base, metadata, document)
1096
- ).to be_a(Mongoid::Relations::Builders::Embedded::Many)
1097
- end
1098
- end
1099
-
1100
- describe "#clear" do
1101
-
1102
- context "when the parent has been persisted" do
1103
-
1104
- let(:person) do
1105
- Person.create
1106
- end
1107
-
1108
- context "when the children are persisted" do
1109
-
1110
- let!(:address) do
1111
- person.addresses.create(street: "High St")
1112
- end
1113
-
1114
- let!(:relation) do
1115
- person.addresses.clear
1116
- end
1117
-
1118
- it "clears out the relation" do
1119
- expect(person.addresses).to be_empty
1120
- end
1121
-
1122
- it "clears the unscoped" do
1123
- expect(person.addresses.send(:_unscoped)).to be_empty
1124
- end
1125
-
1126
- it "marks the documents as deleted" do
1127
- expect(address).to be_destroyed
1128
- end
1129
-
1130
- it "deletes the documents from the db" do
1131
- expect(person.reload.addresses).to be_empty
1132
- end
1133
-
1134
- it "returns the relation" do
1135
- expect(relation).to be_empty
1136
- end
1137
- end
1138
-
1139
- context "when the children are not persisted" do
1140
-
1141
- let!(:address) do
1142
- person.addresses.build(street: "High St")
1143
- end
1144
-
1145
- let!(:relation) do
1146
- person.addresses.clear
1147
- end
1148
-
1149
- it "clears out the relation" do
1150
- expect(person.addresses).to be_empty
1151
- end
1152
- end
1153
- end
1154
-
1155
- context "when the parent is not persisted" do
1156
-
1157
- let(:person) do
1158
- Person.new
1159
- end
1160
-
1161
- let!(:address) do
1162
- person.addresses.build(street: "High St")
1163
- end
1164
-
1165
- let!(:relation) do
1166
- person.addresses.clear
1167
- end
1168
-
1169
- it "clears out the relation" do
1170
- expect(person.addresses).to be_empty
1171
- end
1172
- end
1173
- end
1174
-
1175
- describe "#concat" do
1176
-
1177
- context "when the parent is a new record" do
1178
-
1179
- let(:person) do
1180
- Person.new
1181
- end
1182
-
1183
- let(:address) do
1184
- Address.new
1185
- end
1186
-
1187
- before do
1188
- person.addresses.concat([ address ])
1189
- end
1190
-
1191
- it "appends to the target" do
1192
- expect(person.addresses).to eq([ address ])
1193
- end
1194
-
1195
- it "appends to the unscoped" do
1196
- expect(person.addresses.send(:_unscoped)).to eq([ address ])
1197
- end
1198
-
1199
- it "sets the base on the inverse relation" do
1200
- expect(address.addressable).to eq(person)
1201
- end
1202
-
1203
- it "sets the same instance on the inverse relation" do
1204
- expect(address.addressable).to eql(person)
1205
- end
1206
-
1207
- it "does not save the new document" do
1208
- expect(address).to_not be_persisted
1209
- end
1210
-
1211
- it "sets the parent on the child" do
1212
- expect(address._parent).to eq(person)
1213
- end
1214
-
1215
- it "sets the metadata on the child" do
1216
- expect(address.__metadata).to_not be_nil
1217
- end
1218
-
1219
- it "sets the index on the child" do
1220
- expect(address._index).to eq(0)
1221
- end
1222
- end
1223
-
1224
- context "when the parent is not a new record" do
1225
-
1226
- let(:person) do
1227
- Person.create
1228
- end
1229
-
1230
- let(:address) do
1231
- Address.new
1232
- end
1233
-
1234
- before do
1235
- person.addresses.concat([ address ])
1236
- end
1237
-
1238
- it "saves the new document" do
1239
- expect(address).to be_persisted
1240
- end
1241
- end
1242
-
1243
- context "when concatenating an empty array" do
1244
-
1245
- let(:person) do
1246
- Person.create
1247
- end
1248
-
1249
- before do
1250
- expect(person.addresses).to_not receive(:batch_insert)
1251
- person.addresses.concat([])
1252
- end
1253
-
1254
- it "doesn't update the target" do
1255
- expect(person.addresses).to be_empty
1256
- end
1257
- end
1258
-
1259
- context "when appending more than one document at once" do
1260
-
1261
- let(:person) do
1262
- Person.create
1263
- end
1264
-
1265
- let(:address_one) do
1266
- Address.new
1267
- end
1268
-
1269
- let(:address_two) do
1270
- Address.new
1271
- end
1272
-
1273
- before do
1274
- person.addresses.concat([ address_one, address_two ])
1275
- end
1276
-
1277
- it "saves the first document" do
1278
- expect(address_one).to be_persisted
1279
- end
1280
-
1281
- it "saves the second document" do
1282
- expect(address_two).to be_persisted
1283
- end
1284
- end
1285
-
1286
- context "when the parent and child have a cyclic relation" do
1287
-
1288
- context "when the parent is a new record" do
1289
-
1290
- let(:parent_role) do
1291
- Role.new
1292
- end
1293
-
1294
- let(:child_role) do
1295
- Role.new
1296
- end
1297
-
1298
- before do
1299
- parent_role.child_roles.concat([ child_role ])
1300
- end
1301
-
1302
- it "appends to the target" do
1303
- expect(parent_role.child_roles).to eq([ child_role ])
1304
- end
1305
-
1306
- it "sets the base on the inverse relation" do
1307
- expect(child_role.parent_role).to eq(parent_role)
1308
- end
1309
-
1310
- it "sets the same instance on the inverse relation" do
1311
- expect(child_role.parent_role).to eql(parent_role)
1312
- end
1313
-
1314
- it "does not save the new document" do
1315
- expect(child_role).to_not be_persisted
1316
- end
1317
-
1318
- it "sets the parent on the child" do
1319
- expect(child_role._parent).to eq(parent_role)
1320
- end
1321
-
1322
- it "sets the metadata on the child" do
1323
- expect(child_role.__metadata).to_not be_nil
1324
- end
1325
-
1326
- it "sets the index on the child" do
1327
- expect(child_role._index).to eq(0)
1328
- end
1329
- end
1330
-
1331
- context "when the parent is not a new record" do
1332
-
1333
- let(:parent_role) do
1334
- Role.create(name: "CEO")
1335
- end
1336
-
1337
- let(:child_role) do
1338
- Role.new(name: "COO")
1339
- end
1340
-
1341
- before do
1342
- parent_role.child_roles.concat([ child_role ])
1343
- end
1344
-
1345
- it "saves the new document" do
1346
- expect(child_role).to be_persisted
1347
- end
1348
- end
1349
- end
1350
- end
1351
-
1352
- describe "#count" do
1353
-
1354
- let(:person) do
1355
- Person.create
1356
- end
1357
-
1358
- before do
1359
- person.addresses.create(street: "Upper")
1360
- person.addresses.build(street: "Bond")
1361
- end
1362
-
1363
- it "returns the number of persisted documents" do
1364
- expect(person.addresses.count).to eq(1)
1365
- end
1366
- end
1367
-
1368
- describe "#create" do
1369
-
1370
- context "when providing multiple attributes" do
1371
-
1372
- let(:person) do
1373
- Person.create
1374
- end
1375
-
1376
- let!(:addresses) do
1377
- person.addresses.create([{ street: "Bond" }, { street: "Upper" }])
1378
- end
1379
-
1380
- it "creates multiple documents" do
1381
- expect(addresses.size).to eq(2)
1382
- end
1383
-
1384
- it "sets the first attributes" do
1385
- expect(addresses.first.street).to eq("Bond")
1386
- end
1387
-
1388
- it "sets the second attributes" do
1389
- expect(addresses.last.street).to eq("Upper")
1390
- end
1391
-
1392
- it "persists the children" do
1393
- expect(person.addresses.count).to eq(2)
1394
- end
1395
- end
1396
-
1397
- context "when the relation is not cyclic" do
1398
-
1399
- let(:person) do
1400
- Person.create
1401
- end
1402
-
1403
- let!(:address) do
1404
- person.addresses.create(street: "Bond") do |address|
1405
- address.state = "CA"
1406
- end
1407
- end
1408
-
1409
- it "appends to the target" do
1410
- expect(person.reload.addresses).to eq([ address ])
1411
- end
1412
-
1413
- it "appends to the unscoped" do
1414
- expect(person.reload.addresses.send(:_unscoped)).to eq([ address ])
1415
- end
1416
-
1417
- it "sets the base on the inverse relation" do
1418
- expect(address.addressable).to eq(person)
1419
- end
1420
-
1421
- it "saves the document" do
1422
- expect(address).to be_persisted
1423
- end
1424
-
1425
- it "sets the parent on the child" do
1426
- expect(address._parent).to eq(person)
1427
- end
1428
-
1429
- it "sets the metadata on the child" do
1430
- expect(address.__metadata).to_not be_nil
1431
- end
1432
-
1433
- it "sets the index on the child" do
1434
- expect(address._index).to eq(0)
1435
- end
1436
-
1437
- it "writes to the attributes" do
1438
- expect(address.street).to eq("Bond")
1439
- end
1440
-
1441
- it "calls the passed block" do
1442
- expect(address.state).to eq("CA")
1443
- end
1444
-
1445
- context "when embedding a multi word named document" do
1446
-
1447
- let!(:component) do
1448
- person.address_components.create(street: "Test")
1449
- end
1450
-
1451
- it "saves the embedded document" do
1452
- expect(person.reload.address_components.first).to eq(component)
1453
- end
1454
- end
1455
- end
1456
-
1457
- context "when the relation is cyclic" do
1458
-
1459
- let!(:entry) do
1460
- Entry.create(title: "hi")
1461
- end
1462
-
1463
- let!(:child_entry) do
1464
- entry.child_entries.create(title: "hello")
1465
- end
1466
-
1467
- it "creates a new child" do
1468
- expect(child_entry).to be_persisted
1469
- end
1470
- end
1471
- end
1472
-
1473
- describe "#create!" do
1474
-
1475
- let(:person) do
1476
- Person.create
1477
- end
1478
-
1479
- context "when providing multiple attributes" do
1480
-
1481
- let!(:addresses) do
1482
- person.addresses.create!([{ street: "Bond" }, { street: "Upper" }])
1483
- end
1484
-
1485
- it "creates multiple documents" do
1486
- expect(addresses.size).to eq(2)
1487
- end
1488
-
1489
- it "sets the first attributes" do
1490
- expect(addresses.first.street).to eq("Bond")
1491
- end
1492
-
1493
- it "sets the second attributes" do
1494
- expect(addresses.last.street).to eq("Upper")
1495
- end
1496
-
1497
- it "persists the children" do
1498
- expect(person.addresses.count).to eq(2)
1499
- end
1500
- end
1501
-
1502
- context "when validation passes" do
1503
-
1504
- let(:address) do
1505
- person.addresses.create!(street: "Bond")
1506
- end
1507
-
1508
- it "appends to the target" do
1509
- expect(person.addresses).to eq([ address ])
1510
- end
1511
-
1512
- it "appends to the unscoped" do
1513
- expect(person.addresses.send(:_unscoped)).to eq([ address ])
1514
- end
1515
-
1516
- it "sets the base on the inverse relation" do
1517
- expect(address.addressable).to eq(person)
1518
- end
1519
-
1520
- it "saves the document" do
1521
- expect(address).to be_persisted
1522
- end
1523
-
1524
- it "sets the parent on the child" do
1525
- expect(address._parent).to eq(person)
1526
- end
1527
-
1528
- it "sets the metadata on the child" do
1529
- expect(address.__metadata).to_not be_nil
1530
- end
1531
-
1532
- it "sets the index on the child" do
1533
- expect(address._index).to eq(0)
1534
- end
1535
-
1536
- it "writes to the attributes" do
1537
- expect(address.street).to eq("Bond")
1538
- end
1539
- end
1540
-
1541
- context "when validation fails" do
1542
-
1543
- it "raises an error" do
1544
- expect {
1545
- person.addresses.create!(street: "1")
1546
- }.to raise_error(Mongoid::Errors::Validations)
1547
- end
1548
-
1549
- context 'when the presence of the embedded relation is validated' do
1550
-
1551
- before do
1552
- class Book
1553
- validates :pages, presence: true
1554
- end
1555
- end
1556
-
1557
- let(:book) do
1558
- Book.new.tap do |b|
1559
- b.pages = [Page.new]
1560
- b.save!
1561
- end
1562
- end
1563
-
1564
- let(:num_pages) do
1565
- book.pages.size
1566
- end
1567
-
1568
- let(:reloaded) do
1569
- book.reload
1570
- end
1571
-
1572
- before do
1573
- begin; book.update_attributes!({"pages"=>nil}); rescue; end
1574
- end
1575
-
1576
- it 'does not delete the embedded relation' do
1577
- expect(reloaded.pages.size).to eq(num_pages)
1578
- end
1579
- end
1580
- end
1581
- end
1582
-
1583
- describe "#delete" do
1584
-
1585
- let(:person) do
1586
- Person.new
1587
- end
1588
-
1589
- let(:address_one) do
1590
- Address.new(street: "first")
1591
- end
1592
-
1593
- let(:address_two) do
1594
- Address.new(street: "second")
1595
- end
1596
-
1597
- before do
1598
- person.addresses << [ address_one, address_two ]
1599
- end
1600
-
1601
- context "when the document exists in the relation" do
1602
-
1603
- let!(:deleted) do
1604
- person.addresses.delete(address_one)
1605
- end
1606
-
1607
- it "deletes the document" do
1608
- expect(person.addresses).to eq([ address_two ])
1609
- end
1610
-
1611
- it "deletes the document from the unscoped" do
1612
- expect(person.addresses.send(:_unscoped)).to eq([ address_two ])
1613
- end
1614
-
1615
- it "reindexes the relation" do
1616
- expect(address_two._index).to eq(0)
1617
- end
1618
-
1619
- it "returns the document" do
1620
- expect(deleted).to eq(address_one)
1621
- end
1622
- end
1623
-
1624
- context "when the document does not exist" do
1625
-
1626
- it "returns nil" do
1627
- expect(person.addresses.delete(Address.new)).to be_nil
1628
- end
1629
- end
1630
- end
1631
-
1632
- describe "#delete_if" do
1633
-
1634
- let(:person) do
1635
- Person.create
1636
- end
1637
-
1638
- context "when the documents are new" do
1639
-
1640
- let!(:address_one) do
1641
- person.addresses.build(street: "Bond")
1642
- end
1643
-
1644
- let!(:address_two) do
1645
- person.addresses.build(street: "Upper")
1646
- end
1647
-
1648
- context "when a block is provided" do
1649
-
1650
- let!(:deleted) do
1651
- person.addresses.delete_if do |doc|
1652
- doc.street == "Bond"
1653
- end
1654
- end
1655
-
1656
- it "removes the matching documents" do
1657
- expect(person.addresses.size).to eq(1)
1658
- end
1659
-
1660
- it "removes from the unscoped" do
1661
- expect(person.addresses.send(:_unscoped).size).to eq(1)
1662
- end
1663
-
1664
- it "returns the relation" do
1665
- expect(deleted).to eq(person.addresses)
1666
- end
1667
- end
1668
-
1669
- context "when no block is provided" do
1670
-
1671
- let!(:deleted) do
1672
- person.addresses.delete_if
1673
- end
1674
-
1675
- it "returns an enumerator" do
1676
- expect(deleted).to be_a(Enumerator)
1677
- end
1678
- end
1679
- end
1680
-
1681
- context "when the documents persisted" do
1682
-
1683
- let!(:address_one) do
1684
- person.addresses.create(street: "Bond")
1685
- end
1686
-
1687
- let!(:address_two) do
1688
- person.addresses.create(street: "Upper")
1689
- end
1690
-
1691
- context "when a block is provided" do
1692
-
1693
- let!(:deleted) do
1694
- person.addresses.delete_if do |doc|
1695
- doc.street == "Bond"
1696
- end
1697
- end
1698
-
1699
- it "deletes the matching documents" do
1700
- expect(person.addresses.count).to eq(1)
1701
- end
1702
-
1703
- it "deletes the matching documents from the db" do
1704
- expect(person.reload.addresses.count).to eq(1)
1705
- end
1706
-
1707
- it "returns the relation" do
1708
- expect(deleted).to eq(person.addresses)
1709
- end
1710
- end
1711
- end
1712
-
1713
- context "when the documents are empty" do
1714
-
1715
- context "when a block is provided" do
1716
-
1717
- let!(:deleted) do
1718
- person.addresses.delete_if do |doc|
1719
- doc.street == "Bond"
1720
- end
1721
- end
1722
-
1723
- it "deletes the matching documents" do
1724
- expect(person.addresses.count).to eq(0)
1725
- end
1726
-
1727
- it "deletes all the documents from the db" do
1728
- expect(person.reload.addresses.count).to eq(0)
1729
- end
1730
-
1731
- it "returns the relation" do
1732
- expect(deleted).to eq(person.addresses)
1733
- end
1734
- end
1735
- end
1736
- end
1737
-
1738
- [ :delete_all, :destroy_all ].each do |method|
1739
-
1740
- describe "##{method}" do
1741
-
1742
- let(:person) do
1743
- Person.create
1744
- end
1745
-
1746
- context "when the documents are new" do
1747
-
1748
- let!(:address_one) do
1749
- person.addresses.build(street: "Bond")
1750
- end
1751
-
1752
- let!(:address_two) do
1753
- person.addresses.build(street: "Upper")
1754
- end
1755
-
1756
- context "when conditions are provided" do
1757
-
1758
- let!(:deleted) do
1759
- person.addresses.send(
1760
- method,
1761
- { street: "Bond" }
1762
- )
1763
- end
1764
-
1765
- it "removes the matching documents" do
1766
- expect(person.addresses.size).to eq(1)
1767
- end
1768
-
1769
- it "removes from the unscoped" do
1770
- expect(person.addresses.send(:_unscoped).size).to eq(1)
1771
- end
1772
-
1773
- it "returns the number deleted" do
1774
- expect(deleted).to eq(1)
1775
- end
1776
- end
1777
-
1778
- context "when conditions are not provided" do
1779
-
1780
- let!(:deleted) do
1781
- person.addresses.send(method)
1782
- end
1783
-
1784
- it "removes all documents" do
1785
- expect(person.addresses.size).to eq(0)
1786
- end
1787
-
1788
- it "returns the number deleted" do
1789
- expect(deleted).to eq(2)
1790
- end
1791
- end
1792
- end
1793
-
1794
- context "when the documents persisted" do
1795
-
1796
- let!(:address_one) do
1797
- person.addresses.create(street: "Bond")
1798
- end
1799
-
1800
- let!(:address_two) do
1801
- person.addresses.create(street: "Upper")
1802
- end
1803
-
1804
- context "when conditions are provided" do
1805
-
1806
- let!(:deleted) do
1807
- person.addresses.send(
1808
- method,
1809
- { street: "Bond" }
1810
- )
1811
- end
1812
-
1813
- it "deletes the matching documents" do
1814
- expect(person.addresses.count).to eq(1)
1815
- end
1816
-
1817
- it "deletes the matching documents from the db" do
1818
- expect(person.reload.addresses.count).to eq(1)
1819
- end
1820
-
1821
- it "returns the number deleted" do
1822
- expect(deleted).to eq(1)
1823
- end
1824
- end
1825
-
1826
- context "when conditions are not provided" do
1827
-
1828
- let!(:deleted) do
1829
- person.addresses.send(method)
1830
- end
1831
-
1832
- it "deletes all the documents" do
1833
- expect(person.addresses.count).to eq(0)
1834
- end
1835
-
1836
- it "deletes all the documents from the db" do
1837
- expect(person.reload.addresses.count).to eq(0)
1838
- end
1839
-
1840
- it "returns the number deleted" do
1841
- expect(deleted).to eq(2)
1842
- end
1843
- end
1844
-
1845
- context "when removing and resaving" do
1846
-
1847
- let(:owner) do
1848
- PetOwner.create(title: "AKC")
1849
- end
1850
-
1851
- before do
1852
- owner.pet = Pet.new(name: "Fido")
1853
- owner.pet.vet_visits << VetVisit.new(date: Date.today)
1854
- owner.save!
1855
- owner.pet.vet_visits.destroy_all
1856
- end
1857
-
1858
- it "removes the documents" do
1859
- expect(owner.pet.vet_visits).to be_empty
1860
- end
1861
-
1862
- it "allows addition and a resave" do
1863
- owner.pet.vet_visits << VetVisit.new(date: Date.today)
1864
- owner.save!
1865
- expect(owner.pet.vet_visits.first).to be_persisted
1866
- end
1867
- end
1868
- end
1869
-
1870
- context "when the documents empty" do
1871
-
1872
- context "when scoped" do
1873
- let!(:deleted) do
1874
- person.addresses.without_postcode.send(method)
1875
- end
1876
-
1877
- it "deletes all the documents" do
1878
- expect(person.addresses.count).to eq(0)
1879
- end
1880
-
1881
- it "deletes all the documents from the db" do
1882
- expect(person.reload.addresses.count).to eq(0)
1883
- end
1884
-
1885
- it "returns the number deleted" do
1886
- expect(deleted).to eq(0)
1887
- end
1888
- end
1889
-
1890
- context "when conditions are provided" do
1891
-
1892
- let!(:deleted) do
1893
- person.addresses.send(
1894
- method,
1895
- conditions: { street: "Bond" }
1896
- )
1897
- end
1898
-
1899
- it "deletes all the documents" do
1900
- expect(person.addresses.count).to eq(0)
1901
- end
1902
-
1903
- it "deletes all the documents from the db" do
1904
- expect(person.reload.addresses.count).to eq(0)
1905
- end
1906
-
1907
- it "returns the number deleted" do
1908
- expect(deleted).to eq(0)
1909
- end
1910
- end
1911
-
1912
- context "when conditions are not provided" do
1913
-
1914
- let!(:deleted) do
1915
- person.addresses.send(method)
1916
- end
1917
-
1918
- it "deletes all the documents" do
1919
- expect(person.addresses.count).to eq(0)
1920
- end
1921
-
1922
- it "deletes all the documents from the db" do
1923
- expect(person.reload.addresses.count).to eq(0)
1924
- end
1925
-
1926
- it "returns the number deleted" do
1927
- expect(deleted).to eq(0)
1928
- end
1929
- end
1930
- end
1931
- end
1932
- end
1933
-
1934
- describe ".embedded?" do
1935
-
1936
- it "returns true" do
1937
- expect(described_class).to be_embedded
1938
- end
1939
- end
1940
-
1941
- describe ".foreign_key_suffix" do
1942
-
1943
- it "returns nil" do
1944
- expect(described_class.foreign_key_suffix).to be_nil
1945
- end
1946
- end
1947
-
1948
- describe "#exists?" do
1949
-
1950
- let!(:person) do
1951
- Person.create
1952
- end
1953
-
1954
- context "when documents exist in the database" do
1955
-
1956
- before do
1957
- person.addresses.create(street: "Bond St")
1958
- end
1959
-
1960
- it "returns true" do
1961
- expect(person.addresses.exists?).to be true
1962
- end
1963
- end
1964
-
1965
- context "when no documents exist in the database" do
1966
-
1967
- before do
1968
- person.addresses.build(street: "Hyde Park Dr")
1969
- end
1970
-
1971
- it "returns false" do
1972
- expect(person.addresses.exists?).to be false
1973
- end
1974
- end
1975
- end
1976
-
1977
- describe "#find" do
1978
-
1979
- let(:person) do
1980
- Person.new
1981
- end
1982
-
1983
- let!(:address_one) do
1984
- person.addresses.build(street: "Bond", city: "London")
1985
- end
1986
-
1987
- let!(:address_two) do
1988
- person.addresses.build(street: "Upper", city: "London")
1989
- end
1990
-
1991
- context "when providing an id" do
1992
-
1993
- context "when the id matches" do
1994
-
1995
- let(:address) do
1996
- person.addresses.find(address_one.id)
1997
- end
1998
-
1999
- it "returns the matching document" do
2000
- expect(address).to eq(address_one)
2001
- end
2002
- end
2003
-
2004
- context "when the id does not match" do
2005
-
2006
- context "when config set to raise error" do
2007
-
2008
- before do
2009
- Mongoid.raise_not_found_error = true
2010
- end
2011
-
2012
- it "raises an error" do
2013
- expect {
2014
- person.addresses.find(BSON::ObjectId.new)
2015
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
2016
- end
2017
- end
2018
-
2019
- context "when config set not to raise error" do
2020
-
2021
- let(:address) do
2022
- person.addresses.find(BSON::ObjectId.new)
2023
- end
2024
-
2025
- before do
2026
- Mongoid.raise_not_found_error = false
2027
- end
2028
-
2029
- after do
2030
- Mongoid.raise_not_found_error = true
2031
- end
2032
-
2033
- it "returns nil" do
2034
- expect(address).to be_nil
2035
- end
2036
- end
2037
- end
2038
- end
2039
-
2040
- context "when providing an array of ids" do
2041
-
2042
- context "when the ids match" do
2043
-
2044
- let(:addresses) do
2045
- person.addresses.find([ address_one.id, address_two.id ])
2046
- end
2047
-
2048
- it "returns the matching documents" do
2049
- expect(addresses).to eq([ address_one, address_two ])
2050
- end
2051
- end
2052
-
2053
- context "when the ids do not match" do
2054
-
2055
- context "when config set to raise error" do
2056
-
2057
- before do
2058
- Mongoid.raise_not_found_error = true
2059
- end
2060
-
2061
- it "raises an error" do
2062
- expect {
2063
- person.addresses.find([ BSON::ObjectId.new ])
2064
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
2065
- end
2066
- end
2067
-
2068
- context "when config set not to raise error" do
2069
-
2070
- let(:addresses) do
2071
- person.addresses.find([ BSON::ObjectId.new ])
2072
- end
2073
-
2074
- before do
2075
- Mongoid.raise_not_found_error = false
2076
- end
2077
-
2078
- after do
2079
- Mongoid.raise_not_found_error = true
2080
- end
2081
-
2082
- it "returns an empty array" do
2083
- expect(addresses).to be_empty
2084
- end
2085
- end
2086
- end
2087
- end
2088
- end
2089
-
2090
- describe "#find_or_create_by" do
2091
-
2092
- let(:person) do
2093
- Person.create
2094
- end
2095
-
2096
- let!(:address) do
2097
- person.addresses.build(street: "Bourke", city: "Melbourne")
2098
- end
2099
-
2100
- context "when the document exists" do
2101
-
2102
- let(:found) do
2103
- person.addresses.find_or_create_by(street: "Bourke")
2104
- end
2105
-
2106
- it "returns the document" do
2107
- expect(found).to eq(address)
2108
- end
2109
- end
2110
-
2111
- context "when the document does not exist" do
2112
-
2113
- let(:found) do
2114
- person.addresses.find_or_create_by(street: "King") do |address|
2115
- address.state = "CA"
2116
- end
2117
- end
2118
-
2119
- it "sets the new document attributes" do
2120
- expect(found.street).to eq("King")
2121
- end
2122
-
2123
- it "returns a newly persisted document" do
2124
- expect(found).to be_persisted
2125
- end
2126
-
2127
- it "calls the passed block" do
2128
- expect(found.state).to eq("CA")
2129
- end
2130
- end
2131
-
2132
- context "when the child belongs to another document" do
2133
-
2134
- let(:product) do
2135
- Product.create
2136
- end
2137
-
2138
- let(:purchase) do
2139
- Purchase.create
2140
- end
2141
-
2142
- let(:line_item) do
2143
- purchase.line_items.find_or_create_by(
2144
- product_id: product.id,
2145
- product_type: product.class.name
2146
- )
2147
- end
2148
-
2149
- it "properly creates the document" do
2150
- expect(line_item.product).to eq(product)
2151
- end
2152
- end
2153
- end
2154
-
2155
- describe "#find_or_create_by!" do
2156
-
2157
- let(:person) do
2158
- Person.create
2159
- end
2160
-
2161
- let!(:address) do
2162
- person.addresses.build(street: "Bourke", city: "Melbourne")
2163
- end
2164
-
2165
- context "when the document exists" do
2166
-
2167
- let(:found) do
2168
- person.addresses.find_or_create_by!(street: "Bourke")
2169
- end
2170
-
2171
- it "returns the document" do
2172
- expect(found).to eq(address)
2173
- end
2174
- end
2175
-
2176
- context "when the document does not exist" do
2177
-
2178
- let(:found) do
2179
- person.addresses.find_or_create_by!(street: "King") do |address|
2180
- address.state = "CA"
2181
- end
2182
- end
2183
-
2184
- it "sets the new document attributes" do
2185
- expect(found.street).to eq("King")
2186
- end
2187
-
2188
- it "returns a newly persisted document" do
2189
- expect(found).to be_persisted
2190
- end
2191
-
2192
- it "calls the passed block" do
2193
- expect(found.state).to eq("CA")
2194
- end
2195
-
2196
- context "when validation fails" do
2197
-
2198
- it "raises an error" do
2199
- expect {
2200
- person.addresses.find_or_create_by!(street: "1")
2201
- }.to raise_error(Mongoid::Errors::Validations)
2202
- end
2203
- end
2204
- end
2205
-
2206
- context "when the child belongs to another document" do
2207
-
2208
- let(:product) do
2209
- Product.create
2210
- end
2211
-
2212
- let(:purchase) do
2213
- Purchase.create
2214
- end
2215
-
2216
- let(:line_item) do
2217
- purchase.line_items.find_or_create_by(
2218
- product_id: product.id,
2219
- product_type: product.class.name
2220
- )
2221
- end
2222
-
2223
- it "properly creates the document" do
2224
- expect(line_item.product).to eq(product)
2225
- end
2226
- end
2227
- end
2228
-
2229
- describe "#find_or_initialize_by" do
2230
-
2231
- let(:person) do
2232
- Person.new
2233
- end
2234
-
2235
- let!(:address) do
2236
- person.addresses.build(street: "Bourke", city: "Melbourne")
2237
- end
2238
-
2239
- context "when the document exists" do
2240
-
2241
- let(:found) do
2242
- person.addresses.find_or_initialize_by(street: "Bourke")
2243
- end
2244
-
2245
- it "returns the document" do
2246
- expect(found).to eq(address)
2247
- end
2248
- end
2249
-
2250
- context "when the document does not exist" do
2251
-
2252
- let(:found) do
2253
- person.addresses.find_or_initialize_by(street: "King") do |address|
2254
- address.state = "CA"
2255
- end
2256
- end
2257
-
2258
- it "sets the new document attributes" do
2259
- expect(found.street).to eq("King")
2260
- end
2261
-
2262
- it "returns a non persisted document" do
2263
- expect(found).to_not be_persisted
2264
- end
2265
-
2266
- it "calls the passed block" do
2267
- expect(found.state).to eq("CA")
2268
- end
2269
- end
2270
- end
2271
-
2272
- describe ".macro" do
2273
-
2274
- it "returns embeds_many" do
2275
- expect(described_class.macro).to eq(:embeds_many)
2276
- end
2277
- end
2278
-
2279
- describe "#max" do
2280
-
2281
- let(:person) do
2282
- Person.new
2283
- end
2284
-
2285
- let(:address_one) do
2286
- Address.new(number: 5)
2287
- end
2288
-
2289
- let(:address_two) do
2290
- Address.new(number: 10)
2291
- end
2292
-
2293
- before do
2294
- person.addresses.push(address_one, address_two)
2295
- end
2296
-
2297
- let(:max) do
2298
- person.addresses.max do |a,b|
2299
- a.number <=> b.number
2300
- end
2301
- end
2302
-
2303
- it "returns the document with the max value of the supplied field" do
2304
- expect(max).to eq(address_two)
2305
- end
2306
- end
2307
-
2308
- describe "#max_by" do
2309
-
2310
- let(:person) do
2311
- Person.new
2312
- end
2313
-
2314
- let(:address_one) do
2315
- Address.new(number: 5)
2316
- end
2317
-
2318
- let(:address_two) do
2319
- Address.new(number: 10)
2320
- end
2321
-
2322
- before do
2323
- person.addresses.push(address_one, address_two)
2324
- end
2325
-
2326
- let(:max) do
2327
- person.addresses.max_by(&:number)
2328
- end
2329
-
2330
- it "returns the document with the max value of the supplied field" do
2331
- expect(max).to eq(address_two)
2332
- end
2333
- end
2334
-
2335
- describe "#method_missing" do
2336
-
2337
- let!(:person) do
2338
- Person.create
2339
- end
2340
-
2341
- let!(:address_one) do
2342
- person.addresses.create(
2343
- street: "Market",
2344
- state: "CA",
2345
- services: [ "1", "2" ]
2346
- )
2347
- end
2348
-
2349
- let!(:address_two) do
2350
- person.addresses.create(
2351
- street: "Madison",
2352
- state: "NY",
2353
- services: [ "1", "2" ]
2354
- )
2355
- end
2356
-
2357
- context "when providing a single criteria" do
2358
-
2359
- context "when using a simple criteria" do
2360
-
2361
- let(:addresses) do
2362
- person.addresses.where(state: "CA")
2363
- end
2364
-
2365
- it "applies the criteria to the documents" do
2366
- expect(addresses).to eq([ address_one ])
2367
- end
2368
- end
2369
-
2370
- context "when using an $or criteria" do
2371
-
2372
- let(:addresses) do
2373
- person.addresses.any_of({ state: "CA" }, { state: "NY" })
2374
- end
2375
-
2376
- it "applies the criteria to the documents" do
2377
- expect(addresses).to eq([ address_one, address_two ])
2378
- end
2379
- end
2380
-
2381
- context "when using array comparison" do
2382
-
2383
- let(:addresses) do
2384
- person.addresses.where(services: [ "1", "2" ])
2385
- end
2386
-
2387
- it "applies the criteria to the documents" do
2388
- expect(addresses).to eq([ address_one, address_two ])
2389
- end
2390
- end
2391
- end
2392
-
2393
- context "when providing a criteria class method" do
2394
-
2395
- let(:addresses) do
2396
- person.addresses.california
2397
- end
2398
-
2399
- it "applies the criteria to the documents" do
2400
- expect(addresses).to eq([ address_one ])
2401
- end
2402
- end
2403
-
2404
- context "when chaining criteria" do
2405
-
2406
- let(:addresses) do
2407
- person.addresses.california.where(:street.in => [ "Market" ])
2408
- end
2409
-
2410
- it "applies the criteria to the documents" do
2411
- expect(addresses).to eq([ address_one ])
2412
- end
2413
- end
2414
-
2415
- context "when delegating methods" do
2416
-
2417
- describe "#distinct" do
2418
-
2419
- it "returns the distinct values for the fields" do
2420
- expect(person.addresses.distinct(:street)).to eq([ "Market", "Madison"])
2421
- end
2422
- end
2423
- end
2424
- end
2425
-
2426
- describe "#min" do
2427
-
2428
- let(:person) do
2429
- Person.new
2430
- end
2431
-
2432
- let(:address_one) do
2433
- Address.new(number: 5)
2434
- end
2435
-
2436
- let(:address_two) do
2437
- Address.new(number: 10)
2438
- end
2439
-
2440
- before do
2441
- person.addresses.push(address_one, address_two)
2442
- end
2443
-
2444
- let(:min) do
2445
- person.addresses.min do |a,b|
2446
- a.number <=> b.number
2447
- end
2448
- end
2449
-
2450
- it "returns the min value of the supplied field" do
2451
- expect(min).to eq(address_one)
2452
- end
2453
- end
2454
-
2455
- describe "#min_by" do
2456
-
2457
- let(:person) do
2458
- Person.new
2459
- end
2460
-
2461
- let(:address_one) do
2462
- Address.new(number: 5)
2463
- end
2464
-
2465
- let(:address_two) do
2466
- Address.new(number: 10)
2467
- end
2468
-
2469
- before do
2470
- person.addresses.push(address_one, address_two)
2471
- end
2472
-
2473
- let(:min) do
2474
- person.addresses.min_by(&:number)
2475
- end
2476
-
2477
- it "returns the min value of the supplied field" do
2478
- expect(min).to eq(address_one)
2479
- end
2480
- end
2481
-
2482
- describe ".nested_builder" do
2483
-
2484
- let(:metadata) do
2485
- Person.relations["addresses"]
2486
- end
2487
-
2488
- it "returns the many nested builder class" do
2489
- expect(
2490
- described_class.nested_builder(metadata, {}, {})
2491
- ).to be_a(Mongoid::Relations::Builders::NestedAttributes::Many)
2492
- end
2493
- end
2494
-
2495
- describe "#pop" do
2496
-
2497
- let(:person) do
2498
- Person.create
2499
- end
2500
-
2501
- context "when no argument is provided" do
2502
-
2503
- let!(:address_one) do
2504
- person.addresses.create(street: "sonnenallee")
2505
- end
2506
-
2507
- let!(:address_two) do
2508
- person.addresses.create(street: "hermannstr")
2509
- end
2510
-
2511
- let!(:popped) do
2512
- person.addresses.pop
2513
- end
2514
-
2515
- it "returns the popped document" do
2516
- expect(popped).to eq(address_two)
2517
- end
2518
-
2519
- it "removes the document from the relation" do
2520
- expect(person.addresses).to eq([ address_one ])
2521
- end
2522
-
2523
- it "persists the pop" do
2524
- expect(person.reload.addresses).to eq([ address_one ])
2525
- end
2526
- end
2527
-
2528
- context "when an integer is provided" do
2529
-
2530
- let!(:address_one) do
2531
- person.addresses.create(street: "sonnenallee")
2532
- end
2533
-
2534
- let!(:address_two) do
2535
- person.addresses.create(street: "hermannstr")
2536
- end
2537
-
2538
- context "when the number is not larger than the relation" do
2539
-
2540
- let!(:popped) do
2541
- person.addresses.pop(2)
2542
- end
2543
-
2544
- it "returns the popped documents" do
2545
- expect(popped).to eq([ address_one, address_two ])
2546
- end
2547
-
2548
- it "removes the document from the relation" do
2549
- expect(person.addresses).to be_empty
2550
- end
2551
-
2552
- it "persists the pop" do
2553
- expect(person.reload.addresses).to be_empty
2554
- end
2555
- end
2556
-
2557
- context "when the number is larger than the relation" do
2558
-
2559
- let!(:popped) do
2560
- person.addresses.pop(4)
2561
- end
2562
-
2563
- it "returns the popped documents" do
2564
- expect(popped).to eq([ address_one, address_two ])
2565
- end
2566
-
2567
- it "removes the document from the relation" do
2568
- expect(person.addresses).to be_empty
2569
- end
2570
-
2571
- it "persists the pop" do
2572
- expect(person.reload.addresses).to be_empty
2573
- end
2574
- end
2575
- end
2576
-
2577
- context "when the relation is empty" do
2578
-
2579
- context "when providing no number" do
2580
-
2581
- it "returns nil" do
2582
- expect(person.addresses.pop).to be_nil
2583
- end
2584
- end
2585
-
2586
- context "when providing a number" do
2587
-
2588
- it "returns nil" do
2589
- expect(person.addresses.pop(2)).to be_nil
2590
- end
2591
- end
2592
- end
2593
- end
2594
-
2595
- describe "#scoped" do
2596
-
2597
- let(:person) do
2598
- Person.new
2599
- end
2600
-
2601
- let(:scoped) do
2602
- person.addresses.scoped
2603
- end
2604
-
2605
- it "returns the relation criteria" do
2606
- expect(scoped).to be_a(Mongoid::Criteria)
2607
- end
2608
-
2609
- it "returns with an empty selector" do
2610
- expect(scoped.selector).to be_empty
2611
- end
2612
- end
2613
-
2614
- describe "#respond_to?" do
2615
-
2616
- let(:person) do
2617
- Person.new
2618
- end
2619
-
2620
- let(:addresses) do
2621
- person.addresses
2622
- end
2623
-
2624
- Array.public_instance_methods.each do |method|
2625
-
2626
- context "when checking #{method}" do
2627
-
2628
- it "returns true" do
2629
- expect(addresses.respond_to?(method)).to be true
2630
- end
2631
- end
2632
- end
2633
-
2634
- Mongoid::Relations::Embedded::Many.public_instance_methods.each do |method|
2635
-
2636
- context "when checking #{method}" do
2637
-
2638
- it "returns true" do
2639
- expect(addresses.respond_to?(method)).to be true
2640
- end
2641
- end
2642
- end
2643
-
2644
- Address.scopes.keys.each do |method|
2645
-
2646
- context "when checking #{method}" do
2647
-
2648
- it "returns true" do
2649
- expect(addresses.respond_to?(method)).to be true
2650
- end
2651
- end
2652
- end
2653
-
2654
- it "supports 'include_private = boolean'" do
2655
- expect { addresses.respond_to?(:Rational, true) }.not_to raise_error
2656
- end
2657
- end
2658
-
2659
- [ :size, :length ].each do |method|
2660
-
2661
- describe "##{method}" do
2662
-
2663
- let(:person) do
2664
- Person.create
2665
- end
2666
-
2667
- before do
2668
- person.addresses.create(street: "Upper")
2669
- person.addresses.build(street: "Bond")
2670
- end
2671
-
2672
- it "returns the number of persisted documents" do
2673
- expect(person.addresses.send(method)).to eq(2)
2674
- end
2675
- end
2676
- end
2677
-
2678
- describe "#unscoped" do
2679
-
2680
- let(:person) do
2681
- Person.new
2682
- end
2683
-
2684
- let(:unscoped) do
2685
- person.videos.unscoped
2686
- end
2687
-
2688
- it "returns the relation criteria" do
2689
- expect(unscoped).to be_a(Mongoid::Criteria)
2690
- end
2691
-
2692
- it "returns with empty options" do
2693
- expect(unscoped.options).to be_empty
2694
- end
2695
-
2696
- it "returns with an empty selector" do
2697
- expect(unscoped.selector).to be_empty
2698
- end
2699
- end
2700
-
2701
- describe "#update_all" do
2702
-
2703
- context "when there are no documents present" do
2704
-
2705
- let(:person) do
2706
- Person.create
2707
- end
2708
-
2709
- it "updates nothing" do
2710
- expect(person.addresses.update_all(street: "test")).to be false
2711
- end
2712
- end
2713
-
2714
- context "when documents are present" do
2715
-
2716
- let(:person) do
2717
- Person.create
2718
- end
2719
-
2720
- let!(:address) do
2721
- person.addresses.create(street: "Hobrecht", number: 27)
2722
- end
2723
-
2724
- context "when updating with a where clause" do
2725
-
2726
- before do
2727
- person.addresses.
2728
- where(street: "Hobrecht").
2729
- update_all(number: 26, post_code: "12437")
2730
- end
2731
-
2732
- it "resets the matching dirty flags" do
2733
- expect(address).to_not be_changed
2734
- end
2735
-
2736
- it "updates the first field" do
2737
- expect(address.reload.number).to eq(26)
2738
- end
2739
-
2740
- it "updates the second field" do
2741
- expect(address.reload.post_code).to eq("12437")
2742
- end
2743
-
2744
- it "does not wipe out other fields" do
2745
- expect(address.reload.street).to eq("Hobrecht")
2746
- end
2747
- end
2748
- end
2749
- end
2750
-
2751
- describe ".valid_options" do
2752
-
2753
- it "returns the valid options" do
2754
- expect(described_class.valid_options).to eq(
2755
- [
2756
- :as, :cascade_callbacks, :cyclic, :order,
2757
- :store_as, :before_add, :after_add, :before_remove, :after_remove
2758
- ]
2759
- )
2760
- end
2761
- end
2762
-
2763
- describe ".validation_default" do
2764
-
2765
- it "returns true" do
2766
- expect(described_class.validation_default).to be true
2767
- end
2768
- end
2769
-
2770
- context "when deeply embedding documents" do
2771
-
2772
- context "when updating the bottom level" do
2773
-
2774
- let!(:person) do
2775
- Person.create
2776
- end
2777
-
2778
- let!(:address) do
2779
- person.addresses.create(street: "Joachimstr")
2780
- end
2781
-
2782
- let!(:location) do
2783
- address.locations.create(name: "vacation", number: 0)
2784
- address.locations.create(name: "work", number: 3)
2785
- end
2786
-
2787
- context "when updating with replacement of embedded array" do
2788
-
2789
- context "when updating with a hash" do
2790
-
2791
- before do
2792
- address.update_attributes(locations: [{ name: "home" }])
2793
- end
2794
-
2795
- it "updates the attributes" do
2796
- expect(address.locations.first.name).to eq("home")
2797
- end
2798
-
2799
- it "overwrites the existing documents" do
2800
- expect(address.locations.count).to eq(1)
2801
- end
2802
-
2803
- it "persists the changes" do
2804
- expect(address.reload.locations.count).to eq(1)
2805
- end
2806
- end
2807
- end
2808
-
2809
- context "when updating a field in a document of the embedded array" do
2810
-
2811
- before do
2812
- location.number = 7
2813
- location.save
2814
- end
2815
-
2816
- let(:updated_location_number) do
2817
- person.reload.addresses.first.locations.find(location.id).number
2818
- end
2819
-
2820
- let(:updated_location_name) do
2821
- person.reload.addresses.first.locations.find(location.id).name
2822
- end
2823
-
2824
- it "the change is persisted" do
2825
- expect(updated_location_number).to eq(7)
2826
- end
2827
-
2828
- it "the other field remains unaffected" do
2829
- expect(updated_location_name).to eq("work")
2830
- end
2831
-
2832
- end
2833
- end
2834
-
2835
- context "when building the tree through hashes" do
2836
-
2837
- let(:circus) do
2838
- Circus.new(hash)
2839
- end
2840
-
2841
- let(:animal) do
2842
- circus.animals.first
2843
- end
2844
-
2845
- let(:animal_name) do
2846
- "Lion"
2847
- end
2848
-
2849
- let(:tag_list) do
2850
- "tigers, bears, oh my"
2851
- end
2852
-
2853
- context "when the hash uses stringified keys" do
2854
-
2855
- let(:hash) do
2856
- { 'animals' => [{ 'name' => animal_name, 'tag_list' => tag_list }] }
2857
- end
2858
-
2859
- it "sets up the hierarchy" do
2860
- expect(animal.circus).to eq(circus)
2861
- end
2862
-
2863
- it "assigns the attributes" do
2864
- expect(animal.name).to eq(animal_name)
2865
- end
2866
-
2867
- it "uses custom writer methods" do
2868
- expect(animal.tag_list).to eq(tag_list)
2869
- end
2870
- end
2871
-
2872
- context "when the hash uses symbolized keys" do
2873
-
2874
- let(:hash) do
2875
- { animals: [{ name: animal_name, tag_list: tag_list }] }
2876
- end
2877
-
2878
- it "sets up the hierarchy" do
2879
- expect(animal.circus).to eq(circus)
2880
- end
2881
-
2882
- it "assigns the attributes" do
2883
- expect(animal.name).to eq(animal_name)
2884
- end
2885
-
2886
- it "uses custom writer methods" do
2887
- expect(animal.tag_list).to eq(tag_list)
2888
- end
2889
- end
2890
- end
2891
-
2892
- context "when building the tree through pushes" do
2893
-
2894
- let(:quiz) do
2895
- Quiz.new
2896
- end
2897
-
2898
- let(:page) do
2899
- Page.new
2900
- end
2901
-
2902
- let(:page_question) do
2903
- PageQuestion.new
2904
- end
2905
-
2906
- before do
2907
- quiz.pages << page
2908
- page.page_questions << page_question
2909
- end
2910
-
2911
- let(:question) do
2912
- quiz.pages.first.page_questions.first
2913
- end
2914
-
2915
- it "sets up the hierarchy" do
2916
- expect(question).to eq(page_question)
2917
- end
2918
- end
2919
-
2920
- context "when building the tree through builds" do
2921
-
2922
- let!(:quiz) do
2923
- Quiz.new
2924
- end
2925
-
2926
- let!(:page) do
2927
- quiz.pages.build
2928
- end
2929
-
2930
- let!(:page_question) do
2931
- page.page_questions.build
2932
- end
2933
-
2934
- let(:question) do
2935
- quiz.pages.first.page_questions.first
2936
- end
2937
-
2938
- it "sets up the hierarchy" do
2939
- expect(question).to eq(page_question)
2940
- end
2941
- end
2942
-
2943
- context "when creating a persisted tree" do
2944
-
2945
- let(:quiz) do
2946
- Quiz.create
2947
- end
2948
-
2949
- let(:page) do
2950
- Page.new
2951
- end
2952
-
2953
- let(:page_question) do
2954
- PageQuestion.new
2955
- end
2956
-
2957
- let(:question) do
2958
- quiz.pages.first.page_questions.first
2959
- end
2960
-
2961
- before do
2962
- quiz.pages << page
2963
- page.page_questions << page_question
2964
- end
2965
-
2966
- it "sets up the hierarchy" do
2967
- expect(question).to eq(page_question)
2968
- end
2969
-
2970
- context "when reloading" do
2971
-
2972
- let(:from_db) do
2973
- quiz.reload
2974
- end
2975
-
2976
- let(:reloaded_question) do
2977
- from_db.pages.first.page_questions.first
2978
- end
2979
-
2980
- it "reloads the entire tree" do
2981
- expect(reloaded_question).to eq(question)
2982
- end
2983
- end
2984
- end
2985
- end
2986
-
2987
- context "when deeply nesting documents" do
2988
-
2989
- context "when all documents are new" do
2990
-
2991
- let(:person) do
2992
- Person.new
2993
- end
2994
-
2995
- let(:address) do
2996
- Address.new
2997
- end
2998
-
2999
- let(:location) do
3000
- Location.new
3001
- end
3002
-
3003
- before do
3004
- address.locations << location
3005
- person.addresses << address
3006
- end
3007
-
3008
- context "when saving the root" do
3009
-
3010
- before do
3011
- person.save
3012
- end
3013
-
3014
- it "persists the first level document" do
3015
- expect(person.reload.addresses.first).to eq(address)
3016
- end
3017
-
3018
- it "persists the second level document" do
3019
- expect(person.reload.addresses[0].locations).to eq([ location ])
3020
- end
3021
- end
3022
- end
3023
- end
3024
-
3025
- context "when attempting nil pushes and substitutes" do
3026
-
3027
- let(:home_phone) do
3028
- Phone.new(number: "555-555-5555")
3029
- end
3030
-
3031
- let(:office_phone) do
3032
- Phone.new(number: "666-666-6666")
3033
- end
3034
-
3035
- describe "replacing the entire embedded list" do
3036
-
3037
- context "when an embeds many relationship contains a nil as the first item" do
3038
-
3039
- let(:person) do
3040
- Person.create!
3041
- end
3042
-
3043
- let(:phone_list) do
3044
- [nil, home_phone, office_phone]
3045
- end
3046
-
3047
- before do
3048
- person.phone_numbers = phone_list
3049
- person.save!
3050
- end
3051
-
3052
- it "ignores the nil and persist the remaining items" do
3053
- reloaded = Person.find(person.id)
3054
- expect(reloaded.phone_numbers).to eq([ home_phone, office_phone ])
3055
- end
3056
- end
3057
-
3058
- context "when an embeds many relationship contains a nil in the middle of the list" do
3059
-
3060
- let(:person) do
3061
- Person.create!
3062
- end
3063
-
3064
- let(:phone_list) do
3065
- [home_phone, nil, office_phone]
3066
- end
3067
-
3068
- before do
3069
- person.phone_numbers = phone_list
3070
- person.save!
3071
- end
3072
-
3073
- it "ignores the nil and persist the remaining items" do
3074
- reloaded = Person.find(person.id)
3075
- expect(reloaded.phone_numbers).to eq([ home_phone, office_phone ])
3076
- end
3077
- end
3078
-
3079
- context "when an embeds many relationship contains a nil at the end of the list" do
3080
-
3081
- let(:person) do
3082
- Person.create!
3083
- end
3084
-
3085
- let(:phone_list) do
3086
- [home_phone, office_phone, nil]
3087
- end
3088
-
3089
- before do
3090
- person.phone_numbers = phone_list
3091
- person.save!
3092
- end
3093
-
3094
- it "ignores the nil and persist the remaining items" do
3095
- reloaded = Person.find(person.id)
3096
- expect(reloaded.phone_numbers).to eq([ home_phone, office_phone ])
3097
- end
3098
- end
3099
- end
3100
-
3101
- describe "appending to the embedded list" do
3102
-
3103
- context "when appending a nil to the first position in an embedded list" do
3104
-
3105
- let(:person) do
3106
- Person.create! phone_numbers: []
3107
- end
3108
-
3109
- before do
3110
- person.phone_numbers << nil
3111
- person.phone_numbers << home_phone
3112
- person.phone_numbers << office_phone
3113
- person.save!
3114
- end
3115
-
3116
- it "ignores the nil and persist the remaining items" do
3117
- reloaded = Person.find(person.id)
3118
- expect(reloaded.phone_numbers).to eq(person.phone_numbers)
3119
- end
3120
- end
3121
-
3122
- context "when appending a nil into the middle of an embedded list" do
3123
-
3124
- let(:person) do
3125
- Person.create! phone_numbers: []
3126
- end
3127
-
3128
- before do
3129
- person.phone_numbers << home_phone
3130
- person.phone_numbers << nil
3131
- person.phone_numbers << office_phone
3132
- person.save!
3133
- end
3134
-
3135
- it "ignores the nil and persist the remaining items" do
3136
- reloaded = Person.find(person.id)
3137
- expect(reloaded.phone_numbers).to eq(person.phone_numbers)
3138
- end
3139
- end
3140
-
3141
- context "when appending a nil to the end of an embedded list" do
3142
-
3143
- let(:person) do
3144
- Person.create! phone_numbers: []
3145
- end
3146
-
3147
- before do
3148
- person.phone_numbers << home_phone
3149
- person.phone_numbers << office_phone
3150
- person.phone_numbers << nil
3151
- person.save!
3152
- end
3153
-
3154
- it "ignores the nil and persist the remaining items" do
3155
- reloaded = Person.find(person.id)
3156
- expect(reloaded.phone_numbers).to eq(person.phone_numbers)
3157
- end
3158
- end
3159
- end
3160
- end
3161
-
3162
- context "when accessing the parent in a destroy callback" do
3163
-
3164
- let!(:league) do
3165
- League.create
3166
- end
3167
-
3168
- let!(:division) do
3169
- league.divisions.create
3170
- end
3171
-
3172
- before do
3173
- league.destroy
3174
- end
3175
-
3176
- it "retains the reference to the parent" do
3177
- expect(league.name).to eq("Destroyed")
3178
- end
3179
- end
3180
-
3181
- context "when updating the parent with all attributes" do
3182
-
3183
- let!(:person) do
3184
- Person.create
3185
- end
3186
-
3187
- let!(:address) do
3188
- person.addresses.create
3189
- end
3190
-
3191
- before do
3192
- person.update_attributes(person.attributes)
3193
- end
3194
-
3195
- it "does not duplicate the embedded documents" do
3196
- expect(person.addresses).to eq([ address ])
3197
- end
3198
-
3199
- it "does not persist duplicate embedded documents" do
3200
- expect(person.reload.addresses).to eq([ address ])
3201
- end
3202
- end
3203
-
3204
- context "when embedding children named versions" do
3205
-
3206
- let(:acolyte) do
3207
- Acolyte.create(name: "test")
3208
- end
3209
-
3210
- context "when creating a child" do
3211
-
3212
- let(:version) do
3213
- acolyte.versions.create(number: 1)
3214
- end
3215
-
3216
- it "allows the operation" do
3217
- expect(version.number).to eq(1)
3218
- end
3219
-
3220
- context "when reloading the parent" do
3221
-
3222
- let(:from_db) do
3223
- acolyte.reload
3224
- end
3225
-
3226
- it "saves the child versions" do
3227
- expect(from_db.versions).to eq([ version ])
3228
- end
3229
- end
3230
- end
3231
- end
3232
-
3233
- context "when validating the parent before accessing the child" do
3234
-
3235
- let!(:account) do
3236
- Account.new(name: "Testing").tap do |acct|
3237
- acct.memberships.build
3238
- acct.save
3239
- end
3240
- end
3241
-
3242
- let(:from_db) do
3243
- Account.first
3244
- end
3245
-
3246
- context "when saving" do
3247
-
3248
- before do
3249
- account.name = ""
3250
- account.save
3251
- end
3252
-
3253
- it "does not lose the parent reference" do
3254
- expect(from_db.memberships.first.account).to eq(account)
3255
- end
3256
- end
3257
-
3258
- context "when updating attributes" do
3259
-
3260
- before do
3261
- from_db.update_attributes(name: "")
3262
- end
3263
-
3264
- it "does not lose the parent reference" do
3265
- expect(from_db.memberships.first.account).to eq(account)
3266
- end
3267
- end
3268
- end
3269
-
3270
- context "when moving an embedded document from one parent to another" do
3271
-
3272
- let!(:person_one) do
3273
- Person.create
3274
- end
3275
-
3276
- let!(:person_two) do
3277
- Person.create
3278
- end
3279
-
3280
- let!(:address) do
3281
- person_one.addresses.create(street: "Kudamm")
3282
- end
3283
-
3284
- before do
3285
- person_two.addresses << address
3286
- end
3287
-
3288
- it "adds the document to the new paarent" do
3289
- expect(person_two.addresses).to eq([ address ])
3290
- end
3291
-
3292
- it "sets the new parent on the document" do
3293
- expect(address._parent).to eq(person_two)
3294
- end
3295
-
3296
- context "when reloading the documents" do
3297
-
3298
- before do
3299
- person_one.reload
3300
- person_two.reload
3301
- end
3302
-
3303
- it "persists the change to the new parent" do
3304
- expect(person_two.addresses).to eq([ address ])
3305
- end
3306
-
3307
- it "keeps the address on the previous document" do
3308
- expect(person_one.addresses).to eq([ address ])
3309
- end
3310
- end
3311
- end
3312
-
3313
- context "when the relation has a default scope" do
3314
-
3315
- let!(:person) do
3316
- Person.create
3317
- end
3318
-
3319
- context "when the default scope is a sort" do
3320
-
3321
- let(:cough) do
3322
- Symptom.new(name: "cough")
3323
- end
3324
-
3325
- let(:headache) do
3326
- Symptom.new(name: "headache")
3327
- end
3328
-
3329
- let(:nausea) do
3330
- Symptom.new(name: "nausea")
3331
- end
3332
-
3333
- before do
3334
- person.symptoms.concat([ nausea, cough, headache ])
3335
- end
3336
-
3337
- context "when accessing the relation" do
3338
-
3339
- let(:symptoms) do
3340
- person.reload.symptoms
3341
- end
3342
-
3343
- it "applies the default scope" do
3344
- expect(symptoms).to eq([ cough, headache, nausea ])
3345
- end
3346
- end
3347
-
3348
- context "when modifying the relation" do
3349
-
3350
- let(:constipation) do
3351
- Symptom.new(name: "constipation")
3352
- end
3353
-
3354
- before do
3355
- person.symptoms.push(constipation)
3356
- end
3357
-
3358
- context "when reloading" do
3359
-
3360
- let(:symptoms) do
3361
- person.reload.symptoms
3362
- end
3363
-
3364
- it "applies the default scope" do
3365
- expect(symptoms).to eq([ constipation, cough, headache, nausea ])
3366
- end
3367
- end
3368
- end
3369
-
3370
- context "when unscoping the relation" do
3371
-
3372
- let(:unscoped) do
3373
- person.reload.symptoms.unscoped
3374
- end
3375
-
3376
- it "removes the default scope" do
3377
- expect(unscoped).to eq([ nausea, cough, headache ])
3378
- end
3379
- end
3380
- end
3381
- end
3382
-
3383
- context "when indexing the documents" do
3384
-
3385
- let!(:person) do
3386
- Person.create
3387
- end
3388
-
3389
- context "when the documents have a limiting default scope" do
3390
-
3391
- let(:active) do
3392
- Appointment.new
3393
- end
3394
-
3395
- let(:inactive) do
3396
- Appointment.new(active: false)
3397
- end
3398
-
3399
- before do
3400
- person.appointments.concat([ inactive, active ])
3401
- end
3402
-
3403
- let(:relation) do
3404
- person.reload.appointments
3405
- end
3406
-
3407
- it "retains the unscoped index for the excluded document" do
3408
- expect(relation.send(:_unscoped).first._index).to eq(0)
3409
- end
3410
-
3411
- it "retains the unscoped index for the included document" do
3412
- expect(relation.first._index).to eq(1)
3413
- end
3414
-
3415
- context "when a reindexing operation occurs" do
3416
-
3417
- before do
3418
- relation.send(:reindex)
3419
- end
3420
-
3421
- it "retains the unscoped index for the excluded document" do
3422
- expect(relation.send(:_unscoped).first._index).to eq(0)
3423
- end
3424
-
3425
- it "retains the unscoped index for the included document" do
3426
- expect(relation.first._index).to eq(1)
3427
- end
3428
- end
3429
- end
3430
- end
3431
-
3432
- context "when the embedded document has an array field" do
3433
-
3434
- let!(:person) do
3435
- Person.create
3436
- end
3437
-
3438
- let!(:video) do
3439
- person.videos.create
3440
- end
3441
-
3442
- context "when saving the array on a persisted document" do
3443
-
3444
- before do
3445
- video.genres = [ "horror", "scifi" ]
3446
- video.save
3447
- end
3448
-
3449
- it "sets the value" do
3450
- expect(video.genres).to eq([ "horror", "scifi" ])
3451
- end
3452
-
3453
- it "persists the value" do
3454
- expect(video.reload.genres).to eq([ "horror", "scifi" ])
3455
- end
3456
-
3457
- context "when reloading the parent" do
3458
-
3459
- let!(:loaded_person) do
3460
- Person.find(person.id)
3461
- end
3462
-
3463
- let!(:loaded_video) do
3464
- loaded_person.videos.find(video.id)
3465
- end
3466
-
3467
- context "when writing a new array value" do
3468
-
3469
- before do
3470
- loaded_video.genres = [ "comedy" ]
3471
- loaded_video.save
3472
- end
3473
-
3474
- it "sets the new value" do
3475
- expect(loaded_video.genres).to eq([ "comedy" ])
3476
- end
3477
-
3478
- it "persists the new value" do
3479
- expect(loaded_video.reload.genres).to eq([ "comedy" ])
3480
- end
3481
- end
3482
- end
3483
- end
3484
- end
3485
-
3486
- context "when destroying an embedded document" do
3487
-
3488
- let(:person) do
3489
- Person.create
3490
- end
3491
-
3492
- let!(:address_one) do
3493
- person.addresses.create(street: "hobrecht")
3494
- end
3495
-
3496
- let!(:address_two) do
3497
- person.addresses.create(street: "maybachufer")
3498
- end
3499
-
3500
- before do
3501
- address_one.destroy
3502
- end
3503
-
3504
- it "destroys the document" do
3505
- expect(address_one).to be_destroyed
3506
- end
3507
-
3508
- it "reindexes the relation" do
3509
- expect(address_two._index).to eq(0)
3510
- end
3511
-
3512
- it "removes the document from the unscoped" do
3513
- expect(person.addresses.send(:_unscoped)).to_not include(address_one)
3514
- end
3515
-
3516
- context "when subsequently updating the next document" do
3517
-
3518
- before do
3519
- address_two.update_attribute(:number, 10)
3520
- end
3521
-
3522
- let(:addresses) do
3523
- person.reload.addresses
3524
- end
3525
-
3526
- it "updates the correct document" do
3527
- expect(addresses.first.number).to eq(10)
3528
- end
3529
-
3530
- it "does not add additional documents" do
3531
- expect(addresses.count).to eq(1)
3532
- end
3533
- end
3534
- end
3535
-
3536
- context "when adding a document" do
3537
-
3538
- let(:person) do
3539
- Person.new
3540
- end
3541
-
3542
- let(:address_one) do
3543
- Address.new(street: "hobrecht")
3544
- end
3545
-
3546
- let(:first_add) do
3547
- person.addresses.push(address_one)
3548
- end
3549
-
3550
- context "when chaining a second add" do
3551
-
3552
- let(:address_two) do
3553
- Address.new(street: "friedel")
3554
- end
3555
-
3556
- let(:result) do
3557
- first_add.push(address_two)
3558
- end
3559
-
3560
- it "adds both documents" do
3561
- expect(result).to eq([ address_one, address_two ])
3562
- end
3563
- end
3564
- end
3565
-
3566
- context "when the association has an order defined" do
3567
-
3568
- let(:person) do
3569
- Person.create
3570
- end
3571
-
3572
- let(:message_one) do
3573
- Message.new(priority: 5, body: 'This is a test')
3574
- end
3575
-
3576
- let(:message_two) do
3577
- Message.new(priority: 10, body: 'This is a test')
3578
- end
3579
-
3580
- let(:message_three) do
3581
- Message.new(priority: 20, body: 'Zee test')
3582
- end
3583
-
3584
- before do
3585
- person.messages.push(message_one, message_two, message_three)
3586
- end
3587
-
3588
- let(:criteria) do
3589
- person.messages.order_by(:body.asc, :priority.desc)
3590
- end
3591
-
3592
- it "properly orders the related objects" do
3593
- expect(criteria.to_a).to eq([message_two, message_one, message_three])
3594
- end
3595
-
3596
- context "when the field to order on is an array of documents" do
3597
-
3598
- before do
3599
- person.aliases = [ { name: "A", priority: 3 }, { name: "B", priority: 4 }]
3600
- person.save
3601
- end
3602
-
3603
- let!(:person2) do
3604
- Person.create( aliases: [ { name: "C", priority: 1 }, { name: "D", priority: 2 }])
3605
- end
3606
-
3607
- it "allows ordering on a key of an embedded document" do
3608
- expect(Person.all.order_by("aliases.0.priority" => 1).first).to eq(person2)
3609
- end
3610
- end
3611
- end
3612
-
3613
- context "when using dot notation in a criteria" do
3614
-
3615
- let(:person) do
3616
- Person.new
3617
- end
3618
-
3619
- let!(:address) do
3620
- person.addresses.build(street: "hobrecht")
3621
- end
3622
-
3623
- let!(:location) do
3624
- address.locations.build(number: 5)
3625
- end
3626
-
3627
- let(:criteria) do
3628
- person.addresses.where("locations.number" => { "$gt" => 3 })
3629
- end
3630
-
3631
- it "allows the dot notation criteria" do
3632
- expect(criteria).to eq([ address ])
3633
- end
3634
- end
3635
-
3636
- context "when updating multiple levels in one update" do
3637
-
3638
- let!(:person) do
3639
- Person.create(
3640
- addresses: [
3641
- { locations: [{ name: "home" }]}
3642
- ]
3643
- )
3644
- end
3645
-
3646
- context "when updating with hashes" do
3647
-
3648
- let(:from_db) do
3649
- Person.find(person.id)
3650
- end
3651
-
3652
- before do
3653
- from_db.update_attributes(
3654
- addresses: [
3655
- { locations: [{ name: "work" }]}
3656
- ]
3657
- )
3658
- end
3659
-
3660
- let(:updated) do
3661
- person.reload.addresses.first.locations.first
3662
- end
3663
-
3664
- it "updates the nested document" do
3665
- expect(updated.name).to eq("work")
3666
- end
3667
- end
3668
- end
3669
-
3670
- context "when the embedded relation sorts on a boolean" do
3671
-
3672
- let(:circuit) do
3673
- Circuit.create
3674
- end
3675
-
3676
- let!(:bus_one) do
3677
- circuit.buses.create(saturday: true)
3678
- end
3679
-
3680
- let!(:bus_two) do
3681
- circuit.buses.create(saturday: false)
3682
- end
3683
-
3684
- it "orders properly with the boolean" do
3685
- expect(circuit.reload.buses).to eq([ bus_two, bus_one ])
3686
- end
3687
- end
3688
-
3689
- context "when batch replacing multiple relations in a single update" do
3690
-
3691
- let(:document) do
3692
- Person.create
3693
- end
3694
-
3695
- let(:person) do
3696
- Person.find(document.id)
3697
- end
3698
-
3699
- let!(:symptom_one) do
3700
- person.symptoms.create
3701
- end
3702
-
3703
- let!(:symptom_two) do
3704
- person.symptoms.create
3705
- end
3706
-
3707
- let!(:appointment_one) do
3708
- person.appointments.create
3709
- end
3710
-
3711
- let!(:appointment_two) do
3712
- person.appointments.create
3713
- end
3714
-
3715
- before do
3716
- person.update_attributes(
3717
- appointments: [ appointment_one.as_document, appointment_two.as_document ],
3718
- symptoms: [ symptom_one.as_document, symptom_two.as_document ]
3719
- )
3720
- end
3721
-
3722
- it "does not duplicate the first relation" do
3723
- expect(person.reload.symptoms.count).to eq(2)
3724
- end
3725
-
3726
- it "does not duplicate the second relation" do
3727
- expect(person.reload.appointments.count).to eq(2)
3728
- end
3729
- end
3730
-
3731
- context "when pushing with a before_add callback" do
3732
-
3733
- let(:artist) do
3734
- Artist.new
3735
- end
3736
-
3737
- let(:song) do
3738
- Song.new
3739
- end
3740
-
3741
- context "when no errors are raised" do
3742
-
3743
- before do
3744
- artist.songs << song
3745
- end
3746
-
3747
- it "executes the callback" do
3748
- expect(artist.before_add_called).to be true
3749
- end
3750
-
3751
- it "executes the callback as proc" do
3752
- expect(song.before_add_called).to be true
3753
- end
3754
-
3755
- it "adds the document to the relation" do
3756
- expect(artist.songs).to eq([song])
3757
- end
3758
- end
3759
-
3760
- context "with errors" do
3761
-
3762
- before do
3763
- expect(artist).to receive(:before_add_song).and_raise
3764
- end
3765
-
3766
- it "does not add the document to the relation" do
3767
- expect {
3768
- artist.songs << song
3769
- }.to raise_error
3770
- expect(artist.songs).to be_empty
3771
- end
3772
- end
3773
- end
3774
-
3775
- context "when pushing with an after_add callback" do
3776
-
3777
- let(:artist) do
3778
- Artist.new
3779
- end
3780
-
3781
- let(:label) do
3782
- Label.new
3783
- end
3784
-
3785
- it "executes the callback" do
3786
- artist.labels << label
3787
- expect(artist.after_add_called).to be true
3788
- end
3789
-
3790
- context "when errors are raised" do
3791
-
3792
- before do
3793
- expect(artist).to receive(:after_add_label).and_raise
3794
- end
3795
-
3796
- it "adds the document to the relation" do
3797
- expect {
3798
- artist.labels << label
3799
- }.to raise_error
3800
- expect(artist.labels).to eq([ label ])
3801
- end
3802
- end
3803
- end
3804
-
3805
- context "#delete or #clear with before_remove callback" do
3806
-
3807
- let(:artist) do
3808
- Artist.new
3809
- end
3810
-
3811
- let(:song) do
3812
- Song.new
3813
- end
3814
-
3815
- before do
3816
- artist.songs << song
3817
- end
3818
-
3819
- context "when no errors are raised" do
3820
-
3821
- describe "#delete" do
3822
-
3823
- before do
3824
- artist.songs.delete(song)
3825
- end
3826
-
3827
- it "executes the callback" do
3828
- expect(artist.before_remove_embedded_called).to be true
3829
- end
3830
-
3831
- it "removes the document from the relation" do
3832
- expect(artist.songs).to be_empty
3833
- end
3834
- end
3835
-
3836
- describe "#clear" do
3837
-
3838
- before do
3839
- artist.songs.clear
3840
- end
3841
-
3842
- it "executes the callback" do
3843
- expect(artist.before_remove_embedded_called).to be true
3844
- end
3845
-
3846
- it "shoud clear the relation" do
3847
- expect(artist.songs).to be_empty
3848
- end
3849
- end
3850
-
3851
- context "when errors are raised" do
3852
-
3853
- before do
3854
- expect(artist).to receive(:before_remove_song).and_raise
3855
- end
3856
-
3857
- describe "#delete" do
3858
-
3859
- it "does not remove the document from the relation" do
3860
- expect {
3861
- artist.songs.delete(song)
3862
- }.to raise_error
3863
- expect(artist.songs).to eq([ song ])
3864
- end
3865
- end
3866
-
3867
- describe "#clear" do
3868
-
3869
- it "removes the documents from the relation" do
3870
- expect {
3871
- artist.songs.clear
3872
- }.to raise_error
3873
- expect(artist.songs).to eq([ song ])
3874
- end
3875
- end
3876
- end
3877
- end
3878
- end
3879
-
3880
- context "#delete or #clear with after_remove callback" do
3881
-
3882
- let(:artist) do
3883
- Artist.new
3884
- end
3885
-
3886
- let(:label) do
3887
- Label.new
3888
- end
3889
-
3890
- before do
3891
- artist.labels << label
3892
- end
3893
-
3894
- context "when no errors are raised" do
3895
-
3896
- describe "#delete" do
3897
- before do
3898
- artist.labels.delete(label)
3899
- end
3900
-
3901
- it "executes the callback" do
3902
- expect(artist.after_remove_embedded_called).to be true
3903
- end
3904
- end
3905
-
3906
- describe "#clear" do
3907
-
3908
- before do
3909
- artist.labels.clear
3910
- end
3911
-
3912
- it "executes the callback" do
3913
- artist.labels.clear
3914
- expect(artist.after_remove_embedded_called).to be true
3915
- end
3916
- end
3917
- end
3918
-
3919
- context "when errors are raised" do
3920
-
3921
- before do
3922
- expect(artist).to receive(:after_remove_label).and_raise
3923
- end
3924
-
3925
- describe "#delete" do
3926
-
3927
- before do
3928
- expect {
3929
- artist.labels.delete(label)
3930
- }.to raise_error
3931
- end
3932
-
3933
- it "removes the document from the relation" do
3934
- expect(artist.labels).to be_empty
3935
- end
3936
- end
3937
-
3938
- describe "#clear" do
3939
-
3940
- before do
3941
- expect {
3942
- artist.labels.clear
3943
- }.to raise_error
3944
- end
3945
-
3946
- it "should remove from collection" do
3947
- expect(artist.labels).to be_empty
3948
- end
3949
- end
3950
- end
3951
- end
3952
-
3953
- context "when saving at the parent level" do
3954
-
3955
- let!(:server) do
3956
- Server.new(name: "staging")
3957
- end
3958
-
3959
- let!(:filesystem) do
3960
- server.filesystems.build
3961
- end
3962
-
3963
- context "when the parent has an after create callback" do
3964
-
3965
- before do
3966
- server.save
3967
- end
3968
-
3969
- it "does not push the embedded documents twice" do
3970
- expect(server.reload.filesystems.count).to eq(1)
3971
- end
3972
- end
3973
- end
3974
-
3975
- context "when embedded documents are stored without ids" do
3976
-
3977
- let!(:band) do
3978
- Band.create(name: "Moderat")
3979
- end
3980
-
3981
- before do
3982
- band.collection.
3983
- find(_id: band.id).
3984
- update_one("$set" => { records: [{ name: "Moderat" }]})
3985
- end
3986
-
3987
- context "when loading the documents" do
3988
-
3989
- before do
3990
- band.reload
3991
- end
3992
-
3993
- let(:record) do
3994
- band.records.first
3995
- end
3996
-
3997
- it "creates proper documents from the db" do
3998
- expect(record.name).to eq("Moderat")
3999
- end
4000
-
4001
- it "assigns ids to the documents" do
4002
- expect(record.id).to_not be_nil
4003
- end
4004
-
4005
- context "when subsequently updating the documents" do
4006
-
4007
- before do
4008
- record.update_attribute(:name, "Apparat")
4009
- end
4010
-
4011
- it "updates the document" do
4012
- expect(record.name).to eq("Apparat")
4013
- end
4014
-
4015
- it "persists the change" do
4016
- expect(record.reload.name).to eq("Apparat")
4017
- end
4018
- end
4019
- end
4020
- end
4021
-
4022
- context "when embedded documents get marshalled" do
4023
-
4024
- let(:person) do
4025
- Person.create
4026
- end
4027
-
4028
- let!(:addresses) do
4029
- person.addresses
4030
- end
4031
-
4032
- let!(:dumped) do
4033
- Marshal.dump(addresses)
4034
- end
4035
-
4036
- let!(:loaded) do
4037
- Marshal.load(dumped)
4038
- end
4039
-
4040
- it "keeps the proxy extensions when remarshalling" do
4041
- expect(loaded.extension).to eq("Testing")
4042
- end
4043
- end
4044
-
4045
- context "deleting embedded documents" do
4046
-
4047
- it "able to delete embedded documents upon condition" do
4048
- company = Company.new
4049
- 4.times { |i| company.staffs << Staff.new(age: 50 + i)}
4050
- 2.times { |i| company.staffs << Staff.new(age: 40)}
4051
- company.save
4052
- company.staffs.delete_if {|x| x.age >= 50}
4053
- expect(company.staffs.count).to eq(2)
4054
- end
4055
- end
4056
-
4057
- context "when substituting polymorphic documents" do
4058
-
4059
- before(:all) do
4060
- class DNS; end
4061
-
4062
- class DNS::Zone
4063
- include Mongoid::Document
4064
- embeds_many :rrsets, class_name: 'DNS::RRSet', inverse_of: :zone
4065
- embeds_one :soa, class_name: 'DNS::Record', as: :container
4066
- end
4067
-
4068
- class DNS::RRSet
4069
- include Mongoid::Document
4070
- embedded_in :zone, class_name: 'DNS::Zone', inverse_of: :rrsets
4071
- embeds_many :records, class_name: 'DNS::Record', as: :container
4072
- end
4073
-
4074
- class DNS::Record
4075
- include Mongoid::Document
4076
- embedded_in :container, polymorphic: true
4077
- end
4078
- end
4079
-
4080
- after(:all) do
4081
- Object.send(:remove_const, :DNS)
4082
- end
4083
-
4084
- context "when the parent is new" do
4085
-
4086
- let(:zone) do
4087
- DNS::Zone.new
4088
- end
4089
-
4090
- let(:soa_1) do
4091
- DNS::Record.new
4092
- end
4093
-
4094
- context "when replacing the set document" do
4095
-
4096
- let(:soa_2) do
4097
- DNS::Record.new
4098
- end
4099
-
4100
- before do
4101
- zone.soa = soa_1
4102
- end
4103
-
4104
- it "properly sets the metadata" do
4105
- expect(zone.soa = soa_2).to eq(soa_2)
4106
- end
4107
- end
4108
-
4109
- context "when deleting the set document" do
4110
-
4111
- let(:soa_2) do
4112
- DNS::Record.new
4113
- end
4114
-
4115
- before do
4116
- zone.soa = soa_1
4117
- end
4118
-
4119
- it "properly sets the metadata" do
4120
- expect(zone.soa.delete).to be true
4121
- end
4122
- end
4123
- end
4124
-
4125
- context "when the parent is persisted" do
4126
-
4127
- let(:zone) do
4128
- DNS::Zone.create
4129
- end
4130
-
4131
- let(:soa_1) do
4132
- DNS::Record.new
4133
- end
4134
-
4135
- context "when replacing the set document" do
4136
-
4137
- let(:soa_2) do
4138
- DNS::Record.new
4139
- end
4140
-
4141
- before do
4142
- zone.soa = soa_1
4143
- end
4144
-
4145
- it "properly sets the metadata" do
4146
- expect(zone.soa = soa_2).to eq(soa_2)
4147
- end
4148
- end
4149
-
4150
- context "when deleting the set document" do
4151
-
4152
- let(:soa_2) do
4153
- DNS::Record.new
4154
- end
4155
-
4156
- before do
4157
- zone.soa = soa_1
4158
- end
4159
-
4160
- it "properly sets the metadata" do
4161
- expect(zone.soa.delete).to be true
4162
- end
4163
- end
4164
- end
4165
- end
4166
- end