mongoid 8.0.8 → 8.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/CHANGELOG.md +3 -3
  4. data/README.md +3 -3
  5. data/Rakefile +0 -25
  6. data/lib/config/locales/en.yml +46 -14
  7. data/lib/mongoid/association/accessors.rb +2 -2
  8. data/lib/mongoid/association/builders.rb +1 -1
  9. data/lib/mongoid/association/embedded/batchable.rb +2 -2
  10. data/lib/mongoid/association/embedded/embedded_in/buildable.rb +2 -2
  11. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +2 -1
  12. data/lib/mongoid/association/embedded/embeds_many/buildable.rb +3 -2
  13. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +6 -6
  14. data/lib/mongoid/association/embedded/embeds_one/buildable.rb +1 -1
  15. data/lib/mongoid/association/embedded/embeds_one/proxy.rb +1 -1
  16. data/lib/mongoid/association/macros.rb +0 -6
  17. data/lib/mongoid/association/nested/one.rb +40 -2
  18. data/lib/mongoid/association/proxy.rb +1 -1
  19. data/lib/mongoid/association/referenced/counter_cache.rb +2 -2
  20. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +1 -1
  21. data/lib/mongoid/association/referenced/has_many/enumerable.rb +2 -2
  22. data/lib/mongoid/association/referenced/has_many/proxy.rb +3 -3
  23. data/lib/mongoid/association/reflections.rb +2 -2
  24. data/lib/mongoid/atomic.rb +7 -16
  25. data/lib/mongoid/attributes/dynamic.rb +1 -1
  26. data/lib/mongoid/attributes/nested.rb +2 -2
  27. data/lib/mongoid/attributes/processing.rb +5 -29
  28. data/lib/mongoid/attributes/projector.rb +1 -1
  29. data/lib/mongoid/attributes/readonly.rb +1 -1
  30. data/lib/mongoid/attributes.rb +8 -2
  31. data/lib/mongoid/changeable.rb +107 -5
  32. data/lib/mongoid/clients/storage_options.rb +2 -5
  33. data/lib/mongoid/clients/validators/storage.rb +1 -13
  34. data/lib/mongoid/collection_configurable.rb +58 -0
  35. data/lib/mongoid/composable.rb +2 -0
  36. data/lib/mongoid/config/defaults.rb +60 -0
  37. data/lib/mongoid/config/options.rb +0 -3
  38. data/lib/mongoid/config/validators/async_query_executor.rb +24 -0
  39. data/lib/mongoid/config/validators.rb +1 -0
  40. data/lib/mongoid/config.rb +99 -28
  41. data/lib/mongoid/contextual/atomic.rb +1 -1
  42. data/lib/mongoid/contextual/memory.rb +233 -33
  43. data/lib/mongoid/contextual/mongo/documents_loader.rb +177 -0
  44. data/lib/mongoid/contextual/mongo.rb +370 -133
  45. data/lib/mongoid/contextual/none.rb +162 -7
  46. data/lib/mongoid/contextual.rb +12 -0
  47. data/lib/mongoid/criteria/findable.rb +2 -2
  48. data/lib/mongoid/criteria/includable.rb +4 -3
  49. data/lib/mongoid/criteria/queryable/key.rb +1 -1
  50. data/lib/mongoid/criteria/queryable/mergeable.rb +1 -1
  51. data/lib/mongoid/criteria/queryable/optional.rb +8 -8
  52. data/lib/mongoid/criteria/queryable/selectable.rb +43 -12
  53. data/lib/mongoid/criteria/queryable/selector.rb +1 -1
  54. data/lib/mongoid/criteria/queryable/storable.rb +1 -1
  55. data/lib/mongoid/criteria.rb +6 -5
  56. data/lib/mongoid/deprecable.rb +1 -2
  57. data/lib/mongoid/deprecation.rb +3 -3
  58. data/lib/mongoid/errors/create_collection_failure.rb +33 -0
  59. data/lib/mongoid/errors/drop_collection_failure.rb +27 -0
  60. data/lib/mongoid/errors/immutable_attribute.rb +26 -0
  61. data/lib/mongoid/errors/invalid_async_query_executor.rb +25 -0
  62. data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +22 -0
  63. data/lib/mongoid/errors/invalid_storage_parent.rb +2 -0
  64. data/lib/mongoid/errors.rb +4 -1
  65. data/lib/mongoid/extensions/hash.rb +2 -24
  66. data/lib/mongoid/extensions/object.rb +2 -2
  67. data/lib/mongoid/extensions/time.rb +2 -0
  68. data/lib/mongoid/fields/localized.rb +10 -0
  69. data/lib/mongoid/fields/standard.rb +10 -0
  70. data/lib/mongoid/fields.rb +53 -24
  71. data/lib/mongoid/findable.rb +27 -3
  72. data/lib/mongoid/interceptable.rb +10 -118
  73. data/lib/mongoid/matcher/eq_impl.rb +1 -1
  74. data/lib/mongoid/matcher/type.rb +1 -1
  75. data/lib/mongoid/persistable/creatable.rb +1 -0
  76. data/lib/mongoid/persistable/deletable.rb +1 -1
  77. data/lib/mongoid/persistable/savable.rb +13 -1
  78. data/lib/mongoid/persistable/unsettable.rb +2 -2
  79. data/lib/mongoid/persistable/updatable.rb +51 -1
  80. data/lib/mongoid/persistable/upsertable.rb +20 -1
  81. data/lib/mongoid/persistable.rb +3 -0
  82. data/lib/mongoid/query_cache.rb +5 -1
  83. data/lib/mongoid/railties/database.rake +7 -2
  84. data/lib/mongoid/reloadable.rb +5 -3
  85. data/lib/mongoid/stateful.rb +22 -1
  86. data/lib/mongoid/tasks/database.rake +12 -0
  87. data/lib/mongoid/tasks/database.rb +20 -0
  88. data/lib/mongoid/utils.rb +22 -0
  89. data/lib/mongoid/validatable/associated.rb +18 -96
  90. data/lib/mongoid/validatable/macros.rb +5 -5
  91. data/lib/mongoid/validatable.rb +4 -9
  92. data/lib/mongoid/version.rb +1 -1
  93. data/lib/mongoid/warnings.rb +17 -1
  94. data/lib/mongoid.rb +16 -3
  95. data/spec/integration/app_spec.rb +2 -2
  96. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +0 -40
  97. data/spec/integration/callbacks_models.rb +37 -0
  98. data/spec/integration/callbacks_spec.rb +126 -12
  99. data/spec/integration/discriminator_key_spec.rb +4 -5
  100. data/spec/integration/i18n_fallbacks_spec.rb +3 -2
  101. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +27 -0
  102. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +20 -25
  103. data/spec/mongoid/association/embedded/embeds_many_models.rb +1 -0
  104. data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +15 -2
  105. data/spec/mongoid/association/referenced/belongs_to_spec.rb +2 -18
  106. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +5 -27
  107. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +9 -50
  108. data/spec/mongoid/association/syncable_spec.rb +1 -1
  109. data/spec/mongoid/attributes_spec.rb +3 -33
  110. data/spec/mongoid/changeable_spec.rb +299 -24
  111. data/spec/mongoid/clients_spec.rb +122 -13
  112. data/spec/mongoid/collection_configurable_spec.rb +158 -0
  113. data/spec/mongoid/config/defaults_spec.rb +160 -0
  114. data/spec/mongoid/config_spec.rb +154 -27
  115. data/spec/mongoid/contextual/memory_spec.rb +332 -76
  116. data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +187 -0
  117. data/spec/mongoid/contextual/mongo_spec.rb +1009 -125
  118. data/spec/mongoid/contextual/none_spec.rb +49 -2
  119. data/spec/mongoid/copyable_spec.rb +2 -10
  120. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +4 -10
  121. data/spec/mongoid/criteria/queryable/options_spec.rb +1 -1
  122. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +419 -0
  123. data/spec/mongoid/criteria/queryable/selectable_spec.rb +1 -1
  124. data/spec/mongoid/criteria/queryable/selector_spec.rb +3 -76
  125. data/spec/mongoid/criteria/queryable/storable_spec.rb +0 -72
  126. data/spec/mongoid/criteria_projection_spec.rb +1 -4
  127. data/spec/mongoid/criteria_spec.rb +5 -9
  128. data/spec/mongoid/errors/readonly_document_spec.rb +2 -2
  129. data/spec/mongoid/extensions/hash_spec.rb +3 -3
  130. data/spec/mongoid/extensions/time_spec.rb +8 -43
  131. data/spec/mongoid/extensions/time_with_zone_spec.rb +7 -52
  132. data/spec/mongoid/fields/localized_spec.rb +46 -28
  133. data/spec/mongoid/fields_spec.rb +136 -77
  134. data/spec/mongoid/findable_spec.rb +391 -34
  135. data/spec/mongoid/indexable_spec.rb +16 -10
  136. data/spec/mongoid/interceptable_spec.rb +173 -362
  137. data/spec/mongoid/persistable/deletable_spec.rb +26 -6
  138. data/spec/mongoid/persistable/destroyable_spec.rb +26 -6
  139. data/spec/mongoid/persistable/incrementable_spec.rb +37 -0
  140. data/spec/mongoid/persistable/logical_spec.rb +37 -0
  141. data/spec/mongoid/persistable/poppable_spec.rb +36 -0
  142. data/spec/mongoid/persistable/pullable_spec.rb +72 -0
  143. data/spec/mongoid/persistable/pushable_spec.rb +72 -0
  144. data/spec/mongoid/persistable/renamable_spec.rb +36 -0
  145. data/spec/mongoid/persistable/savable_spec.rb +96 -0
  146. data/spec/mongoid/persistable/settable_spec.rb +37 -0
  147. data/spec/mongoid/persistable/unsettable_spec.rb +36 -0
  148. data/spec/mongoid/persistable/updatable_spec.rb +20 -28
  149. data/spec/mongoid/persistable/upsertable_spec.rb +80 -6
  150. data/spec/mongoid/persistence_context_spec.rb +7 -57
  151. data/spec/mongoid/query_cache_spec.rb +56 -61
  152. data/spec/mongoid/reloadable_spec.rb +24 -28
  153. data/spec/mongoid/scopable_spec.rb +70 -0
  154. data/spec/mongoid/serializable_spec.rb +9 -30
  155. data/spec/mongoid/stateful_spec.rb +122 -8
  156. data/spec/mongoid/tasks/database_rake_spec.rb +74 -0
  157. data/spec/mongoid/tasks/database_spec.rb +127 -0
  158. data/spec/mongoid/timestamps_spec.rb +9 -11
  159. data/spec/mongoid/touchable_spec.rb +277 -5
  160. data/spec/mongoid/touchable_spec_models.rb +3 -1
  161. data/spec/mongoid/traversable_spec.rb +9 -24
  162. data/spec/mongoid/validatable/associated_spec.rb +30 -13
  163. data/spec/mongoid/validatable/uniqueness_spec.rb +2 -3
  164. data/spec/mongoid_spec.rb +36 -10
  165. data/spec/spec_helper.rb +5 -0
  166. data/spec/support/immutable_ids.rb +118 -0
  167. data/spec/support/macros.rb +47 -15
  168. data/spec/support/models/artist.rb +0 -1
  169. data/spec/support/models/band.rb +1 -0
  170. data/spec/support/models/book.rb +1 -0
  171. data/spec/support/models/building.rb +2 -0
  172. data/spec/support/models/cover.rb +10 -0
  173. data/spec/support/models/name.rb +0 -10
  174. data/spec/support/models/person.rb +0 -1
  175. data/spec/support/models/product.rb +1 -0
  176. data.tar.gz.sig +0 -0
  177. metadata +698 -664
  178. metadata.gz.sig +0 -0
  179. data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +0 -60
  180. data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +0 -60
  181. data/spec/support/models/purse.rb +0 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d604924530b809dbef3eef08d7e3c52b14e815d674821789a98c590f704b149
