class Foliate::Pagination
Attributes
Originating controller action.
@return [Symbol]
Originating controller.
@return [Symbol]
Current page number.
@return [Integer]
Number of records per page.
@return [Integer]
Originating request query params.
@return [Hash]
Total number of records.
@return [Integer]
Public Instance Methods
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.
@overload each_query_param
@yieldparam name [String] @yieldparam value [String] @return [nil]
@overload each_query_param
@return [Enumerator]
# 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
Indicates if there is a page expected after the current page.
@return [Boolean]
# File lib/foliate/pagination.rb, line 101 def next? current_page < total_pages end
Linking params for the next page. Returns nil if {#next?} is false. See also {#params_for_page}.
@example usage in view
link_to_if @pagination.next?, "Next", @pagination.next_page_params
@return [Hash, nil]
# File lib/foliate/pagination.rb, line 123 def next_page_params params_for_page(current_page + 1) if next? end
Computes the record set offset based on {#current_page} and {#per_page}.
@return [Integer]
# File lib/foliate/pagination.rb, line 46 def offset (current_page - 1) * per_page end
Returns linking params for a specified page_number
. The returned Hash is intended for use with link_to
, url_for
, etc.
@example usage in view
link_to "First", @pagination.params_for_page(1) link_to "Last", @pagination.params_for_page(@pagination.total_pages)
@param page_number [Integer] @return [Hash]
# 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
Indicates if there is a page expected before the current page.
@return [Boolean]
# File lib/foliate/pagination.rb, line 94 def prev? current_page > 1 end
Linking params for the previous page. Returns nil if {#prev?} is false. See also {#params_for_page}.
@example usage in view
link_to_if @pagination.prev?, "Previous", @pagination.prev_page_params
@return [Hash, nil]
# File lib/foliate/pagination.rb, line 112 def prev_page_params params_for_page(current_page - 1) if prev? end
Path to the view partial. This method exists to allow Pagination
objects to be passed directly to render
calls in the view.
@example rendering view partial from view
render @pagination
@return [String]
# File lib/foliate/pagination.rb, line 135 def to_partial_path "pagination" end
Computes the total number of pages based on {#total_records} and {#per_page}.
@return [Integer]
# File lib/foliate/pagination.rb, line 38 def total_pages (total_records / per_page.to_f).ceil end