mongoid 7.1.0.rc0 → 7.2.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (508) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/CHANGELOG.md +6 -6
  5. data/LICENSE +1 -1
  6. data/README.md +14 -7
  7. data/Rakefile +21 -0
  8. data/lib/config/locales/en.yml +42 -6
  9. data/lib/mongoid.rb +26 -2
  10. data/lib/mongoid/association/accessors.rb +37 -2
  11. data/lib/mongoid/association/depending.rb +1 -6
  12. data/lib/mongoid/association/embedded/embeds_many.rb +2 -1
  13. data/lib/mongoid/association/embedded/embeds_one.rb +2 -1
  14. data/lib/mongoid/association/many.rb +5 -4
  15. data/lib/mongoid/association/proxy.rb +6 -4
  16. data/lib/mongoid/association/referenced/belongs_to/binding.rb +1 -1
  17. data/lib/mongoid/association/referenced/belongs_to/buildable.rb +1 -1
  18. data/lib/mongoid/association/referenced/belongs_to/eager.rb +38 -2
  19. data/lib/mongoid/association/referenced/eager.rb +29 -9
  20. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +1 -1
  21. data/lib/mongoid/association/referenced/has_many/enumerable.rb +30 -15
  22. data/lib/mongoid/association/referenced/has_many/proxy.rb +11 -3
  23. data/lib/mongoid/association/referenced/has_one/buildable.rb +1 -1
  24. data/lib/mongoid/atomic.rb +13 -3
  25. data/lib/mongoid/atomic/modifiers.rb +1 -1
  26. data/lib/mongoid/attributes.rb +28 -20
  27. data/lib/mongoid/attributes/dynamic.rb +1 -1
  28. data/lib/mongoid/clients/factory.rb +19 -2
  29. data/lib/mongoid/clients/options.rb +8 -8
  30. data/lib/mongoid/clients/sessions.rb +20 -4
  31. data/lib/mongoid/clients/storage_options.rb +5 -5
  32. data/lib/mongoid/composable.rb +1 -0
  33. data/lib/mongoid/config.rb +45 -12
  34. data/lib/mongoid/config/options.rb +5 -2
  35. data/lib/mongoid/contextual.rb +5 -4
  36. data/lib/mongoid/contextual/geo_near.rb +3 -2
  37. data/lib/mongoid/contextual/map_reduce.rb +3 -2
  38. data/lib/mongoid/contextual/mongo.rb +25 -5
  39. data/lib/mongoid/copyable.rb +2 -2
  40. data/lib/mongoid/criteria.rb +80 -11
  41. data/lib/mongoid/criteria/modifiable.rb +2 -1
  42. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +1 -1
  43. data/lib/mongoid/criteria/queryable/extensions/regexp.rb +6 -6
  44. data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +12 -0
  45. data/lib/mongoid/criteria/queryable/key.rb +1 -1
  46. data/lib/mongoid/criteria/queryable/mergeable.rb +75 -8
  47. data/lib/mongoid/criteria/queryable/pipeline.rb +3 -2
  48. data/lib/mongoid/criteria/queryable/selectable.rb +141 -19
  49. data/lib/mongoid/criteria/queryable/selector.rb +3 -3
  50. data/lib/mongoid/criteria/queryable/storable.rb +106 -101
  51. data/lib/mongoid/document.rb +15 -3
  52. data/lib/mongoid/errors.rb +7 -0
  53. data/lib/mongoid/errors/eager_load.rb +2 -0
  54. data/lib/mongoid/errors/invalid_discriminator_key_target.rb +25 -0
  55. data/lib/mongoid/errors/invalid_elem_match_operator.rb +33 -0
  56. data/lib/mongoid/errors/invalid_estimated_count_criteria.rb +26 -0
  57. data/lib/mongoid/errors/invalid_expression_operator.rb +28 -0
  58. data/lib/mongoid/errors/invalid_field_operator.rb +33 -0
  59. data/lib/mongoid/errors/invalid_query.rb +41 -0
  60. data/lib/mongoid/errors/no_client_config.rb +2 -2
  61. data/lib/mongoid/errors/no_default_client.rb +1 -1
  62. data/lib/mongoid/extensions/hash.rb +4 -2
  63. data/lib/mongoid/extensions/regexp.rb +1 -1
  64. data/lib/mongoid/factory.rb +27 -10
  65. data/lib/mongoid/fields.rb +2 -1
  66. data/lib/mongoid/fields/standard.rb +2 -1
  67. data/lib/mongoid/fields/validators/macro.rb +26 -10
  68. data/lib/mongoid/findable.rb +55 -18
  69. data/lib/mongoid/indexable.rb +2 -2
  70. data/lib/mongoid/interceptable.rb +5 -1
  71. data/lib/mongoid/matchable.rb +1 -149
  72. data/lib/mongoid/matcher.rb +127 -0
  73. data/lib/mongoid/matcher/all.rb +22 -0
  74. data/lib/mongoid/matcher/and.rb +21 -0
  75. data/lib/mongoid/matcher/elem_match.rb +35 -0
  76. data/lib/mongoid/matcher/elem_match_expression.rb +20 -0
  77. data/lib/mongoid/matcher/eq.rb +11 -0
  78. data/lib/mongoid/matcher/eq_impl.rb +32 -0
  79. data/lib/mongoid/matcher/eq_impl_with_regexp.rb +21 -0
  80. data/lib/mongoid/matcher/exists.rb +15 -0
  81. data/lib/mongoid/matcher/expression.rb +40 -0
  82. data/lib/mongoid/matcher/expression_operator.rb +19 -0
  83. data/lib/mongoid/matcher/field_expression.rb +63 -0
  84. data/lib/mongoid/matcher/field_operator.rb +52 -0
  85. data/lib/mongoid/matcher/gt.rb +17 -0
  86. data/lib/mongoid/matcher/gte.rb +17 -0
  87. data/lib/mongoid/matcher/in.rb +25 -0
  88. data/lib/mongoid/matcher/lt.rb +17 -0
  89. data/lib/mongoid/matcher/lte.rb +17 -0
  90. data/lib/mongoid/matcher/ne.rb +16 -0
  91. data/lib/mongoid/matcher/nin.rb +11 -0
  92. data/lib/mongoid/matcher/nor.rb +25 -0
  93. data/lib/mongoid/matcher/not.rb +29 -0
  94. data/lib/mongoid/matcher/or.rb +21 -0
  95. data/lib/mongoid/matcher/regex.rb +41 -0
  96. data/lib/mongoid/matcher/size.rb +26 -0
  97. data/lib/mongoid/persistable/deletable.rb +1 -1
  98. data/lib/mongoid/persistable/pushable.rb +11 -2
  99. data/lib/mongoid/persistence_context.rb +6 -6
  100. data/lib/mongoid/query_cache.rb +83 -38
  101. data/lib/mongoid/railties/database.rake +7 -0
  102. data/lib/mongoid/reloadable.rb +2 -2
  103. data/lib/mongoid/serializable.rb +11 -3
  104. data/lib/mongoid/shardable.rb +56 -4
  105. data/lib/mongoid/tasks/database.rake +10 -5
  106. data/lib/mongoid/tasks/database.rb +83 -0
  107. data/lib/mongoid/timestamps/timeless.rb +3 -1
  108. data/lib/mongoid/traversable.rb +111 -4
  109. data/lib/mongoid/validatable/uniqueness.rb +1 -1
  110. data/lib/mongoid/version.rb +1 -1
  111. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +32 -23
  112. data/lib/rails/generators/mongoid/model/templates/model.rb.tt +1 -1
  113. data/spec/integration/app_spec.rb +254 -0
  114. data/spec/integration/associations/embedded_spec.rb +148 -0
  115. data/spec/integration/associations/has_many_spec.rb +69 -0
  116. data/spec/integration/associations/has_one_spec.rb +69 -0
  117. data/spec/integration/associations/nested_attributes_assignment_spec.rb +116 -0
  118. data/spec/integration/atomic/modifiers_spec.rb +117 -0
  119. data/spec/integration/bson_regexp_raw_spec.rb +20 -0
  120. data/spec/integration/criteria/date_field_spec.rb +41 -0
  121. data/spec/integration/criteria/logical_spec.rb +13 -0
  122. data/spec/integration/discriminator_key_spec.rb +354 -0
  123. data/spec/integration/discriminator_value_spec.rb +207 -0
  124. data/spec/integration/document_spec.rb +22 -0
  125. data/spec/integration/{matchable_spec.rb → matcher_examples_spec.rb} +120 -41
  126. data/spec/integration/matcher_operator_data/all.yml +140 -0
  127. data/spec/integration/matcher_operator_data/and.yml +93 -0
  128. data/spec/integration/matcher_operator_data/elem_match.yml +363 -0
  129. data/spec/integration/matcher_operator_data/elem_match_expr.yml +213 -0
  130. data/spec/integration/matcher_operator_data/eq.yml +191 -0
  131. data/spec/integration/matcher_operator_data/exists.yml +213 -0
  132. data/spec/integration/matcher_operator_data/generic_op.yml +17 -0
  133. data/spec/integration/matcher_operator_data/gt.yml +132 -0
  134. data/spec/integration/matcher_operator_data/gte.yml +132 -0
  135. data/spec/integration/matcher_operator_data/implicit.yml +331 -0
  136. data/spec/integration/matcher_operator_data/implicit_traversal.yml +16 -0
  137. data/spec/integration/matcher_operator_data/in.yml +194 -0
  138. data/spec/integration/matcher_operator_data/invalid_op.yml +59 -0
  139. data/spec/integration/matcher_operator_data/invalid_syntax.yml +39 -0
  140. data/spec/integration/matcher_operator_data/lt.yml +132 -0
  141. data/spec/integration/matcher_operator_data/lte.yml +132 -0
  142. data/spec/integration/matcher_operator_data/multiple.yml +29 -0
  143. data/spec/integration/matcher_operator_data/ne.yml +150 -0
  144. data/spec/integration/matcher_operator_data/nin.yml +114 -0
  145. data/spec/integration/matcher_operator_data/nor.yml +126 -0
  146. data/spec/integration/matcher_operator_data/not.yml +196 -0
  147. data/spec/integration/matcher_operator_data/or.yml +137 -0
  148. data/spec/integration/matcher_operator_data/regex.yml +174 -0
  149. data/spec/integration/matcher_operator_data/regex_options.yml +72 -0
  150. data/spec/integration/matcher_operator_data/size.yml +174 -0
  151. data/spec/integration/matcher_operator_spec.rb +100 -0
  152. data/spec/integration/matcher_spec.rb +189 -0
  153. data/spec/integration/server_query_spec.rb +142 -0
  154. data/spec/integration/shardable_spec.rb +149 -0
  155. data/spec/lite_spec_helper.rb +15 -4
  156. data/spec/mongoid/association/accessors_spec.rb +238 -63
  157. data/spec/mongoid/association/depending_spec.rb +78 -26
  158. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +33 -0
  159. data/spec/mongoid/association/embedded/embeds_many_models.rb +20 -0
  160. data/spec/mongoid/association/embedded/embeds_many_spec.rb +10 -0
  161. data/spec/mongoid/association/embedded/embeds_one_models.rb +1 -0
  162. data/spec/mongoid/association/embedded/embeds_one_spec.rb +0 -2
  163. data/spec/mongoid/association/referenced/belongs_to/eager_spec.rb +193 -10
  164. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +140 -1
  165. data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +146 -68
  166. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +462 -187
  167. data/spec/mongoid/association/referenced/has_many_models.rb +9 -0
  168. data/spec/mongoid/association/referenced/has_one_models.rb +9 -0
  169. data/spec/mongoid/atomic/modifiers_spec.rb +47 -0
  170. data/spec/mongoid/atomic_spec.rb +23 -0
  171. data/spec/mongoid/attributes/nested_spec.rb +1 -1
  172. data/spec/mongoid/attributes_spec.rb +19 -7
  173. data/spec/mongoid/changeable_spec.rb +23 -0
  174. data/spec/mongoid/clients/factory_spec.rb +45 -8
  175. data/spec/mongoid/clients/options_spec.rb +11 -11
  176. data/spec/mongoid/clients/sessions_spec.rb +9 -10
  177. data/spec/mongoid/clients/transactions_spec.rb +20 -8
  178. data/spec/mongoid/clients_spec.rb +2 -2
  179. data/spec/mongoid/config_spec.rb +28 -0
  180. data/spec/mongoid/contextual/atomic_spec.rb +22 -11
  181. data/spec/mongoid/contextual/geo_near_spec.rb +11 -2
  182. data/spec/mongoid/contextual/map_reduce_spec.rb +20 -5
  183. data/spec/mongoid/contextual/mongo_spec.rb +146 -69
  184. data/spec/mongoid/copyable_spec.rb +153 -1
  185. data/spec/mongoid/copyable_spec_models.rb +14 -0
  186. data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +1 -1
  187. data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +7 -7
  188. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +1 -1
  189. data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +19 -7
  190. data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +28 -1
  191. data/spec/mongoid/criteria/queryable/mergeable_spec.rb +45 -12
  192. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +1051 -392
  193. data/spec/mongoid/criteria/queryable/selectable_spec.rb +136 -82
  194. data/spec/mongoid/criteria/queryable/storable_spec.rb +80 -2
  195. data/spec/mongoid/criteria_spec.rb +240 -41
  196. data/spec/mongoid/document_fields_spec.rb +29 -0
  197. data/spec/mongoid/document_persistence_context_spec.rb +33 -0
  198. data/spec/mongoid/document_spec.rb +89 -15
  199. data/spec/mongoid/errors/delete_restriction_spec.rb +1 -1
  200. data/spec/mongoid/errors/no_client_config_spec.rb +2 -2
  201. data/spec/mongoid/errors/no_client_database_spec.rb +3 -3
  202. data/spec/mongoid/errors/no_client_hosts_spec.rb +3 -3
  203. data/spec/mongoid/factory_spec.rb +261 -28
  204. data/spec/mongoid/fields_spec.rb +72 -3
  205. data/spec/mongoid/findable_spec.rb +32 -0
  206. data/spec/mongoid/indexable_spec.rb +34 -6
  207. data/spec/mongoid/inspectable_spec.rb +29 -2
  208. data/spec/mongoid/interceptable_spec.rb +64 -2
  209. data/spec/mongoid/interceptable_spec_models.rb +76 -0
  210. data/spec/mongoid/matcher/extract_attribute_data/traversal.yml +259 -0
  211. data/spec/mongoid/matcher/extract_attribute_spec.rb +47 -0
  212. data/spec/mongoid/persistable/creatable_spec.rb +108 -25
  213. data/spec/mongoid/persistable/deletable_spec.rb +86 -0
  214. data/spec/mongoid/persistable/pushable_spec.rb +55 -1
  215. data/spec/mongoid/persistable/savable_spec.rb +174 -16
  216. data/spec/mongoid/query_cache_middleware_spec.rb +16 -3
  217. data/spec/mongoid/query_cache_spec.rb +498 -29
  218. data/spec/mongoid/relations/proxy_spec.rb +1 -1
  219. data/spec/mongoid/reloadable_spec.rb +72 -0
  220. data/spec/mongoid/scopable_spec.rb +2 -1
  221. data/spec/mongoid/serializable_spec.rb +149 -20
  222. data/spec/mongoid/shardable_models.rb +61 -0
  223. data/spec/mongoid/shardable_spec.rb +69 -16
  224. data/spec/mongoid/tasks/database_rake_spec.rb +13 -13
  225. data/spec/mongoid/tasks/database_spec.rb +1 -1
  226. data/spec/mongoid/traversable_spec.rb +1100 -0
  227. data/spec/spec_helper.rb +4 -33
  228. data/spec/support/child_process_helper.rb +79 -0
  229. data/spec/support/cluster_config.rb +3 -3
  230. data/spec/support/constraints.rb +50 -10
  231. data/spec/support/expectations.rb +3 -1
  232. data/spec/support/helpers.rb +11 -0
  233. data/spec/support/lite_constraints.rb +22 -0
  234. data/spec/{app → support}/models/account.rb +0 -0
  235. data/spec/{app → support}/models/acolyte.rb +0 -0
  236. data/spec/{app → support}/models/actor.rb +1 -1
  237. data/spec/{app → support}/models/actress.rb +0 -0
  238. data/spec/{app → support}/models/address.rb +0 -0
  239. data/spec/{app → support}/models/address_component.rb +0 -0
  240. data/spec/{app → support}/models/address_number.rb +0 -0
  241. data/spec/{app → support}/models/agency.rb +0 -0
  242. data/spec/{app → support}/models/agent.rb +0 -0
  243. data/spec/{app → support}/models/album.rb +0 -0
  244. data/spec/{app → support}/models/alert.rb +0 -0
  245. data/spec/{app → support}/models/animal.rb +0 -0
  246. data/spec/{app → support}/models/answer.rb +0 -0
  247. data/spec/{app → support}/models/appointment.rb +0 -0
  248. data/spec/support/models/armrest.rb +10 -0
  249. data/spec/{app → support}/models/array_field.rb +0 -0
  250. data/spec/{app → support}/models/article.rb +0 -0
  251. data/spec/{app → support}/models/artist.rb +0 -0
  252. data/spec/{app → support}/models/artwork.rb +0 -0
  253. data/spec/{app → support}/models/audio.rb +0 -0
  254. data/spec/{app → support}/models/augmentation.rb +0 -0
  255. data/spec/{app → support}/models/author.rb +0 -0
  256. data/spec/{app → support}/models/baby.rb +0 -0
  257. data/spec/{app → support}/models/band.rb +0 -0
  258. data/spec/{app → support}/models/bar.rb +0 -0
  259. data/spec/{app → support}/models/basic.rb +0 -0
  260. data/spec/{app → support}/models/bed.rb +0 -0
  261. data/spec/{app → support}/models/big_palette.rb +0 -0
  262. data/spec/{app → support}/models/birthday.rb +0 -0
  263. data/spec/{app → support}/models/bomb.rb +0 -0
  264. data/spec/{app → support}/models/book.rb +0 -0
  265. data/spec/{app → support}/models/breed.rb +0 -0
  266. data/spec/{app → support}/models/browser.rb +1 -1
  267. data/spec/{app → support}/models/building.rb +0 -0
  268. data/spec/{app → support}/models/building_address.rb +0 -0
  269. data/spec/{app → support}/models/bus.rb +0 -0
  270. data/spec/{app → support}/models/business.rb +0 -0
  271. data/spec/{app → support}/models/callback_test.rb +0 -0
  272. data/spec/{app → support}/models/canvas.rb +1 -1
  273. data/spec/{app → support}/models/car.rb +0 -0
  274. data/spec/{app → support}/models/cat.rb +0 -0
  275. data/spec/{app → support}/models/category.rb +0 -0
  276. data/spec/{app → support}/models/child.rb +0 -0
  277. data/spec/{app → support}/models/child_doc.rb +0 -0
  278. data/spec/{app → support}/models/church.rb +0 -0
  279. data/spec/{app → support}/models/circle.rb +0 -0
  280. data/spec/{app → support}/models/circuit.rb +0 -0
  281. data/spec/{app → support}/models/circus.rb +0 -0
  282. data/spec/{app → support}/models/code.rb +0 -0
  283. data/spec/support/models/coding.rb +4 -0
  284. data/spec/support/models/coding/pull_request.rb +12 -0
  285. data/spec/{app → support}/models/comment.rb +0 -0
  286. data/spec/{app → support}/models/company.rb +0 -0
  287. data/spec/{app → support}/models/consumption_period.rb +0 -0
  288. data/spec/{app → support}/models/contextable_item.rb +0 -0
  289. data/spec/{app → support}/models/contractor.rb +0 -0
  290. data/spec/{app → support}/models/cookie.rb +0 -0
  291. data/spec/{app → support}/models/country_code.rb +0 -0
  292. data/spec/{app → support}/models/courier_job.rb +0 -0
  293. data/spec/support/models/crate.rb +13 -0
  294. data/spec/support/models/deed.rb +8 -0
  295. data/spec/{app → support}/models/definition.rb +0 -0
  296. data/spec/support/models/delegating_patient.rb +16 -0
  297. data/spec/{app → support}/models/description.rb +0 -0
  298. data/spec/{app → support}/models/dictionary.rb +0 -0
  299. data/spec/{app → support}/models/division.rb +0 -0
  300. data/spec/{app → support}/models/doctor.rb +0 -0
  301. data/spec/{app → support}/models/dog.rb +0 -0
  302. data/spec/{app → support}/models/dokument.rb +0 -0
  303. data/spec/{app → support}/models/draft.rb +0 -0
  304. data/spec/{app → support}/models/dragon.rb +0 -0
  305. data/spec/{app → support}/models/driver.rb +1 -1
  306. data/spec/{app → support}/models/drug.rb +0 -0
  307. data/spec/{app → support}/models/dungeon.rb +0 -0
  308. data/spec/{app → support}/models/edit.rb +0 -0
  309. data/spec/{app → support}/models/email.rb +0 -0
  310. data/spec/{app → support}/models/employer.rb +0 -0
  311. data/spec/{app → support}/models/entry.rb +0 -0
  312. data/spec/{app → support}/models/eraser.rb +0 -0
  313. data/spec/{app → support}/models/even.rb +0 -0
  314. data/spec/{app → support}/models/event.rb +0 -0
  315. data/spec/{app → support}/models/exhibition.rb +0 -0
  316. data/spec/{app → support}/models/exhibitor.rb +0 -0
  317. data/spec/{app → support}/models/explosion.rb +0 -0
  318. data/spec/{app → support}/models/eye.rb +0 -0
  319. data/spec/{app → support}/models/eye_bowl.rb +0 -0
  320. data/spec/{app → support}/models/face.rb +0 -0
  321. data/spec/{app → support}/models/favorite.rb +0 -0
  322. data/spec/{app → support}/models/filesystem.rb +0 -0
  323. data/spec/{app → support}/models/fire_hydrant.rb +0 -0
  324. data/spec/{app → support}/models/firefox.rb +0 -0
  325. data/spec/{app → support}/models/fish.rb +0 -0
  326. data/spec/{app → support}/models/folder.rb +0 -0
  327. data/spec/{app → support}/models/folder_item.rb +0 -0
  328. data/spec/{app → support}/models/fruits.rb +0 -0
  329. data/spec/{app → support}/models/game.rb +0 -0
  330. data/spec/{app → support}/models/ghost.rb +0 -0
  331. data/spec/support/models/guitar.rb +5 -0
  332. data/spec/{app → support}/models/home.rb +0 -0
  333. data/spec/{app → support}/models/house.rb +0 -0
  334. data/spec/{app → support}/models/html_writer.rb +0 -0
  335. data/spec/{app → support}/models/id_key.rb +0 -0
  336. data/spec/support/models/idnodef.rb +8 -0
  337. data/spec/{app → support}/models/image.rb +0 -0
  338. data/spec/{app → support}/models/implant.rb +0 -0
  339. data/spec/support/models/instrument.rb +9 -0
  340. data/spec/{app → support}/models/item.rb +1 -1
  341. data/spec/{app → support}/models/jar.rb +0 -0
  342. data/spec/{app → support}/models/kaleidoscope.rb +0 -0
  343. data/spec/{app → support}/models/kangaroo.rb +0 -0
  344. data/spec/{app → support}/models/label.rb +0 -0
  345. data/spec/{app → support}/models/language.rb +0 -0
  346. data/spec/{app → support}/models/lat_lng.rb +0 -0
  347. data/spec/{app → support}/models/league.rb +0 -0
  348. data/spec/{app → support}/models/learner.rb +0 -0
  349. data/spec/{app → support}/models/line_item.rb +0 -0
  350. data/spec/{app → support}/models/location.rb +0 -0
  351. data/spec/{app → support}/models/login.rb +0 -0
  352. data/spec/{app → support}/models/manufacturer.rb +0 -0
  353. data/spec/{app → support}/models/meat.rb +0 -0
  354. data/spec/{app → support}/models/membership.rb +0 -0
  355. data/spec/{app → support}/models/message.rb +0 -0
  356. data/spec/{app → support}/models/minim.rb +0 -0
  357. data/spec/{app → support}/models/mixed_drink.rb +0 -0
  358. data/spec/support/models/mop.rb +16 -0
  359. data/spec/{app → support}/models/movie.rb +0 -0
  360. data/spec/{app → support}/models/my_hash.rb +0 -0
  361. data/spec/{app → support}/models/name.rb +0 -0
  362. data/spec/{app → support}/models/name_only.rb +0 -0
  363. data/spec/{app → support}/models/node.rb +0 -0
  364. data/spec/{app → support}/models/note.rb +0 -0
  365. data/spec/{app → support}/models/odd.rb +0 -0
  366. data/spec/{app → support}/models/ordered_post.rb +0 -0
  367. data/spec/{app → support}/models/ordered_preference.rb +0 -0
  368. data/spec/{app → support}/models/oscar.rb +0 -0
  369. data/spec/{app → support}/models/other_owner_object.rb +0 -0
  370. data/spec/{app → support}/models/override.rb +0 -0
  371. data/spec/{app → support}/models/ownable.rb +0 -0
  372. data/spec/{app → support}/models/owner.rb +2 -0
  373. data/spec/{app → support}/models/pack.rb +0 -0
  374. data/spec/{app → support}/models/page.rb +0 -0
  375. data/spec/{app → support}/models/page_question.rb +0 -0
  376. data/spec/{app → support}/models/palette.rb +1 -1
  377. data/spec/{app → support}/models/parent.rb +0 -0
  378. data/spec/{app → support}/models/parent_doc.rb +0 -0
  379. data/spec/{app → support}/models/passport.rb +1 -0
  380. data/spec/{app → support}/models/patient.rb +0 -0
  381. data/spec/{app → support}/models/pdf_writer.rb +0 -0
  382. data/spec/{app → support}/models/pencil.rb +0 -0
  383. data/spec/{app → support}/models/person.rb +1 -1
  384. data/spec/{app → support}/models/pet.rb +0 -0
  385. data/spec/{app → support}/models/pet_owner.rb +0 -0
  386. data/spec/{app → support}/models/phone.rb +1 -0
  387. data/spec/support/models/piano.rb +5 -0
  388. data/spec/{app → support}/models/pizza.rb +0 -0
  389. data/spec/{app → support}/models/player.rb +0 -0
  390. data/spec/{app → support}/models/post.rb +0 -0
  391. data/spec/{app → support}/models/post_genre.rb +0 -0
  392. data/spec/{app → support}/models/powerup.rb +0 -0
  393. data/spec/{app → support}/models/preference.rb +0 -0
  394. data/spec/{app → support}/models/princess.rb +0 -0
  395. data/spec/{app → support}/models/product.rb +0 -0
  396. data/spec/support/models/profile.rb +18 -0
  397. data/spec/{app → support}/models/pronunciation.rb +0 -0
  398. data/spec/{app → support}/models/pub.rb +0 -0
  399. data/spec/support/models/publication.rb +5 -0
  400. data/spec/support/models/publication/encyclopedia.rb +12 -0
  401. data/spec/support/models/publication/review.rb +14 -0
  402. data/spec/{app → support}/models/purchase.rb +0 -0
  403. data/spec/{app → support}/models/question.rb +0 -0
  404. data/spec/{app → support}/models/quiz.rb +0 -0
  405. data/spec/{app → support}/models/rating.rb +0 -0
  406. data/spec/{app → support}/models/record.rb +0 -0
  407. data/spec/{app → support}/models/registry.rb +0 -0
  408. data/spec/{app → support}/models/role.rb +0 -0
  409. data/spec/{app → support}/models/root_category.rb +0 -0
  410. data/spec/{app → support}/models/sandwich.rb +0 -0
  411. data/spec/{app → support}/models/scheduler.rb +0 -0
  412. data/spec/{app/models/profile.rb → support/models/scribe.rb} +2 -2
  413. data/spec/support/models/seat.rb +25 -0
  414. data/spec/{app → support}/models/seo.rb +0 -0
  415. data/spec/{app → support}/models/series.rb +0 -0
  416. data/spec/{app → support}/models/server.rb +0 -0
  417. data/spec/{app → support}/models/service.rb +0 -0
  418. data/spec/{app → support}/models/shape.rb +2 -2
  419. data/spec/{app → support}/models/shelf.rb +0 -0
  420. data/spec/{app → support}/models/shipment_address.rb +0 -0
  421. data/spec/{app → support}/models/shipping_container.rb +0 -0
  422. data/spec/{app → support}/models/shipping_pack.rb +0 -0
  423. data/spec/{app → support}/models/shop.rb +0 -0
  424. data/spec/{app → support}/models/short_agent.rb +0 -0
  425. data/spec/{app → support}/models/short_quiz.rb +0 -0
  426. data/spec/{app → support}/models/simple.rb +0 -0
  427. data/spec/{app → support}/models/slave.rb +0 -0
  428. data/spec/{app → support}/models/song.rb +0 -0
  429. data/spec/{app → support}/models/sound.rb +0 -0
  430. data/spec/{app → support}/models/square.rb +0 -0
  431. data/spec/{app → support}/models/staff.rb +0 -0
  432. data/spec/{app → support}/models/store_as_dup_test1.rb +0 -0
  433. data/spec/{app → support}/models/store_as_dup_test2.rb +0 -0
  434. data/spec/{app → support}/models/store_as_dup_test3.rb +0 -0
  435. data/spec/{app → support}/models/store_as_dup_test4.rb +0 -0
  436. data/spec/{app → support}/models/strategy.rb +0 -0
  437. data/spec/{app → support}/models/sub_item.rb +0 -0
  438. data/spec/{app → support}/models/subscription.rb +0 -0
  439. data/spec/{app → support}/models/survey.rb +0 -0
  440. data/spec/{app → support}/models/symptom.rb +0 -0
  441. data/spec/support/models/system_role.rb +7 -0
  442. data/spec/{app → support}/models/tag.rb +0 -0
  443. data/spec/{app → support}/models/target.rb +0 -0
  444. data/spec/{app → support}/models/template.rb +0 -0
  445. data/spec/{app → support}/models/thing.rb +0 -0
  446. data/spec/{app → support}/models/title.rb +0 -0
  447. data/spec/{app → support}/models/tool.rb +2 -2
  448. data/spec/{app → support}/models/topping.rb +0 -0
  449. data/spec/support/models/toy.rb +10 -0
  450. data/spec/{app → support}/models/track.rb +0 -0
  451. data/spec/{app → support}/models/translation.rb +0 -0
  452. data/spec/{app → support}/models/tree.rb +0 -0
  453. data/spec/{app → support}/models/truck.rb +2 -0
  454. data/spec/{app → support}/models/updatable.rb +0 -0
  455. data/spec/{app → support}/models/user.rb +0 -0
  456. data/spec/{app → support}/models/user_account.rb +0 -0
  457. data/spec/{app → support}/models/validation_callback.rb +0 -0
  458. data/spec/{app → support}/models/vehicle.rb +7 -2
  459. data/spec/{app → support}/models/version.rb +0 -0
  460. data/spec/{app → support}/models/vertex.rb +0 -0
  461. data/spec/{app → support}/models/vet_visit.rb +0 -0
  462. data/spec/{app → support}/models/video.rb +0 -0
  463. data/spec/{app → support}/models/video_game.rb +0 -0
  464. data/spec/{app → support}/models/weapon.rb +0 -0
  465. data/spec/{app → support}/models/wiki_page.rb +0 -0
  466. data/spec/{app → support}/models/word.rb +0 -0
  467. data/spec/{app → support}/models/word_origin.rb +0 -0
  468. data/spec/{app → support}/models/writer.rb +2 -2
  469. data/spec/support/session_registry.rb +50 -0
  470. data/spec/support/spec_config.rb +13 -5
  471. data/spec/support/spec_organizer.rb +130 -0
  472. metadata +882 -752
  473. metadata.gz.sig +0 -0
  474. data/lib/mongoid/matchable/all.rb +0 -30
  475. data/lib/mongoid/matchable/and.rb +0 -32
  476. data/lib/mongoid/matchable/default.rb +0 -121
  477. data/lib/mongoid/matchable/elem_match.rb +0 -36
  478. data/lib/mongoid/matchable/eq.rb +0 -23
  479. data/lib/mongoid/matchable/exists.rb +0 -25
  480. data/lib/mongoid/matchable/gt.rb +0 -25
  481. data/lib/mongoid/matchable/gte.rb +0 -25
  482. data/lib/mongoid/matchable/in.rb +0 -26
  483. data/lib/mongoid/matchable/lt.rb +0 -25
  484. data/lib/mongoid/matchable/lte.rb +0 -25
  485. data/lib/mongoid/matchable/ne.rb +0 -23
  486. data/lib/mongoid/matchable/nin.rb +0 -24
  487. data/lib/mongoid/matchable/nor.rb +0 -38
  488. data/lib/mongoid/matchable/or.rb +0 -35
  489. data/lib/mongoid/matchable/regexp.rb +0 -30
  490. data/lib/mongoid/matchable/size.rb +0 -23
  491. data/spec/mongoid/matchable/all_spec.rb +0 -34
  492. data/spec/mongoid/matchable/and_spec.rb +0 -190
  493. data/spec/mongoid/matchable/default_spec.rb +0 -140
  494. data/spec/mongoid/matchable/elem_match_spec.rb +0 -109
  495. data/spec/mongoid/matchable/eq_spec.rb +0 -49
  496. data/spec/mongoid/matchable/exists_spec.rb +0 -60
  497. data/spec/mongoid/matchable/gt_spec.rb +0 -89
  498. data/spec/mongoid/matchable/gte_spec.rb +0 -87
  499. data/spec/mongoid/matchable/in_spec.rb +0 -52
  500. data/spec/mongoid/matchable/lt_spec.rb +0 -88
  501. data/spec/mongoid/matchable/lte_spec.rb +0 -88
  502. data/spec/mongoid/matchable/ne_spec.rb +0 -49
  503. data/spec/mongoid/matchable/nin_spec.rb +0 -51
  504. data/spec/mongoid/matchable/nor_spec.rb +0 -210
  505. data/spec/mongoid/matchable/or_spec.rb +0 -134
  506. data/spec/mongoid/matchable/regexp_spec.rb +0 -62
  507. data/spec/mongoid/matchable/size_spec.rb +0 -28
  508. data/spec/mongoid/matchable_spec.rb +0 -856
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+ # encoding: utf-8
3
+
4
+ module Mongoid
5
+ module Errors
6
+
7
+ # Raised when invalid field-level operator is passed to an
8
+ # embedded matcher.
9
+ class InvalidFieldOperator < InvalidQuery
10
+
11
+ # @api private
12
+ VALID_OPERATORS = %w(
13
+ all elemMatch eq exists gt gte in lt lte ne nin not regex size
14
+ ).freeze
15
+
16
+ # Creates the exception.
17
+ #
18
+ # @param [ String ] operator The operator that was used.
19
+ #
20
+ # @api private
21
+ def initialize(operator)
22
+ @operator = operator
23
+ super(compose_message("invalid_field_operator",
24
+ operator: operator,
25
+ valid_operators: VALID_OPERATORS.map { |op| "'$#{op}'" }.join(', '),
26
+ ))
27
+ end
28
+
29
+ # @return [ String ] The operator that was used.
30
+ attr_reader :operator
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+ # encoding: utf-8
3
+
4
+ module Mongoid
5
+ module Errors
6
+
7
+ # Raised when invalid query is passed to an embedded matcher, or an
8
+ # invalid query fragment is passed to the query builder (Criteria object).
9
+ class InvalidQuery < MongoidError
10
+
11
+ # Create the new invalid query error.
12
+ #
13
+ # @api private
14
+ def initialize(msg)
15
+ super
16
+ end
17
+
18
+ # Stringifies the argument using #inspect and truncates the result to
19
+ # about 100 characters.
20
+ #
21
+ # @param [ Object ] expr An expression to stringify and truncate.
22
+ #
23
+ # @api private
24
+ def self.truncate_expr(expr)
25
+ unless expr.is_a?(String)
26
+ expr = expr.inspect
27
+ end
28
+
29
+ if expr.length > 103
30
+ expr = if expr =~ /\A<#((?:.|\n)*)>\z/
31
+ "<##{expr.slice(0, 97)}...>"
32
+ else
33
+ expr.slice(0, 100) + '...'
34
+ end
35
+ end
36
+
37
+ expr
38
+ end
39
+ end
40
+ end
41
+ end
@@ -11,9 +11,9 @@ module Mongoid
11
11
  # Create the new error.
