Class: Ippon::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/ippon/paginator.rb

Overview

Paginator represents a pagination of a collection. You initiailize it with the total number of entries, how many entries you want per page, and the current page, and it provides various helpers for working with pages.

Examples:

Using #limit and #offset to build the correct query.

all_rows = User.where(is_active: true)

entries_per_page = 20
total_entries = all_rows.count
current_page = params[:page].to_i
paginator = Paginator.new(total_entries, entries_per_page, current_page)

rows = all_rows.limit(paginator.limit).offset(paginator.offset)

Using Paginator with Tubby to render a Bootstrap pagination

class BootstrapPagination < Struct.new(:paginator, :url)
  def url_for(page)
    "#{url}&page=#{page}"
  end

  def item(t, page, text, is_active = false)
    t.li(class: 'page-item', disabled: !page, active: is_active) {
      if page
        t.a(text, href: url_for(page), class: 'page-link')
      else
        t.span(text, class: 'page-link')
      end
    }
  end

  def to_tubby
    Tubby.new { |t|
      t.nav {
        t.ul(class: "pagination") {
          item(t, paginator.prev_page, "Previous")

          paginator.each_page do |page|
            item(t, page, page.to_s, paginator.current_page == page)
          end

          item(t, paginator.next_page, "Next")
        }
      }
    }
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total_entries, entries_per_page, current_page = 1) ⇒ Paginator

Returns a new instance of Paginator.


60
61
62
63
64
# File 'lib/ippon/paginator.rb', line 60

def initialize(total_entries, entries_per_page, current_page = 1)
  @total_entries = total_entries
  @entries_per_page = entries_per_page
  self.current_page = current_page
end

Instance Attribute Details

#current_pageObject

The current page number. This value is always guaranteed to be within #first_page and #last_page.


58
59
60
# File 'lib/ippon/paginator.rb', line 58

def current_page
  @current_page
end

#entries_per_pageObject (readonly) Also known as: limit

The number of entries per page. This method is aliased as #limit to complement #offset.


54
55
56
# File 'lib/ippon/paginator.rb', line 54

def entries_per_page
  @entries_per_page
end

#total_entriesObject (readonly)

The total number of entries.


50
51
52
# File 'lib/ippon/paginator.rb', line 50

def total_entries
  @total_entries
end

Instance Method Details

#each_page {|num| ... } ⇒ Object

Yields:

  • every page number (from 1 to the last page).

Yield Parameters:

  • num (String)

    page number.


112
113
114
# File 'lib/ippon/paginator.rb', line 112

def each_page(&blk)
  (first_page .. last_page).each(&blk)
end

#first_pageInteger

This always returns 1, but is provided to complement #last_page.

Returns:

  • (Integer)

    the page number of the first page.


81
82
83
# File 'lib/ippon/paginator.rb', line 81

def first_page
  1
end

#first_page?Boolean

Returns true if the paginator is currently on the first page.

Returns:

  • (Boolean)

    true if the paginator is currently on the first page.


86
87
88
# File 'lib/ippon/paginator.rb', line 86

def first_page?
  current_page == first_page
end

#last_pageInteger

Returns the page number of the last page.

Returns:

  • (Integer)

    the page number of the last page.


91
92
93
# File 'lib/ippon/paginator.rb', line 91

def last_page
  @last_page ||= [(total_entries / entries_per_page.to_f).ceil, first_page].max
end

#last_page?Boolean

Returns true if the paginator is currently on the last page.

Returns:

  • (Boolean)

    true if the paginator is currently on the last page.


96
97
98
# File 'lib/ippon/paginator.rb', line 96

def last_page?
  current_page == last_page
end

#next_pageInteger?

Returns the page number of the next page (if it exists).

Returns:

  • (Integer, nil)

    the page number of the next page (if it exists).


106
107
108
# File 'lib/ippon/paginator.rb', line 106

def next_page
  current_page + 1 unless last_page?
end

#offsetInteger

Calculates the number of entries you need to skip in order to reach the current page.

Returns:

  • (Integer)

122
123
124
# File 'lib/ippon/paginator.rb', line 122

def offset
  (current_page - 1) * entries_per_page
end

#prev_pageInteger?

Returns the page number of the previous page (if it exists).

Returns:

  • (Integer, nil)

    the page number of the previous page (if it exists).


101
102
103
# File 'lib/ippon/paginator.rb', line 101

def prev_page
  current_page - 1 unless first_page?
end