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
Returns originating controller action.
24 25 26 |
# File 'lib/foliate/pagination.rb', line 24 def action @action end |
#controller ⇒ Symbol
Returns originating controller.
20 21 22 |
# File 'lib/foliate/pagination.rb', line 20 def controller @controller end |
#current_page ⇒ Integer
Returns current page number.
8 9 10 |
# File 'lib/foliate/pagination.rb', line 8 def current_page @current_page end |
#per_page ⇒ Integer
Returns number of records per page.
12 13 14 |
# File 'lib/foliate/pagination.rb', line 12 def per_page @per_page end |
#query_params ⇒ Hash
Returns originating request query params.
28 29 30 |
# File 'lib/foliate/pagination.rb', line 28 def query_params @query_params end |
#total_records ⇒ Integer
Returns total number of records.
16 17 18 |
# File 'lib/foliate/pagination.rb', line 16 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.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/foliate/pagination.rb', line 61 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.
97 98 99 |
# File 'lib/foliate/pagination.rb', line 97 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.
119 120 121 |
# File 'lib/foliate/pagination.rb', line 119 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.
42 43 44 |
# File 'lib/foliate/pagination.rb', line 42 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.
81 82 83 84 85 |
# File 'lib/foliate/pagination.rb', line 81 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.
90 91 92 |
# File 'lib/foliate/pagination.rb', line 90 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.
108 109 110 |
# File 'lib/foliate/pagination.rb', line 108 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.
131 132 133 |
# File 'lib/foliate/pagination.rb', line 131 def to_partial_path "pagination/pagination" end |
#total_pages ⇒ Integer
Computes the total number of pages based on #total_records and #per_page.
34 35 36 |
# File 'lib/foliate/pagination.rb', line 34 def total_pages (total_records / per_page.to_f).ceil end |