Skip to content
API/RansackResponseStrategy

RansackResponseStrategy

src/strategies/ransack-response.strategy.ts1 public methodsclass

Response strategy for the Ransack (Rails) driver

Ransack only filters and sorts; the response is shaped by the pagination gem, which sends a bare-array body and the metadata in headers. Both common gems are read without configuration:

Pagy (headers_hash) Kaminari + api-pagination
total total-count total
page size page-limit per-page
current page current-page page (off by default)
last page total-pages link rel="last", else total ÷ page size

The Pagy names come from RansackResponseOptions and can be renamed through PaginationConfig; a configured name is read first, then api-pagination’s. Navigation URLs come from the link header (first, prev — also rel="previous", Pagy before 43.7 — next, last).

Whatever no header names is derived:

  • currentPage: the prev link’s page param + 1, else the next link’s − 1, else 1
  • perPage: the row count, whenever a next link proves the page full
  • lastPage: the last link’s page param, else total ÷ page size rounded up (never below 1)
  • from/to: from the page and page size, or counted back from the total on the last page; undefined on an empty page

Header names are written lower-case, as Rack 3 and HTTP/2 send them. A bag with a get() accessor (the DOM Headers, Angular’s HttpHeaders) matches any casing; a plain object must use the casing of its keys. Cross-origin, the browser exposes these headers only when the server lists them in Access-Control-Expose-Headers.

new RansackResponseStrategy()
paginate(response: RawResponse, options: ResponseOptions, headers?: HeaderBag): PaginatedCollection<T>

Parse a Rails response paginated by Pagy or Kaminari into a typed PaginatedCollection

Parameter Type Description
response RawResponse The raw response: the bare array, or an object with the array at options.data
options ResponseOptions The response configuration; currentPage, lastPage, perPage and total name headers, data a body path
headers HeaderBag The HTTP response headers; omission is tolerated and yields a collection with undefined bounds

Returns — A typed PaginatedCollection instance