mongoid 8.1.12 → 8.1.14

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.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/lib/config/locales/en.yml +26 -0
  3. data/lib/mongoid/association/depending.rb +8 -4
  4. data/lib/mongoid/association/nested/many.rb +38 -3
  5. data/lib/mongoid/association/nested/nested_buildable.rb +14 -0
  6. data/lib/mongoid/association/referenced/has_many/proxy.rb +103 -2
  7. data/lib/mongoid/attributes/readonly.rb +19 -1
  8. data/lib/mongoid/changeable.rb +22 -4
  9. data/lib/mongoid/config.rb +62 -0
  10. data/lib/mongoid/contextual/aggregable/memory.rb +5 -2
  11. data/lib/mongoid/contextual/memory.rb +19 -8
  12. data/lib/mongoid/contextual/none.rb +51 -0
  13. data/lib/mongoid/criteria/queryable/mergeable.rb +12 -0
  14. data/lib/mongoid/criteria/queryable/selectable.rb +109 -0
  15. data/lib/mongoid/criteria/scopable.rb +7 -1
  16. data/lib/mongoid/criteria.rb +45 -1
  17. data/lib/mongoid/document.rb +10 -0
  18. data/lib/mongoid/errors/in_memory_regexp_timeout.rb +26 -0
  19. data/lib/mongoid/errors.rb +1 -0
  20. data/lib/mongoid/field_readable.rb +70 -0
  21. data/lib/mongoid/matchable.rb +6 -1
  22. data/lib/mongoid/matcher/eq_impl_with_regexp.rb +3 -5
  23. data/lib/mongoid/matcher/regex.rb +8 -9
  24. data/lib/mongoid/matcher/regexp_budget.rb +389 -0
  25. data/lib/mongoid/matcher.rb +1 -0
  26. data/lib/mongoid/reloadable.rb +1 -0
  27. data/lib/mongoid/threaded.rb +3 -0
  28. data/lib/mongoid/version.rb +1 -1
  29. data/lib/mongoid/warnings.rb +1 -0
  30. data/spec/integration/app_spec.rb +8 -0
  31. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +17 -2
  32. data/spec/integration/dots_and_dollars_spec.rb +12 -2
  33. data/spec/integration/matcher_operator_data/regex.yml +21 -0
  34. data/spec/integration/matcher_regexp_timeout_spec.rb +215 -0
  35. data/spec/integration/query_operator_guard_spec.rb +89 -0
  36. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +96 -0
  37. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +228 -0
  38. data/spec/mongoid/attributes/nested_spec.rb +204 -10
  39. data/spec/mongoid/attributes/readonly_dotted_spec.rb +332 -0
  40. data/spec/mongoid/attributes/readonly_spec.rb +389 -0
  41. data/spec/mongoid/changeable_spec.rb +18 -0
  42. data/spec/mongoid/config_spec.rb +7 -0
  43. data/spec/mongoid/contextual/aggregable/memory_spec.rb +91 -0
  44. data/spec/mongoid/contextual/memory_spec.rb +202 -0
  45. data/spec/mongoid/contextual/none_spec.rb +22 -5
  46. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +2 -0
  47. data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +235 -0
  48. data/spec/mongoid/criteria_spec.rb +19 -0
  49. data/spec/mongoid/findable_spec.rb +14 -0
  50. data/spec/mongoid/matcher/regexp_budget_spec.rb +576 -0
  51. data/spec/mongoid/scopable_spec.rb +116 -2
  52. data/spec/spec_helper.rb +8 -0
  53. data/spec/support/models/delete_all_account.rb +10 -0
  54. data/spec/support/models/delete_all_item.rb +9 -0
  55. data/spec/support/models/delete_all_scoped_item.rb +11 -0
  56. data/spec/support/models/delete_all_tag.rb +7 -0
  57. metadata +1900 -1860
@@ -7,6 +7,33 @@ require_relative './nested_spec_models'
7
7
 
8
8
  describe Mongoid::Attributes::Nested do
