mongoid 7.2.0.rc1 → 7.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/Rakefile +45 -10
  5. data/lib/config/locales/en.yml +2 -2
  6. data/lib/mongoid/association/accessors.rb +1 -1
  7. data/lib/mongoid/association/constrainable.rb +1 -1
  8. data/lib/mongoid/association/depending.rb +4 -4
  9. data/lib/mongoid/association/embedded/batchable.rb +1 -1
  10. data/lib/mongoid/association/embedded/embedded_in.rb +1 -1
  11. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +10 -3
  12. data/lib/mongoid/association/nested/many.rb +1 -1
  13. data/lib/mongoid/association/nested/one.rb +4 -2
  14. data/lib/mongoid/association/proxy.rb +6 -1
  15. data/lib/mongoid/association/referenced/auto_save.rb +2 -2
  16. data/lib/mongoid/association/referenced/has_many/enumerable.rb +493 -495
  17. data/lib/mongoid/association/referenced/has_many/proxy.rb +2 -2
  18. data/lib/mongoid/association/referenced/has_one/buildable.rb +8 -0
  19. data/lib/mongoid/association/referenced/has_one/nested_builder.rb +2 -2
  20. data/lib/mongoid/association/referenced/has_one/proxy.rb +6 -1
  21. data/lib/mongoid/attributes.rb +32 -14
  22. data/lib/mongoid/attributes/projector.rb +120 -0
  23. data/lib/mongoid/cacheable.rb +2 -2
  24. data/lib/mongoid/clients.rb +1 -1
  25. data/lib/mongoid/clients/factory.rb +22 -8
  26. data/lib/mongoid/config.rb +19 -2
  27. data/lib/mongoid/contextual/aggregable/mongo.rb +10 -8
  28. data/lib/mongoid/copyable.rb +6 -2
  29. data/lib/mongoid/criteria.rb +4 -5
  30. data/lib/mongoid/criteria/findable.rb +1 -1
  31. data/lib/mongoid/criteria/queryable/expandable.rb +0 -24
  32. data/lib/mongoid/criteria/queryable/extensions.rb +0 -4
  33. data/lib/mongoid/criteria/queryable/extensions/boolean.rb +1 -1
  34. data/lib/mongoid/criteria/queryable/mergeable.rb +46 -20
  35. data/lib/mongoid/criteria/queryable/selectable.rb +8 -8
  36. data/lib/mongoid/criteria/queryable/selector.rb +0 -4
  37. data/lib/mongoid/document.rb +4 -17
  38. data/lib/mongoid/errors/delete_restriction.rb +8 -9
  39. data/lib/mongoid/evolvable.rb +1 -1
  40. data/lib/mongoid/extensions.rb +1 -0
  41. data/lib/mongoid/extensions/boolean.rb +1 -2
  42. data/lib/mongoid/extensions/false_class.rb +1 -1
  43. data/lib/mongoid/extensions/hash.rb +2 -2
  44. data/lib/mongoid/extensions/true_class.rb +1 -1
  45. data/lib/mongoid/fields.rb +46 -5
  46. data/lib/mongoid/inspectable.rb +1 -1
  47. data/lib/mongoid/interceptable.rb +3 -1
  48. data/lib/mongoid/matcher.rb +26 -43
  49. data/lib/mongoid/matcher/bits.rb +41 -0
  50. data/lib/mongoid/matcher/bits_all_clear.rb +20 -0
  51. data/lib/mongoid/matcher/bits_all_set.rb +20 -0
  52. data/lib/mongoid/matcher/bits_any_clear.rb +20 -0
  53. data/lib/mongoid/matcher/bits_any_set.rb +20 -0
  54. data/lib/mongoid/matcher/elem_match.rb +2 -1
  55. data/lib/mongoid/matcher/expression.rb +9 -14
  56. data/lib/mongoid/matcher/field_expression.rb +4 -5
  57. data/lib/mongoid/matcher/field_operator.rb +13 -11
  58. data/lib/mongoid/matcher/mod.rb +17 -0
  59. data/lib/mongoid/matcher/type.rb +99 -0
  60. data/lib/mongoid/persistable/deletable.rb +1 -2
  61. data/lib/mongoid/persistable/destroyable.rb +8 -2
  62. data/lib/mongoid/persistable/updatable.rb +27 -2
  63. data/lib/mongoid/query_cache.rb +35 -29
  64. data/lib/mongoid/reloadable.rb +5 -0
  65. data/lib/mongoid/selectable.rb +5 -7
  66. data/lib/mongoid/shardable.rb +21 -5
  67. data/lib/mongoid/stringified_symbol.rb +53 -0
  68. data/lib/mongoid/touchable.rb +23 -4
  69. data/lib/mongoid/version.rb +1 -1
  70. data/lib/rails/generators/mongoid/config/config_generator.rb +8 -1
  71. data/spec/README.md +19 -4
  72. data/spec/integration/app_spec.rb +175 -88
  73. data/spec/integration/associations/embeds_many_spec.rb +68 -0
  74. data/spec/integration/associations/embeds_one_spec.rb +24 -0
  75. data/spec/integration/associations/has_many_spec.rb +60 -0
  76. data/spec/integration/associations/has_one_spec.rb +108 -0
  77. data/spec/integration/callbacks_models.rb +49 -0
  78. data/spec/integration/callbacks_spec.rb +216 -0
  79. data/spec/integration/criteria/date_field_spec.rb +1 -1
  80. data/spec/integration/document_spec.rb +30 -0
  81. data/spec/integration/matcher_operator_data/bits_all_clear.yml +159 -0
  82. data/spec/integration/matcher_operator_data/bits_all_set.yml +159 -0
  83. data/spec/integration/matcher_operator_data/bits_any_clear.yml +159 -0
  84. data/spec/integration/matcher_operator_data/bits_any_set.yml +159 -0
  85. data/spec/integration/matcher_operator_data/comment.yml +22 -0
  86. data/spec/integration/matcher_operator_data/elem_match.yml +46 -0
  87. data/spec/integration/matcher_operator_data/gt_types.yml +63 -0
  88. data/spec/integration/matcher_operator_data/gte_types.yml +15 -0
  89. data/spec/integration/matcher_operator_data/implicit_traversal.yml +96 -0
  90. data/spec/integration/matcher_operator_data/in.yml +16 -0
  91. data/spec/integration/matcher_operator_data/lt_types.yml +15 -0
  92. data/spec/integration/matcher_operator_data/lte_types.yml +15 -0
  93. data/spec/integration/matcher_operator_data/mod.yml +55 -0
  94. data/spec/integration/matcher_operator_data/ne_types.yml +15 -0
  95. data/spec/integration/matcher_operator_data/type.yml +70 -0
  96. data/spec/integration/matcher_operator_data/type_array.yml +16 -0
  97. data/spec/integration/matcher_operator_data/type_binary.yml +18 -0
  98. data/spec/integration/matcher_operator_data/type_boolean.yml +39 -0
  99. data/spec/integration/matcher_operator_data/type_code.yml +26 -0
  100. data/spec/integration/matcher_operator_data/type_code_with_scope.yml +26 -0
  101. data/spec/integration/matcher_operator_data/type_date.yml +39 -0
  102. data/spec/integration/matcher_operator_data/type_db_pointer.yml +19 -0
  103. data/spec/integration/matcher_operator_data/type_decimal.yml +40 -0
  104. data/spec/integration/matcher_operator_data/type_double.yml +15 -0
  105. data/spec/integration/matcher_operator_data/type_int32.yml +33 -0
  106. data/spec/integration/matcher_operator_data/type_int64.yml +33 -0
  107. data/spec/integration/matcher_operator_data/type_max_key.yml +17 -0
  108. data/spec/integration/matcher_operator_data/type_min_key.yml +17 -0
  109. data/spec/integration/matcher_operator_data/type_null.yml +23 -0
  110. data/spec/integration/matcher_operator_data/type_object.yml +23 -0
  111. data/spec/integration/matcher_operator_data/type_object_id.yml +25 -0
  112. data/spec/integration/matcher_operator_data/type_regex.yml +44 -0
  113. data/spec/integration/matcher_operator_data/type_string.yml +15 -0
  114. data/spec/integration/matcher_operator_data/type_symbol.yml +32 -0
  115. data/spec/integration/matcher_operator_data/type_timestamp.yml +25 -0
  116. data/spec/integration/matcher_operator_data/type_undefined.yml +17 -0
  117. data/spec/integration/stringified_symbol_field_spec.rb +190 -0
  118. data/spec/lite_spec_helper.rb +9 -7
  119. data/spec/mongoid/association/depending_spec.rb +391 -352
  120. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +50 -0
  121. data/spec/mongoid/association/nested/one_spec.rb +18 -14
  122. data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +25 -8
  123. data/spec/mongoid/association/referenced/has_and_belongs_to_many/binding_spec.rb +1 -1
  124. data/spec/mongoid/association/referenced/has_many/binding_spec.rb +1 -1
  125. data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +1 -1
  126. data/spec/mongoid/association/referenced/has_many_models.rb +12 -0
  127. data/spec/mongoid/association/referenced/has_one_models.rb +20 -0
  128. data/spec/mongoid/association/referenced/has_one_spec.rb +1 -1
  129. data/spec/mongoid/atomic/paths_spec.rb +105 -12
  130. data/spec/mongoid/attributes/projector_data/embedded.yml +105 -0
  131. data/spec/mongoid/attributes/projector_data/fields.yml +93 -0
  132. data/spec/mongoid/attributes/projector_spec.rb +41 -0
  133. data/spec/mongoid/attributes_spec.rb +333 -0
  134. data/spec/mongoid/clients/factory_spec.rb +48 -0
  135. data/spec/mongoid/config_spec.rb +32 -0
  136. data/spec/mongoid/contextual/atomic_spec.rb +17 -4
  137. data/spec/mongoid/contextual/mongo_spec.rb +2 -2
  138. data/spec/mongoid/copyable_spec.rb +44 -17
  139. data/spec/mongoid/copyable_spec_models.rb +14 -0
  140. data/spec/mongoid/criteria/modifiable_spec.rb +1 -1
  141. data/spec/mongoid/criteria/queryable/expandable_spec.rb +0 -73
  142. data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +1 -1
  143. data/spec/mongoid/criteria/queryable/mergeable_spec.rb +105 -7
  144. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +265 -24
  145. data/spec/mongoid/criteria/queryable/selectable_shared_examples.rb +39 -0
  146. data/spec/mongoid/criteria/queryable/selectable_spec.rb +1 -565
  147. data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +590 -0
  148. data/spec/mongoid/criteria_projection_spec.rb +411 -0
  149. data/spec/mongoid/criteria_spec.rb +0 -275
  150. data/spec/mongoid/document_fields_spec.rb +26 -0
  151. data/spec/mongoid/document_spec.rb +13 -13
  152. data/spec/mongoid/equality_spec.rb +0 -1
  153. data/spec/mongoid/errors/delete_restriction_spec.rb +1 -1
  154. data/spec/mongoid/extensions/false_class_spec.rb +1 -1
  155. data/spec/mongoid/extensions/string_spec.rb +5 -5
  156. data/spec/mongoid/extensions/stringified_symbol_spec.rb +85 -0
  157. data/spec/mongoid/extensions/true_class_spec.rb +1 -1
  158. data/spec/mongoid/fields/localized_spec.rb +4 -4
  159. data/spec/mongoid/fields_spec.rb +4 -4
  160. data/spec/mongoid/inspectable_spec.rb +12 -4
  161. data/spec/mongoid/matcher/extract_attribute_data/numeric_keys.yml +104 -0
  162. data/spec/mongoid/matcher/extract_attribute_data/traversal.yml +68 -88
  163. data/spec/mongoid/matcher/extract_attribute_spec.rb +3 -13
  164. data/spec/mongoid/persistable/deletable_spec.rb +175 -1
  165. data/spec/mongoid/persistable/destroyable_spec.rb +191 -3
  166. data/spec/mongoid/persistable/savable_spec.rb +3 -5
  167. data/spec/mongoid/persistable/settable_spec.rb +30 -0
  168. data/spec/mongoid/persistable/upsertable_spec.rb +1 -1
  169. data/spec/mongoid/query_cache_middleware_spec.rb +8 -0
  170. data/spec/mongoid/reloadable_spec.rb +18 -1
  171. data/spec/mongoid/shardable_spec.rb +44 -0
  172. data/spec/mongoid/touchable_spec.rb +104 -16
  173. data/spec/mongoid/touchable_spec_models.rb +52 -0
  174. data/spec/mongoid/validatable_spec.rb +1 -1
  175. data/spec/shared/LICENSE +20 -0
  176. data/spec/shared/bin/get-mongodb-download-url +17 -0
  177. data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
  178. data/spec/shared/lib/mrss/cluster_config.rb +221 -0
  179. data/spec/shared/lib/mrss/constraints.rb +354 -0
  180. data/spec/shared/lib/mrss/docker_runner.rb +265 -0
  181. data/spec/shared/lib/mrss/lite_constraints.rb +191 -0
  182. data/spec/shared/lib/mrss/server_version_registry.rb +115 -0
  183. data/spec/shared/lib/mrss/spec_organizer.rb +162 -0
  184. data/spec/shared/lib/mrss/utils.rb +15 -0
  185. data/spec/shared/share/Dockerfile.erb +231 -0
  186. data/spec/shared/shlib/distro.sh +73 -0
  187. data/spec/shared/shlib/server.sh +290 -0
  188. data/spec/shared/shlib/set_env.sh +128 -0
  189. data/spec/spec_helper.rb +7 -1
  190. data/spec/support/client_registry.rb +9 -0
  191. data/spec/support/constraints.rb +0 -226
  192. data/spec/support/models/bolt.rb +8 -0
  193. data/spec/support/models/customer.rb +11 -0
  194. data/spec/support/models/customer_address.rb +12 -0
  195. data/spec/support/models/dictionary.rb +6 -0
  196. data/spec/support/models/hole.rb +13 -0
  197. data/spec/support/models/mop.rb +9 -0
  198. data/spec/support/models/nut.rb +8 -0
  199. data/spec/support/models/order.rb +11 -0
  200. data/spec/support/models/person.rb +8 -0
  201. data/spec/support/models/sealer.rb +8 -0
  202. data/spec/support/models/series.rb +1 -0
  203. data/spec/support/models/shirt.rb +12 -0
  204. data/spec/support/models/spacer.rb +8 -0
  205. data/spec/support/models/threadlocker.rb +8 -0
  206. data/spec/support/models/washer.rb +8 -0
  207. data/spec/support/models/wiki_page.rb +1 -0
  208. data/spec/support/spec_config.rb +8 -0
  209. metadata +655 -507
  210. metadata.gz.sig +0 -0
  211. data/spec/support/child_process_helper.rb +0 -79
  212. data/spec/support/cluster_config.rb +0 -158
  213. data/spec/support/lite_constraints.rb +0 -22
  214. data/spec/support/spec_organizer.rb +0 -130
