Module: Apress::Api::ApiController::Pagination
- Extended by:
- ActiveSupport::Concern
- Included in:
- Base
- Defined in:
- lib/apress/api/api_controller/pagination.rb
Defined Under Namespace
Classes: InvalidPaginationError, LinkHeaderAppendNotImplemented
Constant Summary collapse
- DEFAULT_PER_PAGE =
30
- MAX_PER_PAGE =
100
Instance Method Summary collapse
-
#pagination_headers(collection) ⇒ Object
Public: sets pagination headers.
-
#prepare_pagination(options = {}) ⇒ Object
Public: sets page and per_page variables.
Instance Method Details
#pagination_headers(collection) ⇒ Object
Public: sets pagination headers
collection - WillPaginate::Collection
Returns nothing
53 54 55 56 57 |
# File 'lib/apress/api/api_controller/pagination.rb', line 53 def pagination_headers(collection) raise LinkHeaderAppendNotImplemented if headers['Link'].present? headers.merge!(::Apress::Api::ApiController::PaginationHelper.headers(collection, request.url)) end |
#prepare_pagination(options = {}) ⇒ Object
Public: sets page and per_page variables
options - Hash, configuration
:per_page - Hash, per_page value configuration
:max - Integer, max per_page value
:defuault - Integer, default value for per_page
Examples
prepare_pagination(per_page: {max: 110, default: 40})
or with defaults max - 100 and default - 30
prepare_pagination
Returns nothing
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/apress/api/api_controller/pagination.rb', line 36 def prepare_pagination( = {}) per_page = .fetch(:per_page, {}) max_per_page = per_page.fetch(:max, MAX_PER_PAGE) default_per_page = per_page.fetch(:default, DEFAULT_PER_PAGE) @page = params.fetch(:page, 1).to_i raise InvalidPaginationError if @page <= 0 @per_page = params.fetch(:per_page, default_per_page).to_i raise InvalidPaginationError unless (1..max_per_page).include?(@per_page) end |