Module: NextPage::PaginationAttributes
- Defined in:
- lib/next_page/pagination_attributes.rb
Overview
Pagination Attributes
Module PaginationAttributes adds in methods required for 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
Instance Attribute Summary collapse
-
#count_query ⇒ Object
checks first to see if an override query has been provided, then fails back to self.
- #current_page ⇒ Object
- #per_page ⇒ Object
- #total_count ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#count_query ⇒ Object
checks first to see if an override query has been provided, then fails back to self
40 41 42 |
# File 'lib/next_page/pagination_attributes.rb', line 40 def count_query @count_query || self end |
#current_page ⇒ Object
19 20 21 |
# File 'lib/next_page/pagination_attributes.rb', line 19 def current_page @current_page ||= (count_query.offset_value || 0) + 1 end |
#per_page ⇒ Object
35 36 37 |
# File 'lib/next_page/pagination_attributes.rb', line 35 def per_page @per_page ||= count_query.limit_value end |
#total_count ⇒ Object
27 28 29 |
# File 'lib/next_page/pagination_attributes.rb', line 27 def total_count @total_count ||= count_query.unscope(:limit).unscope(:offset).count end |
Instance Method Details
#next_page ⇒ Object
23 24 25 |
# File 'lib/next_page/pagination_attributes.rb', line 23 def next_page total_pages > current_page ? current_page + 1 : nil end |
#previous_page ⇒ Object
15 16 17 |
# File 'lib/next_page/pagination_attributes.rb', line 15 def previous_page current_page > 1 ? current_page - 1 : nil end |
#total_pages ⇒ Object
31 32 33 |
# File 'lib/next_page/pagination_attributes.rb', line 31 def total_pages total_count.fdiv(per_page).ceil end |