mongoid-ultra 9.0.0.0.alpha1

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