mongoid 2.8.1 → 3.0.0.rc

Sign up to get free protection for your applications and to get access to all the features.
Files changed (351) hide show
  1. data/CHANGELOG.md +802 -58
  2. data/LICENSE +1 -1
  3. data/README.md +10 -11
  4. data/Rakefile +2 -8
  5. data/lib/config/locales/en.yml +441 -51
  6. data/lib/mongoid.rb +24 -39
  7. data/lib/mongoid/atomic.rb +16 -33
  8. data/lib/mongoid/atomic/modifiers.rb +2 -2
  9. data/lib/mongoid/atomic/paths/embedded.rb +4 -5
  10. data/lib/mongoid/atomic/paths/embedded/many.rb +6 -6
  11. data/lib/mongoid/atomic/paths/embedded/one.rb +5 -5
  12. data/lib/mongoid/atomic/paths/root.rb +4 -5
  13. data/lib/mongoid/attributes.rb +95 -32
  14. data/lib/mongoid/attributes/processing.rb +14 -10
  15. data/lib/mongoid/attributes/readonly.rb +56 -0
  16. data/lib/mongoid/callbacks.rb +90 -27
  17. data/lib/mongoid/collections/retry.rb +2 -3
  18. data/lib/mongoid/components.rb +11 -23
  19. data/lib/mongoid/config.rb +87 -233
  20. data/lib/mongoid/config/environment.rb +5 -6
  21. data/lib/mongoid/config/inflections.rb +6 -0
  22. data/lib/mongoid/config/options.rb +1 -1
  23. data/lib/mongoid/config/validators.rb +3 -0
  24. data/lib/mongoid/config/validators/option.rb +25 -0
  25. data/lib/mongoid/config/validators/session.rb +140 -0
  26. data/lib/mongoid/contextual.rb +50 -0
  27. data/lib/mongoid/contextual/aggregable/memory.rb +98 -0
  28. data/lib/mongoid/contextual/aggregable/mongo.rb +181 -0
  29. data/lib/mongoid/contextual/atomic.rb +179 -0
  30. data/lib/mongoid/contextual/command.rb +43 -0
  31. data/lib/mongoid/contextual/find_and_modify.rb +66 -0
  32. data/lib/mongoid/contextual/map_reduce.rb +273 -0
  33. data/lib/mongoid/contextual/memory.rb +383 -0
  34. data/lib/mongoid/contextual/mongo.rb +543 -0
  35. data/lib/mongoid/copyable.rb +3 -34
  36. data/lib/mongoid/criteria.rb +436 -250
  37. data/lib/mongoid/criterion/inspection.rb +14 -8
  38. data/lib/mongoid/criterion/scoping.rb +114 -44
  39. data/lib/mongoid/dirty.rb +152 -67
  40. data/lib/mongoid/document.rb +69 -50
  41. data/lib/mongoid/errors.rb +22 -1
  42. data/lib/mongoid/errors/ambiguous_relationship.rb +51 -0
  43. data/lib/mongoid/errors/callback.rb +5 -6
  44. data/lib/mongoid/errors/delete_restriction.rb +29 -0
  45. data/lib/mongoid/errors/document_not_found.rb +98 -17
  46. data/lib/mongoid/errors/eager_load.rb +3 -6
  47. data/lib/mongoid/errors/invalid_collection.rb +3 -3
  48. data/lib/mongoid/errors/invalid_config_option.rb +27 -0
  49. data/lib/mongoid/errors/invalid_database.rb +3 -3
  50. data/lib/mongoid/errors/invalid_field.rb +54 -8
  51. data/lib/mongoid/errors/invalid_field_option.rb +35 -0
  52. data/lib/mongoid/errors/invalid_find.rb +3 -3
  53. data/lib/mongoid/errors/invalid_index.rb +28 -0
  54. data/lib/mongoid/errors/invalid_options.rb +4 -4
  55. data/lib/mongoid/errors/invalid_scope.rb +24 -0
  56. data/lib/mongoid/errors/invalid_set_polymorphic_relation.rb +38 -0
  57. data/lib/mongoid/errors/invalid_storage_options.rb +27 -0
  58. data/lib/mongoid/errors/invalid_time.rb +3 -6
  59. data/lib/mongoid/errors/inverse_not_found.rb +29 -0
  60. data/lib/mongoid/errors/mixed_relations.rb +4 -9
  61. data/lib/mongoid/errors/mixed_session_configuration.rb +28 -0
  62. data/lib/mongoid/errors/mongoid_error.rb +54 -3
  63. data/lib/mongoid/errors/nested_attributes_metadata_not_found.rb +28 -0
  64. data/lib/mongoid/errors/no_default_session.rb +23 -0
  65. data/lib/mongoid/errors/no_environment.rb +3 -3
  66. data/lib/mongoid/errors/no_map_reduce_output.rb +24 -0
  67. data/lib/mongoid/errors/no_parent.rb +24 -0
  68. data/lib/mongoid/errors/no_session_config.rb +22 -0
  69. data/lib/mongoid/errors/no_session_database.rb +27 -0
  70. data/lib/mongoid/errors/no_session_hosts.rb +27 -0
  71. data/lib/mongoid/errors/no_sessions_config.rb +20 -0
  72. data/lib/mongoid/errors/readonly_attribute.rb +25 -0
  73. data/lib/mongoid/errors/scope_overwrite.rb +4 -4
  74. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +4 -4
  75. data/lib/mongoid/errors/unknown_attribute.rb +25 -0
  76. data/lib/mongoid/errors/unsaved_document.rb +4 -8
  77. data/lib/mongoid/errors/unsupported_javascript.rb +27 -0
  78. data/lib/mongoid/errors/unsupported_version.rb +4 -4
  79. data/lib/mongoid/errors/validations.rb +7 -6
  80. data/lib/mongoid/errors/versioning_not_on_root.rb +23 -0
  81. data/lib/mongoid/extensions.rb +28 -76
  82. data/lib/mongoid/extensions/array.rb +127 -0
  83. data/lib/mongoid/extensions/big_decimal.rb +42 -0
  84. data/lib/mongoid/extensions/boolean.rb +24 -0
  85. data/lib/mongoid/extensions/date.rb +70 -0
  86. data/lib/mongoid/extensions/date_time.rb +68 -0
  87. data/lib/mongoid/extensions/false_class.rb +26 -0
  88. data/lib/mongoid/extensions/float.rb +44 -0
  89. data/lib/mongoid/extensions/hash.rb +91 -0
  90. data/lib/mongoid/extensions/integer.rb +54 -0
  91. data/lib/mongoid/extensions/module.rb +28 -0
  92. data/lib/mongoid/extensions/nil_class.rb +21 -0
  93. data/lib/mongoid/extensions/object.rb +188 -0
  94. data/lib/mongoid/extensions/object_id.rb +53 -0
  95. data/lib/mongoid/extensions/range.rb +55 -0
  96. data/lib/mongoid/extensions/regexp.rb +27 -0
  97. data/lib/mongoid/extensions/set.rb +55 -0
  98. data/lib/mongoid/extensions/string.rb +155 -0
  99. data/lib/mongoid/extensions/symbol.rb +54 -0
  100. data/lib/mongoid/extensions/time.rb +78 -0
  101. data/lib/mongoid/extensions/time_with_zone.rb +55 -0
  102. data/lib/mongoid/extensions/true_class.rb +26 -0
  103. data/lib/mongoid/factory.rb +1 -1
  104. data/lib/mongoid/fields.rb +129 -194
  105. data/lib/mongoid/fields/foreign_key.rb +134 -0
  106. data/lib/mongoid/fields/localized.rb +73 -0
  107. data/lib/mongoid/fields/standard.rb +268 -0
  108. data/lib/mongoid/fields/validators.rb +2 -0
  109. data/lib/mongoid/fields/validators/macro.rb +83 -0
  110. data/lib/mongoid/finders.rb +42 -43
  111. data/lib/mongoid/hierarchy.rb +25 -14
  112. data/lib/mongoid/identity_map.rb +31 -19
  113. data/lib/mongoid/indexes.rb +66 -15
  114. data/lib/mongoid/indexes/validators/options.rb +80 -0
  115. data/lib/mongoid/inspection.rb +1 -1
  116. data/lib/mongoid/javascript.rb +1 -1
  117. data/lib/mongoid/json.rb +2 -2
  118. data/lib/mongoid/loggable.rb +69 -0
  119. data/lib/mongoid/matchers.rb +1 -1
  120. data/lib/mongoid/matchers/all.rb +7 -8
  121. data/lib/mongoid/matchers/and.rb +3 -3
  122. data/lib/mongoid/matchers/default.rb +6 -4
  123. data/lib/mongoid/matchers/exists.rb +2 -2
  124. data/lib/mongoid/matchers/gt.rb +2 -2
  125. data/lib/mongoid/matchers/gte.rb +2 -2
  126. data/lib/mongoid/matchers/in.rb +3 -7
  127. data/lib/mongoid/matchers/lt.rb +2 -2
  128. data/lib/mongoid/matchers/lte.rb +2 -2
  129. data/lib/mongoid/matchers/ne.rb +2 -2
  130. data/lib/mongoid/matchers/nin.rb +2 -2
  131. data/lib/mongoid/matchers/or.rb +2 -2
  132. data/lib/mongoid/matchers/size.rb +2 -2
  133. data/lib/mongoid/matchers/strategies.rb +3 -3
  134. data/lib/mongoid/multi_parameter_attributes.rb +8 -10
  135. data/lib/mongoid/nested_attributes.rb +17 -9
  136. data/lib/mongoid/observer.rb +1 -2
  137. data/lib/mongoid/paranoia.rb +13 -18
  138. data/lib/mongoid/persistence.rb +43 -39
  139. data/lib/mongoid/persistence/atomic.rb +2 -2
  140. data/lib/mongoid/persistence/atomic/add_to_set.rb +5 -9
  141. data/lib/mongoid/persistence/atomic/bit.rb +5 -7
  142. data/lib/mongoid/persistence/atomic/inc.rb +5 -7
  143. data/lib/mongoid/persistence/atomic/operation.rb +45 -6
  144. data/lib/mongoid/persistence/atomic/pop.rb +5 -7
  145. data/lib/mongoid/persistence/atomic/pull.rb +5 -7
  146. data/lib/mongoid/persistence/atomic/pull_all.rb +5 -7
  147. data/lib/mongoid/persistence/atomic/push.rb +4 -10
  148. data/lib/mongoid/persistence/atomic/push_all.rb +4 -10
  149. data/lib/mongoid/persistence/atomic/rename.rb +6 -7
  150. data/lib/mongoid/persistence/atomic/sets.rb +5 -7
  151. data/lib/mongoid/persistence/atomic/unset.rb +4 -5
  152. data/lib/mongoid/persistence/deletion.rb +2 -2
  153. data/lib/mongoid/persistence/insertion.rb +10 -16
  154. data/lib/mongoid/persistence/modification.rb +5 -9
  155. data/lib/mongoid/persistence/operations.rb +6 -19
  156. data/lib/mongoid/persistence/operations/embedded/insert.rb +7 -6
  157. data/lib/mongoid/persistence/operations/embedded/remove.rb +5 -5
  158. data/lib/mongoid/persistence/operations/insert.rb +4 -4
  159. data/lib/mongoid/persistence/operations/remove.rb +4 -4
  160. data/lib/mongoid/persistence/operations/update.rb +5 -5
  161. data/lib/mongoid/railtie.rb +26 -11
  162. data/lib/mongoid/railties/database.rake +22 -108
  163. data/lib/mongoid/relations.rb +4 -6
  164. data/lib/mongoid/relations/accessors.rb +119 -19
  165. data/lib/mongoid/relations/auto_save.rb +59 -5
  166. data/lib/mongoid/relations/binding.rb +211 -2
  167. data/lib/mongoid/relations/bindings/embedded/in.rb +16 -22
  168. data/lib/mongoid/relations/bindings/embedded/many.rb +9 -50
  169. data/lib/mongoid/relations/bindings/embedded/one.rb +10 -16
  170. data/lib/mongoid/relations/bindings/referenced/in.rb +31 -57
  171. data/lib/mongoid/relations/bindings/referenced/many.rb +8 -20
  172. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +15 -19
  173. data/lib/mongoid/relations/bindings/referenced/one.rb +10 -24
  174. data/lib/mongoid/relations/builder.rb +3 -3
  175. data/lib/mongoid/relations/builders.rb +19 -16
  176. data/lib/mongoid/relations/builders/embedded/in.rb +5 -5
  177. data/lib/mongoid/relations/builders/embedded/many.rb +12 -12
  178. data/lib/mongoid/relations/builders/embedded/one.rb +6 -6
  179. data/lib/mongoid/relations/builders/nested_attributes/many.rb +8 -8
  180. data/lib/mongoid/relations/builders/nested_attributes/one.rb +4 -4
  181. data/lib/mongoid/relations/builders/referenced/in.rb +4 -4
  182. data/lib/mongoid/relations/builders/referenced/many.rb +5 -5
  183. data/lib/mongoid/relations/builders/referenced/many_to_many.rb +7 -5
  184. data/lib/mongoid/relations/builders/referenced/one.rb +5 -5
  185. data/lib/mongoid/relations/cascading.rb +6 -4
  186. data/lib/mongoid/relations/cascading/delete.rb +3 -5
  187. data/lib/mongoid/relations/cascading/destroy.rb +3 -3
  188. data/lib/mongoid/relations/cascading/nullify.rb +3 -3
  189. data/lib/mongoid/relations/cascading/restrict.rb +37 -0
  190. data/lib/mongoid/relations/constraint.rb +4 -3
  191. data/lib/mongoid/relations/conversions.rb +5 -6
  192. data/lib/mongoid/relations/cyclic.rb +7 -7
  193. data/lib/mongoid/relations/embedded/batchable.rb +346 -0
  194. data/lib/mongoid/relations/embedded/in.rb +23 -12
  195. data/lib/mongoid/relations/embedded/many.rb +99 -161
  196. data/lib/mongoid/relations/embedded/one.rb +25 -14
  197. data/lib/mongoid/relations/macros.rb +105 -61
  198. data/lib/mongoid/relations/many.rb +93 -14
  199. data/lib/mongoid/relations/metadata.rb +200 -45
  200. data/lib/mongoid/relations/nested_builder.rb +3 -5
  201. data/lib/mongoid/relations/one.rb +2 -2
  202. data/lib/mongoid/relations/options.rb +2 -2
  203. data/lib/mongoid/relations/polymorphic.rb +9 -9
  204. data/lib/mongoid/relations/proxy.rb +60 -31
  205. data/lib/mongoid/relations/referenced/in.rb +40 -15
  206. data/lib/mongoid/relations/referenced/many.rb +117 -132
  207. data/lib/mongoid/relations/referenced/many_to_many.rb +101 -46
  208. data/lib/mongoid/relations/referenced/one.rb +34 -13
  209. data/lib/mongoid/relations/reflections.rb +3 -3
  210. data/lib/mongoid/relations/synchronization.rb +19 -23
  211. data/lib/mongoid/relations/targets/enumerable.rb +86 -57
  212. data/lib/mongoid/reloading.rb +12 -14
  213. data/lib/mongoid/scoping.rb +329 -0
  214. data/lib/mongoid/serialization.rb +8 -27
  215. data/lib/mongoid/sessions.rb +359 -0
  216. data/lib/mongoid/sessions/factory.rb +106 -0
  217. data/lib/mongoid/sessions/mongo_uri.rb +93 -0
  218. data/lib/mongoid/sessions/validators.rb +2 -0
  219. data/lib/mongoid/sessions/validators/storage.rb +49 -0
  220. data/lib/mongoid/sharding.rb +6 -6
  221. data/lib/mongoid/state.rb +6 -7
  222. data/lib/mongoid/threaded.rb +167 -59
  223. data/lib/mongoid/threaded/lifecycle.rb +21 -22
  224. data/lib/mongoid/threaded/sessions.rb +0 -0
  225. data/lib/mongoid/timestamps.rb +1 -1
  226. data/lib/mongoid/timestamps/created.rb +8 -4
  227. data/lib/mongoid/timestamps/timeless.rb +6 -4
  228. data/lib/mongoid/timestamps/updated.rb +3 -3
  229. data/lib/mongoid/unit_of_work.rb +61 -0
  230. data/lib/mongoid/validations.rb +27 -19
  231. data/lib/mongoid/validations/associated.rb +2 -2
  232. data/lib/mongoid/validations/format.rb +2 -2
  233. data/lib/mongoid/validations/presence.rb +31 -5
  234. data/lib/mongoid/validations/uniqueness.rb +9 -12
  235. data/lib/mongoid/version.rb +2 -2
  236. data/lib/mongoid/versioning.rb +25 -26
  237. data/lib/rack/mongoid/middleware/identity_map.rb +3 -3
  238. data/lib/rails/generators/mongoid/config/config_generator.rb +1 -1
  239. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +59 -19
  240. data/lib/rails/generators/mongoid/model/model_generator.rb +7 -7
  241. data/lib/rails/generators/mongoid/model/templates/model.rb.tt +2 -2
  242. data/lib/rails/generators/mongoid/observer/observer_generator.rb +4 -4
  243. data/lib/rails/generators/mongoid_generator.rb +5 -5
  244. data/lib/rails/mongoid.rb +69 -25
  245. metadata +110 -137
  246. checksums.yaml +0 -7
  247. data/lib/config/locales/bg.yml +0 -61
  248. data/lib/config/locales/de.yml +0 -61
  249. data/lib/config/locales/en-GB.yml +0 -64
  250. data/lib/config/locales/es.yml +0 -59
  251. data/lib/config/locales/fr.yml +0 -62
  252. data/lib/config/locales/hi.yml +0 -53
  253. data/lib/config/locales/hu.yml +0 -64
  254. data/lib/config/locales/id.yml +0 -62
  255. data/lib/config/locales/it.yml +0 -59
  256. data/lib/config/locales/ja.yml +0 -57
  257. data/lib/config/locales/kr.yml +0 -54
  258. data/lib/config/locales/nl.yml +0 -61
  259. data/lib/config/locales/pl.yml +0 -59
  260. data/lib/config/locales/pt-BR.yml +0 -60
  261. data/lib/config/locales/pt.yml +0 -60
  262. data/lib/config/locales/ro.yml +0 -66
  263. data/lib/config/locales/ru.yml +0 -61
  264. data/lib/config/locales/sv.yml +0 -60
  265. data/lib/config/locales/vi.yml +0 -62
  266. data/lib/config/locales/zh-CN.yml +0 -53
  267. data/lib/mongoid/collection.rb +0 -157
  268. data/lib/mongoid/collections.rb +0 -120
  269. data/lib/mongoid/collections/master.rb +0 -45
  270. data/lib/mongoid/collections/operations.rb +0 -44
  271. data/lib/mongoid/config/database.rb +0 -181
  272. data/lib/mongoid/config/replset_database.rb +0 -80
  273. data/lib/mongoid/contexts.rb +0 -25
  274. data/lib/mongoid/contexts/enumerable.rb +0 -313
  275. data/lib/mongoid/contexts/enumerable/sort.rb +0 -43
  276. data/lib/mongoid/contexts/mongo.rb +0 -487
  277. data/lib/mongoid/criterion/builder.rb +0 -34
  278. data/lib/mongoid/criterion/complex.rb +0 -84
  279. data/lib/mongoid/criterion/creational.rb +0 -34
  280. data/lib/mongoid/criterion/exclusion.rb +0 -110
  281. data/lib/mongoid/criterion/inclusion.rb +0 -290
  282. data/lib/mongoid/criterion/optional.rb +0 -259
  283. data/lib/mongoid/criterion/selector.rb +0 -177
  284. data/lib/mongoid/cursor.rb +0 -88
  285. data/lib/mongoid/default_scope.rb +0 -36
  286. data/lib/mongoid/errors/invalid_type.rb +0 -25
  287. data/lib/mongoid/extensions/array/deep_copy.rb +0 -25
  288. data/lib/mongoid/extensions/array/deletion.rb +0 -29
  289. data/lib/mongoid/extensions/false_class/equality.rb +0 -26
  290. data/lib/mongoid/extensions/hash/criteria_helpers.rb +0 -47
  291. data/lib/mongoid/extensions/hash/deep_copy.rb +0 -25
  292. data/lib/mongoid/extensions/hash/scoping.rb +0 -25
  293. data/lib/mongoid/extensions/integer/checks.rb +0 -23
  294. data/lib/mongoid/extensions/nil/collectionization.rb +0 -23
  295. data/lib/mongoid/extensions/object/checks.rb +0 -29
  296. data/lib/mongoid/extensions/object/deep_copy.rb +0 -21
  297. data/lib/mongoid/extensions/object/reflections.rb +0 -48
  298. data/lib/mongoid/extensions/object/substitutable.rb +0 -15
  299. data/lib/mongoid/extensions/object/yoda.rb +0 -44
  300. data/lib/mongoid/extensions/object_id/conversions.rb +0 -60
  301. data/lib/mongoid/extensions/proc/scoping.rb +0 -25
  302. data/lib/mongoid/extensions/string/checks.rb +0 -36
  303. data/lib/mongoid/extensions/string/conversions.rb +0 -22
  304. data/lib/mongoid/extensions/string/inflections.rb +0 -118
  305. data/lib/mongoid/extensions/symbol/checks.rb +0 -23
  306. data/lib/mongoid/extensions/symbol/inflections.rb +0 -67
  307. data/lib/mongoid/extensions/true_class/equality.rb +0 -26
  308. data/lib/mongoid/extras.rb +0 -31
  309. data/lib/mongoid/fields/internal/array.rb +0 -77
  310. data/lib/mongoid/fields/internal/big_decimal.rb +0 -63
  311. data/lib/mongoid/fields/internal/bignum.rb +0 -10
  312. data/lib/mongoid/fields/internal/binary.rb +0 -11
  313. data/lib/mongoid/fields/internal/boolean.rb +0 -58
  314. data/lib/mongoid/fields/internal/date.rb +0 -51
  315. data/lib/mongoid/fields/internal/date_time.rb +0 -28
  316. data/lib/mongoid/fields/internal/false_class.rb +0 -10
  317. data/lib/mongoid/fields/internal/fixnum.rb +0 -10
  318. data/lib/mongoid/fields/internal/float.rb +0 -47
  319. data/lib/mongoid/fields/internal/foreign_keys/array.rb +0 -88
  320. data/lib/mongoid/fields/internal/foreign_keys/object.rb +0 -56
  321. data/lib/mongoid/fields/internal/hash.rb +0 -11
  322. data/lib/mongoid/fields/internal/integer.rb +0 -59
  323. data/lib/mongoid/fields/internal/localized.rb +0 -62
  324. data/lib/mongoid/fields/internal/nil_class.rb +0 -53
  325. data/lib/mongoid/fields/internal/object.rb +0 -11
  326. data/lib/mongoid/fields/internal/object_id.rb +0 -46
  327. data/lib/mongoid/fields/internal/range.rb +0 -61
  328. data/lib/mongoid/fields/internal/set.rb +0 -57
  329. data/lib/mongoid/fields/internal/string.rb +0 -42
  330. data/lib/mongoid/fields/internal/symbol.rb +0 -43
  331. data/lib/mongoid/fields/internal/time.rb +0 -23
  332. data/lib/mongoid/fields/internal/time_with_zone.rb +0 -23
  333. data/lib/mongoid/fields/internal/timekeeping.rb +0 -122
  334. data/lib/mongoid/fields/internal/true_class.rb +0 -10
  335. data/lib/mongoid/fields/mappings.rb +0 -42
  336. data/lib/mongoid/fields/serializable.rb +0 -270
  337. data/lib/mongoid/identity.rb +0 -92
  338. data/lib/mongoid/keys.rb +0 -144
  339. data/lib/mongoid/logger.rb +0 -45
  340. data/lib/mongoid/multi_database.rb +0 -36
  341. data/lib/mongoid/named_scope.rb +0 -166
  342. data/lib/mongoid/relations/embedded/atomic.rb +0 -89
  343. data/lib/mongoid/relations/embedded/atomic/operation.rb +0 -63
  344. data/lib/mongoid/relations/embedded/atomic/pull.rb +0 -65
  345. data/lib/mongoid/relations/embedded/atomic/push_all.rb +0 -59
  346. data/lib/mongoid/relations/embedded/atomic/set.rb +0 -61
  347. data/lib/mongoid/relations/embedded/atomic/unset.rb +0 -41
  348. data/lib/mongoid/relations/referenced/batch.rb +0 -73
  349. data/lib/mongoid/relations/referenced/batch/insert.rb +0 -57
  350. data/lib/mongoid/safety.rb +0 -105
  351. data/lib/mongoid/scope.rb +0 -31
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
- module Mongoid #:nodoc:
3
- module Validations #:nodoc:
2
+ module Mongoid
3
+ module Validations
4
4
 