@@ -336,16 +336,15 @@ module Mongoid
336
336
  #
337
337
  # @since 1.0.0
338
338
  def only(*args)
339
- return clone if args.flatten.empty?
340
339
  args = args.flatten
340
+ return clone if args.empty?
341
341
  if (args & Fields::IDS).empty?
342
342
  args.unshift(:_id)
343
343
  end
344
344
  if klass.hereditary?
345
- super(*args.push(klass.discriminator_key.to_sym))
346
- else
347
- super(*args)
345
+ args.push(klass.discriminator_key.to_sym)
348
346
  end
347
+ super(*args)
349
348
  end
350
349
 
351
350
  # Set the read preference for the criteria.
@@ -375,7 +374,7 @@ module Mongoid
375
374
  #
376
375
  # @since 4.0.3
377
376
  def without(*args)
378
- args -= Fields::IDS
377
+ args -= id_fields
379
378
  super(*args)
380
379
  end
381
380
 
@@ -132,7 +132,7 @@ module Mongoid
132
132
  # @since 3.0.0
133
133
  def mongoize_ids(ids)
134
134
  ids.map do |id|
135
- id = id[:id] if id.respond_to?(:keys) && id[:id]
135
+ id = id[:_id] if id.respond_to?(:keys) && id[:_id]
136
136
  klass.fields["_id"].mongoize(id)
