mongoid 3.0.13 → 3.0.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,23 @@
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.14
7
+
8
+ ### Resolved Issues
9
+
10
+ * \#2575 Prevent end of month times from rounding up since floats are not
11
+ precise enough to handle usec. (Steve Valaitis)
12
+
13
+ * \#2573 Don't use i18n for inspection messages.
14
+
15
+ * \#2571 Remove blank error message from locales. (Jordan Elver)
16
+
17
+ * \#2568 Fix uniqueness validation for lacalized fields when a scope is also
18
+ provided.
19
+
20
+ * \#2552 Ensure `InvalidPath` errors are raised when embedded documents try to
21
+ get paths from a root selector.
22
+
6
23
  ## 3.0.13
7
24
 
8
25
  ### Resolved Issues
@@ -1,29 +1,7 @@
1
1
  en:
2
2
  mongoid:
3
- inspection:
4
- criteria: "#<Mongoid::Criteria\n
5
- \_\_selector: %{selector},\n
6
- \_\_options: %{options},\n
7
- \_\_class: %{klass},\n
8
- \_\_embedded: %{embedded}>\n"
9
- metadata: "#<Mongoid::Relations::Metadata\n
10
- \_\_autobuild: %{autobuild},\n
11
- \_\_class_name: %{class_name},\n
12
- \_\_cyclic: %{cyclic},\n
13
- \_\_dependent: %{dependent},\n
14
- \_\_inverse_of: %{inverse_of},\n
15
- \_\_key: %{key},\n
16
- \_\_macro: %{macro},\n
17
- \_\_name: %{name},\n
18
- \_\_order: %{order},\n
19
- \_\_polymorphic: %{polymorphic},\n
20
- \_\_relation: %{relation},\n
21
- \_\_setter: %{setter},\n
22
- \_\_versioned: %{versioned}>\n"
23
3
  errors:
24
4
  messages:
25
- blank:
26
- "can't be blank"
27
5
  blank_in_locale:
28
6
  "can't be blank in %{location}"
29
7
  ambiguous_relationship:
@@ -171,6 +149,17 @@ en:
171
149
  slip by."
172
150
  resolution: "Valid options are: %{valid}, make sure these are the ones
173
151
  you are using."
152
+ invalid_path:
153
+ message: "Having a root path assigned for %{klass} is invalid."
154
+ summary: "Mongoid has two different path objects for determining
155
+ the location of a document in the database, Root and Embedded.
156
+ This error is raised when an embedded document somehow gets a
157
+ root path assigned."
158
+ resolution: "Most likely your embedded model, %{klass} is also
159
+ referenced via a has_many from a root document in another
160
+ collection. Double check the relation definitions and fix any
161
+ instances where embedded documents are improperly referenced
162
+ from other collections."
174
163
  invalid_scope:
175
164
  message: "Defining a scope of value %{value} on %{klass} is not
176
165
  allowed."
@@ -21,6 +21,19 @@ module Mongoid
21
21
  @document, @path, @position = document, "", ""
22
22
  end
23
23
 
24
+ # Asking for the insert modifier on a document with a root path
25
+ # indicates a mixed relation most likely happened.
26
+ #
27
+ # @example Attempt to get the insert modifier.
28
+ # root.insert_modifier
29
+ #
30
+ # @raise [ Errors::InvalidPath ] The error for the attempt.
31
+ #
32
+ # @since 3.0.14
33
+ def insert_modifier
34
+ raise Errors::InvalidPath.new(document.class)
35
+ end
36
+
24
37
  # Get the selector to use for the root document when performing atomic
25
38
  # updates. When sharding this will include the shard key.
26
39
  #
@@ -201,6 +201,26 @@ module Mongoid
201
201
  results["timeMillis"]
202
202
  end
203
203
 
204
+ # Get a pretty string representation of the map/reduce, including the
205
+ # criteria, map, reduce, finalize, and out option.
206
+ #
207
+ # @example Inspect the map_reduce.
208
+ # map_reduce.inspect
209
+ #
210
+ # @return [ String ] The inspection string.
211
+ #
212
+ # @since 3.1.0
213
+ def inspect
214
+ %Q{#<Mongoid::Contextual::MapReduce
215
+ selector: #{criteria.selector.inspect}
216
+ class: #{criteria.klass}
217
+ map: #{command[:map]}
218
+ reduce: #{command[:reduce]}
219
+ finalize: #{command[:finalize]}
220
+ out: #{command[:out].inspect}>
221
+ }
222
+ end
223
+
204
224
  private
205
225
 
206
226
  # Apply criteria specific options - query, sort, limit.
@@ -13,15 +13,12 @@ module Mongoid
13
13
  #
14
14
  # @since 1.0.0
15
15
  def inspect
16
- ::I18n.translate(
17
- "mongoid.inspection.criteria",
18
- {
19
- selector: selector.inspect,
20
- options: options.inspect,
21
- klass: klass,
22
- embedded: embedded?
23
- }
24
- )
16
+ %Q{#<Mongoid::Criteria
17
+ selector: #{selector.inspect}
18
+ options: #{options.inspect}
19
+ class: #{klass}
20
+ embedded: #{embedded?}>
21
+ }
25
22
  end
