iknow_view_models 2.8.6 → 2.8.7
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/controller.rb +1 -8
- data/lib/view_model.rb +15 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f43951463af8b4ed1a7de702c16736beb1b94b89cb90cf9ea13cff64d1f2de2e
|
4
|
+
data.tar.gz: c3b76cbc92f47a2584be1722c6ea5efc2b9d723b42ee76b07acffa7f23291318
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba99a68fe5d752c6b75f117928374f4f10f5075adef4df43c1292d172d2d7820d1900bf97416913672f9a7a59f082c4f402f8fe4ff166309695f009aa09c9d2a
|
7
|
+
data.tar.gz: b19b5d89503ce5e2000951d7389e327d5c24d55eee6118910a0df09c3be3c597d2349cc065f42a4df69273547ac32e5255fcf8b4ba1b7bc3489513dee7ee2b5d
|
@@ -110,14 +110,7 @@ module ViewModel::Controller
|
|
110
110
|
yield json
|
111
111
|
end
|
112
112
|
|
113
|
-
|
114
|
-
# the context of ActiveSupport, we don't want this, because AS replaces the
|
115
|
-
# .to_json interface with its own .as_json, which demands that everything is
|
116
|
-
# reduced to a Hash before it can be JSON encoded. Using this is not only
|
117
|
-
# slightly more expensive in terms of allocations, but also defeats the
|
118
|
-
# purpose of our precompiled `CompiledJson` terminals. Instead serialize
|
119
|
-
# using OJ with options equivalent to those used by MultiJson.
|
120
|
-
Oj.dump(builder.attributes!, mode: :compat, time_format: :ruby, use_to_json: true)
|
113
|
+
ViewModel.encode_json(builder.attributes!)
|
121
114
|
end
|
122
115
|
|
123
116
|
def render_jbuilder(status:)
|
data/lib/view_model.rb
CHANGED
@@ -155,6 +155,17 @@ class ViewModel
|
|
155
155
|
Jbuilder.new { |json| serialize(viewmodel, json, serialize_context: serialize_context) }.attributes!
|
156
156
|
end
|
157
157
|
|
158
|
+
def encode_json(value)
|
159
|
+
# Jbuilder#encode no longer uses MultiJson, but instead calls `.to_json`. In
|
160
|
+
# the context of ActiveSupport, we don't want this, because AS replaces the
|
161
|
+
# .to_json interface with its own .as_json, which demands that everything is
|
162
|
+
# reduced to a Hash before it can be JSON encoded. Using this is not only
|
163
|
+
# slightly more expensive in terms of allocations, but also defeats the
|
164
|
+
# purpose of our precompiled `CompiledJson` terminals. Instead serialize
|
165
|
+
# using OJ with options equivalent to those used by MultiJson.
|
166
|
+
Oj.dump(value, mode: :compat, time_format: :ruby, use_to_json: true)
|
167
|
+
end
|
168
|
+
|
158
169
|
# Rebuild this viewmodel from a serialized hash.
|
159
170
|
def deserialize_from_view(hash_data, references: {}, deserialize_context: new_deserialize_context)
|
160
171
|
viewmodel = self.new
|
@@ -241,6 +252,10 @@ class ViewModel
|
|
241
252
|
Jbuilder.new { |json| serialize(json, serialize_context: serialize_context) }.attributes!
|
242
253
|
end
|
243
254
|
|
255
|
+
def to_json(serialize_context: self.class.new_serialize_context)
|
256
|
+
ViewModel.encode_json(self.to_hash(serialize_context: serialize_context))
|
257
|
+
end
|
258
|
+
|
244
259
|
# Render this viewmodel to a jBuilder. Usually overridden in subclasses.
|
245
260
|
# Default implementation visits each attribute with Viewmodel.serialize.
|
246
261
|
def serialize_view(json, serialize_context: self.class.new_serialize_context)
|