Sha256: bd4825bf31f5233970585dcb5fc8bcd14019af709f3a198daaea92940dd92270

Contents?: true

Size: 1.75 KB

Versions: 17

Compression:

Stored size: 1.75 KB

Contents

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

  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] || LHS::Pagination::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
    fail 'to be implemented in subclass'
  end

  def current_page
    fail 'to be implemented in subclass'
  end

  def first_page
    1
  end

  def last_page
    total_pages
  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

class LHS::PagePagination < LHS::Pagination

  def current_page
    offset
  end

  def next_offset
    current_page + 1
  end
end

class LHS::StartPagination < LHS::Pagination

  def current_page
    (offset + limit - 1) / limit
  end

  def next_offset
    offset + limit
  end

  def self.page_to_offset(page, limit = LHS::Pagination::DEFAULT_LIMIT)
    (page.to_i - 1) * limit.to_i + 1
  end
end

class LHS::OffsetPagination < LHS::Pagination

  def current_page
    (offset + limit) / limit
  end

  def next_offset
    offset + limit
  end

  def self.page_to_offset(page, limit = LHS::Pagination::DEFAULT_LIMIT)
    (page.to_i - 1) * limit.to_i
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
lhs-6.4.0 lib/lhs/pagination.rb
lhs-6.3.1 lib/lhs/pagination.rb
lhs-6.3.0 lib/lhs/pagination.rb
lhs-6.2.0 lib/lhs/pagination.rb
lhs-6.1.0 lib/lhs/pagination.rb
lhs-6.0.0 lib/lhs/pagination.rb
lhs-5.7.1 lib/lhs/pagination.rb
lhs-5.7.0 lib/lhs/pagination.rb
lhs-5.6.6 lib/lhs/pagination.rb
lhs-5.6.5 lib/lhs/pagination.rb
lhs-5.6.4 lib/lhs/pagination.rb
lhs-5.6.3 lib/lhs/pagination.rb
lhs-5.6.2 lib/lhs/pagination.rb
lhs-5.6.1 lib/lhs/pagination.rb
lhs-5.6.0 lib/lhs/pagination.rb
lhs-5.5.0 lib/lhs/pagination.rb
lhs-5.4.2 lib/lhs/pagination.rb