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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6c8e5b159f5df04cb7bb7e4477670ef3953ec682d7167658e1a4d180302d5366
4
- data.tar.gz: bfdfb839583d7e7e27f020fcce04baefeaad676e1ca3ae23864e633e343d5b26
3
+ metadata.gz: 05c8fa66735f8008894ba88d64885dc307bc71381e2b88391798fa4266da7279
4
+ data.tar.gz: f217babfa1c1894f377fff73fc3b603755122422c8596e349ffd0d8689e86708
5
5
  SHA512:
6
- metadata.gz: 2f262abd83ace33712a19c8fa38d6aff0c1853c59647c8edd916a079e6398537ae3201eb9fd5cf1ad08836d9835868d0fae52aff307609220930bf59040c8976
7
- data.tar.gz: 303778089d5c4621a1ac038b0a240552332cef8e8acc7c1b08ede206b03b51a11c7727a6e42714db307844f86ec2c78e519eaf5fad49fcac461e8f90cc0e73ee
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.
@@ -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
- # This prevents an exception when rendering a partial inside of a component that has also been rendered outside
250
- # of the component. This is due to the partials compiled template method existing in the parent `view_context`,
251
- # and not the component's `view_context`.
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, &block)
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 = vc_test_controller.view_context.render(component, args, &block)
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 = vc_test_controller.view_context.instance_exec(...)
116
+ @rendered_content = vc_test_view_context.instance_exec(...)
107
117
  Nokogiri::HTML5.fragment(@rendered_content)
108
118
  end
109
119
 
@@ -4,7 +4,7 @@ module ViewComponent
4
4
  module VERSION
5
5
  MAJOR = 4
6
6
  MINOR = 0
7
- PATCH = 1
7
+ PATCH = 2
8
8
  PRE = nil
9
9
 
10
10
  STRING = [MAJOR, MINOR, PATCH, PRE].compact.join(".")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: view_component
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.1
4
+ version: 4.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - ViewComponent Team