5
5
  # Validates whether or not a field is unique against the documents in the
6
6
  # database.
@@ -66,7 +66,7 @@ module Mongoid #:nodoc:
66
66
  # @since 2.4.10
67
67
  def add_error(document, attribute, value)
68
68
  document.errors.add(
69
- attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value)
69
+ attribute, :taken, options.except(:case_sensitive, :scope).merge(value: value)
70
70
  )
71
71
  end
72
72
 
@@ -101,11 +101,9 @@ module Mongoid #:nodoc:
101
101
  # @since 2.4.10
102
102
  def create_criteria(base, document, attribute, value)
103
103
  field = document.fields[attribute.to_s]
104
- criteria = base.paranoid ? base.unscoped.where(:deleted_at => nil) : base.unscoped
104
+ criteria = base.unscoped
105
105
  if field.try(:localized?)
106
- criterion(document, attribute, value).each_pair do |key, value|
107
- criteria.selector.store(key, value)
108
- end
106
+ criteria.selector.update(criterion(document, attribute, value))
109
107
  else
110
108
  criteria = criteria.where(criterion(document, attribute, value))
111
109
  end
@@ -127,11 +125,11 @@ module Mongoid #:nodoc:
127
125
  #
128
126
  # @since 2.3.0
129
127
  def criterion(document, attribute, value)
