Class: Wallaby::ModelPaginationProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/interfaces/wallaby/model_pagination_provider.rb

Overview

Model pagination provider interface

Direct Known Subclasses

Custom::ModelPaginationProvider

Instance Method Summary collapse

Constructor Details

#initialize(collection, params) ⇒ ModelPaginationProvider

Returns a new instance of ModelPaginationProvider.

Parameters:

  • collection (#to_a)
  • params (ActionController::Parameters)


8
9
10
11
# File 'lib/interfaces/wallaby/model_pagination_provider.rb', line 8

def initialize(collection, params)
  @collection = collection
  @params = params
end

Instance Method Details

#first_page?Boolean

Check and see if it’s the first page

Returns:

  • (Boolean)


21
22
23
# File 'lib/interfaces/wallaby/model_pagination_provider.rb', line 21

def first_page?
  page_number > first_page_number
end

#first_page_numberInteger

Page number of first page

Returns:

  • (Integer)


75
76
77
# File 'lib/interfaces/wallaby/model_pagination_provider.rb', line 75

def first_page_number
  1
end

#fromInteger

Find out the offset ‘from`

Returns:

  • (Integer)


45
46
47
# File 'lib/interfaces/wallaby/model_pagination_provider.rb', line 45

def from
  total.zero? ? total : ((page_number - 1) * page_size) + 1
end

#last_page?Boolean

Check and see if it’s the last page

Returns:

  • (Boolean)


33
34
35
# File 'lib/interfaces/wallaby/model_pagination_provider.rb', line 33

def last_page?
  page_number < last_page_number
end

#last_page_numberInteger

Page number of last page

Returns:

  • (Integer)


81
82
83
# File 'lib/interfaces/wallaby/model_pagination_provider.rb', line 81

def last_page_number
  number_of_pages
end

#next_page?Boolean

Check and see if it’s the next page

Returns:

  • (Boolean)


39
40
41
# File 'lib/interfaces/wallaby/model_pagination_provider.rb', line 39

def next_page?
  page_number < last_page_number
end

#next_page_numberInteger

Page number of next page

Returns:

  • (Integer)


93
94
95
# File 'lib/interfaces/wallaby/model_pagination_provider.rb', line 93

def next_page_number
  [page_number + 1, last_page_number].min
end

#number_of_pagesInteger

Total number of pages

Returns:

  • (Integer)


99
100
101
# File 'lib/interfaces/wallaby/model_pagination_provider.rb', line 99

def number_of_pages
  (total / page_size.to_f).ceil
end

#page_numberInteger

Find out the current page number

Returns:

  • (Integer)

Raises:



69
70
71
# File 'lib/interfaces/wallaby/model_pagination_provider.rb', line 69

def page_number
  raise NotImplemented
end

#page_sizeInteger

Find out the current page size

Returns:

  • (Integer)

Raises:



63
64
65
# File 'lib/interfaces/wallaby/model_pagination_provider.rb', line 63

def page_size
  raise NotImplemented
end

#paginatable?Boolean

If a collection has pagination feature

Returns:

  • (Boolean)

Raises:



15
16
17
# File 'lib/interfaces/wallaby/model_pagination_provider.rb', line 15

def paginatable?
  raise NotImplemented
end

#prev_page?Boolean

Check and see if it’s the previous page

Returns:

  • (Boolean)


27
28
29
# File 'lib/interfaces/wallaby/model_pagination_provider.rb', line 27

def prev_page?
  page_number > first_page_number
end

#prev_page_numberInteger

Page number of previous page

Returns:

  • (Integer)


87
88
89
# File 'lib/interfaces/wallaby/model_pagination_provider.rb', line 87

def prev_page_number
  [page_number - 1, first_page_number].max
end

#toInteger

Find out the offset ‘to`

Returns:

  • (Integer)


51
52
53
# File 'lib/interfaces/wallaby/model_pagination_provider.rb', line 51

def to
  [page_number * page_size, total].min
end

#totalInteger

Find out the total count of current query

Returns:

  • (Integer)

Raises:



57
58
59
# File 'lib/interfaces/wallaby/model_pagination_provider.rb', line 57

def total
  raise NotImplemented
end