Skip to content

Commit

Permalink
Pass in query params by default
Browse files Browse the repository at this point in the history
Before, default `src` was set using `request.path` which returns virtual
path without query string.

This PR changes default implementation to use `request.fullpath` which
returns virtual path with query string.

Form example; In our app we have one controller endpoint where we filter
the results not just base on user entry `q` but also query strings.
  • Loading branch information
dixpac committed Apr 6, 2024
1 parent 01e7dae commit 0660671
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/hotwire_combobox/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def hw_combobox_options(
def hw_paginated_combobox_options(
options,
for_id: params[:for_id],
src: request.path,
src: request.fullpath,
next_page: nil,
render_in: {},
include_blank: {},
Expand Down Expand Up @@ -166,7 +166,7 @@ def hw_combobox_page_stream_action

def hw_uri_with_params(url_or_path, **params)
URI.parse(url_or_path).tap do |url_or_path|
query = URI.decode_www_form(url_or_path.query || "").to_h.merge(params)
query = URI.decode_www_form(url_or_path.query || "").to_h.merge(params.stringify_keys)
url_or_path.query = URI.encode_www_form query
end.to_s
rescue URI::InvalidURIError
Expand Down
13 changes: 13 additions & 0 deletions test/helpers/hotwire_combobox/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,17 @@ class HotwireCombobox::HelperTest < ApplicationViewTestCase
combobox_tag :foo, multiselect_chip_src: "some_path", name_when_new: :foo
end
end

test "hw_combobox_next_page_uri updates URI with pagination params while preserving other parameters" do
uri = "/foo"
complex_uri = "#{uri}?exclude_id=5&page=1&q=&for_id=movie_id&format=turbo_stream"
next_page = 2
for_id = "movie_id"

expected_next_page_uri = "#{uri}?page=#{next_page}&q&for_id=#{for_id}&format=turbo_stream"
assert_equal expected_next_page_uri, hw_combobox_next_page_uri(uri, next_page, for_id)

expected_next_page_uri = "#{uri}?exclude_id=5&page=#{next_page}&q&for_id=#{for_id}&format=turbo_stream"
assert_equal expected_next_page_uri, hw_combobox_next_page_uri(complex_uri, next_page, for_id)
end
end

0 comments on commit 0660671

Please sign in to comment.