137
137
  end
138
138
  end
@@ -15,30 +15,6 @@ module Mongoid
15
15
 
16
16
  private
17
17
 
18
- # Expands the specified condition to MongoDB syntax.
19
- #
20
- # The condition must be a hash in one of the following forms:
21
- # - {field_name: value}
22
- # - {'field_name' => value}
23
- # - {key_instance: value}
24
- # - {'$operator' => operator_value_expression}
25
- #
26
- # This method expands the key instance form to the the operator form,
27
- # and also converts hash key to string.
28
- #
29
- # The hash may contain multiple items, each representing a separate
30
- # condition.
31
- #
32
- # @param [ Hash ] condition The condition to expand.
33
- #
34
- # @return [ Hash ] The expanded condition.
35
- def expand_condition(condition)
36
- mapped = condition.map do |field, value|
37
- expand_one_condition(field, value)
38
- end
39
- Hash[mapped]
40
- end
41
-
42
18
  # Expands the specified condition to MongoDB syntax.
43
19
  #
44
20
  # This method is meant to be called when processing the items of
@@ -1,10 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  # encoding: utf-8
3
3
 
4
- unless defined?(Boolean)
5
- class Boolean; end
6
- end
7
-
8
4
  if defined?(ActiveSupport)
9
5
  unless defined?(ActiveSupport::TimeWithZone)
