mongoid 1.9.5 → 2.0.0.alpha

Sign up to get free protection for your applications and to get access to all the features.
Files changed (213) hide show
  1. data/.gitignore +6 -0
  2. data/.watchr +29 -0
  3. data/Rakefile +52 -0
  4. data/VERSION +1 -0
  5. data/caliper.yml +4 -0
  6. data/lib/mongoid.rb +20 -9
  7. data/lib/mongoid/associations.rb +100 -123
  8. data/lib/mongoid/associations/belongs_to_related.rb +3 -2
  9. data/lib/mongoid/associations/{embeds_many.rb → embed_many.rb} +29 -90
  10. data/lib/mongoid/associations/{embeds_one.rb → embed_one.rb} +7 -8
  11. data/lib/mongoid/associations/embedded_in.rb +4 -12
  12. data/lib/mongoid/associations/has_many_related.rb +4 -52
  13. data/lib/mongoid/associations/has_one_related.rb +4 -8
  14. data/lib/mongoid/associations/meta_data.rb +1 -2
  15. data/lib/mongoid/associations/options.rb +1 -6
  16. data/lib/mongoid/associations/proxy.rb +21 -14
  17. data/lib/mongoid/attributes.rb +13 -33
  18. data/lib/mongoid/callbacks.rb +11 -16
  19. data/lib/mongoid/collection.rb +3 -4
  20. data/lib/mongoid/collections/master.rb +2 -3
  21. data/lib/mongoid/collections/mimic.rb +46 -0
  22. data/lib/mongoid/collections/slaves.rb +2 -3
  23. data/lib/mongoid/commands.rb +161 -0
  24. data/lib/mongoid/commands/create.rb +19 -0
  25. data/lib/mongoid/commands/delete.rb +16 -0
  26. data/lib/mongoid/commands/delete_all.rb +23 -0
  27. data/lib/mongoid/commands/deletion.rb +18 -0
  28. data/lib/mongoid/commands/destroy.rb +17 -0
  29. data/lib/mongoid/commands/destroy_all.rb +23 -0
  30. data/lib/mongoid/commands/save.rb +29 -0
  31. data/lib/mongoid/components.rb +6 -4
  32. data/lib/mongoid/config.rb +15 -134
  33. data/lib/mongoid/contexts.rb +1 -1
  34. data/lib/mongoid/contexts/enumerable.rb +1 -1
  35. data/lib/mongoid/contexts/mongo.rb +1 -1
  36. data/lib/mongoid/contexts/paging.rb +2 -10
  37. data/lib/mongoid/criterion/inclusion.rb +0 -17
  38. data/lib/mongoid/criterion/optional.rb +1 -1
  39. data/lib/mongoid/cursor.rb +1 -0
  40. data/lib/mongoid/document.rb +45 -49
  41. data/lib/mongoid/errors.rb +1 -32
  42. data/lib/mongoid/extensions.rb +10 -12
  43. data/lib/mongoid/extensions/array/conversions.rb +6 -8
  44. data/lib/mongoid/extensions/big_decimal/conversions.rb +2 -2
  45. data/lib/mongoid/extensions/boolean/conversions.rb +2 -8
  46. data/lib/mongoid/extensions/date/conversions.rb +4 -13
  47. data/lib/mongoid/extensions/datetime/conversions.rb +6 -1
  48. data/lib/mongoid/extensions/float/conversions.rb +1 -5
  49. data/lib/mongoid/extensions/hash/assimilation.rb +3 -12
  50. data/lib/mongoid/extensions/hash/conversions.rb +4 -34
  51. data/lib/mongoid/extensions/integer/conversions.rb +1 -5
  52. data/lib/mongoid/extensions/nil/assimilation.rb +0 -4
  53. data/lib/mongoid/extensions/object/conversions.rb +2 -8
  54. data/lib/mongoid/extensions/objectid/conversions.rb +1 -1
  55. data/lib/mongoid/extensions/string/conversions.rb +1 -1
  56. data/lib/mongoid/extensions/symbol/inflections.rb +1 -1
  57. data/lib/mongoid/extensions/time/conversions.rb +18 -0
  58. data/lib/mongoid/factory.rb +1 -2
  59. data/lib/mongoid/field.rb +2 -9
  60. data/lib/mongoid/fields.rb +7 -11
  61. data/lib/mongoid/finders.rb +2 -2
  62. data/lib/mongoid/identity.rb +4 -4
  63. data/lib/mongoid/indexes.rb +7 -10
  64. data/lib/mongoid/memoization.rb +2 -8
  65. data/lib/mongoid/named_scope.rb +5 -0
  66. data/lib/mongoid/observable.rb +1 -1
  67. data/lib/mongoid/paths.rb +22 -30
  68. data/lib/mongoid/state.rb +21 -28
  69. data/lib/mongoid/timestamps.rb +1 -1
  70. data/lib/mongoid/validations.rb +51 -0
  71. data/lib/mongoid/validations/associated.rb +32 -0
  72. data/lib/mongoid/validations/locale/en.yml +4 -0
  73. data/lib/mongoid/validations/uniqueness.rb +22 -0
  74. data/lib/mongoid/versioning.rb +1 -2
  75. data/mongoid.gemspec +408 -0
  76. data/perf/benchmark.rb +77 -0
  77. data/spec/integration/mongoid/associations_spec.rb +340 -0
  78. data/spec/integration/mongoid/attributes_spec.rb +22 -0
  79. data/spec/integration/mongoid/commands_spec.rb +227 -0
  80. data/spec/integration/mongoid/contexts/enumerable_spec.rb +33 -0
  81. data/spec/integration/mongoid/criteria_spec.rb +272 -0
  82. data/spec/integration/mongoid/document_spec.rb +650 -0
  83. data/spec/integration/mongoid/extensions_spec.rb +22 -0
  84. data/spec/integration/mongoid/finders_spec.rb +119 -0
  85. data/spec/integration/mongoid/inheritance_spec.rb +137 -0
  86. data/spec/integration/mongoid/named_scope_spec.rb +46 -0
  87. data/spec/models/address.rb +39 -0
  88. data/spec/models/animal.rb +6 -0
  89. data/spec/models/callbacks.rb +18 -0
  90. data/spec/models/comment.rb +8 -0
  91. data/spec/models/country_code.rb +6 -0
  92. data/spec/models/employer.rb +5 -0
  93. data/spec/models/game.rb +7 -0
  94. data/spec/models/inheritance.rb +56 -0
  95. data/spec/models/location.rb +5 -0
  96. data/spec/models/mixed_drink.rb +4 -0
  97. data/spec/models/name.rb +13 -0
  98. data/spec/models/namespacing.rb +11 -0
  99. data/spec/models/patient.rb +4 -0
  100. data/spec/models/person.rb +99 -0
  101. data/spec/models/pet.rb +7 -0
  102. data/spec/models/pet_owner.rb +6 -0
  103. data/spec/models/phone.rb +7 -0
  104. data/spec/models/post.rb +15 -0
  105. data/spec/models/translation.rb +5 -0
  106. data/spec/models/vet_visit.rb +5 -0
  107. data/spec/spec.opts +3 -0
  108. data/spec/spec_helper.rb +31 -0
  109. data/spec/unit/mongoid/associations/belongs_to_related_spec.rb +145 -0
  110. data/spec/unit/mongoid/associations/embed_many_spec.rb +516 -0
  111. data/spec/unit/mongoid/associations/embed_one_spec.rb +282 -0
  112. data/spec/unit/mongoid/associations/embedded_in_spec.rb +193 -0
  113. data/spec/unit/mongoid/associations/has_many_related_spec.rb +418 -0
  114. data/spec/unit/mongoid/associations/has_one_related_spec.rb +179 -0
  115. data/spec/unit/mongoid/associations/meta_data_spec.rb +88 -0
  116. data/spec/unit/mongoid/associations/options_spec.rb +192 -0
  117. data/spec/unit/mongoid/associations_spec.rb +595 -0
  118. data/spec/unit/mongoid/attributes_spec.rb +507 -0
  119. data/spec/unit/mongoid/callbacks_spec.rb +55 -0
  120. data/spec/unit/mongoid/collection_spec.rb +187 -0
  121. data/spec/unit/mongoid/collections/cyclic_iterator_spec.rb +75 -0
  122. data/spec/unit/mongoid/collections/master_spec.rb +41 -0
  123. data/spec/unit/mongoid/collections/mimic_spec.rb +43 -0
  124. data/spec/unit/mongoid/collections/slaves_spec.rb +81 -0
  125. data/spec/unit/mongoid/commands/create_spec.rb +31 -0
  126. data/spec/unit/mongoid/commands/delete_all_spec.rb +58 -0
  127. data/spec/unit/mongoid/commands/delete_spec.rb +38 -0
  128. data/spec/unit/mongoid/commands/destroy_all_spec.rb +21 -0
  129. data/spec/unit/mongoid/commands/destroy_spec.rb +51 -0
  130. data/spec/unit/mongoid/commands/save_spec.rb +107 -0
  131. data/spec/unit/mongoid/commands_spec.rb +270 -0
  132. data/spec/unit/mongoid/config_spec.rb +172 -0
  133. data/spec/unit/mongoid/contexts/enumerable_spec.rb +421 -0
  134. data/spec/unit/mongoid/contexts/mongo_spec.rb +682 -0
  135. data/spec/unit/mongoid/contexts_spec.rb +25 -0
  136. data/spec/unit/mongoid/criteria_spec.rb +824 -0
  137. data/spec/unit/mongoid/criterion/complex_spec.rb +19 -0
  138. data/spec/unit/mongoid/criterion/exclusion_spec.rb +91 -0
  139. data/spec/unit/mongoid/criterion/inclusion_spec.rb +219 -0
  140. data/spec/unit/mongoid/criterion/optional_spec.rb +319 -0
  141. data/spec/unit/mongoid/cursor_spec.rb +74 -0
  142. data/spec/unit/mongoid/deprecation_spec.rb +24 -0
  143. data/spec/unit/mongoid/document_spec.rb +818 -0
  144. data/spec/unit/mongoid/errors_spec.rb +103 -0
  145. data/spec/unit/mongoid/extensions/array/accessors_spec.rb +50 -0
  146. data/spec/unit/mongoid/extensions/array/assimilation_spec.rb +24 -0
  147. data/spec/unit/mongoid/extensions/array/conversions_spec.rb +35 -0
  148. data/spec/unit/mongoid/extensions/array/parentization_spec.rb +20 -0
  149. data/spec/unit/mongoid/extensions/big_decimal/conversions_spec.rb +22 -0
  150. data/spec/unit/mongoid/extensions/binary/conversions_spec.rb +22 -0
  151. data/spec/unit/mongoid/extensions/boolean/conversions_spec.rb +49 -0
  152. data/spec/unit/mongoid/extensions/date/conversions_spec.rb +102 -0
  153. data/spec/unit/mongoid/extensions/datetime/conversions_spec.rb +67 -0
  154. data/spec/unit/mongoid/extensions/float/conversions_spec.rb +61 -0
  155. data/spec/unit/mongoid/extensions/hash/accessors_spec.rb +184 -0
  156. data/spec/unit/mongoid/extensions/hash/assimilation_spec.rb +46 -0
  157. data/spec/unit/mongoid/extensions/hash/conversions_spec.rb +21 -0
  158. data/spec/unit/mongoid/extensions/hash/criteria_helpers_spec.rb +17 -0
  159. data/spec/unit/mongoid/extensions/hash/scoping_spec.rb +14 -0
  160. data/spec/unit/mongoid/extensions/integer/conversions_spec.rb +61 -0
  161. data/spec/unit/mongoid/extensions/nil/assimilation_spec.rb +24 -0
  162. data/spec/unit/mongoid/extensions/object/conversions_spec.rb +57 -0
  163. data/spec/unit/mongoid/extensions/proc/scoping_spec.rb +34 -0
  164. data/spec/unit/mongoid/extensions/string/conversions_spec.rb +17 -0
  165. data/spec/unit/mongoid/extensions/string/inflections_spec.rb +208 -0
  166. data/spec/unit/mongoid/extensions/symbol/inflections_spec.rb +91 -0
  167. data/spec/unit/mongoid/extensions/time/conversions_spec.rb +70 -0
  168. data/spec/unit/mongoid/extras_spec.rb +102 -0
  169. data/spec/unit/mongoid/factory_spec.rb +31 -0
  170. data/spec/unit/mongoid/field_spec.rb +143 -0
  171. data/spec/unit/mongoid/fields_spec.rb +181 -0
  172. data/spec/unit/mongoid/finders_spec.rb +404 -0
  173. data/spec/unit/mongoid/identity_spec.rb +109 -0
  174. data/spec/unit/mongoid/indexes_spec.rb +93 -0
  175. data/spec/unit/mongoid/javascript_spec.rb +48 -0
  176. data/spec/unit/mongoid/matchers/all_spec.rb +27 -0
  177. data/spec/unit/mongoid/matchers/default_spec.rb +27 -0
  178. data/spec/unit/mongoid/matchers/exists_spec.rb +56 -0
  179. data/spec/unit/mongoid/matchers/gt_spec.rb +39 -0
  180. data/spec/unit/mongoid/matchers/gte_spec.rb +49 -0
  181. data/spec/unit/mongoid/matchers/in_spec.rb +27 -0
  182. data/spec/unit/mongoid/matchers/lt_spec.rb +39 -0
  183. data/spec/unit/mongoid/matchers/lte_spec.rb +49 -0
  184. data/spec/unit/mongoid/matchers/ne_spec.rb +27 -0
  185. data/spec/unit/mongoid/matchers/nin_spec.rb +27 -0
  186. data/spec/unit/mongoid/matchers/size_spec.rb +27 -0
  187. data/spec/unit/mongoid/matchers_spec.rb +329 -0
  188. data/spec/unit/mongoid/memoization_spec.rb +75 -0
  189. data/spec/unit/mongoid/named_scope_spec.rb +123 -0
  190. data/spec/unit/mongoid/observable_spec.rb +46 -0
  191. data/spec/unit/mongoid/paths_spec.rb +124 -0
  192. data/spec/unit/mongoid/scope_spec.rb +240 -0
  193. data/spec/unit/mongoid/state_spec.rb +83 -0
  194. data/spec/unit/mongoid/timestamps_spec.rb +25 -0
  195. data/spec/unit/mongoid/validations/associated_spec.rb +103 -0
  196. data/spec/unit/mongoid/validations/uniqueness_spec.rb +47 -0
  197. data/spec/unit/mongoid/validations_spec.rb +190 -0
  198. data/spec/unit/mongoid/versioning_spec.rb +41 -0
  199. data/spec/unit/mongoid_spec.rb +46 -0
  200. metadata +316 -110
  201. data/lib/mongoid/collections.rb +0 -41
  202. data/lib/mongoid/concern.rb +0 -31
  203. data/lib/mongoid/dirty.rb +0 -253
  204. data/lib/mongoid/extensions/time_conversions.rb +0 -35
  205. data/lib/mongoid/persistence.rb +0 -222
  206. data/lib/mongoid/persistence/command.rb +0 -39
  207. data/lib/mongoid/persistence/insert.rb +0 -50
  208. data/lib/mongoid/persistence/insert_embedded.rb +0 -38
  209. data/lib/mongoid/persistence/remove.rb +0 -39
  210. data/lib/mongoid/persistence/remove_all.rb +0 -37
  211. data/lib/mongoid/persistence/remove_embedded.rb +0 -50
  212. data/lib/mongoid/persistence/update.rb +0 -63
  213. data/lib/mongoid/version.rb +0 -4
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Commands
4
+ class Create
5
+ # Performs a create of the supplied Document, with the necessary
6
+ # callbacks. It then delegates to the Save command.
7
+ #
8
+ # Options:
9
+ #
10
+ # doc: A new +Document+ that is going to be persisted.
11
+ #
12
+ # Returns: +Document+.
13
+ def self.execute(doc, validate = true)
14
+ doc.run_callbacks(:create) { Save.execute(doc, validate) }
15
+ doc
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Commands
4
+ class Delete
5
+ extend Deletion
6
+ # Performs a delete of the supplied +Document+ without any callbacks.
7
+ #
8
+ # Options:
9
+ #
10
+ # doc: A new +Document+ that is going to be deleted.
11
+ def self.execute(doc)
12
+ doc.destroyed = true if delete(doc)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Commands
4
+ class DeleteAll
5
+ # Performs a delete of the all the +Documents+ that match the criteria
6
+ # supplied.
7
+ #
8
+ # Options:
9
+ #
10
+ # params: A set of conditions to find the +Documents+ by.
11
+ # klass: The class of the +Document+ to execute the find on.
12
+ #
13
+ # Example:
14
+ #
15
+ # <tt>DeleteAll.execute(Person, :conditions => { :field => "value" })</tt>
16
+ def self.execute(klass, params = {})
17
+ safe = Mongoid.persist_in_safe_mode
18
+ collection = klass.collection
19
+ collection.remove((params[:conditions] || {}).merge(:_type => klass.name), :safe => safe)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ module Commands #:nodoc
4
+ module Deletion #:nodoc
5
+ # If the +Document+ has a parent, delete it from the parent's attributes,
6
+ # otherwise delete it from it's collection.
7
+ def delete(doc)
8
+ parent = doc._parent
9
+ if parent
10
+ parent.remove(doc)
11
+ parent.save
12
+ else
13
+ doc.collection.remove(:_id => doc.id)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Commands
4
+ class Destroy
5
+ extend Deletion
6
+ # Performs a destroy of the supplied +Document+, with the necessary
7
+ # callbacks. It then deletes the record from the collection.
8
+ #
9
+ # Options:
10
+ #
11
+ # doc: A new +Document+ that is going to be destroyed.
12
+ def self.execute(doc)
13
+ doc.run_callbacks(:destroy) { doc.destroyed = true if delete(doc) }
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Commands
4
+ class DestroyAll
5
+ # Performs a destroy of the all the +Documents+ that match the criteria
6
+ # supplied. Will execute all the destroy callbacks for each +Document+.
7
+ #
8
+ # Options:
9
+ #
10
+ # params: A set of conditions to find the +Documents+ by.
11
+ # klass: The class of the +Document+ to execute the find on.
12
+ #
13
+ # Example:
14
+ #
15
+ # <tt>DestroyAll.execute(Person, :conditions => { :field => "value" })</tt>
16
+ def self.execute(klass, params)
17
+ conditions = params[:conditions] || {}
18
+ params[:conditions] = conditions.merge(:_type => klass.name)
19
+ klass.find(:all, params).each { |doc| Destroy.execute(doc) }; true
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Commands
4
+ class Save
5
+ # Performs a save of the supplied +Document+, handling all associated
6
+ # callbacks and validation.
7
+ #
8
+ # Options:
9
+ #
10
+ # doc: A +Document+ that is going to be persisted.
11
+ #
12
+ # Returns: +true+ if validation passes, +false+ if not.
13
+ def self.execute(doc, validate = true)
14
+ return false if validate && !doc.valid?
15
+ doc.run_callbacks :save do
16
+ parent = doc._parent
17
+ doc.new_record = false
18
+ saved = if parent
19
+ Save.execute(parent, validate)
20
+ else
21
+ doc.collection.save(doc.raw_attributes, :safe => Mongoid.persist_in_safe_mode)
22
+ end
23
+ return false unless saved
24
+ end
25
+ return true
26
+ end
27
+ end
28
+ end
29
+ end
@@ -5,11 +5,13 @@ module Mongoid #:nodoc
5
5
  included do