26
23
  end
27
24
  end
@@ -193,7 +193,11 @@ module Mongoid
193
193
  without_autobuild do
194
194
  relation, stored = send(name), meta.store_as
195
195
  if attributes.has_key?(stored) || !relation.blank?
196
- attributes[stored] = relation.as_document
196
+ if relation
197
+ attributes[stored] = relation.as_document
198
+ else
199
+ attributes.delete(stored)
200
+ end
197
201
  end
198
202
  end
199
203
  end
@@ -13,6 +13,7 @@ require "mongoid/errors/invalid_find"
13
13
  require "mongoid/errors/invalid_includes"
14
14
  require "mongoid/errors/invalid_index"
15
15
  require "mongoid/errors/invalid_options"
16
+ require "mongoid/errors/invalid_path"
16
17
  require "mongoid/errors/invalid_scope"
17
18
  require "mongoid/errors/invalid_set_polymorphic_relation"
18
19
  require "mongoid/errors/invalid_storage_options"
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ module Mongoid
3
+ module Errors
4
+
5
+ # Used when attempting to get embedded paths with incorrect root path set.
6
+ class InvalidPath < MongoidError
7
+
8
+ # Create the new error.
9
+ #
10
+ # @example Create the error.
11
+ # InvalidPath.new(Address)
12
+ #
13
+ # @param [ Class ] klass The document class.
14
+ #
15
+ # @since 3.0.14
16
+ def initialize(klass)
17
+ super(compose_message("invalid_path", { klass: klass }))
18
+ end
19
+ end
20
+ end
21
+ end
@@ -64,7 +64,9 @@ module Mongoid
64
64
  def mongoize(object)
65
65
  return nil if object.blank?
66
66
  begin
67
- ::Time.at(object.__mongoize_time__.to_f).utc
67
+ t = object.__mongoize_time__
68
+ usec = t.strftime("%9N").to_i / 10**3.0
69
+ ::Time.at(t.to_i, usec).utc
68
70
  rescue ArgumentError
69
71
  raise Errors::InvalidTime.new(object)
70
72
  end
@@ -58,6 +58,21 @@ module Mongoid
58
58
  ivar(name)
59
59
  end
60
60
 
61
+ # Resets the criteria inside the relation proxy. Used by many to many
62
+ # relations to keep the underlying ids array in sync.
63
+ #
64
+ # @example Reset the relation criteria.
65
+ # person.reset_relation_criteria(:preferences)
66
+ #
67
+ # @param [ Symbol ] name The name of the relation.
68
+ #
69
+ # @since 3.0.14
70
+ def reset_relation_criteria(name)
71
+ if instance_variable_defined?("@#{name}")
72
+ send(name).reset_relation_criteria
73
+ end
74
+ end
75
+
61
76
  # Set the supplied relation to an instance variable on the class with the
62
77
  # provided name. Used as a helper just for code cleanliness.
63
78
  #
@@ -19,7 +19,10 @@ module Mongoid
19
19
  def bind_one(doc)
20
20
  binding do
21
21
  inverse_keys = doc.you_must(metadata.inverse_foreign_key)
22
- inverse_keys.push(base.id) if inverse_keys
22
+ if inverse_keys
23
+ inverse_keys.push(base.id)
24
+ doc.reset_relation_criteria(metadata.inverse)
25
+ end
23
26
  base.synced[metadata.foreign_key] = true
24
27
  doc.synced[metadata.inverse_foreign_key] = true
25
28
  end
@@ -35,7 +38,10 @@ module Mongoid
35
38
  binding do
36
39
  base.send(metadata.foreign_key).delete_one(doc.id)
37
40
  inverse_keys = doc.you_must(metadata.inverse_foreign_key)
38
- inverse_keys.delete_one(base.id) if inverse_keys
41
+ if inverse_keys
42
+ inverse_keys.delete_one(base.id)
43
+ doc.reset_relation_criteria(metadata.inverse)
44
+ end
39
45
  base.synced[metadata.foreign_key] = true
40
46
  doc.synced[metadata.inverse_foreign_key] = true
41
47
  end
@@ -357,25 +357,21 @@ module Mongoid
357
357
  #
358
358
  # @since 2.0.0.rc.1
359
359
  def inspect
