iknow_view_models 3.6.7 → 3.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c673bb5888e36eb62ba3ac6ece035d8cda3203cb10bdf19716b66454df4b20f1
4
- data.tar.gz: 157092fa9b7e5e6a815981a144566945ddb342bf3c108d3d788a0e476ed2dc84
3
+ metadata.gz: 4b0920a06f9988d3cc846a4bf1fb592525891adb09ba44e3e7ccded40ed18c2b
4
+ data.tar.gz: 70267048408dbef5eaeb162465ffb2583e93fcd562f5d75d38c0ea7c7dca096d
5
5
  SHA512:
6
- metadata.gz: 546321a01ca0f1bee84d7607b44f93124cf76353c28868b047ffcab66d976e2eeec6287e6b505d68b302ef14ea4eb9b69941596ce9dd8d0437ff6e6b3435cdd3
7
- data.tar.gz: e95d5d0b60defa73d7ebc30dabc0e390acdac733d2f95ed888c3f6001f0330565268ab99c4003a64ac23b092716140f99012af5adb75a0801ce08f901980c7b8
6
+ metadata.gz: a47fe00e8aaac20252a8508438adcf916946f4f5e4e34beabf342d530a30e5b9dbd1837c3422cd38fec8c60ecb0c0e95d1aae76a45b47fde8a66340b5346c3b4
7
+ data.tar.gz: d327bfab6f0c9a985aa4b44995deb51879b9e854d3bec028ebe48dc3eedd4d14158a20e960f16ab7dfcffb80d3ce4d11979f2fb46833e91a07be3f23d056fbc3
@@ -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'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IknowViewModels
4
- VERSION = '3.6.7'
4
+ VERSION = '3.7.0'
5
5
  end
@@ -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, *args, &block)
14
+ def method_missing(method, ...)
15
15
  if _callbacks.respond_to?(method, true)
16
- _callbacks.send(method, *args, &block)
16
+ _callbacks.send(method, ...)
17
17
  else
18
18
  super
19
19
  end
data/lib/view_model.rb CHANGED
@@ -303,12 +303,12 @@ class ViewModel
303
303
  end
304
304
  end
305
305
 
306
- def to_hash(serialize_context: self.class.new_serialize_context)
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.to_hash(serialize_context: serialize_context))
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).to_hash(serialize_context: serialize_context)
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).to_hash(serialize_context: serialize_context)
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
@@ -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).to_hash } },
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).to_hash } },
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).to_hash },
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).to_hash }
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).to_hash },
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).to_hash },
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).to_hash },
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).to_hash },
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).to_hash },
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).to_hash },
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).to_hash,
475
+ 'update' => TargetView.new(target).serialize_to_hash,
476
476
  },
477
477
  {
478
478
  'id' => other_parent.id,
479
- 'update' => TargetView.new(other_target).to_hash,
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).to_hash,
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).to_hash,
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.to_hash },
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.to_hash
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.to_hash, p2_view.to_hash] })
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.to_hash }, parentcontroller.hash_response)
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.to_hash },
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.to_hash)
286
+ assert_equal(@simplesection_view, v.serialize_to_hash)
287
287
 
288
288
  v = SectionView.new(@quizsection)
289
- assert_equal(@quizsection_view, v.to_hash)
289
+ assert_equal(@quizsection_view, v.serialize_to_hash)
290
290
 
291
291
  v = SectionView.new(@vocabsection)
292
- assert_equal(@vocabsection_view, v.to_hash)
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).to_hash
473
- view['children'] << child_viewmodel_class.new(moved_child).to_hash
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).to_hash
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).to_hash
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).to_hash
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).to_hash
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,
@@ -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.to_hash(serialize_context: no_view_context)
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).to_hash.merge('name' => 'p2')
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).to_hash.merge('name' => 'p2')
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).to_hash
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).to_hash
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.6.7
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-24 00:00:00.000000000 Z
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: '2.7'
505
+ version: 2.7.3
506
506
  required_rubygems_version: !ruby/object:Gem::Requirement
507
507
  requirements:
508
508
  - - ">="