Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable components to use @request and request methods/ivars #2135

Merged
merged 6 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading