mongoid 3.1.6 → 4.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (571) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +479 -8
  3. data/README.md +10 -7
  4. data/lib/config/locales/en.yml +34 -20
  5. data/lib/mongoid.rb +10 -76
  6. data/lib/mongoid/atomic.rb +3 -14
  7. data/lib/mongoid/atomic/paths/embedded.rb +0 -30
  8. data/lib/mongoid/atomic/paths/embedded/many.rb +1 -1
  9. data/lib/mongoid/atomic/paths/embedded/one.rb +1 -1
  10. data/lib/mongoid/atomic/paths/root.rb +0 -13
  11. data/lib/mongoid/attributes.rb +65 -101
  12. data/lib/mongoid/attributes/dynamic.rb +154 -0
  13. data/lib/mongoid/attributes/nested.rb +82 -0
  14. data/lib/mongoid/attributes/processing.rb +13 -66
  15. data/lib/mongoid/{dirty.rb → changeable.rb} +32 -2
  16. data/lib/mongoid/composable.rb +105 -0
  17. data/lib/mongoid/config.rb +3 -9
  18. data/lib/mongoid/config/options.rb +1 -1
  19. data/lib/mongoid/contextual.rb +2 -0
  20. data/lib/mongoid/contextual/aggregable/memory.rb +2 -2
  21. data/lib/mongoid/contextual/aggregable/mongo.rb +7 -9
  22. data/lib/mongoid/contextual/atomic.rb +53 -53
  23. data/lib/mongoid/contextual/geo_near.rb +1 -1
  24. data/lib/mongoid/contextual/map_reduce.rb +4 -2
  25. data/lib/mongoid/contextual/memory.rb +23 -11
  26. data/lib/mongoid/contextual/mongo.rb +75 -57
  27. data/lib/mongoid/contextual/none.rb +90 -0
  28. data/lib/mongoid/contextual/text_search.rb +178 -0
  29. data/lib/mongoid/copyable.rb +2 -3
  30. data/lib/mongoid/criteria.rb +39 -34
  31. data/lib/mongoid/criteria/#findable.rb# +141 -0
  32. data/lib/mongoid/{criterion → criteria}/findable.rb +7 -47
  33. data/lib/mongoid/{criterion/inspection.rb → criteria/inspectable.rb} +2 -2
  34. data/lib/mongoid/{criterion → criteria}/marshalable.rb +3 -1
  35. data/lib/mongoid/{criterion → criteria}/modifiable.rb +28 -8
  36. data/lib/mongoid/criteria/permission.rb +70 -0
  37. data/lib/mongoid/{criterion/scoping.rb → criteria/scopable.rb} +2 -2
  38. data/lib/mongoid/document.rb +39 -24
  39. data/lib/mongoid/errors.rb +4 -1
  40. data/lib/mongoid/errors/document_not_destroyed.rb +25 -0
  41. data/lib/mongoid/errors/document_not_found.rb +2 -1
  42. data/lib/mongoid/errors/invalid_storage_options.rb +1 -1
  43. data/lib/mongoid/errors/invalid_storage_parent.rb +26 -0
  44. data/lib/mongoid/errors/invalid_value.rb +16 -0
  45. data/lib/mongoid/errors/mongoid_error.rb +1 -1
  46. data/lib/mongoid/errors/readonly_document.rb +24 -0
  47. data/lib/mongoid/extensions.rb +15 -7
  48. data/lib/mongoid/extensions/array.rb +3 -3
  49. data/lib/mongoid/extensions/big_decimal.rb +1 -1
  50. data/lib/mongoid/extensions/boolean.rb +15 -17
  51. data/lib/mongoid/extensions/date.rb +1 -1
  52. data/lib/mongoid/extensions/date_time.rb +3 -3
  53. data/lib/mongoid/extensions/float.rb +2 -1
  54. data/lib/mongoid/extensions/hash.rb +3 -3
  55. data/lib/mongoid/extensions/integer.rb +1 -1
  56. data/lib/mongoid/extensions/object.rb +7 -7
  57. data/lib/mongoid/extensions/object_id.rb +5 -5
  58. data/lib/mongoid/extensions/range.rb +9 -3
  59. data/lib/mongoid/extensions/regexp.rb +1 -1
  60. data/lib/mongoid/extensions/set.rb +1 -1
  61. data/lib/mongoid/extensions/string.rb +6 -18
  62. data/lib/mongoid/extensions/symbol.rb +1 -1
  63. data/lib/mongoid/extensions/time.rb +3 -3
  64. data/lib/mongoid/extensions/time_with_zone.rb +1 -1
  65. data/lib/mongoid/factory.rb +8 -6
  66. data/lib/mongoid/fields.rb +41 -8
  67. data/lib/mongoid/fields/foreign_key.rb +3 -3
  68. data/lib/mongoid/fields/localized.rb +1 -1
  69. data/lib/mongoid/fields/standard.rb +5 -17
  70. data/lib/mongoid/fields/validators/macro.rb +15 -5
  71. data/lib/mongoid/{finders.rb → findable.rb} +35 -7
  72. data/lib/mongoid/{indexes.rb → indexable.rb} +50 -55
  73. data/lib/mongoid/indexable/specification.rb +104 -0
  74. data/lib/mongoid/{indexes → indexable}/validators/options.rb +2 -1
  75. data/lib/mongoid/{inspection.rb → inspectable.rb} +15 -11
  76. data/lib/mongoid/{callbacks.rb → interceptable.rb} +30 -62
  77. data/lib/mongoid/log_subscriber.rb +55 -0
  78. data/lib/mongoid/matchable.rb +152 -0
  79. data/lib/mongoid/{matchers → matchable}/all.rb +1 -1
  80. data/lib/mongoid/{matchers → matchable}/and.rb +2 -2
  81. data/lib/mongoid/{matchers → matchable}/default.rb +1 -1
  82. data/lib/mongoid/{matchers → matchable}/exists.rb +1 -1
  83. data/lib/mongoid/{matchers → matchable}/gt.rb +1 -1
  84. data/lib/mongoid/{matchers → matchable}/gte.rb +1 -1
  85. data/lib/mongoid/{matchers → matchable}/in.rb +2 -2
  86. data/lib/mongoid/{matchers → matchable}/lt.rb +1 -1
  87. data/lib/mongoid/{matchers → matchable}/lte.rb +1 -1
  88. data/lib/mongoid/{matchers → matchable}/ne.rb +2 -2
  89. data/lib/mongoid/{matchers → matchable}/nin.rb +3 -2
  90. data/lib/mongoid/{matchers → matchable}/or.rb +2 -2
  91. data/lib/mongoid/{matchers → matchable}/size.rb +1 -1
  92. data/lib/mongoid/persistable.rb +216 -0
  93. data/lib/mongoid/persistable/creatable.rb +189 -0
  94. data/lib/mongoid/persistable/deletable.rb +149 -0
  95. data/lib/mongoid/persistable/destroyable.rb +60 -0
  96. data/lib/mongoid/persistable/incrementable.rb +36 -0
  97. data/lib/mongoid/persistable/logical.rb +38 -0
  98. data/lib/mongoid/persistable/poppable.rb +39 -0
  99. data/lib/mongoid/persistable/pullable.rb +55 -0
  100. data/lib/mongoid/persistable/pushable.rb +62 -0
  101. data/lib/mongoid/persistable/renamable.rb +35 -0
  102. data/lib/mongoid/persistable/savable.rb +52 -0
  103. data/lib/mongoid/persistable/settable.rb +33 -0
  104. data/lib/mongoid/persistable/unsettable.rb +36 -0
  105. data/lib/mongoid/persistable/updatable.rb +153 -0
  106. data/lib/mongoid/persistable/upsertable.rb +55 -0
  107. data/lib/mongoid/positional.rb +71 -0
  108. data/lib/mongoid/query_cache.rb +255 -0
  109. data/lib/mongoid/railtie.rb +34 -60
  110. data/lib/mongoid/railties/database.rake +9 -25
  111. data/lib/mongoid/relations.rb +11 -25
  112. data/lib/mongoid/relations/accessors.rb +15 -51
  113. data/lib/mongoid/relations/auto_save.rb +15 -36
  114. data/lib/mongoid/relations/binding.rb +2 -25
  115. data/lib/mongoid/relations/bindings/embedded/in.rb +1 -1
  116. data/lib/mongoid/relations/bindings/embedded/many.rb +1 -3
  117. data/lib/mongoid/relations/bindings/referenced/in.rb +3 -8
  118. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +4 -2
  119. data/lib/mongoid/relations/builder.rb +1 -1
  120. data/lib/mongoid/relations/builders.rb +2 -2
  121. data/lib/mongoid/relations/builders/embedded/one.rb +1 -1
  122. data/lib/mongoid/relations/builders/nested_attributes/many.rb +10 -13
  123. data/lib/mongoid/relations/builders/nested_attributes/one.rb +5 -6
  124. data/lib/mongoid/relations/builders/referenced/in.rb +1 -1
  125. data/lib/mongoid/relations/builders/referenced/many.rb +1 -2
  126. data/lib/mongoid/relations/builders/referenced/many_to_many.rb +1 -2
  127. data/lib/mongoid/relations/builders/referenced/one.rb +1 -1
  128. data/lib/mongoid/relations/cascading.rb +3 -5
  129. data/lib/mongoid/relations/constraint.rb +1 -1
  130. data/lib/mongoid/relations/conversions.rb +1 -1
  131. data/lib/mongoid/relations/counter_cache.rb +39 -15
  132. data/lib/mongoid/relations/eager.rb +46 -0
  133. data/lib/mongoid/relations/eager/base.rb +149 -0
  134. data/lib/mongoid/relations/eager/belongs_to.rb +31 -0
  135. data/lib/mongoid/relations/eager/has_and_belongs_to_many.rb +47 -0
  136. data/lib/mongoid/relations/eager/has_many.rb +38 -0
  137. data/lib/mongoid/relations/eager/has_one.rb +30 -0
  138. data/lib/mongoid/relations/embedded/batchable.rb +4 -5
  139. data/lib/mongoid/relations/embedded/in.rb +4 -4
  140. data/lib/mongoid/relations/embedded/many.rb +14 -32
  141. data/lib/mongoid/relations/embedded/one.rb +1 -1
  142. data/lib/mongoid/relations/macros.rb +3 -4
  143. data/lib/mongoid/relations/many.rb +51 -31
  144. data/lib/mongoid/relations/marshalable.rb +3 -3
  145. data/lib/mongoid/relations/metadata.rb +14 -79
  146. data/lib/mongoid/relations/nested_builder.rb +2 -2
  147. data/lib/mongoid/relations/one.rb +1 -1
  148. data/lib/mongoid/relations/options.rb +1 -0
  149. data/lib/mongoid/relations/polymorphic.rb +0 -1
  150. data/lib/mongoid/relations/proxy.rb +21 -41
  151. data/lib/mongoid/relations/referenced/in.rb +4 -20
  152. data/lib/mongoid/relations/referenced/many.rb +22 -35
  153. data/lib/mongoid/relations/referenced/many_to_many.rb +19 -42
  154. data/lib/mongoid/relations/referenced/one.rb +6 -25
  155. data/lib/mongoid/relations/synchronization.rb +4 -4
  156. data/lib/mongoid/relations/targets/enumerable.rb +10 -10
  157. data/lib/mongoid/relations/touchable.rb +34 -1
  158. data/lib/mongoid/{reloading.rb → reloadable.rb} +7 -5
  159. data/lib/mongoid/{scoping.rb → scopable.rb} +36 -36
  160. data/lib/mongoid/selectable.rb +59 -0
  161. data/lib/mongoid/{serialization.rb → serializable.rb} +11 -2
  162. data/lib/mongoid/sessions.rb +37 -345
  163. data/lib/mongoid/sessions/factory.rb +2 -0
  164. data/lib/mongoid/sessions/options.rb +185 -0
  165. data/lib/mongoid/sessions/storage_options.rb +140 -0
  166. data/lib/mongoid/sessions/thread_options.rb +19 -0
  167. data/lib/mongoid/sessions/validators/storage.rb +16 -3
  168. data/lib/mongoid/{sharding.rb → shardable.rb} +5 -1
  169. data/lib/mongoid/{state.rb → stateful.rb} +13 -1
  170. data/lib/mongoid/support/query_counter.rb +23 -0
  171. data/lib/mongoid/tasks/database.rake +31 -0
  172. data/lib/mongoid/tasks/database.rb +107 -0
  173. data/lib/mongoid/threaded.rb +26 -172
  174. data/lib/mongoid/threaded/lifecycle.rb +0 -28
  175. data/lib/mongoid/timestamps/updated/short.rb +2 -2
  176. data/lib/mongoid/{hierarchy.rb → traversable.rb} +15 -7
  177. data/lib/mongoid/{validations.rb → validatable.rb} +23 -10
  178. data/lib/mongoid/{validations → validatable}/associated.rb +4 -2
  179. data/lib/mongoid/{validations → validatable}/format.rb +1 -1
  180. data/lib/mongoid/{validations → validatable}/length.rb +1 -1
  181. data/lib/mongoid/{validations → validatable}/localizable.rb +1 -1
  182. data/lib/mongoid/{validations → validatable}/macros.rb +4 -3
  183. data/lib/mongoid/{validations → validatable}/presence.rb +1 -1
  184. data/lib/mongoid/{validations → validatable}/queryable.rb +1 -2
  185. data/lib/mongoid/{validations → validatable}/uniqueness.rb +25 -21
  186. data/lib/mongoid/version.rb +1 -1
  187. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +46 -25
  188. data/lib/rails/generators/mongoid/model/model_generator.rb +0 -1
  189. data/lib/rails/generators/mongoid/model/templates/model.rb.tt +0 -3
  190. data/lib/rails/generators/mongoid_generator.rb +3 -40
  191. data/lib/rails/mongoid.rb +0 -122
  192. data/lib/support/ruby_version.rb +1 -1
  193. data/spec/app/models/account.rb +2 -6
  194. data/spec/app/models/acolyte.rb +2 -1
  195. data/spec/app/models/actor.rb +0 -1
  196. data/spec/app/models/address.rb +8 -3
  197. data/spec/app/models/animal.rb +1 -1
  198. data/spec/app/models/appointment.rb +3 -3
  199. data/spec/app/models/article.rb +1 -4
  200. data/spec/app/models/audio.rb +5 -0
  201. data/spec/app/models/author.rb +0 -2
  202. data/spec/app/models/band.rb +3 -2
  203. data/spec/app/models/bar.rb +1 -0
  204. data/spec/app/models/book.rb +1 -0
  205. data/spec/app/models/building.rb +0 -2
  206. data/spec/app/models/building_address.rb +0 -2
  207. data/spec/app/models/bus.rb +1 -1
  208. data/spec/app/models/canvas.rb +1 -1
  209. data/spec/app/models/contextable_item.rb +5 -0
  210. data/spec/app/models/contractor.rb +0 -2
  211. data/spec/app/models/country_code.rb +1 -1
  212. data/spec/app/models/definition.rb +2 -2
  213. data/spec/app/models/dog.rb +1 -1
  214. data/spec/app/models/draft.rb +9 -0
  215. data/spec/app/models/dragon.rb +4 -0
  216. data/spec/app/models/drug.rb +1 -3
  217. data/spec/app/models/dungeon.rb +4 -0
  218. data/spec/app/models/edit.rb +5 -0
  219. data/spec/app/models/even.rb +7 -0
  220. data/spec/app/models/event.rb +1 -1
  221. data/spec/app/models/filesystem.rb +1 -0
  222. data/spec/app/models/fish.rb +0 -1
  223. data/spec/app/models/fruits.rb +6 -0
  224. data/spec/app/models/game.rb +0 -2
  225. data/spec/app/models/house.rb +1 -3
  226. data/spec/app/models/id_key.rb +6 -0
  227. data/spec/app/models/item.rb +1 -5
  228. data/spec/app/models/jar.rb +1 -1
  229. data/spec/app/models/label.rb +4 -4
  230. data/spec/app/models/line_item.rb +1 -1
  231. data/spec/app/models/login.rb +1 -1
  232. data/spec/app/models/message.rb +8 -0
  233. data/spec/app/models/movie.rb +1 -0
  234. data/spec/app/models/name.rb +3 -3
  235. data/spec/app/models/note.rb +3 -1
  236. data/spec/app/models/odd.rb +7 -0
  237. data/spec/app/models/oscar.rb +1 -1
  238. data/spec/app/models/parent_doc.rb +1 -1
  239. data/spec/app/models/person.rb +16 -10
  240. data/spec/app/models/phone.rb +1 -3
  241. data/spec/app/models/player.rb +3 -3
  242. data/spec/app/models/post.rb +5 -3
  243. data/spec/app/models/preference.rb +1 -1
  244. data/spec/app/models/quiz.rb +0 -3
  245. data/spec/app/models/record.rb +10 -5
  246. data/spec/app/models/registry.rb +1 -1
  247. data/spec/app/models/server.rb +1 -1
  248. data/spec/app/models/service.rb +3 -3
  249. data/spec/app/models/sound.rb +5 -0
  250. data/spec/app/models/symptom.rb +1 -1
  251. data/spec/app/models/template.rb +1 -1
  252. data/spec/app/models/title.rb +0 -1
  253. data/spec/app/models/track.rb +5 -5
  254. data/spec/app/models/tree.rb +3 -3
  255. data/spec/app/models/video.rb +1 -5
  256. data/spec/app/models/wiki_page.rb +2 -5
  257. data/spec/app/models/word.rb +3 -0
  258. data/spec/app/models/word_origin.rb +1 -1
  259. data/spec/config/mongoid.yml +7 -8
  260. data/spec/helpers.rb +18 -0
  261. data/spec/mongoid/#atomic_spec.rb# +365 -0
  262. data/spec/mongoid/atomic/modifiers_spec.rb +22 -22
  263. data/spec/mongoid/atomic/paths/embedded/many_spec.rb +8 -49
  264. data/spec/mongoid/atomic/paths/embedded/one_spec.rb +7 -49
  265. data/spec/mongoid/atomic/paths/root_spec.rb +3 -61
  266. data/spec/mongoid/atomic/paths_spec.rb +19 -19
  267. data/spec/mongoid/atomic_spec.rb +19 -19
  268. data/spec/mongoid/{nested_attributes_spec.rb → attributes/nested_spec.rb} +474 -592
  269. data/spec/mongoid/attributes/readonly_spec.rb +58 -18
  270. data/spec/mongoid/attributes_spec.rb +349 -434
  271. data/spec/mongoid/{dirty_spec.rb → changeable_spec.rb} +203 -153
  272. data/spec/mongoid/{components_spec.rb → composable_spec.rb} +3 -3
  273. data/spec/mongoid/config/environment_spec.rb +3 -3
  274. data/spec/mongoid/config/options_spec.rb +6 -6
  275. data/spec/mongoid/config_spec.rb +24 -48
  276. data/spec/mongoid/contextual/aggregable/memory_spec.rb +12 -12
  277. data/spec/mongoid/contextual/aggregable/mongo_spec.rb +78 -38
  278. data/spec/mongoid/contextual/atomic_spec.rb +60 -56
  279. data/spec/mongoid/contextual/find_and_modify_spec.rb +14 -14
  280. data/spec/mongoid/contextual/geo_near_spec.rb +22 -22
  281. data/spec/mongoid/contextual/map_reduce_spec.rb +27 -27
  282. data/spec/mongoid/contextual/memory_spec.rb +156 -94
  283. data/spec/mongoid/contextual/mongo_spec.rb +238 -150
  284. data/spec/mongoid/contextual/none_spec.rb +127 -0
  285. data/spec/mongoid/contextual/text_search_spec.rb +209 -0
  286. data/spec/mongoid/copyable_spec.rb +56 -68
  287. data/spec/mongoid/criteria/findable_spec.rb +991 -0
  288. data/spec/mongoid/{criterion/inspection_spec.rb → criteria/inspectable_spec.rb} +5 -5
  289. data/spec/mongoid/{criterion → criteria}/marshalable_spec.rb +2 -2
  290. data/spec/mongoid/criteria/modifiable_spec.rb +1252 -0
  291. data/spec/mongoid/{criterion/scoping_spec.rb → criteria/scopable_spec.rb} +37 -37
  292. data/spec/mongoid/criteria_spec.rb +715 -2421
  293. data/spec/mongoid/document_spec.rb +171 -121
  294. data/spec/mongoid/equality_spec.rb +22 -22
  295. data/spec/mongoid/errors/ambiguous_relationship_spec.rb +3 -3
  296. data/spec/mongoid/errors/callback_spec.rb +3 -3
  297. data/spec/mongoid/errors/delete_restriction_spec.rb +3 -3
  298. data/spec/mongoid/errors/document_not_destroyed_spec.rb +33 -0
  299. data/spec/mongoid/errors/document_not_found_spec.rb +11 -11
  300. data/spec/mongoid/errors/eager_load_spec.rb +3 -3
  301. data/spec/mongoid/errors/invalid_collection_spec.rb +3 -3
  302. data/spec/mongoid/errors/invalid_config_option_spec.rb +3 -3
  303. data/spec/mongoid/errors/invalid_field_option_spec.rb +3 -3
  304. data/spec/mongoid/errors/invalid_field_spec.rb +3 -3
  305. data/spec/mongoid/errors/invalid_find_spec.rb +3 -3
  306. data/spec/mongoid/errors/invalid_includes_spec.rb +3 -3
  307. data/spec/mongoid/errors/invalid_index_spec.rb +3 -3
  308. data/spec/mongoid/errors/invalid_options_spec.rb +3 -3
  309. data/spec/mongoid/errors/invalid_path_spec.rb +3 -3
  310. data/spec/mongoid/errors/invalid_scope_spec.rb +5 -5
  311. data/spec/mongoid/errors/invalid_set_polymorphic_relation_spec.rb +3 -3
  312. data/spec/mongoid/errors/invalid_storage_options_spec.rb +3 -3
  313. data/spec/mongoid/errors/invalid_time_spec.rb +3 -3
  314. data/spec/mongoid/errors/inverse_not_found_spec.rb +3 -3
  315. data/spec/mongoid/errors/mixed_relations_spec.rb +3 -3
  316. data/spec/mongoid/errors/mixed_session_configuration_spec.rb +3 -3
  317. data/spec/mongoid/errors/mongoid_error_spec.rb +8 -8
  318. data/spec/mongoid/errors/nested_attributes_metadata_not_found_spec.rb +3 -3
  319. data/spec/mongoid/errors/no_environment_spec.rb +3 -3
  320. data/spec/mongoid/errors/no_map_reduce_output_spec.rb +3 -3
  321. data/spec/mongoid/errors/no_metadata_spec.rb +3 -3
  322. data/spec/mongoid/errors/no_parent_spec.rb +3 -3
  323. data/spec/mongoid/errors/no_session_config_spec.rb +3 -3
  324. data/spec/mongoid/errors/no_session_database_spec.rb +3 -3
  325. data/spec/mongoid/errors/no_session_hosts_spec.rb +3 -3
  326. data/spec/mongoid/errors/no_sessions_config_spec.rb +3 -3
  327. data/spec/mongoid/errors/readonly_attribute_spec.rb +3 -3
  328. data/spec/mongoid/errors/readonly_document_spec.rb +29 -0
  329. data/spec/mongoid/errors/scope_overwrite_spec.rb +3 -3
  330. data/spec/mongoid/errors/too_many_nested_attribute_records_spec.rb +3 -3
  331. data/spec/mongoid/errors/unknown_attribute_spec.rb +5 -5
  332. data/spec/mongoid/errors/unsaved_document_spec.rb +3 -3
  333. data/spec/mongoid/errors/unsupported_javascript_spec.rb +3 -3
  334. data/spec/mongoid/errors/validations_spec.rb +7 -7
  335. data/spec/mongoid/extensions/array_spec.rb +61 -61
  336. data/spec/mongoid/extensions/big_decimal_spec.rb +11 -11
  337. data/spec/mongoid/extensions/binary_spec.rb +12 -12
  338. data/spec/mongoid/extensions/boolean_spec.rb +18 -18
  339. data/spec/mongoid/extensions/date_spec.rb +13 -13
  340. data/spec/mongoid/extensions/date_time_spec.rb +8 -8
  341. data/spec/mongoid/extensions/false_class_spec.rb +5 -5
  342. data/spec/mongoid/extensions/float_spec.rb +29 -15
  343. data/spec/mongoid/extensions/hash_spec.rb +29 -29
  344. data/spec/mongoid/extensions/integer_spec.rb +15 -15
  345. data/spec/mongoid/extensions/module_spec.rb +2 -2
  346. data/spec/mongoid/extensions/nil_class_spec.rb +1 -1
  347. data/spec/mongoid/extensions/object_id_spec.rb +134 -134
  348. data/spec/mongoid/extensions/object_spec.rb +26 -26
  349. data/spec/mongoid/extensions/range_spec.rb +61 -12
  350. data/spec/mongoid/extensions/regexp_spec.rb +4 -4
  351. data/spec/mongoid/extensions/set_spec.rb +4 -4
  352. data/spec/mongoid/extensions/string_spec.rb +35 -46
  353. data/spec/mongoid/extensions/symbol_spec.rb +9 -9
  354. data/spec/mongoid/extensions/time_spec.rb +49 -49
  355. data/spec/mongoid/extensions/time_with_zone_spec.rb +34 -34
  356. data/spec/mongoid/extensions/true_class_spec.rb +5 -5
  357. data/spec/mongoid/extensions_spec.rb +29 -0
  358. data/spec/mongoid/factory_spec.rb +15 -15
  359. data/spec/mongoid/fields/foreign_key_spec.rb +73 -66
  360. data/spec/mongoid/fields/internal/foreign_keys/array_spec.rb +15 -15
  361. data/spec/mongoid/fields/internal/foreign_keys/object_spec.rb +22 -22
  362. data/spec/mongoid/fields/localized_spec.rb +38 -23
  363. data/spec/mongoid/fields/standard_spec.rb +12 -12
  364. data/spec/mongoid/fields_spec.rb +263 -217
  365. data/spec/mongoid/findable_spec.rb +509 -0
  366. data/spec/mongoid/indexable/specification_spec.rb +102 -0
  367. data/spec/mongoid/{indexes_spec.rb → indexable_spec.rb} +171 -51
  368. data/spec/mongoid/{inspection_spec.rb → inspectable_spec.rb} +6 -14
  369. data/spec/mongoid/{callbacks_spec.rb → interceptable_spec.rb} +117 -116
  370. data/spec/mongoid/log_subscriber_spec.rb +75 -0
  371. data/spec/mongoid/loggable_spec.rb +1 -1
  372. data/spec/mongoid/{matchers → matchable}/all_spec.rb +4 -4
  373. data/spec/mongoid/{matchers → matchable}/and_spec.rb +11 -11
  374. data/spec/mongoid/{matchers → matchable}/default_spec.rb +15 -15
  375. data/spec/mongoid/{matchers → matchable}/exists_spec.rb +5 -5
  376. data/spec/mongoid/{matchers → matchable}/gt_spec.rb +9 -8
  377. data/spec/mongoid/{matchers → matchable}/gte_spec.rb +7 -7
  378. data/spec/mongoid/matchable/in_spec.rb +49 -0
  379. data/spec/mongoid/{matchers → matchable}/lt_spec.rb +7 -7
  380. data/spec/mongoid/{matchers → matchable}/lte_spec.rb +7 -7
  381. data/spec/mongoid/matchable/ne_spec.rb +46 -0
  382. data/spec/mongoid/matchable/nin_spec.rb +48 -0
  383. data/spec/mongoid/{matchers → matchable}/or_spec.rb +6 -6
  384. data/spec/mongoid/{matchers → matchable}/size_spec.rb +3 -3
  385. data/spec/mongoid/{matchers_spec.rb → matchable_spec.rb} +41 -41
  386. data/spec/mongoid/persistable/creatable_spec.rb +553 -0
  387. data/spec/mongoid/persistable/deletable_spec.rb +218 -0
  388. data/spec/mongoid/persistable/destroyable_spec.rb +192 -0
  389. data/spec/mongoid/persistable/incrementable_spec.rb +173 -0
  390. data/spec/mongoid/persistable/logical_spec.rb +143 -0
  391. data/spec/mongoid/persistable/poppable_spec.rb +115 -0
  392. data/spec/mongoid/persistable/pullable_spec.rb +228 -0
  393. data/spec/mongoid/persistable/pushable_spec.rb +258 -0
  394. data/spec/mongoid/persistable/renamable_spec.rb +135 -0
  395. data/spec/mongoid/persistable/savable_spec.rb +498 -0
  396. data/spec/mongoid/persistable/settable_spec.rb +162 -0
  397. data/spec/mongoid/persistable/unsettable_spec.rb +155 -0
  398. data/spec/mongoid/persistable/updatable_spec.rb +558 -0
  399. data/spec/mongoid/persistable/upsertable_spec.rb +106 -0
  400. data/spec/mongoid/persistable_spec.rb +206 -0
  401. data/spec/mongoid/{atomic/positionable_spec.rb → positional_spec.rb} +14 -19
  402. data/spec/mongoid/query_cache_spec.rb +263 -0
  403. data/spec/mongoid/railties/document_spec.rb +2 -2
  404. data/spec/mongoid/relations/accessors_spec.rb +53 -145
  405. data/spec/mongoid/relations/auto_save_spec.rb +37 -17
  406. data/spec/mongoid/relations/bindings/embedded/in_spec.rb +12 -12
  407. data/spec/mongoid/relations/bindings/embedded/many_spec.rb +3 -3
  408. data/spec/mongoid/relations/bindings/embedded/one_spec.rb +5 -5
  409. data/spec/mongoid/relations/bindings/referenced/in_spec.rb +26 -26
  410. data/spec/mongoid/relations/bindings/referenced/many_spec.rb +10 -10
  411. data/spec/mongoid/relations/bindings/referenced/many_to_many_spec.rb +19 -19
  412. data/spec/mongoid/relations/bindings/referenced/one_spec.rb +12 -12
  413. data/spec/mongoid/relations/builders/embedded/in_spec.rb +4 -4
  414. data/spec/mongoid/relations/builders/embedded/many_spec.rb +18 -18
  415. data/spec/mongoid/relations/builders/embedded/one_spec.rb +11 -11
  416. data/spec/mongoid/relations/builders/nested_attributes/many_spec.rb +12 -13
  417. data/spec/mongoid/relations/builders/nested_attributes/one_spec.rb +14 -14
  418. data/spec/mongoid/relations/builders/referenced/in_spec.rb +24 -35
  419. data/spec/mongoid/relations/builders/referenced/many_spec.rb +15 -15
  420. data/spec/mongoid/relations/builders/referenced/many_to_many_spec.rb +18 -18
  421. data/spec/mongoid/relations/builders/referenced/one_spec.rb +12 -25
  422. data/spec/mongoid/relations/builders_spec.rb +21 -21
  423. data/spec/mongoid/relations/cascading/delete_spec.rb +9 -9
  424. data/spec/mongoid/relations/cascading/destroy_spec.rb +6 -6
  425. data/spec/mongoid/relations/cascading/nullify_spec.rb +4 -4
  426. data/spec/mongoid/relations/cascading/restrict_spec.rb +7 -7
  427. data/spec/mongoid/relations/cascading_spec.rb +16 -16
  428. data/spec/mongoid/relations/constraint_spec.rb +8 -7
  429. data/spec/mongoid/relations/conversions_spec.rb +16 -14
  430. data/spec/mongoid/relations/counter_cache_spec.rb +85 -0
  431. data/spec/mongoid/relations/cyclic_spec.rb +18 -18
  432. data/spec/mongoid/relations/eager/belongs_to_spec.rb +154 -0
  433. data/spec/mongoid/relations/eager/has_and_belongs_to_many_spec.rb +117 -0
  434. data/spec/mongoid/relations/eager/has_many_spec.rb +207 -0
  435. data/spec/mongoid/relations/eager/has_one_spec.rb +163 -0
  436. data/spec/mongoid/relations/eager_spec.rb +228 -0
  437. data/spec/mongoid/relations/embedded/dirty_spec.rb +8 -8
  438. data/spec/mongoid/relations/embedded/in_spec.rb +56 -57
  439. data/spec/mongoid/relations/embedded/many_spec.rb +467 -372
  440. data/spec/mongoid/relations/embedded/one_spec.rb +108 -141
  441. data/spec/mongoid/relations/macros_spec.rb +108 -102
  442. data/spec/mongoid/relations/metadata_spec.rb +180 -255
  443. data/spec/mongoid/relations/options_spec.rb +1 -1
  444. data/spec/mongoid/relations/polymorphic_spec.rb +13 -17
  445. data/spec/mongoid/relations/proxy_spec.rb +2 -2
  446. data/spec/mongoid/relations/referenced/in_spec.rb +128 -303
  447. data/spec/mongoid/relations/referenced/many_spec.rb +587 -527
  448. data/spec/mongoid/relations/referenced/many_to_many_spec.rb +401 -424
  449. data/spec/mongoid/relations/referenced/one_spec.rb +149 -264
  450. data/spec/mongoid/relations/reflections_spec.rb +6 -6
  451. data/spec/mongoid/relations/synchronization_spec.rb +46 -50
  452. data/spec/mongoid/relations/targets/enumerable_spec.rb +118 -118
  453. data/spec/mongoid/relations/touchable_spec.rb +333 -0
  454. data/spec/mongoid/relations_spec.rb +16 -15
  455. data/spec/mongoid/{reloading_spec.rb → reloadable_spec.rb} +38 -55
  456. data/spec/mongoid/{scoping_spec.rb → scopable_spec.rb} +89 -164
  457. data/spec/mongoid/selectable_spec.rb +134 -0
  458. data/spec/mongoid/{serialization_spec.rb → serializable_spec.rb} +129 -82
  459. data/spec/mongoid/sessions/factory_spec.rb +49 -28
  460. data/spec/mongoid/sessions/mongo_uri_spec.rb +7 -7
  461. data/spec/mongoid/sessions/options_spec.rb +108 -0
  462. data/spec/mongoid/sessions_spec.rb +319 -178
  463. data/spec/mongoid/{sharding_spec.rb → shardable_spec.rb} +8 -8
  464. data/spec/mongoid/{state_spec.rb → stateful_spec.rb} +36 -11
  465. data/spec/mongoid/tasks/database_rake_spec.rb +285 -0
  466. data/spec/mongoid/tasks/database_spec.rb +160 -0
  467. data/spec/mongoid/threaded_spec.rb +17 -70
  468. data/spec/mongoid/timestamps/created/short_spec.rb +6 -6
  469. data/spec/mongoid/timestamps/created_spec.rb +5 -5
  470. data/spec/mongoid/timestamps/timeless_spec.rb +6 -6
  471. data/spec/mongoid/timestamps/updated/short_spec.rb +11 -11
  472. data/spec/mongoid/timestamps/updated_spec.rb +9 -13
  473. data/spec/mongoid/timestamps_spec.rb +9 -13
  474. data/spec/mongoid/{hierarchy_spec.rb → traversable_spec.rb} +19 -19
  475. data/spec/mongoid/{validations → validatable}/associated_spec.rb +45 -22
  476. data/spec/mongoid/{validations → validatable}/format_spec.rb +6 -6
  477. data/spec/mongoid/{validations → validatable}/length_spec.rb +14 -14
  478. data/spec/mongoid/{validations → validatable}/numericality_spec.rb +1 -1
  479. data/spec/mongoid/{validations → validatable}/presence_spec.rb +37 -44
  480. data/spec/mongoid/{validations → validatable}/uniqueness_spec.rb +184 -192
  481. data/spec/mongoid/{validations_spec.rb → validatable_spec.rb} +28 -28
  482. data/spec/mongoid_spec.rb +8 -8
  483. data/spec/rails/mongoid_spec.rb +19 -335
  484. data/spec/spec_helper.rb +31 -13
  485. metadata +303 -283
  486. data/lib/mongoid/atomic/positionable.rb +0 -73
  487. data/lib/mongoid/components.rb +0 -92
  488. data/lib/mongoid/config/inflections.rb +0 -6
  489. data/lib/mongoid/contextual/eager.rb +0 -158
  490. data/lib/mongoid/errors/versioning_not_on_root.rb +0 -23
  491. data/lib/mongoid/identity_map.rb +0 -163
  492. data/lib/mongoid/json.rb +0 -16
  493. data/lib/mongoid/matchers.rb +0 -32
  494. data/lib/mongoid/matchers/strategies.rb +0 -97
  495. data/lib/mongoid/multi_parameter_attributes.rb +0 -105
  496. data/lib/mongoid/nested_attributes.rb +0 -78
  497. data/lib/mongoid/observer.rb +0 -192
  498. data/lib/mongoid/paranoia.rb +0 -136
  499. data/lib/mongoid/persistence.rb +0 -357
  500. data/lib/mongoid/persistence/atomic.rb +0 -231
  501. data/lib/mongoid/persistence/atomic/add_to_set.rb +0 -47
  502. data/lib/mongoid/persistence/atomic/bit.rb +0 -35
  503. data/lib/mongoid/persistence/atomic/inc.rb +0 -45
  504. data/lib/mongoid/persistence/atomic/operation.rb +0 -154
  505. data/lib/mongoid/persistence/atomic/pop.rb +0 -32
  506. data/lib/mongoid/persistence/atomic/pull.rb +0 -32
  507. data/lib/mongoid/persistence/atomic/pull_all.rb +0 -32
  508. data/lib/mongoid/persistence/atomic/push.rb +0 -25
  509. data/lib/mongoid/persistence/atomic/push_all.rb +0 -25
  510. data/lib/mongoid/persistence/atomic/rename.rb +0 -30
  511. data/lib/mongoid/persistence/atomic/sets.rb +0 -28
  512. data/lib/mongoid/persistence/atomic/unset.rb +0 -27
  513. data/lib/mongoid/persistence/deletion.rb +0 -31
  514. data/lib/mongoid/persistence/insertion.rb +0 -38
  515. data/lib/mongoid/persistence/modification.rb +0 -35
  516. data/lib/mongoid/persistence/operations.rb +0 -214
  517. data/lib/mongoid/persistence/operations/embedded/insert.rb +0 -46
  518. data/lib/mongoid/persistence/operations/embedded/remove.rb +0 -43
  519. data/lib/mongoid/persistence/operations/insert.rb +0 -34
  520. data/lib/mongoid/persistence/operations/remove.rb +0 -33
  521. data/lib/mongoid/persistence/operations/update.rb +0 -59
  522. data/lib/mongoid/persistence/operations/upsert.rb +0 -28
  523. data/lib/mongoid/persistence/upsertion.rb +0 -31
  524. data/lib/mongoid/unit_of_work.rb +0 -61
  525. data/lib/mongoid/versioning.rb +0 -217
  526. data/lib/rack/mongoid.rb +0 -2
  527. data/lib/rack/mongoid/middleware/identity_map.rb +0 -39
  528. data/lib/rails/generators/mongoid/observer/observer_generator.rb +0 -17
  529. data/lib/rails/generators/mongoid/observer/templates/observer.rb.tt +0 -4
  530. data/spec/app/models/actor_observer.rb +0 -15
  531. data/spec/app/models/callback_recorder.rb +0 -25
  532. data/spec/app/models/paranoid_phone.rb +0 -25
  533. data/spec/app/models/paranoid_post.rb +0 -36
  534. data/spec/app/models/phone_observer.rb +0 -6
  535. data/spec/mongoid/attributes/processing_spec.rb +0 -149
  536. data/spec/mongoid/criterion/destructive_spec.rb +0 -101
  537. data/spec/mongoid/criterion/modifiable_spec.rb +0 -409
  538. data/spec/mongoid/criterion/modification_spec.rb +0 -402
  539. data/spec/mongoid/errors/versioning_not_on_root_spec.rb +0 -29
  540. data/spec/mongoid/finders_spec.rb +0 -321
  541. data/spec/mongoid/identity_map_spec.rb +0 -564
  542. data/spec/mongoid/json_spec.rb +0 -33
  543. data/spec/mongoid/matchers/in_spec.rb +0 -25
  544. data/spec/mongoid/matchers/ne_spec.rb +0 -25
  545. data/spec/mongoid/matchers/nin_spec.rb +0 -25
  546. data/spec/mongoid/multi_parameter_attributes_spec.rb +0 -128
  547. data/spec/mongoid/observer_spec.rb +0 -290
  548. data/spec/mongoid/paranoia_spec.rb +0 -759
  549. data/spec/mongoid/persistence/atomic/add_to_set_spec.rb +0 -262
  550. data/spec/mongoid/persistence/atomic/bit_spec.rb +0 -88
  551. data/spec/mongoid/persistence/atomic/inc_spec.rb +0 -133
  552. data/spec/mongoid/persistence/atomic/pop_spec.rb +0 -111
  553. data/spec/mongoid/persistence/atomic/pull_all_spec.rb +0 -77
  554. data/spec/mongoid/persistence/atomic/pull_spec.rb +0 -80
  555. data/spec/mongoid/persistence/atomic/push_all_spec.rb +0 -77
  556. data/spec/mongoid/persistence/atomic/push_spec.rb +0 -77
  557. data/spec/mongoid/persistence/atomic/rename_spec.rb +0 -42
  558. data/spec/mongoid/persistence/atomic/sets_spec.rb +0 -154
  559. data/spec/mongoid/persistence/atomic/unset_spec.rb +0 -65
  560. data/spec/mongoid/persistence/atomic_spec.rb +0 -216
  561. data/spec/mongoid/persistence/operations/embedded/insert_spec.rb +0 -191
  562. data/spec/mongoid/persistence/operations/embedded/remove_spec.rb +0 -8
  563. data/spec/mongoid/persistence/operations/insert_spec.rb +0 -149
  564. data/spec/mongoid/persistence/operations/remove_spec.rb +0 -113
  565. data/spec/mongoid/persistence/operations/update_spec.rb +0 -141
  566. data/spec/mongoid/persistence/operations/upsert_spec.rb +0 -59
  567. data/spec/mongoid/persistence/operations_spec.rb +0 -313
  568. data/spec/mongoid/persistence_spec.rb +0 -2279
  569. data/spec/mongoid/unit_of_work_spec.rb +0 -196
  570. data/spec/mongoid/versioning_spec.rb +0 -540
  571. data/spec/rack/mongoid/middleware/identity_map_spec.rb +0 -72
