mongoid-pre 2.0.0.beta1 → 2.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. data/.watchr +15 -10
  2. data/HISTORY +342 -0
  3. data/README.rdoc +8 -1
  4. data/Rakefile +2 -3
  5. data/VERSION +1 -1
  6. data/lib/mongoid/associations/{embedded_in.rb → belongs_to.rb} +4 -4
  7. data/lib/mongoid/associations/belongs_to_related.rb +9 -6
  8. data/lib/mongoid/associations/{embeds_many.rb → has_many.rb} +20 -33
  9. data/lib/mongoid/associations/has_many_related.rb +4 -28
  10. data/lib/mongoid/associations/{embeds_one.rb → has_one.rb} +6 -6
  11. data/lib/mongoid/associations/options.rb +6 -1
  12. data/lib/mongoid/associations.rb +32 -41
  13. data/lib/mongoid/attributes.rb +6 -13
  14. data/lib/mongoid/collection.rb +1 -2
  15. data/lib/mongoid/commands/delete.rb +1 -1
  16. data/lib/mongoid/commands/delete_all.rb +1 -4
  17. data/lib/mongoid/commands/destroy.rb +1 -1
  18. data/lib/mongoid/commands/destroy_all.rb +1 -3
  19. data/lib/mongoid/commands/save.rb +0 -1
  20. data/lib/mongoid/commands.rb +13 -2
  21. data/lib/mongoid/components.rb +1 -6
  22. data/lib/mongoid/config.rb +1 -5
  23. data/lib/mongoid/contexts/enumerable.rb +17 -54
  24. data/lib/mongoid/contexts/mongo.rb +38 -101
  25. data/lib/mongoid/contexts/paging.rb +2 -2
  26. data/lib/mongoid/contexts.rb +0 -21
  27. data/lib/mongoid/criteria.rb +73 -15
  28. data/lib/mongoid/criterion/inclusion.rb +0 -2
  29. data/lib/mongoid/criterion/optional.rb +2 -10
  30. data/lib/mongoid/document.rb +30 -41
  31. data/lib/mongoid/extensions.rb +0 -15
  32. data/lib/mongoid/field.rb +7 -20
  33. data/lib/mongoid/fields.rb +10 -15
  34. data/lib/mongoid/finders.rb +98 -10
  35. data/lib/mongoid/identity.rb +2 -8
  36. data/lib/mongoid/named_scope.rb +0 -2
  37. data/lib/mongoid/validations/associated.rb +8 -3
  38. data/lib/mongoid/validations/uniqueness.rb +7 -2
  39. data/lib/mongoid/validations.rb +2 -2
  40. data/lib/mongoid/versioning.rb +1 -1
  41. data/lib/mongoid.rb +5 -21
  42. data/mongoid.gemspec +19 -59
  43. data/spec/integration/mongoid/associations_spec.rb +3 -42
  44. data/spec/integration/mongoid/attributes_spec.rb +2 -2
  45. data/spec/integration/mongoid/commands_spec.rb +13 -27
  46. data/spec/integration/mongoid/contexts/enumerable_spec.rb +0 -13
  47. data/spec/integration/mongoid/criteria_spec.rb +3 -50
  48. data/spec/integration/mongoid/document_spec.rb +5 -72
  49. data/spec/integration/mongoid/finders_spec.rb +28 -85
  50. data/spec/models/address.rb +3 -3
  51. data/spec/models/animal.rb +2 -2
  52. data/spec/models/country_code.rb +2 -2
  53. data/spec/models/game.rb +1 -2
  54. data/spec/models/inheritance.rb +5 -5
  55. data/spec/models/location.rb +2 -2
  56. data/spec/models/name.rb +3 -3
  57. data/spec/models/namespacing.rb +2 -2
  58. data/spec/models/patient.rb +0 -2
  59. data/spec/models/person.rb +4 -6
  60. data/spec/models/pet.rb +3 -3
  61. data/spec/models/pet_owner.rb +3 -3
  62. data/spec/models/phone.rb +3 -3
  63. data/spec/models/post.rb +1 -1
  64. data/spec/models/translation.rb +2 -2
  65. data/spec/models/vet_visit.rb +2 -2
  66. data/spec/spec_helper.rb +2 -2
  67. data/spec/unit/mongoid/associations/belongs_to_related_spec.rb +0 -4
  68. data/spec/unit/mongoid/associations/{embedded_in_spec.rb → belongs_to_spec.rb} +11 -11
  69. data/spec/unit/mongoid/associations/has_many_related_spec.rb +14 -45
  70. data/spec/unit/mongoid/associations/{embeds_many_spec.rb → has_many_spec.rb} +34 -79
  71. data/spec/unit/mongoid/associations/{embeds_one_spec.rb → has_one_spec.rb} +18 -18
  72. data/spec/unit/mongoid/associations/options_spec.rb +19 -20
  73. data/spec/unit/mongoid/associations_spec.rb +12 -74
  74. data/spec/unit/mongoid/attributes_spec.rb +51 -83
  75. data/spec/unit/mongoid/collection_spec.rb +0 -46
  76. data/spec/unit/mongoid/commands/delete_all_spec.rb +8 -9
  77. data/spec/unit/mongoid/commands/delete_spec.rb +3 -6
  78. data/spec/unit/mongoid/commands/destroy_all_spec.rb +2 -0
  79. data/spec/unit/mongoid/commands/destroy_spec.rb +3 -10
  80. data/spec/unit/mongoid/commands_spec.rb +11 -20
  81. data/spec/unit/mongoid/config_spec.rb +0 -18
  82. data/spec/unit/mongoid/contexts/enumerable_spec.rb +11 -198
  83. data/spec/unit/mongoid/contexts/mongo_spec.rb +54 -357
  84. data/spec/unit/mongoid/criteria_spec.rb +78 -107
  85. data/spec/unit/mongoid/criterion/exclusion_spec.rb +13 -3
  86. data/spec/unit/mongoid/criterion/inclusion_spec.rb +19 -25
  87. data/spec/unit/mongoid/criterion/optional_spec.rb +18 -25
  88. data/spec/unit/mongoid/document_spec.rb +34 -71
  89. data/spec/unit/mongoid/extensions/array/conversions_spec.rb +2 -2
  90. data/spec/unit/mongoid/extensions/datetime/conversions_spec.rb +3 -0
  91. data/spec/unit/mongoid/extensions/object/conversions_spec.rb +2 -16
  92. data/spec/unit/mongoid/extensions/proc/scoping_spec.rb +1 -1
  93. data/spec/unit/mongoid/field_spec.rb +0 -62
  94. data/spec/unit/mongoid/fields_spec.rb +0 -33
  95. data/spec/unit/mongoid/finders_spec.rb +1 -37
  96. data/spec/unit/mongoid/identity_spec.rb +6 -47
  97. data/spec/unit/mongoid/named_scope_spec.rb +2 -15
  98. data/spec/unit/mongoid/scope_spec.rb +1 -1
  99. data/spec/unit/mongoid/validations/uniqueness_spec.rb +3 -3
  100. data/spec/unit/mongoid_spec.rb +2 -11
  101. metadata +14 -64
  102. data/lib/mongoid/associations/meta_data.rb +0 -28
  103. data/lib/mongoid/contexts/ids.rb +0 -25
  104. data/lib/mongoid/deprecation.rb +0 -22
  105. data/lib/mongoid/dirty.rb +0 -203
  106. data/lib/mongoid/extensions/big_decimal/conversions.rb +0 -19
  107. data/lib/mongoid/extensions/binary/conversions.rb +0 -17
  108. data/lib/mongoid/extensions/objectid/conversions.rb +0 -15
  109. data/lib/mongoid/extras.rb +0 -61
  110. data/lib/mongoid/javascript/functions.yml +0 -37
  111. data/lib/mongoid/javascript.rb +0 -21
  112. data/lib/mongoid/observable.rb +0 -30
  113. data/lib/mongoid/paths.rb +0 -54
  114. data/lib/mongoid/persistence/command.rb +0 -20
  115. data/lib/mongoid/persistence/insert.rb +0 -71
  116. data/lib/mongoid/persistence/update.rb +0 -78
  117. data/lib/mongoid/persistence.rb +0 -27
  118. data/lib/mongoid/state.rb +0 -32
  119. data/spec/integration/mongoid/dirty_spec.rb +0 -70
  120. data/spec/integration/mongoid/persistence/update_spec.rb +0 -46
  121. data/spec/models/callbacks.rb +0 -18
  122. data/spec/unit/mongoid/associations/meta_data_spec.rb +0 -88
  123. data/spec/unit/mongoid/contexts_spec.rb +0 -25
  124. data/spec/unit/mongoid/deprecation_spec.rb +0 -24
  125. data/spec/unit/mongoid/dirty_spec.rb +0 -286
  126. data/spec/unit/mongoid/extensions/big_decimal/conversions_spec.rb +0 -22
  127. data/spec/unit/mongoid/extensions/binary/conversions_spec.rb +0 -22
  128. data/spec/unit/mongoid/extras_spec.rb +0 -102
  129. data/spec/unit/mongoid/javascript_spec.rb +0 -48
  130. data/spec/unit/mongoid/observable_spec.rb +0 -46
  131. data/spec/unit/mongoid/paths_spec.rb +0 -124
  132. data/spec/unit/mongoid/persistence/insert_spec.rb +0 -175
  133. data/spec/unit/mongoid/persistence/update_spec.rb +0 -148
  134. data/spec/unit/mongoid/persistence_spec.rb +0 -40
  135. data/spec/unit/mongoid/state_spec.rb +0 -83
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc:
3
3
  module Associations #:nodoc:
4
- class EmbedsOne #:nodoc:
4
+ class HasOne #:nodoc:
5
5
  include Proxy
6
6
 
7
7
  # Build a new object for the association.
@@ -46,7 +46,7 @@ module Mongoid #:nodoc:
46
46
  end
47
47
 
48
48
  class << self
49
- # Preferred method of instantiating a new +EmbedsOne+, since nil values
49
+ # Preferred method of instantiating a new +HasOne+, since nil values
50
50
  # will be handled properly.
51
51
  #
52
52
  # Options:
@@ -56,7 +56,7 @@ module Mongoid #:nodoc:
56
56
  #
57
57
  # Returns:
58
58
  #
59
- # A new +EmbedsOne+ association proxy.
59
+ # A new +HasOne+ association proxy.
60
60
  def instantiate(document, options)
61
61
  attributes = document.raw_attributes[options.name]
62
62
  return nil if attributes.blank?
@@ -65,7 +65,7 @@ module Mongoid #:nodoc:
65
65
 
66
66
  # Returns the macro used to create the association.
67
67
  def macro
68
- :embeds_one
68
+ :has_one
69
69
  end
70
70
 
71
71
  # Perform an update of the relationship of the parent and child. This
@@ -79,11 +79,11 @@ module Mongoid #:nodoc:
79
79
  #