9
9
 
10
+ # Expects the enclosing context to define `build_with_id`, taking an id
11
+ # value and returning the document built with it in nested attributes.
12
+ shared_examples 'rejects non-scalar ids' do |klass_name|
13
+ not_found = /not found for class #{klass_name}/
14
+
15
+ context 'when the id is an operator hash' do
16
+ it 'raises a document not found error' do
17
+ expect { build_with_id({ '$ne' => nil }) }
18
+ .to raise_error(Mongoid::Errors::DocumentNotFound, not_found)
19
+ end
20
+ end
21
+
22
+ context 'when the id is an array' do
23
+ it 'raises a document not found error' do
24
+ expect { build_with_id([BSON::ObjectId.new]) }
25
+ .to raise_error(Mongoid::Errors::DocumentNotFound, not_found)
26
+ end
27
+ end
28
+
29
+ context 'when the id is a malformed object id hash' do
30
+ it 'raises a document not found error' do
31
+ expect { build_with_id({ '$oid' => 'junk' }) }
32
+ .to raise_error(Mongoid::Errors::DocumentNotFound, not_found)
33
+ end
34
+ end
35
+ end
36
+
10
37
  describe ".accepts_nested_attributes_for" do
11
38
 
12
39
  context "when the autosave option is not defined" do
@@ -178,6 +205,30 @@ describe Mongoid::Attributes::Nested do
178
205
  end
179
206
  end
180
207
 
208
+ context 'when the id in the attributes is not a scalar' do
209
+ let(:person) { Person.create! }
210
+
211
+ def build_with_id(id)
212
+ person.update!(posts_attributes: { '0' => { id: id, title: 'Crafted' } })
213
+ end
214
+
215
+ context 'when the association is empty' do
216
+ include_examples 'rejects non-scalar ids', 'Post'
217
+ end
218
+
219
+ context 'when the association is not empty' do
220
+ before { person.posts.create!(title: 'Existing') }
221
+
222
+ include_examples 'rejects non-scalar ids', 'Post'
223
+ end
224
+
225
+ context 'when allow_reparenting_via_nested_attributes is true' do
226
+ config_override :allow_reparenting_via_nested_attributes, true
227
+
228
+ include_examples 'rejects non-scalar ids', 'Post'
229
+ end
230
+ end
231
+
181
232
  context "when the relation is a references and referenced in many" do
182
233
 
183
234
  before do
@@ -201,16 +252,100 @@ describe Mongoid::Attributes::Nested do
201
252
  )
202
253
  end
203
254
 
204
- it "sets the nested attributes" do
205
- expect(person.preferences.map(&:name)).to eq([preference.name])
255
+ context 'when allow_reparenting_via_nested_attributes is false' do
256
+ config_override :allow_reparenting_via_nested_attributes, false
257
+
258
+ it 'raises a document not found error' do
259
+ expect { person }.to raise_error(Mongoid::Errors::DocumentNotFound,
260
+ /Document\(s\) not found for class Preference/)
261
+ end
206
262
  end
207
263
 