@@ -15,7 +15,7 @@ describe Mongoid::Attributes::Readonly do
15
15
  end
16
16
 
17
17
  it "adds the field to readonly attributes" do
18
- Person.readonly_attributes.to_a.should eq([ "title" ])
18
+ expect(Person.readonly_attributes.to_a).to eq([ "title" ])
19
19
  end
20
20
  end
21
21
 
@@ -26,7 +26,7 @@ describe Mongoid::Attributes::Readonly do
26
26
  end
27
27
 
28
28
  it "adds the fields to readonly attributes" do
29
- Person.readonly_attributes.to_a.should eq([ "title", "terms" ])
29
+ expect(Person.readonly_attributes.to_a).to eq([ "title", "terms" ])
30
30
  end
31
31
  end
32
32
 
@@ -41,30 +41,30 @@ describe Mongoid::Attributes::Readonly do
41
41
  end
42
42
 
43
43
  it "sets the first readonly value" do
44
- person.title.should eq("sir")
44
+ expect(person.title).to eq("sir")
45
45
  end
46
46
 
47
47
  it "sets subsequent readonly values" do
48
- person.terms.should be_true
48
+ expect(person.terms).to be true
49
49
  end
50
50
 
51
51
  it "persists the first readonly value" do
52
- person.reload.title.should eq("sir")
52
+ expect(person.reload.title).to eq("sir")
53
53
  end
54
54
 
55
55
  it "persists subsequent readonly values" do
56
- person.reload.terms.should be_true
56
+ expect(person.reload.terms).to be true
57
57
  end
58
58
  end
59
59
 
60
60
  context "when updating an existing readonly field" do
61
61
 
62
62
  before do
63
- Person.attr_readonly :title, :terms
63
+ Person.attr_readonly :title, :terms, :score
64
64
  end
65
65
 
66
66
  let(:person) do
67
- Person.create(title: "sir", terms: true)
67
+ Person.create(title: "sir", terms: true, score: 1)
68
68
  end
69
69
 
70
70
  context "when updating via the setter" do
@@ -75,11 +75,51 @@ describe Mongoid::Attributes::Readonly do
75
75
  end
76
76
 
77
77
  it "does not update the field" do
78
- person.title.should eq("sir")
78
+ expect(person.title).to eq("sir")
79
79
  end
80
80
 
81
81
  it "does not persist the changes" do
82
- person.reload.title.should eq("sir")
82
+ expect(person.reload.title).to eq("sir")
83
+ end
84
+ end
85
+
86
+ context "when updating via inc" do
87
+
88
+ context 'with single field operation' do
89
+ it "raises an error " do
90
+ expect {
91
+ person.inc(score: 1)
92
+ }.to raise_error(Mongoid::Errors::ReadonlyAttribute)
93
+ end
94
+ end
95
+
96
+ context 'with multiple fields operation' do
97
+ it "raises an error " do
98
+ expect {
99
+ person.inc(score: 1, age: 1)
100
+ }.to raise_error(Mongoid::Errors::ReadonlyAttribute)
101
+ end
102
+ end
103
+ end
104
+
105
+ context "when updating via bit" do
106
+
107
+ context 'with single field operation' do
108
+ it "raises an error " do
109
+ expect {
110
+ person.bit(score: { or: 13 })
111
+ }.to raise_error(Mongoid::Errors::ReadonlyAttribute)
112
+ end
113
+ end
114
+
115
+ context 'with multiple fields operation' do
116
+ it "raises an error " do
117
+ expect {
118
+ person.bit(
119
+ age: { and: 13 }, score: { or: 13 }, inte: { and: 13, or: 10 }
120
+ )
121
+ }.to raise_error(Mongoid::Errors::ReadonlyAttribute)
122
+ end
83
123
  end