12
12
  #
13
13
  # @example Create the error.
14
- # NoClientConfig.new(:secondary)
14
+ # NoClientConfig.new(:analytics)
15
15
  #
16
- # @param [ String, Symbol ] name The name of the client.
16
+ # @param [ String | Symbol ] name The name of the client.
17
17
  #
18
18
  # @since 3.0.0
19
19
  def initialize(name)
@@ -10,7 +10,7 @@ module Mongoid
10
10
  # Create the new error with the defined client names.
11
11
  #
12
12
  # @example Create the new error.
13
- # NoDefaultClient.new([ :secondary ])
13
+ # NoDefaultClient.new([ :analytics ])
14
14
  #
15
15
  # @param [ Array<Symbol> ] keys The defined clients.
16
16
  #
@@ -48,9 +48,11 @@ module Mongoid
48
48
  value.each_pair do |_key, _value|
49
49
  value[_key] = (key == "$rename") ? _value.to_s : mongoize_for(key, klass, _key, _value)
50
50
  end
51
- (consolidated[key] ||= {}).merge!(value)
51
+ consolidated[key] ||= {}
52
+ consolidated[key].update(value)
52
53
  else
53
- (consolidated["$set"] ||= {}).merge!(key => mongoize_for(key, klass, key, value))
54
+ consolidated["$set"] ||= {}
55
+ consolidated["$set"].update(key => mongoize_for(key, klass, key, value))
54
56
  end
