mongoid 3.0.4 → 3.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,7 +3,38 @@
3
3
  For instructions on upgrading to newer versions, visit
4
4
  [mongoid.org](http://mongoid.org/docs/upgrading.html).
5
5
 
6
- ## 3.0.4 (branch: 3.0.0-stable)
6
+ ## 3.0.5 (branch 3.0.0-stable)
7
+
8
+ * \#2313 Fixed deserialization of `nil` `TimeWithZone` fields. (nagachika)
9
+
10
+ * \#2311 `Document#changes` no longer returns `nil` values for Array and Hash
11
+ fields that were only accessed and didn't actually change. Regression from 2.4.x.
12
+
13
+ * \#2310 Setting a many to many duplicate successively in memory no longer clears
14
+ the inverse foreign keys.
15
+
16
+ * \#2309 Allow embeds_one relations to be set with hashes more than just the
17
+ initial set.
18
+
19
+ * \#2308 Ensure documents retrieved via `#find` on `has_many` and
20
+ `has_and_belongs_to_many` relations are kept in memory.
21
+
22
+ * \#2304 Default scopes now properly merge instead of overwrite when more
23
+ than one is defined as per expectations with AR. (Kirill Maksimov)
24
+
25
+ * \#2300 Ensure reloading refreshes the document in the identity map.
26
+
27
+ * \#2298 Protect against many to many relations pulling a null set of ids.
28
+ (Jonathan Hyman)
29
+
30
+ * \#2291 Fixed touch operations only to update the timestamp and the optional
31
+ field, no matter what the other changes on the document are.
32
+
33
+ * \#1091 Allow presence validation to pass if the value is `false`.
34
+
35
+ ### Resolved Issues
36
+
37
+ ## 3.0.4
7
38
 
8
39
  ### Resolved Issues
9
40
 
@@ -440,14 +440,6 @@ en:
440
440
  since the child could potentially reference a nonexistant parent."
441
441
  resolution: "Make sure to only use create or create! when the parent
442
442
  document %{base} is persisted."
443
- unsupported_version:
444
- message: "MongoDB %{version} not supported, please upgrade to
445
- %{mongo_version}."
446
- summary: "Mongoid is relying on features that were introduced in
447
- MongoDB %{mongo_version} and would cause unexpected behaviour or errors
448
- on version %{version}."
449
- resolution: "Upgrade your MongoDB instances or keep Mongoid on your
450
- previous version."
451
443
  unsupported_javascript:
452
444
  message: "Executing Javascript $where selector on an embedded criteria
453
445
  is not supported."
@@ -335,5 +335,28 @@ module Mongoid
335
335
  mods.add_to_set(doc.atomic_array_add_to_sets)
336
336
  mods.pull_all(doc.atomic_array_pulls)
337
337
  end
338
+
339
+ # Get the atomic updates for a touch operation. Should only include the
340
+ # updated_at field and the optional extra field.
341
+ #
342
+ # @api private
343
+ #
344
+ # @example Get the touch atomic updates.
345
+ # document.touch_atomic_updates
346
+ #
347
+ # @param [ Symbol ] field The optional field.
348
+ #
349
+ # @return [ Hash ] The atomic updates.
350
+ #
351
+ # @since 3.0.6
352
+ def touch_atomic_updates(field = nil)
353
+ updates = atomic_updates
354
+ return {} unless atomic_updates.has_key?("$set")
355
+ touches = {}
356
+ updates["$set"].each_pair do |key, value|
357
+ touches.merge!({ key => value }) if key =~ /updated_at|#{field}/
358
+ end
359
+ { "$set" => touches }
360
+ end
338
361
  end
339
362
  end
@@ -21,6 +21,7 @@ module Mongoid
21
21
  option :protect_sensitive_fields, default: true
22
22
  option :raise_not_found_error, default: true
23
23
  option :scope_overwrite_exception, default: false
24
+ # @todo: Remove at 4.0
24
25
  option :skip_version_check, default: false
25
26
  option :use_activesupport_time_zone, default: true
26
27
  option :use_utc, default: false
@@ -64,7 +64,8 @@ module Mongoid
64
64
  def changes
65
65
  _changes = {}
66
66
  changed.each do |attr|
67
- _changes[attr] = attribute_change(attr)
67
+ change = attribute_change(attr)
68
+ _changes[attr] = change if change
68
69
  end
69
70
  _changes
70
71
  end
@@ -36,7 +36,6 @@ require "mongoid/errors/too_many_nested_attribute_records"
36
36
  require "mongoid/errors/unknown_attribute"
37
37
  require "mongoid/errors/unsaved_document"
38
38
  require "mongoid/errors/unsupported_javascript"
39
- require "mongoid/errors/unsupported_version"
40
39
  require "mongoid/errors/validations"
41
40
  require "mongoid/errors/versioning_not_on_root"
42
41
  require "mongoid/errors/delete_restriction"
@@ -29,6 +29,7 @@ module Mongoid
29
29
  #
30
30
  # @since 3.0.0
31
31
  def demongoize(object)
32
+ return nil if object.blank?
32
33
  ::Time.demongoize(object).in_time_zone
33
34
  end
34
35
 
@@ -120,7 +120,7 @@ module Mongoid
120
120
  current = Time.now
121
121
  write_attribute(:updated_at, current) if fields["updated_at"]
122
122
  write_attribute(field, current) if field
123
- _root.collection.find(atomic_selector).update(atomic_updates)
123
+ _root.collection.find(atomic_selector).update(touch_atomic_updates(field))
124
124
  without_autobuild do
125
125
  touchables.each { |name| send(name).try(:touch) }
126
126
  end
@@ -43,6 +43,7 @@ module Mongoid
43
43
  end
44
44
  unbind_one
45
45
  return nil unless replacement
46
+ replacement = Factory.build(klass, replacement) if replacement.is_a?(::Hash)
46
47
  self.target = replacement
47
48
  bind_one
48
49
  characterize_one(target)
@@ -1120,7 +1120,9 @@ module Mongoid
1120
1120
  if other
1121
1121
  matches = []
1122
1122
  other.class.relations.values.each do |meta|
1123
- matches.push(meta.name) if meta.as == name
1123
+ if meta.as == name && meta.class_name == inverse_class_name
1124
+ matches.push(meta.name)
1125
+ end
1124
1126
  end
1125
1127
  matches
1126
1128
  end
@@ -185,13 +185,18 @@ module Mongoid
185
185
  # @example Find by multiple ids.
186
186
  # person.posts.find([ Moped::BSON::ObjectId.new, Moped::BSON::ObjectId.new ])
187
187
  #
188
+ # @note This will keep matching documents in memory for iteration
189
+ # later.
190
+ #
188
191
  # @param [ Moped::BSON::ObjectId, Array<Moped::BSON::ObjectId> ] arg The ids.
189
192
  #
190
193
  # @return [ Document, Criteria ] The matching document(s).
191
194
  #
192
195
  # @since 2.0.0.beta.1
193
196
  def find(*args)
194
- criteria.find(*args)
197
+ matching = criteria.find(*args)
198
+ Array(matching).each { |doc| target.push(doc) }
199
+ matching
195
200
  end
196
201
 
197
202
  # Instantiate a new references_many relation. Will set the foreign key
@@ -146,6 +146,9 @@ module Mongoid
146
146
  end
147
147
  target.clear do |doc|
148
148
  unbind_one(doc)
149
+ unless metadata.forced_nil_inverse?
150
+ doc.changed_attributes.delete(inverse_foreign_key)
151
+ end
149
152
  end
150
153
  end
151
154
  alias :nullify_all :nullify
@@ -59,7 +59,10 @@ module Mongoid
59
59
  #
60
60
  # @since 2.2.1
61
61
  def remove_inverse_keys(meta)
62
- meta.criteria(send(meta.foreign_key), self.class).pull(meta.inverse_foreign_key, id)
62
+ foreign_keys = send(meta.foreign_key)
63
+ unless foreign_keys.nil? || foreign_keys.empty?
64
+ meta.criteria(foreign_keys, self.class).pull(meta.inverse_foreign_key, id)
65
+ end
63
66
  end
64
67
 
65
68
  # Update the inverse keys for the relation.
@@ -24,6 +24,7 @@ module Mongoid
24
24
  changed_attributes.clear
25
25
  apply_defaults
26
26
  reload_relations
27
+ IdentityMap.set(self)
27
28
  run_callbacks(:initialize)
28
29
  self
29
30
  end
@@ -40,7 +40,7 @@ module Mongoid
40
40
  # @since 1.0.0
41
41
  def default_scope(value)
42
42
  check_scope_validity(value)
43
- self.default_scoping = value.to_proc
43
+ self.default_scoping = process_default_scope(value)
44
44
  end
45
45
 
46
46
  # Is the class able to have the default scope applied?
@@ -284,6 +284,25 @@ module Mongoid
284
284
  SCOPE
285
285
  end
286
286
 
287
+ # Process the default scope value. If one already exists, we merge the
288
+ # new one into the old one.
289
+ #
290
+ # @api private
291
+ #
292
+ # @example Process the default scope.
293
+ # Model.process_default_scope(value)
294
+ #
295
+ # @param [ Criteria, Proc ] value The default scope value.
296
+ #
297
+ # @since 3.0.5
298
+ def process_default_scope(value)
299
+ if existing = default_scoping
300
+ ->{ existing.call.merge(value.to_proc.call) }
301
+ else
302
+ value.to_proc
303
+ end
304
+ end
305
+
287
306
  # Strip the default scope from the provided value, if it is a criteria.
288
307
  # This is used by named scopes - they should not have the default scoping
289
308
  # applied to them.
@@ -33,14 +33,14 @@ module Mongoid
33
33
  attribute,
34
34
  :blank_in_locale,
35
35
  options.merge(location: _locale)
36
- ) if _value.blank?
36
+ ) if not_present?(_value)
37
37
  end
