Class: ROM::SQL::Plugin::Pagination::Pager

Inherits:
Object
  • Object
show all
Extended by:
Initializer
Defined in:
lib/rom/sql/plugin/pagination.rb

Overview

Pager object provides the underlying pagination API for relations

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_pageInteger (readonly)

Returns Current page number.

Returns:

  • (Integer)

    Current page number



25
# File 'lib/rom/sql/plugin/pagination.rb', line 25

option :current_page, default: -> { 1 }

#datasetSequel::Dataset (readonly)

Returns Relation's dataset.

Returns:

  • (Sequel::Dataset)

    Relation's dataset



21
# File 'lib/rom/sql/plugin/pagination.rb', line 21

param :dataset

#per_pageInteger (readonly) Also known as: limit_value

Returns Current per-page number.

Returns:

  • (Integer)

    Current per-page number



29
# File 'lib/rom/sql/plugin/pagination.rb', line 29

option :per_page

Instance Method Details

#first_in_pageInteger

Return one-based index of first tuple in page

Returns:

  • (Integer)


82
83
84
# File 'lib/rom/sql/plugin/pagination.rb', line 82

def first_in_page
  ((current_page - 1) * per_page) + 1
end

#last_in_pageInteger

Return one-based index of last tuple in page

Returns:

  • (Integer)


91
92
93
94
95
# File 'lib/rom/sql/plugin/pagination.rb', line 91

def last_in_page
  return total if current_page == total_pages

  current_page * per_page
end

#next_pageInteger

Return next page number

Examples:

users.page(2).pager.next_page
# => 3

Returns:

  • (Integer)


40
41
42
43
# File 'lib/rom/sql/plugin/pagination.rb', line 40

def next_page
  num = current_page + 1
  num if total_pages >= num
end

#prev_pageInteger

Return previous page number

Examples:

users.page(2).pager.prev_page
# => 1

Returns:

  • (Integer)


54
55
56
57
# File 'lib/rom/sql/plugin/pagination.rb', line 54

def prev_page
  num = current_page - 1
  num if num > 0
end

#totalInteger

Return total number of tuples

Returns:

  • (Integer)


64
65
66
# File 'lib/rom/sql/plugin/pagination.rb', line 64

def total
  dataset.unlimited.count
end

#total_pagesInteger

Return total number of pages

Returns:

  • (Integer)


73
74
75
# File 'lib/rom/sql/plugin/pagination.rb', line 73

def total_pages
  (total / per_page.to_f).ceil
end