Sha256: 69c4e5b8d8f673fcc5f731972c836ef8212405cdf7ff75e87fcc869764c9db5b

Contents?: true

Size: 710 Bytes

Versions: 1

Compression:

Stored size: 710 Bytes

Contents

module DataMapper
  module Is
    module Paginated
      def is_paginated(options = {})
        extend  DataMapper::Is::Paginated::ClassMethods
      end

      module ClassMethods
        def paginated(options = {})
          page     = options.delete(:page) || 1
          per_page = options.delete(:per_page) || 5
    
          options.reverse_merge!({
            :order => [:id.desc]
          })
          
          page_count = (count(options.except(:order)).to_f / per_page).ceil
          
          options.merge!({
            :limit => per_page, 
            :offset => (page - 1) * per_page
          })
          
          [ page_count , all(options) ]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
heedley-dm-is-paginated-0.0.1 lib/dm-is-paginated/is/paginated.rb