208
- it "updates attributes of existing document which is added to relation" do
209
- preference_name = 'updated preference'
210
- person = Person.new(
211
- preferences_attributes: { 0 => { id: preference.id, name: preference_name } }
212
- )
213
- expect(person.preferences.map(&:name)).to eq([preference_name])
264
+ context 'when allow_reparenting_via_nested_attributes is true' do
265
+ config_override :allow_reparenting_via_nested_attributes, true
266
+
267
+ it 'sets the nested attributes' do
268
+ expect(person.preferences.map(&:name)).to eq([preference.name])
269
+ end
270
+
271
+ it 'updates attributes of existing document which is added to relation' do
272
+ preference_name = 'updated preference'
273
+ person = Person.new(
274
+ preferences_attributes: { 0 => { id: preference.id, name: preference_name } }
275
+ )
276
+ expect(person.preferences.map(&:name)).to eq([preference_name])
277
+ end
278
+
279
+ context 'when the id does not correspond to an existing document' do
280
+ let(:person) do
281
+ Person.new(
282
+ preferences_attributes: { 0 => { id: BSON::ObjectId.new, name: 'Ghost' } }
283
+ )
284
+ end
285
+
286
+ context 'when raise_not_found_error is true' do
287
+ config_override :raise_not_found_error, true
288
+
289
+ it 'raises a document not found error' do
290
+ expect { person }.to raise_error(Mongoid::Errors::DocumentNotFound,
291
+ /Document\(s\) not found for class Preference/)
292
+ end
293
+ end
294
+
295
+ context 'when raise_not_found_error is false' do
296
+ config_override :raise_not_found_error, false
297
+
298
+ it 'raises a document not found error' do
299
+ expect { person }.to raise_error(Mongoid::Errors::DocumentNotFound,
300
+ /Document\(s\) not found for class Preference/)
301
+ end
302
+ end
303
+ end
304
+ end
305
+
306
+ context 'when _destroy is true for a document not in the relation' do
307
+ before do
308
+ Person.send(:undef_method, :preferences_attributes=)
309
+ Person.accepts_nested_attributes_for :preferences, allow_destroy: true
310
+ end
311
+
312
+ let(:person) do
313
+ Person.new(
314
+ preferences_attributes: { 0 => { id: preference.id, _destroy: '1' } }
315
+ )
316
+ end
317
+
318
+ it 'does not raise UnknownAttribute' do
319
+ expect { person }.not_to raise_error
320
+ end
321
+
322
+ it 'does not add the document to the relation' do
323
+ expect(person.preferences).to be_empty
324
+ end
325
+ end
326
+ end
327
+
328
+ context 'when the id in the attributes is not a scalar' do
329
+ let(:target) { Person.create! }
330
+
331
+ def build_with_id(id)
332
+ target.update!(preferences_attributes: { 0 => { id: id, name: 'Crafted' } })
333
+ end
334
+
335
+ context 'when the association is empty' do
336
+ include_examples 'rejects non-scalar ids', 'Preference'
337
+ end
338
+
339
+ context 'when the association is not empty' do
340
+ before { target.preferences.create!(name: 'Existing') }
341
+
342
+ include_examples 'rejects non-scalar ids', 'Preference'
343
+ end
344
+
345
+ context 'when allow_reparenting_via_nested_attributes is true' do
346
+ config_override :allow_reparenting_via_nested_attributes, true
347
+
348
+ include_examples 'rejects non-scalar ids', 'Preference'
214
349
  end
215
350
  end
216
351
  end
@@ -3015,7 +3150,7 @@ describe Mongoid::Attributes::Nested do
3015
3150
  { "0" =>
3016
3151
  { "id" => BSON::ObjectId.new.to_s, "title" => "Rogue" }
3017
3152
  }
3018
- }.to raise_error(Mongoid::Errors::DocumentNotFound, /Document\(s\) not found for class Post with id\(s\)/)
3153
+ }.to raise_error(Mongoid::Errors::DocumentNotFound, /Document not found for class Post/)
3019
3154
  end
3020
3155
  end
3021
3156
  end
@@ -3049,7 +3184,7 @@ describe Mongoid::Attributes::Nested do
3049
3184
  expect {
3050
3185
  person.posts_attributes =
3051
3186
  { "foo" => { "id" => "test", "title" => "Test" } }
3052
- }.to raise_error(Mongoid::Errors::DocumentNotFound, /Document\(s\) not found for class Post with id\(s\)/)
3187
+ }.to raise_error(Mongoid::Errors::DocumentNotFound, /Document not found for class Post/)
3053
3188
  end
3054
3189
  end
3055
3190
  end
@@ -4997,4 +5132,63 @@ describe Mongoid::Attributes::Nested do
4997
5132
  end
4998
5133
  end
4999
5134
  end