10
6
  require "active_support/time_with_zone"
@@ -33,4 +33,4 @@ module Mongoid
33
33
  end
34
34
  end
35
35
 
36
- ::Boolean.__send__(:extend, Mongoid::Criteria::Queryable::Extensions::Boolean::ClassMethods)
36
+ Mongoid::Boolean.__send__(:extend, Mongoid::Criteria::Queryable::Extensions::Boolean::ClassMethods)
@@ -227,7 +227,16 @@ module Mongoid
227
227
  end
228
228
 
229
229
  # Takes a criteria hash and expands Key objects into hashes containing
230
- # MQL corresponding to said key objects.
230
+ # MQL corresponding to said key objects. Also converts the input to
231
+ # BSON::Document to permit indifferent access.
232
+ #
233
+ # The argument must be a hash containing key-value pairs of the
234
+ # following forms:
235
+ # - {field_name: value}
236
+ # - {'field_name' => value}
237
+ # - {key_instance: value}
238
+ # - {:$operator => operator_value_expression}
239
+ # - {'$operator' => operator_value_expression}
231
240
  #
232
241
  # Ruby does not permit multiple symbol operators. For example,
233
242
  # {:foo.gt => 1, :foo.gt => 2} is collapsed to {:foo.gt => 2} by the
@@ -237,19 +246,23 @@ module Mongoid
237
246
  # Similarly, this method should never need to expand a literal value
