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

Inherits:
Object
  • Object
show all
Includes:
Options
Defined in:
lib/rom/sql/plugin/pagination.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dataset, options = {}) ⇒ Pager

Returns a new instance of Pager.



14
15
16
17
# File 'lib/rom/sql/plugin/pagination.rb', line 14

def initialize(dataset, options = {})
  super
  @dataset = dataset
end

Instance Attribute Details

#datasetObject (readonly)

Returns the value of attribute dataset.



12
13
14
# File 'lib/rom/sql/plugin/pagination.rb', line 12

def dataset
  @dataset
end

Instance Method Details

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



37
38
39
40
41
42
43
44
45
# File 'lib/rom/sql/plugin/pagination.rb', line 37

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

#next_pageObject



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

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

#prev_pageObject



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

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

#totalObject



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

def total
  dataset.unlimited.count
end

#total_pagesObject



33
34
35
# File 'lib/rom/sql/plugin/pagination.rb', line 33

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