-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add custom pagination helper for enhanced UI
- Loading branch information
1 parent
c5f6448
commit 8d4915f
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module PaginationHelper | ||
def custom_paginate_renderer(collection, options = {}) | ||
content_tag(:nav, class: "pagination-controls") do | ||
previous_page_link(collection, options) + page_numbers(collection, options) + next_page_link(collection, options) | ||
end | ||
end | ||
|
||
private | ||
|
||
def link_to_previous_page(collection, options) | ||
unless collection.first_page? | ||
link_to 'Previous', url_for(page: collection.prev_page), class: options[:previous_class] || "prev-page" | ||
end | ||
end | ||
|
||
def link_to_next_page(collection, options) | ||
unless collection.last_page? | ||
link_to 'Next', url_for(page: collection.next_page), class: options[:next_class] || "next-page" | ||
end | ||
end | ||
|
||
def page_numbers(collection, options) | ||
start_page = [collection.current_page - options[:page_range], 1].max | ||
end_page = [collection.current_page + options[:page_range], collection.total_pages].min | ||
|
||
(start_page..end_page).map do |page| | ||
link_to page, url_for(page: page), class: (page == collection.current_page ? options[:active_class] : options[:page_class]) | ||
end.join.html_safe | ||
end | ||
end |