38
38
  elsif document.relations.has_key?(attribute.to_s)
39
39
  if relation_or_fk_missing?(document, attribute, value)
40
40
  document.errors.add(attribute, :blank, options)
41
41
  end
42
42
  else
43
- document.errors.add(attribute, :blank, options) if value.blank?
43
+ document.errors.add(attribute, :blank, options) if not_present?(value)
44
44
  end
45
45
  end
46
46
 
@@ -65,6 +65,22 @@ module Mongoid
65
65
  metadata = doc.relations[attr.to_s]
66
66
  metadata.stores_foreign_key? && doc.send(metadata.foreign_key).blank?
67
67
  end
68
+
69
+ # For guarding against false values.
70
+ #
71
+ # @api private
72
+ #
73
+ # @example Is the value not present?
74
+ # validator.not_present?(value)
75
+ #
76
+ # @param [ Object ] value The value.
77
+ #
78
+ # @return [ true, false ] If the value is not present.
79
+ #
80
+ # @since 3.0.5
81
+ def not_present?(value)
82
+ value.blank? && value != false
83
+ end
68
84
  end
69
85
  end
70
86
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid
3
- VERSION = "3.0.4"
3
+ VERSION = "3.0.5"
4
4
  end
@@ -50,6 +50,10 @@ module Mongoid
50
50
  "#{name}.errors"
