Class: Pagination::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pagination/collection.rb

Direct Known Subclasses

OhmAdapter

Constant Summary collapse

Unimplemented =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, options = {}) ⇒ Collection

When subclassing ‘Pagination::Collection`, make sure you call super in order to use `page` and `per_page`.



13
14
15
16
# File 'lib/pagination/collection.rb', line 13

def initialize(collection, options = {})
  @page       = Integer(options[:page] || 1)
  @per_page   = Integer(options[:per_page] || Pagination.per_page)
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



7
8
9
# File 'lib/pagination/collection.rb', line 7

def page
  @page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



8
9
10
# File 'lib/pagination/collection.rb', line 8

def per_page
  @per_page
end

#totalObject (readonly)

Returns the value of attribute total.



9
10
11
# File 'lib/pagination/collection.rb', line 9

def total
  @total
end

Instance Method Details

#current?(other_page) ⇒ Boolean

Mainly used as syntatic sugar instead of doing

if items.page == params[:page]

for example, you will do

if items.current?(params[:page])

Returns:

  • (Boolean)


41
42
43
# File 'lib/pagination/collection.rb', line 41

def current?(other_page)
  page == other_page
end

#displayed_pages(limit = 10, left_offset = -5,, right_offset = 4) ⇒ Object

Provides dirt-simple logic for spitting out page numbers based on the current page.

If we have 100 pages for example and we’re at page 50, this would simply return

[45, 46, 47, 48, 49, 50, 51, 52, 53, 54]

When we’re at page 1, it displays 1 to 10.

You can pass in a number to limit the total displayed pages.



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pagination/collection.rb', line 56

def displayed_pages(limit = 10, left_offset = -5, right_offset = 4)
  lower, upper = nil, nil

  if page + left_offset < 1 || page + right_offset > pages.last
    lower = [page, [pages.last - limit, 0].max + 1].min
    upper = [page + limit - 1, pages.last].min
  else
    lower = page + left_offset
    upper = page + right_offset
  end

  (lower..upper).to_a
end

#each(&block) ⇒ Object



79
80
81
# File 'lib/pagination/collection.rb', line 79

def each(&block)
  collection.each(&block) 
end

#next_pageObject

When there’s a valid next page, returns that number

Otherwise returns nil



29
30
31
# File 'lib/pagination/collection.rb', line 29

def next_page
  page + 1  if pages.include?(page + 1)
end

#prev_pageObject

When there’s a valid previous page, returns that number

Otherwise returns nil



22
23
24
# File 'lib/pagination/collection.rb', line 22

def prev_page
  page - 1  if pages.include?(page - 1)
end

#render?Boolean

Mainly used in the presentation layer as a front-line check if we should even proceed with all the nitty-gritty details of rendering.

This basically returns false when there’s only 1 page for the given collection, otherwise returns true.

Returns:

  • (Boolean)


75
76
77
# File 'lib/pagination/collection.rb', line 75

def render?
  pages.to_a.size > 1
end

#total_pagesObject



83
84
85
# File 'lib/pagination/collection.rb', line 83

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