mongoid 3.0.17 → 3.0.18

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,28 @@
3
3
  For instructions on upgrading to newer versions, visit
4
4
  [mongoid.org](http://mongoid.org/en/mongoid/docs/upgrading.html).
5
5
 
6
+ ## 3.0.18
7
+
8
+ ### Resolved Issues
9
+
10
+ * \#2703 Validations on associated documents on fire if the document is
11
+ changed. (Regression)
12
+
13
+ * \#2707 Calling `find_or_create_by` or `find_by_initialize_by` off a relation
14
+ with a chained criteria or scope now properly keeps the relations intact on
15
+ the new or found document.
16
+
17
+ * \#2699 Resetting a field now removes the name from the changed attributes
18
+ list. (Subhash Bhushan)
19
+
20
+ * \#2683 Aliased fields are now supported when executing atomic operations from
21
+ criteria. (Arthur Neves)
22
+
23
+ * \#2678 Calling `Criteria#sum` with no matching documents returns `0` instead
24
+ of `nil`.
25
+
26
+ * \#2671 Matchers now correctly handle symbol keys. (Jonathan Hyman)
27
+
6
28
  ## 3.0.17
7
29
 
8
30
  ### Resolved Issues
@@ -81,7 +81,7 @@ module Mongoid
81
81
  if block_given?
82
82
  super()
83
83
  else
84
- count > 0 ? super(0) { |doc| doc.send(field) } : nil
84
+ count > 0 ? super(0) { |doc| doc.send(field) } : 0
85
85
  end
86
86
  end
87
87
 
@@ -104,7 +104,7 @@ module Mongoid
104
104
  #
105
105
  # @since 3.0.0
106
106
  def sum(field = nil)
107
- block_given? ? super() : aggregates(field)["sum"]
107
+ block_given? ? super() : aggregates(field)["sum"] || 0
108
108
  end
109
109
 
110
110
  private
@@ -15,7 +15,7 @@ module Mongoid
15
15
  #
16
16
  # @since 3.0.0
17
17
  def add_to_set(field, value)
18
- query.update_all("$addToSet" => { field => value })
18
+ query.update_all("$addToSet" => { database_field_name(field) => value })
19
19
  end
20
20
 
21
21
  # Perform an atomic $bit operation on the matching documents.
@@ -31,7 +31,7 @@ module Mongoid
31
31
  #
32
32
  # @since 3.0.0
33
33
  def bit(field, value)
34
- query.update_all("$bit" => { field => value })
34
+ query.update_all("$bit" => { database_field_name(field) => value })
35
35
  end
36
36
 
37
37
  # Perform an atomic $inc operation on the matching documents.
@@ -46,7 +46,7 @@ module Mongoid
46
46
  #
47
47
  # @since 3.0.0
48
48
  def inc(field, value)
49
- query.update_all("$inc" => { field => value })
49
+ query.update_all("$inc" => { database_field_name(field) => value })
50
50
  end
51
51
 
52
52
  # Perform an atomic $pop operation on the matching documents.
@@ -66,7 +66,7 @@ module Mongoid
66
66
  #
67
67
  # @since 3.0.0
68
68
  def pop(field, value)
69
- query.update_all("$pop" => { field => value })
69
+ query.update_all("$pop" => { database_field_name(field) => value })
70
70
  end
71
71
 
72
72
  # Perform an atomic $pull operation on the matching documents.
@@ -83,7 +83,7 @@ module Mongoid
83
83
  #
84
84
  # @since 3.0.0
85
85
  def pull(field, value)
86
- query.update_all("$pull" => { field => value })
86
+ query.update_all("$pull" => { database_field_name(field) => value })
87
87
  end
88
88
 
89
89
  # Perform an atomic $pullAll operation on the matching documents.
@@ -98,7 +98,7 @@ module Mongoid
98
98
  #
99
99
  # @since 3.0.0
100
100
  def pull_all(field, values)
101
- query.update_all("$pullAll" => { field => values })
101
+ query.update_all("$pullAll" => { database_field_name(field) => values })
102
102
  end
103
103
 
104
104
  # Perform an atomic $push operation on the matching documents.
@@ -113,7 +113,7 @@ module Mongoid
113
113
  #
114
114
  # @since 3.0.0
115
115
  def push(field, value)
116
- query.update_all("$push" => { field => value })
116
+ query.update_all("$push" => { database_field_name(field) => value })
117
117
  end
118
118
 
119
119
  # Perform an atomic $pushAll operation on the matching documents.
@@ -128,7 +128,7 @@ module Mongoid
128
128
  #
129
129
  # @since 3.0.0
130
130
  def push_all(field, values)
131
- query.update_all("$pushAll" => { field => values })
131
+ query.update_all("$pushAll" => { database_field_name(field) => values })
132
132
  end
133
133
 
134
134
  # Perform an atomic $rename of fields on the matching documents.
@@ -143,7 +143,7 @@ module Mongoid
143
143
  #
144
144
  # @since 3.0.0
145
145
  def rename(old_name, new_name)
146
- query.update_all("$rename" => { old_name.to_s => new_name.to_s })
146
+ query.update_all("$rename" => { database_field_name(old_name) => new_name.to_s })
147
147
  end
148
148
 
149
149
  # Perform an atomic $set of fields on the matching documents.
@@ -158,7 +158,7 @@ module Mongoid
158
158
  #
159
159
  # @since 3.0.0
160
160
  def set(field, value)
161
- query.update_all("$set" => { field => value })
161
+ query.update_all("$set" => { database_field_name(field) => value })
162
162
  end
163
163
 
164
164
  # Perform an atomic $unset of a field on the matching documents.
@@ -172,7 +172,7 @@ module Mongoid
172
172
  #
173
173
  # @since 3.0.0
174
174
  def unset(field)
175
- query.update_all("$unset" => { field => true })
175
+ query.update_all("$unset" => { database_field_name(field) => true })
176
176
  end
177
177
  end
178
178
  end
@@ -219,6 +219,8 @@ module Mongoid
219
219
  apply_options
220
220
  end
221
221
 
222
+ delegate(:database_field_name, to: :@klass)
223
+
222
224
  # Get the last document in the database for the criteria's selector.
223
225
  #
224
226
  # @example Get the last document.
@@ -60,8 +60,8 @@ module Mongoid
60
60
  # @return [ Document ] A non-persisted document.
61
61
  #
62
62
  # @since 2.0.0
63
- def build(attrs = {})
64
- create_document(:new, attrs)
63
+ def build(attrs = {}, &block)
64
+ create_document(:new, attrs, &block)
65
65
  end
66
66
  alias :new :build
67
67
 
@@ -102,8 +102,8 @@ module Mongoid
102
102
  # @return [ Document ] A newly created document.
103
103
  #
104
104
  # @since 2.0.0.rc.1
105
- def create(attrs = {})
106
- create_document(:create, attrs)
105
+ def create(attrs = {}, &block)
106
+ create_document(:create, attrs, &block)
107
107
  end
108
108
 
109
109
  # Create a document in the database given the selector and return it.
@@ -121,8 +121,8 @@ module Mongoid
121
121
  # @return [ Document ] A newly created document.
122
122
  #
123
123
  # @since 3.0.0
124
- def create!(attrs = {})
125
- create_document(:create!, attrs)
124
+ def create!(attrs = {}, &block)
125
+ create_document(:create!, attrs, &block)
126
126
  end
127
127
 
128
128
  # Get the documents from the embedded criteria.
@@ -245,6 +245,32 @@ module Mongoid
245
245
  for_ids(ids).execute_or_raise(ids, args.multi_arged?)
246
246
  end
247
247
 
248
+ # Find the first +Document+ given the conditions, or creates a new document
249
+ # with the conditions that were supplied.
250
+ #
251
+ # @example Find or create the document.
252
+ # Person.find_or_create_by(:attribute => "value")
253
+ #
254
+ # @param [ Hash ] attrs The attributes to check.
255
+ #
256
+ # @return [ Document ] A matching or newly created document.
257
+ def find_or_create_by(attrs = {}, &block)
258
+ find_or(:create, attrs, &block)
259
+ end
260
+
261
+ # Find the first +Document+ given the conditions, or initializes a new document
262
+ # with the conditions that were supplied.
263
+ #
264
+ # @example Find or initialize the document.
265
+ # Person.find_or_initialize_by(:attribute => "value")
266
+ #
267
+ # @param [ Hash ] attrs The attributes to check.
268
+ #
269
+ # @return [ Document ] A matching or newly initialized document.
270
+ def find_or_initialize_by(attrs = {}, &block)
271
+ find_or(:new, attrs, &block)
272
+ end
273
+
248
274
  # Adds a criterion to the +Criteria+ that specifies an id that must be matched.
249
275
  #
250
276
  # @example Add a single id criteria.
@@ -600,15 +626,29 @@ module Mongoid
600
626
  # @return [ Document ] The new or saved document.
601
627
  #
602
628
  # @since 3.0.0
603
- def create_document(method, attrs = {})
629
+ def create_document(method, attrs = {}, &block)
604
630
  klass.__send__(method,
605
631
  selector.reduce(attrs) do |hash, (key, value)|
606
632
  unless key.to_s =~ /\$/ || value.is_a?(Hash)
607
633
  hash[key] = value
608
634
  end
609
635
  hash
610
- end
611
- )
636
+ end, &block)
637
+ end
638
+
639
+ # Find the first object or create/initialize it.
640
+ #
641
+ # @api private
642
+ #
643
+ # @example Find or perform an action.
644
+ # Person.find_or(:create, :name => "Dev")
645
+ #
646
+ # @param [ Symbol ] method The method to invoke.
647
+ # @param [ Hash ] attrs The attributes to query or set.
648
+ #
649
+ # @return [ Document ] The first or new document.
650
+ def find_or(method, attrs = {}, &block)
651
+ where(attrs).first || send(method, attrs, &block)
612
652
  end