238
247
  # and an operator at the same time.
239
248
  #
249
+ # This method effectively converts symbol keys to string keys in
250
+ # the input +expr+, such that the downstream code can assume that
251
+ # conditions always contain string keys.
252
+ #
240
253
  # @param [ Hash ] expr Criteria including Key instances.
241
254
  #
242
- # @return [ Hash ] The expanded criteria.
255
+ # @return [ BSON::Document ] The expanded criteria.
243
256
  private def _mongoid_expand_keys(expr)
244
257
  unless expr.is_a?(Hash)
245
258
  raise ArgumentError, 'Argument must be a Hash'
246
259
  end
247
260
 
248
- result = {}
261
+ result = BSON::Document.new
249
262
  expr.each do |field, value|
250
- field.__expr_part__(value.__expand_complex__).each do |k, v|
251
- if result[k]
252
- if result[k].is_a?(Hash)
263
+ field.__expr_part__(value.__expand_complex__, negating?).each do |k, v|
264
+ if existing = result[k]
265
+ if existing.is_a?(Hash)
253
266
  # Existing value is an operator.
254
267
  # If new value is also an operator, ensure there are no
255
268
  # conflicts and add
@@ -257,8 +270,8 @@ module Mongoid
257
270
  # The new value is also an operator.
258
271
  # If there are no conflicts, combine the hashes, otherwise
259
272
  # add new conditions to top level with $and.
260
- if (v.keys & result[k].keys).empty?
261
- result[k].update(v)
273
+ if (v.keys & existing.keys).empty?
274
+ existing.update(v)
262
275
  else
