Sha256: 1106c3b2838939573b72a6a5d5aed150b0ee36f38620d41411f097be6e806c55
Contents?: true
Size: 1.43 KB
Versions: 2
Compression:
Stored size: 1.43 KB
Contents
module ActiveRecord class Relation ## # Access the relation without pagination in a block. Limit and offset values are removed, filters still apply. # # @return [Object] Return value of the block # def without_pagination rel = dup rel.limit! nil rel.offset! nil yield rel end ## # 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 # def total @total ||= without_pagination(&:count) end ## # Calculates the last page for paginated results. # # @return [Integer] Last page as a number # def last_page page = (total / limit_value.to_f).ceil page == 0 ? 1 : page end ## # 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. # 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 end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
yap-2.0.0 | lib/yap/active_record/relation.rb |
yap-1.4.2 | lib/yap/active_record/relation.rb |