613
653
 
614
654
  # Clone or dup the current +Criteria+. This will return a new criteria with
@@ -239,7 +239,7 @@ module Mongoid
239
239
  #
240
240
  # @since 2.4.0
241
241
  def reset_attribute!(attr)
242
- attributes[attr] = changed_attributes[attr] if attribute_changed?(attr)
242
+ attributes[attr] = changed_attributes.delete(attr) if attribute_changed?(attr)
243
243
  end
244
244
 
245
245
  module ClassMethods
@@ -337,6 +337,7 @@ module Mongoid
337
337
  doc.instance_variable_set(:@attributes, attributes)
338
338
  doc.apply_defaults
339
339
  IdentityMap.set(doc) unless _loading_revision?
340
+ yield(doc) if block_given?
340
341
  doc.run_callbacks(:initialize) unless doc._initialize_callbacks.empty?
341
342
  doc
342
343
  end
@@ -7,8 +7,24 @@ module Mongoid
7
7
  extend Origin::Forwardable
8
8
 
9
9
  select_with :with_default_scope
10
- delegate :aggregates, :avg, :each, :extras, :find_and_modify, :includes,
11
- :map_reduce, :max, :min, :sum, :update, :update_all, to: :with_default_scope
10
+
11
+ # These are methods defined on the criteria that should also be accessible
12
+ # directly from the the class level.
13
+ delegate \
14
+ :aggregates,
15
+ :avg,
16
+ :each,
17
+ :extras,
18
+ :find_and_modify,
19
+ :find_or_create_by,
20
+ :find_or_initialize_by,
21
+ :includes,
22
+ :map_reduce,
23
+ :max,
24
+ :min,
25
+ :sum,
26
+ :update,
27
+ :update_all, to: :with_default_scope
12
28
 
