Class: Kadmin::Pager

Inherits:
Object
  • Object
show all
Includes:
Presentable
Defined in:
app/components/kadmin/pager.rb,
app/components/kadmin/pager/presenter.rb

Overview

Simple Pager structure, used to paginate collections

Defined Under Namespace

Classes: Presenter

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Presentable

#present

Constructor Details

#initialize(size:, offset:) ⇒ Pager

Returns a new instance of Pager.

Raises:



25
26
27
28
29
30
31
32
33
34
# File 'app/components/kadmin/pager.rb', line 25

def initialize(size:, offset:)
  @size = size
  @offset = offset

  raise(Kadmin::Error, 'Page size must be greater than 0!') unless @size.positive?
  raise(Kadmin::Error, 'Offset must be at least 0!') unless @offset >= 0

  @current_page = (@offset / @size.to_f).floor
  self.total = @offset # assume offset is maximum initially
end

Instance Attribute Details

#current_pageInteger (readonly)



18
19
20
# File 'app/components/kadmin/pager.rb', line 18

def current_page
  @current_page
end

#offsetInteger (readonly)



12
13
14
# File 'app/components/kadmin/pager.rb', line 12

def offset
  @offset
end

#pagesInteger (readonly)



15
16
17
# File 'app/components/kadmin/pager.rb', line 15

def pages
  @pages
end

#sizeInteger (readonly)



9
10
11
# File 'app/components/kadmin/pager.rb', line 9

def size
  @size
end

#totalInteger



21
22
23
# File 'app/components/kadmin/pager.rb', line 21

def total
  @total
end

Instance Method Details

#contains?(page) ⇒ Boolean



64
65
66
# File 'app/components/kadmin/pager.rb', line 64

def contains?(page)
  page.in?(0...@pages)
end

#current_page?(page) ⇒ Boolean



58
59
60
# File 'app/components/kadmin/pager.rb', line 58

def current_page?(page)
  return page == @current_page
end

#next_page?(page = nil) ⇒ Boolean



88
89
90
91
# File 'app/components/kadmin/pager.rb', line 88

def next_page?(page = nil)
  page ||= @current_page
  return offset_at(page.to_i + 1) < @total
end

#offset_at(page = nil) ⇒ Integer



51
52
53
54
# File 'app/components/kadmin/pager.rb', line 51

def offset_at(page = nil)
  page ||= @current_page
  return @size * page.to_i
end

#page_size(page = nil) ⇒ Integer



76
77
78
79
80
81
82
83
84
# File 'app/components/kadmin/pager.rb', line 76

def page_size(page = nil)
  page ||= @current_page
  return 0 unless contains?(page)

  page_start = offset_at(page)
  page_end = [offset_at(page.to_i + 1), @total].min

  return page_end - page_start
end

#paginate(collection) ⇒ ActiveRecord::Relation



38
39
40
41
42
43
44
45
46
47
# File 'app/components/kadmin/pager.rb', line 38

def paginate(collection)
  return nil if collection.nil?

  self.total = collection.count

  collection = collection.offset(@offset)
  collection = collection.limit(@size)

  return collection
end

#previous_page?(page = nil) ⇒ Boolean



95
96
97
98
99
# File 'app/components/kadmin/pager.rb', line 95

def previous_page?(page = nil)
  page ||= @current_page

  return page.to_i.positive?
end