mongoid 9.1.1 → 9.1.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 +44 -11
- data/lib/mongoid/association/accessors.rb +21 -4
- data/lib/mongoid/association/eager_load/discriminated_inclusion.rb +10 -10
- data/lib/mongoid/association/eager_load/embedded_distributor.rb +6 -6
- data/lib/mongoid/association/eager_load/lookup_pipeline.rb +1 -1
- data/lib/mongoid/association/eager_loadable.rb +8 -1
- data/lib/mongoid/association/embedded/batchable.rb +17 -1
- data/lib/mongoid/association/nested/many.rb +1 -1
- data/lib/mongoid/association/referenced/belongs_to/buildable.rb +19 -15
- data/lib/mongoid/association/referenced/belongs_to.rb +11 -3
- data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +25 -5
- data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +12 -0
- data/lib/mongoid/association/referenced/has_many/buildable.rb +17 -7
- data/lib/mongoid/association/referenced/has_many/proxy.rb +4 -2
- data/lib/mongoid/association/referenced/has_one/buildable.rb +20 -6
- data/lib/mongoid/association/relatable.rb +18 -0
- data/lib/mongoid/association.rb +23 -0
- data/lib/mongoid/atomic/modifiers.rb +100 -16
- data/lib/mongoid/attributes/readonly.rb +19 -1
- data/lib/mongoid/changeable.rb +19 -1
- data/lib/mongoid/clients/factory.rb +2 -0
- data/lib/mongoid/config/encryption.rb +152 -19
- data/lib/mongoid/contextual/none.rb +51 -0
- data/lib/mongoid/criteria/scopable.rb +5 -0
- data/lib/mongoid/criteria.rb +45 -1
- data/lib/mongoid/document.rb +13 -0
- data/lib/mongoid/encryptable.rb +43 -16
- data/lib/mongoid/errors/invalid_encrypted_association.rb +28 -0
- data/lib/mongoid/errors/invalid_encryption_schema.rb +27 -0
- data/lib/mongoid/errors/no_encryption_schema.rb +5 -3
- data/lib/mongoid/errors.rb +2 -0
- data/lib/mongoid/factory.rb +1 -1
- data/lib/mongoid/fields/encrypted.rb +14 -3
- data/lib/mongoid/fields/foreign_key.rb +9 -0
- data/lib/mongoid/fields/standard.rb +10 -0
- data/lib/mongoid/fields/validators/macro.rb +29 -0
- data/lib/mongoid/fields.rb +49 -2
- data/lib/mongoid/persistable/creatable.rb +120 -14
- data/lib/mongoid/persistable.rb +15 -15
- data/lib/mongoid/persistence_context.rb +46 -4
- data/lib/mongoid/reloadable.rb +1 -0
- data/lib/mongoid/search_indexable.rb +5 -1
- data/lib/mongoid/tasks/database.rb +10 -7
- data/lib/mongoid/touchable.rb +28 -1
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid/warnings.rb +4 -0
- data/spec/integration/associations/embedded_delayed_atomic_sets_spec.rb +216 -0
- data/spec/integration/associations/has_and_belongs_to_many_spec.rb +14 -0
- data/spec/integration/atomic/modifiers_spec.rb +283 -0
- data/spec/integration/encryption_spec.rb +418 -0
- data/spec/mongoid/association/accessors_spec.rb +35 -0
- data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +35 -6
- data/spec/mongoid/association/referenced/encrypted_foreign_key_spec.rb +142 -0
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +36 -5
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +48 -0
- data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +24 -6
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +81 -0
- data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +18 -3
- data/spec/mongoid/atomic/modifiers_spec.rb +347 -0
- data/spec/mongoid/attributes/nested_spec.rb +29 -0
- data/spec/mongoid/attributes/readonly_dotted_spec.rb +334 -0
- data/spec/mongoid/attributes/readonly_spec.rb +389 -0
- data/spec/mongoid/changeable_spec.rb +18 -0
- data/spec/mongoid/clients/factory_spec.rb +36 -0
- data/spec/mongoid/config/encryption_spec.rb +450 -0
- data/spec/mongoid/contextual/none_spec.rb +18 -0
- data/spec/mongoid/criteria/includable_spec.rb +346 -0
- data/spec/mongoid/criteria_spec.rb +17 -0
- data/spec/mongoid/encryptable_spec.rb +54 -0
- data/spec/mongoid/factory_spec.rb +8 -0
- data/spec/mongoid/fields_spec.rb +37 -0
- data/spec/mongoid/findable_spec.rb +14 -0
- data/spec/mongoid/scopable_spec.rb +102 -0
- data/spec/mongoid/search_indexable_spec.rb +26 -0
- data/spec/mongoid/tasks/database_spec.rb +13 -0
- data/spec/mongoid/touchable_spec.rb +217 -0
- data/spec/mongoid/touchable_spec_models.rb +37 -0
- data/spec/support/crypt/models.rb +138 -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
- data/spec/support/models/modifier_models.rb +65 -0
- metadata +2045 -2008
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 049c589b71cab5be4a47ad8e5c2023f8f5aa00c67ee033d971a87061b587fee7
|
|
4
|
+
data.tar.gz: 3482ca9833b805cdccd5a193492892afa99a6a73be26eefac7bb0301b676322b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: af7a00edbd50604d1f1defda36e046410965cdfe373c3f1e1fb6943d4eb084edf459eed8778ae8ad0fb12fd247d0ec7de706b7370733643284d4314ad10fd28a
|
|
7
|
+
data.tar.gz: 72aed484d484da020d180fa0fddf5b31d3d0a9a137a32f9bca32b9cd16c8cc1efa38abe477fbc5b030ee3f6515dbb1736ae8879347a11cd4cb818ec403dfcafe
|
data/lib/config/locales/en.yml
CHANGED
|
@@ -239,6 +239,26 @@ en:
|
|
|
239
239
|
Mongoid does not support."
|
|
240
240
|
resolution: "Valid field operators are: %{valid_operators}.
|
|
241
241
|
Ensure you are using one of these operators."
|
|
242
|
+
invalid_encrypted_association:
|
|
243
|
+
message: "The %{association} association of %{klass} uses the encrypted
|
|
244
|
+
field %{field} as its foreign key."
|
|
245
|
+
summary: "Client-side field level encryption can encrypt an array
|
|
246
|
+
only as a whole, using the random algorithm; the deterministic
|
|
247
|
+
algorithm does not support arrays at all. A
|
|
248
|
+
has_and_belongs_to_many association stores its foreign key in an
|
|
249
|
+
array and queries and atomically updates individual elements of
|
|
250
|
+
it, which encryption does not support. Mongoid raises this error
|
|
251
|
+
when the association or the field is defined, whichever comes
|
|
252
|
+
first, rather than defining a key the association cannot manage."
|
|
253
|
+
resolution: "Remove the encryption from %{field}, or store a single
|
|
254
|
+
encrypted value with a belongs_to association instead."
|
|
255
|
+
invalid_encryption_schema:
|
|
256
|
+
message: "The encryption schema for %{namespace} is invalid."
|
|
257
|
+
summary: "Two models writing to %{namespace} describe %{path}
|
|
258
|
+
differently. The encryption schema has one entry per namespace,
|
|
259
|
+
so the two descriptions cannot both be used."
|
|
260
|
+
resolution: "Make the models agree on the encryption options of the
|
|
261
|
+
conflicting field, or store them in different collections."
|
|
242
262
|
invalid_estimated_count_criteria:
|
|
243
263
|
message: "Invalid criteria for estimated_count."
|
|
244
264
|
summary: "Estimated count is strictly a collection-level operation and cannot be called
|
|
@@ -544,22 +564,35 @@ en:
|
|
|
544
564
|
no_encryption_schema:
|
|
545
565
|
message: "The encryption schema of client %{client} does not cover
|
|
546
566
|
%{namespace}."
|
|
547
|
-
summary: "%{klass} declares encrypted fields,
|
|
548
|
-
resolved to, %{namespace},
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
567
|
+
summary: "%{klass} declares encrypted fields, directly or through
|
|
568
|
+
an embedded model, but the namespace it resolved to, %{namespace},
|
|
569
|
+
is not part of the automatic encryption schema of client
|
|
570
|
+
%{client}. The encryption schema is built once, when the client
|
|
571
|
+
is created, and is keyed by namespace, so a database name that is
|
|
572
|
+
only known later - a callable :database option,
|
|
573
|
+
Model.with(database:), Mongoid.override_database - is not in it.
|
|
574
|
+
The same happens when an embedded model with encrypted fields was
|
|
575
|
+
not yet loaded when the client was created. Routing the model to a
|
|
576
|
+
client that has no auto_encryption_options has the same effect.
|
|
577
|
+
Mongoid refuses the operation because the driver would store the
|
|
578
|
+
declared fields in plaintext without reporting an error."
|
|
557
579
|
resolution: "Give the model a namespace that is known when the
|
|
558
580
|
client is created, and a client configured with
|
|
559
|
-
auto_encryption_options.
|
|
581
|
+
auto_encryption_options. Load all models, including embedded
|
|
582
|
+
ones, before the first client is created, for example by
|
|
583
|
+
preloading them at boot. If the model has to live in a database
|
|
560
584
|
chosen at runtime, configure one client per database, each with
|
|
561
585
|
its own auto_encryption_options, and select between them with
|
|
562
586
|
Model.with(client:) instead of switching the database."
|
|
587
|
+
no_encryption_schema_field:
|
|
588
|
+
message: "The encryption schema of client %{client} does not cover
|
|
589
|
+
the field %{field} of %{namespace}."
|
|
590
|
+
summary: "The namespace %{namespace} is in the automatic encryption
|
|
591
|
+
schema of client %{client}, but its entry does not encrypt
|
|
592
|
+
%{field}, which %{klass} declares as encrypted. Mongoid refuses
|
|
593
|
+
the operation rather than storing the field in plaintext."
|
|
594
|
+
resolution: "Regenerate the schema map of the client, or add
|
|
595
|
+
%{field} to the schema entry for %{namespace}."
|
|
563
596
|
no_map_reduce_output:
|
|
564
597
|
message: "No output location was specified for the map/reduce
|
|
565
598
|
operation."
|
|
@@ -86,6 +86,27 @@ module Mongoid
|
|
|
86
86
|
instance_variable_set("@_#{name}", relation)
|
|
87
87
|
end
|
|
88
88
|
|
|
89
|
+
# Loads the documents the query preloaded for this one, kept apart from the
|
|
90
|
+
# attributes so they are not part of the document. A name this class
|
|
91
|
+
# doesn't define is dropped: a $lookup writes its field on every document
|
|
92
|
+
# it returns, including those of a sibling subclass that has no such
|
|
93
|
+
# association.
|
|
94
|
+
#
|
|
95
|
+
# The documents are wrapped in PreloadedDocuments so that the builders
|
|
96
|
+
# know where they came from.
|
|
97
|
+
#
|
|
98
|
+
# @param [ Hash ] preloaded The preloaded documents by the field they
|
|
99
|
+
# travelled in.
|
|
100
|
+
#
|
|
101
|
+
# @api private
|
|
102
|
+
def load_preloaded_associations(preloaded)
|
|
103
|
+
preloaded.each do |field, documents|
|
|
104
|
+
name = field.delete_prefix(PRELOADED_PREFIX)
|
|
105
|
+
association = relations[name]
|
|
106
|
+
__build__(name, PreloadedDocuments.new(documents), association) if association
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
89
110
|
private
|
|
90
111
|
|
|
91
112
|
# Get the association. Extracted out from the getter method to avoid
|
|
@@ -127,10 +148,6 @@ module Mongoid
|
|
|
127
148
|
_loading do
|
|
128
149
|
if object && needs_no_database_query?(object, association)
|
|
129
150
|
__build__(name, object, association)
|
|
130
|
-
# Check if data was loaded via $lookup aggregation
|
|
131
|
-
elsif !association.embedded? && attributes.key?(name.to_s)
|
|
132
|
-
# Use the pre-loaded association data from $lookup
|
|
133
|
-
__build__(name, attributes[name.to_s], association)
|
|
134
151
|
else
|
|
135
152
|
selected_fields = _mongoid_filter_selected_fields(association.key)
|
|
136
153
|
__build__(name, attributes[association.key], association, selected_fields)
|
|
@@ -15,15 +15,15 @@ module Mongoid
|
|
|
15
15
|
# For Machine.eager_load(:widgets), where Lathe#widgets => Cog and
|
|
16
16
|
# Press#widgets => Belt, it emits:
|
|
17
17
|
#
|
|
18
|
-
# { '$lookup' => { 'from' => 'cogs', ..., 'as' => '
|
|
19
|
-
# { '$lookup' => { 'from' => 'belts', ..., 'as' => '
|
|
18
|
+
# { '$lookup' => { 'from' => 'cogs', ..., 'as' => '__mongoid_eager_loaded_widgets_Lathe' } },
|
|
19
|
+
# { '$lookup' => { 'from' => 'belts', ..., 'as' => '__mongoid_eager_loaded_widgets_Press' } },
|
|
20
20
|
# { '$set' => {
|
|
21
|
-
# '
|
|
22
|
-
# { 'case' => { '$eq' => [ '$_type', 'Lathe' ] }, 'then' => '$
|
|
23
|
-
# { 'case' => { '$eq' => [ '$_type', 'Press' ] }, 'then' => '$
|
|
21
|
+
# '__mongoid_eager_loaded_widgets' => { '$switch' => { 'branches' => [ # route each document to its
|
|
22
|
+
# { 'case' => { '$eq' => [ '$_type', 'Lathe' ] }, 'then' => '$__mongoid_eager_loaded_widgets_Lathe' }, # own type's matches
|
|
23
|
+
# { 'case' => { '$eq' => [ '$_type', 'Press' ] }, 'then' => '$__mongoid_eager_loaded_widgets_Press' }
|
|
24
24
|
# ], 'default' => [] } }
|
|
25
25
|
# } },
|
|
26
|
-
# { '$unset' => [ '
|
|
26
|
+
# { '$unset' => [ '__mongoid_eager_loaded_widgets_Lathe', '__mongoid_eager_loaded_widgets_Press' ] }
|
|
27
27
|
#
|
|
28
28
|
# @api private
|
|
29
29
|
class DiscriminatedInclusion < Inclusion
|
|
@@ -56,7 +56,7 @@ module Mongoid
|
|
|
56
56
|
end
|
|
57
57
|
|
|
58
58
|
def temporary_field(node)
|
|
59
|
-
|
|
59
|
+
node.association.temporary_field(owner(node).discriminator_value)
|
|
60
60
|
end
|
|
61
61
|
|
|
62
62
|
# The $set that fills the association on each document from the temporary
|
|
@@ -68,15 +68,15 @@ module Mongoid
|
|
|
68
68
|
'then' => "$#{field}"
|
|
69
69
|
}
|
|
70
70
|
end
|
|
71
|
-
{ '$set' => {
|
|
71
|
+
{ '$set' => { preloaded_field => { '$switch' => { 'branches' => branches, 'default' => [] } } } }
|
|
72
72
|
end
|
|
73
73
|
|
|
74
74
|
def owner(node)
|
|
75
75
|
node.association.inverse_class
|
|
76
76
|
end
|
|
77
77
|
|
|
78
|
-
def
|
|
79
|
-
@nodes.first.association.
|
|
78
|
+
def preloaded_field
|
|
79
|
+
@nodes.first.association.preloaded_field
|
|
80
80
|
end
|
|
81
81
|
|
|
82
82
|
def discriminator_key
|
|
@@ -22,15 +22,15 @@ module Mongoid
|
|
|
22
22
|
# 'from' => 'devices', # the embedded port, so they are
|
|
23
23
|
# 'localField' => 'port.device_id', # collected in a temp top-level
|
|
24
24
|
# 'foreignField' => '_id', # field instead
|
|
25
|
-
# 'as' => '
|
|
25
|
+
# 'as' => '__mongoid_eager_loaded_device_port'
|
|
26
26
|
# } },
|
|
27
27
|
# { '$set' => {
|
|
28
|
-
# 'port' => { '$mergeObjects' => [ # merge
|
|
28
|
+
# 'port' => { '$mergeObjects' => [ # merge the devices onto the port
|
|
29
29
|
# '$port',
|
|
30
|
-
# { '
|
|
30
|
+
# { '__mongoid_eager_loaded_device' => { '$filter' => { ... } } } # this port's matches
|
|
31
31
|
# ] }
|
|
32
32
|
# } },
|
|
33
|
-
# { '$unset' => '
|
|
33
|
+
# { '$unset' => '__mongoid_eager_loaded_device_port' } # drop the temp field
|
|
34
34
|
#
|
|
35
35
|
# @api private
|
|
36
36
|
class EmbeddedDistributor
|
|
@@ -92,7 +92,7 @@ module Mongoid
|
|
|
92
92
|
end
|
|
93
93
|
|
|
94
94
|
def temporary_field
|
|
95
|
-
|
|
95
|
+
@association.temporary_field(path.tr('.', '_'))
|
|
96
96
|
end
|
|
97
97
|
|
|
98
98
|
# An embedded collection (embeds_many) is rebuilt with $map so each element
|
|
@@ -104,7 +104,7 @@ module Mongoid
|
|
|
104
104
|
element = many ? "$$#{head.store_as}" : node
|
|
105
105
|
child =
|
|
106
106
|
if rest.empty?
|
|
107
|
-
{ @association.
|
|
107
|
+
{ @association.preloaded_field => correlated_matches(element) }
|
|
108
108
|
else
|
|
109
109
|
segment = rest.first.store_as
|
|
110
110
|
{ segment => distributed_value(rest, "#{element}.#{segment}") }
|
|
@@ -94,7 +94,14 @@ module Mongoid
|
|
|
94
94
|
next unless as = assoc_map.delete(klass)
|
|
95
95
|
|
|
96
96
|
as.each do |assoc|
|
|
97
|
-
queue
|
|
97
|
+
# The queue is matched against the assoc_map keys, which are
|
|
98
|
+
# inverse_class_name values (resolved class names). Push the
|
|
99
|
+
# resolved class name so a class_name such as "::Photo" or a
|
|
100
|
+
# module-relative "Item" still matches its nested associations.
|
|
101
|
+
# A truly polymorphic association (a polymorphic belongs_to) has
|
|
102
|
+
# no single resolvable target class, so fall back to the raw name.
|
|
103
|
+
target = assoc.try_relation_class
|
|
104
|
+
queue << (target ? target.name : assoc.class_name)
|
|
98
105
|
|
|
99
106
|
# If this class is nested in the inclusion tree, only load documents
|
|
100
107
|
# for the association above it. If there is no parent association,
|
|
@@ -130,13 +130,29 @@ module Mongoid
|
|
|
130
130
|
# batchable.add_atomic_sets([{ field: value }])
|
|
131
131
|
#
|
|
132
132
|
# @param [ Array<Hash> ] sets The atomic sets.
|
|
133
|
+
#
|
|
134
|
+
# @return [ Array<Hash> | nil ] The atomic sets, or nil if the base
|
|
135
|
+
# document is not persisted or is not being assigned.
|
|
133
136
|
def add_atomic_sets(sets)
|
|
134
|
-
|
|
137
|
+
# New embedded documents are persisted whole by their parent with a
|
|
138
|
+
# $set or $push. A delayed update for one of their associations is
|
|
139
|
+
# redundant whether or not the document is attached to its parent.
|
|
140
|
+
# Before attachment, its path is relative to the document instead
|
|
141
|
+
# of the root.
|
|
142
|
+
return unless _assigning? && _base.persisted?
|
|
135
143
|
|
|
136
144
|
_base.delayed_atomic_sets[path].try(:clear)
|
|
137
145
|
_base.collect_children.each do |child|
|
|
138
146
|
child.delayed_atomic_sets.clear
|
|
139
147
|
end
|
|
148
|
+
|
|
149
|
+
# If the base document is itself inserted wholesale via a $set of
|
|
150
|
+
# its atomic path, that $set already contains these documents, and
|
|
151
|
+
# a nested $set under the same path would conflict with it. The
|
|
152
|
+
# cleanup above has already cleared the delayed sets the assigned
|
|
153
|
+
# children recorded, so skipping the recording here is safe.
|
|
154
|
+
return if _base.settable?
|
|
155
|
+
|
|
140
156
|
_base.delayed_atomic_sets[path] = sets
|
|
141
157
|
end
|
|
142
158
|
|
|
@@ -230,7 +230,7 @@ module Mongoid
|
|
|
230
230
|
# the id is only ever resolved within the association itself.
|
|
231
231
|
id
|
|
232
232
|
else
|
|
233
|
-
{ _id: id, association.foreign_key => parent.
|
|
233
|
+
{ _id: id, association.foreign_key => parent.send(association.primary_key) }
|
|
234
234
|
end
|
|
235
235
|
end
|
|
236
236
|
end
|
|
@@ -19,29 +19,33 @@ module Mongoid
|
|
|
19
19
|
#
|
|
20
20
|
# @return [ Document ] A single document.
|
|
21
21
|
def build(_base, object, type = nil, selected_fields = nil)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
# Handle array from $lookup aggregation (returns array even for belongs_to)
|
|
25
|
-
if object.is_a?(Array)
|
|
26
|
-
first = object.first
|
|
27
|
-
case first
|
|
28
|
-
when nil, Mongoid::Document then return first
|
|
29
|
-
when Hash then return Factory.execute_from_db(klass, first, nil, selected_fields,
|
|
30
|
-
execute_callbacks: false)
|
|
31
|
-
else raise ArgumentError, 'Cannot build belongs_to association from array'
|
|
32
|
-
end
|
|
22
|
+
if object.is_a?(PreloadedDocuments)
|
|
23
|
+
return build_preloaded(object.documents, selected_fields)
|
|
33
24
|
end
|
|
34
25
|
|
|
35
|
-
|
|
36
|
-
if object.is_a?(Hash)
|
|
37
|
-
return Factory.execute_from_db(klass, object, nil, selected_fields, execute_callbacks: false)
|
|
38
|
-
end
|
|
26
|
+
return object unless query?(object)
|
|
39
27
|
|
|
40
28
|
execute_query(object, type)
|
|
41
29
|
end
|
|
42
30
|
|
|
43
31
|
private
|
|
44
32
|
|
|
33
|
+
# Builds the document from the documents a $lookup preloaded for this
|
|
34
|
+
# association. The lookup returns an array, even for belongs_to, and
|
|
35
|
+
# may be unwound to a single document.
|
|
36
|
+
#
|
|
37
|
+
# @param [ Array<Hash> | Hash | nil ] documents The preloaded documents.
|
|
38
|
+
# @param [ Hash | nil ] selected_fields Fields which were retrieved via #only.
|
|
39
|
+
#
|
|
40
|
+
# @return [ Document | nil ] The built document.
|
|
41
|
+
def build_preloaded(documents, selected_fields)
|
|
42
|
+
document = documents.is_a?(Array) ? documents.first : documents
|
|
43
|
+
return document if document.nil? || document.is_a?(Mongoid::Document)
|
|
44
|
+
|
|
45
|
+
Factory.execute_from_db(klass, document, nil, selected_fields,
|
|
46
|
+
execute_callbacks: false)
|
|
47
|
+
end
|
|
48
|
+
|
|
45
49
|
def execute_query(object, type)
|
|
46
50
|
query_criteria(object, type).take
|
|
47
51
|
end
|
|
@@ -233,14 +233,22 @@ module Mongoid
|
|
|
233
233
|
end
|
|
234
234
|
|
|
235
235
|
def create_foreign_key_field!
|
|
236
|
-
|
|
237
|
-
foreign_key,
|
|
236
|
+
options = {
|
|
238
237
|
type: FOREIGN_KEY_FIELD_TYPE,
|
|
239
238
|
identity: true,
|
|
240
239
|
overwrite: true,
|
|
241
240
|
association: self,
|
|
242
241
|
default: nil
|
|
243
|
-
|
|
242
|
+
}
|
|
243
|
+
# The association must not replace an encrypted field with a plain
|
|
244
|
+
# foreign key: the key would then be stored in the clear. Keep the
|
|
245
|
+
# declared type as well, so the encryption schema describes it
|
|
246
|
+
# correctly.
|
|
247
|
+
if (existing = @owner_class.fields[foreign_key])&.encrypted?
|
|
248
|
+
options[:type] = existing.type
|
|
249
|
+
options[:encrypt] = existing.options[:encrypt]
|
|
250
|
+
end
|
|
251
|
+
@owner_class.field(foreign_key, options)
|
|
244
252
|
end
|
|
245
253
|
end
|
|
246
254
|
end
|
|
@@ -19,12 +19,15 @@ module Mongoid
|
|
|
19
19
|
#
|
|
20
20
|
# @return [ Array<Document> ] The documents.
|
|
21
21
|
def build(_base, object, _type = nil, selected_fields = nil)
|
|
22
|
+
if object.is_a?(PreloadedDocuments)
|
|
23
|
+
return build_preloaded(object.documents, selected_fields)
|
|
24
|
+
end
|
|
25
|
+
|
|
22
26
|
if query?(object)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
end
|
|
27
|
+
if object.is_a?(Array) && object.empty?
|
|
28
|
+
# An empty stored key array is not $lookup output. Build a none
|
|
29
|
+
# criteria so reset, empty?, and size do not query the database.
|
|
30
|
+
return query_criteria(nil)
|
|
28
31
|
end
|
|
29
32
|
|
|
30
33
|
query_criteria(object)
|
|
@@ -35,6 +38,23 @@ module Mongoid
|
|
|
35
38
|
|
|
36
39
|
private
|
|
37
40
|
|
|
41
|
+
# Builds the documents a $lookup preloaded for this association. An
|
|
42
|
+
# empty result builds a none criteria, like an empty key does, so that
|
|
43
|
+
# reset, empty?, and size do not query the database.
|
|
44
|
+
#
|
|
45
|
+
# @param [ Array<Hash> ] documents The preloaded documents.
|
|
46
|
+
# @param [ Hash | nil ] selected_fields Fields which were retrieved via #only.
|
|
47
|
+
#
|
|
48
|
+
# @return [ Array<Document> | Mongoid::Criteria ] The documents.
|
|
49
|
+
def build_preloaded(documents, selected_fields)
|
|
50
|
+
return query_criteria(nil) if documents.empty?
|
|
51
|
+
|
|
52
|
+
documents.map do |attrs|
|
|
53
|
+
Factory.execute_from_db(klass, attrs, nil, selected_fields,
|
|
54
|
+
execute_callbacks: false)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
38
58
|
def query?(object)
|
|
39
59
|
object.nil? || Array(object).all? { |d| !d.is_a?(Mongoid::Document) }
|
|
40
60
|
end
|
|
@@ -238,6 +238,18 @@ module Mongoid
|
|
|
238
238
|
end
|
|
239
239
|
|
|
240
240
|
def create_foreign_key_field!
|
|
241
|
+
# An encrypted array field cannot be used as the foreign key:
|
|
242
|
+
# encryption protects the array only as a whole, while this
|
|
243
|
+
# association queries and atomically updates its elements. Reject
|
|
244
|
+
# the association rather than replace the field and store the key in
|
|
245
|
+
# plaintext. The field may already be declared on a parent, whose
|
|
246
|
+
# declaration is propagated to the descendants.
|
|
247
|
+
([ @owner_class ] + @owner_class.descendants).each do |candidate|
|
|
248
|
+
next unless candidate.fields[foreign_key]&.encrypted?
|
|
249
|
+
|
|
250
|
+
raise Errors::InvalidEncryptedAssociation.new(candidate, name, foreign_key)
|
|
251
|
+
end
|
|
252
|
+
|
|
241
253
|
inverse_class.aliased_associations[foreign_key] = name.to_s
|
|
242
254
|
@owner_class.field(
|
|
243
255
|
foreign_key,
|
|
@@ -19,15 +19,12 @@ module Mongoid
|
|
|
19
19
|
#
|
|
20
20
|
# @return [ Document ] A single document.
|
|
21
21
|
def build(base, object, _type = nil, selected_fields = nil)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
# Handle array of hashes from $lookup aggregation
|
|
25
|
-
if object.is_a?(Array) && object.all? { |o| o.is_a?(Hash) }
|
|
26
|
-
return object.map do |attrs|
|
|
27
|
-
Factory.execute_from_db(klass, attrs, nil, selected_fields, execute_callbacks: false)
|
|
28
|
-
end
|
|
22
|
+
if object.is_a?(PreloadedDocuments)
|
|
23
|
+
return build_preloaded(object.documents, selected_fields)
|
|
29
24
|
end
|
|
30
25
|
|
|
26
|
+
return object || [] unless query?(object)
|
|
27
|
+
|
|
31
28
|
return [] if object.is_a?(Array)
|
|
32
29
|
|
|
33
30
|
query_criteria(object, base)
|
|
@@ -35,6 +32,19 @@ module Mongoid
|
|
|
35
32
|
|
|
36
33
|
private
|
|
37
34
|
|
|
35
|
+
# Builds the documents a $lookup preloaded for this association.
|
|
36
|
+
#
|
|
37
|
+
# @param [ Array<Hash> ] documents The preloaded documents.
|
|
38
|
+
# @param [ Hash | nil ] selected_fields Fields which were retrieved via #only.
|
|
39
|
+
#
|
|
40
|
+
# @return [ Array<Document> ] The built documents.
|
|
41
|
+
def build_preloaded(documents, selected_fields)
|
|
42
|
+
documents.map do |attrs|
|
|
43
|
+
Factory.execute_from_db(klass, attrs, nil, selected_fields,
|
|
44
|
+
execute_callbacks: false)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
38
48
|
def query?(object)
|
|
39
49
|
object && Array(object).all? { |d| !d.is_a?(Mongoid::Document) }
|
|
40
50
|
end
|
|
@@ -537,8 +537,10 @@ module Mongoid
|
|
|
537
537
|
#
|
|
538
538
|
# @return [ Integer ] The number of documents deleted.
|
|
539
539
|
def remove_all(conditions = nil, method = :delete_all)
|
|
540
|
-
|
|
541
|
-
|
|
540
|
+
# Conjoin the conditions with the association selector so that a
|
|
541
|
+
# key present in both is intersected rather than overwritten. This
|
|
542
|
+
# also leaves the caller's Hash alone.
|
|
543
|
+
selector = criteria.where(conditions || {}).selector
|
|
542
544
|
|
|
543
545
|
# Scanning before the delete means scanning only what is already in
|
|
544
546
|
# memory, so it is only done where there is a pattern whose cost has
|
|
@@ -21,13 +21,11 @@ module Mongoid
|
|
|
21
21
|
#
|
|
22
22
|
# @return [ Document ] A single document.
|
|
23
23
|
def build(base, object, _type = nil, selected_fields = nil)
|
|
24
|
-
if
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
doc = object.first
|
|
28
|
-
return doc ? Factory.execute_from_db(klass, doc, nil, selected_fields, execute_callbacks: false) : nil
|
|
29
|
-
end
|
|
24
|
+
if object.is_a?(PreloadedDocuments)
|
|
25
|
+
return build_preloaded(object.documents, selected_fields)
|
|
26
|
+
end
|
|
30
27
|
|
|
28
|
+
if query?(object)
|
|
31
29
|
execute_query(object, base) unless base.new_record?
|
|
32
30
|
else
|
|
33
31
|
clear_associated(object)
|
|
@@ -37,6 +35,22 @@ module Mongoid
|
|
|
37
35
|
|
|
38
36
|
private
|
|
39
37
|
|
|
38
|
+
# Builds the document from the documents a $lookup preloaded for this
|
|
39
|
+
# association. A has_one lookup returns an array with at most one
|
|
40
|
+
# document.
|
|
41
|
+
#
|
|
42
|
+
# @param [ Array<Hash> ] documents The preloaded documents.
|
|
43
|
+
# @param [ Hash | nil ] selected_fields Fields which were retrieved via #only.
|
|
44
|
+
#
|
|
45
|
+
# @return [ Document | nil ] The built document.
|
|
46
|
+
def build_preloaded(documents, selected_fields)
|
|
47
|
+
document = documents.first
|
|
48
|
+
return unless document
|
|
49
|
+
|
|
50
|
+
Factory.execute_from_db(klass, document, nil, selected_fields,
|
|
51
|
+
execute_callbacks: false)
|
|
52
|
+
end
|
|
53
|
+
|
|
40
54
|
def clear_associated(object)
|
|
41
55
|
unless inverse
|
|
42
56
|
raise Errors::InverseNotFound.new(
|
|
@@ -217,6 +217,24 @@ module Mongoid
|
|
|
217
217
|
stores_foreign_key? ? foreign_key : primary_key
|
|
218
218
|
end
|
|
219
219
|
|
|
220
|
+
# The field a $lookup writes this association's preloaded documents into.
|
|
221
|
+
#
|
|
222
|
+
# @return [ String ] The field.
|
|
223
|
+
def preloaded_field
|
|
224
|
+
"#{PRELOADED_PREFIX}#{name}"
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# A field where the aggregation collects this association's documents
|
|
228
|
+
# before moving them to where they belong. The qualifier tells it apart
|
|
229
|
+
# from the other temporary fields of the same association.
|
|
230
|
+
#
|
|
231
|
+
# @param [ String ] qualifier What tells this temporary field apart.
|
|
232
|
+
#
|
|
233
|
+
# @return [ String ] The field.
|
|
234
|
+
def temporary_field(qualifier)
|
|
235
|
+
"#{preloaded_field}_#{qualifier}"
|
|
236
|
+
end
|
|
237
|
+
|
|
220
238
|
# The name of the setter on this object for assigning an associated object.
|
|
221
239
|
#
|
|
222
240
|
# @return [ String ] The setter name.
|
data/lib/mongoid/association.rb
CHANGED
|
@@ -33,6 +33,29 @@ module Mongoid
|
|
|
33
33
|
include Macros
|
|
34
34
|
include Reflections
|
|
35
35
|
|
|
36
|
+
# Prefixes the field a $lookup writes an association's preloaded documents
|
|
37
|
+
# into. They travel apart from the document's own fields, so the hydration
|
|
38
|
+
# can keep them out of the attributes instead of leaving them on the
|
|
39
|
+
# document, where a later save would persist them.
|
|
40
|
+
#
|
|
41
|
+
# @api private
|
|
42
|
+
PRELOADED_PREFIX = '__mongoid_eager_loaded_'
|
|
43
|
+
|
|
44
|
+
# The documents a $lookup preloaded for an association. The wrapper tells
|
|
45
|
+
# the builders that these documents come from eager loading, so that a key
|
|
46
|
+
# that happens to be an array of hashes is not built into documents.
|
|
47
|
+
#
|
|
48
|
+
# @api private
|
|
49
|
+
class PreloadedDocuments
|
|
50
|
+
# @return [ Array<Hash> ] The documents the $lookup returned.
|
|
51
|
+
attr_reader :documents
|
|
52
|
+
|
|
53
|
+
# @param [ Array<Hash> ] documents The documents the $lookup returned.
|
|
54
|
+
def initialize(documents)
|
|
55
|
+
@documents = documents || []
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
36
59
|
# Map the macros to their corresponding Association classes.
|
|
37
60
|
#
|
|
38
61
|
# @return [ Hash ] The mapping from macros to their Association class.
|