Skip to content

Commit

Permalink
argument adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcam-src committed Jan 3, 2025
1 parent dacff3d commit abae3cb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@
<% 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.to_i, @total_unique_facets, params: @facet_field.search_state.to_h.merge(page: page, 'facet.page' => page), class: "page-link" %>
<!-- Typecasting not effective in helper method, using hash as a workaround -->
<%= helpers.link_to_specific_page(
@facet_field.paginator,
{ integer: page.to_i, string: "#{page}" },
@total_unique_facets,
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
23 changes: 10 additions & 13 deletions app/overrides/lib/kanamari/helpers/helper_methods_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@
# [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, page, total_entries, **options)
def link_to_specific_page(scope, page_hash, total_entries, **options)
begin
# Validate inputs
raise ArgumentError, "Page number must be a positive integer - got #{page}" unless page.is_a?(Integer) && page.positive?

specific_page_path = path_to_specific_page(scope, page, total_entries, options)
specific_page_path = path_to_specific_page(scope, page_hash[:integer], total_entries, options)

# Remove unnecessary keys :params and :param_name from the options hash before generating the link
options.except! :params, :param_name

# Setting aria instead of rel for accessibility
options[:aria] ||= { label: "Go to page #{page}" }
options[:aria] ||= { label: "Go to page #{page_hash[:string]}" }

if specific_page_path
link_to("#{page}" || page, specific_page_path, options)
link_to(page_hash[:string] || page, specific_page_path, options)
else
Rails.logger.warn "Specific page path could not be generated for page: #{page}"
Rails.logger.warn "Specific page path could not be generated for page: #{page_hash[:string]}"
end
rescue ArgumentError => e
Rails.logger.error "Error in link_to_specific_page: #{e.message}"
Expand All @@ -29,19 +26,19 @@ def link_to_specific_page(scope, page, total_entries, **options)
end

# Helper to generate the path for a specific page
def path_to_specific_page(scope, page, total_entries, options = {})
def path_to_specific_page(scope, page_integer, total_entries, options = {})
begin
# Calculate total pages manually
limit = scope.instance_variable_get(:@limit)
total_pages = (total_entries.to_f / limit).ceil

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

# Validate inputs
raise ArgumentError, 'Page number must be a positive integer' unless page.is_a?(Integer) && page.positive?
raise ArgumentError, "Page number exceeds total pages (#{total_pages})" if page > total_pages
raise ArgumentError, 'Page number must be a positive integer' unless page_integer.positive?
raise ArgumentError, "Page number exceeds total pages (#{total_pages})" if page_integer > total_pages
# Generate URL using Kaminari's Page helper
Kaminari::Helpers::Page.new(self, **options.reverse_merge(page: page)).url
Kaminari::Helpers::Page.new(self, **options.reverse_merge(page: page_integer)).url
rescue ArgumentError => e
Rails.logger.info "Error in path_to_specific_page: #{e.message}"
rescue StandardError => e
Expand Down

0 comments on commit abae3cb

Please sign in to comment.