mongoid 5.2.1 → 5.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +1 -0
  3. data.tar.gz.sig +0 -0
  4. data/lib/mongoid/atomic.rb +1 -1
  5. data/lib/mongoid/atomic/modifiers.rb +12 -8
  6. data/lib/mongoid/attributes.rb +1 -2
  7. data/lib/mongoid/attributes/dynamic.rb +3 -2
  8. data/lib/mongoid/attributes/nested.rb +1 -1
  9. data/lib/mongoid/attributes/processing.rb +0 -4
  10. data/lib/mongoid/changeable.rb +1 -1
  11. data/lib/mongoid/clients/options.rb +3 -2
  12. data/lib/mongoid/contextual/atomic.rb +9 -6
  13. data/lib/mongoid/contextual/geo_near.rb +3 -2
  14. data/lib/mongoid/contextual/map_reduce.rb +4 -0
  15. data/lib/mongoid/contextual/memory.rb +1 -1
  16. data/lib/mongoid/contextual/mongo.rb +3 -3
  17. data/lib/mongoid/contextual/none.rb +2 -2
  18. data/lib/mongoid/copyable.rb +0 -4
  19. data/lib/mongoid/criteria.rb +1 -1
  20. data/lib/mongoid/criteria/findable.rb +3 -2
  21. data/lib/mongoid/criteria/includable.rb +2 -2
  22. data/lib/mongoid/errors/delete_restriction.rb +2 -2
  23. data/lib/mongoid/errors/inverse_not_found.rb +1 -1
  24. data/lib/mongoid/extensions/hash.rb +3 -2
  25. data/lib/mongoid/extensions/object.rb +2 -2
  26. data/lib/mongoid/extensions/time.rb +1 -1
  27. data/lib/mongoid/factory.rb +0 -1
  28. data/lib/mongoid/fields/foreign_key.rb +2 -2
  29. data/lib/mongoid/findable.rb +1 -1
  30. data/lib/mongoid/indexable.rb +3 -2
  31. data/lib/mongoid/indexable/specification.rb +1 -1
  32. data/lib/mongoid/interceptable.rb +2 -2
  33. data/lib/mongoid/loggable.rb +1 -1
  34. data/lib/mongoid/matchable.rb +2 -2
  35. data/lib/mongoid/persistable.rb +0 -3
  36. data/lib/mongoid/persistable/creatable.rb +0 -2
  37. data/lib/mongoid/query_cache.rb +5 -1
  38. data/lib/mongoid/relations/accessors.rb +1 -2
  39. data/lib/mongoid/relations/bindings/embedded/in.rb +0 -4
  40. data/lib/mongoid/relations/bindings/embedded/many.rb +1 -8
  41. data/lib/mongoid/relations/bindings/embedded/one.rb +0 -10
  42. data/lib/mongoid/relations/bindings/referenced/many.rb +0 -4
  43. data/lib/mongoid/relations/builders/embedded/one.rb +1 -1
  44. data/lib/mongoid/relations/builders/nested_attributes/many.rb +1 -1
  45. data/lib/mongoid/relations/conversions.rb +1 -1
  46. data/lib/mongoid/relations/counter_cache.rb +10 -10
  47. data/lib/mongoid/relations/eager/base.rb +4 -4
  48. data/lib/mongoid/relations/embedded/batchable.rb +33 -9
  49. data/lib/mongoid/relations/embedded/in.rb +1 -3
  50. data/lib/mongoid/relations/embedded/many.rb +4 -5
  51. data/lib/mongoid/relations/embedded/one.rb +1 -3
  52. data/lib/mongoid/relations/macros.rb +2 -1
  53. data/lib/mongoid/relations/metadata.rb +3 -3
  54. data/lib/mongoid/relations/nested_builder.rb +1 -1
  55. data/lib/mongoid/relations/proxy.rb +3 -2
  56. data/lib/mongoid/relations/referenced/in.rb +1 -4
  57. data/lib/mongoid/relations/referenced/many.rb +3 -5
  58. data/lib/mongoid/relations/referenced/many_to_many.rb +1 -1
  59. data/lib/mongoid/relations/referenced/one.rb +0 -2
  60. data/lib/mongoid/relations/reflections.rb +2 -2
  61. data/lib/mongoid/scopable.rb +2 -2
  62. data/lib/mongoid/serializable.rb +3 -2
  63. data/lib/mongoid/tasks/database.rb +2 -1
  64. data/lib/mongoid/threaded.rb +4 -4
  65. data/lib/mongoid/validatable.rb +1 -1
  66. data/lib/mongoid/validatable/macros.rb +4 -2
  67. data/lib/mongoid/version.rb +1 -1
  68. data/spec/mongoid/atomic/modifiers_spec.rb +17 -17
  69. data/spec/mongoid/atomic_spec.rb +17 -17
  70. data/spec/mongoid/clients/options_spec.rb +9 -0
  71. data/spec/mongoid/contextual/atomic_spec.rb +3 -3
  72. data/spec/mongoid/persistable/savable_spec.rb +2 -2
  73. data/spec/mongoid/persistable/updatable_spec.rb +2 -2
  74. data/spec/mongoid/positional_spec.rb +10 -10
  75. data/spec/spec_helper.rb +1 -1
  76. metadata +440 -418
  77. metadata.gz.sig +0 -0