130
- { attribute => filter(value) }.tap do |selector|
131
- if document.persisted? && !document.embedded?
132
- selector.merge!(:_id => { "$ne" => document.id })
133
- end
128
+ selector = { attribute => filter(value) }
129
+ if document.persisted? && !document.embedded?
130
+ selector.merge!(_id: { "$ne" => document.id })
134
131
  end
132
+ selector
135
133
  end
136
134
 
137
135
  # Filter the value based on whether the check is case sensitive or not.
@@ -167,7 +165,6 @@ module Mongoid #:nodoc:
167
165
  Array.wrap(options[:scope]).each do |item|
168
166
  criteria = criteria.where(item => document.attributes[item.to_s])
169
167
  end
170
- criteria = criteria.where(:deleted_at => nil) if document.paranoid?
171
168
  criteria
172
169
  end
173
170
 
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
- module Mongoid #:nodoc
3
- VERSION = "2.8.1"
2
+ module Mongoid
3
+ VERSION = "3.0.0.rc"
4
4
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
- module Mongoid #:nodoc:
2
+ module Mongoid
3
3
 
4
4
  # Include this module to get automatic versioning of root level documents.
5
5
  # This will add a version field to the +Document+ and a has_many association
@@ -8,17 +8,17 @@ module Mongoid #:nodoc:
8
8
  extend ActiveSupport::Concern
