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

Set request full path with request url #1893

Merged
merged 1 commit into from
Nov 1, 2023
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

* Fix bug where `#with_request_url`, set the incorrect `request.fullpath`.

*Nachiket Pusalkar*

## 3.7.0

* Support Rails 7.1 in CI.
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ ViewComponents are Ruby objects, making it easy to follow (and enforce) code qua

ViewComponent is built by over a hundred members of the community, including:

<img src="https://avatars.githubusercontent.com/nachiket87?s=64" alt="nachiket87" width="32" />
<img src="https://avatars.githubusercontent.com/andrewjtait?s=64" alt="andrewjtait" width="32" />
<img src="https://avatars.githubusercontent.com/asgerb?s=64" alt="asgerb" width="32" />
<img src="https://avatars.githubusercontent.com/bbugh?s=64" alt="bbugh" width="32" />
Expand Down
6 changes: 4 additions & 2 deletions lib/view_component/test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,17 @@ def with_controller_class(klass)
#
# @param path [String] The path to set for the current request.
# @param host [String] The host to set for the current request.
def with_request_url(path, host: nil)
def with_request_url(full_path, host: nil)
old_request_host = vc_test_request.host
old_request_path_info = vc_test_request.path_info
old_request_path_parameters = vc_test_request.path_parameters
old_request_query_parameters = vc_test_request.query_parameters
old_request_query_string = vc_test_request.query_string
old_controller = defined?(@vc_test_controller) && @vc_test_controller

path, query = path.split("?", 2)
path, query = full_path.split("?", 2)
vc_test_request.instance_variable_set(:@fullpath, full_path)
vc_test_request.instance_variable_set(:@original_fullpath, full_path)
vc_test_request.host = host if host
vc_test_request.path_info = path
vc_test_request.path_parameters = Rails.application.routes.recognize_path_with_request(vc_test_request, path, {})
Expand Down
7 changes: 7 additions & 0 deletions test/sandbox/app/components/request_url_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class RequestUrlComponent < ViewComponent::Base
def call
content_tag(:span, request.path, class: "path") + content_tag(:span, request.fullpath, class: "fullpath")
end
end
28 changes: 28 additions & 0 deletions test/sandbox/test/components/request_url_component_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require "test_helper"

class RequestUrlComponentTest < ViewComponent::TestCase
test "should print path and fullpath" do
with_request_url "/products" do
render_inline(RequestUrlComponent.new)

assert_selector ".path", text: "/products"
assert_selector ".fullpath", text: "/products"
end

with_request_url "/slots?param=1&paramtwo=2" do
render_inline(RequestUrlComponent.new)

assert_selector ".path", text: "/slots"
assert_selector ".fullpath", text: "/slots?param=1&paramtwo=2"
end

with_request_url "/slots" do
render_inline(RequestUrlComponent.new)

assert_selector ".path", text: "/slots"
assert_selector ".fullpath", text: "/slots"
end
end
end