Skip to content

Commit

Permalink
manually calculate total pages
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcam-src committed Dec 17, 2024
1 parent 0733838 commit 8b7e3e2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<% surrounding_pages = 4 %>
<!-- Page Numbers -->
<% (1..total_pages).each do |page| %>
<% page_display = number_with_delimiter(page) %>
<!-- Display pages within +/-4 of the current page, always show the first and last two pages -->
<% within_range = (page >= current_page - surrounding_pages && page <= current_page + surrounding_pages) %>
<% show_page = (
Expand All @@ -34,7 +33,7 @@
<% if page == current_page %>
<span class="page-link" aria-label="Current Page, Page <%= page %>" aria-current="true"><%= page %></span>
<% else %>
<%= helpers.link_to_specific_page @facet_field.paginator, "Page: #{page}", page.to_i, class: "page-link" %>
<%= helpers.link_to_specific_page @facet_field.paginator,"#{page}", page.to_i, params: @facet_field.search_state.to_h.merge(page: page, 'facet.page' => page), class: "page-link" %>
<% end %>
</li>
<!-- Render ellipsis for pages that would be just outside of the range (3,last page - 2) if they aren't show pages -->
Expand Down
19 changes: 13 additions & 6 deletions app/overrides/lib/kanamari/helpers/helper_methods_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# [hyc-override] https://github.com/kaminari/kaminari/blob/v1.2.2/kaminari-core/lib/kaminari/helpers/helper_methods.rb
Kaminari::Helpers::HelperMethods.module_eval do
# Helper to generate a link to a specific page
def link_to_specific_page(scope, name, page = 1, **options)
def link_to_specific_page(scope, name, page, **options)
begin
# Validate inputs
raise ArgumentError, 'Scope is required and must respond to :total_pages' unless scope&.respond_to?(:total_pages)
Expand Down Expand Up @@ -36,16 +36,23 @@ def link_to_specific_page(scope, name, page = 1, **options)
# Helper to generate the path for a specific page
def path_to_specific_page(scope, page, options = {})
begin
# Calculate total pages manually
total_items = scope.instance_variable_get(:@all).size
limit = scope.instance_variable_get(:@limit)
total_pages = (total_items.to_f / limit).ceil

Rails.logger.info "path_to_specific_page: total_items=#{total_items}, limit=#{limit}, calculated total_pages=#{total_pages}, page=#{page}"

# Validate inputs
raise ArgumentError, 'Page number must be a positive integer' unless page.is_a?(Integer) && page.positive?
raise ArgumentError, 'Scope must respond to :total_pages' unless scope&.respond_to?(:total_pages)

Kaminari::Helpers::Page.new(self, **options.reverse_merge(current_page: page)).url if page <= scope.total_pages
raise ArgumentError, "Page number exceeds total pages (#{total_pages})" if page > total_pages
# Generate URL using Kaminari's Page helper
Kaminari::Helpers::Page.new(self, **options.reverse_merge(page: page)).url
rescue ArgumentError => e
Rails.logger.error "Error in path_to_specific_page: #{e.message}"
Rails.logger.info "Error in path_to_specific_page: #{e.message}"
nil
rescue StandardError => e
Rails.logger.error "Unexpected error in path_to_specific_page: #{e.message}"
Rails.logger.error "Unexpected error in path_to_specific_page: #{e.message}\n#{e.backtrace.join("\n")}"
nil
end
end
Expand Down

0 comments on commit 8b7e3e2

Please sign in to comment.