@@ -71,7 +71,7 @@ module Mongoid
71
71
  # @example Do any documents exist for the conditions?
72
72
  # Person.exists?
73
73
  #
74
- # @param [ Array ] args The conditions.
74
+ # @return [ true, false ] If any documents exist for the conditions.
75
75
  def exists?
76
76
  with_default_scope.exists?
77
77
  end
@@ -90,7 +90,7 @@ module Mongoid
90
90
  # index({ name: 1 }, { background: true })
91
91
  # end
92
92
  #
93
- # @param [ Symbol ] name The name of the field.
93
+ # @param [ Symbol ] spec The index spec.
94
94
  # @param [ Hash ] options The index options.
95
95
  #
96
96
  # @return [ Hash ] The index options.
@@ -108,7 +108,8 @@ module Mongoid
108
108
  # @example Get the index specification.
109
109
  # Model.index_specification(name: 1)
110
110
  #
111
- # @param [ Hash ] key The index key/direction pair.
111
+ # @param [ Hash ] index_hash The index key/direction pair.
112
+ # @param [ String ] index_name The index name.
112
113
  #
113
114
  # @return [ Specification ] The found specification.
114
115
  #
@@ -99,7 +99,7 @@ module Mongoid
99
99
  # @example Normalize the index options.
100
100
  # specification.normalize_options(drop_dups: true)
101
101
  #
102
- # @param [ Hash ] options The index options.
102
+ # @param [ Hash ] opts The index options.
103
103
  #
104
104
  # @return [ Hash ] The normalized options.
105
105
  #
@@ -46,7 +46,7 @@ module Mongoid
46
46
  # @example Is the callback executable?
47
47
  # document.callback_executable?(:save)
48
48
  #
49
- # @param [ Symbol ] kin The type of callback.
49
+ # @param [ Symbol ] kind The type of callback.
50
50
  #
51
51
  # @return [ true, false ] If the callback can be executed.
52
52
  #
@@ -118,7 +118,7 @@ module Mongoid
118
118
  # end
119
119
  #
120
120
  # @param [ Symbol ] kind The type of callback to execute.
121
- # @param [ Array ] *args Any options.
121
+ # @param [ Array ] args Any options.
122
122
  #
123
123
  # @return [ Document ] The document
124
124
  #
@@ -25,7 +25,7 @@ module Mongoid
25
25
  # @example Set the logger.
26
26
  # Loggable.logger = Logger.new($stdout)
27
27
  #
28
- # @param [ Logger ] The logger to set.
28
+ # @param [ Logger ] logger The logger to set.
29
29
  #
30
30
  # @return [ Logger ] The new logger.
31
31
  #
@@ -82,7 +82,7 @@ module Mongoid
82
82
  #
83
83
  # @param [ Document ] document The document to check.
84
84
  # @param [ Symbol, String ] key The field name.
85
- # @param [ Object, Hash ] The value or selector.
85
+ # @param [ Object, Hash ] value The value or selector.
86
86
  #
