mongoid 5.4.1 → 7.4.0

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