4
- data.tar.gz: bfefc068fa68ed135560cff738dd7641f7df3780d01600a9f936752d5c760b8c
3
+ metadata.gz: 1f5f0b81e26dba41d786804bd1213f2c05c4594ca9e05d0f62417fc15da6532e
4
+ data.tar.gz: c44f03a981d2fc648822718522e2ee7e8db6fa3fec01764eb09d77a4183a3559
5
5
  SHA512:
6
- metadata.gz: 50280304426a7dc55cb2e86d2ce69d295f5b51df6f714dee96e4a5c0b8da00c1ca09ad516d300da6755cf5160699a982868bb46c1afd2da812f952ca8c18e240
7
- data.tar.gz: 0fccee8bb9add55b0dfaa6454a33c89fe40553ce1a3fe9bef8be36fe4c92a884970d2e2b2a44c32761a198e750351276ef38f27a5e95f7fe11be02686425568b
6
+ metadata.gz: bafd2e708f204e23b0e277df74f897d19a83f059eb34bff3b636231f6e76a83176a45ea1b49586bff52c44bc3a526bd0d988cec3441bc469514035ad0acc7272
7
+ data.tar.gz: 59e6b77e3e03d94cb82fd2aad20c3951cbcc9f0872138ae76f6837986aad641cce339d5d51b8122047b5732266883708f847a7bd23c0ed2fe7bf8e19e834e045
checksums.yaml.gz.sig CHANGED
Binary file
data/CHANGELOG.md CHANGED
@@ -63,7 +63,7 @@ For instructions on upgrading to newer versions, visit
63
63
 