80
80
  # Example:
81
81
  #
82
- # <tt>EmbedsOne.update({:first_name => "Hank"}, person, options)</tt>
82
+ # <tt>HasOne.update({:first_name => "Hank"}, person, options)</tt>
83
83
  #
84
84
  # Returns:
85
85
  #
86
- # A new +EmbedsOne+ association proxy.
86
+ # A new +HasOne+ association proxy.
87
87
  def update(child, parent, options)
88
88
  child.assimilate(parent, options)
89
89
  instantiate(parent, options)
@@ -21,7 +21,7 @@ module Mongoid #:nodoc:
21
21
 
22
22
  # Return the foreign key based off the association name.
23
23
  def foreign_key
24
- @attributes[:foreign_key] || klass.name.to_s.foreign_key
24
+ name.to_s.foreign_key
25
25
  end
26
26
 
27
27
  # Returns the name of the inverse_of association
@@ -42,6 +42,11 @@ module Mongoid #:nodoc:
42
42
  @attributes[:name].to_s
43
43
  end
44
44
 
45
+ # Returns the parent foreign key association name.
46
+ def parent_key
47
+ @attributes[:parent_key]
48
+ end
49
+
45
50
  # Returns whether or not this association is polymorphic.
46
51
  def polymorphic
47
52
  @attributes[:polymorphic] == true
@@ -1,13 +1,11 @@
1
1
  # encoding: utf-8
2
2
  require "mongoid/associations/proxy"
3
+ require "mongoid/associations/belongs_to"
3
4
  require "mongoid/associations/belongs_to_related"
4
- require "mongoid/associations/embedded_in"
5
- require "mongoid/associations/embeds_many"
6
- require "mongoid/associations/embeds_one"
5
+ require "mongoid/associations/has_many"
7
6
  require "mongoid/associations/has_many_related"
7
+ require "mongoid/associations/has_one"
8
8
  require "mongoid/associations/has_one_related"
9
- require "mongoid/associations/options"
10
- require "mongoid/associations/meta_data"
11
9
 
12
10
  module Mongoid # :nodoc:
13
11
  module Associations #:nodoc:
@@ -26,18 +24,17 @@ module Mongoid # :nodoc:
26
24
 
27
25
  # Updates all the one-to-many relational associations for the name.
28
26
  def update_associations(name)
29
- send(name).each { |doc| doc.save } if new_record?
27
+ send(name).each { |doc| doc.save }
30
28
  end
31
29
 
32
30
  # Update the one-to-one relational association for the name.
33
31
  def update_association(name)
34
32
  association = send(name)
35
- association.save if new_record? && !association.nil?
33
+ association.save unless association.nil?
36
34
  end
37
35
  end
38
36
 
39
37
  module ClassMethods
40
-
41
38
  # Adds the association back to the parent document. This macro is
42
39
  # necessary to set the references from the child back to the parent
43
40
  # document. If a child does not define this association calling
@@ -51,20 +48,20 @@ module Mongoid # :nodoc:
51
48
  #
52
49
  # class Person
53
50
  # include Mongoid::Document
54
- # embeds_many :addresses
51
+ # has_many :addresses
55
52
  # end
56
53
  #
57
54
  # class Address
58
55
  # include Mongoid::Document
59
- # embedded_in :person, :inverse_of => :addresses
56
+ # belongs_to :person, :inverse_of => :addresses
60
57
  # end
61
- def embedded_in(name, options = {}, &block)
58
+ def belongs_to(name, options = {}, &block)
62
59
  unless options.has_key?(:inverse_of)
63
60
  raise Errors::InvalidOptions.new("Options for belongs_to association must include :inverse_of")
64
61
  end
65
62
  self.embedded = true
66
63
  add_association(
67
- Associations::EmbeddedIn,
64
+ Associations::BelongsTo,
68
65
  Associations::Options.new(
69
66
  options.merge(:name => name, :extend => block)
70
67
  )
@@ -86,12 +83,14 @@ module Mongoid # :nodoc:
86
83
  # end
87
84
  #
88
85
  def belongs_to_related(name, options = {}, &block)
89
- opts = Associations::Options.new(
90
- options.merge(:name => name, :extend => block, :foreign_key => foreign_key(name, options))
86
+ field "#{name.to_s}_id"
87
+ index "#{name.to_s}_id" unless self.embedded
88
+ add_association(
89
+ Associations::BelongsToRelated,
90
+ Associations::Options.new(
91
+ options.merge(:name => name, :extend => block)
91
92
  )
92
- add_association(Associations::BelongsToRelated, opts)
93
- field(opts.foreign_key, :type => Mongoid.use_object_ids ? Mongo::ObjectID : String)
94
- index(opts.foreign_key) unless self.embedded
93
+ )
95
94
  end
96
95
 
97
96
  # Adds the association from a parent document to its children. The name
@@ -106,24 +105,22 @@ module Mongoid # :nodoc:
106
105
  #
107
106
  # class Person
108
107
  # include Mongoid::Document
109
- # embeds_many :addresses
108
+ # has_many :addresses
110
109
  # end
111
110
  #
112
111
  # class Address
113
112
  # include Mongoid::Document
114
- # embedded_in :person, :inverse_of => :addresses
113
+ # belongs_to :person, :inverse_of => :addresses
115
114
  # end
116
- def embeds_many(name, options = {}, &block)
115
+ def has_many(name, options = {}, &block)
117
116
  add_association(
118
- Associations::EmbedsMany,
117
+ Associations::HasMany,
119
118
  Associations::Options.new(
120
119
  options.merge(:name => name, :extend => block)
121
120
  )
122
121
  )
123
122
  end
124
123
 
125
- alias :embed_many :embeds_many
126
-
127
124
  # Adds a relational association from the Document to many Documents in
128
125
  # another database or collection.
129
126
  #
@@ -139,9 +136,10 @@ module Mongoid # :nodoc:
139
136
  # end
140
137
  #
141
138
  def has_many_related(name, options = {}, &block)
142
- add_association(Associations::HasManyRelated,
139
+ add_association(
140
+ Associations::HasManyRelated,
143
141
  Associations::Options.new(
144
- options.merge(:name => name, :foreign_key => foreign_key(self.name, options), :extend => block)
142
+ options.merge(:name => name, :parent_key => self.name.foreign_key, :extend => block)
145
143
  )
146
144
  )
147
145
  before_save do |document|
@@ -161,25 +159,23 @@ module Mongoid # :nodoc:
161
159
  #
162
160
  # class Person
163
161
  # include Mongoid::Document
164
- # embeds_one :name
162
+ # has_many :addresses
165
163
  # end
166
164
  #
167
- # class Name
165
+ # class Address
168
166
  # include Mongoid::Document
169
- # embedded_in :person
167
+ # belongs_to :person
170
168
  # end
171
- def embeds_one(name, options = {}, &block)
169
+ def has_one(name, options = {}, &block)
172
170
  opts = Associations::Options.new(
173
171
  options.merge(:name => name, :extend => block)
174
172
  )
175
- type = Associations::EmbedsOne
173
+ type = Associations::HasOne
176
174
  add_association(type, opts)
177
175
  add_builder(type, opts)
178
176
  add_creator(type, opts)
179
177
  end
180
178
 
181
- alias :embed_one :embeds_one
182
-
183
179
  # Adds a relational association from the Document to one Document in
184
180
  # another database or collection.
185
181
  #
@@ -197,7 +193,7 @@ module Mongoid # :nodoc:
197
193
  add_association(
198
194
  Associations::HasOneRelated,
199
195
  Associations::Options.new(
200
- options.merge(:name => name, :foreign_key => foreign_key(name, options), :extend => block)
196
+ options.merge(:name => name, :parent_key => self.name.foreign_key, :extend => block)
201
197
  )
202
198
  )
203
199
  before_save do |document|
@@ -226,7 +222,7 @@ module Mongoid # :nodoc:
226
222
  # getters for the associations will perform the necessary memoization.
227
223
  def add_association(type, options)
228
224
  name = options.name.to_s
229
- associations[name] = MetaData.new(type, options)
225
+ associations[name] = type
230
226
  define_method(name) do
231
227
  memoized(name) { type.instantiate(self, options) }
232
228
  end
@@ -240,7 +236,7 @@ module Mongoid # :nodoc:
240
236
  def add_builder(type, options)
241
237
  name = options.name.to_s
242
238
  define_method("build_#{name}") do |attrs|
243
- reset(name) { type.new(self, (attrs || {}).stringify_keys, options) }
239
+ reset(name) { type.new(self, attrs.stringify_keys, options) }
244
240
  end
245
241
  end
246
242
 
@@ -250,14 +246,9 @@ module Mongoid # :nodoc:
250
246
  name = options.name.to_s
251
247
  define_method("create_#{name}") do |attrs|
252
248
  document = send("build_#{name}", attrs)
253
- document.run_callbacks(:create) { document.save }; document
249
+ document.save; document
254
250
  end
255
251
  end
256
-
257
- # Find the foreign key.
258
- def foreign_key(name, options)
259
- options[:foreign_key] || name.to_s.foreign_key
260
- end
261
252
  end
262
253
  end
263
254
  end
@@ -37,11 +37,10 @@ module Mongoid #:nodoc:
37
37
  (attrs || {}).each_pair do |key, value|
38
38
  if set_allowed?(key)
39
39
  @attributes[key.to_s] = value
40
- elsif write_allowed?(key)
41
- send("#{key}=", value)
40
+ else
41
+ send("#{key}=", value) if value
42
42
  end
43
43
  end
44
- setup_modifications
45
44
  end
46
45
 
47
46
  # Read a value from the +Document+ attributes. If the value does not exist
@@ -102,7 +101,7 @@ module Mongoid #:nodoc:
102
101
  # there is any.
103
102
  def write_attribute(name, value)
104
103
  access = name.to_s
105
- modify(access, @attributes[access], fields[access].set(value))
104
+ @attributes[access] = fields[access].set(value)
106
105
  notify unless id.blank?
107
106
  end
108
107
 
@@ -125,7 +124,6 @@ module Mongoid #:nodoc:
125
124
  identify if id.blank?
126
125
  notify
127
126
  end
128
- alias :attributes= :write_attributes
129
127
 
130
128
  protected
131
129
  # Return true if dynamic field setting is enabled.
@@ -144,11 +142,6 @@ module Mongoid #:nodoc:
144
142
  end
145
143
  end
146
144
 
147
- # Return true if writing to the given field is allowed
148
- def write_allowed?(key)
149
- return true unless fields[key.to_s]
150
- fields[key.to_s].accessible?
151
- end
152
145
  end
153
146
 
154
147
  module ClassMethods
@@ -159,8 +152,8 @@ module Mongoid #:nodoc:
159
152
  #
160
153
  # class Person
161
154
  # include Mongoid::Document
162
- # embeds_one :name
163
- # embeds_many :addresses
155
+ # has_one :name
156
+ # has_many :addresses
164
157
  #
165
158
  # accepts_nested_attributes_for :name, :addresses
166
159
  # end
@@ -172,7 +165,7 @@ module Mongoid #:nodoc:
172
165
  reject(attrs, options)
173
166
  association = send(name)
174
167
  if association
175
- observe(association, true)
168
+ update(association, true)
176
169
  association.nested_build(attrs)
177
170
  else
178
171
  send("build_#{name}", attrs)
@@ -30,8 +30,7 @@ module Mongoid #:nodoc
30
30
  #
31
31
  # Either a +Master+ or +Slaves+ collection.
32
32
  def directed(options = {})
33
- options.delete(:cache)
34
- enslave = options.delete(:enslave) || @klass.enslaved?
33
+ enslave = options.delete(:enslave)
35
34
  enslave ? master_or_slaves : master
36
35
  end
37
36
 
@@ -9,7 +9,7 @@ module Mongoid #:nodoc:
9
9
  #
10
10
  # doc: A new +Document+ that is going to be deleted.
11
11
  def self.execute(doc)
12
- doc.destroyed = true if delete(doc)
12
+ delete(doc)
13
13
  end
14
14
  end
15
15
  end
@@ -14,11 +14,8 @@ module Mongoid #:nodoc:
14
14
  #
15
15
  # <tt>DeleteAll.execute(Person, :conditions => { :field => "value" })</tt>
16
16
  def self.execute(klass, params = {})
17
- safe = Mongoid.persist_in_safe_mode
18
17
  collection = klass.collection
19
- selector = (params[:conditions] || {}).merge(:_type => klass.name)
20
- matching = collection.find(selector).count
21
- collection.remove(selector, :safe => safe) ? matching : 0
18
+ collection.remove((params[:conditions] || {}).merge(:_type => klass.name))
22
19
  end
23
20
  end
24
21
  end
@@ -10,7 +10,7 @@ module Mongoid #:nodoc:
10
10
  #
11
11
  # doc: A new +Document+ that is going to be destroyed.
12
12
  def self.execute(doc)
13
- doc.run_callbacks(:destroy) { doc.destroyed = true if delete(doc) }
13
+ doc.run_callbacks(:destroy) { delete(doc) }
14
14
  end
15
15
  end
16
16
  end
@@ -16,9 +16,7 @@ module Mongoid #:nodoc:
16
16
  def self.execute(klass, params)
17
17
  conditions = params[:conditions] || {}
18
18
  params[:conditions] = conditions.merge(:_type => klass.name)
19
- documents = klass.find(:all, params)
20
- matching = documents.count
21
- documents.each { |doc| Destroy.execute(doc) }; matching
19
+ klass.find(:all, params).each { |doc| Destroy.execute(doc) }
22
20
  end
23
21
  end
24
22
  end
@@ -20,7 +20,6 @@ module Mongoid #:nodoc:
20
20
  else
21
21
  doc.collection.save(doc.raw_attributes, :safe => Mongoid.persist_in_safe_mode)
22
22
  end
23
- doc.move_changes if saved
24
23
  return false unless saved
25
24
  end
26
25
  return true
@@ -55,7 +55,12 @@ module Mongoid #:nodoc:
55
55
  # Returns: true if validation passes, false if not.
56
56
  def save(validate = true)
57
57
  save = lambda { Save.execute(self, validate) }
58
- new_record? ? run_callbacks(:create, &save) : save.call
58
+ begin
59
+ return new_record? ? run_callbacks(:create, &save) : save.call
60
+ rescue Mongo::OperationFailure => e
61
+ errors.add(:mongoid, e.message)
62
+ return false
63
+ end
59
64
  end
60
65
 
61
66
  # Save the +Document+, dangerously. Before and after save callbacks will
@@ -110,7 +115,11 @@ module Mongoid #:nodoc:
110
115
  # Returns: the +Document+.
111
116
  def create(attributes = {})
112
117
  document = new(attributes)
113
- Create.execute(document)
118
+ begin
119
+ Create.execute(document)
120
+ rescue Mongo::OperationFailure => e
121
+ document.errors.add(:mongoid, e.message)
122
+ end
114
123
  document
115
124
  end
116
125
 
@@ -156,6 +165,8 @@ module Mongoid #:nodoc:
156
165
  def destroy_all(conditions = {})
157
166
  DestroyAll.execute(self, conditions)
158
167
  end
168
+
159
169
  end
170
+
160
171
  end
161
172
  end
@@ -12,17 +12,12 @@ module Mongoid #:nodoc
12
12
  include Mongoid::Attributes
13
13
  include Mongoid::Callbacks
14
14
  include Mongoid::Commands
15
- include Mongoid::Dirty
16
- include Mongoid::Extras
17
15
  include Mongoid::Fields
18
16
  include Mongoid::Indexes
19
17
  include Mongoid::Matchers
20
18
  include Mongoid::Memoization
21
- include Mongoid::Observable
22
- include Mongoid::Paths
23
- include Mongoid::Persistence
24
- include Mongoid::State
25
19
  include Mongoid::Validations
20
+ include Observable
26
21
  extend ActiveModel::Translation
27
22
  extend Mongoid::Finders
28
23
  extend Mongoid::NamedScope
@@ -8,19 +8,15 @@ module Mongoid #:nodoc
8
8
  :reconnect_time,
9
9
  :parameterize_keys,
10
10
  :persist_in_safe_mode,
11
- :persist_types,
12
- :raise_not_found_error,
13
- :use_object_ids
11
+ :raise_not_found_error
14
12
 
15
13
  # Defaults the configuration options to true.
16
14
  def initialize
17
15
  @allow_dynamic_fields = true
18
16
  @parameterize_keys = true
19
17
  @persist_in_safe_mode = true
20
- @persist_types = true
21
18
  @raise_not_found_error = true
22
19
  @reconnect_time = 3
23
- @use_object_ids = false
24
20
  end
25
21
 
26
22
  # Sets the Mongo::DB master database to be used. If the object trying to me
@@ -2,11 +2,10 @@
2
2
  module Mongoid #:nodoc:
3
3
  module Contexts #:nodoc:
4
4
  class Enumerable
5
- include Ids, Paging
6
- attr_reader :criteria
5
+ include Paging
6
+ attr_reader :selector, :options, :documents
7
7
 
8
- delegate :blank?, :empty?, :first, :last, :to => :execute
9
- delegate :documents, :options, :selector, :to => :criteria
8
+ delegate :first, :last, :to => :execute
10
9
 
11
10
  # Return aggregation counts of the grouped documents. This will count by
12
11
  # the first field provided in the fields array.
@@ -20,33 +19,19 @@ module Mongoid #:nodoc:
20
19
  counts
21
20
  end
22
21
 
23
- # Get the average value for the supplied field.
24
- #
25
- # Example:
26
- #
27
- # <tt>context.avg(:age)</tt>
28
- #
29
- # Returns:
30
- #
31
- # A numeric value that is the average.
32
- def avg(field)
33
- total = sum(field)
34
- total ? (total.to_f / count) : nil
35
- end
36
-
37
22
  # Gets the number of documents in the array. Delegates to size.
38
23
  def count
39
- @count ||= documents.size
24
+ @count ||= @documents.size
40
25
  end
41
26
 
42
- # Gets an array of distinct values for the supplied field across the
43
- # entire array or the susbset given the criteria.
27
+ # Groups the documents by the first field supplied in the field options.
44
28
  #
45
- # Example:
29
+ # Returns:
46
30
  #
47
- # <tt>context.distinct(:title)</tt>
48
- def distinct(field)
49
- execute.collect { |doc| doc.send(field) }.uniq
31
+ # A +Hash+ with field values as keys, arrays of documents as values.
32
+ def group
33
+ field = @options[:fields].first
34
+ @documents.group_by { |doc| doc.send(field) }
50
35
  end
51
36
 
52
37
  # Enumerable implementation of execute. Returns matching documents for
@@ -56,17 +41,7 @@ module Mongoid #:nodoc:
56
41
  #
57
42
  # An +Array+ of documents that matched the selector.
58
43
  def execute(paginating = false)
59
- limit(documents.select { |document| document.matches?(selector) })
60
- end
61
-
62
- # Groups the documents by the first field supplied in the field options.
63
- #
64
- # Returns:
65
- #
66
- # A +Hash+ with field values as keys, arrays of documents as values.
67
- def group
68
- field = options[:fields].first
69
- documents.group_by { |doc| doc.send(field) }
44
+ limit(@documents.select { |document| document.matches?(@selector) })
70
45
  end
71
46
 
72
47
  # Create the new enumerable context. This will need the selector and
@@ -75,19 +50,9 @@ module Mongoid #:nodoc:
75
50
  #
76
51
  # Example:
77
52
  #
78
- # <tt>Mongoid::Contexts::Enumerable.new(criteria)</tt>
79
- def initialize(criteria)
80
- @criteria = criteria
81
- end
82
-
83
- # Iterate over each +Document+ in the results. This can take an optional
84
- # block to pass to each argument in the results.
85
- #
86
- # Example:
87
- #
88
- # <tt>context.iterate { |doc| p doc }</tt>
89
- def iterate(&block)
90
- execute.each(&block)
53
+ # <tt>Mongoid::Contexts::Enumerable.new(selector, options, docs)</tt>
54
+ def initialize(selector, options, documents)
55
+ @selector, @options, @documents = selector, options, documents
91
56
  end
92
57
 
93
58
  # Get the largest value for the field in all the documents.
@@ -121,7 +86,7 @@ module Mongoid #:nodoc:
121
86
  #
122
87
  # The numerical sum of all the document field values.
123
88
  def sum(field)
124
- sum = documents.inject(nil) do |memo, doc|
89
+ sum = @documents.inject(nil) do |memo, doc|
125
90
  value = doc.send(field)
126
91
  memo ? memo += value : value
127
92
  end
@@ -130,7 +95,7 @@ module Mongoid #:nodoc:
130
95
  protected
131
96
  # If the field exists, perform the comparison and set if true.
132
97
  def determine(field, operator)
133
- matching = documents.inject(nil) do |memo, doc|
98
+ matching = @documents.inject(nil) do |memo, doc|
134
99
  value = doc.send(field)
135
100
  (memo && memo.send(operator, value)) ? memo : value
136
101
  end
@@ -138,11 +103,9 @@ module Mongoid #:nodoc:
138
103
 
139
104
  # Limits the result set if skip and limit options.
140
105
  def limit(documents)
141
- skip, limit = options[:skip], options[:limit]
106
+ skip, limit = @options[:skip], @options[:limit]
142
107
  if skip && limit
143
108
  return documents.slice(skip, limit)
144
- elsif limit
145
- return documents.first(limit)
146
109
  end
147
110
  documents
148
111
  end