mongoid 7.6.1 → 7.6.3
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.
- checksums.yaml +4 -4
- data/lib/config/locales/en.yml +26 -0
- data/lib/mongoid/association/depending.rb +8 -4
- data/lib/mongoid/association/nested/many.rb +38 -3
- data/lib/mongoid/association/nested/nested_buildable.rb +14 -0
- data/lib/mongoid/association/referenced/has_many/proxy.rb +105 -4
- data/lib/mongoid/attributes/readonly.rb +33 -3
- data/lib/mongoid/changeable.rb +22 -4
- data/lib/mongoid/config.rb +62 -0
- data/lib/mongoid/contextual/aggregable/memory.rb +8 -2
- data/lib/mongoid/contextual/memory.rb +19 -8
- data/lib/mongoid/contextual/none.rb +51 -0
- data/lib/mongoid/criteria/queryable/mergeable.rb +12 -0
- data/lib/mongoid/criteria/queryable/selectable.rb +124 -10
- data/lib/mongoid/criteria/scopable.rb +7 -1
- data/lib/mongoid/criteria.rb +45 -1
- data/lib/mongoid/document.rb +10 -0
- data/lib/mongoid/errors/in_memory_regexp_timeout.rb +26 -0
- data/lib/mongoid/errors.rb +1 -0
- data/lib/mongoid/field_readable.rb +70 -0
- data/lib/mongoid/matchable.rb +6 -1
- data/lib/mongoid/matcher/eq_impl_with_regexp.rb +3 -5
- data/lib/mongoid/matcher/regex.rb +8 -9
- data/lib/mongoid/matcher/regexp_budget.rb +389 -0
- data/lib/mongoid/matcher.rb +1 -0
- data/lib/mongoid/reloadable.rb +1 -0
- data/lib/mongoid/threaded.rb +3 -0
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid/warnings.rb +1 -0
- data/spec/integration/app_spec.rb +8 -0
- data/spec/integration/matcher_operator_data/regex.yml +21 -0
- data/spec/integration/matcher_regexp_timeout_spec.rb +215 -0
- data/spec/integration/query_operator_guard_spec.rb +94 -0
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +96 -0
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +228 -0
- data/spec/mongoid/attributes/nested_spec.rb +202 -8
- data/spec/mongoid/attributes/readonly_dotted_spec.rb +286 -0
- data/spec/mongoid/attributes/readonly_spec.rb +387 -0
- data/spec/mongoid/changeable_spec.rb +18 -0
- data/spec/mongoid/contextual/aggregable/memory_spec.rb +98 -0
- data/spec/mongoid/contextual/memory_spec.rb +196 -0
- data/spec/mongoid/contextual/none_spec.rb +18 -0
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +29 -0
- data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +200 -0
- data/spec/mongoid/criteria_spec.rb +19 -0
- data/spec/mongoid/findable_spec.rb +14 -0
- data/spec/mongoid/matcher/regexp_budget_spec.rb +576 -0
- data/spec/mongoid/scopable_spec.rb +116 -2
- data/spec/shared/lib/mrss/cluster_config.rb +5 -40
- data/spec/shared/lib/mrss/constraints.rb +7 -46
- data/spec/shared/lib/mrss/docker_runner.rb +10 -21
- data/spec/shared/lib/mrss/eg_config_utils.rb +0 -31
- data/spec/shared/lib/mrss/release/candidate.rb +18 -12
- data/spec/shared/lib/mrss/server_version_registry.rb +1 -5
- data/spec/shared/share/Dockerfile.erb +6 -8
- data/spec/shared/shlib/server.sh +0 -50
- data/spec/shared/shlib/set_env.sh +4 -10
- data/spec/spec_helper.rb +8 -0
- data/spec/support/models/delete_all_account.rb +10 -0
- data/spec/support/models/delete_all_item.rb +9 -0
- data/spec/support/models/delete_all_scoped_item.rb +11 -0
- data/spec/support/models/delete_all_tag.rb +7 -0
- metadata +1817 -1781
|
@@ -666,22 +666,27 @@ module Mongoid
|
|
|
666
666
|
else
|
|
667
667
|
# When we have multiple criteria, combine them all with $or
|
|
668
668
|
# and add the result to self.
|
|
669
|
+
#
|
|
670
|
+
# Every criterion is normalized through _mongoid_expand_keys, which
|
|
671
|
+
# runs the operator guard (MONGOID-6005). Note that the plain Hash
|
|
672
|
+
# branch must use _mongoid_expand_keys too: building the hash by
|
|
673
|
+
# hand skips the guard, letting a criterion such as
|
|
674
|
+
# {'$where' => ...} reach the selector.
|
|
675
|
+
#
|
|
676
|
+
# The disjunction is stored directly rather than via
|
|
677
|
+
# and('$or' => exprs). Routing it through #and would nest the new
|
|
678
|
+
# $or under a top-level $and (the MONGOID-5172 behavior, first
|
|
679
|
+
# released in 8.0), which changes both the selector shape and the
|
|
680
|
+
# meaning of a pre-existing top-level $or. On 7.6 the historical
|
|
681
|
+
# shape is preserved: append to the existing $or when one is
|
|
682
|
+
# present.
|
|
669
683
|
exprs = criteria.map do |criterion|
|
|
670
684
|
if criterion.is_a?(Selectable)
|
|
671
685
|
_mongoid_expand_keys(criterion.selector)
|
|
672
686
|
else
|
|
673
|
-
|
|
674
|
-
if k.is_a?(Symbol)
|
|
675
|
-
[k.to_s, v]
|
|
676
|
-
else
|
|
677
|
-
[k, v]
|
|
678
|
-
end
|
|
679
|
-
end]
|
|
687
|
+
_mongoid_expand_keys(criterion)
|
|
680
688
|
end
|
|
681
689
|
end
|
|
682
|
-
# Should be able to do:
|
|
683
|
-
#where('$or' => exprs)
|
|
684
|
-
# But since that is broken do instead:
|
|
685
690
|
clone.tap do |query|
|
|
686
691
|
if query.selector['$or']
|
|
687
692
|
query.selector.store('$or', query.selector['$or'] + exprs)
|
|
@@ -813,6 +818,12 @@ module Mongoid
|
|
|
813
818
|
# only ever specify one criterion to #where.
|
|
814
819
|
@criterion = criterion
|
|
815
820
|
if criterion.is_a?(String)
|
|
821
|
+
unless Mongoid.allow_unsafe_query_operators?
|
|
822
|
+
raise Errors::InvalidQuery,
|
|
823
|
+
"String criteria are not allowed because they compile to the '$where' operator, " \
|
|
824
|
+
'which is not allowed in a query expression. Set Mongoid.allow_unsafe_query_operators = true ' \
|
|
825
|
+
'to permit all operators.'
|
|
826
|
+
end
|
|
816
827
|
js_query(criterion)
|
|
817
828
|
else
|
|
818
829
|
expr_query(criterion)
|
|
@@ -822,6 +833,19 @@ module Mongoid
|
|
|
822
833
|
|
|
823
834
|
private
|
|
824
835
|
|
|
836
|
+
# Operators permitted at the top level of a query expression without
|
|
837
|
+
# opt-in. Excludes $where (JS execution) and other operators not needed
|
|
838
|
+
# for ordinary application queries.
|
|
839
|
+
ALLOWED_QUERY_OPERATORS = %w[
|
|
840
|
+
$and $or $nor $not $text $comment $expr $jsonSchema $alwaysFalse $alwaysTrue
|
|
841
|
+
].freeze
|
|
842
|
+
|
|
843
|
+
# Operators that execute server-side JavaScript. These are rejected at
|
|
844
|
+
# any depth, not just at the top level: the allowlist above permits
|
|
845
|
+
# $expr and the logical operators, and their values are arbitrary
|
|
846
|
+
# nested expressions that can carry $function or $where.
|
|
847
|
+
JAVASCRIPT_QUERY_OPERATORS = %w[$where $function $accumulator].freeze
|
|
848
|
+
|
|
825
849
|
# Adds the specified expression to the query.
|
|
826
850
|
#
|
|
827
851
|
# Criterion must be a hash in one of the following forms:
|
|
@@ -847,6 +871,8 @@ module Mongoid
|
|
|
847
871
|
raise Errors::InvalidQuery, "Expression must be a Hash: #{Errors::InvalidQuery.truncate_expr(criterion)}"
|
|
848
872
|
end
|
|
849
873
|
|
|
874
|
+
# The operator guard is applied by _mongoid_expand_keys, which every
|
|
875
|
+
# query method that accepts a user-supplied expression passes through.
|
|
850
876
|
normalized = _mongoid_expand_keys(criterion)
|
|
851
877
|
clone.tap do |query|
|
|
852
878
|
normalized.each do |field, value|
|
|
@@ -862,6 +888,94 @@ module Mongoid
|
|
|
862
888
|
end
|
|
863
889
|
end
|
|
864
890
|
|
|
891
|
+
# Enforces the operator rules governed by the
|
|
892
|
+
# +allow_unsafe_query_operators+ configuration option against a
|
|
893
|
+
# normalized query expression.
|
|
894
|
+
#
|
|
895
|
+
# Two rules apply, and both are skipped when the option is true:
|
|
896
|
+
#
|
|
897
|
+
# - An operator at the top level of the expression must appear in
|
|
898
|
+
# ALLOWED_QUERY_OPERATORS.
|
|
899
|
+
# - An operator in JAVASCRIPT_QUERY_OPERATORS is rejected at any depth.
|
|
900
|
+
#
|
|
901
|
+
# This is called from #_mongoid_expand_keys rather than from the
|
|
902
|
+
# individual query methods, because that is the one point every query
|
|
903
|
+
# method taking a user-supplied expression passes through on its way to
|
|
904
|
+
# the selector.
|
|
905
|
+
#
|
|
906
|
+
# It deliberately does not cover the APIs that ask for JavaScript
|
|
907
|
+
# outright, such as #js_query and Criteria#for_js: there the developer
|
|
908
|
+
# has chosen server-side JavaScript, so there is nothing to guard
|
|
909
|
+
# against. The same goes for the low-level Storable methods
|
|
910
|
+
# (#add_field_expression, #add_operator_expression), which write to the
|
|
911
|
+
# selector directly.
|
|
912
|
+
#
|
|
913
|
+
# @param [ Hash ] expr A normalized query expression.
|
|
914
|
+
#
|
|
915
|
+
# @raise [ Errors::InvalidQuery ] If a disallowed operator is present.
|
|
916
|
+
#
|
|
917
|
+
# @api private
|
|
918
|
+
def _mongoid_validate_operators!(expr)
|
|
919
|
+
return if Mongoid.allow_unsafe_query_operators?
|
|
920
|
+
|
|
921
|
+
expr.each_key do |field|
|
|
922
|
+
field_s = field.to_s
|
|
923
|
+
next unless field_s.start_with?('$')
|
|
924
|
+
next if ALLOWED_QUERY_OPERATORS.include?(field_s)
|
|
925
|
+
|
|
926
|
+
raise Errors::InvalidQuery,
|
|
927
|
+
"Operator '#{field_s}' is not allowed in a query expression. " \
|
|
928
|
+
'Set Mongoid.allow_unsafe_query_operators = true to permit all operators.'
|
|
929
|
+
end
|
|
930
|
+
|
|
931
|
+
_mongoid_validate_no_javascript!(expr)
|
|
932
|
+
end
|
|
933
|
+
|
|
934
|
+
# Walks a query expression looking for operators that execute
|
|
935
|
+
# server-side JavaScript, descending through both hashes and arrays so
|
|
936
|
+
# that nested forms such as {'$expr' => {'$function' => ...}} and
|
|
937
|
+
# {'$or' => [ {'$where' => ...} ]} are caught.
|
|
938
|
+
#
|
|
939
|
+
# The walk does not distinguish operator position from value position,
|
|
940
|
+
# so it also rejects queries where a JavaScript operator name appears as
|
|
941
|
+
# data rather than as an operator. Server 5.0+ permits $-prefixed field
|
|
942
|
+
# names in stored documents, which makes this reachable:
|
|
943
|
+
#
|
|
944
|
+
# Doc.where(payload: { '$eq' => { '$function' => 'abc' } })
|
|
945
|
+
#
|
|
946
|
+
# Here the $eq marks its argument as a literal value to compare, so the
|
|
947
|
+
# server never evaluates it, but the guard raises anyway. The only
|
|
948
|
+
# workaround today is the global allow_unsafe_query_operators flag.
|
|
949
|
+
#
|
|
950
|
+
# TODO: discuss whether to track operator position (skipping the subtree
|
|
951
|
+
# under $eq, $ne, $in, $nin, and $elemMatch values) in a future
|
|
952
|
+
# iteration. It removes the false positive but adds exactly the kind of
|
|
953
|
+
# state that a real bypass could hide in, so it was left out for now.
|
|
954
|
+
#
|
|
955
|
+
# @param [ Object ] object A fragment of a query expression.
|
|
956
|
+
#
|
|
957
|
+
# @raise [ Errors::InvalidQuery ] If a JavaScript operator is present.
|
|
958
|
+
#
|
|
959
|
+
# @api private
|
|
960
|
+
def _mongoid_validate_no_javascript!(object)
|
|
961
|
+
case object
|
|
962
|
+
when Hash
|
|
963
|
+
object.each do |key, value|
|
|
964
|
+
key_s = key.to_s
|
|
965
|
+
if JAVASCRIPT_QUERY_OPERATORS.include?(key_s)
|
|
966
|
+
raise Errors::InvalidQuery,
|
|
967
|
+
"Operator '#{key_s}' executes server-side JavaScript and is not allowed " \
|
|
968
|
+
'anywhere in a query expression. Set Mongoid.allow_unsafe_query_operators = true ' \
|
|
969
|
+
'to permit all operators.'
|
|
970
|
+
end
|
|
971
|
+
|
|
972
|
+
_mongoid_validate_no_javascript!(value)
|
|
973
|
+
end
|
|
974
|
+
when Array
|
|
975
|
+
object.each { |value| _mongoid_validate_no_javascript!(value) }
|
|
976
|
+
end
|
|
977
|
+
end
|
|
978
|
+
|
|
865
979
|
# Force the values of the criterion to be evolved.
|
|
866
980
|
#
|
|
867
981
|
# @api private
|
|
@@ -11,9 +11,13 @@ module Mongoid
|
|
|
11
11
|
#
|
|
12
12
|
# @return [ Criteria ] The criteria.
|
|
13
13
|
def apply_default_scope
|
|
14
|
+
explicit_none = none_state == true
|
|
14
15
|
klass.without_default_scope do
|
|
15
16
|
merge!(klass.default_scoping.call)
|
|
16
17
|
end
|
|
18
|
+
# A none coming from the default scope is not an explicit none, so it
|
|
19
|
+
# may be removed by unscoping.
|
|
20
|
+
@none = :default if !explicit_none && @none
|
|
17
21
|
self.scoping_options = true, false
|
|
18
22
|
end
|
|
19
23
|
|
|
@@ -98,7 +102,9 @@ module Mongoid
|
|
|
98
102
|
crit = clone
|
|
99
103
|
unless unscoped?
|
|
100
104
|
crit.scoping_options = false, true
|
|
101
|
-
crit.selector.clear
|
|
105
|
+
crit.selector.clear
|
|
106
|
+
crit.options.clear
|
|
107
|
+
crit.send(:clear_default_scope_none)
|
|
102
108
|
end
|
|
103
109
|
crit
|
|
104
110
|
end
|
data/lib/mongoid/criteria.rb
CHANGED
|
@@ -276,6 +276,20 @@ module Mongoid
|
|
|
276
276
|
self.documents = criteria.documents.dup unless criteria.documents.empty?
|
|
277
277
|
self.scoping_options = criteria.scoping_options
|
|
278
278
|
self.inclusions = (inclusions + criteria.inclusions).uniq
|
|
279
|
+
if criteria.empty_and_chainable?
|
|
280
|
+
# An explicit none wins over a none that only came from a default
|
|
281
|
+
# scope. Association proxies forward is_a? to their target, so the
|
|
282
|
+
# runtime class is checked instead; anything that is not a criteria is
|
|
283
|
+
# treated as an explicit none.
|
|
284
|
+
explicit = if Criteria === criteria # rubocop:disable Style/CaseEquality
|
|
285
|
+
criteria.send(:none_state) == true
|
|
286
|
+
else
|
|
287
|
+
true
|
|
288
|
+
end
|
|
289
|
+
@none = true if @none == true || explicit
|
|
290
|
+
@none ||= :default
|
|
291
|
+
@context = nil
|
|
292
|
+
end
|
|
279
293
|
self
|
|
280
294
|
end
|
|
281
295
|
|
|
@@ -287,7 +301,9 @@ module Mongoid
|
|
|
287
301
|
#
|
|
288
302
|
# @return [ Criteria ] The none criteria.
|
|
289
303
|
def none
|
|
290
|
-
@none = true
|
|
304
|
+
@none = true
|
|
305
|
+
@context = nil
|
|
306
|
+
self
|
|
291
307
|
end
|
|
292
308
|
|
|
293
309
|
# Is the criteria an empty but chainable criteria?
|
|
@@ -469,6 +485,34 @@ module Mongoid
|
|
|
469
485
|
|
|
470
486
|
private
|
|
471
487
|
|
|
488
|
+
# The kind of none on this criteria, if any. An explicit none is +true+,
|
|
489
|
+
# a none coming only from the default scope is +:default+, and a criteria
|
|
490
|
+
# that is not none returns +nil+.
|
|
491
|
+
#
|
|
492
|
+
# @api private
|
|
493
|
+
#
|
|
494
|
+
# @example Get the none state.
|
|
495
|
+
# criteria.send(:none_state)
|
|
496
|
+
#
|
|
497
|
+
# @return [ true | :default | nil ] The none state.
|
|
498
|
+
def none_state
|
|
499
|
+
@none
|
|
500
|
+
end
|
|
501
|
+
|
|
502
|
+
# Removes the none flag if it was set only by the default scope. An
|
|
503
|
+
# explicitly requested none is never removed.
|
|
504
|
+
#
|
|
505
|
+
# @api private
|
|
506
|
+
#
|
|
507
|
+
# @example Clear a default scope none.
|
|
508
|
+
# criteria.send(:clear_default_scope_none)
|
|
509
|
+
#
|
|
510
|
+
# @return [ Criteria ] The criteria.
|
|
511
|
+
def clear_default_scope_none
|
|
512
|
+
@none = nil if @none == :default
|
|
513
|
+
self
|
|
514
|
+
end
|
|
515
|
+
|
|
472
516
|
# Are documents in the query missing, and are we configured to raise an
|
|
473
517
|
# error?
|
|
474
518
|
#
|
data/lib/mongoid/document.rb
CHANGED
|
@@ -211,6 +211,12 @@ module Mongoid
|
|
|
211
211
|
new_errors.copy!(errors)
|
|
212
212
|
became.instance_variable_set(:@errors, new_errors)
|
|
213
213
|
became.instance_variable_set(:@new_record, new_record?)
|
|
214
|
+
# The document was constructed as new, collecting pre-create change
|
|
215
|
+
# markers. Drop them when the copied state is that of a persisted
|
|
216
|
+
# document: its pending changes were all tracked after the document
|
|
217
|
+
# was persisted, and stale markers would let in-place changes to
|
|
218
|
+
# readonly attributes bypass the dirty filter.
|
|
219
|
+
became.instance_variable_set(:@pre_create_changed_attributes, nil) unless new_record?
|
|
214
220
|
became.instance_variable_set(:@destroyed, destroyed?)
|
|
215
221
|
became.changed_attributes[klass.discriminator_key] = self.class.discriminator_value
|
|
216
222
|
became[klass.discriminator_key] = klass.discriminator_value
|
|
@@ -221,6 +227,10 @@ module Mongoid
|
|
|
221
227
|
relation = became.__send__(name)
|
|
222
228
|
Array.wrap(relation).each do |r|
|
|
223
229
|
r.instance_variable_set(:@new_record, new_record?)
|
|
230
|
+
# The embedded documents were also constructed as new; drop
|
|
231
|
+
# their pre-create change markers when they are marked as
|
|
232
|
+
# persisted, as is done for the parent document.
|
|
233
|
+
r.instance_variable_set(:@pre_create_changed_attributes, nil) unless new_record?
|
|
224
234
|
end
|
|
225
235
|
end
|
|
226
236
|
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mongoid
|
|
4
|
+
module Errors
|
|
5
|
+
# This error is raised when evaluating a query in memory exceeds
|
|
6
|
+
# Mongoid::Config.in_memory_regexp_time_limit.
|
|
7
|
+
#
|
|
8
|
+
# What the limit bounds depends on the Ruby in use. Where per-Regexp
|
|
9
|
+
# timeouts are available it is the time spent executing regular
|
|
10
|
+
# expressions; elsewhere it is the elapsed time of the whole in-memory
|
|
11
|
+
# evaluation. The message is worded to hold either way.
|
|
12
|
+
class InMemoryRegexpTimeout < MongoidError
|
|
13
|
+
# Create the new error.
|
|
14
|
+
#
|
|
15
|
+
# @example Create the new in-memory regexp timeout error.
|
|
16
|
+
# InMemoryRegexpTimeout.new(5.0)
|
|
17
|
+
#
|
|
18
|
+
# @param [ Float ] limit The limit that was exceeded, in seconds. Not
|
|
19
|
+
# always the configured one: a global Regexp.timeout stricter than the
|
|
20
|
+
# configuration takes its place.
|
|
21
|
+
def initialize(limit)
|
|
22
|
+
super(compose_message('in_memory_regexp_timeout', limit: limit))
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
data/lib/mongoid/errors.rb
CHANGED
|
@@ -9,6 +9,7 @@ require "mongoid/errors/document_not_found"
|
|
|
9
9
|
require "mongoid/errors/eager_load"
|
|
10
10
|
require "mongoid/errors/empty_config_file"
|
|
11
11
|
require "mongoid/errors/in_memory_collation_not_supported"
|
|
12
|
+
require "mongoid/errors/in_memory_regexp_timeout"
|
|
12
13
|
require "mongoid/errors/invalid_collection"
|
|
13
14
|
require "mongoid/errors/invalid_config_file"
|
|
14
15
|
require "mongoid/errors/invalid_config_option"
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rubocop:todo all
|
|
3
|
+
|
|
4
|
+
module Mongoid
|
|
5
|
+
# Reads the value of a field name from a document without dispatching the
|
|
6
|
+
# name as an arbitrary method.
|
|
7
|
+
#
|
|
8
|
+
# Field names may come from application input, where a name like +destroy+
|
|
9
|
+
# or +attributes+ would delete the document or disclose its contents.
|
|
10
|
+
# Declared field and association names are validated when they are declared
|
|
11
|
+
# (see Mongoid.destructive_fields), so a name that resolves to one of them
|
|
12
|
+
# is safe to send to a document. Any other name is read from the attributes
|
|
13
|
+
# hash, which is what the database-backed query contexts do.
|
|
14
|
+
#
|
|
15
|
+
# @api private
|
|
16
|
+
module FieldReadable
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
# Read the value of the given field name from the given document.
|
|
20
|
+
#
|
|
21
|
+
# @param [ Document ] document The document to read from.
|
|
22
|
+
# @param [ String | Symbol ] name The name of the field.
|
|
23
|
+
#
|
|
24
|
+
# @return [ Object | nil ] The value of the field, or nil when the name
|
|
25
|
+
# is neither a declared field nor present in the attributes.
|
|
26
|
+
def read_field_value(document, name)
|
|
27
|
+
name = name.to_s
|
|
28
|
+
# A blank name cannot name a field. Return nil, which is what reading
|
|
29
|
+
# one has always done.
|
|
30
|
+
return nil if name.blank?
|
|
31
|
+
|
|
32
|
+
if (meth = readable_method_for(document.class, name))
|
|
33
|
+
document.public_send(meth)
|
|
34
|
+
else
|
|
35
|
+
document.attributes[document.class.database_field_name(name)]
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Resolve the given name to a method that is declared by the given class,
|
|
40
|
+
# and therefore safe to send to one of its instances.
|
|
41
|
+
#
|
|
42
|
+
# @param [ Class ] klass The document class.
|
|
43
|
+
# @param [ String ] name The name of the field.
|
|
44
|
+
#
|
|
45
|
+
# @return [ String | nil ] The method to send, or nil when the name is
|
|
46
|
+
# not declared by the class.
|
|
47
|
+
def readable_method_for(klass, name)
|
|
48
|
+
# Fields, associations, and field aliases each define a reader of their
|
|
49
|
+
# own name. Note that associations must be resolved before aliases: a
|
|
50
|
+
# belongs_to aliases its own name to its foreign key, and reading
|
|
51
|
+
# `band` must give the document, not the id.
|
|
52
|
+
return name if klass.relations.key?(name) ||
|
|
53
|
+
klass.fields.key?(name) ||
|
|
54
|
+
klass.aliased_fields.key?(name)
|
|
55
|
+
|
|
56
|
+
# An association may also be named by its `store_as`, which has no
|
|
57
|
+
# reader of its own, or by its ids accessor, which does.
|
|
58
|
+
if (assoc = klass.relations[klass.aliased_associations[name]])
|
|
59
|
+
return (assoc.store_as == name) ? assoc.name.to_s : name
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Localized fields also get a _translations reader, which does not
|
|
63
|
+
# appear in the fields hash.
|
|
64
|
+
base = name.delete_suffix("_translations")
|
|
65
|
+
return nil if base == name
|
|
66
|
+
|
|
67
|
+
name if klass.fields[klass.database_field_name(base)]&.localized?
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
data/lib/mongoid/matchable.rb
CHANGED
|
@@ -17,7 +17,12 @@ module Mongoid
|
|
|
17
17
|
#
|
|
18
18
|
# @return [ true, false ] True if matches, false if not.
|
|
19
19
|
def _matches?(selector)
|
|
20
|
-
|
|
20
|
+
# Opens a regexp budget for this document only. Callers that match many
|
|
21
|
+
# documents against one selector open a budget of their own first, and
|
|
22
|
+
# this one joins it rather than giving every document a fresh limit.
|
|
23
|
+
Matcher::RegexpBudget.open(selector) do
|
|
24
|
+
Matcher::Expression.matches?(self, selector)
|
|
25
|
+
end
|
|
21
26
|
end
|
|
22
27
|
end
|
|
23
28
|
end
|
|
@@ -6,12 +6,10 @@ module Mongoid
|
|
|
6
6
|
#
|
|
7
7
|
# @api private
|
|
8
8
|
module EqImplWithRegexp
|
|
9
|
-
module_function def matches?(
|
|
9
|
+
module_function def matches?(_original_operator, value, condition)
|
|
10
10
|
case condition
|
|
11
|
-
when Regexp
|
|
12
|
-
value
|
|
13
|
-
when ::BSON::Regexp::Raw
|
|
14
|
-
value =~ condition.compile
|
|
11
|
+
when Regexp, ::BSON::Regexp::Raw
|
|
12
|
+
value.respond_to?(:=~) && RegexpBudget.match?(value, condition)
|
|
15
13
|
else
|
|
16
14
|
if Mongoid.compare_time_by_ms &&
|
|
17
15
|
value.kind_of?(Time) && condition.kind_of?(Time)
|
|
@@ -3,25 +3,24 @@ module Mongoid
|
|
|
3
3
|
|
|
4
4
|
# @api private
|
|
5
5
|
module Regex
|
|
6
|
-
module_function def matches?(
|
|
7
|
-
condition
|
|
8
|
-
when Regexp
|
|
9
|
-
condition
|
|
10
|
-
when BSON::Regexp::Raw
|
|
11
|
-
condition.compile
|
|
12
|
-
else
|
|
6
|
+
module_function def matches?(_exists, value, condition)
|
|
7
|
+
unless condition.is_a?(Regexp) || condition.is_a?(BSON::Regexp::Raw)
|
|
13
8
|
# Note that strings must have been converted to a regular expression
|
|
14
9
|
# instance already (with $options taken into account, if provided).
|
|
15
10
|
raise Errors::InvalidQuery, "$regex requires a regular expression argument: #{Errors::InvalidQuery.truncate_expr(condition)}"
|
|
16
11
|
end
|
|
17
12
|
|
|
13
|
+
# The condition is compiled by RegexpBudget rather than here, so that
|
|
14
|
+
# the budget's timeout can be baked into the pattern.
|
|
18
15
|
case value
|
|
19
16
|
when Array
|
|
17
|
+
# Object#=~ is gone as of Ruby 3.2, so an element that cannot be
|
|
18
|
+
# matched against has to be rejected rather than passed to =~.
|
|
20
19
|
value.any? do |v|
|
|
21
|
-
v
|
|
20
|
+
v.respond_to?(:=~) && RegexpBudget.match?(v, condition)
|
|
22
21
|
end
|
|
23
22
|
when String
|
|
24
|
-
value
|
|
23
|
+
RegexpBudget.match?(value, condition)
|
|
25
24
|
else
|
|
26
25
|
false
|
|
27
26
|
end
|