64
64
  * Mongoid now uses the official Mongo Ruby Driver 2.x instead of Moped.
65
65
 
66
- * Most driver specific configuration options have changed, please see [here](http://docs.mongodb.org/ecosystem/tutorial/ruby-driver-tutorial/#ruby-options) for the new options.
66
+ * Most driver specific configuration options have changed, please see [here](https://www.mongodb.com/docs/ecosystem/tutorial/ruby-driver-tutorial/#ruby-options) for the new options.
67
67
 
68
68
  * All references to `session` are now replaced with `client`. This includes the mongoid.yml configuration, `store_in` options, and all exceptions and modules with `Session` in the name.
69
69
 
@@ -872,8 +872,8 @@ child elements.
872
872
  handling of validations. (Gerad Suyderhoud)
873
873
 
874
874
  * \#2443 `expire_after_seconds` is now a valid index option
875
- (http://docs.mongodb.org/manual/core/indexes/#ttl-indexes,
876
- http://docs.mongodb.org/manual/tutorial/expire-data/).
875
+ (https://www.mongodb.com/docs/manual/core/indexes/#ttl-indexes,
876
+ https://www.mongodb.com/docs/manual/tutorial/expire-data/).
877
877
 
878
878
  class Event
879
879
  include Mongoid::Document
data/README.md CHANGED
@@ -8,11 +8,11 @@ Mongoid is an ODM (Object-Document Mapper) framework for MongoDB in Ruby.
8
8
  Documentation
9
9
  -------------
10
10
 
11
- Mongoid has [extensive user documentation](https://docs.mongodb.com/mongoid/current/).
12
- [API documentation](https://docs.mongodb.com/mongoid/current/api/) is also available.
11
+ Mongoid has [extensive user documentation](https://www.mongodb.com/docs/mongoid/current/).
12
+ [API documentation](https://www.mongodb.com/docs/mongoid/current/api/) is also available.
13
13
 
14
14
  Mongoid is built on top of the MongoDB Ruby driver which has
15
- [its own user documentation](https://docs.mongodb.com/ruby-driver/current/).
15
+ [its own user documentation](https://www.mongodb.com/docs/ruby-driver/current/).
16
16
 
17
17
  Compatibility
18
18
  -------------
data/Rakefile CHANGED
@@ -11,15 +11,6 @@ $: << File.join(ROOT, 'spec/shared/lib')
11
11
  require "rake"
12
12
  require "rspec/core/rake_task"
13
13
  require 'mrss/spec_organizer'
14
- require 'rubygems/package'
15
- require 'rubygems/security/policies'
16
-
17
- def signed_gem?(path_to_gem)
18
- Gem::Package.new(path_to_gem, Gem::Security::HighSecurity).verify
19
- true
20
- rescue Gem::Security::Exception => e
21
- false
22
- end
23
14
 
24
15
  $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
25
16
  require "mongoid/version"
@@ -112,19 +103,3 @@ namespace :release do
112
103
  end
113
104
  end
114
105
  end
115
-
116
- desc 'Verifies that all built gems in pkg/ are valid'
117
- task :verify do
118
- gems = Dir['pkg/*.gem']
119
- if gems.empty?
120
- puts 'There are no gems in pkg/ to verify'
121
- else
122
- gems.each do |gem|
123
- if signed_gem?(gem)
124
- puts "#{gem} is signed"
125
- else
126
- abort "#{gem} is not signed"
127
- end
128
- end
129
- end
130
- end
@@ -31,6 +31,17 @@ en:
31
31
  since the document did not actually get saved."
32
32
  resolution: "Double check all before callbacks to make sure they are
33
33
  not unintentionally returning false."
34
+ create_collection_failure:
35
+ message: "Cannot create collection %{collection_name}
36
+ with options %{collection_options}. The following error was raised:
37
+ %{error}."
38
+ summary: "The server rejected createCollection command with given collection
39
+ options. This may happen when some of the options are invalid, or not
40
+ supported by your version of the server."
41
+ resolution: "Double check that collection options for the collection
42
+ %{collection_name} are valid. Consult with Ruby driver documentation
43
+ and MongoDB documentation if the desired options are supported by
44
+ your version of the server."
34
45
  criteria_argument_required:
35
46
  message: "Calling Criteria methods with nil arguments is not allowed."
36
47
  summary: "Arguments to Criteria methods cannot be nil, and most
@@ -86,11 +97,24 @@ en:
86
97
  resolution: "Search for an id/shard key that is in the database or set
87
98
  the Mongoid.raise_not_found_error configuration option to false,
88
99
  which will cause nil to be returned instead of raising this error."
100
+ drop_collection_failure:
101
+ message: "Cannot drop collection %{collection_name}."
102
+ summary: "Mongoid tried to drop collection %{collection_name}, but the
103
+ collection still exists in the database."
104
+ resolution: "Try to drop the collection manually using Ruby driver or
105
+ mongo shell."
89
106
  empty_config_file:
90
107
  message: "Empty configuration file: %{path}."
91
108
  summary: "Your mongoid.yml configuration file appears to be empty."
92
109
  resolution: "Ensure your configuration file contains the correct contents.
93
- Refer to: https://docs.mongodb.com/mongoid/current/reference/configuration/"
110
+ Refer to: https://www.mongodb.com/docs/mongoid/current/reference/configuration/"
111
+ immutable_attribute:
112
+ message: "Attempted to change the immutable attribute '%{name}' with
113
+ the value: %{value}."
114
+ summary: "Immutable attributes can only have values set when the
115
+ document is a new record."
116
+ resolution: "Do not attempt to update the value of '%{name}' after
117
+ the document is persisted."
94
118
  invalid_collection:
95
119
  message: "Access to the collection for %{klass} is not allowed."
96
120
  summary: "%{klass}.collection was called, and %{klass} is an embedded
@@ -105,12 +129,20 @@ en:
105
129
  A collation option is only supported if the query is executed on a MongoDB server
106
130
  with version >= 3.4."
107
131
  resolution: "Remove the collation option from the query."
132
+ invalid_async_query_executor:
133
+ message: "Invalid async_query_executor option: %{executor}."
134
+ summary: "A invalid async query executor was specified.
135
+ The valid options are: %{options}."
136
+ resolution: "Pick an allowed option or fix the typo. If you were
137
+ expecting the option to be there, please consult the following page
138
+ with respect to Mongoid's configuration:\n\n
139
+ \_\_https://www.mongodb.com/docs/mongoid/current/reference/configuration/#mongoid-configuration-options"
108
140
  invalid_config_file:
109
141
  message: "Invalid configuration file: %{path}."
110
142
  summary: "Your mongoid.yml configuration file does not contain the
111
143
  correct file structure."
112
144
  resolution: "Ensure your configuration file contains the correct contents.
113
- Refer to: https://docs.mongodb.com/mongoid/current/reference/configuration/"
145
+ Refer to: https://www.mongodb.com/docs/mongoid/current/reference/configuration/"
114
146
  invalid_config_option:
115
147
  message: "Invalid configuration option: %{name}."
116
148
  summary: "A invalid configuration option was provided in your
@@ -197,14 +229,21 @@ en:
197
229
  \_\_\_\_field :%{name}, %{option}: true\n
198
230
  \_\_end\n\n
199
231
  Refer to:
200
- https://docs.mongodb.com/mongoid/current/reference/fields/#custom-field-options"
232
+ https://www.mongodb.com/docs/mongoid/current/reference/fields/#custom-field-options"
201
233
  invalid_field_type:
202
234
  message: "Invalid field type %{type_inspection} for field '%{field}' on model '%{klass}'."
203
235
  summary: "Model '%{klass}' defines a field '%{field}' with an unknown type value
204
236
  %{type_inspection}."
205
237
  resolution: "Please provide a valid type value for the field.
206
238
  Refer to:
207
- https://docs.mongodb.com/mongoid/current/reference/fields/#using-symbols-or-strings-instead-of-classes"
239
+ https://www.mongodb.com/docs/mongoid/current/reference/fields/#using-symbols-or-strings-instead-of-classes"
240
+ invalid_global_executor_concurrency:
241
+ message: "Invalid global_executor_concurrency option."
242
+ summary: "You set global_executor_concurrency while async_query_executor
243
+ option is not set to :global_thread_pool. The global_executor_concurrency is
244
+ allowed only for the global thread pool executor."
245
+ resolution: "Set global_executor_concurrency option to :global_thread_pool
246
+ or remove global_executor_concurrency option."
208
247
  invalid_includes:
209
248
  message: "Invalid includes directive: %{klass}.includes(%{args})"
210
249
  summary: "Eager loading in Mongoid only supports providing arguments
@@ -315,7 +354,7 @@ en:
315
354
  invalid_storage_options:
316
355
  message: "Invalid options passed to %{klass}.store_in: %{options}."
317
356
  summary: "The :store_in macro takes only a hash of parameters with
318
- the keys :database, :collection, or :client."
357
+ the keys :database, :collection, :collection_options, or :client."
319
358
  resolution: "Change the options passed to store_in to match the
320
359
  documented API, and ensure all keys in the options hash are
321
360
  symbols.\n\n
@@ -324,12 +363,6 @@ en:
324
363
  \_\_\_\_include Mongoid::Document\n
325
364
  \_\_\_\_store_in collection: 'artists', database: 'music'\n
326
365
  \_\_end\n\n"
327
- invalid_storage_parent:
328
- message: "Invalid store_in call on class %{klass}."
329
- summary: "The :store_in macro can only be called on a base Mongoid Document"
330
- resolution: "Remove the store_in call on class %{klass}, as it will use its
331
- parent store configuration. Or remove the hierarchy extension for this
332
- class."
333
366
  invalid_time:
334
367
  message: "'%{value}' is not a valid Time."
335
368
  summary: "Mongoid tries to serialize the values for Date, DateTime, and
@@ -545,9 +578,8 @@ en:
545
578
  resolution: "Don't define '%{name}' as readonly, or do not attempt
546
579
  to update its value after the document is persisted."
547
580
  readonly_document:
548
- message: "Attempted to persist the readonly document '%{klass}'."
549
- summary: "Documents loaded from the database using #only
550
- cannot be persisted."
581
+ message: "Attempted to persist a readonly document of class '%{klass}'."
582
+ summary: "Documents that are marked readonly cannot be persisted."
551
583
  resolution: "Don't attempt to persist documents that are flagged as
552
584
  readonly."
553
585
  scope_overwrite:
@@ -206,7 +206,7 @@ module Mongoid
206
206
 
207
207
  # Positional projection is specified as "foo.$". In this case the
208
208
  # document that the $ is referring to should be retrieved with all
209
- # fields. See https://docs.mongodb.com/manual/reference/operator/projection/positional/
209
+ # fields. See https://www.mongodb.com/docs/manual/reference/operator/projection/positional/
210
210
  # and https://jira.mongodb.org/browse/MONGOID-4769.
211
211
  if filtered.keys == %w($)
212
212
  filtered = nil
@@ -251,7 +251,7 @@ module Mongoid
251
251
  # @example Parse the args.
252
252
  # doc.parse_args(:name => "Joe")
253
253
  #
254
- # @param [ Array ] args The arguments.
254
+ # @param [ Hash... ] *args The arguments.
255
255
  #
256
256
  # @return [ Array<Hash> ] The attributes and options.
257
257
  def parse_args(*args)
@@ -27,7 +27,7 @@ module Mongoid
27
27
  # @example Parse the args.
28
28
  # doc.parse_args(:name => "Joe")
29
29
  #
30
- # @param [ Array ] args The arguments.
30
+ # @param [ Hash... ] *args The arguments.
31
31
  #
32
32
  # @return [ Array<Hash> ] The attributes and options.
33
33
  def parse_args(*args)
@@ -97,7 +97,7 @@ module Mongoid
97
97
  # @example Batch replace the documents.
98
98
  # batchable.batch_replace([ doc_one, doc_two ])
99
99
  #
100
- # @param [ Array<Document> ] docs The docs to replace with.
100
+ # @param [ Array<Document> | Array<Hash> ] docs The docs to replace with.
101
101
  #
102
102
  # @return [ Array<Hash> ] The inserts.
103
103
  def batch_replace(docs)
@@ -235,7 +235,7 @@ module Mongoid
235
235
  # @example Normalize the docs.
236
236
  # batchable.normalize_docs(docs)
237
237
  #
238
- # @param [ Array<Hash | Document> ] docs The docs to normalize.
238
+ # @param [ Array<Document> | Array<Hash> ] docs The docs to normalize.
239
239
  #
240
240
  # @return [ Array<Document> ] The docs.
241
241
  def normalize_docs(docs)
@@ -15,8 +15,8 @@ module Mongoid
15
15
  # @example Build the document.
16
16
  # Builder.new(meta, attrs).build
17
17
  #
18
- # @param [ Object ] base The object.
19
- # @param [ Object ] object The parent hash or document.
18
+ # @param [ Document ] base The object.
19
+ # @param [ Document | Hash ] object The parent hash or document.
20
20
  # @param [ String ] type Not used in this context.
21
21
  # @param [ Hash ] selected_fields Fields which were retrieved via
22
22
  # #only. If selected_fields are specified, fields not listed in it
@@ -30,7 +30,7 @@ module Mongoid
30
30
  # @example Substitute the new document.
31
31
  # person.name.substitute(new_name)
32
32
  #
33
- # @param [ Document ] replacement A document to replace the target.
33
+ # @param [ Document | Hash ] replacement A document to replace the target.
34
34
  #
35
35
  # @return [ Document | nil ] The association or nil.
36
36
  def substitute(replacement)
@@ -40,6 +40,7 @@ module Mongoid
40
40
  return nil
41
41
  end
42
42
  _base.new_record = true
43
+ replacement = Factory.build(klass, replacement) if replacement.is_a?(::Hash)
43
44
  self._target = replacement
44
45
  bind_one
45
46
  self
@@ -17,8 +17,9 @@ module Mongoid
17
17
  # @example Build the documents.
18
18
  # Builder.new(meta, attrs).build
19
19
  #
20
- # @param [ Object ] base The base object.
21
- # @param [ Object ] object The object to use to build the association.
20
+ # @param [ Document ] base The base object.
21
+ # @param [ Array<Document> | Array<Hash> ] object The object to use
22
+ # to build the association.
22
23
  # @param [ String ] type Not used in this context.
23
24
  # @param [ Hash ] selected_fields Fields which were retrieved via
24
25
  # #only. If selected_fields are specified, fields not listed in it
@@ -19,7 +19,7 @@ module Mongoid
19
19
  # @example Push a document.
20
20
  # person.addresses.push(address)
21
21
  #
22
- # @param [ Document | Array<Document> ] args Any number of documents.
22
+ # @param [ Document... ] *args Any number of documents.
23
23
  def <<(*args)
24
24
  docs = args.flatten
25
25
  return concat(docs) if docs.size > 1
@@ -117,7 +117,7 @@ module Mongoid
117
117
  # @example Use #persisted? inside block to count persisted documents.
118
118
  # person.addresses.count { |a| a.persisted? && a.country == "FR" }
119
119
  #
120
- # @param [ Object | Array<Object> ] args Args to delegate to the target.
120
+ # @param [ Object... ] *args Args to delegate to the target.
121
121
  #
122
122
  # @return [ Integer ] The total number of persisted embedded docs, as
123
123
  # flagged by the #persisted? method.
@@ -252,7 +252,7 @@ module Mongoid
252
252
  # @example Finds the first matching document using a block.
253
253
  # person.addresses.find { |addr| addr.state == 'CA' }
254
254
  #
255
- # @param [ Array<Object> ] args Various arguments.
255
+ # @param [ Object... ] *args Various arguments.
256
256
  # @param [ Proc ] block Optional block to pass.
257
257
  #
258
258
  # @return [ Document | Array<Document> | nil ] A document or matching documents.
@@ -348,7 +348,7 @@ module Mongoid
348
348
  # @example Substitute the association's target.
349
349
  # person.addresses.substitute([ address ])
350
350
  #
351
- # @param [ Array<Document> ] docs The replacement docs.
351
+ # @param [ Array<Document> | Array<Hash> ] docs The replacement docs.
352
352
  #
353
353
  # @return [ Many ] The proxied association.
354
354
  def substitute(docs)
@@ -432,10 +432,10 @@ module Mongoid
432
432
  # If the method exists on the array, use the default proxy behavior.
433
433
  #
434
434
  # @param [ Symbol | String ] name The name of the method.
435
- # @param [ Array ] args The method args
435
+ # @param [ Object... ] *args The method args.
436
436
  # @param [ Proc ] block Optional block to pass.
437
437
  #
438
- # @return [ Criteria, Object ] A Criteria or return value from the target.
438
+ # @return [ Criteria | Object ] A Criteria or return value from the target.
439
439
  ruby2_keywords def method_missing(name, *args, &block)
440
440
  return super if _target.respond_to?(name)
441
441
  klass.send(:with_scope, criteria) do
@@ -17,7 +17,7 @@ module Mongoid
17
17
  # Builder.new(meta, attrs).build
18
18
  #
19
19
  # @param [ Document ] base The document this association hangs off of.
20
- # @param [ Document ] object The related document.
20
+ # @param [ Document | Hash ] object The related document.
21
21
  # @param [ String ] _type Not used in this context.
22
22
  # @param [ Hash ] selected_fields Fields which were retrieved via
23
23
  # #only. If selected_fields are specified, fields not listed in it
@@ -43,7 +43,7 @@ module Mongoid
43
43
  # @example Substitute the new document.
44
44
  # person.name.substitute(new_name)
45
45
  #
46
- # @param [ Document ] replacement A document to replace the target.
46
+ # @param [ Document | Hash ] replacement A document to replace the target.
47
47
  #
48
48
  # @return [ Document | nil ] The association or nil.
49
49
  def substitute(replacement)
@@ -35,15 +35,10 @@ module Mongoid
35
35
  # @api private
36
36
  class_attribute :aliased_associations
37
37
 
38
- # @return [ Set<String> ] The set of associations that are configured
39
- # with :store_as parameter.
40
- class_attribute :stored_as_associations
41
-
42
38
  self.embedded = false
43
39
  self.embedded_relations = BSON::Document.new
44
40
  self.relations = BSON::Document.new
45
41
  self.aliased_associations = {}
46
- self.stored_as_associations = Set.new
47
42
  end
48
43
 
49
44
  # This is convenience for libraries still on the old API.
@@ -224,7 +219,6 @@ module Mongoid
224
219
  self.relations = self.relations.merge(name => assoc)
225
220
  if assoc.embedded? && assoc.respond_to?(:store_as) && assoc.store_as != name
226
221
  self.aliased_associations[assoc.store_as] = name
227
- self.stored_as_associations << assoc.store_as
228
222
  end
229
223
  end
230
224
  end
@@ -32,6 +32,8 @@ module Mongoid
32
32
  parent.send(association.setter, Factory.build(@class_name, attributes))
33
33
  elsif delete?
34
34
  parent.send(association.setter, nil)
35
+ else
36
+ check_for_id_violation!
35
37
  end
36
38
  end
37
39
 
@@ -54,6 +56,17 @@ module Mongoid
54
56
 
55
57
  private
56
58
 
59
+ # Extracts and converts the id to the expected type.
60
+ #
61
+ # @return [ BSON::ObjectId | String | Object | nil ] The converted id,
62
+ # or nil if no id is present in the attributes hash.
63
+ def extracted_id
64
+ @extracted_id ||= begin
65
+ id = association.klass.extract_id_field(attributes)
66
+ convert_id(existing.class, id)
67
+ end
68
+ end
69
+
57
70
  # Is the id in the attributes acceptable for allowing an update to
58
71
  # the existing association?
59
72
  #
@@ -64,8 +77,7 @@ module Mongoid
64
77
  #
65
78
  # @return [ true | false ] If the id part of the logic will allow an update.
66
79
  def acceptable_id?
67
- id = association.klass.extract_id_field(attributes)
68
- id = convert_id(existing.class, id)
80
+ id = extracted_id
69
81
  existing._id == id || id.nil? || (existing._id != id && update_only?)
70
82
  end
71
83
 
@@ -110,6 +122,32 @@ module Mongoid
110
122
  def update?
111
123
  existing && !destroyable? && acceptable_id?
112
124
  end
125
+
126
+ # Checks to see if the _id attribute (which is supposed to be
127
+ # immutable) is being asked to change. If so, raise an exception.
128
+ #
129
+ # If Mongoid::Config.immutable_ids is false, this will do nothing,
130
+ # and the update operation will fail silently.
131
+ #
132
+ # @raise [ Errors::ImmutableAttribute ] if _id has changed, and
133
+ # the document has been persisted.
134
+ def check_for_id_violation!
135
+ # look for the basic criteria of an update (see #update?)
136
+ return unless existing&.persisted? && !destroyable?
137
+
138
+ # if the id is either absent, or if it equals the existing record's
139
+ # id, there is no immutability violation.
140
+ id = extracted_id
141
+ return if existing._id == id || id.nil?
142
+
143
+ # otherwise, an attempt has been made to set the _id of an existing,
144
+ # persisted document.
145
+ if Mongoid::Config.immutable_ids
146
+ raise Errors::ImmutableAttribute.new(:_id, id)
147
+ else
148
+ Mongoid::Warnings.warn_mutable_ids
149
+ end
150
+ end
113
151
  end
114
152
  end
115
153
  end
@@ -118,7 +118,7 @@ module Mongoid
118
118
  # to the target of the proxy. This can be overridden in special cases.
119
119
  #
120
120
  # @param [ String | Symbol ] name The name of the method.
121
- # @param [ Array ] args The arguments passed to the method.
121
+ # @param [ Object... ] *args The arguments passed to the method.
122
122
  ruby2_keywords def method_missing(name, *args, &block)
123
123
  _target.send(name, *args, &block)
124
124
  end
@@ -13,7 +13,7 @@ module Mongoid
13
13
  # @example Reset the given counter cache
14
14
  # post.reset_counters(:comments)
15
15
  #
16
- # @param [ Symbol | Array ] counters One or more counter caches to reset
16
+ # @param [ Symbol... ] *counters One or more counter caches to reset.
17
17
  def reset_counters(*counters)
18
18
  self.class.with(persistence_context) do |_class|
19
19
  _class.reset_counters(self, *counters)
@@ -30,7 +30,7 @@ module Mongoid
30
30
  # Post.reset_counters('50e0edd97c71c17ea9000001', :comments)
31
31
  #
32
32
  # @param [ String ] id The id of the object that will be reset.
33
- # @param [ Symbol | Array ] counters One or more counter caches to reset
33
+ # @param [ Symbol... ] *counters One or more counter caches to reset.
34
34
  def reset_counters(id, *counters)
35
35
  document = id.is_a?(Document) ? id : find(id)
36
36
  counters.each do |name|
@@ -21,7 +21,7 @@ module Mongoid
21
21
  # @example Concat with other documents.
22
22
  # person.posts.concat([ post_one, post_two ])
23
23
  #
24
- # @param [ Document | Array<Document> ] args Any number of documents.
24
+ # @param [ Document... ] *args Any number of documents.
25
25
  #
26
26
  # @return [ Array<Document> ] The loaded docs.
27
27
  def <<(*args)
@@ -215,13 +215,13 @@ module Mongoid
215
215
  # completely depending on whether it is iterated to completion.
216
216
  #
217
217
  # This method can take a parameter and a block. The behavior with
218
- # either the paramater or the block is delegated to the standard
218
+ # either the parameter or the block is delegated to the standard
219
219
  # library Enumerable module.
220
220
  #
221
221
  # Note that when Enumerable's any? method is invoked with both
222
222
  # a block and a pattern, it only uses the pattern.
223
223
  #
224
- # @param [ Object ] condition The condition that documents
224
+ # @param [ Object... ] *args The condition that documents
225
225
  # must satisfy. See Enumerable documentation for details.
226
226
  #
227
227
  # @return [ true | false ] If the association has any documents.
@@ -25,7 +25,7 @@ module Mongoid
25
25
  # @example Concat with other documents.
26
26
  # person.posts.concat([ post_one, post_two ])
27
27
  #
28
- # @param [ Document | Array<Document> ] args Any number of documents.
28
+ # @param [ Document... ] *args Any number of documents.
29
29
  #
30
30
  # @return [ Array<Document> ] The loaded docs.
31
31
  def <<(*args)
@@ -203,7 +203,7 @@ module Mongoid
203
203
  # @note This will keep matching documents in memory for iteration
204
204
  # later.
205
205
  #
206
- # @param [ Object | Array<Object> ] *args The ids.
206
+ # @param [ [ Object | Array<Object> ]... ] *args The ids.
207
207
  # @param [ Proc ] block Optional block to pass.
208
208
  #
209
209
  # @return [ Document | Array<Document> | nil ] A document or matching documents.
@@ -418,7 +418,7 @@ module Mongoid
418
418
  # If the method exists on the array, use the default proxy behavior.
419
419
  #
420
420
  # @param [ Symbol | String ] name The name of the method.
421
- # @param [ Array ] args The method args
421
+ # @param [ Object... ] *args The method args
422
422
  # @param [ Proc ] block Optional block to pass.
423
423
  #
424
424
  # @return [ Criteria | Object ] A Criteria or return value from the target.
@@ -25,7 +25,7 @@ module Mongoid
25
25
  # @example Find multiple association metadata by macro.
26
26
  # person.reflect_on_all_associations(:embeds_many)
27
27
  #
28
- # @param [ Array<Symbol> ] macros The association macros.
28
+ # @param [ Symbol... ] *macros The association macros.
29
29
  #
30
30
  # @return [ Array<Association> ] The matching association metadata.
31
31
  def reflect_on_all_association(*macros)
@@ -51,7 +51,7 @@ module Mongoid
51
51
  # @example Find multiple association metadata by macro.
52
52
  # Person.reflect_on_all_associations(:embeds_many)
53
53
  #
54
- # @param [ Array<Symbol> ] macros The association macros.
54
+ # @param [ Symbol... ] *macros The association macros.
55
55
  #
56
56
  # @return [ Array<Association> ] The matching association metadata.
57
57
  def reflect_on_all_associations(*macros)
@@ -178,15 +178,13 @@ module Mongoid
178
178
  #
179
179
  # @return [ Object ] The associated path.
180
180
  def atomic_paths
181
- return @atomic_paths if @atomic_paths
182
-
183
- paths = if _association
184
- _association.path(self)
185
- else
186
- Atomic::Paths::Root.new(self)
187
- end
188
-
189
- paths.tap { @atomic_paths = paths unless new_record? }
181
+ @atomic_paths ||= begin
182
+ if _association
183
+ _association.path(self)
184
+ else
185
+ Atomic::Paths::Root.new(self)
186
+ end
187
+ end
190
188
  end
191
189
 
192
190
  # Get all the attributes that need to be pulled.
@@ -313,13 +311,6 @@ module Mongoid
313
311
 
314
312
  private
315
313
 
316
- # Clears all pending atomic updates.
317
- def reset_atomic_updates!
318
- Atomic::UPDATES.each do |update|
319
- send(update).clear
320
- end
321
- end
322
-
323
314
  # Generates the atomic updates in the correct order.
324
315
  #
325
316
  # @example Generate the updates.
@@ -115,7 +115,7 @@ module Mongoid
115
115
  # document.method_missing(:test)
116
116
  #
117
117
  # @param [ String | Symbol ] name The name of the method.
118
- # @param [ Array ] args The arguments to the method.
118
+ # @param [ Object... ] *args The arguments to the method.
119
119
  #
120
120
  # @return [ Object ] The result of the method call.
121
121
  def method_missing(name, *args)
@@ -33,8 +33,8 @@ module Mongoid
33
33
  # accepts_nested_attributes_for :addresses, :game, :posts
34
34
  # end
35
35
  #
36
- # @param [ Array<Symbol> | Hash ] args A list of association names, followed
37
- # by a hash of options.
36
+ # @param [ Symbol..., Hash ] *args A list of association names, followed
37
+ # by an optional hash of options.
38
38
  #
39
39
  # @option *args [ true | false ] :allow_destroy Can deletion occur?
40
40
  # @option *args [ Proc | Symbol ] :reject_if Block or symbol pointing