Class: Foliate::Pagination
- Inherits:
-
Object
- Object
- Foliate::Pagination
- Defined in:
- lib/foliate/pagination.rb
Instance Attribute Summary collapse
-
#action ⇒ Symbol
Originating controller action.
-
#controller ⇒ Symbol
Originating controller.
-
#current_page ⇒ Integer
Current page number.
-
#per_page ⇒ Integer
Number of records per page.
-
#query_params ⇒ Hash
Originating request query params.
-
#total_records ⇒ Integer
Total number of records.
Instance Method Summary collapse
-
#each_query_param ⇒ Object
Iterates through each #query_params as name-value pairs.
-
#next? ⇒ Boolean
Indicates if there is a page expected after the current page.
-
#next_page_params ⇒ Hash?
Linking params for the next page.
-
#offset ⇒ Integer
Computes the record set offset based on #current_page and #per_page.
-
#params_for_page(page_number) ⇒ Hash
Returns linking params for a specified
page_number. -
#prev? ⇒ Boolean
Indicates if there is a page expected before the current page.
-
#prev_page_params ⇒ Hash?
Linking params for the previous page.
-
#to_partial_path ⇒ String
Path to the view partial.
-
#total_pages ⇒ Integer
Computes the total number of pages based on #total_records and #per_page.
Instance Attribute Details
#action ⇒ Symbol
Originating controller action.
27 28 29 |
# File 'lib/foliate/pagination.rb', line 27 def action @action end |
#controller ⇒ Symbol
Originating controller.
22 23 24 |
# File 'lib/foliate/pagination.rb', line 22 def controller @controller end |
#current_page ⇒ Integer
Current page number.
7 8 9 |
# File 'lib/foliate/pagination.rb', line 7 def current_page @current_page end |
#per_page ⇒ Integer
Number of records per page.
12 13 14 |
# File 'lib/foliate/pagination.rb', line 12 def per_page @per_page end |
#query_params ⇒ Hash
Originating request query params.
32 33 34 |
# File 'lib/foliate/pagination.rb', line 32 def query_params @query_params end |
#total_records ⇒ Integer
Total number of records.
17 18 19 |
# File 'lib/foliate/pagination.rb', line 17 def total_records @total_records end |
Instance Method Details
#each_query_param {|name, value| ... } ⇒ nil #each_query_param ⇒ Enumerator
Iterates through each #query_params as name-value pairs. Nested Hashes and Arrays are iterated as well. The name of nested a value will reflect its nesting in the same way as when converting a nested Hash to a query string via Hash#to_query. Intended for use when generating form fields.
If no block is given, an Enumerator is returned.
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/foliate/pagination.rb', line 65 def each_query_param if block_given? ParamsHelper.to_keyed_pairs(query_params).each do |pair| yield pair[:name], pair[:value] end nil else to_enum(__method__) end end |
#next? ⇒ Boolean
Indicates if there is a page expected after the current page.
101 102 103 |
# File 'lib/foliate/pagination.rb', line 101 def next? current_page < total_pages end |
#next_page_params ⇒ Hash?
Linking params for the next page. Returns nil if #next? is false. See also #params_for_page.
123 124 125 |
# File 'lib/foliate/pagination.rb', line 123 def next_page_params params_for_page(current_page + 1) if next? end |
#offset ⇒ Integer
Computes the record set offset based on #current_page and #per_page.
46 47 48 |
# File 'lib/foliate/pagination.rb', line 46 def offset (current_page - 1) * per_page end |
#params_for_page(page_number) ⇒ Hash
Returns linking params for a specified page_number. The returned Hash is intended for use with link_to, url_for, etc.
85 86 87 88 89 |
# File 'lib/foliate/pagination.rb', line 85 def params_for_page(page_number) { controller: controller, action: action, params: query_params.merge( Foliate.config.page_param => (page_number if page_number > 1) ) } end |
#prev? ⇒ Boolean
Indicates if there is a page expected before the current page.
94 95 96 |
# File 'lib/foliate/pagination.rb', line 94 def prev? current_page > 1 end |
#prev_page_params ⇒ Hash?
Linking params for the previous page. Returns nil if #prev? is false. See also #params_for_page.
112 113 114 |
# File 'lib/foliate/pagination.rb', line 112 def prev_page_params params_for_page(current_page - 1) if prev? end |
#to_partial_path ⇒ String
Path to the view partial. This method exists to allow Pagination objects to be passed directly to render calls in the view.
135 136 137 |
# File 'lib/foliate/pagination.rb', line 135 def to_partial_path "pagination" end |
#total_pages ⇒ Integer
Computes the total number of pages based on #total_records and #per_page.
38 39 40 |
# File 'lib/foliate/pagination.rb', line 38 def total_pages (total_records / per_page.to_f).ceil end |