Module: FetcheableOnApi::Pageable

Defined in:
lib/fetcheable_on_api/pageable.rb

Overview

Pageable implements support for JSONAPI-compliant pagination via ‘page` query parameters.

This module enables controllers to process pagination parameters in the format: ‘page=2&page=25` following the JSONAPI specification for page-based pagination.

It handles the controller parameters:

  • page` - The requested page number (default: 1)

  • page` - Number of records per page (default: from configuration)

If no ‘page` parameter is present on the request, the full collection is returned.

The following pagination information is automatically added to response headers:

  • ‘Pagination-Current-Page` - The page number that is returned

  • ‘Pagination-Per` - The number of records included in the page

  • ‘Pagination-Total-Pages` - The total number of pages available

  • ‘Pagination-Total-Count` - The total number of records available

Examples:

Basic pagination setup

class UsersController < ApplicationController
  def index
    users = apply_fetcheable(User.all)
    render json: users
    # Response headers will include pagination info
  end
end

# GET /users?page[number]=2&page[size]=10

With custom default page size

# In config/initializers/fetcheable_on_api.rb
FetcheableOnApi.configure do |config|
  config.pagination_default_size = 50
end

Response headers

# Pagination-Current-Page: 2
# Pagination-Per: 10
# Pagination-Total-Pages: 15
# Pagination-Total-Count: 150

See Also:

Since:

  • 0.1.0