6
6
  # All modules that a +Document+ is composed of are defined in this
7
7
  # module, to keep the document class from getting too cluttered.
8
+ include ActiveModel::Conversion
9
+ include ActiveModel::Naming
10
+ include ActiveModel::Serialization
8
11
  include Mongoid::Associations
9
12
  include Mongoid::Attributes
10
13
  include Mongoid::Callbacks
11
- include Mongoid::Collections
12
- include Mongoid::Dirty
14
+ include Mongoid::Commands
13
15
  include Mongoid::Extras
14
16
  include Mongoid::Fields
15
17
  include Mongoid::Indexes
@@ -17,9 +19,9 @@ module Mongoid #:nodoc
17
19
  include Mongoid::Memoization
18
20
  include Mongoid::Observable
19
21
  include Mongoid::Paths
20
- include Mongoid::Persistence
21
22
  include Mongoid::State
22
- include Validatable
23
+ include Mongoid::Validations
24
+ extend ActiveModel::Translation
23
25
  extend Mongoid::Finders
24
26
  extend Mongoid::NamedScope
25
27
  end
@@ -1,6 +1,4 @@
1
1
  # encoding: utf-8
2
- require "uri"
3
-
4
2
  module Mongoid #:nodoc
5
3
  class Config #:nodoc
