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
|
@@ -4,6 +4,33 @@ require "spec_helper"
|
|
|
4
4
|
|
|
5
5
|
describe Mongoid::Attributes::Nested do
|
|
6
6
|
|
|
7
|
+
# Expects the enclosing context to define `build_with_id`, taking an id
|
|
8
|
+
# value and returning the document built with it in nested attributes.
|
|
9
|
+
shared_examples 'rejects non-scalar ids' do |klass_name|
|
|
10
|
+
not_found = /not found for class #{klass_name}/
|
|
11
|
+
|
|
12
|
+
context 'when the id is an operator hash' do
|
|
13
|
+
it 'raises a document not found error' do
|
|
14
|
+
expect { build_with_id({ '$ne' => nil }) }
|
|
15
|
+
.to raise_error(Mongoid::Errors::DocumentNotFound, not_found)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
context 'when the id is an array' do
|
|
20
|
+
it 'raises a document not found error' do
|
|
21
|
+
expect { build_with_id([BSON::ObjectId.new]) }
|
|
22
|
+
.to raise_error(Mongoid::Errors::DocumentNotFound, not_found)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
context 'when the id is a malformed object id hash' do
|
|
27
|
+
it 'raises a document not found error' do
|
|
28
|
+
expect { build_with_id({ '$oid' => 'junk' }) }
|
|
29
|
+
.to raise_error(Mongoid::Errors::DocumentNotFound, not_found)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
7
34
|
describe ".accepts_nested_attributes_for" do
|
|
8
35
|
|
|
9
36
|
context "when the autosave option is not defined" do
|
|
@@ -175,6 +202,30 @@ describe Mongoid::Attributes::Nested do
|
|
|
175
202
|
end
|
|
176
203
|
end
|
|
177
204
|
|
|
205
|
+
context 'when the id in the attributes is not a scalar' do
|
|
206
|
+
let(:person) { Person.create! }
|
|
207
|
+
|
|
208
|
+
def build_with_id(id)
|
|
209
|
+
person.update!(posts_attributes: { '0' => { id: id, title: 'Crafted' } })
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
context 'when the association is empty' do
|
|
213
|
+
include_examples 'rejects non-scalar ids', 'Post'
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
context 'when the association is not empty' do
|
|
217
|
+
before { person.posts.create!(title: 'Existing') }
|
|
218
|
+
|
|
219
|
+
include_examples 'rejects non-scalar ids', 'Post'
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
context 'when allow_reparenting_via_nested_attributes is true' do
|
|
223
|
+
config_override :allow_reparenting_via_nested_attributes, true
|
|
224
|
+
|
|
225
|
+
include_examples 'rejects non-scalar ids', 'Post'
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
|
|
178
229
|
context "when the relation is a references and referenced in many" do
|
|
179
230
|
|
|
180
231
|
before do
|
|
@@ -198,16 +249,100 @@ describe Mongoid::Attributes::Nested do
|
|
|
198
249
|
)
|
|
199
250
|
end
|
|
200
251
|
|
|
201
|
-
|
|
202
|
-
|
|
252
|
+
context 'when allow_reparenting_via_nested_attributes is false' do
|
|
253
|
+
config_override :allow_reparenting_via_nested_attributes, false
|
|
254
|
+
|
|
255
|
+
it 'raises a document not found error' do
|
|
256
|
+
expect { person }.to raise_error(Mongoid::Errors::DocumentNotFound,
|
|
257
|
+
/Document\(s\) not found for class Preference/)
|
|
258
|
+
end
|
|
203
259
|
end
|
|
204
260
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
261
|
+
context 'when allow_reparenting_via_nested_attributes is true' do
|
|
262
|
+
config_override :allow_reparenting_via_nested_attributes, true
|
|
263
|
+
|
|
264
|
+
it 'sets the nested attributes' do
|
|
265
|
+
expect(person.preferences.map(&:name)).to eq([preference.name])
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
it 'updates attributes of existing document which is added to relation' do
|
|
269
|
+
preference_name = 'updated preference'
|
|
270
|
+
person = Person.new(
|
|
271
|
+
preferences_attributes: { 0 => { id: preference.id, name: preference_name } }
|
|
272
|
+
)
|
|
273
|
+
expect(person.preferences.map(&:name)).to eq([preference_name])
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
context 'when the id does not correspond to an existing document' do
|
|
277
|
+
let(:person) do
|
|
278
|
+
Person.new(
|
|
279
|
+
preferences_attributes: { 0 => { id: BSON::ObjectId.new, name: 'Ghost' } }
|
|
280
|
+
)
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
context 'when raise_not_found_error is true' do
|
|
284
|
+
config_override :raise_not_found_error, true
|
|
285
|
+
|
|
286
|
+
it 'raises a document not found error' do
|
|
287
|
+
expect { person }.to raise_error(Mongoid::Errors::DocumentNotFound,
|
|
288
|
+
/Document\(s\) not found for class Preference/)
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
context 'when raise_not_found_error is false' do
|
|
293
|
+
config_override :raise_not_found_error, false
|
|
294
|
+
|
|
295
|
+
it 'raises a document not found error' do
|
|
296
|
+
expect { person }.to raise_error(Mongoid::Errors::DocumentNotFound,
|
|
297
|
+
/Document\(s\) not found for class Preference/)
|
|
298
|
+
end
|
|
299
|
+
end
|
|
300
|
+
end
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
context 'when _destroy is true for a document not in the relation' do
|
|
304
|
+
before do
|
|
305
|
+
Person.send(:undef_method, :preferences_attributes=)
|
|
306
|
+
Person.accepts_nested_attributes_for :preferences, allow_destroy: true
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
let(:person) do
|
|
310
|
+
Person.new(
|
|
311
|
+
preferences_attributes: { 0 => { id: preference.id, _destroy: '1' } }
|
|
312
|
+
)
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
it 'does not raise UnknownAttribute' do
|
|
316
|
+
expect { person }.not_to raise_error
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
it 'does not add the document to the relation' do
|
|
320
|
+
expect(person.preferences).to be_empty
|
|
321
|
+
end
|
|
322
|
+
end
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
context 'when the id in the attributes is not a scalar' do
|
|
326
|
+
let(:target) { Person.create! }
|
|
327
|
+
|
|
328
|
+
def build_with_id(id)
|
|
329
|
+
target.update!(preferences_attributes: { 0 => { id: id, name: 'Crafted' } })
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
context 'when the association is empty' do
|
|
333
|
+
include_examples 'rejects non-scalar ids', 'Preference'
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
context 'when the association is not empty' do
|
|
337
|
+
before { target.preferences.create!(name: 'Existing') }
|
|
338
|
+
|
|
339
|
+
include_examples 'rejects non-scalar ids', 'Preference'
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
context 'when allow_reparenting_via_nested_attributes is true' do
|
|
343
|
+
config_override :allow_reparenting_via_nested_attributes, true
|
|
344
|
+
|
|
345
|
+
include_examples 'rejects non-scalar ids', 'Preference'
|
|
211
346
|
end
|
|
212
347
|
end
|
|
213
348
|
end
|
|
@@ -4928,4 +5063,63 @@ describe Mongoid::Attributes::Nested do
|
|
|
4928
5063
|
end
|
|
4929
5064
|
end
|
|
4930
5065
|
end
|
|
5066
|
+
|
|
5067
|
+
context 'when an id in nested attributes belongs to another parent' do
|
|
5068
|
+
let(:victim) { Person.create! }
|
|
5069
|
+
let(:attacker) { Person.create! }
|
|
5070
|
+
|
|
5071
|
+
context 'when the association is a has-many' do
|
|
5072
|
+
before do
|
|
5073
|
+
Person.send(:undef_method, :posts_attributes=)
|
|
5074
|
+
Person.accepts_nested_attributes_for :posts
|
|
5075
|
+
end
|
|
5076
|
+
|
|
5077
|
+
let!(:victim_post) { victim.posts.create!(title: 'Private') }
|
|
5078
|
+
|
|
5079
|
+
it 'does not update the other parent\'s document' do
|
|
5080
|
+
expect { attacker.update!(posts_attributes: { '0' => { id: victim_post.id, title: 'Overwritten' } }) }
|
|
5081
|
+
.to raise_error(Mongoid::Errors::DocumentNotFound)
|
|
5082
|
+
expect(victim_post.reload.title).to eq('Private')
|
|
5083
|
+
end
|
|
5084
|
+
|
|
5085
|
+
it 'does not add the other parent\'s document to the association' do
|
|
5086
|
+
expect { attacker.update!(posts_attributes: { '0' => { id: victim_post.id, title: 'Overwritten' } }) }
|
|
5087
|
+
.to raise_error(Mongoid::Errors::DocumentNotFound)
|
|
5088
|
+
expect(attacker.reload.posts).to be_empty
|
|
5089
|
+
end
|
|
5090
|
+
|
|
5091
|
+
it 'does not update via a direct association assignment' do
|
|
5092
|
+
expect { attacker.update!(posts: { '0' => { id: victim_post.id, title: 'Overwritten' } }) }
|
|
5093
|
+
.to raise_error(Mongoid::Errors::DocumentNotFound)
|
|
5094
|
+
expect(victim_post.reload.title).to eq('Private')
|
|
5095
|
+
end
|
|
5096
|
+
end
|
|
5097
|
+
|
|
5098
|
+
context 'when the association is a has-and-belongs-to-many' do
|
|
5099
|
+
before do
|
|
5100
|
+
Person.send(:undef_method, :preferences_attributes=)
|
|
5101
|
+
Person.accepts_nested_attributes_for :preferences
|
|
5102
|
+
end
|
|
5103
|
+
|
|
5104
|
+
let!(:victim_preference) { victim.preferences.create!(name: 'Private') }
|
|
5105
|
+
|
|
5106
|
+
it 'does not update the other parent\'s document' do
|
|
5107
|
+
expect { attacker.update!(preferences_attributes: { '0' => { id: victim_preference.id, name: 'Overwritten' } }) }
|
|
5108
|
+
.to raise_error(Mongoid::Errors::DocumentNotFound)
|
|
5109
|
+
expect(victim_preference.reload.name).to eq('Private')
|
|
5110
|
+
end
|
|
5111
|
+
|
|
5112
|
+
it 'does not add the other parent\'s document to the association' do
|
|
5113
|
+
expect { attacker.update!(preferences_attributes: { '0' => { id: victim_preference.id, name: 'Overwritten' } }) }
|
|
5114
|
+
.to raise_error(Mongoid::Errors::DocumentNotFound)
|
|
5115
|
+
expect(attacker.reload.preferences).to be_empty
|
|
5116
|
+
end
|
|
5117
|
+
|
|
5118
|
+
it 'does not update via a direct association assignment' do
|
|
5119
|
+
expect { attacker.update!(preferences: { '0' => { id: victim_preference.id, name: 'Overwritten' } }) }
|
|
5120
|
+
.to raise_error(Mongoid::Errors::DocumentNotFound)
|
|
5121
|
+
expect(victim_preference.reload.name).to eq('Private')
|
|
5122
|
+
end
|
|
5123
|
+
end
|
|
5124
|
+
end
|
|
4931
5125
|
end
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
describe 'Readonly dotted attributes' do
|
|
6
|
+
let(:model_class) do
|
|
7
|
+
test_model do
|
|
8
|
+
field :stored_settings, as: :settings, type: Hash
|
|
9
|
+
field :other_settings, type: Hash
|
|
10
|
+
attr_readonly :settings
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
let(:document) do
|
|
15
|
+
model_class.create!(settings: { 'is_admin' => false }, other_settings: { 'enabled' => false })
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def expect_stored_value(field, value)
|
|
19
|
+
expect(model_class.collection.find(_id: document.id).first[field]).to eq(value)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
shared_examples 'a readonly dotted attribute' do |path, field, original_value|
|
|
23
|
+
it 'ignores write_attribute' do
|
|
24
|
+
document.write_attribute(path, true)
|
|
25
|
+
document.save!
|
|
26
|
+
|
|
27
|
+
expect_stored_value(field, original_value)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it 'ignores the bracket setter' do
|
|
31
|
+
document[path.to_sym] = true
|
|
32
|
+
document.save!
|
|
33
|
+
|
|
34
|
+
expect_stored_value(field, original_value)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it 'raises ReadonlyAttribute from update_attribute' do
|
|
38
|
+
expect do
|
|
39
|
+
document.update_attribute(path, true)
|
|
40
|
+
end.to raise_error(Mongoid::Errors::ReadonlyAttribute)
|
|
41
|
+
|
|
42
|
+
expect_stored_value(field, original_value)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
context 'without dynamic attributes' do
|
|
47
|
+
context 'with the database field name' do
|
|
48
|
+
include_examples 'a readonly dotted attribute',
|
|
49
|
+
'stored_settings.is_admin', 'stored_settings', { 'is_admin' => false }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
context 'with the field alias' do
|
|
53
|
+
include_examples 'a readonly dotted attribute',
|
|
54
|
+
'settings.is_admin', 'stored_settings', { 'is_admin' => false }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
context 'with a new key in the readonly field' do
|
|
58
|
+
include_examples 'a readonly dotted attribute',
|
|
59
|
+
'settings.new_key', 'stored_settings', { 'is_admin' => false }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
context 'with a deeply nested key' do
|
|
63
|
+
let(:document) do
|
|
64
|
+
model_class.create!(settings: { 'permissions' => { 'is_admin' => false } })
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
include_examples 'a readonly dotted attribute',
|
|
68
|
+
'settings.permissions.is_admin', 'stored_settings', { 'permissions' => { 'is_admin' => false } }
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
it 'allows the readonly field to be initialized on a new document' do
|
|
72
|
+
expect_stored_value('stored_settings', 'is_admin' => false)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
it 'allows dotted writes to an unprotected field' do
|
|
76
|
+
document.write_attribute('other_settings.enabled', true)
|
|
77
|
+
document.save!
|
|
78
|
+
|
|
79
|
+
expect_stored_value('other_settings', 'enabled' => true)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
it 'continues to protect an exact dotted readonly name' do
|
|
83
|
+
model_class.attr_readonly('other_settings.enabled')
|
|
84
|
+
document.write_attribute('other_settings.enabled', true)
|
|
85
|
+
document.save!
|
|
86
|
+
|
|
87
|
+
expect_stored_value('other_settings', 'enabled' => false)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
it 'rejects an unknown dotted field during mass assignment' do
|
|
91
|
+
expect do
|
|
92
|
+
document.update('stored_settings.is_admin' => true)
|
|
93
|
+
end.to raise_error(Mongoid::Errors::UnknownAttribute)
|
|
94
|
+
|
|
95
|
+
expect_stored_value('stored_settings', 'is_admin' => false)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
context 'with dynamic attributes' do
|
|
100
|
+
let(:model_class) do
|
|
101
|
+
test_model do
|
|
102
|
+
include Mongoid::Attributes::Dynamic
|
|
103
|
+
|
|
104
|
+
field :stored_settings, as: :settings, type: Hash
|
|
105
|
+
field :other_settings, type: Hash
|
|
106
|
+
attr_readonly :settings
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
context 'with the database field name' do
|
|
111
|
+
include_examples 'a readonly dotted attribute',
|
|
112
|
+
'stored_settings.is_admin', 'stored_settings', { 'is_admin' => false }
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
context 'with the field alias' do
|
|
116
|
+
include_examples 'a readonly dotted attribute',
|
|
117
|
+
'settings.is_admin', 'stored_settings', { 'is_admin' => false }
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
context 'with a new key in the readonly field' do
|
|
121
|
+
include_examples 'a readonly dotted attribute',
|
|
122
|
+
'settings.new_key', 'stored_settings', { 'is_admin' => false }
|
|
123
|
+
|
|
124
|
+
it 'ignores mass assignment' do
|
|
125
|
+
document.update('settings.new_key' => true)
|
|
126
|
+
|
|
127
|
+
expect_stored_value('stored_settings', 'is_admin' => false)
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
context 'with a deeply nested key' do
|
|
132
|
+
let(:document) do
|
|
133
|
+
model_class.create!(settings: { 'permissions' => { 'is_admin' => false } })
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
include_examples 'a readonly dotted attribute',
|
|
137
|
+
'settings.permissions.is_admin', 'stored_settings', { 'permissions' => { 'is_admin' => false } }
|
|
138
|
+
|
|
139
|
+
it 'ignores mass assignment' do
|
|
140
|
+
document.update('settings.permissions.is_admin' => true)
|
|
141
|
+
|
|
142
|
+
expect_stored_value('stored_settings', 'permissions' => { 'is_admin' => false })
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
it 'ignores mass assignment using the database field name' do
|
|
147
|
+
document.update('stored_settings.is_admin' => true)
|
|
148
|
+
|
|
149
|
+
expect_stored_value('stored_settings', 'is_admin' => false)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
it 'ignores mass assignment using the field alias' do
|
|
153
|
+
document.update('settings.is_admin' => true)
|
|
154
|
+
|
|
155
|
+
expect_stored_value('stored_settings', 'is_admin' => false)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
it 'does not persist the dotted field through update_attribute' do
|
|
159
|
+
begin
|
|
160
|
+
document.update_attribute('settings.is_admin', true)
|
|
161
|
+
rescue Mongoid::Errors::ReadonlyAttribute
|
|
162
|
+
# update_attribute is expected to raise for readonly attributes.
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
expect_stored_value('stored_settings', 'is_admin' => false)
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
context 'when a nested path is readonly' do
|
|
170
|
+
let(:model_class) do
|
|
171
|
+
test_model do
|
|
172
|
+
field :stored_settings, as: :settings, type: Hash
|
|
173
|
+
attr_readonly 'settings.permissions.protected'
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
let(:document) do
|
|
178
|
+
model_class.create!(settings: { 'permissions' => { 'protected' => { 'is_admin' => false } } })
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
include_examples 'a readonly dotted attribute',
|
|
182
|
+
'settings.permissions.protected.is_admin', 'stored_settings',
|
|
183
|
+
{ 'permissions' => { 'protected' => { 'is_admin' => false } } }
|
|
184
|
+
|
|
185
|
+
include_examples 'a readonly dotted attribute',
|
|
186
|
+
'stored_settings.permissions.protected.is_admin', 'stored_settings',
|
|
187
|
+
{ 'permissions' => { 'protected' => { 'is_admin' => false } } }
|
|
188
|
+
|
|
189
|
+
it 'allows the bracket setter for the top-level parent' do
|
|
190
|
+
document[:settings] = { 'permissions' => { 'protected' => { 'is_admin' => true } } }
|
|
191
|
+
document.save!
|
|
192
|
+
|
|
193
|
+
expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
it 'allows mass assignment to the top-level parent' do
|
|
197
|
+
document.update(settings: { 'permissions' => { 'protected' => { 'is_admin' => true } } })
|
|
198
|
+
|
|
199
|
+
expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
it 'allows update_attribute on the top-level parent' do
|
|
203
|
+
document.update_attribute('settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
|
|
204
|
+
|
|
205
|
+
expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
it 'allows removing the top-level parent' do
|
|
209
|
+
document.remove_attribute('settings')
|
|
210
|
+
document.save!
|
|
211
|
+
|
|
212
|
+
expect_stored_value('stored_settings', nil)
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
it 'allows an in-place change to the parent hash' do
|
|
216
|
+
document.settings['permissions']['protected']['is_admin'] = true
|
|
217
|
+
document.save!
|
|
218
|
+
|
|
219
|
+
expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
it 'allows replacing a parent inside the hash' do
|
|
223
|
+
document.settings['permissions'] = { 'protected' => { 'is_admin' => true } }
|
|
224
|
+
document.save!
|
|
225
|
+
|
|
226
|
+
expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
context 'with dynamic attributes' do
|
|
230
|
+
let(:model_class) do
|
|
231
|
+
test_model do
|
|
232
|
+
include Mongoid::Attributes::Dynamic
|
|
233
|
+
|
|
234
|
+
field :stored_settings, as: :settings, type: Hash
|
|
235
|
+
attr_readonly 'settings.permissions.protected'
|
|
236
|
+
end
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
include_examples 'a readonly dotted attribute',
|
|
240
|
+
'settings.permissions.protected.is_admin', 'stored_settings',
|
|
241
|
+
{ 'permissions' => { 'protected' => { 'is_admin' => false } } }
|
|
242
|
+
|
|
243
|
+
it 'ignores mass assignment to a descendant' do
|
|
244
|
+
document.update('settings.permissions.protected.is_admin' => true)
|
|
245
|
+
|
|
246
|
+
expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => false } })
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
context 'with a separate field whose name contains a dot' do
|
|
252
|
+
let(:document) { model_class.create!(settings: { 'theme' => 'light' }) }
|
|
253
|
+
|
|
254
|
+
context 'when the dotted field is readonly' do
|
|
255
|
+
let(:model_class) do
|
|
256
|
+
test_model do
|
|
257
|
+
field :settings, type: Hash
|
|
258
|
+
field :'settings.is_admin', type: String, default: 'literal field'
|
|
259
|
+
attr_readonly :'settings.is_admin'
|
|
260
|
+
end
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
it 'allows updating the separate field' do
|
|
264
|
+
document.settings = { 'theme' => 'dark' }
|
|
265
|
+
document.save!
|
|
266
|
+
|
|
267
|
+
expect_stored_value('settings', 'theme' => 'dark')
|
|
268
|
+
expect_stored_value('settings.is_admin', 'literal field')
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
it 'allows updating a nested hash with the same name as the dotted field' do
|
|
272
|
+
document.write_attribute('settings.is_admin.enabled', true)
|
|
273
|
+
document.save!
|
|
274
|
+
|
|
275
|
+
expect_stored_value('settings', 'theme' => 'light', 'is_admin' => { 'enabled' => true })
|
|
276
|
+
expect_stored_value('settings.is_admin', 'literal field')
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
it 'still treats the dotted field as readonly' do
|
|
280
|
+
expect do
|
|
281
|
+
document.update_attribute('settings.is_admin', 'new value')
|
|
282
|
+
end.to raise_error(Mongoid::Errors::ReadonlyAttribute)
|
|
283
|
+
end
|
|
284
|
+
end
|
|
285
|
+
end
|
|
286
|
+
end
|