5135
+
5136
+ context 'when an id in nested attributes belongs to another parent' do
5137
+ let(:victim) { Person.create! }
5138
+ let(:attacker) { Person.create! }
5139
+
5140
+ context 'when the association is a has-many' do
5141
+ before do
5142
+ Person.send(:undef_method, :posts_attributes=)
5143
+ Person.accepts_nested_attributes_for :posts
5144
+ end
5145
+
5146
+ let!(:victim_post) { victim.posts.create!(title: 'Private') }
5147
+
5148
+ it 'does not update the other parent\'s document' do
5149
+ expect { attacker.update!(posts_attributes: { '0' => { id: victim_post.id, title: 'Overwritten' } }) }
5150
+ .to raise_error(Mongoid::Errors::DocumentNotFound)
5151
+ expect(victim_post.reload.title).to eq('Private')
5152
+ end
5153
+
5154
+ it 'does not add the other parent\'s document to the association' do
5155
+ expect { attacker.update!(posts_attributes: { '0' => { id: victim_post.id, title: 'Overwritten' } }) }
5156
+ .to raise_error(Mongoid::Errors::DocumentNotFound)
5157
+ expect(attacker.reload.posts).to be_empty
5158
+ end
5159
+
5160
+ it 'does not update via a direct association assignment' do
5161
+ expect { attacker.update!(posts: { '0' => { id: victim_post.id, title: 'Overwritten' } }) }
5162
+ .to raise_error(Mongoid::Errors::DocumentNotFound)
5163
+ expect(victim_post.reload.title).to eq('Private')
5164
+ end
5165
+ end
5166
+
5167
+ context 'when the association is a has-and-belongs-to-many' do
5168
+ before do
5169
+ Person.send(:undef_method, :preferences_attributes=)
5170
+ Person.accepts_nested_attributes_for :preferences
5171
+ end
5172
+
5173
+ let!(:victim_preference) { victim.preferences.create!(name: 'Private') }
5174
+
5175
+ it 'does not update the other parent\'s document' do
5176
+ expect { attacker.update!(preferences_attributes: { '0' => { id: victim_preference.id, name: 'Overwritten' } }) }
5177
+ .to raise_error(Mongoid::Errors::DocumentNotFound)
5178
+ expect(victim_preference.reload.name).to eq('Private')
5179
+ end
5180
+
5181
+ it 'does not add the other parent\'s document to the association' do
5182
+ expect { attacker.update!(preferences_attributes: { '0' => { id: victim_preference.id, name: 'Overwritten' } }) }
5183
+ .to raise_error(Mongoid::Errors::DocumentNotFound)
5184
+ expect(attacker.reload.preferences).to be_empty
5185
+ end
5186
+
5187
+ it 'does not update via a direct association assignment' do
5188
+ expect { attacker.update!(preferences: { '0' => { id: victim_preference.id, name: 'Overwritten' } }) }
5189
+ .to raise_error(Mongoid::Errors::DocumentNotFound)
5190
+ expect(victim_preference.reload.name).to eq('Private')
5191
+ end
5192
+ end
5193
+ end
5000
5194
  end
