Sha256: 2fcacb11c579d4e84d5b680de59e724511e8ff79a39d95b5f32d69f541ad2cb3

Contents?: true

Size: 893 Bytes

Versions: 10

Compression:

Stored size: 893 Bytes

Contents

# frozen_string_literal: true
module Shipit
  module Api
    module Paginable
      extend ActiveSupport::Concern

      LINK = 'Link'

      included do
        class_attribute :max_page_size
        class_attribute :default_page_size
        class_attribute :default_order

        self.max_page_size = 100
        self.default_page_size = 30
        self.default_order = { id: :desc }.freeze
      end

      private

      def render_resources(resource, *)
        paginator = Shipit::Paginator.new(
          resource,
          self,
          order: default_order,
          max_page_size: max_page_size,
          default_page_size: default_page_size,
        )
        headers[LINK] = render_links(paginator.links)
        super(paginator.to_a)
      end

      def render_links(links)
        links.map { |rel, url| %(<#{url}>; rel="#{rel}") }.join(', ')
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
shipit-engine-0.39.0 app/controllers/concerns/shipit/api/paginable.rb
shipit-engine-0.38.0 app/controllers/concerns/shipit/api/paginable.rb
shipit-engine-0.37.0 app/controllers/concerns/shipit/api/paginable.rb
shipit-engine-0.36.1 app/controllers/concerns/shipit/api/paginable.rb
shipit-engine-0.36.0 app/controllers/concerns/shipit/api/paginable.rb
shipit-engine-0.35.1 app/controllers/concerns/shipit/api/paginable.rb
shipit-engine-0.35.0 app/controllers/concerns/shipit/api/paginable.rb
shipit-engine-0.34.0 app/controllers/concerns/shipit/api/paginable.rb
shipit-engine-0.33.0 app/controllers/concerns/shipit/api/paginable.rb
shipit-engine-0.32.0 app/controllers/concerns/shipit/api/paginable.rb