class Lucky::Paginator

Defined in:

lucky/paginator/paginator.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(page : Int32, per_page : Int32, item_count : Int32 | Int64, full_path : String) #

[View source]

Instance Method Detail

def first_page? : Bool #

Returns true if the current #page is the first one.


[View source]
def full_path : String #

[View source]
def item_count : Int32 | Int64 #

[View source]
def item_range : 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.


[View source]
def last_page? : Bool #

Returns true if current #page is the last one.


[View source]
def next_page : Int32 | Nil #

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


[View source]
def offset : Int32 #

[View source]
def one_page? : Bool #

Returns true if there is just one page.


[View source]
def overflowed? : Bool #

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


[View source]
def page : Int32 #

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


[View source]
def path_to_next : String | Nil #

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

Return nil if there is no previous page


[View source]
def 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"

[View source]
def path_to_previous : String | Nil #

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

Return nil if there is no previous page


[View source]
def per_page : Int32 #

[View source]
def previous_page : Int32 | Nil #

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


[View source]
def 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


[View source]
def total : Int64 #

Returns the total number of pages.


[View source]