mongoid 7.6.2 → 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/mongoid/association/referenced/has_many/proxy.rb +4 -2
- data/lib/mongoid/attributes/readonly.rb +33 -3
- data/lib/mongoid/changeable.rb +22 -4
- data/lib/mongoid/contextual/none.rb +51 -0
- data/lib/mongoid/criteria/queryable/selectable.rb +15 -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/reloadable.rb +1 -0
- data/lib/mongoid/threaded.rb +1 -1
- data/lib/mongoid/version.rb +1 -1
- data/spec/integration/query_operator_guard_spec.rb +5 -0
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +48 -0
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +83 -0
- 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/none_spec.rb +18 -0
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +27 -0
- data/spec/mongoid/criteria_spec.rb +17 -0
- data/spec/mongoid/findable_spec.rb +14 -0
- data/spec/mongoid/matcher/regexp_budget_spec.rb +6 -0
- data/spec/mongoid/scopable_spec.rb +116 -2
- 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 -1790
|
@@ -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
|
|
@@ -83,6 +83,28 @@ describe Mongoid::Attributes::Readonly do
|
|
|
83
83
|
it "persists the third readonly value" do
|
|
84
84
|
expect(person.reload.aliased_timestamp).to eq(Time.at(42))
|
|
85
85
|
end
|
|
86
|
+
|
|
87
|
+
it 'reports the readonly value as a saved change' do
|
|
88
|
+
expect(person.previous_changes['title']).to eq([ nil, 'sir' ])
|
|
89
|
+
expect(person.title_previously_changed?).to be true
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
it 'reports the saved change for an aliased readonly field' do
|
|
93
|
+
expect(person.previous_changes['at']).to eq([ nil, Time.at(42) ])
|
|
94
|
+
expect(person.aliased_timestamp_previously_changed?).to be true
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it 'does not persist a later change to the readonly field' do
|
|
98
|
+
person.title = 'mr'
|
|
99
|
+
person.score = 1
|
|
100
|
+
person.save!
|
|
101
|
+
|
|
102
|
+
expect(person.previous_changes).not_to have_key('title')
|
|
103
|
+
expect(person.previous_changes).to have_key('score')
|
|
104
|
+
|
|
105
|
+
expect(person.reload.title).to eq('sir')
|
|
106
|
+
expect(person.reload.score).to eq(1)
|
|
107
|
+
end
|
|
86
108
|
end
|
|
87
109
|
|
|
88
110
|
context "when updating an existing readonly field" do
|
|
@@ -233,6 +255,371 @@ describe Mongoid::Attributes::Readonly do
|
|
|
233
255
|
end
|
|
234
256
|
end
|
|
235
257
|
|
|
258
|
+
context 'when modifying a readonly field in place' do
|
|
259
|
+
let(:attributes) do
|
|
260
|
+
%i[map aliases array]
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
let(:person) do
|
|
264
|
+
Person.create!(map: { 'enabled' => false }, aliases: [ 'original' ], array: [ 'original' ], title: 'old')
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
it 'does not persist changes to a readonly hash' do
|
|
268
|
+
person.map['enabled'] = true
|
|
269
|
+
person.save!
|
|
270
|
+
|
|
271
|
+
expect(person.reload.map).to eq('enabled' => false)
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
it 'does not mark the document as changed' do
|
|
275
|
+
person.map['enabled'] = true
|
|
276
|
+
|
|
277
|
+
expect(person).not_to be_changed
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
it 'does not mark an aliased readonly field as changed' do
|
|
281
|
+
person.array << 'added'
|
|
282
|
+
|
|
283
|
+
expect(person).not_to be_changed
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
it 'allows in-place changes before the document is created' do
|
|
287
|
+
new_person = Person.new(map: { 'enabled' => false })
|
|
288
|
+
new_person.map['enabled'] = true
|
|
289
|
+
new_person.save!
|
|
290
|
+
|
|
291
|
+
expect(new_person.reload.map).to eq('enabled' => true)
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
it 'does not report the ignored change as saved' do
|
|
295
|
+
person.map['enabled'] = true
|
|
296
|
+
person.title = 'new'
|
|
297
|
+
person.save!
|
|
298
|
+
|
|
299
|
+
expect(person.previous_changes).not_to have_key('map')
|
|
300
|
+
expect(person.previous_changes).to have_key('title')
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
it 'does not update timestamps for an ignored change' do
|
|
304
|
+
model = test_model do
|
|
305
|
+
include Mongoid::Timestamps
|
|
306
|
+
|
|
307
|
+
field :settings, type: Hash
|
|
308
|
+
attr_readonly :settings
|
|
309
|
+
end
|
|
310
|
+
document = Timecop.freeze(Time.utc(2020, 1, 1)) do
|
|
311
|
+
model.create!(settings: { 'enabled' => false })
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
document.settings['enabled'] = true
|
|
315
|
+
Timecop.freeze(Time.utc(2020, 1, 2)) { document.save! }
|
|
316
|
+
|
|
317
|
+
expect(document.reload.updated_at).to eq(Time.utc(2020, 1, 1))
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
it 'does not persist changes made through attributes' do
|
|
321
|
+
person.map
|
|
322
|
+
person.attributes['map']['enabled'] = true
|
|
323
|
+
person.save!
|
|
324
|
+
|
|
325
|
+
expect(person.reload.map).to eq('enabled' => false)
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
it 'resets an in-place change to a readonly hash' do
|
|
329
|
+
person.map['enabled'] = true
|
|
330
|
+
person.reset_map!
|
|
331
|
+
|
|
332
|
+
expect(person.map).to eq('enabled' => false)
|
|
333
|
+
expect(person).not_to be_changed
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
context 'when a tracked field becomes readonly' do
|
|
337
|
+
let(:model) do
|
|
338
|
+
test_model do
|
|
339
|
+
include Mongoid::Timestamps
|
|
340
|
+
|
|
341
|
+
field :settings, type: Hash
|
|
342
|
+
field :title
|
|
343
|
+
end
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
let(:document) do
|
|
347
|
+
Timecop.freeze(Time.utc(2020, 1, 1)) do
|
|
348
|
+
model.create!(settings: { 'enabled' => false }, title: 'old')
|
|
349
|
+
end
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
before do
|
|
353
|
+
document.settings['enabled'] = true
|
|
354
|
+
model.attr_readonly :settings
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
it 'does not persist the ignored change' do
|
|
358
|
+
document.save!
|
|
359
|
+
|
|
360
|
+
expect(document.reload.settings).to eq('enabled' => false)
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
it 'does not report the ignored change as dirty' do
|
|
364
|
+
expect(document).not_to be_changed
|
|
365
|
+
expect(document.changes).not_to have_key('settings')
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
it 'does not report the readonly field as changed' do
|
|
369
|
+
expect(document.settings_changed?).to be false
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
it 'does not report the ignored change as saved' do
|
|
373
|
+
document.title = 'new'
|
|
374
|
+
document.save!
|
|
375
|
+
|
|
376
|
+
expect(document.previous_changes).not_to have_key('settings')
|
|
377
|
+
expect(document.settings_previously_changed?).to be false
|
|
378
|
+
expect(document.previous_changes).to have_key('title')
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
it 'does not update timestamps for the ignored change' do
|
|
382
|
+
Timecop.freeze(Time.utc(2020, 1, 2)) { document.save! }
|
|
383
|
+
|
|
384
|
+
expect(document.reload.updated_at).to eq(Time.utc(2020, 1, 1))
|
|
385
|
+
end
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
it 'does not persist changes to a readonly array' do
|
|
389
|
+
person.aliases << 'added'
|
|
390
|
+
person.save!
|
|
391
|
+
|
|
392
|
+
expect(person.reload.aliases).to eq([ 'original' ])
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
it 'does not persist changes to an aliased readonly field' do
|
|
396
|
+
person.array << 'added'
|
|
397
|
+
person.save!
|
|
398
|
+
|
|
399
|
+
expect(person.reload.array).to eq([ 'original' ])
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
it 'persists changes to other fields' do
|
|
403
|
+
person.map['enabled'] = true
|
|
404
|
+
person.title = 'new'
|
|
405
|
+
person.save!
|
|
406
|
+
|
|
407
|
+
expect(person.reload.map).to eq('enabled' => false)
|
|
408
|
+
expect(person.title).to eq('new')
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
it 'still allows atomic set on a readonly field' do
|
|
412
|
+
person.set('map.enabled' => true)
|
|
413
|
+
|
|
414
|
+
expect(person.reload.map).to eq('enabled' => true)
|
|
415
|
+
end
|
|
416
|
+
|
|
417
|
+
it 'still allows atomic unset on a readonly field' do
|
|
418
|
+
person.unset('map.enabled')
|
|
419
|
+
|
|
420
|
+
expect(person.reload.map).to eq({})
|
|
421
|
+
end
|
|
422
|
+
|
|
423
|
+
context 'when an atomic array update fails' do
|
|
424
|
+
it 'restores the readonly array in memory and in the database' do
|
|
425
|
+
expect do
|
|
426
|
+
person.atomically do
|
|
427
|
+
person.push(aliases: 'added')
|
|
428
|
+
raise 'abort'
|
|
429
|
+
end
|
|
430
|
+
end.to raise_error('abort')
|
|
431
|
+
|
|
432
|
+
expect(Person.collection.find(_id: person.id).first['aliases']).to eq([ 'original' ])
|
|
433
|
+
expect(person.aliases).to eq([ 'original' ])
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
it 'still allows a successful atomic update' do
|
|
437
|
+
person.atomically { person.push(aliases: 'added') }
|
|
438
|
+
|
|
439
|
+
expect(person.reload.aliases).to eq(%w[original added])
|
|
440
|
+
end
|
|
441
|
+
end
|
|
442
|
+
end
|
|
443
|
+
|
|
444
|
+
context 'when an embedded document has a readonly hash' do
|
|
445
|
+
let(:attributes) do
|
|
446
|
+
:title
|
|
447
|
+
end
|
|
448
|
+
|
|
449
|
+
let(:person) do
|
|
450
|
+
Person.create!(addresses: [ { street: 'Main', map: { 'enabled' => false } } ])
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
around do |example|
|
|
454
|
+
original = Address.readonly_attributes
|
|
455
|
+
Address.attr_readonly :map
|
|
456
|
+
example.run
|
|
457
|
+
ensure
|
|
458
|
+
Address.readonly_attributes = original
|
|
459
|
+
end
|
|
460
|
+
|
|
461
|
+
it 'allows the initial value to be saved' do
|
|
462
|
+
expect(person.reload.addresses.first.map).to eq('enabled' => false)
|
|
463
|
+
end
|
|
464
|
+
|
|
465
|
+
it 'ignores in-place changes when saving the parent' do
|
|
466
|
+
person.addresses.first.map['enabled'] = true
|
|
467
|
+
person.save!
|
|
468
|
+
|
|
469
|
+
expect(person.reload.addresses.first.map).to eq('enabled' => false)
|
|
470
|
+
end
|
|
471
|
+
|
|
472
|
+
it 'ignores in-place changes when saving the embedded document' do
|
|
473
|
+
address = person.addresses.first
|
|
474
|
+
address.map['enabled'] = true
|
|
475
|
+
address.save!
|
|
476
|
+
|
|
477
|
+
expect(person.reload.addresses.first.map).to eq('enabled' => false)
|
|
478
|
+
end
|
|
479
|
+
|
|
480
|
+
it 'allows an initial value when adding a new embedded document' do
|
|
481
|
+
person.addresses.build(street: 'Second', map: { 'enabled' => true })
|
|
482
|
+
person.save!
|
|
483
|
+
|
|
484
|
+
expect(person.reload.addresses.last.map).to eq('enabled' => true)
|
|
485
|
+
end
|
|
486
|
+
|
|
487
|
+
it 'reports the initial value as a saved change' do
|
|
488
|
+
address = person.addresses.first
|
|
489
|
+
|
|
490
|
+
expect(address.previous_changes['map']).to eq([ nil, { 'enabled' => false } ])
|
|
491
|
+
expect(address.map_previously_changed?).to be true
|
|
492
|
+
end
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
context 'when a persisted root document is converted with becomes' do
|
|
496
|
+
let(:attributes) do
|
|
497
|
+
:title
|
|
498
|
+
end
|
|
499
|
+
|
|
500
|
+
let(:model) do
|
|
501
|
+
test_model do
|
|
502
|
+
field :settings, type: Hash
|
|
503
|
+
attr_readonly :settings
|
|
504
|
+
end
|
|
505
|
+
end
|
|
506
|
+
|
|
507
|
+
let(:original) do
|
|
508
|
+
model.create!(settings: { 'enabled' => false })
|
|
509
|
+
end
|
|
510
|
+
|
|
511
|
+
it 'does not persist an in-place change to the readonly field of the became root document' do
|
|
512
|
+
became = original.becomes(model)
|
|
513
|
+
became.settings['enabled'] = true
|
|
514
|
+
became.save!
|
|
515
|
+
|
|
516
|
+
expect(model.collection.find(_id: original.id).first['settings']).to eq('enabled' => false)
|
|
517
|
+
end
|
|
518
|
+
end
|
|
519
|
+
|
|
520
|
+
context 'when a persisted document is converted to another class with becomes' do
|
|
521
|
+
let(:attributes) do
|
|
522
|
+
:title
|
|
523
|
+
end
|
|
524
|
+
|
|
525
|
+
let(:model) do
|
|
526
|
+
test_model do
|
|
527
|
+
field :settings, type: Hash
|
|
528
|
+
attr_readonly :settings
|
|
529
|
+
end
|
|
530
|
+
end
|
|
531
|
+
|
|
532
|
+
let(:subclass) do
|
|
533
|
+
Class.new(model)
|
|
534
|
+
end
|
|
535
|
+
|
|
536
|
+
let(:original) do
|
|
537
|
+
model.create!(settings: { 'enabled' => false })
|
|
538
|
+
end
|
|
539
|
+
|
|
540
|
+
it 'does not persist an in-place change to the readonly field of the became document' do
|
|
541
|
+
became = original.becomes(subclass)
|
|
542
|
+
became.settings['enabled'] = true
|
|
543
|
+
became.save!
|
|
544
|
+
|
|
545
|
+
expect(model.collection.find(_id: original.id).first['settings']).to eq('enabled' => false)
|
|
546
|
+
end
|
|
547
|
+
end
|
|
548
|
+
|
|
549
|
+
context 'when a persisted document with embedded documents is converted with becomes' do
|
|
550
|
+
let(:attributes) do
|
|
551
|
+
:title
|
|
552
|
+
end
|
|
553
|
+
|
|
554
|
+
let(:person) do
|
|
555
|
+
Person.create!(addresses: [ { street: 'Main', map: { 'enabled' => false } } ])
|
|
556
|
+
end
|
|
557
|
+
|
|
558
|
+
around do |example|
|
|
559
|
+
original = Address.readonly_attributes
|
|
560
|
+
Address.attr_readonly :map
|
|
561
|
+
example.run
|
|
562
|
+
ensure
|
|
563
|
+
Address.readonly_attributes = original
|
|
564
|
+
end
|
|
565
|
+
|
|
566
|
+
it 'does not persist an in-place change to the readonly field of a became embedded document' do
|
|
567
|
+
became = person.becomes(Person)
|
|
568
|
+
became.addresses.first.map['enabled'] = true
|
|
569
|
+
became.save!
|
|
570
|
+
|
|
571
|
+
stored = Person.collection.find(_id: person.id).first['addresses'].first['map']
|
|
572
|
+
expect(stored).to eq('enabled' => false)
|
|
573
|
+
end
|
|
574
|
+
end
|
|
575
|
+
|
|
576
|
+
context 'when a new document is converted with becomes' do
|
|
577
|
+
let(:attributes) do
|
|
578
|
+
:title
|
|
579
|
+
end
|
|
580
|
+
|
|
581
|
+
let(:model) do
|
|
582
|
+
test_model do
|
|
583
|
+
field :settings, type: Hash
|
|
584
|
+
attr_readonly :settings
|
|
585
|
+
end
|
|
586
|
+
end
|
|
587
|
+
|
|
588
|
+
it 'reports the readonly value as a saved change when the became document is created' do
|
|
589
|
+
became = model.new(settings: { 'enabled' => false }).becomes(model)
|
|
590
|
+
became.save!
|
|
591
|
+
|
|
592
|
+
expect(became.previous_changes['settings']).to eq([ nil, { 'enabled' => false } ])
|
|
593
|
+
expect(model.collection.find(_id: became.id).first['settings']).to eq('enabled' => false)
|
|
594
|
+
end
|
|
595
|
+
end
|
|
596
|
+
|
|
597
|
+
context 'when a field is named like an internal dirty tracking method' do
|
|
598
|
+
let(:attributes) do
|
|
599
|
+
:title
|
|
600
|
+
end
|
|
601
|
+
|
|
602
|
+
let(:model) do
|
|
603
|
+
test_model do
|
|
604
|
+
field :name, type: String
|
|
605
|
+
field :readonly_change_ignored, type: String
|
|
606
|
+
end
|
|
607
|
+
end
|
|
608
|
+
|
|
609
|
+
it 'tracks changes and creates the document' do
|
|
610
|
+
document = model.new(name: 'original', readonly_change_ignored: 'value')
|
|
611
|
+
|
|
612
|
+
expect(document.name_changed?).to be true
|
|
613
|
+
|
|
614
|
+
document.save!
|
|
615
|
+
|
|
616
|
+
expect(document.reload.name).to eq('original')
|
|
617
|
+
expect(document.readonly_change_ignored).to eq('value')
|
|
618
|
+
expect(document.readonly_change_ignored?).to be true
|
|
619
|
+
expect(document).not_to be_changed
|
|
620
|
+
end
|
|
621
|
+
end
|
|
622
|
+
|
|
236
623
|
context 'when a foreign_key is defined as readonly' do
|
|
237
624
|
|
|
238
625
|
let(:attributes) do
|