360
- ::I18n.translate(
361
- "mongoid.inspection.metadata",
362
- {
363
- autobuild: autobuilding?,
364
- class_name: class_name,
365
- cyclic: cyclic.inspect,
366
- dependent: dependent.inspect,
367
- inverse_of: inverse_of.inspect,
368
- key: key,
369
- locale: :en,
370
- macro: macro,
371
- name: name,
372
- order: order.inspect,
373
- polymorphic: polymorphic?,
374
- relation: relation,
375
- setter: setter,
376
- versioned: versioned?
377
- }
378
- )
360
+ %Q{#<Mongoid::Relations::Metadata
361
+ autobuild: #{autobuilding?}
362
+ class_name: #{class_name}
363
+ cyclic: #{cyclic.inspect}
364
+ dependent: #{dependent.inspect}
365
+ inverse_of: #{inverse_of.inspect}
366
+ key: #{key}
367
+ macro: #{macro}
368
+ name: #{name}
369
+ order: #{order.inspect}
370
+ polymorphic: #{polymorphic?}
371
+ relation: #{relation}
372
+ setter: #{setter}
373
+ versioned: #{versioned?}>
374
+ }
379
375
  end
380
376
 
381
377
  # Get the name of the inverse relations if they exists. If this is a
@@ -39,6 +39,17 @@ module Mongoid
39
39
  extend_proxy(metadata.extension) if metadata.extension?
40
40
  end
41
41
 
42
+ # Resets the criteria inside the relation proxy. Used by many to many
43
+ # relations to keep the underlying ids array in sync.
44
+ #
45
+ # @example Reset the relation criteria.
46
+ # person.preferences.reset_relation_criteria
47
+ #
48
+ # @since 3.0.14
49
+ def reset_relation_criteria
50
+ target.reset_unloaded(criteria)
51
+ end
52
+
42
53
  # The default substitutable object for a relation proxy is the clone of
43
54
  # the target.
44
55
  #
@@ -121,6 +121,7 @@ module Mongoid
121
121
  doc = super
122
122
  if doc && persistable?
123
123
  base.pull(foreign_key, doc.id)
124
+ target._unloaded = criteria
124
125
  unsynced(base, foreign_key)
125
126
  end
126
127
  doc
@@ -169,7 +170,11 @@ module Mongoid
169
170
  # @since 2.0.0.rc.1
170
171
  def substitute(replacement)
171
172
  purge
172
- push(replacement.compact.uniq) unless replacement.blank?
173
+ unless replacement.blank?
174
+ push(replacement.compact.uniq)
175
+ else
176
+ reset_relation_criteria
177
+ end
173
178
  self
174
179
  end
175
180
 
@@ -147,10 +147,10 @@ module Mongoid
147
147
  # puts doc
148
148
  # end
149
149
  #
150
- # @example return an enumerator containing all the docs
150
+ # @example return an enumerator containing all the docs
151
151
  #
152
152
  # a = enumerable.each
153
- #
153
+ #
154
154
  # @return [ true ] That the enumerable is now _loaded.
155
155
  #
156
156
  # @since 2.1.0
@@ -322,6 +322,19 @@ module Mongoid
322
322
  @executed = false
323
323
  end
324
324
 
325
+ # Resets the underlying unloaded criteria object with a new one. Used
326
+ # my HABTM relations to keep the underlying array in sync.
327
+ #
328
+ # @example Reset the unloaded documents.
329
+ # enumerable.reset_unloaded(criteria)
330
+ #
331
+ # @param [ Criteria ] criteria The criteria to replace with.
332
+ #
333
+ # @since 3.0.14
334
+ def reset_unloaded(criteria)
335
+ @_unloaded = criteria if _unloaded.is_a?(Criteria)
336
+ end
337
+
325
338
  # Does this enumerable respond to the provided method?
326
339
  #
327
340
  # @example Does the enumerable respond to the method?
@@ -105,13 +105,13 @@ module Mongoid
105
105
  # @since 2.4.10
106
106
  def create_criteria(base, document, attribute, value)
107
107
  field = document.fields[attribute.to_s]
108
- criteria = base.unscoped
108
+ criteria = scope(base.unscoped, document, attribute)
109
109
  if field.try(:localized?)
110
110
  criteria.selector.update(criterion(document, attribute, value))
111
111
  else
112
112
  criteria = criteria.where(criterion(document, attribute, value))
113
113
  end
114
- scope(criteria, document, attribute)
114
+ criteria
115
115
  end
116
116
 
117
117
  # Get the default criteria for checking uniqueness.
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid
3
- VERSION = "3.0.13"
3
+ VERSION = "3.0.14"
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.13
4
+ version: 3.0.14
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-11-12 00:00:00.000000000 Z
12
+ date: 2012-11-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -132,6 +132,7 @@ files:
132
132
  - lib/mongoid/errors/invalid_includes.rb
133
133
  - lib/mongoid/errors/invalid_index.rb
134
134
  - lib/mongoid/errors/invalid_options.rb
135
+ - lib/mongoid/errors/invalid_path.rb
135
136
  - lib/mongoid/errors/invalid_scope.rb
136
137
  - lib/mongoid/errors/invalid_set_polymorphic_relation.rb
137
138
  - lib/mongoid/errors/invalid_storage_options.rb
@@ -352,7 +353,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
352
353
  version: '0'
353
354
  segments:
354
355
  - 0
355
- hash: 4207639439439343607
356
+ hash: 4134482588596477262
356
357
  required_rubygems_version: !ruby/object:Gem::Requirement
357
358
  none: false
358
359
  requirements: