iknow_view_models 3.6.6 → 3.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/iknow_view_models.gemspec +1 -1
- data/lib/iknow_view_models/version.rb +1 -1
- data/lib/view_model/callbacks.rb +2 -2
- data/lib/view_model/migrator.rb +8 -1
- data/lib/view_model.rb +2 -2
- data/test/helpers/arvm_test_utilities.rb +2 -2
- data/test/helpers/viewmodel_spec_helpers.rb +2 -2
- data/test/unit/view_model/active_record/controller_nested_test.rb +14 -14
- data/test/unit/view_model/active_record/controller_test.rb +5 -5
- data/test/unit/view_model/active_record/customization_test.rb +3 -3
- data/test/unit/view_model/active_record/has_many_test.rb +6 -6
- data/test/unit/view_model/active_record/migration_test.rb +121 -11
- data/test/unit/view_model/active_record_test.rb +3 -3
- data/test/unit/view_model/callbacks_test.rb +29 -0
- data/test/unit/view_model/record_test.rb +2 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b0920a06f9988d3cc846a4bf1fb592525891adb09ba44e3e7ccded40ed18c2b
|
4
|
+
data.tar.gz: 70267048408dbef5eaeb162465ffb2583e93fcd562f5d75d38c0ea7c7dca096d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a47fe00e8aaac20252a8508438adcf916946f4f5e4e34beabf342d530a30e5b9dbd1837c3422cd38fec8c60ecb0c0e95d1aae76a45b47fde8a66340b5346c3b4
|
7
|
+
data.tar.gz: d327bfab6f0c9a985aa4b44995deb51879b9e854d3bec028ebe48dc3eedd4d14158a20e960f16ab7dfcffb80d3ce4d11979f2fb46833e91a07be3f23d056fbc3
|
data/iknow_view_models.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
20
|
spec.require_paths = ['lib']
|
21
21
|
|
22
|
-
spec.required_ruby_version = '>= 2.7'
|
22
|
+
spec.required_ruby_version = '>= 2.7.3'
|
23
23
|
|
24
24
|
spec.add_dependency 'actionpack', '>= 5.0'
|
25
25
|
spec.add_dependency 'activerecord', '>= 5.0'
|
data/lib/view_model/callbacks.rb
CHANGED
@@ -11,9 +11,9 @@ module ViewModel::Callbacks
|
|
11
11
|
# callbacks instance with additional instance method access to the view,
|
12
12
|
# context and extra context-dependent parameters.
|
13
13
|
module CallbackEnvContext
|
14
|
-
def method_missing(method,
|
14
|
+
def method_missing(method, ...)
|
15
15
|
if _callbacks.respond_to?(method, true)
|
16
|
-
_callbacks.send(method,
|
16
|
+
_callbacks.send(method, ...)
|
17
17
|
else
|
18
18
|
super
|
19
19
|
end
|
data/lib/view_model/migrator.rb
CHANGED
@@ -37,8 +37,15 @@ class ViewModel
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def migrate!(serialization)
|
40
|
-
|
40
|
+
references = (serialization['references'] ||= {})
|
41
|
+
|
42
|
+
migrate_tree!(serialization, references: references)
|
43
|
+
|
41
44
|
GarbageCollection.garbage_collect_references!(serialization)
|
45
|
+
|
46
|
+
if references.empty?
|
47
|
+
serialization.delete('references')
|
48
|
+
end
|
42
49
|
end
|
43
50
|
|
44
51
|
private
|
data/lib/view_model.rb
CHANGED
@@ -303,12 +303,12 @@ class ViewModel
|
|
303
303
|
end
|
304
304
|
end
|
305
305
|
|
306
|
-
def
|
306
|
+
def serialize_to_hash(serialize_context: self.class.new_serialize_context)
|
307
307
|
Jbuilder.new { |json| serialize(json, serialize_context: serialize_context) }.attributes!
|
308
308
|
end
|
309
309
|
|
310
310
|
def to_json(serialize_context: self.class.new_serialize_context)
|
311
|
-
ViewModel.encode_json(self.
|
311
|
+
ViewModel.encode_json(self.serialize_to_hash(serialize_context: serialize_context))
|
312
312
|
end
|
313
313
|
|
314
314
|
# Render this viewmodel to a jBuilder. Usually overridden in subclasses.
|
@@ -98,7 +98,7 @@ module ARVMTestUtilities
|
|
98
98
|
end
|
99
99
|
|
100
100
|
def assert_serializes(vm, model, serialize_context: vm.new_serialize_context)
|
101
|
-
h = vm.new(model).
|
101
|
+
h = vm.new(model).serialize_to_hash(serialize_context: serialize_context)
|
102
102
|
assert_kind_of(Hash, h)
|
103
103
|
refs = serialize_context.serialize_references_to_hash
|
104
104
|
assert_kind_of(Hash, refs)
|
@@ -106,7 +106,7 @@ module ARVMTestUtilities
|
|
106
106
|
|
107
107
|
def refute_serializes(vm, model, message = nil, serialize_context: vm.new_serialize_context)
|
108
108
|
ex = assert_raises(ViewModel::AccessControlError) do
|
109
|
-
vm.new(model).
|
109
|
+
vm.new(model).serialize_to_hash(serialize_context: serialize_context)
|
110
110
|
serialize_context.serialize_references_to_hash
|
111
111
|
end
|
112
112
|
assert_match(message, ex.message) if message
|
@@ -143,8 +143,8 @@ module ViewModelSpecHelpers
|
|
143
143
|
|
144
144
|
module ParentAndChildMigrations
|
145
145
|
extend ActiveSupport::Concern
|
146
|
-
|
147
|
-
|
146
|
+
|
147
|
+
def model_attributes
|
148
148
|
super.merge(
|
149
149
|
schema: ->(t) { t.integer :new_field, default: 1, null: false },
|
150
150
|
viewmodel: ->(_v) {
|
@@ -44,7 +44,7 @@ class ViewModel::ActiveRecord::ControllerNestedTest < ActiveSupport::TestCase
|
|
44
44
|
assert_equal(200, childcontroller.status)
|
45
45
|
|
46
46
|
expected_children = @parent.children
|
47
|
-
assert_equal({ 'data' => expected_children.map { |c| ChildView.new(c).
|
47
|
+
assert_equal({ 'data' => expected_children.map { |c| ChildView.new(c).serialize_to_hash } },
|
48
48
|
childcontroller.hash_response)
|
49
49
|
|
50
50
|
assert_all_hooks_nested_inside_parent_hook(childcontroller.hook_trace)
|
@@ -59,7 +59,7 @@ class ViewModel::ActiveRecord::ControllerNestedTest < ActiveSupport::TestCase
|
|
59
59
|
assert_equal(200, childcontroller.status)
|
60
60
|
|
61
61
|
expected_children = @parent.children + distractor.children
|
62
|
-
assert_equal({ 'data' => expected_children.map { |c| ChildView.new(c).
|
62
|
+
assert_equal({ 'data' => expected_children.map { |c| ChildView.new(c).serialize_to_hash } },
|
63
63
|
childcontroller.hash_response)
|
64
64
|
end
|
65
65
|
|
@@ -79,7 +79,7 @@ class ViewModel::ActiveRecord::ControllerNestedTest < ActiveSupport::TestCase
|
|
79
79
|
@parent.reload
|
80
80
|
|
81
81
|
assert_equal(%w[c1 c2 c3], @parent.children.order(:position).pluck(:name))
|
82
|
-
assert_equal({ 'data' => ChildView.new(@parent.children.last).
|
82
|
+
assert_equal({ 'data' => ChildView.new(@parent.children.last).serialize_to_hash },
|
83
83
|
childcontroller.hash_response)
|
84
84
|
|
85
85
|
assert_all_hooks_nested_inside_parent_hook(childcontroller.hook_trace)
|
@@ -102,7 +102,7 @@ class ViewModel::ActiveRecord::ControllerNestedTest < ActiveSupport::TestCase
|
|
102
102
|
@parent.reload
|
103
103
|
|
104
104
|
assert_equal(%w[c1 c2 c3 c4], @parent.children.order(:position).pluck(:name))
|
105
|
-
new_children_hashes = @parent.children.last(2).map { |c| ChildView.new(c).
|
105
|
+
new_children_hashes = @parent.children.last(2).map { |c| ChildView.new(c).serialize_to_hash }
|
106
106
|
assert_equal({ 'data' => new_children_hashes },
|
107
107
|
childcontroller.hash_response)
|
108
108
|
|
@@ -265,7 +265,7 @@ class ViewModel::ActiveRecord::ControllerNestedTest < ActiveSupport::TestCase
|
|
265
265
|
old_child.reload
|
266
266
|
|
267
267
|
assert_equal('new_name', old_child.name)
|
268
|
-
assert_equal({ 'data' => ChildView.new(old_child).
|
268
|
+
assert_equal({ 'data' => ChildView.new(old_child).serialize_to_hash },
|
269
269
|
childcontroller.hash_response)
|
270
270
|
end
|
271
271
|
|
@@ -275,7 +275,7 @@ class ViewModel::ActiveRecord::ControllerNestedTest < ActiveSupport::TestCase
|
|
275
275
|
childcontroller = ChildController.new(params: { id: old_child.id })
|
276
276
|
childcontroller.invoke(:show)
|
277
277
|
|
278
|
-
assert_equal({ 'data' => ChildView.new(old_child).
|
278
|
+
assert_equal({ 'data' => ChildView.new(old_child).serialize_to_hash },
|
279
279
|
childcontroller.hash_response)
|
280
280
|
|
281
281
|
assert_equal(200, childcontroller.status)
|
@@ -323,7 +323,7 @@ class ViewModel::ActiveRecord::ControllerNestedTest < ActiveSupport::TestCase
|
|
323
323
|
|
324
324
|
assert_equal(200, labelcontroller.status, labelcontroller.hash_response)
|
325
325
|
|
326
|
-
assert_equal({ 'data' => LabelView.new(old_label).
|
326
|
+
assert_equal({ 'data' => LabelView.new(old_label).serialize_to_hash },
|
327
327
|
labelcontroller.hash_response)
|
328
328
|
|
329
329
|
assert_all_hooks_nested_inside_parent_hook(labelcontroller.hook_trace)
|
@@ -367,7 +367,7 @@ class ViewModel::ActiveRecord::ControllerNestedTest < ActiveSupport::TestCase
|
|
367
367
|
old_label.reload
|
368
368
|
|
369
369
|
assert_equal('new label', old_label.text)
|
370
|
-
assert_equal({ 'data' => LabelView.new(old_label).
|
370
|
+
assert_equal({ 'data' => LabelView.new(old_label).serialize_to_hash },
|
371
371
|
labelcontroller.hash_response)
|
372
372
|
|
373
373
|
assert_all_hooks_nested_inside_parent_hook(labelcontroller.hook_trace)
|
@@ -381,7 +381,7 @@ class ViewModel::ActiveRecord::ControllerNestedTest < ActiveSupport::TestCase
|
|
381
381
|
|
382
382
|
assert_equal(200, labelcontroller.status, labelcontroller.hash_response)
|
383
383
|
|
384
|
-
assert_equal({ 'data' => LabelView.new(old_label).
|
384
|
+
assert_equal({ 'data' => LabelView.new(old_label).serialize_to_hash },
|
385
385
|
labelcontroller.hash_response)
|
386
386
|
end
|
387
387
|
|
@@ -414,7 +414,7 @@ class ViewModel::ActiveRecord::ControllerNestedTest < ActiveSupport::TestCase
|
|
414
414
|
old_label.reload
|
415
415
|
|
416
416
|
assert_equal('new label', old_label.text)
|
417
|
-
assert_equal({ 'data' => LabelView.new(old_label).
|
417
|
+
assert_equal({ 'data' => LabelView.new(old_label).serialize_to_hash },
|
418
418
|
labelcontroller.hash_response)
|
419
419
|
end
|
420
420
|
|
@@ -472,11 +472,11 @@ class ViewModel::ActiveRecord::ControllerNestedTest < ActiveSupport::TestCase
|
|
472
472
|
'updates' => [
|
473
473
|
{
|
474
474
|
'id' => @parent.id,
|
475
|
-
'update' => TargetView.new(target).
|
475
|
+
'update' => TargetView.new(target).serialize_to_hash,
|
476
476
|
},
|
477
477
|
{
|
478
478
|
'id' => other_parent.id,
|
479
|
-
'update' => TargetView.new(other_target).
|
479
|
+
'update' => TargetView.new(other_target).serialize_to_hash,
|
480
480
|
},
|
481
481
|
].sort_by { |x| x.fetch('id') }
|
482
482
|
}
|
@@ -535,7 +535,7 @@ class ViewModel::ActiveRecord::ControllerNestedTest < ActiveSupport::TestCase
|
|
535
535
|
|
536
536
|
assert_equal(
|
537
537
|
{
|
538
|
-
ref_key => CategoryView.new(@parent.category).
|
538
|
+
ref_key => CategoryView.new(@parent.category).serialize_to_hash,
|
539
539
|
},
|
540
540
|
references,
|
541
541
|
)
|
@@ -591,7 +591,7 @@ class ViewModel::ActiveRecord::ControllerNestedTest < ActiveSupport::TestCase
|
|
591
591
|
|
592
592
|
assert_equal(
|
593
593
|
{
|
594
|
-
ref_key => TagView.new(@parent.parent_tags.first.tag).
|
594
|
+
ref_key => TagView.new(@parent.parent_tags.first.tag).serialize_to_hash,
|
595
595
|
},
|
596
596
|
references,
|
597
597
|
)
|
@@ -37,7 +37,7 @@ class ViewModel::ActiveRecord::ControllerTest < ActiveSupport::TestCase
|
|
37
37
|
parentcontroller = ParentController.new(params: { id: @parent.id })
|
38
38
|
parentcontroller.invoke(:show)
|
39
39
|
|
40
|
-
assert_equal({ 'data' => @parent_view.
|
40
|
+
assert_equal({ 'data' => @parent_view.serialize_to_hash },
|
41
41
|
parentcontroller.hash_response)
|
42
42
|
|
43
43
|
assert_equal(200, parentcontroller.status)
|
@@ -52,7 +52,7 @@ class ViewModel::ActiveRecord::ControllerTest < ActiveSupport::TestCase
|
|
52
52
|
|
53
53
|
parentcontroller.invoke(:show)
|
54
54
|
|
55
|
-
expected_view = @parent_view.
|
55
|
+
expected_view = @parent_view.serialize_to_hash
|
56
56
|
.except('name')
|
57
57
|
.merge('old_name' => @parent.name,
|
58
58
|
ViewModel::VERSION_ATTRIBUTE => 1,
|
@@ -88,7 +88,7 @@ class ViewModel::ActiveRecord::ControllerTest < ActiveSupport::TestCase
|
|
88
88
|
assert_equal(200, parentcontroller.status)
|
89
89
|
|
90
90
|
assert_equal(parentcontroller.hash_response,
|
91
|
-
{ 'data' => [@parent_view.
|
91
|
+
{ 'data' => [@parent_view.serialize_to_hash, p2_view.serialize_to_hash] })
|
92
92
|
|
93
93
|
assert_all_hooks_nested_inside_parent_hook(parentcontroller.hook_trace)
|
94
94
|
end
|
@@ -112,7 +112,7 @@ class ViewModel::ActiveRecord::ControllerTest < ActiveSupport::TestCase
|
|
112
112
|
p2_view = ParentView.new(p2)
|
113
113
|
assert(p2.present?, 'p2 created')
|
114
114
|
|
115
|
-
assert_equal({ 'data' => p2_view.
|
115
|
+
assert_equal({ 'data' => p2_view.serialize_to_hash }, parentcontroller.hash_response)
|
116
116
|
|
117
117
|
assert_all_hooks_nested_inside_parent_hook(parentcontroller.hook_trace)
|
118
118
|
end
|
@@ -160,7 +160,7 @@ class ViewModel::ActiveRecord::ControllerTest < ActiveSupport::TestCase
|
|
160
160
|
@parent.reload
|
161
161
|
|
162
162
|
assert_equal('new', @parent.name)
|
163
|
-
assert_equal({ 'data' => @parent_view.
|
163
|
+
assert_equal({ 'data' => @parent_view.serialize_to_hash },
|
164
164
|
parentcontroller.hash_response)
|
165
165
|
|
166
166
|
assert_all_hooks_nested_inside_parent_hook(parentcontroller.hook_trace)
|
@@ -283,13 +283,13 @@ class ViewModel::ActiveRecord::FlattenAssociationTest < ActiveSupport::TestCase
|
|
283
283
|
|
284
284
|
def test_serialize
|
285
285
|
v = SectionView.new(@simplesection)
|
286
|
-
assert_equal(@simplesection_view, v.
|
286
|
+
assert_equal(@simplesection_view, v.serialize_to_hash)
|
287
287
|
|
288
288
|
v = SectionView.new(@quizsection)
|
289
|
-
assert_equal(@quizsection_view, v.
|
289
|
+
assert_equal(@quizsection_view, v.serialize_to_hash)
|
290
290
|
|
291
291
|
v = SectionView.new(@vocabsection)
|
292
|
-
assert_equal(@vocabsection_view, v.
|
292
|
+
assert_equal(@vocabsection_view, v.serialize_to_hash)
|
293
293
|
end
|
294
294
|
|
295
295
|
def new_view_like(view)
|
@@ -469,8 +469,8 @@ class ViewModel::ActiveRecord::HasManyTest < ActiveSupport::TestCase
|
|
469
469
|
old_children = @model1.children.order(:position)
|
470
470
|
moved_child = old_children[1]
|
471
471
|
|
472
|
-
view = viewmodel_class.new(@model2).
|
473
|
-
view['children'] << child_viewmodel_class.new(moved_child).
|
472
|
+
view = viewmodel_class.new(@model2).serialize_to_hash
|
473
|
+
view['children'] << child_viewmodel_class.new(moved_child).serialize_to_hash
|
474
474
|
|
475
475
|
retained_children = old_children - [moved_child]
|
476
476
|
release_view = { '_type' => 'Model', 'id' => @model1.id,
|
@@ -503,7 +503,7 @@ class ViewModel::ActiveRecord::HasManyTest < ActiveSupport::TestCase
|
|
503
503
|
def test_has_many_append_and_update_existing_association
|
504
504
|
child = @model1.children[1]
|
505
505
|
|
506
|
-
cv = child_viewmodel_class.new(child).
|
506
|
+
cv = child_viewmodel_class.new(child).serialize_to_hash
|
507
507
|
cv['name'] = 'newname'
|
508
508
|
|
509
509
|
viewmodel_class.new(@model1).append_associated(:children, cv)
|
@@ -558,7 +558,7 @@ class ViewModel::ActiveRecord::HasManyTest < ActiveSupport::TestCase
|
|
558
558
|
def test_move_and_edit_child_to_new
|
559
559
|
child = @model1.children[1]
|
560
560
|
|
561
|
-
child_view = child_viewmodel_class.new(child).
|
561
|
+
child_view = child_viewmodel_class.new(child).serialize_to_hash
|
562
562
|
child_view['name'] = 'changed'
|
563
563
|
|
564
564
|
view = { '_type' => 'Model',
|
@@ -592,9 +592,9 @@ class ViewModel::ActiveRecord::HasManyTest < ActiveSupport::TestCase
|
|
592
592
|
def test_move_and_edit_child_to_existing
|
593
593
|
old_child = @model1.children[1]
|
594
594
|
|
595
|
-
old_child_view = child_viewmodel_class.new(old_child).
|
595
|
+
old_child_view = child_viewmodel_class.new(old_child).serialize_to_hash
|
596
596
|
old_child_view['name'] = 'changed'
|
597
|
-
view = viewmodel_class.new(@model2).
|
597
|
+
view = viewmodel_class.new(@model2).serialize_to_hash
|
598
598
|
view['children'] << old_child_view
|
599
599
|
|
600
600
|
release_view = { '_type' => 'Model', 'id' => @model1.id,
|
@@ -30,8 +30,16 @@ class ViewModel::ActiveRecord::Migration < ActiveSupport::TestCase
|
|
30
30
|
let(:current_serialization) do
|
31
31
|
ctx = viewmodel_class.new_serialize_context
|
32
32
|
view = ViewModel.serialize_to_hash(viewmodel, serialize_context: ctx)
|
33
|
-
|
34
|
-
{ 'data' => view
|
33
|
+
|
34
|
+
serialization = { 'data' => view }
|
35
|
+
|
36
|
+
# Only include 'references' key if there are actually references to
|
37
|
+
# serialize. This matches prerender_viewmodel.
|
38
|
+
if ctx.has_references?
|
39
|
+
serialization['references'] = ctx.serialize_references_to_hash
|
40
|
+
end
|
41
|
+
|
42
|
+
serialization
|
35
43
|
end
|
36
44
|
|
37
45
|
let(:v2_serialization_data) do
|
@@ -51,12 +59,9 @@ class ViewModel::ActiveRecord::Migration < ActiveSupport::TestCase
|
|
51
59
|
}
|
52
60
|
end
|
53
61
|
|
54
|
-
let(:v2_serialization_references) { {} }
|
55
|
-
|
56
62
|
let(:v2_serialization) do
|
57
63
|
{
|
58
64
|
'data' => v2_serialization_data,
|
59
|
-
'references' => v2_serialization_references,
|
60
65
|
}
|
61
66
|
end
|
62
67
|
|
@@ -101,8 +106,7 @@ class ViewModel::ActiveRecord::Migration < ActiveSupport::TestCase
|
|
101
106
|
describe 'upwards' do
|
102
107
|
let(:migrator) { up_migrator }
|
103
108
|
let(:subject_data) { v2_serialization_data.deep_dup }
|
104
|
-
let(:
|
105
|
-
let(:subject) { { 'data' => subject_data, 'references' => subject_references } }
|
109
|
+
let(:subject) { { 'data' => subject_data } }
|
106
110
|
|
107
111
|
let(:expected_result) do
|
108
112
|
current_serialization.deep_merge(
|
@@ -229,12 +233,9 @@ class ViewModel::ActiveRecord::Migration < ActiveSupport::TestCase
|
|
229
233
|
}
|
230
234
|
end
|
231
235
|
|
232
|
-
let(:v1_serialization_references) { {} }
|
233
|
-
|
234
236
|
let(:v1_serialization) do
|
235
237
|
{
|
236
238
|
'data' => v1_serialization_data,
|
237
|
-
'references' => v1_serialization_references,
|
238
239
|
}
|
239
240
|
end
|
240
241
|
|
@@ -331,7 +332,6 @@ class ViewModel::ActiveRecord::Migration < ActiveSupport::TestCase
|
|
331
332
|
ViewModel::MIGRATED_ATTRIBUTE => true,
|
332
333
|
'name' => viewmodel.name,
|
333
334
|
},
|
334
|
-
'references' => {},
|
335
335
|
}
|
336
336
|
end
|
337
337
|
|
@@ -370,4 +370,114 @@ class ViewModel::ActiveRecord::Migration < ActiveSupport::TestCase
|
|
370
370
|
end
|
371
371
|
end
|
372
372
|
end
|
373
|
+
|
374
|
+
describe 'changing references' do
|
375
|
+
include ViewModelSpecHelpers::ParentAndBelongsToChild
|
376
|
+
let(:migration_versions) { { viewmodel_class => 1 } }
|
377
|
+
|
378
|
+
let(:old_field_serialization) do
|
379
|
+
{
|
380
|
+
ViewModel::TYPE_ATTRIBUTE => 'SyntheticType',
|
381
|
+
ViewModel::VERSION_ATTRIBUTE => 1,
|
382
|
+
}
|
383
|
+
end
|
384
|
+
|
385
|
+
def model_attributes
|
386
|
+
old_field_serialization = self.old_field_serialization
|
387
|
+
super.merge(
|
388
|
+
viewmodel: ->(_v) {
|
389
|
+
self.schema_version = 2
|
390
|
+
|
391
|
+
# The migration from 1 -> 2 deleted a referenced field.
|
392
|
+
migrates from: 1, to: 2 do
|
393
|
+
# Put the referenced field back with a canned serialization
|
394
|
+
down do |view, refs|
|
395
|
+
view['old_field'] = { ViewModel::REFERENCE_ATTRIBUTE => 'ref:old_field' }
|
396
|
+
refs['ref:old_field'] = old_field_serialization
|
397
|
+
end
|
398
|
+
|
399
|
+
# Remove the referenced field
|
400
|
+
up do |view, _refs|
|
401
|
+
view.delete('old_field')
|
402
|
+
end
|
403
|
+
end
|
404
|
+
},
|
405
|
+
)
|
406
|
+
end
|
407
|
+
|
408
|
+
let(:v1_serialization_data) do
|
409
|
+
base = {
|
410
|
+
ViewModel::TYPE_ATTRIBUTE => viewmodel_class.view_name,
|
411
|
+
ViewModel::VERSION_ATTRIBUTE => 1,
|
412
|
+
ViewModel::ID_ATTRIBUTE => viewmodel.id,
|
413
|
+
'name' => viewmodel.name,
|
414
|
+
'child' => {
|
415
|
+
ViewModel::TYPE_ATTRIBUTE => child_viewmodel_class.view_name,
|
416
|
+
ViewModel::VERSION_ATTRIBUTE => 1,
|
417
|
+
ViewModel::ID_ATTRIBUTE => viewmodel.child.id,
|
418
|
+
'name' => viewmodel.child.name,
|
419
|
+
},
|
420
|
+
'old_field' => { ViewModel::REFERENCE_ATTRIBUTE => 'ref:old_field' },
|
421
|
+
}
|
422
|
+
end
|
423
|
+
|
424
|
+
let(:v1_serialization_references) do
|
425
|
+
{ 'ref:old_field' => old_field_serialization }
|
426
|
+
end
|
427
|
+
|
428
|
+
let(:v1_serialization) do
|
429
|
+
{
|
430
|
+
'data' => v1_serialization_data,
|
431
|
+
'references' => v1_serialization_references,
|
432
|
+
}
|
433
|
+
end
|
434
|
+
|
435
|
+
describe 'adding references' do
|
436
|
+
let(:migrator) { down_migrator }
|
437
|
+
let(:subject) do
|
438
|
+
ser = current_serialization.deep_dup
|
439
|
+
raise ArgumentError.new("Expected no references") if ser.has_key?('references')
|
440
|
+
ser
|
441
|
+
end
|
442
|
+
|
443
|
+
let(:expected_result) do
|
444
|
+
{
|
445
|
+
'data' => v1_serialization_data.deep_dup.deep_merge(
|
446
|
+
{ ViewModel::MIGRATED_ATTRIBUTE => true }
|
447
|
+
),
|
448
|
+
'references' => v1_serialization_references
|
449
|
+
}
|
450
|
+
end
|
451
|
+
|
452
|
+
it 'migrates and returns references' do
|
453
|
+
migrate!
|
454
|
+
|
455
|
+
assert_equal(expected_result, subject)
|
456
|
+
end
|
457
|
+
end
|
458
|
+
|
459
|
+
describe 'removing references' do
|
460
|
+
let(:migrator) { up_migrator }
|
461
|
+
let(:subject) do
|
462
|
+
ser = v1_serialization.deep_dup
|
463
|
+
raise ArgumentError.new("Expected references") unless ser.has_key?('references')
|
464
|
+
ser
|
465
|
+
end
|
466
|
+
|
467
|
+
let(:expected_result) do
|
468
|
+
{
|
469
|
+
'data' => current_serialization.fetch('data').deep_dup.merge({
|
470
|
+
ViewModel::MIGRATED_ATTRIBUTE => true,
|
471
|
+
}),
|
472
|
+
# references key is absent
|
473
|
+
}
|
474
|
+
end
|
475
|
+
|
476
|
+
it 'migrates and returns references' do
|
477
|
+
migrate!
|
478
|
+
|
479
|
+
assert_equal(expected_result, subject)
|
480
|
+
end
|
481
|
+
end
|
482
|
+
end
|
373
483
|
end
|
@@ -146,7 +146,7 @@ class ViewModel::ActiveRecordTest < ActiveSupport::TestCase
|
|
146
146
|
|
147
147
|
assert_raises(ViewModel::AccessControlError) do
|
148
148
|
no_view_context = ViewModelBase.new_serialize_context(can_view: false)
|
149
|
-
parentview.
|
149
|
+
parentview.serialize_to_hash(serialize_context: no_view_context)
|
150
150
|
end
|
151
151
|
|
152
152
|
assert_raises(ViewModel::AccessControlError) do
|
@@ -191,7 +191,7 @@ class ViewModel::ActiveRecordTest < ActiveSupport::TestCase
|
|
191
191
|
|
192
192
|
ex = assert_raises(ViewModel::AccessControlError) do
|
193
193
|
# edit
|
194
|
-
v = ParentView.new(@parent1).
|
194
|
+
v = ParentView.new(@parent1).serialize_to_hash.merge('name' => 'p2')
|
195
195
|
ParentView.deserialize_from_view(v, deserialize_context: no_edit_context)
|
196
196
|
end
|
197
197
|
assert_match(/Illegal edit/, ex.message)
|
@@ -214,7 +214,7 @@ class ViewModel::ActiveRecordTest < ActiveSupport::TestCase
|
|
214
214
|
|
215
215
|
ex = assert_raises(ViewModel::AccessControlError) do
|
216
216
|
# edit
|
217
|
-
v = ParentView.new(@parent1).
|
217
|
+
v = ParentView.new(@parent1).serialize_to_hash.merge('name' => 'p2')
|
218
218
|
ParentView.deserialize_from_view(v, deserialize_context: no_edit_context)
|
219
219
|
end
|
220
220
|
assert_match(/Illegal edit/, ex.message)
|
@@ -554,6 +554,35 @@ class ViewModel::CallbacksTest < ActiveSupport::TestCase
|
|
554
554
|
end
|
555
555
|
end
|
556
556
|
|
557
|
+
describe 'delegates to methods on the callback object' do
|
558
|
+
class TestCallback
|
559
|
+
include ViewModel::Callbacks
|
560
|
+
|
561
|
+
attr_reader :a_args, :b_args
|
562
|
+
|
563
|
+
def a(*args)
|
564
|
+
@a_args = args
|
565
|
+
end
|
566
|
+
|
567
|
+
def b(*args, **kwargs)
|
568
|
+
@b_args = [args, kwargs]
|
569
|
+
end
|
570
|
+
|
571
|
+
before_visit do
|
572
|
+
a(1, 2, x: 1, y: 2)
|
573
|
+
b(1, 2, x: 1, y: 2)
|
574
|
+
end
|
575
|
+
end
|
576
|
+
|
577
|
+
let(:callback) { TestCallback.new }
|
578
|
+
|
579
|
+
it 'delegates to callback methods including kwargs' do
|
580
|
+
serialize(vm)
|
581
|
+
value(callback.a_args).must_equal([1, 2, { x: 1, y: 2 }])
|
582
|
+
value(callback.b_args).must_equal([[1, 2], { x: 1, y: 2 }])
|
583
|
+
end
|
584
|
+
end
|
585
|
+
|
557
586
|
describe 'provides details to the execution environment' do
|
558
587
|
class EnvCallback
|
559
588
|
include ViewModel::Callbacks
|
@@ -204,7 +204,7 @@ class ViewModel::RecordTest < ActiveSupport::TestCase
|
|
204
204
|
def self.included(base)
|
205
205
|
base.instance_eval do
|
206
206
|
it 'can serialize to the expected view' do
|
207
|
-
h = viewmodel_class.new(subject_model).
|
207
|
+
h = viewmodel_class.new(subject_model).serialize_to_hash
|
208
208
|
assert_equal(expected_view, h)
|
209
209
|
end
|
210
210
|
end
|
@@ -318,7 +318,7 @@ class ViewModel::RecordTest < ActiveSupport::TestCase
|
|
318
318
|
it 'raises correctly on an undeserializable value' do
|
319
319
|
bad_model = subject_model.tap { |m| m.moment = 2.7 }
|
320
320
|
ex = assert_raises(ViewModel::SerializationError) do
|
321
|
-
viewmodel_class.new(bad_model).
|
321
|
+
viewmodel_class.new(bad_model).serialize_to_hash
|
322
322
|
end
|
323
323
|
assert_match(/Could not serialize invalid value.*'moment'.*Incorrect type/, ex.detail)
|
324
324
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: iknow_view_models
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- iKnow Team
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-10
|
11
|
+
date: 2022-11-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -502,7 +502,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
502
502
|
requirements:
|
503
503
|
- - ">="
|
504
504
|
- !ruby/object:Gem::Version
|
505
|
-
version:
|
505
|
+
version: 2.7.3
|
506
506
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
507
507
|
requirements:
|
508
508
|
- - ">="
|