2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-21 14:15:46 -05:00
|
|
|
module API
|
|
|
|
# Concern for declare pagination params.
|
|
|
|
#
|
|
|
|
# @example
|
2020-06-29 17:09:07 -04:00
|
|
|
# class CustomApiResource < Grape::API::Instance
|
2016-11-21 14:15:46 -05:00
|
|
|
# include PaginationParams
|
|
|
|
#
|
|
|
|
# params do
|
|
|
|
# use :pagination
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
module PaginationParams
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
helpers do
|
|
|
|
params :pagination do
|
2017-01-16 23:45:07 -05:00
|
|
|
optional :page, type: Integer, default: 1, desc: 'Current page number'
|
2022-01-31 19:14:06 -05:00
|
|
|
optional :per_page, type: Integer, default: 20, desc: 'Number of items per page', except_values: [0]
|
2016-11-21 14:15:46 -05:00
|
|
|
end
|
2022-08-04 17:08:44 -04:00
|
|
|
|
|
|
|
def verify_pagination_params!
|
|
|
|
return if Feature.disabled?(:only_positive_pagination_values)
|
|
|
|
|
|
|
|
page = begin
|
|
|
|
Integer(params[:page])
|
|
|
|
rescue ArgumentError, TypeError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
return render_structured_api_error!({ error: 'page does not have a valid value' }, 400) if page&.< 1
|
|
|
|
|
|
|
|
per_page = begin
|
|
|
|
Integer(params[:per_page])
|
|
|
|
rescue ArgumentError, TypeError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
return render_structured_api_error!({ error: 'per_page does not have a valid value' }, 400) if per_page&.< 1
|
|
|
|
end
|
2016-11-21 14:15:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|