9
9
 
10
10
  included do
11
- field :version, :type => Integer, :default => 1
11
+ field :version, type: Integer, default: 1
12
12
 
13
13
  embeds_many \
14
14
  :versions,
15
- :class_name => self.name,
16
- :validate => false,
17
- :cyclic => true,
18
- :inverse_of => nil,
19
- :versioned => true
15
+ class_name: self.name,
16
+ validate: false,
17
+ cyclic: true,
18
+ inverse_of: nil,
19
+ versioned: true
20
20
 
21
- set_callback :save, :before, :revise, :if => :revisable?
21
+ set_callback :save, :before, :revise, if: :revisable?
22
22
 
23
23
  class_attribute :version_max
24
24
  self.cyclic = true
@@ -36,17 +36,16 @@ module Mongoid #:nodoc:
36
36
  def revise
37
37
  previous = previous_revision
38
38
  if previous && versioned_attributes_changed?
39
- versions.build(
40
- previous.versioned_attributes, :without_protection => true
41
- ).attributes.delete("_id")
39
+ new_version = versions.build(
40
+ previous.versioned_attributes, without_protection: true
41
+ )
42
+ new_version._id = nil
42
43
  if version_max.present? && versions.length > version_max
43
44
  deleted = versions.first
44
45
  if deleted.paranoid?