13
29
  # Returns a count of records in the database.
14
30
  # If you want to specify conditions use where.
@@ -61,32 +77,6 @@ module Mongoid
61
77
  with_default_scope.find(*args)
62
78
  end
63
79
 
64
- # Find the first +Document+ given the conditions, or creates a new document
65
- # with the conditions that were supplied.
66
- #
67
- # @example Find or create the document.
68
- # Person.find_or_create_by(:attribute => "value")
69
- #
70
- # @param [ Hash ] attrs The attributes to check.
71
- #
72
- # @return [ Document ] A matching or newly created document.
73
- def find_or_create_by(attrs = {}, &block)
74
- find_or(:create, attrs, &block)
75
- end
76
-
77
- # Find the first +Document+ given the conditions, or initializes a new document
78
- # with the conditions that were supplied.
79
- #
80
- # @example Find or initialize the document.
81
- # Person.find_or_initialize_by(:attribute => "value")
82
- #
83
- # @param [ Hash ] attrs The attributes to check.
84
- #
85
- # @return [ Document ] A matching or newly initialized document.
86
- def find_or_initialize_by(attrs = {}, &block)
87
- find_or(:new, attrs, &block)
88
- end
89
-
90
80
  # Find the first +Document+ given the conditions, or raises
91
81
  # Mongoid::Errors::DocumentNotFound