@@ -0,0 +1,332 @@
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 writes to the immediate parent' do
190
+ document.write_attribute('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 the bracket setter for the top-level parent' do
197
+ document[:settings] = { 'permissions' => { 'protected' => { 'is_admin' => true } } }
198
+ document.save!
199
+
200
+ expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
201
+ end
202
+
203
+ it 'allows mass assignment to the top-level parent' do
204
+ document.update(settings: { 'permissions' => { 'protected' => { 'is_admin' => true } } })
205
+
206
+ expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
207
+ end
208
+
209
+ it 'allows update_attribute on the top-level parent' do
210
+ document.update_attribute('settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
211
+
212
+ expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
213
+ end
214
+
215
+ it 'allows removing the top-level parent' do
216
+ document.remove_attribute('settings')
217
+ document.save!
218
+
219
+ expect_stored_value('stored_settings', nil)
220
+ end
221
+
222
+ it 'allows replacing the immediate parent with a scalar' do
223
+ document.write_attribute('settings.permissions', 'none')
224
+ document.save!
225
+
226
+ expect_stored_value('stored_settings', 'permissions' => 'none')
227
+ end
228
+
229
+ it 'allows an in-place change to the parent hash' do
230
+ document.settings['permissions']['protected']['is_admin'] = true
231
+ document.save!
232
+
233
+ expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
234
+ end
235
+
236
+ it 'allows replacing a parent inside the hash' do
237
+ document.settings['permissions'] = { 'protected' => { 'is_admin' => true } }
238
+ document.save!
239
+
240
+ expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
241
+ end
242
+
243
+ it 'allows writes to a sibling with a similar name' do
244
+ document.write_attribute('settings.permissions.protected_extra', true)
245
+ document.save!
246
+
247
+ expect_stored_value(
248
+ 'stored_settings',
249
+ 'permissions' => { 'protected' => { 'is_admin' => false }, 'protected_extra' => true }
250
+ )
251
+ end
252
+
253
+ context 'with dynamic attributes' do
254
+ let(:model_class) do
255
+ test_model do
256
+ include Mongoid::Attributes::Dynamic
257
+
258
+ field :stored_settings, as: :settings, type: Hash
259
+ attr_readonly 'settings.permissions.protected'
260
+ end
261
+ end
262
+
263
+ include_examples 'a readonly dotted attribute',
264
+ 'settings.permissions.protected.is_admin', 'stored_settings',
265
+ { 'permissions' => { 'protected' => { 'is_admin' => false } } }
266
+
267
+ it 'ignores mass assignment to a descendant' do
268
+ document.update('settings.permissions.protected.is_admin' => true)
269
+
270
+ expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => false } })
271
+ end
272
+
273
+ it 'allows mass assignment to a parent' do
274
+ document.update('settings.permissions' => { 'protected' => { 'is_admin' => true } })
275
+
276
+ expect_stored_value('stored_settings', 'permissions' => { 'protected' => { 'is_admin' => true } })
277
+ end
278
+ end
279
+ end
280
+
281
+ context 'with a separate field whose name contains a dot' do
282
+ let(:document) { model_class.create!(settings: { 'theme' => 'light' }) }
283
+
284
+ context 'when the dotted field is readonly' do
285
+ let(:model_class) do
286
+ test_model do
287
+ field :settings, type: Hash
288
+ field :'settings.is_admin', type: String, default: 'literal field'
289
+ attr_readonly :'settings.is_admin'
290
+ end
291
+ end
292
+
293
+ it 'allows updating the separate field' do
294
+ document.settings = { 'theme' => 'dark' }
295
+ document.save!
296
+
297
+ expect_stored_value('settings', 'theme' => 'dark')
298
+ expect_stored_value('settings.is_admin', 'literal field')
299
+ end
300
+
301
+ it 'allows updating a nested hash with the same name as the dotted field' do
302
+ document.write_attribute('settings.is_admin.enabled', true)
303
+ document.save!
304
+
305
+ expect_stored_value('settings', 'theme' => 'light', 'is_admin' => { 'enabled' => true })
306
+ expect_stored_value('settings.is_admin', 'literal field')
307
+ end
308
+
309
+ it 'still treats the dotted field as readonly' do
310
+ expect do
311
+ document.update_attribute('settings.is_admin', 'new value')
312
+ end.to raise_error(Mongoid::Errors::ReadonlyAttribute)
313
+ end
314
+ end
315
+
316
+ context 'when the separate field is readonly' do
317
+ let(:model_class) do
318
+ test_model do
319
+ field :settings, type: Hash
320
+ field :'settings.is_admin', type: String, default: 'literal field'
321
+ attr_readonly :settings
322
+ end
323
+ end
324
+
325
+ it 'raises the invalid dotted assignment error for the dotted field' do
326
+ expect do
327
+ document.update_attribute('settings.is_admin', 'new value')
328
+ end.to raise_error(Mongoid::Errors::InvalidDotDollarAssignment)
329
+ end
330
+ end
331
+ end
332
+ end