6
4
  include Singleton
@@ -10,44 +8,23 @@ module Mongoid #:nodoc
10
8
  :reconnect_time,
11
9
  :parameterize_keys,
12
10
  :persist_in_safe_mode,
11
+ :persist_types,
13
12
  :raise_not_found_error,
14
13
  :use_object_ids
15
14
 
16
15
  # Defaults the configuration options to true.
17
16
  def initialize
18
- reset
19
- end
20
-
21
- # Sets whether the times returned from the database are in UTC or local time.
22
- # If you omit this setting, then times will be returned in
23
- # the local time zone.
24
- #
25
- # Example:
26
- #
27
- # <tt>Config.use_utc = true</tt>
28
- #
29
- # Returns:
30
- #
31
- # A boolean
32
- def use_utc=(value)
33
- @use_utc = value || false
17
+ @allow_dynamic_fields = true
18
+ @parameterize_keys = true
19
+ @persist_in_safe_mode = true
20
+ @persist_types = true
21
+ @raise_not_found_error = true
22
+ @reconnect_time = 3
23
+ @use_object_ids = false
34
24
  end
35
25
 
36
- # Returns whether times are return from the database in UTC. If
37
- # this setting is false, then times will be returned in the local time zone.
38
- #
39
- # Example:
40
- #
41
- # <tt>Config.use_utc</tt>
42
- #
43
- # Returns:
44
- #
45
- # A boolean
46
- attr_reader :use_utc
47
- alias_method :use_utc?, :use_utc
48
-
49
- # Sets the Mongo::DB master database to be used. If the object trying to be
50
- # set is not a valid +Mongo::DB+, then an error will be raised.
26
+ # Sets the Mongo::DB master database to be used. If the object trying to me
27
+ # set is not a valid +Mongo::DB+, then an error will be raise.
51
28
  #
52
29
  # Example:
53
30
  #
@@ -57,7 +34,7 @@ module Mongoid #:nodoc
57
34
  #
58
35
  # The Master DB instance.
59
36
  def master=(db)
60
- check_database!(db)
37
+ raise Errors::InvalidDatabase.new(db) unless db.kind_of?(Mongo::DB)
61
38
  @master = db
62
39
  end
63
40
 
@@ -78,8 +55,8 @@ module Mongoid #:nodoc
78
55
  alias :database :master
79
56
  alias :database= :master=
80
57
 
81
- # Sets the Mongo::DB slave databases to be used. If the objects provided
82
- # are not valid +Mongo::DBs+ an error will be raised.
58
+ # Sets the Mongo::DB slave databases to be used. If the objects trying to me
59
+ # set are not valid +Mongo::DBs+, then an error will be raise.
83
60
  #
84
61
  # Example:
85
62
  #
@@ -87,12 +64,9 @@ module Mongoid #:nodoc
87
64
  #
88
65
  # Returns:
89
66
  #
90
- # The slave DB instances.
67
+ # The slaves DB instances.
91
68
  def slaves=(dbs)
92
- return unless dbs
93
- dbs.each do |db|
94
- check_database!(db)
95
- end
69
+ dbs.each { |db| raise Errors::InvalidDatabase.new(db) unless db.kind_of?(Mongo::DB) }
96
70
  @slaves = dbs
97
71
  end
98
72
 
@@ -108,98 +82,5 @@ module Mongoid #:nodoc
108
82
  def slaves
109
83
  @slaves
110
84
  end
111
-
112
- # Confiure mongoid from a hash that was usually parsed out of yml.
113
- #
114
- # Example:
115
- #
116
- # <tt>Mongoid::Config.instance.from_hash({})</tt>
117
- def from_hash(settings)
118
- _master(settings)
119
- _slaves(settings)
120
- settings.except("database").each_pair do |name, value|
121
- send("#{name}=", value) if respond_to?(name)
122
- end
123
- end
124
-
125
- # Reset the configuration options to the defaults.
126
- #
127
- # Example:
128
- #
129
- # <tt>config.reset</tt>
130
- def reset
131
- @allow_dynamic_fields = true
132
- @parameterize_keys = true
133
- @persist_in_safe_mode = true
134
- @raise_not_found_error = true
135
- @reconnect_time = 3
136
- @use_object_ids = false
137
- @time_zone = nil
138
- end
139
-
140
- protected
141
-
142
- # Check if the database is valid and the correct version.
143
- #
144
- # Example:
145
- #
146
- # <tt>config.check_database!</tt>
147
- def check_database!(database)
148
- raise Errors::InvalidDatabase.new(database) unless database.kind_of?(Mongo::DB)
149
- version = database.connection.server_version
150
- raise Errors::UnsupportedVersion.new(version) if version < Mongoid::MONGODB_VERSION
151
- end
152
-
153
- # Get a Rails logger or stdout logger.
154
- #
155
- # Example:
156
- #
157
- # <tt>config.logger</tt>
158
- def logger
159
- defined?(Rails) ? Rails.logger : Logger.new($stdout)
160
- end
161
-
162
- # Get a master database from settings.
163
- #
164
- # Example:
165
- #
166
- # <tt>config._master({}, "test")</tt>
167
- def _master(settings)
168
- self.master = database_from_hash(settings)
169
- end
170
-
171
- # Get a bunch-o-slaves from settings and names.
172
- #
173
- # Example:
174
- #
175
- # <tt>config._slaves({}, "test")</tt>
176
- def _slaves(settings)
177
- self.slaves = settings["slaves"].to_a.map do |slave|
178
- database_from_hash({"database" => master.name}.merge(slave), :slave_ok => true)
179
- end
180
- end
181
-
182
- def database_from_hash(settings, connection_options={})
183
- mongo_uri = settings["uri"].present? ? URI.parse(settings["uri"]) : OpenStruct.new
184
-
185
- name = settings["database"] || mongo_uri.path.to_s.sub("/", "")
186
- host = settings["host"] || mongo_uri.host || "localhost"
187
- port = settings["port"] || mongo_uri.port || 27017
188
- pool_size = settings["pool_size"] || 1
189
- username = settings["username"] || mongo_uri.user
190
- password = settings["password"] || mongo_uri.password
191
-
192
- local_options = {
193
- :logger => logger,
194
- :pool_size => pool_size
195
- }.merge(connection_options)
196
-
197
- Mongo::Connection.new(host, port, local_options).tap do |connection|
198
- if username || password
199
- connection.add_auth(name, username, password)
200
- connection.apply_saved_authentication
201
- end
202
- end.db(name)
203
- end
204
85
  end
205
86
  end
@@ -15,7 +15,7 @@ module Mongoid
15
15
  #
16
16
  # <tt>Contexts.context_for(criteria)</tt>
17
17
  def self.context_for(criteria)
18
- if criteria.klass.embedded?
18
+ if criteria.klass.embedded
19
19
  return Contexts::Enumerable.new(criteria)
20
20
  end
21
21
  Contexts::Mongo.new(criteria)
@@ -6,7 +6,7 @@ module Mongoid #:nodoc:
6
6
  attr_reader :criteria
7
7
 
8
8
  delegate :blank?, :empty?, :first, :last, :to => :execute
9
- delegate :klass, :documents, :options, :selector, :to => :criteria
9
+ delegate :documents, :options, :selector, :to => :criteria
10
10
 
11
11
  # Return aggregation counts of the grouped documents. This will count by
12
12
  # the first field provided in the fields array.
@@ -133,7 +133,7 @@ module Mongoid #:nodoc:
133
133
  # <tt>Mongoid::Contexts::Mongo.new(criteria)</tt>
134
134
  def initialize(criteria)
135
135
  @criteria = criteria
136
- if klass.hereditary
136
+ if klass.hereditary && Mongoid.persist_types
137
137
  criteria.in(:_type => criteria.klass._types)
138
138
  end
139
139
  criteria.enslave if klass.enslaved?
@@ -6,20 +6,12 @@ module Mongoid #:nodoc:
6
6
  #
7
7
  # Example:
8
8
  #
9
- # <tt>context.paginate(:page => 6, :per_page => 25)</tt>
9
+ # <tt>context.paginate</tt>
10
10
  #
11
11
  # Returns:
12
12
  #
13
13
  # A collection of documents paginated.
14
- # All previous <tt>limit</tt> and <tt>skip</tt> call will be ignored.
15
- def paginate(pager_options={})
16
- if pager_options[:per_page]
17
- options[:limit] = pager_options[:per_page].to_i
18
- if pager_options[:page]
19
- options[:skip] = (pager_options[:page].to_i - 1) * pager_options[:per_page].to_i
20
- end
21
- end
22
-
14
+ def paginate
23
15
  @collection ||= execute(true)
24
16
  WillPaginate::Collection.create(page, per_page, count) do |pager|
25
17
  pager.replace(@collection.to_a)
@@ -64,23 +64,6 @@ module Mongoid #:nodoc:
64
64
  end
65
65
  alias :any_in :in
66
66
 
67
- # Adds a criterion to the +Criteria+ that specifies values to do
68
- # geospacial searches by. The field must be indexed with the "2d" option.
69
- #
70
- # Options:
71
- #
72
- # attributes: A +Hash+ where the keys are the field names and the values are
73
- # +Arrays+ of [latitude, longitude] pairs.
74
- #
75
- # Example:
76
- #
77
- # <tt>criteria.near(:field1 => [30, -44])</tt>
78
- #
79
- # Returns: <tt>self</tt>
80
- def near(attributes = {})
81
- update_selector(attributes, "$near")
82
- end
83
-
84
67
  # Adds a criterion to the +Criteria+ that specifies values that must
85
68
  # be matched in order to return results. This is similar to a SQL "WHERE"
86
69
  # clause. This is the actual selector that will be provided to MongoDB,