Skip to content

Commit

Permalink
Enable components to use @request and request methods/ivars (#2135)
Browse files Browse the repository at this point in the history
* 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 <blake@blakewilliams.me>
  • Loading branch information
joelhawksley and BlakeWilliams authored Oct 16, 2024
1 parent 1176505 commit d0576c1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ nav_order: 5

## main

* Enable components to use `@request` and `request` methods/ivars.

*Blake Williams*

* Fix bug where implicit locales in component filenames threw a `NameError`.

*Chloe Fons*
Expand Down
17 changes: 12 additions & 5 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ def render_in(view_context, &block)

if render?
rendered_template =
if compiler.renders_template_for?(@__vc_variant, request&.format&.to_sym)
render_template_for(@__vc_variant, request&.format&.to_sym)
if compiler.renders_template_for?(@__vc_variant, __vc_request&.format&.to_sym)
render_template_for(@__vc_variant, __vc_request&.format&.to_sym)
else
maybe_escape_html(render_template_for(@__vc_variant, request&.format&.to_sym)) do
maybe_escape_html(render_template_for(@__vc_variant, __vc_request&.format&.to_sym)) do
Kernel.warn("WARNING: The #{self.class} component rendered HTML-unsafe output. The output will be automatically escaped, but you may want to investigate.")
end
end.to_s
Expand Down Expand Up @@ -286,7 +286,14 @@ def format
#
# @return [ActionDispatch::Request]
def request
@request ||= controller.request if controller.respond_to?(:request)
__vc_request
end

# Enables consumers to override request/@request
#
# @private
def __vc_request
@__vc_request ||= controller.request if controller.respond_to?(:request)
end

# The content passed to the component instance as a block.
Expand Down Expand Up @@ -328,7 +335,7 @@ def content_evaluated?
end

def maybe_escape_html(text)
return text if request && !request.format.html?
return text if __vc_request && !__vc_request.format.html?
return text if text.blank?

if text.html_safe?
Expand Down
9 changes: 9 additions & 0 deletions test/sandbox/app/components/request_param_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class RequestParamComponent < ViewComponent::Base
def initialize(request:)
@request = request
end

def call
@request
end
end
6 changes: 6 additions & 0 deletions test/sandbox/test/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1221,4 +1221,10 @@ def test_localised_component

assert_selector("div", text: "salut,monde!")
end

def test_request_param
render_inline(RequestParamComponent.new(request: "foo"))

assert_text("foo")
end
end

0 comments on commit d0576c1

Please # to comment.