87
87
  # @return [ Matcher ] The matcher.
88
88
  #
@@ -103,7 +103,7 @@ module Mongoid
103
103
  #
104
104
  # @param [ Document ] document The document to check.
105
105
  # @param [ Symbol, String ] key The field name.
106
- # @param [ Object, Hash ] The value or selector.
106
+ # @param [ Object, Hash ] value The value or selector.
107
107
  #
108
108
  # @return [ Matcher ] The matcher.
109
109
  #
@@ -69,8 +69,6 @@ module Mongoid
69
69
  # @example Raise the validation error.
70
70
  # Person.fail_due_to_validation!(person)
71
71
  #
72
- # @param [ Document ] document The document to fail.
73
- #
74
72
  # @raise [ Errors::Validations ] The validation error.
75
73
  #
76
74
  # @since 4.0.0
@@ -83,7 +81,6 @@ module Mongoid
83
81
  # @example Raise the callback error.
84
82
  # Person.fail_due_to_callback!(person, :create!)
85
83
  #
86
- # @param [ Document ] document The document to fail.
87
84
  # @param [ Symbol ] method The method being called.
88
85
  #
89
86
  # @raise [ Errors::Callback ] The callback error.
@@ -165,8 +165,6 @@ module Mongoid
165
165
  #
166
166
  # @param [ Hash, Array ] attributes The attributes to create with, or an
167
167
  # Array of multiple attributes for multiple documents.
168
- # @param [ Hash ] options A mass-assignment protection options. Supports
169
- # :as and :without_protection
170
168
  #
171
169
  # @return [ Document, Array<Document> ] The newly created document(s).
172
170
  #
@@ -221,7 +221,7 @@ module Mongoid
221
221
  super
222
222
  else
223
223
  unless cursor = cached_cursor
224
- server = read.select_server(cluster)
224
+ server = read_or_server_selector.select_server(cluster)
225
225
  cursor = CachedCursor.new(view, send_initial_query(server), server)
226
226
  QueryCache.cache_table[cache_key] = cursor
227
227
  end
@@ -234,6 +234,10 @@ module Mongoid
234
234
 
235
235
  private
236
236
 
237
+ def read_or_server_selector
238
+ respond_to?(:server_selector, true) ? server_selector : read
239
+ end
240
+
237
241
  def cached_cursor
238
242
  if limit
239
243
  key = [ collection.namespace, selector, nil, skip, sort, projection, collation ]
@@ -17,7 +17,6 @@ module Mongoid
17
17
  # @param [ String, Symbol ] name The name of the relation.
18
18
  # @param [ Hash, BSON::ObjectId ] object The id or attributes to use.
19
19
  # @param [ Metadata ] metadata The relation's metadata.
20
- # @param [ true, false ] building If we are in a build operation.
21
20
  #
22
21
  # @return [ Proxy ] The relation.
23
22
  #
@@ -32,7 +31,7 @@ module Mongoid
32
31
  # @example Create the relation.
33
32
  # person.create_relation(document, metadata)
34
33
  #
35
- # @param [ Document, Array<Document ] object The relation target.
34
+ # @param [ Document, Array<Document> ] object The relation target.
36
35
  # @param [ Metadata ] metadata The relation metadata.
37
36
  #
38
37
  # @return [ Proxy ] The relation.
@@ -17,8 +17,6 @@ module Mongoid
17
17
  # name.person.bind(:continue => true)
18
18
  # name.person = Person.new
19
19
  #
20
- # @param [ Hash ] options The binding options.
21
- #
22
20
  # @option options [ true, false ] :continue Continue binding the inverse.
23
21
  # @option options [ true, false ] :binding Are we in build mode?
24
22
  #
@@ -42,8 +40,6 @@ module Mongoid
42
40
  # name.person.unbind(:continue => true)
43
41
  # name.person = nil
44
42
  #
45
- # @param [ Hash ] options The options to pass through.
46
- #
47
43
  # @option options [ true, false ] :continue Do we continue unbinding?
48
44
  #
49
45
  # @since 2.0.0.rc.1
