class ActiveRecord::Relation

Public Instance Methods

last_page() click to toggle source

Calculates the last page for paginated results.

@return [Integer] Last page as a number

# File lib/yap/active_record/relation.rb, line 31
def last_page
  page = (total / limit_value.to_f).ceil

  page == 0 ? 1 : page
end
range(include_total = false) click to toggle source

Returns a hash defining a range with :from, :to and optionally :total. Note that querying the total count requires an extra query to be executed.

@param [Boolean] include_total Include total value @return [Hash] Values defining the range of the current page.

# File lib/yap/active_record/relation.rb, line 44
def range(include_total = false)
  from = offset_value + 1
  to = offset_value + limit_value
  to = total if total < to && include_total

  range = { from: from, to: to }
  return range unless include_total

  range.merge(total: total)
end
total() click to toggle source

Returns the total number of results without pagination. This is used for generating range and last_page values. The result for a relation is cached because count can be quite expensive.

@return [Integer] Total number of results

# File lib/yap/active_record/relation.rb, line 22
def total
  @total ||= without_pagination(&:count)
end
without_pagination() { |rel| ... } click to toggle source

Access the relation without pagination in a block. Limit and offset values are removed, filters still apply.

@return [Object] Return value of the block

# File lib/yap/active_record/relation.rb, line 8
def without_pagination
  rel = dup
  rel.limit! nil
  rel.offset! nil

  yield rel
end