55
57
  end
56
58
  consolidated
@@ -11,7 +11,7 @@ module Mongoid
11
11
  # type.
12
12
  #
13
13
  # @example Mongoize the object.
14
- # Regexp.mongoize(/^[abc]/)
14
+ # Regexp.mongoize(/\A[abc]/)
15
15
  #
16
16
  # @param [ Regexp, String ] object The object to mongoize.
17
17
  #
@@ -7,10 +7,18 @@ module Mongoid
7
7
  module Factory
8
8
  extend self
9
9
 
10
+ # @deprecated
10
11
  TYPE = "_type".freeze
11
12
 
12
13
  # Builds a new +Document+ from the supplied attributes.
13
14
  #
15
+ # This method either instantiats klass or a descendant of klass if the attributes include
16
+ # klass' discriminator key.
17
+ #
18
+ # If the attributes contain the discriminator key (which is _type by default) and the
19
+ # discriminator value does not correspond to a descendant of klass then this method
20
+ # would create an instance of klass.
21
+ #
14
22
  # @example Build the document.
15
23
  # Mongoid::Factory.build(Person, { "name" => "Durran" })
16
24
  #
@@ -20,9 +28,10 @@ module Mongoid
20
28
  # @return [ Document ] The instantiated document.
21
29
  def build(klass, attributes = nil)
22
30
  attributes ||= {}
23
- type = attributes[TYPE] || attributes[TYPE.to_sym]
24
- if type && klass._types.include?(type)
25
- type.constantize.new(attributes)
31
+ dvalue = attributes[klass.discriminator_key] || attributes[klass.discriminator_key.to_sym]
32
+ type = klass.get_discriminator_mapping(dvalue)
33
+ if type
34
+ type.new(attributes)
26
35
  else
27
36
  klass.new(attributes)
28
37
  end
@@ -31,6 +40,10 @@ module Mongoid
31
40
  # Builds a new +Document+ from the supplied attributes loaded from the
32
41
  # database.
33
42
  #
43
+ # If the attributes contain the discriminator key (which is _type by default) and the
44
+ # discriminator value does not correspond to a descendant of klass then this method
45
+ # raises an UnknownModel error.
46
+ #
34
47
  # If a criteria object is given, it is used in two ways:
35
48
  # 1. If the criteria has a list of fields specified via #only,
36
49
  # only those fields are populated in the returned document.
@@ -54,7 +67,7 @@ module Mongoid
54
67
  if criteria
55
68
  selected_fields ||= criteria.options[:fields]
56
69
  end
57
- type = (attributes || {})[TYPE]
70
+ type = (attributes || {})[klass.discriminator_key]
58
71
  if type.blank?