84
124
  end
85
125
 
@@ -91,11 +131,11 @@ describe Mongoid::Attributes::Readonly do
91
131
  end
92
132
 
93
133
  it "does not update the field" do
94
- person.title.should eq("sir")
134
+ expect(person.title).to eq("sir")
95
135
  end
96
136
 
97
137
  it "does not persist the changes" do
98
- person.reload.title.should eq("sir")
138
+ expect(person.reload.title).to eq("sir")
99
139
  end
100
140
  end
101
141
 
@@ -107,11 +147,11 @@ describe Mongoid::Attributes::Readonly do
107
147
  end
108
148
 
109
149
  it "does not update the field" do
110
- person.title.should eq("sir")
150
+ expect(person.title).to eq("sir")
111
151
  end
112
152
 
113
153
  it "does not persist the changes" do
114
- person.reload.title.should eq("sir")
154
+ expect(person.reload.title).to eq("sir")
115
155
  end
116
156
  end
117
157
 
@@ -123,11 +163,11 @@ describe Mongoid::Attributes::Readonly do
123
163
  end
124
164
 
125
165
  it "does not update the field" do
126
- person.title.should eq("sir")
166
+ expect(person.title).to eq("sir")
127
167
  end
128
168
 
129
169
  it "does not persist the changes" do
130
- person.reload.title.should eq("sir")
170
+ expect(person.reload.title).to eq("sir")
131
171
  end
132
172
  end
133
173
 
@@ -139,11 +179,11 @@ describe Mongoid::Attributes::Readonly do
139
179
  end
140
180
 
141
181
  it "does not update the field" do
142
- person.title.should eq("sir")
182
+ expect(person.title).to eq("sir")
143
183
  end
144
184
 
145
185
  it "does not persist the changes" do
146
- person.reload.title.should eq("sir")
186
+ expect(person.reload.title).to eq("sir")
147
187
  end
148
188
  end
149
189
 
@@ -11,7 +11,75 @@ describe Mongoid::Attributes do
11
11
  end
12
12
 
13
13
  it "does not cause an infinite loop" do
