Sha256: 827a4ec488ab65b11eb144abea33e47c4b9d72379278d3286ed67723af3e96d9

Contents?: true

Size: 1.33 KB

Versions: 27

Compression:

Stored size: 1.33 KB

Contents

# Pagination is used to navigate paginateable collections
module LHS::Pagination
  class Base

    DEFAULT_LIMIT = 100

    delegate :_record, to: :data
    attr_accessor :data

    def initialize(data)
      self.data = data
    end

    # as standard in Rails' ActiveRecord count is not summing up, but using the number provided from data source
    def count
      total
    end

    def total
      data._raw[_record.total_key.to_sym]
    end

    def limit
      data._raw[_record.limit_key.to_sym] || DEFAULT_LIMIT
    end

    def offset
      data._raw[_record.pagination_key.to_sym].presence || 0
    end
    alias current_page offset
    alias start offset

    def pages_left
      total_pages - current_page
    end

    def next_offset(_step = 1)
      raise 'to be implemented in subclass'
    end

    def current_page
      raise 'to be implemented in subclass'
    end

    def first_page
      1
    end

    def last_page
      total_pages
    end

    def next?
      data._raw[:next].present?
    end

    def previous?
      data._raw[:previous].present?
    end

    def prev_page
      current_page - 1
    end

    def next_page
      current_page + 1
    end

    def limit_value
      limit
    end

    def total_pages
      (total.to_f / limit).ceil
    end

    def self.page_to_offset(page, _limit)
      page.to_i
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
lhs-12.0.3 lib/lhs/pagination/base.rb
lhs-12.0.2 lib/lhs/pagination/base.rb
lhs-12.0.1 lib/lhs/pagination/base.rb
lhs-12.0.0 lib/lhs/pagination/base.rb
lhs-11.3.3 lib/lhs/pagination/base.rb
lhs-11.3.2 lib/lhs/pagination/base.rb
lhs-11.3.1 lib/lhs/pagination/base.rb
lhs-11.3.0 lib/lhs/pagination/base.rb
lhs-11.2.2 lib/lhs/pagination/base.rb
lhs-11.2.1 lib/lhs/pagination/base.rb
lhs-11.2.0 lib/lhs/pagination/base.rb
lhs-11.1.0 lib/lhs/pagination/base.rb
lhs-11.0.3 lib/lhs/pagination/base.rb
lhs-11.0.2 lib/lhs/pagination/base.rb
lhs-11.0.1 lib/lhs/pagination/base.rb
lhs-11.0.0 lib/lhs/pagination/base.rb
lhs-10.1.1 lib/lhs/pagination/base.rb
lhs-10.1.0 lib/lhs/pagination/base.rb
lhs-10.0.0 lib/lhs/pagination/base.rb
lhs-9.1.1 lib/lhs/pagination/base.rb