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
|
@@ -209,12 +209,15 @@ module Mongoid
|
|
|
209
209
|
# that schema, and when it is absent it asks the server for a schema
|
|
210
210
|
# instead; a collection without a validator then yields no encryption at
|
|
211
211
|
# all, and no error. Checking the namespace here is what turns that silent
|
|
212
|
-
# downgrade into a failure.
|
|
212
|
+
# downgrade into a failure. When the namespace is present, its entry is
|
|
213
|
+
# checked against the fields the model declares as encrypted too: a
|
|
214
|
+
# hand-written schema can cover the namespace and still leave out a field.
|
|
213
215
|
#
|
|
214
216
|
# @param [ Mongo::Client ] client The client this context resolved to.
|
|
215
217
|
#
|
|
216
218
|
# @raise [ Errors::NoEncryptionSchema ] if the model needs an encryption
|
|
217
|
-
# schema and the client's schema does not cover the target namespace
|
|
219
|
+
# schema and the client's schema does not cover the target namespace, or
|
|
220
|
+
# the namespace entry does not cover every encrypted field of the model.
|
|
218
221
|
def verify_encryption_schema!(client)
|
|
219
222
|
klass = @object.is_a?(Class) ? @object : @object.class
|
|
220
223
|
# A model whose encrypted fields all live on embedded models declares no
|
|
@@ -229,9 +232,48 @@ module Mongoid
|
|
|
229
232
|
|
|
230
233
|
schema_map = client.options.dig(:auto_encryption_options, :schema_map)
|
|
231
234
|
namespace = "#{client.database.name}.#{collection_name}"
|
|
232
|
-
|
|
235
|
+
entry = schema_map && schema_map[namespace]
|
|
236
|
+
raise Errors::NoEncryptionSchema.new(klass, namespace, client_name) unless entry
|
|
233
237
|
|
|
234
|
-
|
|
238
|
+
# The schema map may have been written by hand, with symbol keys rather
|
|
239
|
+
# than strings; both are tolerated at every level.
|
|
240
|
+
Mongoid.config.encrypted_paths_for(klass).each do |path|
|
|
241
|
+
next if encryption_schema_has_path?(entry, path)
|
|
242
|
+
|
|
243
|
+
raise Errors::NoEncryptionSchema.new(
|
|
244
|
+
klass, namespace, client_name, path.join('.')
|
|
245
|
+
)
|
|
246
|
+
end
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
# Whether a schema entry encrypts the property at the given path.
|
|
250
|
+
#
|
|
251
|
+
# @param [ Hash ] entry The namespace schema entry.
|
|
252
|
+
# @param [ Array<String> ] path The property path, outermost first.
|
|
253
|
+
#
|
|
254
|
+
# @return [ true | false ] Whether the leaf is marked as encrypted.
|
|
255
|
+
def encryption_schema_has_path?(entry, path)
|
|
256
|
+
node = entry
|
|
257
|
+
path.each do |name|
|
|
258
|
+
properties = schema_value(node, 'properties')
|
|
259
|
+
return false unless properties.is_a?(Hash)
|
|
260
|
+
|
|
261
|
+
node = schema_value(properties, name)
|
|
262
|
+
return false unless node.is_a?(Hash)
|
|
263
|
+
end
|
|
264
|
+
!schema_value(node, 'encrypt').nil?
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
# Read a schema key that may be written as a string or a symbol.
|
|
268
|
+
#
|
|
269
|
+
# @param [ Hash ] hash The schema object.
|
|
270
|
+
# @param [ String ] key The key to read.
|
|
271
|
+
#
|
|
272
|
+
# @return [ Object ] The value, or nil when the key is absent.
|
|
273
|
+
def schema_value(hash, key)
|
|
274
|
+
return hash[key] if hash.key?(key)
|
|
275
|
+
|
|
276
|
+
hash[key.to_sym]
|
|
235
277
|
end
|
|
236
278
|
|
|
237
279
|
def client_options
|
data/lib/mongoid/reloadable.rb
CHANGED
|
@@ -421,7 +421,11 @@ module Mongoid
|
|
|
421
421
|
#
|
|
422
422
|
# @return [ Array<Hash> ] the raw index documents
|
|
423
423
|
def get_indexes(names)
|
|
424
|
-
|
|
424
|
+
# Reading index metadata sends no document data, so it is exempt from
|
|
425
|
+
# the encryption schema check, like the other index operations here.
|
|
426
|
+
Threaded.with_collection_management do
|
|
427
|
+
collection.search_indexes.select { |i| names.include?(i['name']) }
|
|
428
|
+
end
|
|
425
429
|
end
|
|
426
430
|
|
|
427
431
|
# Resolves the index name and vector path from the declared specs,
|
|
@@ -64,15 +64,18 @@ module Mongoid
|
|
|
64
64
|
# @param [ true | false ] wait whether to wait for the indexes to be
|
|
65
65
|
# built.
|
|
66
66
|
def create_search_indexes(models = ::Mongoid.models, wait: true)
|
|
67
|
-
|
|
67
|
+
Threaded.with_collection_management do
|
|
68
|
+
searchable = models.select { |m| m.search_index_specs.any? }
|
|
68
69
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
70
|
+
# queue up the search index creation requests
|
|
71
|
+
index_names_by_model = searchable.each_with_object({}) do |model, obj|
|
|
72
|
+
logger.info("MONGOID: Creating search indexes on #{model}...")
|
|
73
|
+
obj[model] = model.create_search_indexes
|
|
74
|
+
end
|
|
74
75
|
|
|
75
|
-
|
|
76
|
+
wait_for_search_indexes(index_names_by_model) if wait
|
|
77
|
+
index_names_by_model
|
|
78
|
+
end
|
|
76
79
|
end
|
|
77
80
|
|
|
78
81
|
# Return the list of indexes by model that exist in the database but aren't
|
data/lib/mongoid/touchable.rb
CHANGED
|
@@ -49,7 +49,7 @@ module Mongoid
|
|
|
49
49
|
|
|
50
50
|
begin
|
|
51
51
|
touches = _gather_touch_updates(Time.current, field)
|
|
52
|
-
_root.
|
|
52
|
+
_root.persist_atomic_operations('$set' => touches) if touches.present?
|
|
53
53
|
_run_touch_callbacks_from_root
|
|
54
54
|
ensure
|
|
55
55
|
_clear_touch_updates(field)
|
|
@@ -104,6 +104,33 @@ module Mongoid
|
|
|
104
104
|
run_callbacks(:touch)
|
|
105
105
|
end
|
|
106
106
|
|
|
107
|
+
# The root-relative paths that a touch of this document's parent chain
|
|
108
|
+
# writes: the (possibly aliased) +updated_at+ field of each touchable
|
|
109
|
+
# node in the chain, plus this document's custom touch field, if any.
|
|
110
|
+
#
|
|
111
|
+
# @api private
|
|
112
|
+
#
|
|
113
|
+
# @param [ Symbol, String, nil ] field The custom touch field.
|
|
114
|
+
#
|
|
115
|
+
# @return [ Array<String> ] The touchable paths.
|
|
116
|
+
def _touchable_chain_paths(field = nil)
|
|
117
|
+
field = database_field_name(field) if field
|
|
118
|
+
|
|
119
|
+
node = self
|
|
120
|
+
paths = []
|
|
121
|
+
loop do
|
|
122
|
+
if node.respond_to?(:updated_at=)
|
|
123
|
+
paths << node.atomic_attribute_name(node.database_field_name(:updated_at))
|
|
124
|
+
end
|
|
125
|
+
paths << node.atomic_attribute_name(field) if node.equal?(self) && field
|
|
126
|
+
|
|
127
|
+
break unless node._touchable_parent?
|
|
128
|
+
|
|
129
|
+
node = node._parent
|
|
130
|
+
end
|
|
131
|
+
paths
|
|
132
|
+
end
|
|
133
|
+
|
|
107
134
|
# Indicates whether the parent exists and is touchable.
|
|
108
135
|
#
|
|
109
136
|
# @api private
|
data/lib/mongoid/version.rb
CHANGED
data/lib/mongoid/warnings.rb
CHANGED
|
@@ -36,5 +36,9 @@ module Mongoid
|
|
|
36
36
|
warning :autosave_saves_unchanged_documents, "Autosave associations are currently configured to save documents even if they haven't changed. " \
|
|
37
37
|
'This legacy behavior is deprecated. Set Mongoid.autosave_saves_unchanged_documents to false to ' \
|
|
38
38
|
'skip saving unchanged documents in autosave associations.'
|
|
39
|
+
warning :bypass_auto_encryption,
|
|
40
|
+
'auto_encryption_options includes bypass_auto_encryption: true. The driver ' \
|
|
41
|
+
'will not encrypt anything for this client: fields declared with encrypt: true ' \
|
|
42
|
+
'are written to the database unencrypted.'
|
|
39
43
|
end
|
|
40
44
|
end
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module EmbeddedDelayedAtomicSetsSpec
|
|
6
|
+
class Section
|
|
7
|
+
include Mongoid::Document
|
|
8
|
+
|
|
9
|
+
field :content
|
|
10
|
+
|
|
11
|
+
embedded_in :parent, polymorphic: true
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class Group
|
|
15
|
+
include Mongoid::Document
|
|
16
|
+
|
|
17
|
+
embedded_in :root
|
|
18
|
+
embeds_many :sections, class_name: 'EmbeddedDelayedAtomicSetsSpec::Section'
|
|
19
|
+
accepts_nested_attributes_for :sections
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class Root
|
|
23
|
+
include Mongoid::Document
|
|
24
|
+
|
|
25
|
+
embeds_many :sections, class_name: 'EmbeddedDelayedAtomicSetsSpec::Section'
|
|
26
|
+
embeds_one :group, class_name: 'EmbeddedDelayedAtomicSetsSpec::Group'
|
|
27
|
+
accepts_nested_attributes_for :sections
|
|
28
|
+
accepts_nested_attributes_for :group
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
module NewEmbeddedChildSpec
|
|
33
|
+
class Member
|
|
34
|
+
include Mongoid::Document
|
|
35
|
+
|
|
36
|
+
field :name
|
|
37
|
+
|
|
38
|
+
embedded_in :team
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
class Team
|
|
42
|
+
include Mongoid::Document
|
|
43
|
+
|
|
44
|
+
embedded_in :org
|
|
45
|
+
embeds_many :members, class_name: 'NewEmbeddedChildSpec::Member'
|
|
46
|
+
accepts_nested_attributes_for :members
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
class Org
|
|
50
|
+
include Mongoid::Document
|
|
51
|
+
|
|
52
|
+
field :title
|
|
53
|
+
|
|
54
|
+
embeds_one :team, class_name: 'NewEmbeddedChildSpec::Team'
|
|
55
|
+
accepts_nested_attributes_for :team
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
RSpec.describe 'embedded delayed atomic sets' do
|
|
60
|
+
context 'with the same stored name' do
|
|
61
|
+
let!(:root) do
|
|
62
|
+
root = EmbeddedDelayedAtomicSetsSpec::Root.new
|
|
63
|
+
root.sections = [ EmbeddedDelayedAtomicSetsSpec::Section.new(content: 'root-original') ]
|
|
64
|
+
root.save!
|
|
65
|
+
root
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def stored_document
|
|
69
|
+
EmbeddedDelayedAtomicSetsSpec::Root.collection.find(_id: root.id).first
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def stored_contents(sections)
|
|
73
|
+
sections.to_a.map { |section| section['content'] }
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def document_contents(sections)
|
|
77
|
+
sections.map(&:content)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# The persisted root has no group, so attributes= builds the new nested group
|
|
81
|
+
# while the root sections update is still pending.
|
|
82
|
+
it 'does not merge a new child into the parent association' do
|
|
83
|
+
reloaded = EmbeddedDelayedAtomicSetsSpec::Root.find(root.id)
|
|
84
|
+
reloaded.attributes = {
|
|
85
|
+
sections: [ { _id: reloaded.sections.first.id, content: 'root-updated' } ],
|
|
86
|
+
group: { sections: [ { content: 'group-new' } ] }
|
|
87
|
+
}
|
|
88
|
+
reloaded.save!
|
|
89
|
+
|
|
90
|
+
stored = stored_document
|
|
91
|
+
expect(stored_contents(stored['sections'])).to eq [ 'root-updated' ]
|
|
92
|
+
expect(stored_contents(stored.dig('group', 'sections'))).to eq [ 'group-new' ]
|
|
93
|
+
expect(document_contents(reloaded.sections)).to eq [ 'root-updated' ]
|
|
94
|
+
expect(document_contents(reloaded.group.sections)).to eq [ 'group-new' ]
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it 'does not copy a new child into an empty parent association' do
|
|
98
|
+
reloaded = EmbeddedDelayedAtomicSetsSpec::Root.find(root.id)
|
|
99
|
+
reloaded.attributes = {
|
|
100
|
+
sections: [],
|
|
101
|
+
group: { sections: [ { content: 'group-new' } ] }
|
|
102
|
+
}
|
|
103
|
+
reloaded.save!
|
|
104
|
+
|
|
105
|
+
stored = stored_document
|
|
106
|
+
expect(stored).not_to have_key('sections')
|
|
107
|
+
expect(stored_contents(stored.dig('group', 'sections'))).to eq [ 'group-new' ]
|
|
108
|
+
expect(reloaded.sections).to be_empty
|
|
109
|
+
expect(document_contents(reloaded.group.sections)).to eq [ 'group-new' ]
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
it 'does not merge a new child when the parent association is unchanged' do
|
|
113
|
+
reloaded = EmbeddedDelayedAtomicSetsSpec::Root.find(root.id)
|
|
114
|
+
reloaded.attributes = { group: { sections: [ { content: 'group-new' } ] } }
|
|
115
|
+
reloaded.save!
|
|
116
|
+
|
|
117
|
+
stored = stored_document
|
|
118
|
+
expect(stored_contents(stored['sections'])).to eq [ 'root-original' ]
|
|
119
|
+
expect(stored_contents(stored.dig('group', 'sections'))).to eq [ 'group-new' ]
|
|
120
|
+
expect(document_contents(reloaded.sections)).to eq [ 'root-original' ]
|
|
121
|
+
expect(document_contents(reloaded.group.sections)).to eq [ 'group-new' ]
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
it 'updates an existing group and isolates a subsequently added group' do
|
|
125
|
+
root.group = EmbeddedDelayedAtomicSetsSpec::Group.new(
|
|
126
|
+
sections: [ EmbeddedDelayedAtomicSetsSpec::Section.new(content: 'group-original') ]
|
|
127
|
+
)
|
|
128
|
+
root.save!
|
|
129
|
+
|
|
130
|
+
reloaded = EmbeddedDelayedAtomicSetsSpec::Root.find(root.id)
|
|
131
|
+
reloaded.attributes = {
|
|
132
|
+
group: {
|
|
133
|
+
_id: reloaded.group.id,
|
|
134
|
+
sections: [ { _id: reloaded.group.sections.first.id, content: 'group-updated' } ]
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
reloaded.save!
|
|
138
|
+
|
|
139
|
+
stored = stored_document
|
|
140
|
+
expect(stored_contents(stored['sections'])).to eq [ 'root-original' ]
|
|
141
|
+
expect(stored_contents(stored.dig('group', 'sections'))).to eq [ 'group-updated' ]
|
|
142
|
+
expect(document_contents(reloaded.sections)).to eq [ 'root-original' ]
|
|
143
|
+
expect(document_contents(reloaded.group.sections)).to eq [ 'group-updated' ]
|
|
144
|
+
|
|
145
|
+
reloaded.group = nil
|
|
146
|
+
reloaded.save!
|
|
147
|
+
reloaded = EmbeddedDelayedAtomicSetsSpec::Root.find(root.id)
|
|
148
|
+
reloaded.attributes = {
|
|
149
|
+
sections: [ { _id: reloaded.sections.first.id, content: 'root-updated' } ],
|
|
150
|
+
group: { sections: [ { content: 'group-new' } ] }
|
|
151
|
+
}
|
|
152
|
+
reloaded.save!
|
|
153
|
+
|
|
154
|
+
stored = stored_document
|
|
155
|
+
expect(stored_contents(stored['sections'])).to eq [ 'root-updated' ]
|
|
156
|
+
expect(stored_contents(stored.dig('group', 'sections'))).to eq [ 'group-new' ]
|
|
157
|
+
expect(document_contents(reloaded.sections)).to eq [ 'root-updated' ]
|
|
158
|
+
expect(document_contents(reloaded.group.sections)).to eq [ 'group-new' ]
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
it 'does not merge a new child when updating the parent through nested attributes' do
|
|
162
|
+
reloaded = EmbeddedDelayedAtomicSetsSpec::Root.find(root.id)
|
|
163
|
+
reloaded.attributes = {
|
|
164
|
+
sections_attributes: {
|
|
165
|
+
'0' => { _id: reloaded.sections.first.id, content: 'root-updated' }
|
|
166
|
+
},
|
|
167
|
+
group: { sections: [ { content: 'group-new' } ] }
|
|
168
|
+
}
|
|
169
|
+
reloaded.save!
|
|
170
|
+
|
|
171
|
+
stored = stored_document
|
|
172
|
+
expect(stored_contents(stored['sections'])).to eq [ 'root-updated' ]
|
|
173
|
+
expect(stored_contents(stored.dig('group', 'sections'))).to eq [ 'group-new' ]
|
|
174
|
+
expect(document_contents(reloaded.sections)).to eq [ 'root-updated' ]
|
|
175
|
+
expect(document_contents(reloaded.group.sections)).to eq [ 'group-new' ]
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
context 'when assigning a new embeds_one child with nested embeds_many' do
|
|
180
|
+
let!(:org) { NewEmbeddedChildSpec::Org.create!(title: 't0') }
|
|
181
|
+
|
|
182
|
+
it 'does not write the nested array as a root-level field' do
|
|
183
|
+
reloaded = NewEmbeddedChildSpec::Org.find(org.id)
|
|
184
|
+
reloaded.attributes = { title: 't1', team: { members: [ { name: 'm1' } ] } }
|
|
185
|
+
reloaded.save!
|
|
186
|
+
|
|
187
|
+
stored = NewEmbeddedChildSpec::Org.collection.find(_id: org.id).first
|
|
188
|
+
expect(stored.keys).to match_array(%w[_id title team])
|
|
189
|
+
expect(stored.dig('team', 'members').map { |member| member['name'] }).to eq [ 'm1' ]
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
it 'does not write nested attributes at the root level' do
|
|
193
|
+
reloaded = NewEmbeddedChildSpec::Org.find(org.id)
|
|
194
|
+
reloaded.attributes = {
|
|
195
|
+
team_attributes: { members: [ { name: 'm1' } ] }
|
|
196
|
+
}
|
|
197
|
+
reloaded.save!
|
|
198
|
+
|
|
199
|
+
stored = NewEmbeddedChildSpec::Org.collection.find(_id: org.id).first
|
|
200
|
+
expect(stored.keys).to match_array(%w[_id title team])
|
|
201
|
+
expect(stored.dig('team', 'members').map { |member| member['name'] }).to eq [ 'm1' ]
|
|
202
|
+
expect(reloaded.team.members.map(&:name)).to eq [ 'm1' ]
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
it 'does not conflict when assigning to a newly built child' do
|
|
206
|
+
reloaded = NewEmbeddedChildSpec::Org.find(org.id)
|
|
207
|
+
reloaded.build_team
|
|
208
|
+
reloaded.team.attributes = { members: [ { name: 'm1' } ] }
|
|
209
|
+
reloaded.save!
|
|
210
|
+
|
|
211
|
+
stored = NewEmbeddedChildSpec::Org.collection.find(_id: org.id).first
|
|
212
|
+
expect(stored.dig('team', 'members').map { |member| member['name'] }).to eq [ 'm1' ]
|
|
213
|
+
expect(reloaded.team.members.map(&:name)).to eq [ 'm1' ]
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
end
|
|
@@ -60,6 +60,20 @@ module HabtmSpec
|
|
|
60
60
|
end
|
|
61
61
|
|
|
62
62
|
describe 'has_and_belongs_to_many associations' do
|
|
63
|
+
context 'when the stored foreign-key array is empty' do
|
|
64
|
+
it 'can reset and enumerate the association proxy' do
|
|
65
|
+
item = HabtmSpec::Item.create!(color_ids: [])
|
|
66
|
+
colors = item.reload.colors
|
|
67
|
+
|
|
68
|
+
expect_query(0) do
|
|
69
|
+
colors.reset
|
|
70
|
+
expect(colors.empty?).to be true
|
|
71
|
+
expect(colors.size).to be 0
|
|
72
|
+
expect(colors.to_a).to eq []
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
63
77
|
context 'when an anonymous class defines a has_and_belongs_to_many association' do
|
|
64
78
|
let(:klass) do
|
|
65
79
|
Class.new do
|