14
- account.overridden.should eq("not recommended")
14
+ expect(account.overridden).to eq("not recommended")
15
+ end
16
+ end
17
+
18
+ context "when the attribute was excluded in a criteria" do
19
+
20
+ let!(:person) do
21
+ Person.create(title: "sir")
22
+ end
23
+
24
+ context "when the attribute is localized" do
25
+
26
+ before do
27
+ person.update_attribute(:desc, "test")
28
+ end
29
+
30
+ context "when the context includes" do
31
+
32
+ context "when the attribute exists" do
33
+
34
+ let(:from_db) do
35
+ Person.only(:desc).first
36
+ end
37
+
38
+ it "does not raise an error" do
39
+ expect(from_db.desc).to eq("test")
40
+ end
41
+ end
42
+ end
43
+
44
+ context "when the context excludes" do
45
+
46
+ context "when the attribute exists" do
47
+
48
+ let(:from_db) do
49
+ Person.without(:pets).first
50
+ end
51
+
52
+ it "does not raise an error" do
53
+ expect(from_db.desc).to eq("test")
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ context "when excluding with only" do
60
+
61
+ let(:from_db) do
62
+ Person.only(:_id).first
63
+ end
64
+
65
+ it "raises an error" do
66
+ expect {
67
+ from_db.title
68
+ }.to raise_error(ActiveModel::MissingAttributeError)
69
+ end
70
+ end
71
+
72
+ context "when excluding with without" do
73
+
74
+ let(:from_db) do
75
+ Person.without(:title).first
76
+ end
77
+
78
+ it "raises an error" do
79
+ expect {
80
+ from_db.title
81
+ }.to raise_error(ActiveModel::MissingAttributeError)
82
+ end
15
83
  end
16
84
  end
17
85
  end
@@ -33,14 +101,14 @@ describe Mongoid::Attributes do
33
101
  context "when passing just the name" do
34
102
 
35
103
  it "returns the full value" do
36
- person[:desc].should eq("en" => "testing")
104
+ expect(person[:desc]).to eq("en" => "testing")
37
105
  end
38
106
  end
39
107
 
40
108
  context "when passing the name with locale" do
41
109
 
42
110
  it "returns the value for the locale" do
43
- person["desc.en"].should eq("testing")
111
+ expect(person["desc.en"]).to eq("testing")
44
112
  end
45
113
  end
46
114
  end
@@ -48,7 +116,7 @@ describe Mongoid::Attributes do
48
116
  context "when attribute does not exist" do
49
117
 
50
118
  it "returns the default value" do
51
- person[:age].should eq(100)
119
+ expect(person[:age]).to eq(100)
52
120
  end
53
121
  end
54
122
 
@@ -59,7 +127,7 @@ describe Mongoid::Attributes do
59
127
  end
60
128
 
61
129
  it "returns the value" do
62
- person[:owner_id].should eq(5)
130
+ expect(person[:owner_id]).to eq(5)
63
131
  end
64
132
  end
65
133
  end
@@ -70,6 +138,35 @@ describe Mongoid::Attributes do
70
138
  Person.create(title: "sir")
71
139
  end
72
140
 
141
+ context "when the attribute was excluded in a criteria" do
142
+
143
+ context "when excluding with only" do
144
+
145
+ let(:from_db) do
146
+ Person.only(:_id).first
147
+ end
148
+
149
+ it "raises an error" do
150
+ expect {
151
+ from_db[:title]
152
+ }.to raise_error(ActiveModel::MissingAttributeError)
153
+ end
154
+ end
155
+
156
+ context "when excluding with without" do
157
+
158
+ let(:from_db) do
159
+ Person.without(:title).first
160
+ end
161
+
162
+ it "raises an error" do
163
+ expect {
164
+ from_db[:title]
165
+ }.to raise_error(ActiveModel::MissingAttributeError)
166
+ end
167
+ end
168
+ end
169
+
73
170
  context "when the attribute does not exist" do
74
171
 
75
172
  before do
@@ -85,7 +182,7 @@ describe Mongoid::Attributes do
85
182
  end
86
183
 
87
184
  it "returns the default value" do
88
- found[:age].should eq(100)
185
+ expect(found[:age]).to eq(100)
89
186
  end
90
187
  end
91
188
 
@@ -98,7 +195,7 @@ describe Mongoid::Attributes do
98
195
  end
99
196
 
100
197
  it "returns the default value" do
101
- person[:age].should eq(100)
198
+ expect(person[:age]).to eq(100)
102
199
  end
103
200
  end
104
201
  end
@@ -113,293 +210,66 @@ describe Mongoid::Attributes do
113
210
 
114
211
  context "when setting the attribute to nil" do
115
212
 
116
- before do
213
+ let!(:age) do
117
214
  person[:age] = nil
118
215
  end
119
216
 
120
217
  it "does not use the default value" do
121
- person.age.should be_nil
218
+ expect(person.age).to be_nil
219
+ end
220
+
221
+ it "returns the set value" do
222
+ expect(age).to be_nil
122
223
  end
123
224
  end
124
225
 
125
226
  context "when field has a default value" do
126
227
 
127
- before do
228
+ let!(:terms) do
128
229
  person[:terms] = true
129
230
  end
130
231
 
131
232
  it "allows overwriting of the default value" do
132
- person.terms.should be_true
133
- end
134
- end
135
- end
136
-
137
- describe ".attr_accessible" do
138
-
139
- context "when the field is not _id" do
140
-
141
- let(:account) do
142
- Account.new(number: 999999)
233
+ expect(person.terms).to be true
143
234
  end
144
235
 
145
- it "prevents setting via mass assignment" do
146
- account.number.should be_nil
147
- end
148
- end
149
-
150
- context "when the field is _id" do
151
-
152
- let(:account) do
153
- Account.new(_id: "ABBA")
154
- end
155
-
156
- it "prevents setting via mass assignment" do
157
- account._id.should_not eq("ABBA")
158
- end
159
- end
160
-
161
- context "when using instantiate" do
162
-
163
- let(:account) do
164
- Account.instantiate("_id" => "1", "balance" => "ABBA")
165
- end
166
-
167
- it "ignores any protected attribute" do
168
- account.balance.should eq("ABBA")
169
- end
170
- end
171
-
172
- context "when using override" do
173
-
174
- let(:account) do
175
- Account.new
176
- end
177
-
178
- it "ignores any protected attribute" do
179
- account.write_attributes({balance: "ABBA"}, false)
180
- account.balance.should eq("ABBA")
181
- end
182
- end
183
-
184
- context "when mass assignment role is indicated" do
185
-
186
- context "when attributes assigned from default role" do
187
-
188
- let(:article) do
189
- Article.new(
190
- title: "Some Title",
191
- is_rss: true,
192
- user_login: "SomeLogin"
193
- )
194
- end
195
-
196
- it "sets the title field for default role" do
197
- article.title.should eq("Some Title")
198
- end
199
-
200
- it "sets the user login field for the default role" do
201
- article.user_login.should eq("SomeLogin")
202
- end
203
-
204
- it "sets the rss field for the default role" do
205
- article.is_rss.should be_false
206
- end
207
- end
208
-
209
- context "when attributes assigned from parser role" do
210
-
211
- let(:article) do
212
- Article.new({
213
- title: "Some Title",
214
- is_rss: true,
215
- user_login: "SomeLogin"
216
- }, as: :parser
217
- )
218
- end
219
-
220
- it "sets the title field" do
221
- article.title.should eq("Some Title")
222
- end
223
-
224
- it "sets the rss field" do
225
- article.is_rss.should be_true
226
- end
227
-
228
- it "does not set the user login field" do
229
- article.user_login.should be_nil
230
- end
231
- end
232
-
233
- context "when attributes assigned without protection" do
234
-
235
- let(:article) do
236
- Article.new(
237
- { title: "Some Title",
238
- is_rss: true,
239
- user_login: "SomeLogin"
240
- }, without_protection: true
241
- )
242
- end
243
-
244
- it "sets the title field" do
245
- article.title.should eq("Some Title")
246
- end
247
-
248
- it "sets the user login field" do
249
- article.user_login.should eq("SomeLogin")
250
- end
251
-
252
- it "sets the rss field" do
253
- article.is_rss.should be_true
254
- end
236
+ it "returns the set value" do
237
+ expect(terms).to eq(true)
255
238
  end
256
239
  end
257
240
  end
258
241
 
259
- describe ".attr_protected" do
260
-
261
- context "when the field is not _id" do
262
-
263
- let(:person) do
264
- Person.new(security_code: "ABBA")
265
- end
266
-
267
- it "prevents setting via mass assignment" do
268
- person.security_code.should be_nil
269
- end
270
- end
271
-
272
- context "when the field is _id" do
273
-
274
- let(:game) do
275
- Game.new(_id: "ABBA")
276
- end
242
+ describe "#_id" do
277
243
 
278
- it "prevents setting via mass assignment" do
279
- game._id.should_not eq("ABBA")
280
- end
244
+ let(:person) do
245
+ Person.new
281
246
  end
282
247
 
283
- context "when using instantiate" do
284
-
285
- let(:person) do
286
- Person.instantiate("_id" => "1", "security_code" => "ABBA")
287
- end
288
-
289
- it "ignores any protected attribute" do
290
- person.security_code.should eq("ABBA")
291
- end
248
+ it "delegates to #id" do
249
+ expect(person._id).to eq(person.id)
292
250
  end
293
251
 
294
- context "when using override" do
252
+ context "when #id alias is overridden" do
295
253
 
296
- let(:person) do
297
- Person.new
254
+ let(:object) do
255
+ IdKey.new(key: 'foo')
298
256
  end
299
257
 
300
- it "ignores any protected attribute" do
301
- person.write_attributes({security_code: "ABBA"}, false)
302
- person.security_code.should eq("ABBA")
303
- end
304
- end
305
-
306
- context "when mass assignment role is indicated" do
307
-
308
- let(:item) do
309
- Item.new
310
- end
311
-
312
- context "when attributes assigned from default role" do
313
-
314
- before do
315
- item.assign_attributes(
316
- title: "Some Title",
317
- is_rss: true,
318
- user_login: "SomeLogin"
319
- )
320
- end
321
-
322
- it "sets the field for the default role" do
323
- item.is_rss.should be_true
324
- end
325
-
326
- it "does not set the field for non default role title" do
327
- item.title.should be_nil
328
- end
329
-
330
- it "does not set the field for non default role user login" do
331
- item.user_login.should be_nil
332
- end
333
- end
334
-
335
- context "when attributes assigned from parser role" do
336
-
337
- before do
338
- item.assign_attributes(
339
- { title: "Some Title",
340
- is_rss: true,
341
- user_login: "SomeLogin" }, as: :parser
342
- )
343
- end
344
-
345
- it "sets the user login field for parser role" do
346
- item.user_login.should eq("SomeLogin")
347
- end
348
-
349
- it "sets the is rss field for parse role" do
350
- item.is_rss.should be_false
351
- end
352
-
353
- it "does not set the title field" do
354
- item.title.should be_nil
355
- end
356
- end
357
-
358
- context "when attributes assigned without protection" do
359
-
360
- before do
361
- item.assign_attributes(
362
- { title: "Some Title",
363
- is_rss: true,
364
- user_login: "SomeLogin"
365
- }, without_protection: true
366
- )
367
- end
368
-
369
- it "sets the title attribute" do
370
- item.title.should eq("Some Title")
371
- end
372
-
373
- it "sets the user login attribute" do
374
- item.user_login.should eq("SomeLogin")
375
- end
376
-
377
- it "sets the rss attribute" do
378
- item.is_rss.should be_true
379
- end
258
+ it "delegates to another method" do
259
+ expect(object.id).to eq(object.key)
380
260
  end
381
261
  end
382
262
  end
383
263
 
384
- describe "#_id" do
385
-
386
- let(:person) do
387
- Person.new
388
- end
389
-
390
- it "delegates to #id" do
391
- person._id.should eq(person.id)
392
- end
393
- end
394
-
395
264
  describe "#_id=" do
396
265
 
397
266
  after(:all) do
398
267
  Person.field(
399
268
  :_id,
400
- type: Moped::BSON::ObjectId,
269
+ type: BSON::ObjectId,
401
270
  pre_processed: true,
402
- default: ->{ Moped::BSON::ObjectId.new }
271
+ default: ->{ BSON::ObjectId.new },
272
+ overwrite: true
403
273
  )
404
274
  end
405
275
 
@@ -408,9 +278,10 @@ describe Mongoid::Attributes do
408
278
  before(:all) do
409
279
  Person.field(
410
280
  :_id,
411
- type: Moped::BSON::ObjectId,
281
+ type: BSON::ObjectId,
412
282
  pre_processed: true,
413
- default: ->{ Moped::BSON::ObjectId.new }
283
+ default: ->{ BSON::ObjectId.new },
284
+ overwrite: true
414
285
  )
415
286
  end
416
287
 
@@ -419,7 +290,7 @@ describe Mongoid::Attributes do
419
290
  end
420
291
 
421
292
  let(:bson_id) do
422
- Moped::BSON::ObjectId.new
293
+ BSON::ObjectId.new
423
294
  end
424
295
 
425
296
  context "when providing an object id" do
@@ -429,7 +300,7 @@ describe Mongoid::Attributes do
429
300
  end
430
301
 
431
302
  it "sets the id as the object id" do
432
- person.id.should eq(bson_id)
303
+ expect(person.id).to eq(bson_id)
433
304
  end
434
305
  end
435
306
 
@@ -440,7 +311,7 @@ describe Mongoid::Attributes do
440
311
  end
441
312
 
442
313
  it "sets the id as the object id" do
443
- person.id.should eq(bson_id)
314
+ expect(person.id).to eq(bson_id)
444
315
  end
445
316
  end
446
317
 
@@ -451,9 +322,22 @@ describe Mongoid::Attributes do
451
322
  end
452
323
 
453
324
  it "sets the id as the supplied value to_s" do
454
- person.id.should eq(2)
325
+ expect(person.id).to eq(2)
455
326
  end
456
327
  end
328
+
329
+ context "when #id= alias is overridden" do
330
+
331
+ let(:object) do
332
+ IdKey.new(key: 'foo')
333
+ end
334
+
335
+ it "delegates to another method" do
336
+ object.id = 'bar'
337
+ expect(object.id).to eq('bar')
338
+ end
339
+ end
340
+
457
341
  end
458
342
 
459
343
  context "when using string ids" do
@@ -463,7 +347,8 @@ describe Mongoid::Attributes do
463
347
  :_id,
464
348
  type: String,
465
349
  pre_processed: true,
466
- default: ->{ Moped::BSON::ObjectId.new.to_s }
350
+ default: ->{ BSON::ObjectId.new.to_s },
351
+ overwrite: true
467
352
  )
468
353
  end
469
354
 
@@ -472,7 +357,7 @@ describe Mongoid::Attributes do
472
357
  end
473
358
 
474
359
  let(:bson_id) do
475
- Moped::BSON::ObjectId.new
360
+ BSON::ObjectId.new
476
361
  end
477
362
 
478
363
  context "when providing an object id" do
@@ -482,7 +367,7 @@ describe Mongoid::Attributes do
482
367
  end
483
368
 
484
369
  it "sets the id as the string of the object id" do
485
- person.id.should eq(bson_id.to_s)
370
+ expect(person.id).to eq(bson_id.to_s)
486
371
  end
487
372
  end
488
373
 
@@ -493,7 +378,7 @@ describe Mongoid::Attributes do
493
378
  end
494
379
 
495
380
  it "sets the id as the string" do
496
- person.id.should eq(bson_id.to_s)
381
+ expect(person.id).to eq(bson_id.to_s)
497
382
  end
498
383
  end
499
384
 
@@ -504,7 +389,7 @@ describe Mongoid::Attributes do
504
389
  end
505
390
 
506
391
  it "sets the id as the supplied value to_s" do
507
- person.id.should eq("2")
392
+ expect(person.id).to eq("2")
508
393
  end
509
394
  end
510
395
  end
@@ -512,7 +397,7 @@ describe Mongoid::Attributes do
512
397
  context "when using integer ids" do
513
398
 
514
399
  before(:all) do
515
- Person.field(:_id, type: Integer)
400
+ Person.field(:_id, type: Integer, overwrite: true)
516
401
  end
517
402
 
518
403
  let(:person) do
@@ -526,7 +411,7 @@ describe Mongoid::Attributes do
526
411
  end
527
412
 
528
413
  it "sets the id as the integer" do
529
- person.id.should eq(1)
414
+ expect(person.id).to eq(1)
530
415
  end
531
416
  end
532
417
 
@@ -537,7 +422,7 @@ describe Mongoid::Attributes do
537
422
  end
538
423
 
539
424
  it "sets the id as the supplied value" do
540
- person.id.should eq(2)
425
+ expect(person.id).to eq(2)
541
426
  end
542
427
  end
543
428
  end
@@ -553,27 +438,23 @@ describe Mongoid::Attributes do
553
438
  Person.new(attributes)
554
439
  end
555
440
 
556
- before do
557
- Mongoid.configure.allow_dynamic_fields = true
558
- end
559
-
560
441
  context "when an attribute exists" do
561
442
 
562
443
  it "allows the getter" do
563
- person.testing.should eq("Testing")
444
+ expect(person.testing).to eq("Testing")
564
445
  end
565
446
 
566
447
  it "allows the setter" do
567
448
  person.testing = "Test"
568
- person.testing.should eq("Test")
449
+ expect(person.testing).to eq("Test")
569
450
  end
570
451
 
571
452
  it "allows the getter before_type_cast" do
572
- person.testing_before_type_cast.should eq("Testing")
453
+ expect(person.testing_before_type_cast).to eq("Testing")
573
454
  end
574
455
 
575
456
  it "returns true for respond_to?" do
576
- person.respond_to?(:testing).should be_true
457
+ expect(person.respond_to?(:testing)).to be true
577
458
  end
578
459
  end
579
460
 
@@ -588,28 +469,17 @@ describe Mongoid::Attributes do
588
469
  end
589
470
 
590
471
  it "mongoizes the dynamic field" do
591
- person.new_years.should be_a(Time)
472
+ expect(person.new_years).to be_a(Time)
592
473
  end
593
474
 
594
475
  it "keeps the same value" do
595
- person.new_years.should eq(new_years)
476
+ expect(person.new_years).to eq(new_years)
596
477
  end
597
478
  end
598
479
  end
599
480
 
600
481
  describe "#process" do
601
482
 
602
- context "when passing non accessible fields" do
603
-
604
- let(:person) do
605
- Person.new(owner_id: 6)
606
- end
607
-
608
- it "does not set the value" do
609
- person.owner_id.should be_nil
610
- end
611
- end
612
-
613
483
  context "when attributes dont have fields defined" do
614
484
 
615
485
  let(:attributes) do
@@ -626,43 +496,26 @@ describe Mongoid::Attributes do
626
496
  Person.new(attributes)
627
497
  end
628
498
 
629
- before do
630
- Mongoid.configure.allow_dynamic_fields = true
631
- end
632
-
633
499
  context "when attribute is a string" do
634
500
 
635
501
  it "adds the string to the attributes" do
636
- person.attributes["nofieldstring"].should eq("Testing")
502
+ expect(person.attributes["nofieldstring"]).to eq("Testing")
637
503
  end
638
504
  end
639
505
 
640
506
  context "when attribute is not a string" do
641
507
 
642
508
  it "adds a cast value to the attributes" do
643
- person.attributes["nofieldint"].should eq(5)
509
+ expect(person.attributes["nofieldint"]).to eq(5)
644
510
  end
645
511
  end
646
512
  end
647
513
 
648
514
  context "when not allowing dynamic fields" do
649
515
 
650
- let!(:attributes) do
651
- { nofieldstring: "Testing" }
652
- end
653
-
654
- before do
655
- Mongoid.configure.allow_dynamic_fields = false
656
- Person.fields.delete(:nofieldstring)
657
- end
658
-
659
- after do
660
- Mongoid.configure.allow_dynamic_fields = true
661
- end
662
-
663
- it "raises an unknown attribute error" do
516
+ it "raises an unknown attribute error on instantiation" do
664
517
  expect {
665
- Person.new({ anothernew: "Test" })
518
+ Account.new({ anothernew: "Test" })
666
519
  }.to raise_error(Mongoid::Errors::UnknownAttribute)
667
520
  end
668
521
  end
@@ -671,7 +524,7 @@ describe Mongoid::Attributes do
671
524
  context "when supplied hash has string values" do
672
525
 
673
526
  let(:bson_id) do
674
- Moped::BSON::ObjectId.new
527
+ BSON::ObjectId.new
675
528
  end
676
529
 
677
530
  let!(:attributes) do
@@ -695,15 +548,15 @@ describe Mongoid::Attributes do
695
548
  end
696
549
 
697
550
  it "casts integers" do
698
- person[:age].should eq(30)
551
+ expect(person[:age]).to eq(30)
699
552
  end
700
553
 
701
554
  it "casts booleans" do
702
- person[:terms].should be_true
555
+ expect(person[:terms]).to be true
703
556
  end
704
557
 
705
558
  it "sets empty strings to nil" do
706
- person[:score].should be_nil
559
+ expect(person[:score]).to be_nil
707
560
  end
708
561
  end
709
562
 
@@ -724,7 +577,7 @@ describe Mongoid::Attributes do
724
577
  end
725
578
 
726
579
  it "sets the associations" do
727
- person.name.should eq(name)
580
+ expect(person.name).to eq(name)
728
581
  end
729
582
  end
730
583
 
@@ -743,11 +596,11 @@ describe Mongoid::Attributes do
743
596
  end
744
597
 
745
598
  it "sets the parent association" do
746
- person.game.should eq(game)
599
+ expect(person.game).to eq(game)
747
600
  end
748
601
 
749
602
  it "sets the inverse association" do
750
- game.person.should eq(person)
603
+ expect(game.person).to eq(person)
751
604
  end
752
605
  end
753
606
 
@@ -762,7 +615,7 @@ describe Mongoid::Attributes do
762
615
  end
763
616
 
764
617
  it "sets the association" do
765
- name.person.should eq(person)
618
+ expect(name.person).to eq(person)
766
619
  end
767
620
  end
768
621
  end
@@ -782,7 +635,7 @@ describe Mongoid::Attributes do
782
635
  end
783
636
 
784
637
  it "calls the setter for the association" do
785
- person.employer_id.should eq("1")
638
+ expect(person.employer_id).to eq("1")
786
639
  end
787
640
  end
788
641
 
@@ -797,7 +650,7 @@ describe Mongoid::Attributes do
797
650
  end
798
651
 
799
652
  it "sets the empty array" do
800
- person.aliases.should be_empty
653
+ expect(person.aliases).to be_empty
801
654
  end
802
655
  end
803
656
 
@@ -812,7 +665,20 @@ describe Mongoid::Attributes do
812
665
  end
813
666
 
814
667
  it "sets the empty hash" do
815
- person.map.should eq({})
668
+ expect(person.map).to eq({})
669
+ end
670
+ end
671
+
672
+ context "when providing tainted parameters" do
673
+
674
+ let(:params) do
675
+ ActionController::Parameters.new(title: "sir")
676
+ end
677
+
678
+ it "raises an error" do
679
+ expect {
680
+ Person.new(params)
681
+ }.to raise_error(ActiveModel::ForbiddenAttributesError)
816
682
  end
817
683
  end
818
684
  end
@@ -832,7 +698,7 @@ describe Mongoid::Attributes do
832
698
  end
833
699
 
834
700
  it "only overwrites supplied attributes" do
835
- person.title.should eq("Sir")
701
+ expect(person.title).to eq("Sir")
836
702
  end
837
703
  end
838
704
 
@@ -847,8 +713,8 @@ describe Mongoid::Attributes do
847
713
  context "when attribute does not exist" do
848
714
 
849
715
  it "returns the default value" do
850
- person.age.should eq(100)
851
- person.pets.should be_false
716
+ expect(person.age).to eq(100)
717
+ expect(person.pets).to be false
852
718
  end
853
719
 
854
720
  end
@@ -860,7 +726,7 @@ describe Mongoid::Attributes do
860
726
  end
861
727
 
862
728
  it "returns the value" do
863
- person.read_attribute(:owner_id).should eq(5)
729
+ expect(person.read_attribute(:owner_id)).to eq(5)
864
730
  end
865
731
  end
866
732
  end
@@ -883,13 +749,29 @@ describe Mongoid::Attributes do
883
749
  end
884
750
 
885
751
  it "returns the default value" do
886
- person.age.should eq(100)
752
+ expect(person.age).to eq(100)
887
753
  end
888
754
  end
889
755
  end
756
+
757
+ context "when attribute has an aliased name" do
758
+
759
+ let(:person) do
760
+ Person.new
761
+ end
762
+
763
+ before(:each) do
764
+ person.write_attribute(:t, "aliased field to test")
765
+ end
766
+
767
+ it "returns the value of the aliased field" do
768
+ expect(person.read_attribute(:test)).to eq("aliased field to test")
769
+ end
770
+ end
890
771
  end
891
772
 
892
773
  describe "#read_attribute_before_type_cast" do
774
+
893
775
  let(:person) do
894
776
  Person.create
895
777
  end
@@ -897,7 +779,7 @@ describe Mongoid::Attributes do
897
779
  context "when the attribute has not yet been assigned" do
898
780
 
899
781
  it "returns the default value" do
900
- person.age_before_type_cast.should eq(100)
782
+ expect(person.age_before_type_cast).to eq(100)
901
783
  end
902
784
  end
903
785
 
@@ -905,12 +787,11 @@ describe Mongoid::Attributes do
905
787
 
906
788
  it "returns the default value" do
907
789
  person.age = "old"
908
- person.age_before_type_cast.should eq("old")
790
+ expect(person.age_before_type_cast).to eq("old")
909
791
  end
910
792
  end
911
793
  end
912
794
 
913
-
914
795
  describe "#attribute_present?" do
915
796
 
916
797
  context "when document is a new record" do
@@ -922,7 +803,7 @@ describe Mongoid::Attributes do
922
803
  context "when attribute does not exist" do
923
804
 
924
805
  it "returns false" do
925
- person.attribute_present?(:owner_id).should be_false
806
+ expect(person.attribute_present?(:owner_id)).to be false
926
807
  end
927
808
  end
928
809
 
@@ -932,7 +813,7 @@ describe Mongoid::Attributes do
932
813
  end
933
814
 
934
815
  it "returns true" do
935
- person.attribute_present?(:owner_id).should be_true
816
+ expect(person.attribute_present?(:owner_id)).to be true
936
817
  end
937
818
  end
938
819
  end
@@ -955,7 +836,7 @@ describe Mongoid::Attributes do
955
836
  end
956
837
 
957
838
  it "returns true" do
958
- person.attribute_present?(:age).should be_true
839
+ expect(person.attribute_present?(:age)).to be true
959
840
  end
960
841
  end
961
842
  end
@@ -972,7 +853,7 @@ describe Mongoid::Attributes do
972
853
 
973
854
  it "return true" do
974
855
  person.terms = false
975
- person.attribute_present?(:terms).should be_true
856
+ expect(person.attribute_present?(:terms)).to be true
976
857
  end
977
858
  end
978
859
 
@@ -980,7 +861,7 @@ describe Mongoid::Attributes do
980
861
 
981
862
  it "return true" do
982
863
  person.terms = false
983
- person.attribute_present?(:terms).should be_true
864
+ expect(person.attribute_present?(:terms)).to be true
984
865
  end
985
866
  end
986
867
  end
@@ -993,7 +874,19 @@ describe Mongoid::Attributes do
993
874
  end
994
875
 
995
876
  it "return false" do
996
- person.attribute_present?(:title).should be_false
877
+ expect(person.attribute_present?(:title)).to be false
878
+ end
879
+ end
880
+
881
+ context "when the attribute is not on only list" do
882
+
883
+ before { Person.create }
884
+ let(:person) do
885
+ Person.only(:id).first
886
+ end
887
+
888
+ it "return false" do
889
+ expect(person.attribute_present?(:foobar)).to be false
997
890
  end
998
891
  end
999
892
  end
@@ -1009,14 +902,14 @@ describe Mongoid::Attributes do
1009
902
  context "when provided a symbol" do
1010
903
 
1011
904
  it "returns true" do
1012
- person.has_attribute?(:title).should be_true
905
+ expect(person.has_attribute?(:title)).to be true
1013
906
  end
1014
907
  end
1015
908
 
1016
909
  context "when provided a string" do
1017
910
 
1018
911
  it "returns true" do
1019
- person.has_attribute?("title").should be_true
912
+ expect(person.has_attribute?("title")).to be true
1020
913
  end
1021
914
  end
1022
915
  end
@@ -1024,7 +917,7 @@ describe Mongoid::Attributes do
1024
917
  context "when the key is not in the attributes" do
1025
918
 
1026
919
  it "returns false" do
1027
- person.has_attribute?(:employer_id).should be_false
920
+ expect(person.has_attribute?(:employer_id)).to be false
1028
921
  end
1029
922
  end
1030
923
  end
@@ -1038,7 +931,7 @@ describe Mongoid::Attributes do
1038
931
  context "before the attribute has been assigned" do
1039
932
 
1040
933
  it "returns false" do
1041
- person.has_attribute_before_type_cast?(:age).should be_false
934
+ expect(person.has_attribute_before_type_cast?(:age)).to be false
1042
935
  end
1043
936
  end
1044
937
 
@@ -1046,7 +939,7 @@ describe Mongoid::Attributes do
1046
939
 
1047
940
  it "returns true" do
1048
941
  person.age = 'old'
1049
- person.has_attribute_before_type_cast?(:age).should be_true
942
+ expect(person.has_attribute_before_type_cast?(:age)).to be true
1050
943
  end
1051
944
  end
1052
945
  end
@@ -1064,11 +957,11 @@ describe Mongoid::Attributes do
1064
957
  end
1065
958
 
1066
959
  it "removes the attribute" do
1067
- person.title.should be_nil
960
+ expect(person.title).to be_nil
1068
961
  end
1069
962
 
1070
963
  it "removes the key from the attributes hash" do
1071
- person.has_attribute?(:title).should be_false
964
+ expect(person.has_attribute?(:title)).to be false
1072
965
  end
1073
966
 
1074
967
  context "when saving after the removal" do
@@ -1078,7 +971,7 @@ describe Mongoid::Attributes do
1078
971
  end
1079
972
 
1080
973
  it "persists the removal" do
1081
- person.reload.has_attribute?(:title).should be_false
974
+ expect(person.reload.has_attribute?(:title)).to be false
1082
975
  end
1083
976
  end
1084
977
  end
@@ -1099,11 +992,11 @@ describe Mongoid::Attributes do
1099
992
  end
1100
993
 
1101
994
  it "removes the attribute" do
1102
- person.pet.name.should be_nil
995
+ expect(person.pet.name).to be_nil
1103
996
  end
1104
997
 
1105
998
  it "removes the key from the attributes hash" do
1106
- person.pet.has_attribute?(:name).should be_false
999
+ expect(person.pet.has_attribute?(:name)).to be false
1107
1000
  end
1108
1001
 
1109
1002
  context "when saving after the removal" do
@@ -1113,7 +1006,7 @@ describe Mongoid::Attributes do
1113
1006
  end
1114
1007
 
1115
1008
  it "persists the removal" do
1116
- person.reload.pet.has_attribute?(:name).should be_false
1009
+ expect(person.reload.pet.has_attribute?(:name)).to be false
1117
1010
  end
1118
1011
  end
1119
1012
 
@@ -1130,7 +1023,7 @@ describe Mongoid::Attributes do
1130
1023
  end
1131
1024
 
1132
1025
  it "does not fail" do
1133
- person.title.should be_nil
1026
+ expect(person.title).to be_nil
1134
1027
  end
1135
1028
  end
1136
1029
 
@@ -1145,7 +1038,7 @@ describe Mongoid::Attributes do
1145
1038
  end
1146
1039
 
1147
1040
  it "does not add a delayed unset operation" do
1148
- person.delayed_atomic_unsets.should be_empty
1041
+ expect(person.delayed_atomic_unsets).to be_empty
1149
1042
  end
1150
1043
  end
1151
1044
  end
@@ -1158,10 +1051,6 @@ describe Mongoid::Attributes do
1158
1051
  Person.new
1159
1052
  end
1160
1053
 
1161
- before(:all) do
1162
- Mongoid.allow_dynamic_fields = true
1163
- end
1164
-
1165
1054
  context "when asking for the getter" do
1166
1055
 
1167
1056
  context "when the attribute exists" do
@@ -1171,14 +1060,14 @@ describe Mongoid::Attributes do
1171
1060
  end
1172
1061
 
1173
1062
  it "returns true" do
1174
- person.should respond_to(:attr)
1063
+ expect(person).to respond_to(:attr)
1175
1064
  end
1176
1065
  end
1177
1066
 
1178
1067
  context "when the attribute does not exist" do
1179
1068
 
1180
1069
  it "returns false" do
1181
- person.should_not respond_to(:attr)
1070
+ expect(person).to_not respond_to(:attr)
1182
1071
  end
1183
1072
  end
1184
1073
  end
@@ -1192,14 +1081,14 @@ describe Mongoid::Attributes do
1192
1081
  end
1193
1082
 
1194
1083
  it "returns true" do
1195
- person.should respond_to(:attr=)
1084
+ expect(person).to respond_to(:attr=)
1196
1085
  end
1197
1086
  end
1198
1087
 
1199
1088
  context "when the attribute does not exist" do
1200
1089
 
1201
1090
  it "returns false" do
1202
- person.should_not respond_to(:attr=)
1091
+ expect(person).to_not respond_to(:attr=)
1203
1092
  end
1204
1093
  end
1205
1094
  end
@@ -1207,29 +1096,21 @@ describe Mongoid::Attributes do
1207
1096
 
1208
1097
  context "when not allowing dynamic fields" do
1209
1098
 
1210
- let(:person) do
1211
- Person.new
1212
- end
1213
-
1214
- before(:all) do
1215
- Mongoid.allow_dynamic_fields = false
1216
- end
1217
-
1218
- after(:all) do
1219
- Mongoid.allow_dynamic_fields = true
1099
+ let(:bar) do
1100
+ Bar.new
1220
1101
  end
1221
1102
 
1222
1103
  context "when asking for the getter" do
1223
1104
 
1224
1105
  it "returns false" do
1225
- person.should_not respond_to(:attr)
1106
+ expect(bar).to_not respond_to(:attr)
1226
1107
  end
1227
1108
  end
1228
1109
 
1229
1110
  context "when asking for the setter" do
1230
1111
 
1231
1112
  it "returns false" do
1232
- person.should_not respond_to(:attr=)
1113
+ expect(bar).to_not respond_to(:attr=)
1233
1114
  end
1234
1115
  end
1235
1116
  end
@@ -1244,7 +1125,7 @@ describe Mongoid::Attributes do
1244
1125
  end
1245
1126
 
1246
1127
  it "returns the default value" do
1247
- person.age.should eq(100)
1128
+ expect(person.age).to eq(100)
1248
1129
  end
1249
1130
  end
1250
1131
 
@@ -1254,7 +1135,7 @@ describe Mongoid::Attributes do
1254
1135
  end
1255
1136
 
1256
1137
  it "should store the attribute before type cast" do
1257
- person.age_before_type_cast.should eq("old")
1138
+ expect(person.age_before_type_cast).to eq("old")
1258
1139
  end
1259
1140
  end
1260
1141
 
@@ -1265,7 +1146,7 @@ describe Mongoid::Attributes do
1265
1146
  end
1266
1147
 
1267
1148
  it "does not use the default value" do
1268
- person.age.should be_nil
1149
+ expect(person.age).to be_nil
1269
1150
  end
1270
1151
  end
1271
1152
 
@@ -1280,7 +1161,7 @@ describe Mongoid::Attributes do
1280
1161
  end
1281
1162
 
1282
1163
  it "allows overwriting of the default value" do
1283
- person.terms.should be_true
1164
+ expect(person.terms).to be true
1284
1165
  end
1285
1166
  end
1286
1167
 
@@ -1295,7 +1176,35 @@ describe Mongoid::Attributes do
1295
1176
  end
1296
1177
 
1297
1178
  it "allows the field name to be udpated" do
1298
- person.t.should eq("aliased field to test")
1179
+ expect(person.t).to eq("aliased field to test")
1180
+ end
1181
+ end
1182
+
1183
+ context "when attribute is a Hash" do
1184
+ let(:person) { Person.new map: { somekey: "somevalue" } }
1185
+
1186
+ it "raises an error when try to set an invalid value" do
1187
+ expect {
1188
+ person.map = []
1189
+ }.to raise_error(Mongoid::Errors::InvalidValue)
1190
+ end
1191
+
1192
+ it "can set a Hash value" do
1193
+ expect(person.map).to eq( { somekey: "somevalue" } )
1194
+ end
1195
+ end
1196
+
1197
+ context "when attribute is an Array" do
1198
+ let(:person) { Person.new aliases: [ :alias_1 ] }
1199
+
1200
+ it "can set an Array Value" do
1201
+ expect(person.aliases).to eq([ :alias_1 ])
1202
+ end
1203
+
1204
+ it "raises an error when try to set an invalid value" do
1205
+ expect {
1206
+ person.aliases = {}
1207
+ }.to raise_error(Mongoid::Errors::InvalidValue)
1299
1208
  end
1300
1209
  end
1301
1210
  end
@@ -1316,11 +1225,11 @@ describe Mongoid::Attributes do
1316
1225
  context "when the key has not been specified as a field" do
1317
1226
 
1318
1227
  before do
1319
- person.stub(:fields).and_return({})
1228
+ allow(person).to receive(:fields).and_return({})
1320
1229
  end
1321
1230
 
1322
1231
  it "returns the value" do
1323
- person.send(:typed_value_for, "age", "51").should eq("51")
1232
+ person.send(:typed_value_for, "age", expect("51")).to eq("51")
1324
1233
  end
1325
1234
  end
1326
1235
  end
@@ -1332,7 +1241,7 @@ describe Mongoid::Attributes do
1332
1241
  end
1333
1242
 
1334
1243
  it "typecasts proc values" do
1335
- person.age.should eq(100)
1244
+ expect(person.age).to eq(100)
1336
1245
  end
1337
1246
  end
1338
1247
 
@@ -1351,7 +1260,7 @@ describe Mongoid::Attributes do
1351
1260
  end
1352
1261
 
1353
1262
  it "sets nested documents" do
1354
- person.videos.first.title.should eq("Fight Club")
1263
+ expect(person.videos.first.title).to eq("Fight Club")
1355
1264
  end
1356
1265
  end
1357
1266
 
@@ -1372,7 +1281,7 @@ describe Mongoid::Attributes do
1372
1281
  end
1373
1282
 
1374
1283
  it "properly casts values" do
1375
- person.age.should eq(50)
1284
+ expect(person.age).to eq(50)
1376
1285
  end
1377
1286
  end
1378
1287
 
@@ -1383,11 +1292,38 @@ describe Mongoid::Attributes do
1383
1292
  end
1384
1293
 
1385
1294
  it "does not set anything" do
1386
- person.age.should eq(100)
1295
+ expect(person.age).to eq(100)
1387
1296
  end
1388
1297
  end
1389
1298
  end
1390
1299
 
1300
+ context "copying from instance" do
1301
+
1302
+ let(:person) do
1303
+ Person.new
1304
+ end
1305
+
1306
+ let(:instance) do
1307
+ Person.new(attributes)
1308
+ end
1309
+
1310
+ let(:attributes) do
1311
+ { age: 50, range: 1..100 }
1312
+ end
1313
+
1314
+ before do
1315
+ person.send(method, instance.attributes)
1316
+ end
1317
+
1318
+ it "properly copies values" do
1319
+ expect(person.age).to eq(50)
1320
+ end
1321
+
1322
+ it "properly copies ranges" do
1323
+ expect(person.range).to eq(1..100)
1324
+ end
1325
+ end
1326
+
1391
1327
  context "on a parent document" do
1392
1328
 
1393
1329
  context "when the parent has a has many through a has one" do
@@ -1411,8 +1347,8 @@ describe Mongoid::Attributes do
1411
1347
  end
1412
1348
 
1413
1349
  it "does not overwrite child attributes if not in the hash" do
1414
- owner.pet.name.should eq("Bingo")
1415
- owner.pet.vet_visits.size.should eq(1)
1350
+ expect(owner.pet.name).to eq("Bingo")
1351
+ expect(owner.pet.vet_visits.size).to eq(1)
1416
1352
  end
1417
1353
  end
1418
1354
 
@@ -1436,11 +1372,11 @@ describe Mongoid::Attributes do
1436
1372
  end
1437
1373
 
1438
1374
  it "has 3 new entries" do
1439
- pet.vet_visits.count.should eq(3)
1375
+ expect(pet.vet_visits.count).to eq(3)
1440
1376
  end
1441
1377
 
1442
1378
  it "persists the changes" do
1443
- pet.reload.vet_visits.count.should eq(3)
1379
+ expect(pet.reload.vet_visits.count).to eq(3)
1444
1380
  end
1445
1381
  end
1446
1382
 
@@ -1478,7 +1414,7 @@ describe Mongoid::Attributes do
1478
1414
  end
1479
1415
 
1480
1416
  it "sets the child attributes on the parent" do
1481
- name.attributes.should eq(
1417
+ expect(name.attributes).to eq(
1482
1418
  { "_id" => "Test-User", "first_name" => "Test2", "last_name" => "User2" }
1483
1419
  )
1484
1420
  end
@@ -1500,7 +1436,7 @@ describe Mongoid::Attributes do
1500
1436
  end
1501
1437
 
1502
1438
  it "updates the child attributes on the parent" do
1503
- address.attributes.should eq(
1439
+ expect(address.attributes).to eq(
1504
1440
  { "_id" => "test", "street" => "Test2" }
1505
1441
  )
1506
1442
  end
@@ -1521,38 +1457,42 @@ describe Mongoid::Attributes do
1521
1457
  product.cost = 500
1522
1458
  end
1523
1459
 
1460
+ it "adds the alias for criteria" do
1461
+ expect(Product.where(cost: 500).selector).to eq("price" => 500)
1462
+ end
1463
+
1524
1464
  it "aliases the getter" do
1525
- product.cost.should eq(500)
1465
+ expect(product.cost).to eq(500)
1526
1466
  end
1527
1467
 
1528
1468
  it "aliases the existance check" do
1529
- product.cost?.should be_true
1469
+ expect(product.cost?).to be true
1530
1470
  end
1531
1471
 
1532
1472
  it "aliases *_changed?" do
1533
- product.cost_changed?.should be_true
1473
+ expect(product.cost_changed?).to be true
1534
1474
  end
1535
1475
 
1536
1476
  it "aliases *_change" do
1537
- product.cost_change.should eq([ nil, 500 ])
1477
+ expect(product.cost_change).to eq([ nil, 500 ])
1538
1478
  end
1539
1479
 
1540
1480
  it "aliases *_will_change!" do
1541
- product.should respond_to(:cost_will_change!)
1481
+ expect(product).to respond_to(:cost_will_change!)
1542
1482
  end
1543
1483
 
1544
1484
  it "aliases *_was" do
1545
- product.cost_was.should be_nil
1485
+ expect(product.cost_was).to be_nil
1546
1486
  end
1547
1487
 
1548
1488
  it "aliases reset_*!" do
1549
1489
  product.reset_cost!
1550
- product.cost.should be_nil
1490
+ expect(product.cost).to be_nil
1551
1491
  end
1552
1492
 
1553
1493
  it "aliases *_before_type_cast" do
1554
1494
  product.cost = "expensive"
1555
- product.cost_before_type_cast.should eq("expensive")
1495
+ expect(product.cost_before_type_cast).to eq("expensive")
1556
1496
  end
1557
1497
  end
1558
1498
 
@@ -1563,32 +1503,32 @@ describe Mongoid::Attributes do
1563
1503
  end
1564
1504
 
1565
1505
  it "aliases the getter" do
1566
- product.price.should eq(500)
1506
+ expect(product.price).to eq(500)
1567
1507
  end
1568
1508
 
1569
1509
  it "aliases the existance check" do
1570
- product.price?.should be_true
1510
+ expect(product.price?).to be true
1571
1511
  end
1572
1512
 
1573
1513
  it "aliases *_changed?" do
1574
- product.price_changed?.should be_true
1514
+ expect(product.price_changed?).to be true
1575
1515
  end
1576
1516
 
1577
1517
  it "aliases *_change" do
1578
- product.price_change.should eq([ nil, 500 ])
1518
+ expect(product.price_change).to eq([ nil, 500 ])
1579
1519
  end
1580
1520
 
1581
1521
  it "aliases *_will_change!" do
1582
- product.should respond_to(:price_will_change!)
1522
+ expect(product).to respond_to(:price_will_change!)
1583
1523
  end
1584
1524
 
1585
1525
  it "aliases *_was" do
1586
- product.price_was.should be_nil
1526
+ expect(product.price_was).to be_nil
1587
1527
  end
1588
1528
 
1589
1529
  it "aliases reset_*!" do
1590
1530
  product.reset_price!
1591
- product.price.should be_nil
1531
+ expect(product.price).to be_nil
1592
1532
  end
1593
1533
  end
1594
1534
  end
@@ -1600,7 +1540,7 @@ describe Mongoid::Attributes do
1600
1540
  end
1601
1541
 
1602
1542
  it "has an entry in the attributes" do
1603
- person.reload.attributes.should have_key("score")
1543
+ expect(person.reload.attributes).to have_key("score")
1604
1544
  end
1605
1545
  end
1606
1546
 
@@ -1612,7 +1552,7 @@ describe Mongoid::Attributes do
1612
1552
 
1613
1553
  it "saves the default" do
1614
1554
  expect { person.save }.to_not raise_error
1615
- person.last_drink_taken_at.should eq(1.day.ago.in_time_zone("Alaska").to_date)
1555
+ expect(person.last_drink_taken_at).to eq(1.day.ago.in_time_zone("Alaska").to_date)
1616
1556
  end
1617
1557
  end
1618
1558
 
@@ -1625,7 +1565,7 @@ describe Mongoid::Attributes do
1625
1565
  end
1626
1566
 
1627
1567
  it "applies the default value" do
1628
- person.last_drink_taken_at.should eq(1.day.ago.in_time_zone("Alaska").to_date)
1568
+ expect(person.last_drink_taken_at).to eq(1.day.ago.in_time_zone("Alaska").to_date)
1629
1569
  end
1630
1570
  end
1631
1571
 
@@ -1642,7 +1582,7 @@ describe Mongoid::Attributes do
1642
1582
  end
1643
1583
 
1644
1584
  it "does not set the default" do
1645
- from_db.age.should eq(50)
1585
+ expect(from_db.age).to eq(50)
1646
1586
  end
1647
1587
  end
1648
1588
 
@@ -1657,7 +1597,7 @@ describe Mongoid::Attributes do
1657
1597
  end
1658
1598
 
1659
1599
  it "does not set the default" do
1660
- from_db.age.should be_nil
1600
+ expect(from_db.age).to be_nil
1661
1601
  end
1662
1602
  end
1663
1603
 
@@ -1672,32 +1612,7 @@ describe Mongoid::Attributes do
1672
1612
  end
1673
1613
 
1674
1614
  it "applies the defaults after all attributes are set" do
1675
- from_db.should be_balanced
1676
- end
1677
- end
1678
- end
1679
- end
1680
-
1681
- context "when dynamic fields are not allowed" do
1682
-
1683
- before do
1684
- Mongoid.configure.allow_dynamic_fields = false
1685
- end
1686
-
1687
- after do
1688
- Mongoid.configure.allow_dynamic_fields = true
1689
- end
1690
-
1691
- context "when an embedded document has been persisted" do
1692
-
1693
- context "when the field is no longer recognized" do
1694
-
1695
- before do
1696
- Person.collection.insert 'pet' => { 'unrecognized_field' => true }
1697
- end
1698
-
1699
- it "allows access to the legacy data" do
1700
- Person.first.pet.read_attribute(:unrecognized_field).should be_true
1615
+ expect(from_db).to be_balanced
1701
1616
  end
1702
1617
  end
1703
1618
  end