Sha256: 24790cb37db923b86294961e613ff40c145c460fb613ffa3249adb8a2110ea01

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

require "grape/kaminari/version"
require "grape/kaminari/max_value_validator"
require "kaminari/grape"

module Grape
  module Kaminari
    def self.included(base)
      base.class_eval do
        helpers do
          def paginate(collection)
            collection.page(params[:page]).per(params[:per_page]).padding(params[:offset]).tap do |data|
              header "X-Total",       data.total_count.to_s
              header "X-Total-Pages", data.num_pages.to_s
              header "X-Per-Page",    params[:per_page].to_s
              header "X-Page",        data.current_page.to_s
              header "X-Next-Page",   data.next_page.to_s
              header "X-Prev-Page",   data.prev_page.to_s
              header "X-Offset",      params[:offset].to_s
            end
          end
        end

        def self.paginate(options = {})
          options.reverse_merge!(
            per_page: ::Kaminari.config.default_per_page || 10,
            max_per_page: ::Kaminari.config.max_per_page,
            offset: 0
          )
          params do
            optional :page,     type: Integer, default: 1,
                                desc: 'Page offset to fetch.'
            optional :per_page, type: Integer, default: options[:per_page],
                                desc: 'Number of results to return per page.',
                                max_value: options[:max_per_page]
            if  options[:offset].is_a? Numeric
              optional :offset, type: Integer, default: options[:offset],
                                desc: 'Pad a number of results.'
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
grape-kaminari-0.1.7 lib/grape/kaminari.rb
grape-kaminari-0.1.6 lib/grape/kaminari.rb