@@ -14,10 +14,6 @@ module Mongoid
14
14
  # person.addresses.bind_one(address)
15
15
  #
16
16
  # @param [ Document ] doc The single document to bind.
17
- # @param [ Hash ] options The binding options.
18
- #
19
- # @option options [ true, false ] :continue Continue binding the inverse.
20
- # @option options [ true, false ] :binding Are we in build mode?
21
17
  #
22
18
  # @since 2.0.0.rc.1
23
19
  def bind_one(doc)
@@ -32,10 +28,7 @@ module Mongoid
32
28
  # @example Unbind the document.
33
29
  # person.addresses.unbind_one(document)
34
30
  #
35
- # @param [ Hash ] options The binding options.
36
- #
37
- # @option options [ true, false ] :continue Continue binding the inverse.
38
- # @option options [ true, false ] :binding Are we in build mode?
31
+ # @param [ Document ] doc The single document to unbind.
39
32
  #
40
33
  # @since 2.0.0.rc.1
41
34
  def unbind_one(doc)
@@ -17,11 +17,6 @@ module Mongoid
17
17
  # person.name.bind(:continue => true)
18
18
  # person.name = Name.new
19
19
  #
20
- # @param [ Hash ] options The options to pass through.
21
- #
22
- # @option options [ true, false ] :continue Do we continue binding?
23
- # @option options [ true, false ] :binding Are we in build mode?
24
- #
25
20
  # @since 2.0.0.rc.1
26
21
  def bind_one
27
22
  target.parentize(base)
@@ -37,11 +32,6 @@ module Mongoid
37
32
  # person.name.unbind(:continue => true)
38
33
  # person.name = nil
39
34
  #
40
- # @param [ Hash ] options The options to pass through.
41
- #
42
- # @option options [ true, false ] :continue Do we continue unbinding?
43
- # @option options [ true, false ] :binding Are we in build mode?
44
- #
45
35
  # @since 2.0.0.rc.1
46
36
  def unbind_one
47
37
  binding do
@@ -13,8 +13,6 @@ module Mongoid
13
13
  # @example Bind one document.
14
14
  # person.posts.bind_one(post)
15
15
  #
16
- # @param [ Document ] doc The single document to bind.
17
- #
18
16
  # @since 2.0.0.rc.1
19
17
  def bind_one(doc)
20
18
  binding do
@@ -27,8 +25,6 @@ module Mongoid
27
25
  # @example Unbind the document.
28
26
  # person.posts.unbind_one(document)
29
27
  #
30
- # @param [ Document ] document The document to unbind.
31
- #
32
28
  # @since 2.0.0.rc.1
33
29
  def unbind_one(doc)
34
30
  binding do
@@ -12,7 +12,7 @@ module Mongoid
12
12
  # @example Build the document.
13
13
  # Builder.new(meta, attrs).build
14
14
  #
15
- # @param [ String ] type Not used in this context.
15
+ # @param [ String ] _type Not used in this context.
16
16
  #
17
17
  # @return [ Document ] A single document.
18
18
  def build(_type = nil)
@@ -60,7 +60,7 @@ module Mongoid
60
60
  # @example Is the document destroyable?
61
61
  # destroyable?({ :_destroy => "1" })
62
62
  #
63
- # @parma [ Hash ] attributes The attributes to pull the flag from.
63
+ # @param [ Hash ] attributes The attributes to pull the flag from.
64
64
  #
65
65
  # @return [ true, false ] If the relation can potentially be deleted.
66
66
  def destroyable?(attributes)
@@ -13,7 +13,7 @@ module Mongoid
13
13
  # Conversions.flag(metadata, 15)
14
14
  #
15
15
  # @param [ Object ] object The object to flag.
16
- # @param [ Metadata ] The relation metadata.
16
+ # @param [ Metadata ] metadata The relation metadata.
17
17
  #
18
18
  # @return [ Object ] The provided object.
19
19
  #
@@ -11,7 +11,7 @@ module Mongoid
11
11
  # @example Reset the given counter cache
12
12
  # post.reset_counters(:comments)
13
13
  #
14
- # @param [ Symbol, Array ] One or more counter caches to reset
14
+ # @param [ Symbol, Array ] counters One or more counter caches to reset
15
15
  #
