Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow options to be passed to grape paginate for kaminari paginate_ar… #124

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/api-pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def pagy_from(collection, options)
else
count = collection.is_a?(Array) ? collection.count : collection.count(:all)
end

Pagy.new(count: count, items: options[:per_page], page: options[:page])
end

Expand All @@ -94,7 +94,10 @@ def paginate_with_kaminari(collection, options, paginate_array_options = {})
options[:per_page] = get_default_per_page_for_kaminari(collection)
end

collection = Kaminari.paginate_array(collection, paginate_array_options) if collection.is_a?(Array)
if collection.is_a?(Array) || (options.key?(:paginate_array) && !!options[:paginate_array])
collection = Kaminari.paginate_array(collection, paginate_array_options)
end

collection = collection.page(options[:page]).per(options[:per_page])
collection.without_count if !collection.is_a?(Array) && !ApiPagination.config.include_total
[collection, nil]
Expand Down
5 changes: 3 additions & 2 deletions lib/grape/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ module Grape
module Pagination
def self.included(base)
Grape::Endpoint.class_eval do
def paginate(collection)
def paginate(collection, options = {})
per_page = ApiPagination.config.per_page_param(params) || route_setting(:per_page)

options = {
:page => ApiPagination.config.page_param(params),
:per_page => [per_page, route_setting(:max_per_page)].compact.min
}
}.merge(options)

collection, pagy = ApiPagination.paginate(collection, options)

links = (header['Link'] || "").split(',').map(&:strip)
Expand Down