iknow_view_models 3.3.1 → 3.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/iknow_view_models/version.rb +1 -1
- data/lib/view_model.rb +5 -5
- data/lib/view_model/active_record.rb +8 -17
- data/lib/view_model/active_record/association_manipulation.rb +1 -4
- data/lib/view_model/active_record/cache.rb +6 -9
- data/lib/view_model/active_record/collection_nested_controller.rb +3 -3
- data/lib/view_model/active_record/controller.rb +4 -4
- data/lib/view_model/active_record/nested_controller_base.rb +5 -5
- data/test/unit/view_model/active_record/has_many_through_poly_test.rb +1 -1
- data/test/unit/view_model/active_record/has_many_through_test.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e0fb612b3ee4fc28b8bba11d40dfcd2da75775af0c8154a83a913f179824ccd
|
4
|
+
data.tar.gz: 6027d6f5a46e2654432e8c7b28cfcde8d2cb07f523efa7f87be199241246b83e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7301fdf85df162d86aaaa558e136dd1df2f047aec97abd084ba83369cac83b5d1b7904a8d0930c9cfb34a9c08aa49e835f89d0ea5c83eef007dbec4ff92a4b31
|
7
|
+
data.tar.gz: 861f4a2b0ab0eb4a48fa6ef1a2116307dd9d8409508e40c2534cb3e07b6dfe92e843f2eeb4904249d97326d83147eccf3b1757c0200b2ce796aa83eeb1eb3c0f
|
data/lib/view_model.rb
CHANGED
@@ -134,7 +134,7 @@ class ViewModel
|
|
134
134
|
# If this viewmodel represents an AR model, what associations does it make
|
135
135
|
# use of? Returns a includes spec appropriate for DeepPreloader, either as
|
136
136
|
# AR-style nested hashes or DeepPreloader::Spec.
|
137
|
-
def eager_includes(
|
137
|
+
def eager_includes(include_referenced: true)
|
138
138
|
{}
|
139
139
|
end
|
140
140
|
|
@@ -267,10 +267,10 @@ class ViewModel
|
|
267
267
|
Base64.urlsafe_encode64(hash, padding: false)
|
268
268
|
end
|
269
269
|
|
270
|
-
def preload_for_serialization(viewmodels,
|
270
|
+
def preload_for_serialization(viewmodels, include_referenced: true, lock: nil)
|
271
271
|
Array.wrap(viewmodels).group_by(&:class).each do |type, views|
|
272
272
|
DeepPreloader.preload(views.map(&:model),
|
273
|
-
type.eager_includes(
|
273
|
+
type.eager_includes(include_referenced: include_referenced),
|
274
274
|
lock: lock)
|
275
275
|
end
|
276
276
|
end
|
@@ -354,8 +354,8 @@ class ViewModel
|
|
354
354
|
context.for_child(self, association_name: member_name)
|
355
355
|
end
|
356
356
|
|
357
|
-
def preload_for_serialization(lock: nil
|
358
|
-
ViewModel.preload_for_serialization([self], lock: lock
|
357
|
+
def preload_for_serialization(lock: nil)
|
358
|
+
ViewModel.preload_for_serialization([self], lock: lock)
|
359
359
|
end
|
360
360
|
|
361
361
|
def ==(other)
|
@@ -135,7 +135,7 @@ class ViewModel::ActiveRecord < ViewModel::Record
|
|
135
135
|
end
|
136
136
|
|
137
137
|
## Load instances of the viewmodel by id(s)
|
138
|
-
def find(id_or_ids, scope: nil, lock: nil, eager_include: true
|
138
|
+
def find(id_or_ids, scope: nil, lock: nil, eager_include: true)
|
139
139
|
find_scope = self.model_class.all
|
140
140
|
find_scope = find_scope.order(:id).lock(lock) if lock
|
141
141
|
find_scope = find_scope.merge(scope) if scope
|
@@ -152,19 +152,19 @@ class ViewModel::ActiveRecord < ViewModel::Record
|
|
152
152
|
end
|
153
153
|
|
154
154
|
vms = models.map { |m| self.new(m) }
|
155
|
-
ViewModel.preload_for_serialization(vms, lock: lock
|
155
|
+
ViewModel.preload_for_serialization(vms, lock: lock) if eager_include
|
156
156
|
vms
|
157
157
|
end
|
158
158
|
end
|
159
159
|
|
160
160
|
## Load instances of the viewmodel by scope
|
161
161
|
## TODO: is this too much of a encapsulation violation?
|
162
|
-
def load(scope: nil, eager_include: true, lock: nil
|
162
|
+
def load(scope: nil, eager_include: true, lock: nil)
|
163
163
|
load_scope = self.model_class.all
|
164
164
|
load_scope = load_scope.lock(lock) if lock
|
165
165
|
load_scope = load_scope.merge(scope) if scope
|
166
166
|
vms = load_scope.map { |model| self.new(model) }
|
167
|
-
ViewModel.preload_for_serialization(vms, lock: lock
|
167
|
+
ViewModel.preload_for_serialization(vms, lock: lock) if eager_include
|
168
168
|
vms
|
169
169
|
end
|
170
170
|
|
@@ -184,7 +184,7 @@ class ViewModel::ActiveRecord < ViewModel::Record
|
|
184
184
|
# Constructs a preload specification of the required models for
|
185
185
|
# serializing/deserializing this view. Cycles in the schema will be broken
|
186
186
|
# after two layers of eager loading.
|
187
|
-
def eager_includes(
|
187
|
+
def eager_includes(include_referenced: true, vm_path: [])
|
188
188
|
association_specs = {}
|
189
189
|
|
190
190
|
return nil if vm_path.count(self) > 2
|
@@ -194,19 +194,10 @@ class ViewModel::ActiveRecord < ViewModel::Record
|
|
194
194
|
next unless association_data.is_a?(AssociationData)
|
195
195
|
next if association_data.external?
|
196
196
|
|
197
|
-
child_context =
|
198
|
-
if self.synthetic
|
199
|
-
serialize_context
|
200
|
-
elsif association_data.referenced?
|
201
|
-
serialize_context.for_references
|
202
|
-
else
|
203
|
-
serialize_context.for_child(nil, association_name: assoc_name)
|
204
|
-
end
|
205
|
-
|
206
197
|
case
|
207
198
|
when association_data.through?
|
208
199
|
viewmodel = association_data.direct_viewmodel
|
209
|
-
children = viewmodel.eager_includes(
|
200
|
+
children = viewmodel.eager_includes(include_referenced: include_referenced, vm_path: child_path)
|
210
201
|
|
211
202
|
when !include_referenced && association_data.referenced?
|
212
203
|
children = nil # Load up to the root viewmodel, but no further
|
@@ -215,13 +206,13 @@ class ViewModel::ActiveRecord < ViewModel::Record
|
|
215
206
|
children_by_klass = {}
|
216
207
|
association_data.viewmodel_classes.each do |vm_class|
|
217
208
|
klass = vm_class.model_class.name
|
218
|
-
children_by_klass[klass] = vm_class.eager_includes(
|
209
|
+
children_by_klass[klass] = vm_class.eager_includes(include_referenced: include_referenced, vm_path: child_path)
|
219
210
|
end
|
220
211
|
children = DeepPreloader::PolymorphicSpec.new(children_by_klass)
|
221
212
|
|
222
213
|
else
|
223
214
|
viewmodel = association_data.viewmodel_class
|
224
|
-
children = viewmodel.eager_includes(
|
215
|
+
children = viewmodel.eager_includes(include_referenced: include_referenced, vm_path: child_path)
|
225
216
|
end
|
226
217
|
|
227
218
|
association_specs[association_data.direct_reflection.name.to_s] = children
|
@@ -34,10 +34,7 @@ module ViewModel::ActiveRecord::AssociationManipulation
|
|
34
34
|
|
35
35
|
vms = association_scope.map { |model| associated_viewmodel.new(model) }
|
36
36
|
|
37
|
-
if eager_include
|
38
|
-
child_context = self.context_for_child(association_name, context: serialize_context)
|
39
|
-
ViewModel.preload_for_serialization(vms, serialize_context: child_context)
|
40
|
-
end
|
37
|
+
ViewModel.preload_for_serialization(vms) if eager_include
|
41
38
|
|
42
39
|
if association_data.collection?
|
43
40
|
vms
|
@@ -196,7 +196,7 @@ class ViewModel::ActiveRecord::Cache
|
|
196
196
|
# {id=>serialized_view}. Any references encountered are added to the
|
197
197
|
# worklist.
|
198
198
|
def load_from_cache(viewmodel_cache, ids)
|
199
|
-
cached_serializations = viewmodel_cache.load(ids, migrated_cache_version(viewmodel_cache)
|
199
|
+
cached_serializations = viewmodel_cache.load(ids, migrated_cache_version(viewmodel_cache))
|
200
200
|
|
201
201
|
cached_serializations.each_with_object({}) do |(id, cached_serialization), result|
|
202
202
|
add_refs_to_worklist(cached_serialization[:ref_cache])
|
@@ -255,8 +255,7 @@ class ViewModel::ActiveRecord::Cache
|
|
255
255
|
if viewmodel.class < CacheableView
|
256
256
|
cacheable_references = referenced_viewmodels.transform_values { |vm| cacheable_reference(vm) }
|
257
257
|
target_cache = viewmodel.class.viewmodel_cache
|
258
|
-
target_cache.store(viewmodel.id, migrated_cache_version(target_cache), data_serialization, cacheable_references
|
259
|
-
serialize_context: serialize_context)
|
258
|
+
target_cache.store(viewmodel.id, migrated_cache_version(target_cache), data_serialization, cacheable_references)
|
260
259
|
end
|
261
260
|
|
262
261
|
result[viewmodel.id] = data_serialization
|
@@ -279,15 +278,13 @@ class ViewModel::ActiveRecord::Cache
|
|
279
278
|
if ids.present?
|
280
279
|
found = viewmodel_class.find(ids,
|
281
280
|
eager_include: false,
|
282
|
-
lock: 'FOR SHARE'
|
283
|
-
serialize_context: serialize_context)
|
281
|
+
lock: 'FOR SHARE')
|
284
282
|
viewmodels.concat(found)
|
285
283
|
end
|
286
284
|
|
287
285
|
ViewModel.preload_for_serialization(viewmodels,
|
288
286
|
include_referenced: false,
|
289
|
-
lock: 'FOR SHARE'
|
290
|
-
serialize_context: serialize_context)
|
287
|
+
lock: 'FOR SHARE')
|
291
288
|
|
292
289
|
viewmodels
|
293
290
|
end
|
@@ -335,12 +332,12 @@ class ViewModel::ActiveRecord::Cache
|
|
335
332
|
end
|
336
333
|
|
337
334
|
# Save the provided serialization and reference data in the cache
|
338
|
-
def store(id, migration_version, data_serialization, ref_cache
|
335
|
+
def store(id, migration_version, data_serialization, ref_cache)
|
339
336
|
key = key_for(id, migration_version)
|
340
337
|
cache_for(migration_version).write(key, { data: data_serialization, ref_cache: ref_cache })
|
341
338
|
end
|
342
339
|
|
343
|
-
def load(ids, migration_version
|
340
|
+
def load(ids, migration_version)
|
344
341
|
keys = ids.map { |id| key_for(id, migration_version) }
|
345
342
|
results = cache_for(migration_version).read_multi(keys)
|
346
343
|
results.transform_keys! { |key| id_for(key) }
|
@@ -50,7 +50,7 @@ module ViewModel::ActiveRecord::CollectionNestedController
|
|
50
50
|
raise ViewModel::DeserializationError::InvalidSyntax.new('Can not provide both `before` and `after` anchors for a collection append')
|
51
51
|
end
|
52
52
|
|
53
|
-
owner_view = owner_viewmodel.find(owner_viewmodel_id, eager_include: false, lock: lock_owner
|
53
|
+
owner_view = owner_viewmodel.find(owner_viewmodel_id, eager_include: false, lock: lock_owner)
|
54
54
|
|
55
55
|
assoc_view = owner_view.append_associated(association_name,
|
56
56
|
update_hash,
|
@@ -60,7 +60,7 @@ module ViewModel::ActiveRecord::CollectionNestedController
|
|
60
60
|
deserialize_context: deserialize_context)
|
61
61
|
ViewModel::Callbacks.wrap_serialize(owner_view, context: serialize_context) do
|
62
62
|
child_context = owner_view.context_for_child(association_name, context: serialize_context)
|
63
|
-
ViewModel.preload_for_serialization(assoc_view
|
63
|
+
ViewModel.preload_for_serialization(assoc_view)
|
64
64
|
assoc_view = yield(assoc_view) if block_given?
|
65
65
|
prerender_viewmodel(assoc_view, serialize_context: child_context)
|
66
66
|
end
|
@@ -71,7 +71,7 @@ module ViewModel::ActiveRecord::CollectionNestedController
|
|
71
71
|
|
72
72
|
def disassociate(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context, lock_owner: nil)
|
73
73
|
owner_viewmodel.transaction do
|
74
|
-
owner_view = owner_viewmodel.find(owner_viewmodel_id, eager_include: false, lock: lock_owner
|
74
|
+
owner_view = owner_viewmodel.find(owner_viewmodel_id, eager_include: false, lock: lock_owner)
|
75
75
|
owner_view.delete_associated(association_name, viewmodel_id, type: viewmodel_class, deserialize_context: deserialize_context)
|
76
76
|
render_viewmodel(nil)
|
77
77
|
end
|
@@ -22,7 +22,7 @@ module ViewModel::ActiveRecord::Controller
|
|
22
22
|
def show(scope: nil, viewmodel_class: self.viewmodel_class, serialize_context: new_serialize_context(viewmodel_class: viewmodel_class))
|
23
23
|
view = nil
|
24
24
|
pre_rendered = viewmodel_class.transaction do
|
25
|
-
view = viewmodel_class.find(viewmodel_id, scope: scope
|
25
|
+
view = viewmodel_class.find(viewmodel_id, scope: scope)
|
26
26
|
view = yield(view) if block_given?
|
27
27
|
prerender_viewmodel(view, serialize_context: serialize_context)
|
28
28
|
end
|
@@ -33,7 +33,7 @@ module ViewModel::ActiveRecord::Controller
|
|
33
33
|
def index(scope: nil, viewmodel_class: self.viewmodel_class, serialize_context: new_serialize_context(viewmodel_class: viewmodel_class))
|
34
34
|
views = nil
|
35
35
|
pre_rendered = viewmodel_class.transaction do
|
36
|
-
views = viewmodel_class.load(scope: scope
|
36
|
+
views = viewmodel_class.load(scope: scope)
|
37
37
|
views = yield(views) if block_given?
|
38
38
|
prerender_viewmodel(views, serialize_context: serialize_context)
|
39
39
|
end
|
@@ -47,7 +47,7 @@ module ViewModel::ActiveRecord::Controller
|
|
47
47
|
view = nil
|
48
48
|
pre_rendered = viewmodel_class.transaction do
|
49
49
|
view = viewmodel_class.deserialize_from_view(update_hash, references: refs, deserialize_context: deserialize_context)
|
50
|
-
ViewModel.preload_for_serialization(view
|
50
|
+
ViewModel.preload_for_serialization(view)
|
51
51
|
view = yield(view) if block_given?
|
52
52
|
prerender_viewmodel(view, serialize_context: serialize_context)
|
53
53
|
end
|
@@ -57,7 +57,7 @@ module ViewModel::ActiveRecord::Controller
|
|
57
57
|
|
58
58
|
def destroy(serialize_context: new_serialize_context, deserialize_context: new_deserialize_context)
|
59
59
|
viewmodel_class.transaction do
|
60
|
-
view = viewmodel_class.find(viewmodel_id, eager_include: false
|
60
|
+
view = viewmodel_class.find(viewmodel_id, eager_include: false)
|
61
61
|
view.destroy!(deserialize_context: deserialize_context)
|
62
62
|
end
|
63
63
|
render_viewmodel(nil)
|
@@ -12,10 +12,10 @@ module ViewModel::ActiveRecord::NestedControllerBase
|
|
12
12
|
def show_association(scope: nil, serialize_context: new_serialize_context, lock_owner: nil)
|
13
13
|
associated_views = nil
|
14
14
|
pre_rendered = owner_viewmodel.transaction do
|
15
|
-
owner_view = owner_viewmodel.find(owner_viewmodel_id, eager_include: false, lock: lock_owner
|
15
|
+
owner_view = owner_viewmodel.find(owner_viewmodel_id, eager_include: false, lock: lock_owner)
|
16
16
|
ViewModel::Callbacks.wrap_serialize(owner_view, context: serialize_context) do
|
17
17
|
# Association manipulation methods construct child contexts internally
|
18
|
-
associated_views = owner_view.load_associated(association_name, scope: scope
|
18
|
+
associated_views = owner_view.load_associated(association_name, scope: scope)
|
19
19
|
|
20
20
|
associated_views = yield(associated_views) if block_given?
|
21
21
|
|
@@ -32,7 +32,7 @@ module ViewModel::ActiveRecord::NestedControllerBase
|
|
32
32
|
pre_rendered = owner_viewmodel.transaction do
|
33
33
|
update_hash, refs = parse_viewmodel_updates
|
34
34
|
|
35
|
-
owner_view = owner_viewmodel.find(owner_viewmodel_id, eager_include: false, lock: lock_owner
|
35
|
+
owner_view = owner_viewmodel.find(owner_viewmodel_id, eager_include: false, lock: lock_owner)
|
36
36
|
|
37
37
|
association_view = owner_view.replace_associated(association_name, update_hash,
|
38
38
|
references: refs,
|
@@ -40,7 +40,7 @@ module ViewModel::ActiveRecord::NestedControllerBase
|
|
40
40
|
|
41
41
|
ViewModel::Callbacks.wrap_serialize(owner_view, context: serialize_context) do
|
42
42
|
child_context = owner_view.context_for_child(association_name, context: serialize_context)
|
43
|
-
ViewModel.preload_for_serialization(association_view
|
43
|
+
ViewModel.preload_for_serialization(association_view)
|
44
44
|
association_view = yield(association_view) if block_given?
|
45
45
|
prerender_viewmodel(association_view, serialize_context: child_context)
|
46
46
|
end
|
@@ -51,7 +51,7 @@ module ViewModel::ActiveRecord::NestedControllerBase
|
|
51
51
|
|
52
52
|
def destroy_association(collection, serialize_context: new_serialize_context, deserialize_context: new_deserialize_context, lock_owner: nil)
|
53
53
|
if lock_owner
|
54
|
-
owner_viewmodel.find(owner_viewmodel_id, eager_include: false, lock: lock_owner
|
54
|
+
owner_viewmodel.find(owner_viewmodel_id, eager_include: false, lock: lock_owner)
|
55
55
|
end
|
56
56
|
|
57
57
|
empty_update = collection ? [] : nil
|
@@ -118,7 +118,7 @@ class ViewModel::ActiveRecord::HasManyThroughPolyTest < ActiveSupport::TestCase
|
|
118
118
|
def test_loading_batching
|
119
119
|
context = ParentView.new_serialize_context
|
120
120
|
log_queries do
|
121
|
-
parent_views = ParentView.load
|
121
|
+
parent_views = ParentView.load
|
122
122
|
serialize(parent_views, serialize_context: context)
|
123
123
|
end
|
124
124
|
|
@@ -112,7 +112,7 @@ class ViewModel::ActiveRecord::HasManyThroughTest < ActiveSupport::TestCase
|
|
112
112
|
def test_loading_batching
|
113
113
|
context = ParentView.new_serialize_context
|
114
114
|
log_queries do
|
115
|
-
parent_views = ParentView.load
|
115
|
+
parent_views = ParentView.load
|
116
116
|
serialize(parent_views, serialize_context: context)
|
117
117
|
end
|
118
118
|
|
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.4.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: 2021-
|
11
|
+
date: 2021-06-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|