Sha256: f1edcec92602966f9e7932072173c1ce46f9b8b5127c7e1442f3072e87e25b48

Contents?: true

Size: 1.48 KB

Versions: 10

Compression:

Stored size: 1.48 KB

Contents

module Graphiti
  class Scoping::Paginate < Scoping::Base
    DEFAULT_PAGE_SIZE = 20

    def apply
      if size > resource.max_page_size
        raise Graphiti::Errors::UnsupportedPageSize
          .new(size, resource.max_page_size)
      elsif requested? && @opts[:sideload_parent_length].to_i > 1
        raise Graphiti::Errors::UnsupportedPagination
      else
        super
      end
    end

    # We want to apply this logic unless we've explicitly received the
    # +default: false+ option. In that case, only apply if pagination
    # was explicitly specified in the request.
    #
    # @return [Boolean] should we apply this logic?
    def apply?
      if @opts[:default_paginate] == false
        requested?
      else
        true
      end
    end

    # @return [Proc, Nil] the custom pagination proc
    def custom_scope
      resource.pagination
    end

    # Apply default pagination proc via the Resource adapter
    def apply_standard_scope
      resource.adapter.paginate(@scope, number, size)
    end

    # Apply the custom pagination proc
    def apply_custom_scope
      custom_scope.call(@scope, number, size, resource.context)
    end

    private

    def requested?
      ![page_param[:size], page_param[:number]].all?(&:nil?)
    end

    def page_param
      @page_param ||= (query_hash[:page] || {})
    end

    def number
      (page_param[:number] || 1).to_i
    end

    def size
      (page_param[:size] || resource.default_page_size || DEFAULT_PAGE_SIZE).to_i
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
graphiti-1.0.2 lib/graphiti/scoping/paginate.rb
graphiti-1.0.1 lib/graphiti/scoping/paginate.rb
graphiti-1.0.0 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.28 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.27 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.26 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.25 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.24 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.23 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.22 lib/graphiti/scoping/paginate.rb