Module: NextPage::PaginationAttributes

Defined in:
lib/next_page/pagination_attributes.rb

Overview

Pagination Attributes

Module PaginationAttributes adds in methods required for pagination links: 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

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



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

def count_query
  @count_query || self
end

#current_pageObject



15
16
17
# File 'lib/next_page/pagination_attributes.rb', line 15

def current_page
  @current_page ||= count_query.offset_value + 1
end

#per_pageObject



31
32
33
# File 'lib/next_page/pagination_attributes.rb', line 31

def per_page
  @per_page ||= count_query.limit_value
end

#total_countObject



23
24
25
# File 'lib/next_page/pagination_attributes.rb', line 23

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

Instance Method Details

#next_pageObject



19
20
21
# File 'lib/next_page/pagination_attributes.rb', line 19

def next_page
  current_page + 1
end

#total_pagesObject



27
28
29
# File 'lib/next_page/pagination_attributes.rb', line 27

def total_pages
  total_count.fdiv(per_page).ceil
end