Module: FetcheableOnApi

Defined in:
lib/fetcheable_on_api.rb,
lib/fetcheable_on_api/version.rb,
lib/fetcheable_on_api/pageable.rb,
lib/fetcheable_on_api/sortable.rb,
lib/fetcheable_on_api/filterable.rb,
lib/fetcheable_on_api/configuration.rb,
lib/generators/fetcheable_on_api/install_generator.rb

Overview

FetcheableOnApi provides standardized sorting, filtering and pagination for Rails API controllers following the JSONAPI specification.

This gem automatically adds support for query parameters like:

  • filter[attribute]=value for filtering data
  • sort=attribute1,-attribute2 for sorting (- prefix for descending)
  • page[number]=1&page[size]=25 for pagination

Examples:

Basic usage in a controller

class UsersController < ApplicationController
  # Configure allowed filters and sorts
  filter_by :name, :email, :status
  sort_by :name, :created_at, :updated_at

  def index
    users = apply_fetcheable(User.all)
    render json: users
  end
end

Using with associations

class PostsController < ApplicationController
  filter_by :title
  filter_by :author, class_name: User, as: 'name'
  sort_by :title, :created_at
  sort_by :author, class_name: User, as: 'name'

  def index
    posts = apply_fetcheable(Post.joins(:author).includes(:author))
    render json: posts
  end
end

Author:

  • Fabien Piette

Since:

  • 0.1.0

Defined Under Namespace

Modules: Filterable, Generators, Pageable, Sortable Classes: Configuration

Constant Summary collapse

ArgumentError =

Raised when invalid parameters are provided to filtering, sorting, or pagination

Examples:

raise FetcheableOnApi::ArgumentError, "Invalid filter parameter type"

Since:

  • 0.1.0

Class.new(ArgumentError)
NotImplementedError =

Raised when a feature is not yet implemented or supported

Examples:

raise FetcheableOnApi::NotImplementedError, "Custom predicate not supported"

Since:

  • 0.1.0

Class.new(NotImplementedError)
VERSION =

Current version of the FetcheableOnApi gem. Follows semantic versioning (semver.org) principles.

Returns:

  • (String) —

    The version string in format "MAJOR.MINOR.PATCH"

Since:

  • 0.1.0

'0.6.1'.freeze

Class Method Summary collapse

Class Method Details

.configuration ⇒ Configuration

Global configuration settings for FetcheableOnApi. This method provides access to the singleton configuration instance that can be used to customize default behavior across the application.

Examples:

Set default pagination size

FetcheableOnApi.configuration.pagination_default_size = 25

Returns:

See Also:

Since:

  • 0.1.0



56
57
58
# File 'lib/fetcheable_on_api.rb', line 56

def self.configuration
  @configuration ||= Configuration.new
end

.configure {|Configuration| ... } ⇒ Object

Configure FetcheableOnApi using a block. This is the recommended way to set up configuration in an initializer.

Examples:

Set default pagination size in config/initializers/fetcheable_on_api.rb

FetcheableOnApi.configure do |config|
  config.pagination_default_size = 50
end

Yields:

  • (Configuration) —

    Gives the global configuration instance to the block

See Also:

Since:

  • 0.1.0



70
71
72
# File 'lib/fetcheable_on_api.rb', line 70

def self.configure
  yield(configuration)
end

.included(klass) ⇒ Object

Hook called when this module is included in a class. Automatically includes the three main concern modules that provide filtering, sorting, and pagination functionality.

Parameters:

  • klass (Class) —

    The class that is including FetcheableOnApi

Since:

  • 0.1.0



94
95
96
97
98
99
100
# File 'lib/fetcheable_on_api.rb', line 94

def self.included(klass)
  klass.class_eval do
    include Filterable
    include Sortable
    include Pageable
  end
end