263
276
  raise NotImplementedError, 'Ruby does not allow same symbol operator with different values'
264
277
  result['$and'] ||= []
@@ -266,26 +279,39 @@ module Mongoid
266
279
  end
267
280
  else
268
281
  # The new value is a simple value.
269
- # If there isn't an $eq operator already in the query,
270
- # transform the new value into an $eq operator and add it
271
- # to the existing hash. Otherwise add the new condition
272
- # with $and to the top level.
273
- if result[k].key?('$eq')
282
+ # Transform the implicit equality to either $eq or $regexp
283
+ # depending on the type of the argument. See
284
+ # https://docs.mongodb.com/manual/reference/operator/query/eq/#std-label-eq-usage-examples
285
+ # for the description of relevant server behavior.
286
+ op = case v
287
+ when Regexp, BSON::Regexp::Raw
288
+ '$regex'
289
+ else
290
+ '$eq'
291
+ end
292
+ # If there isn't an $eq/$regex operator already in the
293
+ # query, transform the new value into an operator
294
+ # expression and add it to the existing hash. Otherwise
295
+ # add the new condition with $and to the top level.
296
+ if existing.key?(op)
274
297
  raise NotImplementedError, 'Ruby does not allow same symbol operator with different values'
275
298
  result['$and'] ||= []
276
299
  result['$and'] << {k => v}
277
300
  else
278
- result[k].update('$eq' => v)
301
+ existing.update(op => v)
279
302
  end
280
303
  end
281
304
  else
282
305
  # Existing value is a simple value.
283
- # If we are adding an operator, and the operator is not $eq,
284
- # convert existing value into $eq and add the new operator
285
- # to the same hash. Otherwise add the new condition with $and
286
- # to the top level.
287
- if v.is_a?(Hash) && !v.key?('$eq')
288
- result[k] = {'$eq' => result[k]}.update(v)
306
+ # See the notes above about transformations to $eq/$regex.
307
+ op = case existing
308
+ when Regexp, BSON::Regexp::Raw
309
+ '$regex'
310
+ else
311
+ '$eq'
312
+ end
313
+ if v.is_a?(Hash) && !v.key?(op)
314
+ result[k] = {op => existing}.update(v)
289
315
  else
290
316
  raise NotImplementedError, 'Ruby does not allow same symbol operator with different values'
291
317
  result['$and'] ||= []
@@ -195,11 +195,11 @@ module Mongoid
195
195
  end
196
196
 
197
197
  typed_override(criterion, "$exists") do |value|
198
- ::Boolean.evolve(value)
198
+ Mongoid::Boolean.evolve(value)
199
199
  end
200
200
  end
201
201
  key :exists, :override, "$exists" do |value|
202
- ::Boolean.evolve(value)
202
+ Mongoid::Boolean.evolve(value)
203
203
  end
204
204
 
205
205
  # Add a $geoIntersects or $geoWithin selection. Symbol operators must
@@ -882,19 +882,19 @@ module Mongoid
882
882
  if criterion.nil?
883
883
  raise ArgumentError, 'Criterion cannot be nil here'
884
884
  end
885
+ unless Hash === criterion
886
+ raise Errors::InvalidQuery, "Expression must be a Hash: #{Errors::InvalidQuery.truncate_expr(criterion)}"
887
+ end
885
888
 
889
+ normalized = _mongoid_expand_keys(criterion)
886
890
  clone.tap do |query|
887
- unless Hash === criterion
888
- raise Errors::InvalidQuery, "Expression must be a Hash: #{Errors::InvalidQuery.truncate_expr(criterion)}"
889
- end
890
- criterion.each do |field, value|
891
+ normalized.each do |field, value|
891
892
  field_s = field.to_s
892
893
  if field_s[0] == ?$
893
894
  # Query expression-level operator, like $and or $where
894
895
  query.add_operator_expression(field_s, value)
895
896
  else
896
- exp_field, exp_value = expand_one_condition(field, value)
897
- query.add_field_expression(exp_field, exp_value)
897
+ query.add_field_expression(field, value)
898
898
  end
899
899
  end
900
900
  query.reset_strategies!
