Module: NextPage::PaginationAttributes

Defined in:
lib/next_page/pagination_attributes.rb

Overview

# Pagination Attributes

Module PaginationAttributes adds in methods to help with pagination links: previous_page, current_page, next_page, and total_pages. It reads the offset and limit on the query to determine the values.

In some cases the query will not support count. In that case, there are two ways to override the default behavior:

  • provide a count_query that can resolve the attributes

  • specify the following attributes manually: current_page, total_count, and per_page

These can be completed after the call to ‘paginate_resource`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#count_queryObject

checks first to see if an override query has been provided, then fails back to self



50
51
52
# File 'lib/next_page/pagination_attributes.rb', line 50

def count_query
  @count_query || self
end

#total_countObject



37
38
39
# File 'lib/next_page/pagination_attributes.rb', line 37

def total_count
  @total_count ||= count_query.unscope(:limit).unscope(:offset).count
end

Instance Method Details

#current_pageObject



29
30
31
# File 'lib/next_page/pagination_attributes.rb', line 29

def current_page
  @current_page ||= 1
end

#current_page=(value) ⇒ Object



17
18
19
# File 'lib/next_page/pagination_attributes.rb', line 17

def current_page=(value)
  @current_page = [value.to_i, 1].max
end

#next_pageObject



33
34
35
# File 'lib/next_page/pagination_attributes.rb', line 33

def next_page
  total_pages > current_page ? current_page + 1 : nil
end

#per_pageObject



45
46
47
# File 'lib/next_page/pagination_attributes.rb', line 45

def per_page
  @per_page ||= count_query.limit_value
end

#per_page=(value) ⇒ Object



21
22
23
# File 'lib/next_page/pagination_attributes.rb', line 21

def per_page=(value)
  @per_page = [value.to_i, 1].max
end

#previous_pageObject



25
26
27
# File 'lib/next_page/pagination_attributes.rb', line 25

def previous_page
  current_page > 1 ? current_page - 1 : nil
end

#total_pagesObject



41
42
43
# File 'lib/next_page/pagination_attributes.rb', line 41

def total_pages
  total_count.fdiv(per_page).ceil
end