45
46
  versions.delete_one(deleted)
46
- collection.update(
47
- atomic_selector,
48
- { "$pull" => { "versions" => { "version" => deleted.version }}}
49
- )
47
+ collection.find(atomic_selector).
48
+ update({ "$pull" => { "versions" => { "version" => deleted.version }}})
50
49
  else
51
50
  versions.delete(deleted)
52
51
  end
@@ -63,8 +62,8 @@ module Mongoid #:nodoc:
63
62
  #
64
63
  # @since 2.2.1
65
64
  def revise!
66
- new_version = versions.build(
67
- (previous_revision || self).versioned_attributes, :without_protection => true
65
+ versions.build(
66
+ (previous_revision || self).versioned_attributes, without_protection: true
68
67
  )
69
68
  versions.shift if version_max.present? && versions.length > version_max
70
69
  self.version = (version || 1 ) + 1
@@ -132,8 +131,8 @@ module Mongoid #:nodoc:
132
131
  def previous_revision
133
132
  _loading_revision do
134
133
  self.class.unscoped.
135
- where(:_id => id).
136
- any_of({ :version => version }, { :version => nil }).first
134
+ where(_id: id).
135
+ any_of({ version: version }, { version: nil }).first
137
136
  end
138
137
  end
139
138
 
@@ -160,7 +159,7 @@ module Mongoid #:nodoc:
160
159
  #
161
160
  # @since 2.0.0
162
161
  def versionless?
163
- !!@versionless
162
+ @versionless ||= false
164
163
  end
165
164
 
166
165
  # Filters fields that should not be versioned out of an attributes hash.
@@ -172,15 +171,15 @@ module Mongoid #:nodoc:
172
171
  #
173
172
  # @since 2.1.0
174
173
  def only_versioned_attributes(hash)
175
- {}.tap do |versioned|
176
- hash.except("versions").each_pair do |name, value|
177
- field = fields[name]
178
- versioned[name] = value if !field || field.versioned?
179
- end
174
+ versioned = {}
175
+ hash.except("versions").each_pair do |name, value|
176
+ field = fields[name]
177
+ versioned[name] = value if !field || field.versioned?
180
178
  end
179
+ versioned
181
180
  end
182
181
 
183
- module ClassMethods #:nodoc:
182
+ module ClassMethods
184
183
 
185
184
  # Sets the maximum number of versions to store.
186
185
  #
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
- module Rack #:nodoc:
3
- module Mongoid #:nodoc:
4
- module Middleware #:nodoc:
2
+ module Rack
3
+ module Mongoid
4
+ module Middleware
5
5
 
6
6
  # This middleware contains the behaviour needed to properly use the
7
7
  # identity map in Rack based applications.
@@ -6,7 +6,7 @@ module Mongoid
6
6
  class ConfigGenerator < Rails::Generators::Base
7
7
  desc "Creates a Mongoid configuration file at config/mongoid.yml"
8
8
 
9
- argument :database_name, :type => :string, :optional => true
9
+ argument :database_name, type: :string, optional: true
10
10
 
11
11
  def self.source_root
12
12
  @_mongoid_source_root ||= File.expand_path("../templates", __FILE__)
@@ -1,20 +1,60 @@
1
1
  development:
2
- host: localhost
3
- database: <%= database_name || app_name %>_development
4
-
5
- test:
6
- host: localhost
7
- database: <%= database_name || app_name %>_test
8
-
9
- # set these environment variables on your prod server
10
- production:
11
- host: <%%= ENV['MONGOID_HOST'] %>
12
- port: <%%= ENV['MONGOID_PORT'] %>
13
- username: <%%= ENV['MONGOID_USERNAME'] %>
14
- password: <%%= ENV['MONGOID_PASSWORD'] %>
15
- database: <%%= ENV['MONGOID_DATABASE'] %>
16
- # slaves:
17
- # - host: slave1.local
18
- # port: 27018
19
- # - host: slave2.local
20
- # port: 27019
2
+ # Configure available database sessions. (required)
3
+ sessions:
4
+ # Defines the default session. (required)
5
+ default:
6
+ # Defines the name of the default database that Mongoid can connect to.
7
+ # (required).
8
+ database: <%= database_name || app_name %>_development
9
+ # Provides the hosts the default session can connect to. Must be an array
10
+ # of host:port pairs. (required)
11
+ hosts:
12
+ - localhost:27017
13
+ options:
14
+ # Change whether the session persists in safe mode by default.
15
+ # (default: false)
16
+ # safe: false
17
+
18
+ # Change the default consistency model to :eventual or :strong.
19
+ # :eventual will send reads to secondaries, :strong sends everything
20
+ # to master. (default: :eventual)
21
+ consistency: :strong
22
+ # Configure Mongoid specific options. (optional)
23
+ options:
24
+ # Configuration for whether or not to allow access to fields that do
25
+ # not have a field definition on the model. (default: true)
26
+ # allow_dynamic_fields: true
27
+
28
+ # Enable the identity map, needed for eager loading. (default: false)
29
+ # identity_map_enabled: false
30
+
31
+ # Includes the root model name in json serialization. (default: false)
32
+ # include_root_in_json: false
33
+
34
+ # Include the _type field in serializaion. (default: false)
35
+ # include_type_for_serialization: false
36
+
37
+ # Preload all models in development, needed when models use
38
+ # inheritance. (default: false)
39
+ # preload_models: false
40
+
41
+ # Protect id and type from mass assignment. (default: true)
42
+ # protect_sensitive_fields: true
43
+
44
+ # Raise an error when performing a #find and the document is not found.
45
+ # (default: true)
46
+ # raise_not_found_error: true
47
+
48
+ # Raise an error when defining a scope with the same name as an
49
+ # existing method. (default: false)
50
+ # scope_overwrite_exception: false
51
+
52
+ # Skip the database version check, used when connecting to a db without
53
+ # admin access. (default: false)
54
+ # skip_version_check: false
55
+
56
+ # User Active Support's time zone in conversions. (default: true)
57
+ # use_activesupport_time_zone: true
58
+
59
+ # Ensure all times are UTC in the app side. (default: false)
60
+ # use_utc: false
@@ -1,18 +1,18 @@
1
1
  # encoding: utf-8
2
2
  require "rails/generators/mongoid_generator"
3
3
 
4
- module Mongoid #:nodoc:
5
- module Generators #:nodoc:
6
- class ModelGenerator < Base #:nodoc:
4
+ module Mongoid
5
+ module Generators
6
+ class ModelGenerator < Base
7
7
 
8
8
  desc "Creates a Mongoid model"
9
- argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
9
+ argument :attributes, type: :array, default: [], banner: "field:type field:type"
10
10
 
11
11
  check_class_collision
12
12
 
13
- class_option :timestamps, :type => :boolean
14
- class_option :parent, :type => :string, :desc => "The parent class for the generated model"
15
- class_option :versioning, :type => :boolean, :default => false, :desc => "Enable mongoid versioning"
13
+ class_option :timestamps, type: :boolean
14
+ class_option :parent, type: :string, desc: "The parent class for the generated model"
15
+ class_option :versioning, type: :boolean, default: false, desc: "Enable mongoid versioning"
16
16
 
17
17
  def create_model_file
18
18
  template "model.rb.tt", File.join("app/models", class_path, "#{file_name}.rb")
@@ -10,10 +10,10 @@ class <%= class_name %><%= " < #{options[:parent].classify}" if options[:parent]
10
10
  include Mongoid::Versioning
11
11
  <% end -%>
12
12
  <% attributes.reject{|attr| attr.reference?}.each do |attribute| -%>
13
- field :<%= attribute.name %>, :type => <%= attribute.type_class %>
13
+ field :<%= attribute.name %>, type: <%= attribute.type_class %>
14
14
  <% end -%>
15
15
  <% attributes.select{|attr| attr.reference? }.each do |attribute| -%>
16
- embedded_in :<%= attribute.name%>, :inverse_of => :<%= class_name.tableize %>
16
+ embedded_in :<%= attribute.name%>
17
17
  <% end -%>
18
18
  end
19
19
  <% end -%>
@@ -1,11 +1,11 @@
1
1
  # encoding: utf-8
2
2
  require "rails/generators/mongoid_generator"
3
3
 
4
- module Mongoid #:nodoc:
5
- module Generators #:nodoc:
6
- class ObserverGenerator < Base #:nodoc:
4
+ module Mongoid
5
+ module Generators
6
+ class ObserverGenerator < Base
7
7
 
8
- check_class_collision :suffix => "Observer"
8
+ check_class_collision suffix: "Observer"
9
9
 
10
10
  def create_observer_file
11
11
  template 'observer.rb.tt', File.join('app/models', class_path, "#{file_name}_observer.rb")
@@ -2,9 +2,9 @@
2
2
  require "rails/generators/named_base"
3
3
  require "rails/generators/active_model"
4
4
 
5
- module Mongoid #:nodoc:
6
- module Generators #:nodoc:
7
- class Base < ::Rails::Generators::NamedBase #:nodoc:
5
+ module Mongoid
6
+ module Generators
7
+ class Base < ::Rails::Generators::NamedBase
8
8
 
9
9
  def self.source_root
10
10
  @_mongoid_source_root ||=
@@ -21,7 +21,7 @@ module Mongoid #:nodoc:
21
21
  end
22
22
  end
23
23
 
24
- class ActiveModel < ::Rails::Generators::ActiveModel #:nodoc:
24
+ class ActiveModel < ::Rails::Generators::ActiveModel
25
25
  def self.all(klass)
26
26
  "#{klass}.all"
27
27
  end
@@ -59,7 +59,7 @@ end
59
59
 
60
60
  module Rails
61
61
  module Generators
62
- class GeneratedAttribute #:nodoc:
62
+ class GeneratedAttribute
63
63
  def type_class
64
64
  return "Time" if type.to_s == "datetime"
65
65
  return "String" if type.to_s == "text"
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
- module Rails #:nodoc:
3
- module Mongoid #:nodoc:
2
+ module Rails
3
+ module Mongoid
4
4
  extend self
5
5
 
6
6
  # Create indexes for each model given the provided pattern and the class is
@@ -15,23 +15,61 @@ module Rails #:nodoc:
15
15
  #
16
16
  # @since 2.1.0
17
17
  def create_indexes(pattern)
18
- Dir.glob(pattern).each do |file|
18
+ logger = Logger.new($stdout)
19
+ models(pattern).each do |model|
20
+ next if model.index_options.empty?
21
+ unless model.embedded?
22
+ model.create_indexes
23
+ logger.info("Creating indexes on: #{model} for: #{model.index_options.keys.join(", ")}.")
24
+ else
25
+ logger.info("Index ignored on: #{model}, please define in the root model.")
26
+ end
27
+ end
28
+ end
29
+
30
+ # Remove indexes for each model given the provided pattern and the class is
31
+ # not embedded.
32
+ #
33
+ # @example Remove all the indexes.
34
+ # Rails::Mongoid.create_indexes("app/models/**/*.rb")
35
+ #
36
+ # @param [ String ] pattern The file matching pattern.
37
+ #
38
+ # @return [ Array<String> ] The file names.
39
+ #
40
+ def remove_indexes(pattern)
41
+ logger = Logger.new($stdout)
42
+ models(pattern).each do |model|
43
+ next if model.embedded?
44
+ indexes = model.collection.indexes.map{ |doc| doc["name"] }
45
+ indexes.delete_one("_id_")
46
+ model.remove_indexes
47
+ logger.info("Removing indexes on: #{model} for: #{indexes.join(', ')}.")
48
+ end
49
+ end
50
+
51
+ # Return all models matching the pattern.
52
+ #
53
+ # @example Return all models.
54
+ # Rails::Mongoid.models("app/models/**/*.rb")
55
+ #
56
+ # @param [ String ] pattern The file matching pattern.
57
+ #
58
+ # @return [ Array<Class> ] The models.
59
+ #
60
+ def models(pattern)
61
+ Dir.glob(pattern).map do |file|
19
62
  logger = Logger.new($stdout)
20
63
  begin
21
- model = determine_model(file, logger)
64
+ determine_model(file, logger)
22
65
  rescue => e
23
66
  logger.error(%Q{Failed to determine model from #{file}:
24
67
  #{e.class}:#{e.message}
25
68
  #{e.backtrace.join("\n")}
26
69
  })
70
+ nil
27
71
  end
28
- if model
29
- model.create_indexes
30
- logger.info("Generated indexes for #{model}")
31
- else
32
- logger.info("Not a Mongoid parent model: #{file}")
33
- end
34
- end
72
+ end.flatten.compact
35
73
  end
36
74
 
37
75
  # Use the application configuration to get every model and require it, so
@@ -44,7 +82,14 @@ module Rails #:nodoc:
44
82
  # @param [ Application ] app The rails application.
45
83
  def load_models(app)
46
84
  app.config.paths["app/models"].each do |path|
47
- Dir.glob("#{path}/**/*.rb").sort.each do |file|
85
+ preload = ::Mongoid.preload_models
86
+ if preload.resizable?
87
+ files = preload.map { |model| "#{path}/#{model}.rb" }
88
+ else
89
+ files = Dir.glob("#{path}/**/*.rb")
90
+ end
91
+
92
+ files.sort.each do |file|
48
93
  load_model(file.gsub("#{path}/" , "").gsub(".rb", ""))
49
94
  end
50
95
  end
@@ -84,20 +129,19 @@ module Rails #:nodoc:
84
129
  #
85
130
  # @since 2.1.0
86
131
  def determine_model(file, logger)
87
- if file =~ /app\/models\/(.*).rb$/
88
- model_path = $1.split('/')
89
- begin
90
- parts = model_path.map { |path| path.camelize }
91
- name = parts.join("::")
92
- klass = name.constantize
93
- rescue NameError, LoadError => e
94
- logger.info("Attempted to constantize #{name}, trying without namespacing.")
95
- klass = parts.last.constantize
96
- end
97
- if klass.ancestors.include?(::Mongoid::Document) && !klass.embedded
98
- return klass
99
- end
132
+ return nil unless file =~ /app\/models\/(.*).rb$/
133
+ return nil unless logger
134
+
135
+ model_path = $1.split('/')
136
+ begin
137
+ parts = model_path.map { |path| path.camelize }
138
+ name = parts.join("::")
139
+ klass = name.constantize
140
+ rescue NameError, LoadError
141
+ logger.info("Attempted to constantize #{name}, trying without namespacing.")
142
+ klass = parts.last.constantize
100
143
  end
144
+ klass if klass.ancestors.include?(::Mongoid::Document)
101
145
  end
102
146
  end
103
147
  end