mongoid 3.1.4 → 3.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (429) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +34 -0
  3. data/lib/mongoid/contextual.rb +3 -0
  4. data/lib/mongoid/criteria.rb +10 -2
  5. data/lib/mongoid/document.rb +1 -1
  6. data/lib/mongoid/paranoia.rb +4 -0
  7. data/lib/mongoid/persistence/upsertion.rb +1 -0
  8. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +16 -2
  9. data/lib/mongoid/relations/macros.rb +1 -1
  10. data/lib/mongoid/relations/one.rb +12 -0
  11. data/lib/mongoid/relations/referenced/in.rb +18 -1
  12. data/lib/mongoid/relations/referenced/many_to_many.rb +2 -2
  13. data/lib/mongoid/validations.rb +1 -1
  14. data/lib/mongoid/validations/localizable.rb +1 -1
  15. data/lib/mongoid/validations/presence.rb +1 -1
  16. data/lib/mongoid/validations/uniqueness.rb +1 -1
  17. data/lib/mongoid/version.rb +1 -1
  18. data/lib/support/ruby_version.rb +1 -1
  19. data/spec/app/models/account.rb +32 -0
  20. data/spec/app/models/acolyte.rb +16 -0
  21. data/spec/app/models/actor.rb +19 -0
  22. data/spec/app/models/actor_observer.rb +15 -0
  23. data/spec/app/models/actress.rb +2 -0
  24. data/spec/app/models/address.rb +72 -0
  25. data/spec/app/models/address_component.rb +5 -0
  26. data/spec/app/models/address_number.rb +6 -0
  27. data/spec/app/models/agency.rb +5 -0
  28. data/spec/app/models/agent.rb +12 -0
  29. data/spec/app/models/album.rb +14 -0
  30. data/spec/app/models/alert.rb +5 -0
  31. data/spec/app/models/animal.rb +25 -0
  32. data/spec/app/models/answer.rb +4 -0
  33. data/spec/app/models/appointment.rb +7 -0
  34. data/spec/app/models/article.rb +13 -0
  35. data/spec/app/models/artist.rb +66 -0
  36. data/spec/app/models/artwork.rb +4 -0
  37. data/spec/app/models/augmentation.rb +11 -0
  38. data/spec/app/models/author.rb +6 -0
  39. data/spec/app/models/band.rb +25 -0
  40. data/spec/app/models/bar.rb +9 -0
  41. data/spec/app/models/basic.rb +6 -0
  42. data/spec/app/models/bed.rb +1 -0
  43. data/spec/app/models/big_palette.rb +2 -0
  44. data/spec/app/models/birthday.rb +13 -0
  45. data/spec/app/models/book.rb +12 -0
  46. data/spec/app/models/breed.rb +4 -0
  47. data/spec/app/models/browser.rb +6 -0
  48. data/spec/app/models/building.rb +7 -0
  49. data/spec/app/models/building_address.rb +7 -0
  50. data/spec/app/models/bus.rb +7 -0
  51. data/spec/app/models/business.rb +5 -0
  52. data/spec/app/models/callback_recorder.rb +25 -0
  53. data/spec/app/models/callback_test.rb +9 -0
  54. data/spec/app/models/canvas.rb +25 -0
  55. data/spec/app/models/car.rb +1 -0
  56. data/spec/app/models/cat.rb +8 -0
  57. data/spec/app/models/category.rb +8 -0
  58. data/spec/app/models/child.rb +4 -0
  59. data/spec/app/models/child_doc.rb +22 -0
  60. data/spec/app/models/church.rb +4 -0
  61. data/spec/app/models/circle.rb +3 -0
  62. data/spec/app/models/circuit.rb +4 -0
  63. data/spec/app/models/circus.rb +7 -0
  64. data/spec/app/models/code.rb +5 -0
  65. data/spec/app/models/comment.rb +16 -0
  66. data/spec/app/models/contractor.rb +7 -0
  67. data/spec/app/models/cookie.rb +6 -0
  68. data/spec/app/models/country_code.rb +8 -0
  69. data/spec/app/models/definition.rb +8 -0
  70. data/spec/app/models/description.rb +11 -0
  71. data/spec/app/models/dictionary.rb +10 -0
  72. data/spec/app/models/division.rb +10 -0
  73. data/spec/app/models/doctor.rb +12 -0
  74. data/spec/app/models/dog.rb +7 -0
  75. data/spec/app/models/dokument.rb +5 -0
  76. data/spec/app/models/driver.rb +7 -0
  77. data/spec/app/models/drug.rb +8 -0
  78. data/spec/app/models/email.rb +6 -0
  79. data/spec/app/models/employer.rb +5 -0
  80. data/spec/app/models/entry.rb +6 -0
  81. data/spec/app/models/eraser.rb +1 -0
  82. data/spec/app/models/event.rb +22 -0
  83. data/spec/app/models/exhibition.rb +4 -0
  84. data/spec/app/models/exhibitor.rb +5 -0
  85. data/spec/app/models/eye.rb +9 -0
  86. data/spec/app/models/eye_bowl.rb +9 -0
  87. data/spec/app/models/face.rb +8 -0
  88. data/spec/app/models/favorite.rb +6 -0
  89. data/spec/app/models/filesystem.rb +4 -0
  90. data/spec/app/models/fire_hydrant.rb +6 -0
  91. data/spec/app/models/firefox.rb +4 -0
  92. data/spec/app/models/fish.rb +8 -0
  93. data/spec/app/models/folder.rb +7 -0
  94. data/spec/app/models/folder_item.rb +9 -0
  95. data/spec/app/models/fruits.rb +28 -0
  96. data/spec/app/models/game.rb +21 -0
  97. data/spec/app/models/ghost.rb +7 -0
  98. data/spec/app/models/home.rb +4 -0
  99. data/spec/app/models/house.rb +8 -0
  100. data/spec/app/models/html_writer.rb +3 -0
  101. data/spec/app/models/image.rb +22 -0
  102. data/spec/app/models/implant.rb +16 -0
  103. data/spec/app/models/item.rb +12 -0
  104. data/spec/app/models/jar.rb +7 -0
  105. data/spec/app/models/label.rb +40 -0
  106. data/spec/app/models/language.rb +5 -0
  107. data/spec/app/models/lat_lng.rb +15 -0
  108. data/spec/app/models/league.rb +11 -0
  109. data/spec/app/models/learner.rb +2 -0
  110. data/spec/app/models/line_item.rb +6 -0
  111. data/spec/app/models/location.rb +8 -0
  112. data/spec/app/models/login.rb +8 -0
  113. data/spec/app/models/manufacturer.rb +7 -0
  114. data/spec/app/models/meat.rb +4 -0
  115. data/spec/app/models/membership.rb +4 -0
  116. data/spec/app/models/mixed_drink.rb +4 -0
  117. data/spec/app/models/movie.rb +12 -0
  118. data/spec/app/models/my_hash.rb +2 -0
  119. data/spec/app/models/name.rb +23 -0
  120. data/spec/app/models/node.rb +5 -0
  121. data/spec/app/models/note.rb +12 -0
  122. data/spec/app/models/ordered_post.rb +6 -0
  123. data/spec/app/models/ordered_preference.rb +6 -0
  124. data/spec/app/models/oscar.rb +15 -0
  125. data/spec/app/models/override.rb +16 -0
  126. data/spec/app/models/ownable.rb +6 -0
  127. data/spec/app/models/owner.rb +6 -0
  128. data/spec/app/models/pack.rb +3 -0
  129. data/spec/app/models/page.rb +5 -0
  130. data/spec/app/models/page_question.rb +4 -0
  131. data/spec/app/models/palette.rb +7 -0
  132. data/spec/app/models/paranoid_phone.rb +25 -0
  133. data/spec/app/models/paranoid_post.rb +36 -0
  134. data/spec/app/models/parent.rb +5 -0
  135. data/spec/app/models/parent_doc.rb +6 -0
  136. data/spec/app/models/passport.rb +5 -0
  137. data/spec/app/models/patient.rb +9 -0
  138. data/spec/app/models/pdf_writer.rb +3 -0
  139. data/spec/app/models/pencil.rb +1 -0
  140. data/spec/app/models/person.rb +201 -0
  141. data/spec/app/models/pet.rb +23 -0
  142. data/spec/app/models/pet_owner.rb +6 -0
  143. data/spec/app/models/phone.rb +11 -0
  144. data/spec/app/models/phone_observer.rb +6 -0
  145. data/spec/app/models/pizza.rb +7 -0
  146. data/spec/app/models/player.rb +35 -0
  147. data/spec/app/models/post.rb +43 -0
  148. data/spec/app/models/powerup.rb +11 -0
  149. data/spec/app/models/preference.rb +9 -0
  150. data/spec/app/models/princess.rb +8 -0
  151. data/spec/app/models/product.rb +17 -0
  152. data/spec/app/models/profile.rb +5 -0
  153. data/spec/app/models/pronunciation.rb +5 -0
  154. data/spec/app/models/purchase.rb +4 -0
  155. data/spec/app/models/question.rb +8 -0
  156. data/spec/app/models/quiz.rb +10 -0
  157. data/spec/app/models/rating.rb +8 -0
  158. data/spec/app/models/record.rb +46 -0
  159. data/spec/app/models/registry.rb +4 -0
  160. data/spec/app/models/role.rb +7 -0
  161. data/spec/app/models/root_category.rb +4 -0
  162. data/spec/app/models/sandwich.rb +4 -0
  163. data/spec/app/models/scheduler.rb +7 -0
  164. data/spec/app/models/seo.rb +7 -0
  165. data/spec/app/models/series.rb +4 -0
  166. data/spec/app/models/server.rb +13 -0
  167. data/spec/app/models/service.rb +22 -0
  168. data/spec/app/models/shape.rb +12 -0
  169. data/spec/app/models/shelf.rb +5 -0
  170. data/spec/app/models/shipping_container.rb +5 -0
  171. data/spec/app/models/shipping_pack.rb +3 -0
  172. data/spec/app/models/shop.rb +6 -0
  173. data/spec/app/models/short_agent.rb +4 -0
  174. data/spec/app/models/short_quiz.rb +5 -0
  175. data/spec/app/models/slave.rb +6 -0
  176. data/spec/app/models/song.rb +8 -0
  177. data/spec/app/models/square.rb +4 -0
  178. data/spec/app/models/strategy.rb +3 -0
  179. data/spec/app/models/sub_item.rb +3 -0
  180. data/spec/app/models/subscription.rb +4 -0
  181. data/spec/app/models/survey.rb +5 -0
  182. data/spec/app/models/symptom.rb +6 -0
  183. data/spec/app/models/tag.rb +8 -0
  184. data/spec/app/models/target.rb +5 -0
  185. data/spec/app/models/template.rb +5 -0
  186. data/spec/app/models/thing.rb +9 -0
  187. data/spec/app/models/title.rb +4 -0
  188. data/spec/app/models/tool.rb +8 -0
  189. data/spec/app/models/topping.rb +5 -0
  190. data/spec/app/models/track.rb +38 -0
  191. data/spec/app/models/translation.rb +5 -0
  192. data/spec/app/models/tree.rb +9 -0
  193. data/spec/app/models/truck.rb +3 -0
  194. data/spec/app/models/user.rb +21 -0
  195. data/spec/app/models/user_account.rb +10 -0
  196. data/spec/app/models/validation_callback.rb +10 -0
  197. data/spec/app/models/vehicle.rb +11 -0
  198. data/spec/app/models/version.rb +5 -0
  199. data/spec/app/models/vet_visit.rb +5 -0
  200. data/spec/app/models/video.rb +17 -0
  201. data/spec/app/models/video_game.rb +1 -0
  202. data/spec/app/models/weapon.rb +11 -0
  203. data/spec/app/models/wiki_page.rb +17 -0
  204. data/spec/app/models/word.rb +12 -0
  205. data/spec/app/models/word_origin.rb +11 -0
  206. data/spec/app/models/writer.rb +11 -0
  207. data/spec/config/mongoid.yml +40 -0
  208. data/spec/mongoid/atomic/modifiers_spec.rb +456 -0
  209. data/spec/mongoid/atomic/paths/embedded/many_spec.rb +159 -0
  210. data/spec/mongoid/atomic/paths/embedded/one_spec.rb +152 -0
  211. data/spec/mongoid/atomic/paths/root_spec.rb +106 -0
  212. data/spec/mongoid/atomic/paths_spec.rb +270 -0
  213. data/spec/mongoid/atomic/positionable_spec.rb +227 -0
  214. data/spec/mongoid/atomic_spec.rb +365 -0
  215. data/spec/mongoid/attributes/processing_spec.rb +149 -0
  216. data/spec/mongoid/attributes/readonly_spec.rb +169 -0
  217. data/spec/mongoid/attributes_spec.rb +1705 -0
  218. data/spec/mongoid/callbacks_spec.rb +1564 -0
  219. data/spec/mongoid/components_spec.rb +24 -0
  220. data/spec/mongoid/config/environment_spec.rb +83 -0
  221. data/spec/mongoid/config/options_spec.rb +56 -0
  222. data/spec/mongoid/config_spec.rb +334 -0
  223. data/spec/mongoid/contextual/aggregable/memory_spec.rb +293 -0
  224. data/spec/mongoid/contextual/aggregable/mongo_spec.rb +455 -0
  225. data/spec/mongoid/contextual/atomic_spec.rb +525 -0
  226. data/spec/mongoid/contextual/find_and_modify_spec.rb +220 -0
  227. data/spec/mongoid/contextual/geo_near_spec.rb +405 -0
  228. data/spec/mongoid/contextual/map_reduce_spec.rb +464 -0
  229. data/spec/mongoid/contextual/memory_spec.rb +1236 -0
  230. data/spec/mongoid/contextual/mongo_spec.rb +1757 -0
  231. data/spec/mongoid/copyable_spec.rb +393 -0
  232. data/spec/mongoid/criteria_spec.rb +5140 -0
  233. data/spec/mongoid/criterion/destructive_spec.rb +101 -0
  234. data/spec/mongoid/criterion/inspection_spec.rb +27 -0
  235. data/spec/mongoid/criterion/marshalable_spec.rb +28 -0
  236. data/spec/mongoid/criterion/modifiable_spec.rb +409 -0
  237. data/spec/mongoid/criterion/modification_spec.rb +402 -0
  238. data/spec/mongoid/criterion/scoping_spec.rb +391 -0
  239. data/spec/mongoid/dirty_spec.rb +1508 -0
  240. data/spec/mongoid/document_spec.rb +1146 -0
  241. data/spec/mongoid/equality_spec.rb +241 -0
  242. data/spec/mongoid/errors/ambiguous_relationship_spec.rb +29 -0
  243. data/spec/mongoid/errors/callback_spec.rb +29 -0
  244. data/spec/mongoid/errors/delete_restriction_spec.rb +29 -0
  245. data/spec/mongoid/errors/document_not_found_spec.rb +104 -0
  246. data/spec/mongoid/errors/eager_load_spec.rb +29 -0
  247. data/spec/mongoid/errors/invalid_collection_spec.rb +36 -0
  248. data/spec/mongoid/errors/invalid_config_option_spec.rb +29 -0
  249. data/spec/mongoid/errors/invalid_field_option_spec.rb +29 -0
  250. data/spec/mongoid/errors/invalid_field_spec.rb +37 -0
  251. data/spec/mongoid/errors/invalid_find_spec.rb +29 -0
  252. data/spec/mongoid/errors/invalid_includes_spec.rb +40 -0
  253. data/spec/mongoid/errors/invalid_index_spec.rb +29 -0
  254. data/spec/mongoid/errors/invalid_options_spec.rb +29 -0
  255. data/spec/mongoid/errors/invalid_path_spec.rb +23 -0
  256. data/spec/mongoid/errors/invalid_scope_spec.rb +29 -0
  257. data/spec/mongoid/errors/invalid_set_polymorphic_relation_spec.rb +17 -0
  258. data/spec/mongoid/errors/invalid_storage_options_spec.rb +29 -0
  259. data/spec/mongoid/errors/invalid_time_spec.rb +29 -0
  260. data/spec/mongoid/errors/inverse_not_found_spec.rb +29 -0
  261. data/spec/mongoid/errors/mixed_relations_spec.rb +29 -0
  262. data/spec/mongoid/errors/mixed_session_configuration_spec.rb +29 -0
  263. data/spec/mongoid/errors/mongoid_error_spec.rb +48 -0
  264. data/spec/mongoid/errors/nested_attributes_metadata_not_found_spec.rb +29 -0
  265. data/spec/mongoid/errors/no_environment_spec.rb +29 -0
  266. data/spec/mongoid/errors/no_map_reduce_output_spec.rb +29 -0
  267. data/spec/mongoid/errors/no_metadata_spec.rb +23 -0
  268. data/spec/mongoid/errors/no_parent_spec.rb +29 -0
  269. data/spec/mongoid/errors/no_session_config_spec.rb +29 -0
  270. data/spec/mongoid/errors/no_session_database_spec.rb +29 -0
  271. data/spec/mongoid/errors/no_session_hosts_spec.rb +29 -0
  272. data/spec/mongoid/errors/no_sessions_config_spec.rb +29 -0
  273. data/spec/mongoid/errors/readonly_attribute_spec.rb +29 -0
  274. data/spec/mongoid/errors/scope_overwrite_spec.rb +29 -0
  275. data/spec/mongoid/errors/too_many_nested_attribute_records_spec.rb +29 -0
  276. data/spec/mongoid/errors/unknown_attribute_spec.rb +29 -0
  277. data/spec/mongoid/errors/unsaved_document_spec.rb +37 -0
  278. data/spec/mongoid/errors/unsupported_javascript_spec.rb +29 -0
  279. data/spec/mongoid/errors/validations_spec.rb +45 -0
  280. data/spec/mongoid/errors/versioning_not_on_root_spec.rb +29 -0
  281. data/spec/mongoid/extensions/array_spec.rb +638 -0
  282. data/spec/mongoid/extensions/big_decimal_spec.rb +104 -0
  283. data/spec/mongoid/extensions/binary_spec.rb +60 -0
  284. data/spec/mongoid/extensions/boolean_spec.rb +135 -0
  285. data/spec/mongoid/extensions/date_spec.rb +235 -0
  286. data/spec/mongoid/extensions/date_time_spec.rb +155 -0
  287. data/spec/mongoid/extensions/false_class_spec.rb +42 -0
  288. data/spec/mongoid/extensions/float_spec.rb +133 -0
  289. data/spec/mongoid/extensions/hash_spec.rb +333 -0
  290. data/spec/mongoid/extensions/integer_spec.rb +136 -0
  291. data/spec/mongoid/extensions/module_spec.rb +42 -0
  292. data/spec/mongoid/extensions/nil_class_spec.rb +11 -0
  293. data/spec/mongoid/extensions/object_id_spec.rb +946 -0
  294. data/spec/mongoid/extensions/object_spec.rb +292 -0
  295. data/spec/mongoid/extensions/range_spec.rb +105 -0
  296. data/spec/mongoid/extensions/regexp_spec.rb +47 -0
  297. data/spec/mongoid/extensions/set_spec.rb +33 -0
  298. data/spec/mongoid/extensions/string_spec.rb +368 -0
  299. data/spec/mongoid/extensions/symbol_spec.rb +76 -0
  300. data/spec/mongoid/extensions/time_spec.rb +467 -0
  301. data/spec/mongoid/extensions/time_with_zone_spec.rb +405 -0
  302. data/spec/mongoid/extensions/true_class_spec.rb +42 -0
  303. data/spec/mongoid/factory_spec.rb +185 -0
  304. data/spec/mongoid/fields/foreign_key_spec.rb +694 -0
  305. data/spec/mongoid/fields/internal/foreign_keys/array_spec.rb +184 -0
  306. data/spec/mongoid/fields/internal/foreign_keys/object_spec.rb +201 -0
  307. data/spec/mongoid/fields/localized_spec.rb +386 -0
  308. data/spec/mongoid/fields/standard_spec.rb +166 -0
  309. data/spec/mongoid/fields_spec.rb +1253 -0
  310. data/spec/mongoid/finders_spec.rb +321 -0
  311. data/spec/mongoid/hierarchy_spec.rb +244 -0
  312. data/spec/mongoid/identity_map_spec.rb +564 -0
  313. data/spec/mongoid/indexes_spec.rb +404 -0
  314. data/spec/mongoid/inspection_spec.rb +57 -0
  315. data/spec/mongoid/json_spec.rb +33 -0
  316. data/spec/mongoid/loggable_spec.rb +21 -0
  317. data/spec/mongoid/matchers/all_spec.rb +31 -0
  318. data/spec/mongoid/matchers/and_spec.rb +162 -0
  319. data/spec/mongoid/matchers/default_spec.rb +130 -0
  320. data/spec/mongoid/matchers/exists_spec.rb +57 -0
  321. data/spec/mongoid/matchers/gt_spec.rb +74 -0
  322. data/spec/mongoid/matchers/gte_spec.rb +74 -0
  323. data/spec/mongoid/matchers/in_spec.rb +25 -0
  324. data/spec/mongoid/matchers/lt_spec.rb +74 -0
  325. data/spec/mongoid/matchers/lte_spec.rb +74 -0
  326. data/spec/mongoid/matchers/ne_spec.rb +25 -0
  327. data/spec/mongoid/matchers/nin_spec.rb +25 -0
  328. data/spec/mongoid/matchers/or_spec.rb +106 -0
  329. data/spec/mongoid/matchers/size_spec.rb +25 -0
  330. data/spec/mongoid/matchers_spec.rb +532 -0
  331. data/spec/mongoid/multi_parameter_attributes_spec.rb +128 -0
  332. data/spec/mongoid/nested_attributes_spec.rb +4945 -0
  333. data/spec/mongoid/observer_spec.rb +290 -0
  334. data/spec/mongoid/paranoia_spec.rb +759 -0
  335. data/spec/mongoid/persistence/atomic/add_to_set_spec.rb +262 -0
  336. data/spec/mongoid/persistence/atomic/bit_spec.rb +88 -0
  337. data/spec/mongoid/persistence/atomic/inc_spec.rb +133 -0
  338. data/spec/mongoid/persistence/atomic/pop_spec.rb +111 -0
  339. data/spec/mongoid/persistence/atomic/pull_all_spec.rb +77 -0
  340. data/spec/mongoid/persistence/atomic/pull_spec.rb +80 -0
  341. data/spec/mongoid/persistence/atomic/push_all_spec.rb +77 -0
  342. data/spec/mongoid/persistence/atomic/push_spec.rb +77 -0
  343. data/spec/mongoid/persistence/atomic/rename_spec.rb +42 -0
  344. data/spec/mongoid/persistence/atomic/sets_spec.rb +154 -0
  345. data/spec/mongoid/persistence/atomic/unset_spec.rb +65 -0
  346. data/spec/mongoid/persistence/atomic_spec.rb +216 -0
  347. data/spec/mongoid/persistence/operations/embedded/insert_spec.rb +191 -0
  348. data/spec/mongoid/persistence/operations/embedded/remove_spec.rb +8 -0
  349. data/spec/mongoid/persistence/operations/insert_spec.rb +149 -0
  350. data/spec/mongoid/persistence/operations/remove_spec.rb +113 -0
  351. data/spec/mongoid/persistence/operations/update_spec.rb +141 -0
  352. data/spec/mongoid/persistence/operations/upsert_spec.rb +59 -0
  353. data/spec/mongoid/persistence/operations_spec.rb +313 -0
  354. data/spec/mongoid/persistence_spec.rb +2279 -0
  355. data/spec/mongoid/railties/document_spec.rb +24 -0
  356. data/spec/mongoid/relations/accessors_spec.rb +844 -0
  357. data/spec/mongoid/relations/auto_save_spec.rb +261 -0
  358. data/spec/mongoid/relations/bindings/embedded/in_spec.rb +171 -0
  359. data/spec/mongoid/relations/bindings/embedded/many_spec.rb +54 -0
  360. data/spec/mongoid/relations/bindings/embedded/one_spec.rb +77 -0
  361. data/spec/mongoid/relations/bindings/referenced/in_spec.rb +241 -0
  362. data/spec/mongoid/relations/bindings/referenced/many_spec.rb +153 -0
  363. data/spec/mongoid/relations/bindings/referenced/many_to_many_spec.rb +178 -0
  364. data/spec/mongoid/relations/bindings/referenced/one_spec.rb +131 -0
  365. data/spec/mongoid/relations/builders/embedded/in_spec.rb +34 -0
  366. data/spec/mongoid/relations/builders/embedded/many_spec.rb +132 -0
  367. data/spec/mongoid/relations/builders/embedded/one_spec.rb +99 -0
  368. data/spec/mongoid/relations/builders/nested_attributes/many_spec.rb +234 -0
  369. data/spec/mongoid/relations/builders/nested_attributes/one_spec.rb +250 -0
  370. data/spec/mongoid/relations/builders/referenced/in_spec.rb +241 -0
  371. data/spec/mongoid/relations/builders/referenced/many_spec.rb +137 -0
  372. data/spec/mongoid/relations/builders/referenced/many_to_many_spec.rb +178 -0
  373. data/spec/mongoid/relations/builders/referenced/one_spec.rb +124 -0
  374. data/spec/mongoid/relations/builders_spec.rb +226 -0
  375. data/spec/mongoid/relations/cascading/delete_spec.rb +101 -0
  376. data/spec/mongoid/relations/cascading/destroy_spec.rb +47 -0
  377. data/spec/mongoid/relations/cascading/nullify_spec.rb +32 -0
  378. data/spec/mongoid/relations/cascading/restrict_spec.rb +68 -0
  379. data/spec/mongoid/relations/cascading_spec.rb +355 -0
  380. data/spec/mongoid/relations/constraint_spec.rb +74 -0
  381. data/spec/mongoid/relations/conversions_spec.rb +126 -0
  382. data/spec/mongoid/relations/counter_cache_spec.rb +205 -0
  383. data/spec/mongoid/relations/cyclic_spec.rb +156 -0
  384. data/spec/mongoid/relations/embedded/dirty_spec.rb +65 -0
  385. data/spec/mongoid/relations/embedded/in_spec.rb +580 -0
  386. data/spec/mongoid/relations/embedded/many_spec.rb +3841 -0
  387. data/spec/mongoid/relations/embedded/one_spec.rb +1055 -0
  388. data/spec/mongoid/relations/macros_spec.rb +625 -0
  389. data/spec/mongoid/relations/metadata_spec.rb +2030 -0
  390. data/spec/mongoid/relations/options_spec.rb +35 -0
  391. data/spec/mongoid/relations/polymorphic_spec.rb +132 -0
  392. data/spec/mongoid/relations/proxy_spec.rb +48 -0
  393. data/spec/mongoid/relations/referenced/in_spec.rb +1501 -0
  394. data/spec/mongoid/relations/referenced/many_spec.rb +3632 -0
  395. data/spec/mongoid/relations/referenced/many_to_many_spec.rb +3561 -0
  396. data/spec/mongoid/relations/referenced/one_spec.rb +1331 -0
  397. data/spec/mongoid/relations/reflections_spec.rb +101 -0
  398. data/spec/mongoid/relations/synchronization_spec.rb +453 -0
  399. data/spec/mongoid/relations/targets/enumerable_spec.rb +1710 -0
  400. data/spec/mongoid/relations_spec.rb +188 -0
  401. data/spec/mongoid/reloading_spec.rb +305 -0
  402. data/spec/mongoid/scoping_spec.rb +978 -0
  403. data/spec/mongoid/serialization_spec.rb +833 -0
  404. data/spec/mongoid/sessions/factory_spec.rb +312 -0
  405. data/spec/mongoid/sessions/mongo_uri_spec.rb +103 -0
  406. data/spec/mongoid/sessions_spec.rb +1111 -0
  407. data/spec/mongoid/sharding_spec.rb +61 -0
  408. data/spec/mongoid/state_spec.rb +102 -0
  409. data/spec/mongoid/threaded_spec.rb +296 -0
  410. data/spec/mongoid/timestamps/created/short_spec.rb +51 -0
  411. data/spec/mongoid/timestamps/created_spec.rb +44 -0
  412. data/spec/mongoid/timestamps/timeless_spec.rb +130 -0
  413. data/spec/mongoid/timestamps/updated/short_spec.rb +90 -0
  414. data/spec/mongoid/timestamps/updated_spec.rb +86 -0
  415. data/spec/mongoid/timestamps_spec.rb +112 -0
  416. data/spec/mongoid/unit_of_work_spec.rb +196 -0
  417. data/spec/mongoid/validations/associated_spec.rb +183 -0
  418. data/spec/mongoid/validations/format_spec.rb +83 -0
  419. data/spec/mongoid/validations/length_spec.rb +223 -0
  420. data/spec/mongoid/validations/numericality_spec.rb +30 -0
  421. data/spec/mongoid/validations/presence_spec.rb +592 -0
  422. data/spec/mongoid/validations/uniqueness_spec.rb +2399 -0
  423. data/spec/mongoid/validations_spec.rb +309 -0
  424. data/spec/mongoid/versioning_spec.rb +540 -0
  425. data/spec/mongoid_spec.rb +74 -0
  426. data/spec/rack/mongoid/middleware/identity_map_spec.rb +72 -0
  427. data/spec/rails/mongoid_spec.rb +462 -0
  428. data/spec/spec_helper.rb +93 -0
  429. metadata +826 -6
