mongoid-ultra 9.0.0.0.alpha1

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