@@ -123,10 +123,6 @@ module Mongoid
123
123
  # {'foo' => {'$lt' => 5}}. This step should be done after all
124
124
  # value-based processing is complete.
125
125
  if key.is_a?(Key)
126
- if serializer && evolved_value != value
127
- raise NotImplementedError, "This method is not prepared to handle key being a Key and serializer being not nil"
128
- end
129
-
130
126
  evolved_value = key.transform_value(evolved_value)
131
127
  end
132
128
 
@@ -151,7 +151,7 @@ module Mongoid
151
151
  #
152
152
  # @since 2.4.0
153
153
  def to_key
154
- (persisted? || destroyed?) ? [ id.to_s ] : nil
154
+ (persisted? || destroyed?) ? [ _id.to_s ] : nil
155
155
  end
156
156
 
157
157
  # Return an array with this +Document+ only in it.
@@ -229,8 +229,9 @@ module Mongoid
229
229
  became = klass.new(clone_document)
230
230
  became._id = _id
231
231
  became.instance_variable_set(:@changed_attributes, changed_attributes)
232
- became.instance_variable_set(:@errors, ActiveModel::Errors.new(became))
233
- became.errors.instance_variable_set(:@messages, errors.instance_variable_get(:@messages))
232
+ new_errors = ActiveModel::Errors.new(became)
233
+ new_errors.copy!(errors)
234
+ became.instance_variable_set(:@errors, new_errors)
234
235
  became.instance_variable_set(:@new_record, new_record?)
235
236
  became.instance_variable_set(:@destroyed, destroyed?)
236
237
  became.changed_attributes[klass.discriminator_key] = self.class.discriminator_value
@@ -272,20 +273,6 @@ module Mongoid
272
273
  @model_cache_key ||= self.class.model_name.cache_key
273
274
  end
274
275
 
275
- # Implement this for calls to flatten on array.
276
- #
277
- # @example Get the document as an array.
278
- # document.to_ary
279
- #
280
- # @return [ nil ] Always nil.
281
- #
282
- # @since 2.1.0
283
- def to_ary
284
- nil
285
- end
286
-
287
- private
288
-
289
276
  def as_attributes
290
277
  return attributes if frozen?
291
278
  embedded_relations.each_pair do |name, meta|
@@ -4,24 +4,23 @@
4
4
  module Mongoid
5
5
  module Errors
6
6
 
7
- # This error is raised when calling #save! or .create! on a model when one
8
- # of the callbacks returns false.
7
+ # This error is raised when attempting to destroy a model which has
8
+ # an association with dependency option set to restrict.
9
9
  class DeleteRestriction < MongoidError
10
10
 
11
11
  # Create the new callbacks error.
12
12
  #
13
- # @example Create the new callbacks error.
14
- # Callbacks.new(Post, :create!)
15
- #
16
- # @param [ Class ] document
17
- # @param [ Symbol ] association
13
+ # @param [ Document ] document The document that was attempted to be
14
+ # destroyed.
15
+ # @param [ Symbol ] association_name The name of the dependent
16
+ # association that prevents the document from being deleted.
18
17
  #
19
18
  # @since 3.0.0
20
- def initialize(document, relation)
19
+ def initialize(document, association_name)
21
20
  super(
22
21
  compose_message(
23
22
  "delete_restriction",
24
- { document: document.class, relation: relation }
23
+ { document: document.class, relation: association_name }
25
24
  )
26
25
  )
27
26
  end
@@ -15,7 +15,7 @@ module Mongoid
15
15
  #
16
16
  # @since 3.0.0
17
17
  def __evolve_object_id__
18
- id
18
+ _id
19
19
  end
20
20
  end
21
21
  end
@@ -38,6 +38,7 @@ require "mongoid/extensions/range"
38
38
  require "mongoid/extensions/regexp"
39
39
  require "mongoid/extensions/set"
40
40
  require "mongoid/extensions/string"
41
+ require "mongoid/stringified_symbol"
41
42
  require "mongoid/extensions/symbol"
42
43
  require "mongoid/extensions/time"
43
44
  require "mongoid/extensions/time_with_zone"
@@ -16,9 +16,8 @@ module Mongoid
16
16
  #
17
17
  # @since 3.0.0
18
18
  def mongoize(object)
19
- ::Boolean.evolve(object)
19
+ evolve(object)
20
20
  end
21
- alias :evolve :mongoize
22
21
  end
23
22
  end
24
23
  end
@@ -28,7 +28,7 @@ module Mongoid
28
28
  #
29
29
  # @since 1.0.0
30
30
  def is_a?(other)
31
- if other == ::Boolean || other.class == ::Boolean
31
+ if other == Mongoid::Boolean || other.class == Mongoid::Boolean
32
32
  return true
33
33
  end
34
34
  super(other)
@@ -117,7 +117,7 @@ module Mongoid
117
117
  #
118
118
  # @since 3.0.2
119
119
  def delete_id
120
- delete("_id") || delete("id") || delete(:id) || delete(:_id)
120
+ delete("_id") || delete(:_id) || delete("id") || delete(:id)
121
121
  end
122
122
 
123
123
  # Get the id attribute from this hash, whether it's prefixed with an
@@ -130,7 +130,7 @@ module Mongoid
130
130
  #
131
131
  # @since 2.3.2
132
132
  def extract_id
133
- self["_id"] || self["id"] || self[:id] || self[:_id]
133
+ self["_id"] || self[:_id] || self["id"] || self[:id]
134
134
  end
135
135
 
136
136
  # Fetch a nested value via dot syntax.
@@ -28,7 +28,7 @@ module Mongoid
28
28
  #
29
29
  # @since 1.0.0
30
30
  def is_a?(other)
31
- if other == ::Boolean || other.class == ::Boolean
31
+ if other == Mongoid::Boolean || other.class == Mongoid::Boolean
32
32
  return true
33
33
  end
34
34
  super(other)
@@ -12,6 +12,9 @@ module Mongoid
12
12
  module Fields
13
13
  extend ActiveSupport::Concern
14
14
 
15
+ StringifiedSymbol = Mongoid::StringifiedSymbol
16
+ Boolean = Mongoid::Boolean
17
+
15
18
  # For fields defined with symbols use the correct class.
16
19
  #
17
20
  # @since 4.0.0
@@ -30,14 +33,53 @@ module Mongoid
30
33
  regexp: Regexp,
31
34
  set: Set,
32
35
  string: String,
36
+ stringified_symbol: StringifiedSymbol,
33
37
  symbol: Symbol,
34
38
  time: Time
35
39
  }.with_indifferent_access
36
40
 
37
- # Constant for all names of the id field in a document.
41
+ # Constant for all names of the _id field in a document.
42
+ #
43
+ # This does not include aliases of _id field.
38
44
  #
39
- # @since 5.0.0
40
- IDS = [ :_id, :id, '_id', 'id' ].freeze
45
+ # @api private
46
+ IDS = [ :_id, '_id', ].freeze
47
+
48
+ module ClassMethods
49
+ # Returns the list of id fields for this model class, as both strings
50
+ # and symbols.
51
+ #
52
+ # @return [ Array<Symbol | String> ] List of id fields.
53
+ #
54
+ # @api private
55
+ def id_fields
56
+ IDS.dup.tap do |id_fields|
57
+ aliased_fields.each do |k, v|
58
+ if v == '_id'
59
+ id_fields << k.to_sym
60
+ id_fields << k
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ # Extracts the id field from the specified attributes hash based on
67
+ # aliases defined in this class.
68
+ #
69
+ # @param [ Hash ] attributes The attributes to inspect.
70
+ #
71
+ # @return [ Object ] The id value.
72
+ #
73
+ # @api private
74
+ def extract_id_field(attributes)
75
+ id_fields.each do |k|
76
+ if v = attributes[k]
77
+ return v
78
+ end
79
+ end
80
+ nil
81
+ end
82
+ end
41
83
 
42
84
  included do
43
85
  class_attribute :aliased_fields
@@ -59,8 +101,7 @@ module Mongoid
59
101
  type: BSON::ObjectId
60
102
  )
61
103
 
62
- alias :id :_id
63
- alias :id= :_id=
104
+ alias_attribute(:id, :_id)
64
105
  end
65
106
 
66
107
  # Apply all default values to the document which are not procs.