59
72
  obj = klass.instantiate(attributes, selected_fields)
60
73
  if criteria && criteria.association && criteria.parent_document
@@ -62,13 +75,17 @@ module Mongoid
62
75
  end
63
76
  obj
64
77
  else
65
- camelized = type.camelize
78
+ constantized = klass.get_discriminator_mapping(type)
66
79
 
67
- # Check if the class exists
68
- begin
69
- constantized = camelized.constantize
70
- rescue NameError
71
- raise Errors::UnknownModel.new(camelized, type)
80
+ unless constantized
81
+ camelized = type.camelize
82
+
83
+ # Check if the class exists
84
+ begin
85
+ constantized = camelized.constantize
86
+ rescue NameError
87
+ raise Errors::UnknownModel.new(camelized, type)
88
+ end
72
89
  end
73
90
 
74
91
  # Check if the class is a Document class
@@ -500,7 +500,8 @@ module Mongoid
500
500
  def create_translations_getter(name, meth)
501
501
  generated_methods.module_eval do
502
502
  re_define_method("#{meth}_translations") do
503
- (attributes[name] ||= {}).with_indifferent_access
503
+ attributes[name] ||= {}
504
+ attributes[name].with_indifferent_access
504
505
  end
505
506
  alias_method :"#{meth}_t", :"#{meth}_translations"
