Sha256: 8095724d529c492ce8c12a1957acf09fcbaf378ac92a8fb1f5460c84762cd5eb

Contents?: true

Size: 1.4 KB

Versions: 20

Compression:

Stored size: 1.4 KB

Contents

require 'active_support/core_ext/module/delegation'

# 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

    def total
      data._raw.dig(*_record.total_key) || 0
    end

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

    def limit
      data._raw.dig(*_record.limit_key(:body)) || DEFAULT_LIMIT
    end

    def offset
      data._raw.dig(*_record.pagination_key(:body)) || self.class::DEFAULT_OFFSET
    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

20 entries across 20 versions & 1 rubygems

Version Path
lhs-15.3.1 lib/lhs/pagination/base.rb
lhs-15.3.1.pre.fixlhc.1 lib/lhs/pagination/base.rb
lhs-15.3.0 lib/lhs/pagination/base.rb
lhs-15.2.5 lib/lhs/pagination/base.rb
lhs-15.2.4 lib/lhs/pagination/base.rb
lhs-15.2.3 lib/lhs/pagination/base.rb
lhs-15.2.3.pre.favorites.1 lib/lhs/pagination/base.rb
lhs-15.2.2.pre.favorites.1 lib/lhs/pagination/base.rb
lhs-15.2.2 lib/lhs/pagination/base.rb
lhs-15.2.1 lib/lhs/pagination/base.rb
lhs-15.2.0 lib/lhs/pagination/base.rb
lhs-15.1.1 lib/lhs/pagination/base.rb
lhs-15.1.0 lib/lhs/pagination/base.rb
lhs-15.0.2 lib/lhs/pagination/base.rb
lhs-15.0.1 lib/lhs/pagination/base.rb
lhs-15.0.0 lib/lhs/pagination/base.rb
lhs-14.6.5 lib/lhs/pagination/base.rb
lhs-14.6.4 lib/lhs/pagination/base.rb
lhs-14.6.3 lib/lhs/pagination/base.rb
lhs-14.6.2 lib/lhs/pagination/base.rb