Skip to content

Commit

Permalink
Merge pull request #135 from dixpac/dix/fullpath
Browse files Browse the repository at this point in the history
Pass in query params to default async src
  • Loading branch information
josefarias authored Apr 8, 2024
2 parents 90a91f6 + f48df85 commit fd07254
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 17 deletions.
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ GEM
minitest (5.22.2)
minitest-focus (1.4.0)
minitest (>= 4, < 6)
mocha (2.1.0)
ruby2_keywords (>= 0.0.5)
mutex_m (0.2.0)
net-imap (0.4.10)
date
Expand Down Expand Up @@ -302,6 +304,7 @@ DEPENDENCIES
hotwire_combobox!
importmap-rails
minitest-focus
mocha (~> 2.1)
propshaft
puma
rubocop-37signals
Expand Down
4 changes: 2 additions & 2 deletions app/views/hotwire_combobox/_next_page.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%# locals: (for_id:, next_page:, src:) -%>
<%# locals: (for_id:, src:) -%>

<%= turbo_stream.remove hw_pagination_frame_wrapper_id(for_id) %>
<%= turbo_stream.append hw_listbox_id(for_id) do %>
<%= render "hotwire_combobox/pagination", for_id: for_id, src: hw_combobox_next_page_uri(src, next_page, for_id) %>
<%= render "hotwire_combobox/pagination", for_id: for_id, src: src %>
<% end %>
2 changes: 2 additions & 0 deletions hotwire_combobox.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ Gem::Specification.new do |spec|
spec.add_dependency "rails", ">= 7.0.7.2"
spec.add_dependency "stimulus-rails", ">= 1.2"
spec.add_dependency "turbo-rails", ">= 1.2"

spec.add_development_dependency "mocha", "~> 2.1"
end
26 changes: 13 additions & 13 deletions lib/hotwire_combobox/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ 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: {},
**custom_methods)
include_blank = params[:page].to_i > 0 ? nil : include_blank
options = hw_combobox_options options, render_in: render_in, include_blank: include_blank, **custom_methods
this_page = render "hotwire_combobox/paginated_options", for_id: for_id, options: options
next_page = render "hotwire_combobox/next_page", for_id: for_id, src: src, next_page: next_page
next_page = render "hotwire_combobox/next_page", for_id: for_id, src: hw_combobox_next_page_uri(src, next_page, for_id)

safe_join [ this_page, next_page ]
end
Expand Down Expand Up @@ -146,23 +146,13 @@ def hw_pagination_frame_id(id)
"#{id}__hw_combobox_pagination"
end

def hw_combobox_next_page_uri(uri, next_page, for_id)
if next_page
hw_uri_with_params uri,
page: next_page,
q: params[:q],
for_id: for_id,
format: :turbo_stream
end
end

def hw_combobox_page_stream_action
params[:page].to_i > 0 ? :append : :update
end

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.compact_blank.stringify_keys)
url_or_path.query = URI.encode_www_form query
end.to_s
rescue URI::InvalidURIError
Expand All @@ -178,6 +168,16 @@ def hw_call_method_or_proc(object, method_or_proc)
end

private
def hw_combobox_next_page_uri(uri, next_page, for_id)
return unless next_page

hw_uri_with_params uri,
page: next_page,
q: params[:q],
for_id: for_id,
format: :turbo_stream
end

def hw_extract_options_and_src(options_or_src, render_in, include_blank)
if options_or_src.is_a? String
[ hw_combobox_options([]), options_or_src ]
Expand Down
5 changes: 5 additions & 0 deletions test/application_system_test_case.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :headless_chrome

def states(...)
# https://github.com/freerange/mocha/issues/620
access_fixture("states", ...)
end
end
4 changes: 2 additions & 2 deletions test/application_view_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ def assert_no_attrs(tag, tag_name: :input, **attrs)
def escaped_attrs(attrs)
attrs.map do |k, v|
%Q(#{escape_specials(k)}="#{escape_specials(v)}".*)
end.join(" ")
end.join(" ").gsub("&", "&amp;")
end

def escape_specials(value)
special_characters = Regexp.union "[]".chars
special_characters = Regexp.union "[]?".chars
value.to_s.gsub(special_characters) { |char| "\\#{char}" }
end
end
8 changes: 8 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,12 @@ class HotwireCombobox::HelperTest < ApplicationViewTestCase
combobox_tag :foo, multiselect_chip_src: "some_path", name_when_new: :foo
end
end

test "hw_paginated_combobox_options includes existing params in the default next page src" do
request.expects(:fullpath).returns "/foo?bar=baz&qux=quux&page=1000"

html = hw_paginated_combobox_options [], next_page: 2

assert_attrs html, tag_name: "turbo-frame", src: "/foo?bar=baz&qux=quux&page=2&format=turbo_stream"
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
ActiveRecord::Migrator.migrations_paths = [ File.expand_path("../test/dummy/db/migrate", __dir__) ]
ActiveRecord::Migrator.migrations_paths << File.expand_path("../db/migrate", __dir__)
require "rails/test_help"
require "mocha/minitest"

# Load fixtures from the engine
if ActiveSupport::TestCase.respond_to?(:fixture_paths=)
Expand Down

0 comments on commit fd07254

Please sign in to comment.