506
507
  end
@@ -4,12 +4,13 @@
4
4
  module Mongoid
5
5
  module Fields
6
6
  class Standard
7
+ extend Forwardable
7
8
 
8
9
  # Defines the behavior for defined fields in the document.
9
10
  # Set readers for the instance variables.
10
11
  attr_accessor :default_val, :label, :name, :options
11
12
 
12
- delegate :demongoize, :evolve, :mongoize, to: :type
13
+ def_delegators :type, :demongoize, :evolve, :mongoize
13
14
 
14
15
  # Adds the atomic changes for this type of resizable field.
15
16
  #
@@ -39,7 +39,8 @@ module Mongoid
39
39
  #
40
40
  # @since 3.0.0
41
41
  def validate(klass, name, options)
42
- validate_name(klass, name, options)
42
+ validate_field_name(klass, name)
43
+ validate_name_uniqueness(klass, name, options)
43
44
  validate_options(klass, name, options)
44
45
  end
45
46
 
@@ -61,28 +62,40 @@ module Mongoid
61
62
  end
62
63
  end
63
64
 
64
- private
65
-
66
- # Determine if the field name is allowed, if not raise an error.
67
- #
68
- # @api private
65
+ # Determine if the field name is valid, if not raise an error.
69
66
  #
70
67
  # @example Check the field name.
71
- # Macro.validate_name(Model, :name)
68
+ # Macro.validate_field_name(Model, :name)
72
69
  #
73
70
  # @param [ Class ] klass The model class.
74
71
  # @param [ Symbol ] name The field name.
75
72
  #
76
73
  # @raise [ Errors::InvalidField ] If the name is not allowed.
77
74
  #
78
- # @since 3.0.0
79
- def validate_name(klass, name, options)
75
+ # @api private
76
+ def validate_field_name(klass, name)
80
77
  [name, "#{name}?".to_sym, "#{name}=".to_sym].each do |n|
81
78
  if Mongoid.destructive_fields.include?(n)
82
79
  raise Errors::InvalidField.new(klass, n)
83
80
  end
84
81
  end
82
+ end
85
83
 
84
+ private
85
+
86
+ # Determine if the field name is unique, if not raise an error.
87
+ #
88
+ # @example Check the field name.
89
+ # Macro.validate_name_uniqueness(Model, :name, {})
90
+ #
91
+ # @param [ Class ] klass The model class.
92
+ # @param [ Symbol ] name The field name.
93
+ # @param [ Hash ] options The provided options.
94
+ #
95
+ # @raise [ Errors::InvalidField ] If the name is not allowed.
96
+ #
97
+ # @api private
98
+ def validate_name_uniqueness(klass, name, options)
86
99
  if !options[:overwrite] && klass.fields.keys.include?(name.to_s)
87
100
  if Mongoid.duplicate_fields_exception
88
101
  raise Errors::InvalidField.new(klass, name)
@@ -113,7 +126,10 @@ module Mongoid
113
126
  end
114
127
 
115
128
  if option == :type && options[option] == Symbol
116
- Mongoid.logger.warn(FIELD_TYPE_IS_SYMBOL)
129
+ @field_type_is_symbol_warned ||= begin
130
+ Mongoid.logger.warn(FIELD_TYPE_IS_SYMBOL)
131
+ true
132
+ end
117
133
  end
118
134
  end
119
135
  end
@@ -8,15 +8,16 @@ module Mongoid
8
8
  #
9
9
  # @since 4.0.0
10
10
  module Findable
11
+ extend Forwardable
11
12
 
12
- delegate *(
13
+ def_delegators :with_default_scope, *(
13
14
  Criteria::Queryable::Selectable.forwardables +
14
15
  Criteria::Queryable::Optional.forwardables
15
- ), to: :with_default_scope
16
+ )
16
17
 
17
18
  # These are methods defined on the criteria that should also be accessible
18
19
  # directly from the class level.
19
- delegate \
20
+ def_delegators :with_default_scope,
20
21
  :aggregates,
21
22
  :avg,
22
23
  :create_with,
@@ -45,7 +46,7 @@ module Mongoid
45
46
  :sum,
46
47
  :text_search,
47
48
  :update,
48
- :update_all, to: :with_default_scope
49
+ :update_all
49
50
 
50
51
  # Returns a count of records in the database.
51
52
  # If you want to specify conditions use where.
@@ -59,6 +60,16 @@ module Mongoid
59
60
  with_default_scope.count
60
61
  end
61
62
 
63
+ # Returns an estimated count of records in the database.
64
+ #
65
+ # @example Get the count of matching documents.
66
+ # Person.estimated_count
67
+ #
68
+ # @return [ Integer ] The number of matching documents.
69
+ def estimated_count
70
+ with_default_scope.estimated_count
71
+ end
72
+
62
73
  # Returns true if count is zero
63
74
  #
64
75
  # @example Are there no saved documents for this model?
@@ -80,20 +91,46 @@ module Mongoid
80
91
  with_default_scope.exists?
81
92
  end
82
93
 
83
- # Find a +Document+ in several different ways.
84
- #
85
- # If a +String+ is provided, it will be assumed that it is a
86
- # representation of a Mongo::ObjectID and will attempt to find a single
87
- # +Document+ based on that id. If a +Symbol+ and +Hash+ is provided then
88
- # it will attempt to find either a single +Document+ or multiples based
89
- # on the conditions provided and the first parameter.
90
- #
91
- # @example Find a single document by an id.
92
- # Person.find(BSON::ObjectId)
93
- #
94
- # @param [ Array ] args An assortment of finder options.
95
- #
96
- # @return [ Document, nil, Criteria ] A document or matching documents.
94
+ # Finds a +Document+ or multiple documents by their _id values.
95
+ #
96
+ # If a single non-Array argument is given, this argument is interpreted
97
+ # as the _id value of a document to find. If there is a matching document
98
+ # in the database, this document is returned; otherwise, if the
99
+ # +raise_not_found_error+ Mongoid configuration option is truthy
100
+ # (which is the default), +Errors::DocumentNotFound+ is raised, and if
101
+ # +raise_not_found_error+ is falsy, +find+ returns +nil+.
102
+ #
103
+ # If multiple arguments are given, or an Array argument is given, the
104
+ # array is flattened and each array element is interpreted as the _id
105
+ # value of the document to find. Mongoid then attempts to retrieve all
106
+ # documents with the provided _id values. The return value is an array
107
+ # of found documents. Each document appears one time in the returned array,
108
+ # even if its _id is given multiple times in the argument to +find+.
109
+ # If the +raise_not_found_error+ Mongoid configuration option is truthy,
110
+ # +Errors::DocumentNotFound+ exception is raised if any of the specified
111
+ # _ids were not found in the database. If the ++raise_not_found_error+
112
+ # Mongoid configuration option is falsy, only those documents which are
113
+ # found are returned; if no documents are found, the return value is an
114
+ # empty array.
115
+ #
116
+ # Note that MongoDB does not allow the _id field to be an array.
117
+ #
118
+ # The argument undergoes customary Mongoid type conversions based on
119
+ # the type declared for the _id field. By default the _id field is a
120
+ # +BSON::ObjectId+; this allows strings to be passed to +find+ and the
121
+ # strings will be transparently converted to +BSON::ObjectId+ instances
122
+ # during query construction.
123
+ #
124
+ # The +find+ method takes into account the default scope defined on the
125
+ # model class, if any.
126
+ #
127
+ # @param [ Object | Array<Object> ] args The _id values to find or an
128
+ # array thereof.
129
+ #
130
+ # @return [ Document | Array<Document> | nil ] A document or matching documents.
131
+ #
132
+ # @raise Errors::DocumentNotFound If not all documents are found and
133
+ # the +raise_not_found_error+ Mongoid configuration option is truthy.
97
134
  def find(*args)
