Sha256: 959fbe920f5c59fb4e6d1b953c793bb406b14ac3c5039b19f4cc3a6fe7a21442
Contents?: true
Size: 1.46 KB
Versions: 2
Compression:
Stored size: 1.46 KB
Contents
# require 'active_support/core_ext/hash/keys' module Might # Paginates ActiveRecord scopes # @example # # Paginator.new(limit: 10, offset: 100).paginate(Movie.all) # # As a side effect of pagination it defines the following methods on collection: # collection#limit # collection#offset # collection#count # collection#total_count # class Paginator InvalidLimitOrOffset = Class.new(StandardError) attr_reader :limit, :offset # @param [{Symbol => Integer}] params # @option params [Integer] :limit # @option params [Integer] :offset # def initialize(options = {}) @limit = Integer(options.fetch(:limit)) @offset = Integer(options.fetch(:offset)) fail InvalidLimitOrOffset if @limit < 0 || @offset < 0 end # Paginate given collection # @param [ActiveRecord::CollectionProxy, ActiveRecord::Base] collection # @return [ActiveRecord::CollectionProxy] # def paginate(collection) paginated_collection = collection.offset(offset).limit(limit) pagination_hash = pagination(collection, paginated_collection) paginated_collection.define_singleton_method(:pagination) do pagination_hash end paginated_collection end private def pagination(collection, paginated_collection) { limit: limit, offset: offset, count: paginated_collection.count(:all), total: collection.count(:all) } end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
might-0.3.1 | lib/might/paginator.rb |
might-0.3.0 | lib/might/paginator.rb |