mongoid 6.4.4 → 7.5.1

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