Skip to content

Nested operation Pagination

Vladislav Trotsenko edited this page Sep 30, 2019 · 2 revisions

Synopsis

Provides JSON API pagination with offset strategy. Accepts AciveRelation or Array as collection. By default returns 25 items per page.

HTTP request example: GET /users?page[number]=1&page[size]=1

Code

PLease note, before using this nested operation you should set ctx[:model] as AciveRelation or Array.

module Api::V1::Lib::Operation
  class Pagination < ApplicationOperation
    step Macro::Contract::Schema(Api::V1::Lib::Contract::PaginationValidation, name: :uri_query)
    step Contract::Validate(name: :uri_query), fail_fast: true
    step :pagy # sets ctx[:pagy], ctx[:model]
    step :valid_page?
    fail Macro::AddContractError(name: :uri_query, page: [:number, [I18n.t('errors.pagination_overflow')]])
  end
end

Used nested & macroses

Dependent from

  • Service::Pagy
Clone this wiki locally