iknow_view_models 3.6.6 → 3.6.7

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: eb322edf3edec85adb744dba24cab64e15914716f16cfa5ec6bb75de5fcb8e33
4
- data.tar.gz: 4b41f0f4ba1fa9a3d7568ee8b0005c4139e8186c91244c7aa04ff8b3bae19fce
3
+ metadata.gz: c673bb5888e36eb62ba3ac6ece035d8cda3203cb10bdf19716b66454df4b20f1
4
+ data.tar.gz: 157092fa9b7e5e6a815981a144566945ddb342bf3c108d3d788a0e476ed2dc84
5
5
  SHA512:
6
- metadata.gz: a51e6a510110c90e3b4a77d0f3da2df2191e68d12137cd9ab040a97125f799e8292ae86c9b53f39b85ebb589d785fa791e8bf56883b122d6b08bfc15b2a0f702
7
- data.tar.gz: 03c790566b0304ab4333e632fd81ba8886425ee8a45f1edb2fc5c777af7e2bcbfccecaa947c8a3001d47711ed8dee4132dcb4b52284cd799b915e147de9daf35
6
+ metadata.gz: 546321a01ca0f1bee84d7607b44f93124cf76353c28868b047ffcab66d976e2eeec6287e6b505d68b302ef14ea4eb9b69941596ce9dd8d0437ff6e6b3435cdd3
7
+ data.tar.gz: e95d5d0b60defa73d7ebc30dabc0e390acdac733d2f95ed888c3f6001f0330565268ab99c4003a64ac23b092716140f99012af5adb75a0801ce08f901980c7b8
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IknowViewModels
4
- VERSION = '3.6.6'
4
+ VERSION = '3.6.7'
5
5
  end
@@ -37,8 +37,15 @@ class ViewModel
37
37
  end
38
38
 
39
39
  def migrate!(serialization)
40
- migrate_tree!(serialization, references: serialization['references'] || {})
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
@@ -143,8 +143,8 @@ module ViewModelSpecHelpers
143
143
 
144
144
  module ParentAndChildMigrations
145
145
  extend ActiveSupport::Concern
146
-
147
- def model_attributes
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) {
@@ -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
- refs = ctx.serialize_references_to_hash
34
- { 'data' => view, 'references' => refs }
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(:subject_references) { v2_serialization_references.deep_dup }
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
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.6
4
+ version: 3.6.7
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-05 00:00:00.000000000 Z
11
+ date: 2022-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack