class Foliate::Pagination

Attributes

action[RW]

Originating controller action.

@return [Symbol]

controller[RW]

Originating controller.

@return [Symbol]

current_page[RW]

Current page number.

@return [Integer]

per_page[RW]

Number of records per page.

@return [Integer]

query_params[RW]

Originating request query params.

@return [Hash]

total_records[RW]

Total number of records.

@return [Integer]

Public Instance Methods

each_query_param() { |pair, pair| ... } click to toggle source

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
next?() click to toggle source

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
next_page_params() click to toggle source

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
offset() click to toggle source

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
params_for_page(page_number) click to toggle source

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
prev?() click to toggle source

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
prev_page_params() click to toggle source

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
to_partial_path() click to toggle source

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
total_pages() click to toggle source

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