Ruby Developer Guide
Other documentationsDemoCommunityGitHub
  • Forest Admin
  • Getting started
    • How it works
    • Quick start
      • Ruby on Rails
    • Create your agent
    • Troubleshooting
    • Migrating legacy agents
      • Pre-requisites
      • Recommendations
      • Migration steps
      • Code transformations
        • API Charts
        • Live Queries
        • Smart Charts
        • Route overrides
        • Smart Actions
        • Smart Fields
        • Smart Relationships
        • Smart Segments
  • Data Sources
    • Getting Started
      • Collection selection
      • Naming conflicts
      • Query interface and Native Queries
        • Fields and projections
        • Filters
        • Aggregations
    • Provided data sources
      • ActiveRecord
        • Polymorphic relationships
      • Mongoid
    • Write your own
      • Translation strategy
        • Structure declaration
        • Capabilities declaration
        • Read implementation
        • Write implementation
        • Intra-data source Relationships
      • Contribute
  • Agent customization
    • Getting Started
    • Actions
      • Scope and context
      • Result builder
      • Static Forms
      • Widgets in Forms
      • Dynamic Forms
      • Form layout customization
      • Related data invalidation
    • Charts
      • Value
      • Objective
      • Percentage
      • Distribution
      • Leaderboard
      • Time-based
    • Fields
      • Add fields
      • Move, rename and remove fields
      • Override binary field mode
      • Override writing behavior
      • Override filtering behavior
      • Override sorting behavior
      • Validation
    • Hooks
      • Collection hook
      • Collection override
    • Pagination
    • Plugins
      • Write your own
    • Relationships
      • To a single record
      • To multiple records
      • Computed foreign keys
      • Under the hood
    • Search
    • Segments
  • Frontend customization
    • Smart Charts
      • Create a table chart
      • Create a bar chart
      • Create a cohort chart
      • Create a density map
    • Smart Views
      • Create a Map view
      • Create a Calendar view
      • Create a Shipping view
      • Create a Gallery view
      • Create a custom tinder-like validation view
      • Create a custom moderation view
  • Deploying to production
    • Environments
      • Deploy on AWS
      • Deploy on Heroku
      • Deploy on GCP
      • Deploy on Ubuntu
    • Development workflow
    • Using branches
    • Deploying your changes
    • Forest Admin CLI commands
      • init
      • login
      • branch
      • switch
      • set-origin
      • push
      • environments:create
      • environments:reset
      • deploy
  • Under the hood
    • .forestadmin-schema.json
    • Data Model
      • Typing
      • Relationships
    • Security & Privacy
Powered by GitBook
On this page
  • How it Works
  • Setting Up Overrides
  • Basic Use Cases

Was this helpful?

  1. Agent customization
  2. Hooks

Collection override

This is the official documentation of the agent_ruby Ruby agent.

Forest Admin allows customizing at a very low level the behavior of any given Collection via the usage of Collection Overrides.

Collection Overrides provide a powerful means to completely replace the default behavior of CUD operations (create, update, delete) for your Collections. This feature should be used with caution, as it directly affects the core operations on your data.

How it Works

In addition to the standard Collection functions:

  • create

  • update

  • delete

Collection Overrides allow you to define custom behavior that will entirely replace the default implementation of the create, update, and delete operations.

To define an Override for a Collection, you must specify:

  • The handler function that will be executed instead of the default operation.

The custom handler function will receive a context object containing relevant information for the operation, allowing for comprehensive control over the behavior of these CUD operations.

Setting Up Overrides

Overrides are declared similarly to hooks but are aimed at replacing an entire operation rather than augmenting its execution. However this can also be used to enrich the default behavior. Here's how to set up overrides in your Collection:

Custom Create Operation

To replace the default create operation, use override_create with your custom handler:

collection.override_create do |context|
  # Custom logic to handle creation
  # context.data contains the data intended for creation
  # Return an array of created records
end

Custom Update Operation

To replace the default update operation, use with your custom handler:

collection.override_update do |context|
  # Custom logic to handle update
  # context.filter to determine which records are targeted
  # context.patch contains the data for update
  # Perform update operation
end

Custom Delete Operation

To replace the default delete operation, use with your custom handler:

collection.override_delete do |context|
  # Custom logic to handle deletion
  # context.filter to determine which records are targeted
  # Perform deletion operation
end

Overrides take precedence over the default operation. Ensure your custom handlers properly manage all necessary logic for the operation, as the default behavior will not be executed.

Basic Use Cases

Create over API

You might want to create the record with your custom API:

product.override_create do |context|
  response = HTTParty.post("https://my-product-api.com/products", body: context.data)
  response.parsed_response
end

Modify data before update

You might want to modify payload data before update your record:

product.override_update do |context|
  # Execute data modification and validation only if one of name or slug was edited
  if context.patch.key?('name') || context.patch.key?('slug')
    name = context.patch['name'] || context.patch['slug'].split("-")[0]
    uuid = HTTParty.get('https://my-product-api.com/slug', body: { name: name }).parsed_response

    context.patch['name'] = name
    context.patch['slug'] = "#{name}-#{uuid}"
  end

  context.collection.update(context.filter, context.patch)
end
PreviousCollection hookNextPagination

Last updated 12 months ago

Was this helpful?