class

Lucky::Paginator

Inherits Reference < Object

Constructors

new(page : Int32, per_page : Int32, item_count : Int32 | Int64, full_path : String)
Source

Instance methods

first_page?

Returns true if the current page is the first one.

Source
full_path
Source
item_count
Source
item_range

Returns the Range of items on this page.

For example if you have 50 records, showing 20 per page, and you are on the 2nd page this method will return a range of 21-40.

You can get the beginning and end by calling begin or end on the returned Range.

Source
last_page?

Returns true if current page is the last one.

Source
next_page

Returns the next page number or nil if the current page is the last one.

Source
offset
Source
one_page?

Returns true if there is just one page.

Source
overflowed?

Returns true if the current page is past the last page.

Source
page

Returns the current page. Return 1 if the passed in page is lower than 1.

Source
path_to_next

Returns the path with a 'page' query param for the previous page.

Return nil if there is no previous page

Source
path_to_page(page_number : Int) : String

Generate a page with the 'page' query param set to the passed in page_number.

Examples

pages = Paginator.new(
  page: 1,
  per_page: 25,
  item_count: 70,
  full_path: "/comments"
)
pages.path_to_page(2) # "/comments?page=2"
Source
path_to_previous

Returns the path with a 'page' query param for the previous page.

Return nil if there is no previous page

Source
per_page
Source
previous_page

Returns the previous page number or nil if the current page is the first one.

Source
series(begin beginning : Int32 = 0, left_of_current : Int32 = 0, right_of_current : Int32 = 0, end ending : Int32 = 0) : Array(SeriesItem)

The series method is smart and will not add gaps if there is no gap.

It will also not add items past the current page.

series = pages.series(begin: 6) series # [1, 2, 3, 4, 5]


As mentioned above the **actual** objects in the Array are made up of
`Lucky::Paginator::Gap`, `Lucky::Paginator::Page`, and
`Lucky::Paginator::CurrentPage` objects.

pages.series(begin: 1, end: 1)

Returns:

[

Lucky::Paginator::Page(1),

Lucky::Paginator::Gap,

Lucky::Paginator::CurrentPage(5),

Lucky::Paginator::Gap,

Lucky::Paginator::Page(10),

]


The `Page` and `CurrentPage` objects have a `number` and `path` method.
`Page#number` returns the number of the page as an Int. The `Page#path` method
Return the path to the next page.

The `Gap` object has no methods or instance variables. It is there to
represent a "gap" of pages.

These objects make it easy to use [method # overloading](https://crystal-lang.org/reference/syntax_and_semantics/overloading.html)
or `is_a?` to determine how to render each item.

Here's a quick example:

pages.series(begin: 1, end: 1).each do |item| case item when Lucky::Paginator::CurrentPage | Lucky::Paginator::Page pp! item.number # Int32 representing the page number pp! item.path # "/items?page=2" when Lucky::Paginator::Gap puts "..." end end


Or use method overloading. This will show an example using Lucky's HTML methods:

class PageNav < BaseComponent needs pages : Lucky::Paginator

def render pages.series(begin: 1, end: 1).each do |item| page_item(item) end end

def page_item(page : Lucky::Paginator::CurrentPage) # If it is the current page, just display text and no link text page.number end

def page_item(page : Lucky::Paginator::CurrentPage) a page.number, href: page.path end

def page_item(gap : Lucky::Paginator::Gap) text ".." end end

Source
total

Returns the total number of pages.

Source

Nested types