51
51
  end
52
52
 
53
+ def delete
54
+ "#{name}.delete"
55
+ end
56
+
53
57
  def destroy
54
58
  "#{name}.destroy"
55
59
  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.4
4
+ version: 3.0.5
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: 2012-08-11 00:00:00.000000000 Z
12
+ date: 2012-08-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -156,7 +156,6 @@ files:
156
156
  - lib/mongoid/errors/unknown_attribute.rb
157
157
  - lib/mongoid/errors/unsaved_document.rb
158
158
  - lib/mongoid/errors/unsupported_javascript.rb
159
- - lib/mongoid/errors/unsupported_version.rb
160
159
  - lib/mongoid/errors/validations.rb
161
160
  - lib/mongoid/errors/versioning_not_on_root.rb
162
161
  - lib/mongoid/errors.rb
@@ -354,7 +353,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
354
353
  version: '0'
355
354
  segments:
356
355
  - 0
357
- hash: -1276565475413611308
356
+ hash: -1770994134498643287
358
357
  required_rubygems_version: !ruby/object:Gem::Requirement
359
358
  none: false
360
359
  requirements:
@@ -1,20 +0,0 @@
1
- # encoding: utf-8
2
- module Mongoid
3
- module Errors
4
-
5
- # Raised when the database version is not supported by Mongoid.
6
- #
7
- # @example Create the error.
8
- # UnsupportedVersion.new(Mongo::ServerVersion.new("1.3.1"))
9
- class UnsupportedVersion < MongoidError
10
- def initialize(version)
11
- super(
12
- compose_message(
13
- "unsupported_version",
14
- { version: version, mongo_version: Mongoid::MONGODB_VERSION }
15
- )
16
- )
17
- end
18
- end
19
- end
20
- end