92
82
  #
@@ -128,20 +118,5 @@ module Mongoid
128
118
  def last
129
119
  with_default_scope.last
130
120
  end
131
-
132
- protected
133
-
134
- # Find the first object or create/initialize it.
135
- #
136
- # @example Find or perform an action.
137
- # Person.find_or(:create, :name => "Dev")
138
- #
139
- # @param [ Symbol ] method The method to invoke.
140
- # @param [ Hash ] attrs The attributes to query or set.
141
- #
142
- # @return [ Document ] The first or new document.
143
- def find_or(method, attrs = {}, &block)
144
- where(attrs).first || send(method, attrs, &block)
145
- end
146
121
  end
147
122
  end
@@ -21,7 +21,7 @@ module Mongoid
21
21
  module Strategies
22
22
  extend self
23
23
 
24
- MATCHERS = {
24
+ MATCHERS = HashWithIndifferentAccess.new({
25
25
  "$all" => Matchers::All,
26
26
  "$and" => Matchers::And,
27
27
  "$exists" => Matchers::Exists,
@@ -34,7 +34,7 @@ module Mongoid
34
34
  "$nin" => Matchers::Nin,
35
35
  "$or" => Matchers::Or,
36
36
  "$size" => Matchers::Size
37
- }
37
+ })
38
38
 
39
39
  # Get the matcher for the supplied key and value. Will determine the class
40
40
  # name from the key.
@@ -59,8 +59,8 @@ module Mongoid
59
59
  end
60
60
  else
61
61
  case key
62
- when "$or" then Matchers::Or.new(value, document)
63
- when "$and" then Matchers::And.new(value, document)
62
+ when "$or", :$or then Matchers::Or.new(value, document)
63
+ when "$and", :$and then Matchers::And.new(value, document)
64
64
  else Default.new(extract_attribute(document, key))
65
65
  end
66
66
  end
@@ -30,7 +30,7 @@ module Mongoid
30
30
  begin
31
31
  document.begin_validate
32
32
  valid = Array.wrap(value).collect do |doc|
33
- if doc.nil?
33
+ if doc.nil? || !doc.changed?
34
34
  true
35
35
  else
36
36
  doc.validated? ? true : doc.valid?
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid
3
- VERSION = "3.0.17"
3
+ VERSION = "3.0.18"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.17
4
+ version: 3.0.18
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-11 00:00:00.000000000 Z
12
+ date: 2013-01-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel