Skip to content

Commit d0576c1

Browse files
Enable components to use @request and request methods/ivars (#2135)
* Enable components to use `@request` and `request` methods/ivars This replaces internal uses of `request` with `__vc_request` so any component that uses a request method or ivar will continue to work. * Update lib/view_component/base.rb * add test case * add changelog, fix test case * fix newline --------- Co-authored-by: Blake Williams <[email protected]>
1 parent 1176505 commit d0576c1

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ nav_order: 5
1010

1111
## main
1212

13+
* Enable components to use `@request` and `request` methods/ivars.
14+
15+
*Blake Williams*
16+
1317
* Fix bug where implicit locales in component filenames threw a `NameError`.
1418

1519
*Chloe Fons*

lib/view_component/base.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ def render_in(view_context, &block)
109109

110110
if render?
111111
rendered_template =
112-
if compiler.renders_template_for?(@__vc_variant, request&.format&.to_sym)
113-
render_template_for(@__vc_variant, request&.format&.to_sym)
112+
if compiler.renders_template_for?(@__vc_variant, __vc_request&.format&.to_sym)
113+
render_template_for(@__vc_variant, __vc_request&.format&.to_sym)
114114
else
115-
maybe_escape_html(render_template_for(@__vc_variant, request&.format&.to_sym)) do
115+
maybe_escape_html(render_template_for(@__vc_variant, __vc_request&.format&.to_sym)) do
116116
Kernel.warn("WARNING: The #{self.class} component rendered HTML-unsafe output. The output will be automatically escaped, but you may want to investigate.")
117117
end
118118
end.to_s
@@ -286,7 +286,14 @@ def format
286286
#
287287
# @return [ActionDispatch::Request]
288288
def request
289-
@request ||= controller.request if controller.respond_to?(:request)
289+
__vc_request
290+
end
291+
292+
# Enables consumers to override request/@request
293+
#
294+
# @private
295+
def __vc_request
296+
@__vc_request ||= controller.request if controller.respond_to?(:request)
290297
end
291298

292299
# The content passed to the component instance as a block.
@@ -328,7 +335,7 @@ def content_evaluated?
328335
end
329336

330337
def maybe_escape_html(text)
331-
return text if request && !request.format.html?
338+
return text if __vc_request && !__vc_request.format.html?
332339
return text if text.blank?
333340

334341
if text.html_safe?
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class RequestParamComponent < ViewComponent::Base
2+
def initialize(request:)
3+
@request = request
4+
end
5+
6+
def call
7+
@request
8+
end
9+
end

test/sandbox/test/rendering_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,4 +1221,10 @@ def test_localised_component
12211221

12221222
assert_selector("div", text: "salut,monde!")
12231223
end
1224+
1225+
def test_request_param
1226+
render_inline(RequestParamComponent.new(request: "foo"))
1227+
1228+
assert_text("foo")
1229+
end
12241230
end

0 commit comments

Comments
 (0)