98
135
  with_default_scope.find(*args)
99
136
  end
@@ -82,8 +82,8 @@ module Mongoid
82
82
  #
83
83
  # @since 1.0.0
84
84
  def add_indexes
85
- if hereditary? && !index_keys.include?(_type: 1)
86
- index({ _type: 1 }, unique: false, background: true)
85
+ if hereditary? && !index_keys.include?(self.discriminator_key.to_sym => 1)
86
+ index({ self.discriminator_key.to_sym => 1 }, unique: false, background: true)
87
87
  end
88
88
  true
89
89
  end
@@ -131,7 +131,11 @@ module Mongoid
131
131
  return false
132
132
  end
133
133
  end
134
- callback_executable?(kind) ? super(kind, *args, &block) : true
134
+ if callback_executable?(kind)
135
+ super(kind, *args, &block)
136
+ else
137
+ true
138
+ end
135
139
  end
136
140
 
137
141
  private
@@ -1,24 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  # encoding: utf-8
3
3
 
4
- require "mongoid/matchable/default"
5
- require "mongoid/matchable/all"
6
- require "mongoid/matchable/and"
7
- require "mongoid/matchable/elem_match"
8
- require "mongoid/matchable/eq"
9
- require "mongoid/matchable/exists"
10
- require "mongoid/matchable/gt"
11
- require "mongoid/matchable/gte"
12
- require "mongoid/matchable/in"
13
- require "mongoid/matchable/lt"
14
- require "mongoid/matchable/lte"
15
- require "mongoid/matchable/ne"
16
- require "mongoid/matchable/nin"
17
- require "mongoid/matchable/nor"
18
- require "mongoid/matchable/or"
19
- require "mongoid/matchable/regexp"
20
- require "mongoid/matchable/size"
21
-
22
4
  module Mongoid
23
5
 
24
6
  # This module contains all the behavior for Ruby implementations of MongoDB
@@ -28,27 +10,6 @@ module Mongoid
28
10
  module Matchable
29
11
  extend ActiveSupport::Concern
30
12
 
31
- # Hash lookup for the matcher for a specific operation.
32
- #
33
- # @since 1.0.0
34
- MATCHERS = {
35
- "$all" => All,
36
- "$and" => And,
37
- "$elemMatch" => ElemMatch,
38
- "$eq" => Eq,
39
- "$exists" => Exists,
40
- "$gt" => Gt,
41
- "$gte" => Gte,
42
- "$in" => In,
43
- "$lt" => Lt,
44
- "$lte" => Lte,
45
- "$ne" => Ne,
46
- "$nin" => Nin,
47
- "$nor" => Nor,
48
- "$or" => Or,
49
- "$size" => Size,
50
- }.with_indifferent_access.freeze
51
-
52
13
  # Determines if this document has the attributes to match the supplied
53
14
  # MongoDB selector. Used for matching on embedded associations.
54
15
  #
@@ -61,116 +22,7 @@ module Mongoid
61
22
  #
62
23
  # @since 1.0.0
63
24
  def _matches?(selector)
64
- selector.each_pair do |key, value|
65
- if value.is_a?(Hash)
66
- value.each do |item|
67
- if item[0].to_s == "$not".freeze
68
- item = item[1]
69
- return false if matcher(key, item)._matches?(item)
70
- else
71
- return false unless matcher(key, Hash[*item])._matches?(Hash[*item])
72
- end
73
- end
74
- else
75
- return false unless matcher(key, value)._matches?(value)
76
- end
77
- end
78
- true
79
- end
80
-
81
- private
82
-
83
- # Get the matcher for the supplied key and value. Will determine the class
84
- # name from the key.
85
- #
86
- # @example Get the matcher.
87
- # document.matcher(:title, { "$in" => [ "test" ] })
88
- #
89
- # @param [ Symbol, String ] key The field name.
90
- # @param [ Object, Hash ] value The value or selector.
91
- #
92
- # @return [ Matcher ] The matcher.
93
- #
94
- # @since 2.0.0.rc.7
95
- # @api private
96
- def matcher(key, value)
97
- Matchable.matcher(self, key, value)
98
- end
99
-
100
- class << self
101
-
102
- # Get the matcher for the supplied key and value. Will determine the class
103
- # name from the key.
104
- #
105
- # @api private
106
- #
107
- # @example Get the matcher.
108
- # Matchable.matcher(document, :title, { "$in" => [ "test" ] })
109
- #
110
- # @param [ Document ] document The document to check.
111
- # @param [ Symbol, String ] key The field name.
112
- # @param [ Object, Hash ] value The value or selector.
113
- #
114
- # @return [ Matcher ] The matcher.
115
- #
116
- # @since 2.0.0.rc.7
117
- def matcher(document, key, value)
118
- if value.is_a?(Hash)
119
- matcher = MATCHERS[value.keys.first]
120
- if matcher
121
- matcher.new(extract_attribute(document, key))
122
- else
123
- Default.new(extract_attribute(document, key))
124
- end
125
- elsif value.regexp?
126
- Regexp.new(extract_attribute(document, key))
127
- else
128
- case key.to_s
129
- when "$or" then Or.new(value, document)
130
- when "$and" then And.new(value, document)
131
- when "$nor" then Nor.new(value, document)
132
- else Default.new(extract_attribute(document, key))
133
- end
134
- end
135
- end
136
-
137
- private
138
-
139
- # Extract the attribute from the key, being smarter about dot notation.
140
- #
141
- # @api private
142
- #
143
- # @example Extract the attribute.
144
- # strategy.extract_attribute(doc, "info.field")
145
- #
146
- # @param [ Document ] document The document.
147
- # @param [ String ] key The key.
148
- #
149
- # @return [ Object ] The value of the attribute.
150
- #
151
- # @since 2.2.1
152
- # @api private
153
- def extract_attribute(document, key)
154
- if (key_string = key.to_s) =~ /.+\..+/
155
- key_string.split('.').inject(document.send(:as_attributes)) do |_attribs, _key|
156
- if _attribs.is_a?(::Array)
157
- if _key =~ /\A\d+\z/ && _attribs.none? {|doc| doc.is_a?(Hash)}
158
- _attribs.try(:[], _key.to_i)
159
- else
160
- _attribs.map { |doc| doc.try(:[], _key) }
161
- end
162
- else
163
- _attribs.try(:[], _key)
164
- end
165
- end
166
- else
167
- if document.is_a?(Hash)
168
- document[key_string]
169
- else
170
- document.attributes[key_string]
171
- end
172
- end
173
- end
25
+ Matcher::Expression.matches?(self, selector)
174
26
  end
175
27
  end
176
28
  end