16
16
  # @since 4.0.0
17
17
  def reset_counters(*counters)
@@ -27,8 +27,8 @@ module Mongoid
27
27
  # @example Reset the given counter cache
28
28
  # Post.reset_counters('50e0edd97c71c17ea9000001', :comments)
29
29
  #
30
- # @param [ String ] The id of the object that will be reset.
31
- # @param [ Symbol, Array ] One or more counter caches to reset
30
+ # @param [ String ] id The id of the object that will be reset.
31
+ # @param [ Symbol, Array ] counters One or more counter caches to reset
32
32
  #
33
33
  # @since 3.1.0
34
34
  def reset_counters(id, *counters)
@@ -49,8 +49,8 @@ module Mongoid
49
49
  # Post.update_counters('50e0edd97c71c17ea9000001',
50
50
  # :comments_count => 5, :likes_count => -2)
51
51
  #
52
- # @param [ String ] The id of the object to update.
53
- # @param [ Hash ] Key = counter_cache and Value = factor.
52
+ # @param [ String ] id The id of the object to update.
53
+ # @param [ Hash ] counters
54
54
  #
55
55
  # @since 3.1.0
56
56
  def update_counters(id, counters)
@@ -64,8 +64,8 @@ module Mongoid
64
64
  # @example Increment comments counter
65
65
  # Post.increment_counter(:comments_count, '50e0edd97c71c17ea9000001')
66
66
  #
67
- # @param [ Symbol ] Counter cache name
68
- # @param [ String ] The id of the object that will have its counter incremented.
67
+ # @param [ Symbol ] counter_name Counter cache name
68
+ # @param [ String ] id The id of the object that will have its counter incremented.
69
69
  #
70
70
  # @since 3.1.0
71
71
  def increment_counter(counter_name, id)
@@ -79,8 +79,8 @@ module Mongoid
79
79
  # @example Decrement comments counter
80
80
  # Post.decrement_counter(:comments_count, '50e0edd97c71c17ea9000001')
81
81
  #
82
- # @param [ Symbol ] Counter cache name
83
- # @param [ String ] The id of the object that will have its counter decremented.
82
+ # @param [ Symbol ] counter_name Counter cache name
83
+ # @param [ String ] id The id of the object that will have its counter decremented.
84
84
  #
85
85
  # @since 3.1.0
86
86
  def decrement_counter(counter_name, id)
@@ -96,7 +96,7 @@ module Mongoid
96
96
  # @example Add the touchable.
97
97
  # Person.add_counter_cache_callbacks(meta)
98
98
  #
99
- # @param [ Metadata ] metadata The metadata for the relation.
99
+ # @param [ Metadata ] meta The metadata for the relation.
100
100
  #
101
101
  # @since 3.1.0
102
102
  def add_counter_cache_callbacks(meta)
@@ -12,8 +12,8 @@ module Mongoid
12
12
  # @example Create the new belongs to eager load preloader.
13
13
  # BelongsTo.new(relations_metadata, parent_docs)
14
14
  #
15
- # @param [ Array<Metadata> ] Relations to eager load
16
- # @param [ Array<Document> ] Documents to preload the relations
15
+ # @param [ Array<Metadata> ] associations Relations to eager load
16
+ # @param [ Array<Document> ] docs Documents to preload the relations
17
17
  #
18
18
  # @return [ Base ] The eager load preloader
19
19
  #
@@ -84,7 +84,7 @@ module Mongoid
84
84
  # @example Set docs into parent with pk = "foo"
85
85
  # loader.set_on_parent("foo", docs)
86
86
  #
87
- # @param [ ObjectId ] parent`s id
87
+ # @param [ ObjectId ] id parent`s id
88
88
  # @param [ Document, Array ] element to push into the parent
89
89
  #
90
90
  # @since 4.0.0
@@ -140,7 +140,7 @@ module Mongoid
140
140
  # @example Set docs into parent using the current relation name.
141
141
  # loader.set_relation(doc, docs)
142
142
  #
143
- # @param [ Document ] document
143
+ # @param [ Document ] doc
144
144
  # @param [ Document, Array ] element to set into the parent