@@ -0,0 +1,2030 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Relations::Metadata do
4
+
5
+ describe "#counter_cached?" do
6
+
7
+ context "when counter_cache is false" do
8
+
9
+ let(:metadata) do
10
+ described_class.new(
11
+ name: :car,
12
+ relation: Mongoid::Relations::Referenced::In,
13
+ counter_cache: false
14
+ )
15
+ end
16
+
17
+ it "returns false" do
18
+ metadata.should_not be_counter_cached
19
+ end
20
+ end
21
+
22
+ context "when counter_cache is true" do
23
+
24
+ let(:metadata) do
25
+ described_class.new(
26
+ name: :car,
27
+ relation: Mongoid::Relations::Referenced::In,
28
+ counter_cache: true
29
+ )
30
+ end
31
+
32
+ it "returns true" do
33
+ metadata.should be_counter_cached
34
+ end
35
+ end
36
+
37
+ context "when counter_cache is name for column" do
38
+
39
+ let(:metadata) do
40
+ described_class.new(
41
+ name: :car,
42
+ relation: Mongoid::Relations::Referenced::In,
43
+ counter_cache: 'counter'
44
+ )
45
+ end
46
+
47
+ it "returns true" do
48
+ metadata.should be_counter_cached
49
+ end
50
+ end
51
+
52
+ context "when counter_cache is nil" do
53
+
54
+ let(:metadata) do
55
+ described_class.new(
56
+ name: :car,
57
+ relation: Mongoid::Relations::Referenced::In,
58
+ )
59
+ end
60
+
61
+ it "returns false" do
62
+ metadata.should_not be_counter_cached
63
+ end
64
+ end
65
+ end
66
+
67
+ describe "#counter_cache_column_name" do
68
+
69
+ let(:inverse_class_name) do
70
+ "Wheel"
71
+ end
72
+
73
+ context "when the counter_cache is true" do
74
+
75
+ let(:metadata) do
76
+ described_class.new(
77
+ name: :car,
78
+ relation: Mongoid::Relations::Referenced::In,
79
+ inverse_class_name: inverse_class_name,
80
+ counter_cache: true
81
+ )
82
+ end
83
+
84
+ before do
85
+ metadata.should_receive(:inverse).and_return(:wheels)
86
+ end
87
+
88
+ it "returns inverse name + _count" do
89
+ expect(metadata.counter_cache_column_name).to eq(
90
+ "#{inverse_class_name.demodulize.underscore.pluralize}_count"
91
+ )
92
+ end
93
+ end
94
+
95
+ context "when given a custom name for counter cache" do
96
+ let(:counter_cache_name) { 'counte_cache_for_wheels' }
97
+ let(:metadata) do
98
+ described_class.new(
99
+ name: :car,
100
+ relation: Mongoid::Relations::Referenced::In,
101
+ inverse_class_name: inverse_class_name,
102
+ counter_cache: counter_cache_name
103
+ )
104
+ end
105
+
106
+ it "returns the name given" do
107
+ metadata.counter_cache_column_name.should eq(counter_cache_name)
108
+ end
109
+ end
110
+ end
111
+
112
+ describe "#autobuilding?" do
113
+
114
+ context "when the option is true" do
115
+
116
+ let(:metadata) do
117
+ described_class.new(
118
+ relation: Mongoid::Relations::Embedded::One,
119
+ autobuild: true
120
+ )
121
+ end
122
+
123
+ it "returns true" do
124
+ metadata.should be_autobuilding
125
+ end
126
+ end
127
+
128
+ context "when the option is false" do
129
+
130
+ let(:metadata) do
131
+ described_class.new(
132
+ relation: Mongoid::Relations::Embedded::One,
133
+ autobuild: false
134
+ )
135
+ end
136
+
137
+ it "returns false" do
138
+ metadata.should_not be_autobuilding
139
+ end
140
+ end
141
+
142
+ context "when the option is nil" do
143
+
144
+ let(:metadata) do
145
+ described_class.new(
146
+ relation: Mongoid::Relations::Embedded::One
147
+ )
148
+ end
149
+
150
+ it "returns false" do
151
+ metadata.should_not be_autobuilding
152
+ end
153
+ end
154
+ end
155
+
156
+ describe "#builder" do
157
+
158
+ let(:metadata) do
159
+ described_class.new(relation: Mongoid::Relations::Embedded::One)
160
+ end
161
+
162
+ let(:object) do
163
+ stub
164
+ end
165
+
166
+ let(:base) do
167
+ stub
168
+ end
169
+
170
+ it "returns the builder from the relation" do
171
+ metadata.builder(base, object).should
172
+ be_a_kind_of(Mongoid::Relations::Builders::Embedded::One)
173
+ end
174
+ end
175
+
176
+ describe "#cascading_callbacks?" do
177
+
178
+ context "when the option is true" do
179
+
180
+ let(:metadata) do
181
+ described_class.new(
182
+ relation: Mongoid::Relations::Embedded::Many,
183
+ cascade_callbacks: true
184
+ )
185
+ end
186
+
187
+ it "returns true" do
188
+ metadata.should be_cascading_callbacks
189
+ end
190
+ end
191
+
192
+ context "when the option is false" do
193
+
194
+ let(:metadata) do
195
+ described_class.new(
196
+ relation: Mongoid::Relations::Embedded::Many,
197
+ cascade_callbacks: false
198
+ )
199
+ end
200
+
201
+ it "returns false" do
202
+ metadata.should_not be_cascading_callbacks
203
+ end
204
+ end
205
+
206
+ context "when the option is nil" do
207
+
208
+ let(:metadata) do
209
+ described_class.new(
210
+ relation: Mongoid::Relations::Embedded::Many
211
+ )
212
+ end
213
+
214
+ it "returns false" do
215
+ metadata.should_not be_cascading_callbacks
216
+ end
217
+ end
218
+ end
219
+
220
+ describe "#cascade_strategy" do
221
+
222
+ context "when no dependent option is set" do
223
+
224
+ let(:metadata) do
225
+ described_class.new(
226
+ name: :posts,
227
+ relation: Mongoid::Relations::Referenced::Many
228
+ )
229
+ end
230
+
231
+ it "returns nil" do
232
+ metadata.cascade_strategy.should be_nil
233
+ end
234
+ end
235
+
236
+ context "when dependent is delete" do
237
+
238
+ let(:metadata) do
239
+ described_class.new(
240
+ name: :posts,
241
+ relation: Mongoid::Relations::Referenced::Many,
242
+ dependent: :delete
243
+ )
244
+ end
245
+
246
+ it "returns the delete strategy" do
247
+ metadata.cascade_strategy.should eq(
248
+ Mongoid::Relations::Cascading::Delete
249
+ )
250
+ end
251
+ end
252
+
253
+ context "when dependent is destroy" do
254
+
255
+ let(:metadata) do
256
+ described_class.new(
257
+ name: :posts,
258
+ relation: Mongoid::Relations::Referenced::Many,
259
+ dependent: :destroy
260
+ )
261
+ end
262
+
263
+ it "returns the destroy strategy" do
264
+ metadata.cascade_strategy.should eq(
265
+ Mongoid::Relations::Cascading::Destroy
266
+ )
267
+ end
268
+ end
269
+
270
+ context "when dependent is nullify" do
271
+
272
+ let(:metadata) do
273
+ described_class.new(
274
+ name: :posts,
275
+ relation: Mongoid::Relations::Referenced::Many,
276
+ dependent: :nullify
277
+ )
278
+ end
279
+
280
+ it "returns the nullify strategy" do
281
+ metadata.cascade_strategy.should eq(
282
+ Mongoid::Relations::Cascading::Nullify
283
+ )
284
+ end
285
+ end
286
+ end
287
+
288
+ describe "#constraint" do
289
+
290
+ let(:metadata) do
291
+ described_class.new(
292
+ relation: Mongoid::Relations::Referenced::Many,
293
+ class_name: "Person"
294
+ )
295
+ end
296
+
297
+ it "returns the constraint object" do
298
+ metadata.constraint.should be_a(Mongoid::Relations::Constraint)
299
+ end
300
+ end
301
+
302
+ describe "#classify" do
303
+
304
+ let(:name) do
305
+ "name"
306
+ end
307
+
308
+ let(:metadata) do
309
+ described_class.new(name: name)
310
+ end
311
+
312
+ let(:classified) do
313
+ metadata.send(:classify)
314
+ end
315
+
316
+ before do
317
+ Mongoid::Relations::Options.should_receive(:validate!).at_least(:once)
318
+ end
319
+
320
+ it "concatenates the result from #find_module and name.classify" do
321
+ metadata.should_receive(:find_module).once.and_return("Fruit")
322
+ classified.should eq("Fruit::Name")
323
+ end
324
+ end
325
+
326
+ describe "#find_module" do
327
+
328
+ let(:name) do
329
+ "name"
330
+ end
331
+
332
+ let(:metadata) do
333
+ described_class.new(name: name, inverse_class_name: inverse_class_name)
334
+ end
335
+
336
+ let(:mod) do
337
+ metadata.send(:find_module)
338
+ end
339
+
340
+ before do
341
+ Mongoid::Relations::Options.should_receive(:validate!).at_least(:once)
342
+ end
343
+
344
+ context "when inverse_class_name is nil" do
345
+
346
+ let(:inverse_class_name) do
347
+ nil
348
+ end
349
+
350
+ it "returns nil" do
351
+ mod.should be_nil
352
+ end
353
+ end
354
+
355
+ context "when inverse_class_name is empty" do
356
+
357
+ let(:inverse_class_name) do
358
+ ""
359
+ end
360
+
361
+ it "returns nil" do
362
+ mod.should be_nil
363
+ end
364
+ end
365
+
366
+ context "when inverse_class_name is defined" do
367
+
368
+ context "when inverse_class_name is in root namespace" do
369
+
370
+ let(:inverse_class_name) do
371
+ "Person"
372
+ end
373
+
374
+ context "when name isn't defined" do
375
+
376
+ let(:name) do
377
+ "undefined"
378
+ end
379
+
380
+ it "returns nil" do
381
+ mod.should be_nil
382
+ end
383
+ end
384
+
385
+ context "when name is defined in root namespace" do
386
+
387
+ let(:name) do
388
+ "account"
389
+ end
390
+
391
+ it "returns root namespace" do
392
+ mod.should be_empty
393
+ end
394
+ end
395
+
396
+ context "when name is defined in module Fruits" do
397
+
398
+ context "when inverse_class_name is defined in the same module" do
399
+
400
+ let(:inverse_class_name) do
401
+ "Fruits::Apple"
402
+ end
403
+
404
+ let(:name) do
405
+ "banana"
406
+ end
407
+
408
+ it "returns Fruits" do
409
+ mod.should eq("Fruits")
410
+ end
411
+ end
412
+
413
+ context "when inverse_class_name is defined in a nested module" do
414
+
415
+ let(:inverse_class_name) do
416
+ "Fruits::Big::Ananas"
417
+ end
418
+
419
+ let(:name) do
420
+ "banana"
421
+ end
422
+
423
+ it "returns Fruits" do
424
+ mod.should eq("Fruits")
425
+ end
426
+ end
427
+
428
+ context "when the inverse_class_name is defined in the root namespace" do
429
+
430
+ let(:inverse_class_name) do
431
+ "Person"
432
+ end
433
+
434
+ let(:name) do
435
+ "banana"
436
+ end
437
+
438
+ it "returns nil" do
439
+ mod.should be_nil
440
+ end
441
+ end
442
+ end
443
+ end
444
+ end
445
+ end
446
+
447
+ describe "#class_name" do
448
+
449
+ context "when class_name provided" do
450
+
451
+ context "when the class name contains leading ::" do
452
+
453
+ let(:metadata) do
454
+ described_class.new(
455
+ relation: Mongoid::Relations::Referenced::Many,
456
+ class_name: "::Person"
457
+ )
458
+ end
459
+
460
+ it "returns the stripped class name" do
461
+ metadata.class_name.should eq("Person")
462
+ end
463
+ end
464
+
465
+ context "when the class name has no prefix" do
466
+
467
+ let(:metadata) do
468
+ described_class.new(
469
+ relation: Mongoid::Relations::Referenced::Many,
470
+ class_name: "Person"
471
+ )
472
+ end
473
+
474
+ it "constantizes the class name" do
475
+ metadata.class_name.should eq("Person")
476
+ end
477
+ end
478
+ end
479
+
480
+ context "when no class_name provided" do
481
+
482
+ context "when inverse_class_name is provided" do
483
+
484
+ context "when inverse_class_name is a simple class name" do
485
+
486
+ context "when association name is singular" do
487
+
488
+ let(:relation) do
489
+ Mongoid::Relations::Embedded::One
490
+ end
491
+
492
+ let(:metadata) do
493
+ described_class.new(name: :name, relation: relation, inverse_class_name: "Person")
494
+ end
495
+
496
+ it "classifies and constantizes the association name and adds the module" do
497
+ metadata.class_name.should eq("Name")
498
+ end
499
+ end
500
+
501
+ context "when association name is plural" do
502
+
503
+ let(:relation) do
504
+ Mongoid::Relations::Embedded::Many
505
+ end
506
+
507
+ let(:metadata) do
508
+ described_class.new(name: :addresses, relation: relation, inverse_class_name: "Person")
509
+ end
510
+
511
+ it "classifies and constantizes the association name and adds the module" do
512
+ metadata.class_name.should eq("Address")
513
+ end
514
+ end
515
+
516
+ end
517
+
518
+ context "when inverse_class_name is a class name in a module" do
519
+
520
+ context "when association name is singular" do
521
+
522
+ let(:relation) do
523
+ Mongoid::Relations::Embedded::One
524
+ end
525
+
526
+ let(:metadata) do
527
+ described_class.new(name: :apple, relation: relation, inverse_class_name: "Fruits::Banana")
528
+ end
529
+
530
+ it "classifies and constantizes the association name and adds the module" do
531
+ metadata.class_name.should eq("Fruits::Apple")
532
+ end
533
+ end
534
+
535
+ context "when association name is plural" do
536
+
537
+ let(:relation) do
538
+ Mongoid::Relations::Embedded::Many
539
+ end
540
+
541
+ let(:metadata) do
542
+ described_class.new(name: :apples, relation: relation, inverse_class_name: "Fruits::Banana")
543
+ end
544
+
545
+ it "classifies and constantizes the association name and adds the module" do
546
+ metadata.class_name.should eq("Fruits::Apple")
547
+ end
548
+ end
549
+
550
+ end
551
+ end
552
+
553
+ context "when no inverse_class_name is provided" do
554
+
555
+ context "when association name is singular" do
556
+
557
+ let(:relation) do
558
+ Mongoid::Relations::Embedded::One
559
+ end
560
+
561
+ let(:metadata) do
562
+ described_class.new(name: :name, relation: relation)
563
+ end
564
+
565
+ it "classifies and constantizes the association name" do
566
+ metadata.class_name.should eq("Name")
567
+ end
568
+ end
569
+
570
+ context "when association name is plural" do
571
+
572
+ let(:relation) do
573
+ Mongoid::Relations::Embedded::Many
574
+ end
575
+
576
+ let(:metadata) do
577
+ described_class.new(name: :addresses, relation: relation)
578
+ end
579
+
580
+ it "classifies and constantizes the association name" do
581
+ metadata.class_name.should eq("Address")
582
+ end
583
+ end
584
+ end
585
+ end
586
+ end
587
+
588
+ describe "#destructive?" do
589
+
590
+ context "when the relation has a destructive dependent option" do
591
+
592
+ let(:metadata) do
593
+ described_class.new(
594
+ relation: Mongoid::Relations::Referenced::Many,
595
+ dependent: :destroy
596
+ )
597
+ end
598
+
599
+ it "returns true" do
600
+ metadata.should be_destructive
601
+ end
602
+ end
603
+
604
+ context "when no dependent option" do
605
+
606
+ let(:metadata) do
607
+ described_class.new(
608
+ relation: Mongoid::Relations::Referenced::Many
609
+ )
610
+ end
611
+
612
+ it "returns false" do
613
+ metadata.should_not be_destructive
614
+ end
615
+ end
616
+ end
617
+
618
+ describe "#embedded?" do
619
+
620
+ context "when the relation is embedded" do
621
+
622
+ let(:metadata) do
623
+ described_class.new(
624
+ relation: Mongoid::Relations::Embedded::Many
625
+ )
626
+ end
627
+
628
+ it "returns true" do
629
+ metadata.should be_embedded
630
+ end
631
+ end
632
+
633
+ context "when the relation is not embedded" do
634
+
635
+ let(:metadata) do
636
+ described_class.new(
637
+ relation: Mongoid::Relations::Referenced::Many
638
+ )
639
+ end
640
+
641
+ it "returns false" do
642
+ metadata.should_not be_embedded
643
+ end
644
+ end
645
+ end
646
+
647
+ describe "#extension" do
648
+
649
+ let(:metadata) do
650
+ described_class.new(
651
+ relation: Mongoid::Relations::Referenced::Many,
652
+ extend: :value
653
+ )
654
+ end
655
+
656
+ it "returns the extend property" do
657
+ metadata.extension.should eq(:value)
658
+ end
659
+ end
660
+
661
+ describe "#extension?" do
662
+
663
+ context "when an extends property exists" do
664
+
665
+ let(:metadata) do
666
+ described_class.new(
667
+ relation: Mongoid::Relations::Referenced::Many,
668
+ extend: :value
669
+ )
670
+ end
671
+
672
+ it "returns true" do
673
+ metadata.extension?.should be_true
674
+ end
675
+ end
676
+
677
+ context "when the extend option is nil" do
678
+
679
+ let(:metadata) do
680
+ described_class.new(
681
+ relation: Mongoid::Relations::Referenced::Many
682
+ )
683
+ end
684
+
685
+ it "returns false" do
686
+ metadata.extension?.should be_false
687
+ end
688
+ end
689
+ end
690
+
691
+ describe "#foreign_key" do
692
+
693
+ context "when no foreign key was explicitly defined" do
694
+
695
+ context "when the relation stores a foreign key" do
696
+
697
+ context "when referenced in" do
698
+
699
+ context "when not providing a class name" do
700
+
701
+ let(:metadata) do
702
+ described_class.new(
703
+ name: :person,
704
+ relation: Mongoid::Relations::Referenced::In
705
+ )
706
+ end
707
+
708
+ it "returns the foreign_key" do
709
+ metadata.foreign_key.should eq("person_id")
710
+ end
711
+ end
712
+
713
+ context "when providing a class name" do
714
+
715
+ let(:metadata) do
716
+ described_class.new(
717
+ name: :person,
718
+ relation: Mongoid::Relations::Referenced::In,
719
+ class_name: "TheseAreNotTheDriods"
720
+ )
721
+ end
722
+
723
+ it "returns the foreign_key" do
724
+ metadata.foreign_key.should eq("person_id")
725
+ end
726
+ end
727
+
728
+ context "when the class is namespaces" do
729
+
730
+ let(:metadata) do
731
+ described_class.new(
732
+ name: :apple,
733
+ relation: Mongoid::Relations::Referenced::In,
734
+ class_name: "Fruits::Apple"
735
+ )
736
+ end
737
+
738
+ it "returns the foreign_key without the module name" do
739
+ metadata.foreign_key.should eq("apple_id")
740
+ end
741
+ end
742
+ end
743
+
744
+ context "when references and referenced in many" do
745
+
746
+ let(:metadata) do
747
+ described_class.new(
748
+ name: :people,
749
+ relation: Mongoid::Relations::Referenced::ManyToMany
750
+ )
751
+ end
752
+
753
+ it "returns the foreign_key" do
754
+ metadata.foreign_key.should eq("person_ids")
755
+ end
756
+
757
+ context "given a specific foreign key" do
758
+
759
+ let(:metadata) do
760
+ described_class.new(
761
+ name: :follower,
762
+ foreign_key: :follower_list,
763
+ relation: Mongoid::Relations::Referenced::ManyToMany
764
+ )
765
+ end
766
+
767
+ it "returns the foreign_key" do
768
+ metadata.foreign_key.should eq("follower_list")
769
+ end
770
+ end
771
+
772
+ context "using name as foreign key" do
773
+
774
+ let(:metadata) do
775
+ described_class.new(
776
+ name: :followers,
777
+ class_name: "Person",
778
+ relation: Mongoid::Relations::Referenced::ManyToMany
779
+ )
780
+ end
781
+
782
+ it "returns the foreign_key" do
783
+ metadata.foreign_key.should eq("follower_ids")
784
+ end
785
+ end
786
+
787
+ context "when the class is namespaced" do
788
+
789
+ let(:metadata) do
790
+ described_class.new(
791
+ name: :bananas,
792
+ relation: Mongoid::Relations::Referenced::ManyToMany,
793
+ inverse_class_name: "Fruits::Apple",
794
+ class_name: "Fruits::Banana"
795
+ )
796
+ end
797
+
798
+ it "returns the foreign_key without the module name" do
799
+ metadata.foreign_key.should eq("banana_ids")
800
+ end
801
+
802
+ it "returns the inverse_foreign_key without the module name" do
803
+ metadata.inverse_foreign_key.should eq("apple_ids")
804
+ end
805
+ end
806
+ end
807
+ end
808
+
809
+ context "when the relation does not store a foreign key" do
810
+
811
+ context "when references one" do
812
+
813
+ let(:metadata) do
814
+ described_class.new(
815
+ name: :post,
816
+ relation: Mongoid::Relations::Referenced::One,
817
+ inverse_class_name: "Person"
818
+ )
819
+ end
820
+
821
+ it "returns the inverse foreign key" do
822
+ metadata.foreign_key.should eq("person_id")
823
+ end
824
+ end
825
+
826
+ context "when references many" do
827
+
828
+ context "when an inverse_of is defined" do
829
+
830
+ let(:metadata) do
831
+ described_class.new(
832
+ name: :created_streets,
833
+ relation: Mongoid::Relations::Referenced::Many,
834
+ inverse_class_name: "House",
835
+ inverse_of: :creator
836
+ )
837
+ end
838
+
839
+ it "returns the inverse_of plus suffix" do
840
+ metadata.foreign_key.should eq("creator_id")
841
+ end
842
+ end
843
+
844
+ context "when the class is not namespaced" do
845
+
846
+ let(:metadata) do
847
+ described_class.new(
848
+ name: :posts,
849
+ relation: Mongoid::Relations::Referenced::Many,
850
+ inverse_class_name: "Person"
851
+ )
852
+ end
853
+
854
+ it "returns the inverse foreign key" do
855
+ metadata.foreign_key.should eq("person_id")
856
+ end
857
+ end
858
+
859
+ context "when the class is namespaced" do
860
+
861
+ let(:metadata) do
862
+ described_class.new(
863
+ name: :bananas,
864
+ relation: Mongoid::Relations::Referenced::Many,
865
+ inverse_class_name: "Fruits::Apple",
866
+ class_name: "Fruits::Banana"
867
+ )
868
+ end
869
+
870
+ it "returns the foreign_key without the module name" do
871
+ metadata.foreign_key.should eq("apple_id")
872
+ end
873
+ end
874
+ end
875
+
876
+ context "when embeds one" do
877
+ let(:metadata) do
878
+ described_class.new(
879
+ name: :post,
880
+ relation: Mongoid::Relations::Embedded::One,
881
+ inverse_class_name: "Person"
882
+ )
883
+ end
884
+
885
+ it "returns the inverse foreign key" do
886
+ metadata.foreign_key.should eq("person_id")
887
+ end
888
+ end
889
+ end
890
+ end
891
+
892
+ context "when a foreign_key was defined" do
893
+
894
+ let(:metadata) do
895
+ described_class.new(
896
+ name: :person,
897
+ relation: Mongoid::Relations::Referenced::ManyToMany,
898
+ foreign_key: "blog_post_id"
899
+ )
900
+ end
901
+
902
+ it "returns the foreign_key" do
903
+ metadata.foreign_key.should eq("blog_post_id")
904
+ end
905
+ end
906
+ end
907
+
908
+ describe "#foreign_key_default" do
909
+
910
+ context "when the relation stores keys in an array" do
911
+
912
+ let(:metadata) do
913
+ described_class.new(
914
+ name: :preferences,
915
+ relation: Mongoid::Relations::Referenced::ManyToMany,
916
+ foreign_key: "preference_ids"
917
+ )
918
+ end
919
+
920
+ it "returns an empty array" do
921
+ metadata.foreign_key_default.should be_empty
922
+ end
923
+ end
924
+
925
+ context "when the relation stores a single key" do
926
+
927
+ let(:metadata) do
928
+ described_class.new(
929
+ name: :person,
930
+ relation: Mongoid::Relations::Referenced::In,
931
+ foreign_key: "person_id"
932
+ )
933
+ end
934
+
935
+ it "returns an empty array" do
936
+ metadata.foreign_key_default.should be_nil
937
+ end
938
+ end
939
+ end
940
+
941
+ describe "#foreign_key_setter" do
942
+
943
+ context "when the relation is not polymorphic" do
944
+
945
+ let(:metadata) do
946
+ described_class.new(
947
+ name: :person,
948
+ relation: Mongoid::Relations::Referenced::In,
949
+ foreign_key: "person_id"
950
+ )
951
+ end
952
+
953
+ it "returns the foreign_key plus =" do
954
+ metadata.foreign_key_setter.should eq("person_id=")
955
+ end
956
+ end
957
+
958
+ context "when the relation is polymorphic" do
959
+
960
+ let(:metadata) do
961
+ described_class.new(
962
+ name: :ratings,
963
+ relation: Mongoid::Relations::Referenced::Many,
964
+ as: :ratable,
965
+ inverse_class_name: "Movie"
966
+ )
967
+ end
968
+
969
+ it "returns the polymorphic foreign_key plus =" do
970
+ metadata.foreign_key_setter.should eq("ratable_id=")
971
+ end
972
+ end
973
+ end
974
+
975
+ describe "#inverse_type" do
976
+
977
+ context "when the relation is not polymorphic" do
978
+
979
+ let(:metadata) do
980
+ described_class.new(
981
+ name: :person,
982
+ relation: Mongoid::Relations::Referenced::In,
983
+ foreign_key: "person_id"
984
+ )
985
+ end
986
+
987
+ it "returns nil" do
988
+ metadata.inverse_type.should be_nil
989
+ end
990
+ end
991
+
992
+ context "when the relation is polymorphic" do
993
+
994
+ let(:metadata) do
995
+ described_class.new(
996
+ name: :ratable,
997
+ relation: Mongoid::Relations::Referenced::In,
998
+ polymorphic: true,
999
+ inverse_class_name: "Rating"
1000
+ )
1001
+ end
1002
+
1003
+ it "returns the polymorphic name plus type" do
1004
+ metadata.inverse_type.should eq("ratable_type")
1005
+ end
1006
+ end
1007
+ end
1008
+
1009
+ describe "#inverse_type_setter" do
1010
+
1011
+ context "when the relation is not polymorphic" do
1012
+
1013
+ let(:metadata) do
1014
+ described_class.new(
1015
+ name: :person,
1016
+ relation: Mongoid::Relations::Referenced::In,
1017
+ foreign_key: "person_id"
1018
+ )
1019
+ end
1020
+
1021
+ it "returns nil" do
1022
+ metadata.inverse_type_setter.should be_nil
1023
+ end
1024
+ end
1025
+
1026
+ context "when the relation is polymorphic" do
1027
+
1028
+ let(:metadata) do
1029
+ described_class.new(
1030
+ name: :ratable,
1031
+ relation: Mongoid::Relations::Referenced::In,
1032
+ polymorphic: true,
1033
+ inverse_class_name: "Rating"
1034
+ )
1035
+ end
1036
+
1037
+ it "returns the inverse type plus =" do
1038
+ metadata.inverse_type_setter.should eq("ratable_type=")
1039
+ end
1040
+ end
1041
+ end
1042
+
1043
+ describe "#inverse_of_field" do
1044
+
1045
+ context "when the relation is not polymorphic" do
1046
+
1047
+ let(:metadata) do
1048
+ described_class.new(
1049
+ name: :person,
1050
+ relation: Mongoid::Relations::Referenced::In,
1051
+ foreign_key: "person_id"
1052
+ )
1053
+ end
1054
+
1055
+ it "returns nil" do
1056
+ metadata.inverse_of_field.should be_nil
1057
+ end
1058
+ end
1059
+
1060
+ context "when the relation is polymorphic" do
1061
+
1062
+ let(:metadata) do
1063
+ described_class.new(
1064
+ name: :ratable,
1065
+ relation: Mongoid::Relations::Referenced::In,
1066
+ polymorphic: true,
1067
+ inverse_class_name: "Rating"
1068
+ )
1069
+ end
1070
+
1071
+ it "returns the polymorphic name plus field" do
1072
+ metadata.inverse_of_field.should eq("ratable_field")
1073
+ end
1074
+ end
1075
+ end
1076
+
1077
+ describe "#inverse_of_field_setter" do
1078
+
1079
+ context "when the relation is not polymorphic" do
1080
+
1081
+ let(:metadata) do
1082
+ described_class.new(
1083
+ name: :person,
1084
+ relation: Mongoid::Relations::Referenced::In,
1085
+ foreign_key: "person_id"
1086
+ )
1087
+ end
1088
+
1089
+ it "returns nil" do
1090
+ metadata.inverse_of_field_setter.should be_nil
1091
+ end
1092
+ end
1093
+
1094
+ context "when the relation is polymorphic" do
1095
+
1096
+ let(:metadata) do
1097
+ described_class.new(
1098
+ name: :ratable,
1099
+ relation: Mongoid::Relations::Referenced::In,
1100
+ polymorphic: true,
1101
+ inverse_class_name: "Rating"
1102
+ )
1103
+ end
1104
+
1105
+ it "returns the inverse of field plus =" do
1106
+ metadata.inverse_of_field_setter.should eq("ratable_field=")
1107
+ end
1108
+ end
1109
+ end
1110
+
1111
+ describe "#inverses" do
1112
+
1113
+ context "when the relation is polymorphic" do
1114
+
1115
+ context "when an inverse_of is defined" do
1116
+
1117
+ let(:metadata) do
1118
+ described_class.new(
1119
+ name: :ratable,
1120
+ relation: Mongoid::Relations::Referenced::In,
1121
+ polymorphic: true,
1122
+ inverse_of: "my_ratings"
1123
+ )
1124
+ end
1125
+
1126
+ it "returns only the inverse_of" do
1127
+ metadata.inverses(nil).should eq([ "my_ratings" ])
1128
+ end
1129
+ end
1130
+ end
1131
+ end
1132
+
1133
+ describe "#indexed?" do
1134
+
1135
+ context "when an index property exists" do
1136
+
1137
+ let(:metadata) do
1138
+ described_class.new(
1139
+ index: true,
1140
+ relation: Mongoid::Relations::Referenced::In
1141
+ )
1142
+ end
1143
+
1144
+ it "returns true" do
1145
+ metadata.indexed?.should be_true
1146
+ end
1147
+ end
1148
+
1149
+ context "when the index option is nil" do
1150
+
1151
+ let(:metadata) do
1152
+ described_class.new(
1153
+ relation: Mongoid::Relations::Referenced::In
1154
+ )
1155
+ end
1156
+
1157
+ it "returns false" do
1158
+ metadata.indexed?.should be_false
1159
+ end
1160
+ end
1161
+
1162
+ context "when the index option is false" do
1163
+
1164
+ let(:metadata) do
1165
+ described_class.new(
1166
+ index: false,
1167
+ relation: Mongoid::Relations::Referenced::In
1168
+ )
1169
+ end
1170
+
1171
+ it "returns false" do
1172
+ metadata.indexed?.should be_false
1173
+ end
1174
+ end
1175
+ end
1176
+
1177
+ describe "#inverse" do
1178
+
1179
+ context "when an inverse relation exists" do
1180
+
1181
+ context "when the inverse has a foreign key match" do
1182
+
1183
+ let(:metadata) do
1184
+ User.reflect_on_association(:account)
1185
+ end
1186
+
1187
+ it "returns the name of the inverse with the matching foreign_key" do
1188
+ metadata.inverse.should eq(:creator)
1189
+ end
1190
+ end
1191
+
1192
+ context "when multiple relations against the same class exist" do
1193
+
1194
+ context "when not polymorphic" do
1195
+
1196
+ let(:metadata) do
1197
+ described_class.new(
1198
+ inverse_class_name: "User",
1199
+ name: :shop,
1200
+ relation: Mongoid::Relations::Referenced::One
1201
+ )
1202
+ end
1203
+
1204
+ it "returns the name of the inverse with the matching inverse of" do
1205
+ metadata.inverse.should eq(:user)
1206
+ end
1207
+ end
1208
+
1209
+ context "when polymorphic" do
1210
+
1211
+ let(:metadata) do
1212
+ described_class.new(
1213
+ name: :left_eye,
1214
+ as: :eyeable,
1215
+ inverse_class_name: "Face",
1216
+ class_name: "Eye",
1217
+ relation: Mongoid::Relations::Referenced::One
1218
+ )
1219
+ end
1220
+
1221
+ it "returns the name of the relation" do
1222
+ metadata.inverse.should eq(:eyeable)
1223
+ end
1224
+ end
1225
+
1226
+ context "when polymorphic on the child" do
1227
+
1228
+ let(:metadata) do
1229
+ described_class.new(
1230
+ name: :eyeable,
1231
+ polymorphic: true,
1232
+ inverse_class_name: "Eye",
1233
+ relation: Mongoid::Relations::Referenced::In
1234
+ )
1235
+ end
1236
+
1237
+ it "returns nil" do
1238
+ metadata.inverse(Face.new).should be_nil
1239
+ end
1240
+ end
1241
+ end
1242
+
1243
+ context "when inverse_of is defined" do
1244
+
1245
+ context "when inverse_of is nil" do
1246
+
1247
+ let(:metadata) do
1248
+ described_class.new(
1249
+ inverse_of: nil,
1250
+ relation: Mongoid::Relations::Referenced::In
1251
+ )
1252
+ end
1253
+
1254
+ it "returns nil" do
1255
+ metadata.inverse.should be_nil
1256
+ end
1257
+ end
1258
+
1259
+ context "when inverse_of is a symbol" do
1260
+
1261
+ let(:metadata) do
1262
+ described_class.new(
1263
+ inverse_of: :crazy_name,
1264
+ relation: Mongoid::Relations::Referenced::In
1265
+ )
1266
+ end
1267
+
1268
+ it "returns the name of the inverse_of property" do
1269
+ metadata.inverse.should eq(:crazy_name)
1270
+ end
1271
+ end
1272
+ end
1273
+
1274
+ context "when not polymorphic" do
1275
+
1276
+ let(:metadata) do
1277
+ described_class.new(
1278
+ name: :pet,
1279
+ class_name: "Animal",
1280
+ inverse_class_name: "Person",
1281
+ relation: Mongoid::Relations::Referenced::In
1282
+ )
1283
+ end
1284
+
1285
+ it "returns the name of the relation" do
1286
+ metadata.inverse.should eq(:person)
1287
+ end
1288
+ end
1289
+
1290
+ context "when polymorphic" do
1291
+
1292
+ let(:metadata) do
1293
+ described_class.new(
1294
+ name: :addresses,
1295
+ as: :addressable,
1296
+ inverse_class_name: "Person",
1297
+ relation: Mongoid::Relations::Referenced::Many
1298
+ )
1299
+ end
1300
+
1301
+ it "returns the name of the relation" do
1302
+ metadata.inverse.should eq(:addressable)
1303
+ end
1304
+ end
1305
+
1306
+ context "when polymorphic on the child" do
1307
+
1308
+ let(:metadata) do
1309
+ described_class.new(
1310
+ name: :addressable,
1311
+ polymorphic: true,
1312
+ inverse_class_name: "Address",
1313
+ relation: Mongoid::Relations::Referenced::In
1314
+ )
1315
+ end
1316
+
1317
+ it "returns the name of the relation" do
1318
+ metadata.inverse(Person.new).should eq(:addresses)
1319
+ end
1320
+ end
1321
+
1322
+ context "when in a cyclic relation" do
1323
+
1324
+ context "when the base name is included in the plural form" do
1325
+
1326
+ let(:metadata) do
1327
+ described_class.new(
1328
+ name: :parent_role,
1329
+ class_name: "Role",
1330
+ inverse_class_name: "Role",
1331
+ relation: Mongoid::Relations::Embedded::In,
1332
+ cyclic: true
1333
+ )
1334
+ end
1335
+
1336
+ it "returns the name of the relation" do
1337
+ metadata.inverse(Role.new).should eq(:child_roles)
1338
+ end
1339
+ end
1340
+
1341
+ context "when the base name is not included in the plural form" do
1342
+
1343
+ let(:metadata) do
1344
+ described_class.new(
1345
+ name: :parent_entry,
1346
+ class_name: "Entry",
1347
+ inverse_class_name: "Entry",
1348
+ relation: Mongoid::Relations::Embedded::In,
1349
+ cyclic: true
1350
+ )
1351
+ end
1352
+
1353
+ it "returns the name of the relation" do
1354
+ metadata.inverse(Entry.new).should eq(:child_entries)
1355
+ end
1356
+ end
1357
+ end
1358
+ end
1359
+ end
1360
+
1361
+ context "#inverse_foreign_key" do
1362
+
1363
+ context "when the inverse foreign key is not defined" do
1364
+
1365
+ let(:metadata) do
1366
+ described_class.new(
1367
+ name: :preferences,
1368
+ index: true,
1369
+ dependent: :nullify,
1370
+ relation: Mongoid::Relations::Referenced::ManyToMany,
1371
+ inverse_class_name: "Person"
1372
+ )
1373
+ end
1374
+
1375
+ it "returns the inverse class name plus suffix" do
1376
+ metadata.inverse_foreign_key.should eq("person_ids")
1377
+ end
1378
+ end
1379
+
1380
+ context "when the inverse_of is nil" do
1381
+
1382
+ let(:metadata) do
1383
+ described_class.new(
1384
+ name: :blogs,
1385
+ class_name: "Blog",
1386
+ relation: Mongoid::Relations::Referenced::ManyToMany,
1387
+ inverse_of: nil
1388
+ )
1389
+ end
1390
+
1391
+ it "returns nil" do
1392
+ metadata.inverse_foreign_key.should be_nil
1393
+ end
1394
+ end
1395
+ end
1396
+
1397
+ context "#inverse_klass" do
1398
+
1399
+ let(:metadata) do
1400
+ described_class.new(
1401
+ inverse_class_name: "Person",
1402
+ relation: Mongoid::Relations::Referenced::In
1403
+ )
1404
+ end
1405
+
1406
+ it "constantizes the inverse_class_name" do
1407
+ metadata.inverse_klass.should eq(Person)
1408
+ end
1409
+ end
1410
+
1411
+ context "#inverse_setter" do
1412
+
1413
+ context "when the relation is not polymorphic" do
1414
+
1415
+ let(:metadata) do
1416
+ described_class.new(
1417
+ name: :pet,
1418
+ class_name: "Animal",
1419
+ inverse_class_name: "Person",
1420
+ relation: Mongoid::Relations::Referenced::In
1421
+ )
1422
+ end
1423
+
1424
+ it "returns a string for the setter" do
1425
+ metadata.inverse_setter.should eq("person=")
1426
+ end
1427
+ end
1428
+
1429
+ context "when the relation is polymorphic" do
1430
+
1431
+ context "when multiple relations against the same class exist" do
1432
+
1433
+ context "when a referenced in" do
1434
+
1435
+ let(:metadata) do
1436
+ described_class.new(
1437
+ name: :eyeable,
1438
+ polymorphic: true,
1439
+ relation: Mongoid::Relations::Referenced::In
1440
+ )
1441
+ end
1442
+
1443
+ let(:other) do
1444
+ Face.new
1445
+ end
1446
+
1447
+ it "returns nil" do
1448
+ metadata.inverse_setter(other).should be_nil
1449
+ end
1450
+ end
1451
+
1452
+ context "when a references many" do
1453
+
1454
+ let(:metadata) do
1455
+ described_class.new(
1456
+ name: :blue_eyes,
1457
+ inverse_class_name: "EyeBowl",
1458
+ as: :eyeable,
1459
+ relation: Mongoid::Relations::Referenced::Many
1460
+ )
1461
+ end
1462
+
1463
+ it "returns a string for the setter" do
1464
+ metadata.inverse_setter.should eq("eyeable=")
1465
+ end
1466
+ end
1467
+ end
1468
+
1469
+ context "when a referenced in" do
1470
+
1471
+ let(:metadata) do
1472
+ described_class.new(
1473
+ name: :ratable,
1474
+ inverse_class_name: "Rating",
1475
+ polymorphic: true,
1476
+ relation: Mongoid::Relations::Referenced::In
1477
+ )
1478
+ end
1479
+
1480
+ let(:other) do
1481
+ Movie.new
1482
+ end
1483
+
1484
+ it "returns a string for the setter" do
1485
+ metadata.inverse_setter(other).should eq("ratings=")
1486
+ end
1487
+ end
1488
+
1489
+ context "when a references many" do
1490
+
1491
+ let(:metadata) do
1492
+ described_class.new(
1493
+ name: :ratings,
1494
+ inverse_class_name: "Rating",
1495
+ as: :ratable,
1496
+ relation: Mongoid::Relations::Referenced::Many
1497
+ )
1498
+ end
1499
+
1500
+ it "returns a string for the setter" do
1501
+ metadata.inverse_setter.should eq("ratable=")
1502
+ end
1503
+ end
1504
+ end
1505
+ end
1506
+
1507
+ context "#key" do
1508
+
1509
+ context "when relation is embedded" do
1510
+
1511
+ let(:metadata) do
1512
+ described_class.new(
1513
+ name: :addresses,
1514
+ relation: Mongoid::Relations::Embedded::Many
1515
+ )
1516
+ end
1517
+
1518
+ it "returns the name as a string" do
1519
+ metadata.key.should eq("addresses")
1520
+ end
1521
+
1522
+ context "with a store_as option defined" do
1523
+
1524
+ let(:metadata) do
1525
+ described_class.new(
1526
+ name: :comment,
1527
+ relation: Mongoid::Relations::Embedded::Many,
1528
+ store_as: "user_comments"
1529
+ )
1530
+ end
1531
+
1532
+ it "return the name define by store_as option" do
1533
+ metadata.key.should eq("user_comments")
1534
+ end
1535
+ end
1536
+ end
1537
+
1538
+ context "when relation is referenced" do
1539
+
1540
+ context "when relation stores foreign_key" do
1541
+
1542
+ context "when the relation is not polymorphic" do
1543
+
1544
+ let(:metadata) do
1545
+ described_class.new(
1546
+ name: :posts,
1547
+ relation: Mongoid::Relations::Referenced::ManyToMany
1548
+ )
1549
+ end
1550
+
1551
+ it "returns the foreign_key" do
1552
+ metadata.key.should eq("post_ids")
1553
+ end
1554
+ end
1555
+
1556
+ context "when the relation is polymorphic" do
1557
+
1558
+ let(:metadata) do
1559
+ described_class.new(
1560
+ name: :ratable,
1561
+ relation: Mongoid::Relations::Referenced::In,
1562
+ polymorphic: true
1563
+ )
1564
+ end
1565
+
1566
+ it "returns the polymorphic foreign_key" do
1567
+ metadata.key.should eq("ratable_id")
1568
+ end
1569
+ end
1570
+ end
1571
+
1572
+ context "when relation does not store a foreign_key" do
1573
+
1574
+ let(:metadata) do
1575
+ described_class.new(
1576
+ name: :addresses,
1577
+ relation: Mongoid::Relations::Referenced::Many,
1578
+ inverse_class_name: "Person"
1579
+ )
1580
+ end
1581
+
1582
+ it "returns _id" do
1583
+ metadata.key.should eq("_id")
1584
+ end
1585
+ end
1586
+ end
1587
+ end
1588
+
1589
+ describe "#options" do
1590
+
1591
+ let(:metadata) do
1592
+ described_class.new(
1593
+ order: :rating.asc,
1594
+ relation: Mongoid::Relations::Referenced::Many
1595
+ )
1596
+ end
1597
+
1598
+ it "returns self" do
1599
+ metadata.options.should eq(metadata)
1600
+ end
1601
+ end
1602
+
1603
+ describe "#order" do
1604
+
1605
+ let(:metadata) do
1606
+ described_class.new(
1607
+ order: :rating.asc,
1608
+ relation: Mongoid::Relations::Referenced::Many
1609
+ )
1610
+ end
1611
+
1612
+ it "returns order criteria" do
1613
+ metadata.order.should eq(:rating.asc)
1614
+ end
1615
+ end
1616
+
1617
+ describe "#klass" do
1618
+
1619
+ context "when the class name is not namespaced" do
1620
+
1621
+ let(:metadata) do
1622
+ described_class.new(
1623
+ class_name: "Address",
1624
+ relation: Mongoid::Relations::Embedded::Many
1625
+ )
1626
+ end
1627
+
1628
+ it "constantizes the class_name" do
1629
+ metadata.klass.should eq(Address)
1630
+ end
1631
+ end
1632
+
1633
+ context "when the class name is prepended with ::" do
1634
+
1635
+ let(:metadata) do
1636
+ described_class.new(
1637
+ class_name: "::Address",
1638
+ relation: Mongoid::Relations::Embedded::Many
1639
+ )
1640
+ end
1641
+
1642
+ it "returns the class" do
1643
+ metadata.klass.should eq(Address)
1644
+ end
1645
+ end
1646
+ end
1647
+
1648
+ describe "#many?" do
1649
+
1650
+ context "when the relation is a many" do
1651
+
1652
+ let(:metadata) do
1653
+ described_class.new(relation: Mongoid::Relations::Embedded::Many)
1654
+ end
1655
+
1656
+ it "returns true" do
1657
+ metadata.should be_many
1658
+ end
1659
+ end
1660
+
1661
+ context "when the relation is not a many" do
1662
+
1663
+ let(:metadata) do
1664
+ described_class.new(relation: Mongoid::Relations::Embedded::One)
1665
+ end
1666
+
1667
+ it "returns false" do
1668
+ metadata.should_not be_many
1669
+ end
1670
+ end
1671
+ end
1672
+
1673
+ describe "#macro" do
1674
+
1675
+ let(:metadata) do
1676
+ described_class.new(relation: Mongoid::Relations::Embedded::One)
1677
+ end
1678
+
1679
+ it "returns the macro from the relation" do
1680
+ metadata.macro.should eq(:embeds_one)
1681
+ end
1682
+ end
1683
+
1684
+ describe "#nested_builder" do
1685
+
1686
+ let(:metadata) do
1687
+ described_class.new(relation: Mongoid::Relations::Embedded::One)
1688
+ end
1689
+
1690
+ let(:attributes) do
1691
+ {}
1692
+ end
1693
+
1694
+ let(:options) do
1695
+ {}
1696
+ end
1697
+
1698
+ it "returns the nested builder from the relation" do
1699
+ metadata.nested_builder(attributes, options).should
1700
+ be_a_kind_of(Mongoid::Relations::Builders::NestedAttributes::One)
1701
+ end
1702
+ end
1703
+
1704
+ describe "#primary_key" do
1705
+
1706
+ context "when no primary key exists" do
1707
+
1708
+ let(:metadata) do
1709
+ described_class.new(
1710
+ name: :person,
1711
+ inverse_class_name: "Person",
1712
+ relation: Mongoid::Relations::Referenced::In
1713
+ )
1714
+ end
1715
+
1716
+ it "returns _id" do
1717
+ metadata.primary_key.should eq("_id")
1718
+ end
1719
+ end
1720
+
1721
+ context "when a primary key exists" do
1722
+
1723
+ let(:metadata) do
1724
+ described_class.new(
1725
+ name: :person,
1726
+ inverse_class_name: "Person",
1727
+ relation: Mongoid::Relations::Referenced::In,
1728
+ primary_key: :something_id
1729
+ )
1730
+ end
1731
+
1732
+ it "returns the primary key" do
1733
+ metadata.primary_key.should eq("something_id")
1734
+ end
1735
+ end
1736
+ end
1737
+
1738
+ describe "#validate?" do
1739
+
1740
+ context "when validate is provided" do
1741
+
1742
+ context "when validate is true" do
1743
+
1744
+ let(:metadata) do
1745
+ described_class.new(
1746
+ name: :posts,
1747
+ inverse_class_name: "Post",
1748
+ relation: Mongoid::Relations::Referenced::Many,
1749
+ validate: true
1750
+ )
1751
+ end
1752
+
1753
+ it "returns true" do
1754
+ metadata.validate?.should be_true
1755
+ end
1756
+ end
1757
+
1758
+ context "when validate is false" do
1759
+
1760
+ let(:metadata) do
1761
+ described_class.new(
1762
+ name: :posts,
1763
+ inverse_class_name: "Post",
1764
+ relation: Mongoid::Relations::Referenced::Many,
1765
+ validate: false
1766
+ )
1767
+ end
1768
+
1769
+ it "returns false" do
1770
+ metadata.validate?.should be_false
1771
+ end
1772
+ end
1773
+ end
1774
+
1775
+ context "when validate is not provided" do
1776
+
1777
+ let(:metadata) do
1778
+ described_class.new(
1779
+ name: :posts,
1780
+ inverse_class_name: "Post",
1781
+ relation: Mongoid::Relations::Referenced::Many
1782
+ )
1783
+ end
1784
+
1785
+ it "returns the relation default" do
1786
+ metadata.validate?.should be_true
1787
+ end
1788
+ end
1789
+ end
1790
+
1791
+ describe "#versioned?" do
1792
+
1793
+ context "when versioned is true" do
1794
+
1795
+ let(:metadata) do
1796
+ described_class.new(
1797
+ name: :versions,
1798
+ relation: Mongoid::Relations::Embedded::Many,
1799
+ versioned: true
1800
+ )
1801
+ end
1802
+
1803
+ it "returns true" do
1804
+ metadata.should be_versioned
1805
+ end
1806
+ end
1807
+
1808
+ context "when versioned is false" do
1809
+
1810
+ let(:metadata) do
1811
+ described_class.new(
1812
+ name: :versions,
1813
+ relation: Mongoid::Relations::Embedded::Many,
1814
+ versioned: false
1815
+ )
1816
+ end
1817
+
1818
+ it "returns false" do
1819
+ metadata.should_not be_versioned
1820
+ end
1821
+ end
1822
+
1823
+ context "when versioned is nil" do
1824
+
1825
+ let(:metadata) do
1826
+ described_class.new(
1827
+ name: :versions,
1828
+ relation: Mongoid::Relations::Embedded::Many
1829
+ )
1830
+ end
1831
+
1832
+ it "returns false" do
1833
+ metadata.should_not be_versioned
1834
+ end
1835
+ end
1836
+ end
1837
+
1838
+ describe "#store_as" do
1839
+
1840
+ context "when store_as is define" do
1841
+
1842
+ let(:metadata) do
1843
+ described_class.new(
1844
+ name: :comment,
1845
+ relation: Mongoid::Relations::Embedded::Many,
1846
+ store_as: 'user_comments'
1847
+ )
1848
+ end
1849
+
1850
+ it "returns the value" do
1851
+ metadata.store_as.should eq("user_comments")
1852
+ end
1853
+ end
1854
+
1855
+ context "when is not define" do
1856
+
1857
+ let(:metadata) do
1858
+ described_class.new(
1859
+ name: :comments,
1860
+ relation: Mongoid::Relations::Embedded::Many,
1861
+ )
1862
+ end
1863
+
1864
+ it "returns false" do
1865
+ metadata.store_as.should eq("comments")
1866
+ end
1867
+ end
1868
+ end
1869
+
1870
+ describe "#determine_inverse_relation" do
1871
+
1872
+ let(:class_name) do
1873
+ "Person"
1874
+ end
1875
+
1876
+ let(:metadata) do
1877
+ described_class.new(
1878
+ relation: Mongoid::Relations::Referenced::In,
1879
+ inverse_class_name: "Drug",
1880
+ class_name: class_name,
1881
+ name: :person
1882
+ )
1883
+ end
1884
+
1885
+ let(:inverse_relation) do
1886
+ metadata.send(:determine_inverse_relation)
1887
+ end
1888
+
1889
+ context "when no match" do
1890
+
1891
+ let(:class_name) do
1892
+ "Slave"
1893
+ end
1894
+
1895
+ it "returns nil" do
1896
+ inverse_relation.should be_nil
1897
+ end
1898
+ end
1899
+
1900
+ context "when one match" do
1901
+
1902
+ it "returns correct relation" do
1903
+ inverse_relation.should eq(:drugs)
1904
+ end
1905
+ end
1906
+
1907
+ context "when multiple matches" do
1908
+
1909
+ before do
1910
+ class_name.constantize.has_many(:evil_drugs, class_name: "Drug")
1911
+ end
1912
+
1913
+ after do
1914
+ class_name.constantize.relations.delete("evil_drugs")
1915
+ Person.reset_callbacks(:validate)
1916
+ end
1917
+
1918
+ it "raises AmbiguousRelationship" do
1919
+ expect {
1920
+ inverse_relation
1921
+ }.to raise_error(Mongoid::Errors::AmbiguousRelationship)
1922
+ end
1923
+ end
1924
+ end
1925
+
1926
+ describe "touchable?" do
1927
+
1928
+ context "when touch is false" do
1929
+
1930
+ let(:metadata) do
1931
+ described_class.new(
1932
+ name: :versions,
1933
+ relation: Mongoid::Relations::Referenced::In,
1934
+ touch: false
1935
+ )
1936
+ end
1937
+
1938
+ it "returns false" do
1939
+ metadata.should_not be_touchable
1940
+ end
1941
+ end
1942
+
1943
+ context "when touch is true" do
1944
+
1945
+ let(:metadata) do
1946
+ described_class.new(
1947
+ name: :versions,
1948
+ relation: Mongoid::Relations::Referenced::In,
1949
+ touch: true
1950
+ )
1951
+ end
1952
+
1953
+ it "returns true" do
1954
+ metadata.should be_touchable
1955
+ end
1956
+ end
1957
+
1958
+ context "when touch is nil" do
1959
+
1960
+ let(:metadata) do
1961
+ described_class.new(
1962
+ name: :versions,
1963
+ relation: Mongoid::Relations::Referenced::In,
1964
+ )
1965
+ end
1966
+
1967
+ it "returns false" do
1968
+ metadata.should_not be_touchable
1969
+ end
1970
+ end
1971
+ end
1972
+
1973
+ context "properties" do
1974
+
1975
+ PROPERTIES = [
1976
+ "as",
1977
+ "cyclic",
1978
+ "name",
1979
+ "order"
1980
+ ]
1981
+
1982
+ PROPERTIES.each do |property|
1983
+
1984
+ describe "##{property}" do
1985
+
1986
+ let(:metadata) do
1987
+ described_class.new(
1988
+ property.to_sym => :value,
1989
+ relation: Mongoid::Relations::Embedded::Many
1990
+ )
1991
+ end
1992
+
1993
+ it "returns the #{property} property" do
1994
+ metadata.send(property).should eq(:value)
1995
+ end
1996
+ end
1997
+
1998
+ describe "##{property}?" do
1999
+
2000
+ context "when a #{property} property exists" do
2001
+
2002
+ let(:metadata) do
2003
+ described_class.new(
2004
+ property.to_sym => :value,
2005
+ relation: Mongoid::Relations::Embedded::Many
2006
+ )
2007
+ end
2008
+
2009
+ it "returns true" do
2010
+ metadata.send("#{property}?").should be_true
2011
+ end
2012
+ end
2013
+
2014
+ context "when the #{property} property is nil" do
2015
+
2016
+ let(:metadata) do
2017
+ described_class.new(
2018
+ relation: Mongoid::Relations::Embedded::Many
2019
+ )
2020
+ end
2021
+
2022
+ it "returns false" do
2023
+ metadata.send("#{property}?").should be_false
2024
+ end
2025
+ end
2026
+ end
2027
+ end
2028
+ end
2029
+
2030
+ end