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
-
#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
36 37 38 |
# File 'lib/next_page/pagination_attributes.rb', line 36 def count_query @count_query || self end |
#current_page ⇒ Object
15 16 17 |
# File 'lib/next_page/pagination_attributes.rb', line 15 def current_page @current_page ||= count_query.offset_value + 1 end |
#per_page ⇒ Object
31 32 33 |
# File 'lib/next_page/pagination_attributes.rb', line 31 def per_page @per_page ||= count_query.limit_value end |
#total_count ⇒ Object
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_page ⇒ Object
19 20 21 |
# File 'lib/next_page/pagination_attributes.rb', line 19 def next_page current_page + 1 end |
#total_pages ⇒ Object
27 28 29 |
# File 'lib/next_page/pagination_attributes.rb', line 27 def total_pages total_count.fdiv(per_page).ceil end |