145
145
  #
146
146
  # @since 4.0.0
@@ -13,15 +13,15 @@ module Mongoid
13
13
  # made to the database.
14
14
  #
15
15
  # @example Execute the batch push.
16
- # batchable.batch_insert([ doc_one, doc_two ])
16
+ # batchable.batch_push([ doc_one, doc_two ])
17
17
  #
18
18
  # @param [ Array<Document> ] docs The docs to add.
19
19
  #
20
20
  # @return [ Array<Hash> ] The inserts.
21
21
  #
22
- # @since 3.0.0
23
- def batch_insert(docs)
24
- execute_batch_insert(docs, "$pushAll")
22
+ # @since 5.4.0
23
+ def batch_push(docs)
24
+ execute_batch_push(docs)
25
25
  end
26
26
 
27
27
  # Clear all of the docs out of the relation in a single swipe.
@@ -89,7 +89,7 @@ module Mongoid
89
89
  base.delayed_atomic_sets.clear unless _assigning?
90
90
  docs = normalize_docs(docs).compact
91
91
  target.clear and _unscoped.clear
92
- inserts = execute_batch_insert(docs, "$set")
92
+ inserts = execute_batch_set(docs)
93
93
  add_atomic_sets(inserts)
94
94
  end
95
95
  end
@@ -122,26 +122,50 @@ module Mongoid
122
122
  # @api private
123
123
  #
124
124
  # @example Perform a batch operation.
125
- # batchable.execute_batch(docs, "$set")
125
+ # batchable.execute_batch_set(docs)
126
126
  #
127
127
  # @param [ Array<Document> ] docs The docs to persist.
128
128
  # @param [ String ] operation The atomic operation.
129
129
  #
130
130
  # @return [ Array<Hash> ] The inserts.
131
131
  #
132
- # @since 3.0.0
133
- def execute_batch_insert(docs, operation)
132
+ # @since 5.4.0
133
+ def execute_batch_set(docs)
134
134
  self.inserts_valid = true
135
135
  inserts = pre_process_batch_insert(docs)
136
136
  if insertable?
137
137
  collection.find(selector).update_one(
138
- positionally(selector, operation => { path => inserts })
138
+ positionally(selector, '$set' => { path => inserts })
139
139
  )
140
140
  post_process_batch_insert(docs)
141
141
  end
142
142
  inserts
143
143
  end
144
144
 
145
+ # Perform a batch persist of the provided documents with $push and $each.
146
+ #
147
+ # @api private
148
+ #
149
+ # @example Perform a batch push.
150
+ # batchable.execute_batch_push(docs)
151
+ #
152
+ # @param [ Array<Document> ] docs The docs to persist.
153
+ # @param [ String ] operation The atomic operation.
154
+ #
155
+ # @return [ Array<Hash> ] The inserts.
156
+ #
157
+ # @since 5.4.0
158
+ def execute_batch_push(docs)
159
+ self.inserts_valid = true
160
+ pushes = pre_process_batch_insert(docs)
161
+ if insertable?
162
+ collection.find(selector).update_one(
163
+ positionally(selector, '$push' => { path => { '$each' => pushes } }))
164
+ post_process_batch_insert(docs)
165
+ end
166
+ pushes
167
+ end
168
+
145
169
  # Are we in a state to be able to batch insert?
146
170
  #
147
171
  # @api private
@@ -31,7 +31,7 @@ module Mongoid
31
31
  # @example Substitute the new document.
32
32
  # person.name.substitute(new_name)
33
33
  #
34
- # @param [ Document ] other A document to replace the target.
34
+ # @param [ Document ] replacement A document to replace the target.
35
35
  #
36
36
  # @return [ Document, nil ] The relation or nil.
37
37
  #
@@ -55,8 +55,6 @@ module Mongoid
55
55
  # @example Get the binding.
56
56
  # binding([ address ])
57
57
  #
58
- # @param [ Proxy ] new_target The new documents to bind with.
59
- #
60
58
  # @return [ Binding ] A binding object.
61
59
  #
62
60
  # @since 2.0.0.rc.1