Skip to content

Commit

Permalink
fix: pagy 9 data-turbo-frame (#3117)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob committed Aug 9, 2024
1 parent d41d3a7 commit f09a99e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
8 changes: 4 additions & 4 deletions app/components/avo/paginator_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
<div class="flex items-center">
<%= content_tag :label, name: t('avo.per_page').downcase do %>
<%= select_tag 'per_page',
options_for_select(per_page_options, index_params[:per_page]),
options_for_select(per_page_options, @index_params[:per_page]),
class: 'appearance-none inline-flex bg-white-100 disabled:bg-white-400 disabled:cursor-not-allowed focus:bg-white text-slate-700 disabled:text-slate-600 rounded-md py-1 px-2 leading-tight border border-slate-300 outline-none focus:border-slate-400 outline w-16 mr-1 text-sm',
data: {
'turbo-frame': turbo_frame,
'turbo-frame': @turbo_frame,
'per-page-target': 'selector',
action: 'change->per-page#reload'
}
%> <%= t('avo.per_page').downcase %>
<% end %>
</div>
<% per_page_options.each do |option| %>
<%= link_to "Change to #{option} items per page", change_items_per_page_url(option), class: 'hidden', 'data-per-page-option': option, 'data-turbo-frame': turbo_frame %>
<%= link_to "Change to #{option} items per page", change_items_per_page_url(option), class: 'hidden', 'data-per-page-option': option, 'data-turbo-frame': @turbo_frame %>
<% end %>
</div>
</div>
Expand All @@ -31,7 +31,7 @@
<% if @pagy.pages > 1 %>
<%# @todo: add first & last page. make the first and last buttons rounded %>
<%== helpers.pagy_nav @pagy %>
<%== helpers.pagy_nav @pagy, anchor_string: "data-turbo-frame=\"#{@turbo_frame}\"" %>
<% end %>
</div>
</div>
Expand Down
21 changes: 7 additions & 14 deletions app/components/avo/paginator_component.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
# frozen_string_literal: true

class Avo::PaginatorComponent < Avo::BaseComponent
attr_reader :pagy
attr_reader :turbo_frame
attr_reader :index_params
attr_reader :resource
attr_reader :parent_record
attr_reader :discreet_pagination

def initialize(resource: nil, parent_record: nil, pagy: nil, turbo_frame: nil, index_params: nil, discreet_pagination: nil)
@pagy = pagy
@turbo_frame = turbo_frame
@turbo_frame = CGI.escapeHTML(turbo_frame) if turbo_frame
@index_params = index_params
@resource = resource
@parent_record = parent_record
@discreet_pagination = discreet_pagination
end

def change_items_per_page_url(option)
if parent_record.present?
helpers.related_resources_path(parent_record, parent_record, per_page: option, keep_query_params: true, page: 1)
if @parent_record.present?
helpers.related_resources_path(@parent_record, @parent_record, per_page: option, keep_query_params: true, page: 1)
else
helpers.resources_path(resource: resource, per_page: option, keep_query_params: true, page: 1)
helpers.resources_path(resource: @resource, per_page: option, keep_query_params: true, page: 1)
end
end

def render?
return false if discreet_pagination && pagy.pages <= 1
return false if @discreet_pagination && @pagy.pages <= 1

if ::Pagy::VERSION >= ::Gem::Version.new("9.0")
@pagy.limit > 0
Expand All @@ -37,9 +30,9 @@ def render?

def per_page_options
@per_page_options ||= begin
options = [*Avo.configuration.per_page_steps, Avo.configuration.per_page.to_i, index_params[:per_page].to_i]
options = [*Avo.configuration.per_page_steps, Avo.configuration.per_page.to_i, @index_params[:per_page].to_i]

if parent_record.present?
if @parent_record.present?
options.prepend Avo.configuration.via_per_page
end

Expand Down
4 changes: 2 additions & 2 deletions spec/system/avo/filters/filters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@
wait_for_loaded
check "Featured"

expect(page).to have_css "[aria-label='Next']"
expect(page).to have_css "[aria-label='Previous']"
expect(page).to have_css "[data-turbo-frame][aria-label='Next']"
expect(page).to have_css "[data-turbo-frame][aria-label='Previous']"

expect(find(".current")).to have_text "1"
expect(find(".current")).not_to have_text "2"
Expand Down

0 comments on commit f09a99e

Please sign in to comment.