Sha256: 0c5a09fb86ca6fe2e19ad9988f70312aa7c4e65681a1c36d69a515d5c934a72a

Contents?: true

Size: 882 Bytes

Versions: 1

Compression:

Stored size: 882 Bytes

Contents

module Paginate
  class Base
    attr_accessor :options
    attr_accessor :scope

    def initialize(scope, options = {})
      @scope = scope

      if options.kind_of?(Hash)
        @options = options
      else
        @options = {page: options.to_i}
      end

      @options.reverse_merge!(Paginate::Config.to_hash)
    end

    def collection_size
      @collection_size ||= options[:collection].size
    end

    def next_page?
      collection_size > options[:size]
    end

    def previous_page?
      page > 1
    end

    def page
      [1, options.fetch(:page, 1).to_i].max
    end

    def offset
      (page - 1) * (limit - 1)
    end

    def limit
      [options[:size], 10].compact.first.to_i + 1
    end

    def to_options
      {limit: limit, offset: offset}
    end

    def to_scope
      scope
        .limit(limit)
        .offset(offset)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
paginate-3.0.0 lib/paginate/base.rb