view_component 4.0.1 → 4.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/docs/CHANGELOG.md +7 -0
- data/lib/view_component/base.rb +19 -4
- data/lib/view_component/test_helpers.rb +13 -3
- data/lib/view_component/version.rb +1 -1
- 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: 05c8fa66735f8008894ba88d64885dc307bc71381e2b88391798fa4266da7279
|
4
|
+
data.tar.gz: f217babfa1c1894f377fff73fc3b603755122422c8596e349ffd0d8689e86708
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a10d8362a64c181f11dd0aff69725eb7045a4a4cb546c2e960183bf416083dc8b5bcee76c807be3341dd01919eba3fdc2636083de2794101d8751f1c405396a1
|
7
|
+
data.tar.gz: 2ae8c40ff912472d16d27e3a92af772905d74b3ab010ced00578208ee281f53f59adc31bc0ef0b170cf1ea8c64cd0750db0c0804bb65deca11a33f430cc7012d
|
data/docs/CHANGELOG.md
CHANGED
@@ -10,6 +10,13 @@ nav_order: 6
|
|
10
10
|
|
11
11
|
## main
|
12
12
|
|
13
|
+
## 4.0.2
|
14
|
+
|
15
|
+
* Share the view context in tests to prevent out-of-order rendering issues for certain advanced use-cases, eg. testing instances of Rails' `FormBuilder`.
|
16
|
+
* Fix double rendering issue for partials that yield.
|
17
|
+
|
18
|
+
*Cameron Dutro*
|
19
|
+
|
13
20
|
## 4.0.1
|
14
21
|
|
15
22
|
* Setup Trusted Publishing to RubyGems to improve software supply chain safety.
|
data/lib/view_component/base.rb
CHANGED
@@ -246,17 +246,32 @@ module ViewComponent
|
|
246
246
|
|
247
247
|
# Re-use original view_context if we're not rendering a component.
|
248
248
|
#
|
249
|
-
#
|
250
|
-
#
|
251
|
-
#
|
249
|
+
# As of v4, ViewComponent::Base re-uses the existing view context created
|
250
|
+
# by ActionView, meaning the current view context and the original view
|
251
|
+
# context are the same object. set_original_view_context is still called
|
252
|
+
# to maintain backwards compatibility.
|
252
253
|
#
|
253
254
|
# @private
|
254
255
|
def render(options = {}, args = {}, &block)
|
255
256
|
if options.respond_to?(:set_original_view_context)
|
256
257
|
options.set_original_view_context(self.__vc_original_view_context)
|
258
|
+
|
259
|
+
# We assume options is a component, so there's no need to evaluate the
|
260
|
+
# block in the view context as we do below.
|
257
261
|
@view_context.render(options, args, &block)
|
262
|
+
elsif block
|
263
|
+
__vc_original_view_context.render(options, args) do
|
264
|
+
# Partials are rendered to their own buffer and do not append to the
|
265
|
+
# original @output_buffer we retain a reference to in #render_in. This
|
266
|
+
# is a problem since the block passed to us here in the #render method
|
267
|
+
# is evaluated within the context of ViewComponent::Base, and thus
|
268
|
+
# appends to the original @output_buffer. To avoid this, we evaluate the
|
269
|
+
# block in the view context instead, which will append to the output buffer
|
270
|
+
# created for the partial.
|
271
|
+
__vc_original_view_context.instance_exec(&block)
|
272
|
+
end
|
258
273
|
else
|
259
|
-
__vc_original_view_context.render(options, args
|
274
|
+
__vc_original_view_context.render(options, args)
|
260
275
|
end
|
261
276
|
end
|
262
277
|
|
@@ -38,9 +38,19 @@ module ViewComponent
|
|
38
38
|
# @return [Nokogiri::HTML5]
|
39
39
|
def render_inline(component, **args, &block)
|
40
40
|
@page = nil
|
41
|
-
@rendered_content =
|
41
|
+
@rendered_content = vc_test_view_context.render(component, args, &block)
|
42
42
|
|
43
|
-
Nokogiri::HTML5.fragment(@rendered_content)
|
43
|
+
fragment = Nokogiri::HTML5.fragment(@rendered_content)
|
44
|
+
@vc_test_view_context = nil
|
45
|
+
fragment
|
46
|
+
end
|
47
|
+
|
48
|
+
# Returns the view context used to render components in tests. Note that the view context
|
49
|
+
# is reset after each call to `render_inline`.
|
50
|
+
#
|
51
|
+
# @return [ActionView::Base]
|
52
|
+
def vc_test_view_context
|
53
|
+
@vc_test_view_context ||= vc_test_controller.view_context
|
44
54
|
end
|
45
55
|
|
46
56
|
# `JSON.parse`-d component output.
|
@@ -103,7 +113,7 @@ module ViewComponent
|
|
103
113
|
# ```
|
104
114
|
def render_in_view_context(...)
|
105
115
|
@page = nil
|
106
|
-
@rendered_content =
|
116
|
+
@rendered_content = vc_test_view_context.instance_exec(...)
|
107
117
|
Nokogiri::HTML5.fragment(@rendered_content)
|
108
118
|
end
|
109
119
|
|