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

#at(dataset, current_page, per_page = self.per_page) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



98
99
100
101
102
103
104
105
106
# File 'lib/rom/sql/plugin/pagination.rb', line 98

def at(dataset, current_page, per_page = self.per_page)
  current_page = current_page.to_i
  per_page = per_page.to_i

  self.class.new(
    dataset.offset((current_page-1)*per_page).limit(per_page),
    current_page: current_page, per_page: per_page
  )
end

#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