Sha256: 7e7f1b0256a7b17f9c4b2dfe005215b74e8a8008ef41527ce130b8a8079afad1

Contents?: true

Size: 1.49 KB

Versions: 25

Compression:

Stored size: 1.49 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?
      not [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

25 entries across 25 versions & 1 rubygems

Version Path
graphiti-1.0.rc.21 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.19 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.18 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.17 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.16 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.15 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.14 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.12 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.11 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.10 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.9 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.8 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.7 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.6 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.5 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.4 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.3 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.2 lib/graphiti/scoping/paginate.rb
graphiti-1.0.rc.1 lib/graphiti/scoping/paginate.rb
graphiti-1.0.beta.23 lib/graphiti/scoping/paginate.rb