Sha256: 85bbd3f6984098ef38df2b89cf323492e76f120de0e31002a5e2c7731ec7775e

Contents?: true

Size: 699 Bytes

Versions: 4

Compression:

Stored size: 699 Bytes

Contents

module DeliveryUncle
  class Paginate
    def initialize(per_page=20)
      @per_page = per_page
    end
    
    def page(model, params) 
      @page = params[:page].present? ? params[:page].to_i :  1
      @params = params
      start = (@page-1) * @per_page 
      start = 0 if start < 0
      @size = model.count
      @model = model.limit(@per_page).offset(start)
      self
    end
    
    def data
      @model
    end

    def prev_page_url
      return nil unless @page > 1
      @params.merge({page: @page-1})
    end

    def next_page_url
      return nil if end_of_page?
      @params.merge(page: @page +1)
    end

    def end_of_page?
      @page*@per_page > @size
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
delivery_uncle-0.1.5 app/services/delivery_uncle/paginate.rb
delivery_uncle-0.1.4 app/services/delivery_uncle/paginate.rb
delivery_uncle-0.1.3 app/services/delivery_uncle/paginate.rb
delivery_uncle-0.1.2 app/services/delivery_uncle/paginate.rb