class Graphiti::Delegates::Pagination

Public Class Methods

new(proxy) click to toggle source
# File lib/graphiti/delegates/pagination.rb, line 4
def initialize(proxy)
  @proxy = proxy
end

Public Instance Methods

has_next_page?() click to toggle source
# File lib/graphiti/delegates/pagination.rb, line 22
def has_next_page?
  current_page != last_page && last_page.present?
end
has_previous_page?() click to toggle source
# File lib/graphiti/delegates/pagination.rb, line 26
def has_previous_page?
  current_page != 1 ||
    !!pagination_params.try(:[], :page).try(:[], :after) ||
    !!pagination_params.try(:[], :page).try(:[], :offset)
end

Private Instance Methods

current_page() click to toggle source
# File lib/graphiti/delegates/pagination.rb, line 95
def current_page
  @current_page ||= (page_param[:number] || 1).to_i
end
item_count() click to toggle source
# File lib/graphiti/delegates/pagination.rb, line 68
def item_count
  begin
    return @item_count if @item_count
    @item_count = item_count_from_proxy || item_count_from_stats
    unless @item_count.is_a?(Numeric)
      raise TypeError, "#{@proxy.resource}.stat(:total, :count) returned an invalid value #{@item_count}"
    end
  rescue
    # FIXME: Unable to log because of how rspec mocks were
    # created for the logger. In other words, logging here will
    # break tests.

    # Graphiti.logger.warn(e.message)
    @item_count = 0
  end
  @item_count
end
item_count_from_proxy() click to toggle source
# File lib/graphiti/delegates/pagination.rb, line 86
def item_count_from_proxy
  @proxy.stats.dig(:total, :count)
end
item_count_from_stats() click to toggle source
# File lib/graphiti/delegates/pagination.rb, line 90
def item_count_from_stats
  stats = Stats::Payload.new(@proxy.resource, @proxy.query, @proxy.scope.unpaginated_object, @proxy.data)
  stats.calculate_stat(:total, @proxy.resource.stat(:total, :count))
end
last_page() click to toggle source
# File lib/graphiti/delegates/pagination.rb, line 54
def last_page
  if @last_page
    return @last_page
  elsif page_size == 0 || item_count == 0
    return nil
  end

  count = item_count
  count = item_count - offset if offset
  @last_page = (count / page_size)
  @last_page += 1 if count % page_size > 0
  @last_page
end
offset() click to toggle source
# File lib/graphiti/delegates/pagination.rb, line 99
def offset
  @offset ||= if (value = page_param[:offset])
    value.to_i
  end
end
page_param() click to toggle source
# File lib/graphiti/delegates/pagination.rb, line 111
def page_param
  @page_param ||= (@proxy.query.hash[:page] || {})
end
page_size() click to toggle source
# File lib/graphiti/delegates/pagination.rb, line 105
def page_size
  @page_size ||= (page_param[:size] ||
                  @proxy.resource.default_page_size ||
                  Graphiti::Scoping::Paginate::DEFAULT_PAGE_SIZE).to_i
end
pagination_params() click to toggle source
# File lib/graphiti/delegates/pagination.rb, line 34
def pagination_params
  @pagination_params ||= @proxy.query.params.reject { |key, _| [:action, :controller, :format].include?(key) }
end