sepastian-mongoid-rails4 4.0.0.alpha

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