Sha256: d609e1daa43ba8967a79bc66d9f2fd462350a6f642c2c6280a3b413f21229c5e
Contents?: true
Size: 1.19 KB
Versions: 1
Compression:
Stored size: 1.19 KB
Contents
require "active_support/core_ext/array/extract_options" module Paginary module Relation module Paginated attr_accessor :items_per_page, :current_page def paginate!(*args) options = args.extract_options! self.items_per_page = options[:per_page] || 50 self.current_page = args.first || 1 self.limit_value = items_per_page self.offset_value = items_per_page * (current_page - 1) end def current_page=(page) number = page.to_i unless number.to_s == page.to_s && number.between?(1, page_count) raise ActiveRecord::RecordNotFound, "unknown page #{page}, expected 1..#{page_count}" end @current_page = number end def paginated? page_count > 1 end def page_count # Integer arithmetic is simpler and faster. @page_count ||= item_count > 0 ? (item_count - 1) / items_per_page + 1 : 1 end def item_count @item_count ||= except(:includes, :limit, :offset).count end def first_page? current_page == 1 end def last_page? current_page == page_count end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
paginary-0.0.1.pre2 | lib/paginary/relation/paginated.rb |