Skip to main content
Complete API reference for Forest Ruby agent packages.

Agent Setup

Creating an Agent

The Ruby agent is designed for Rails applications and automatically introspects your data models.
Installation:
The generator creates two files:
  • config/initializers/forest_admin_rails.rb, secrets and configuration
  • app/lib/forest_admin_rails/create_agent.rb, datasource setup and collection customizations
Configuration (config/initializers/forest_admin_rails.rb):
Datasource setup and customizations (app/lib/forest_admin_rails/create_agent.rb):
Configuration Options (passed to ForestAdminRails.configure):

Customizing Collections

agent.customize_collection(name, &block)

Customize a specific collection with the provided block.
Parameters:

Datasources

agent.add_datasource(datasource, options = )

Add a datasource to the agent. Called inside ForestAdminRails::CreateAgent.setup!.
Options: Example with Mongoid:

Actions

collection.add_action(name, definition)

Add an action to the collection.
Definition Properties: Execute Block:
ActionContext Methods:
  • context.collection - Collection instance
  • context.filter - Filter for selected records
  • context.caller - User who triggered the action
  • context.form_values - Form values submitted
  • context.get_records(fields) - Get multiple records
  • context.get_record(fields) - Get single record (Single scope)
  • context.get_record_ids - Get IDs of selected records
  • context.has_field_changed(field_name) - Check if form field changed
ResultBuilder Methods:
  • result_builder.success(message, options = {}) - Success response
    • options[:html] - Custom HTML to display
    • options[:invalidated] - Array of collection names to refresh
  • result_builder.error(message, options = {}) - Error response
  • result_builder.webhook(url, method, headers, body) - Trigger webhook
  • result_builder.file(stream, filename, mime_type) - Return file download
  • result_builder.redirect_to(path) - Redirect to URL
  • result_builder.set_header(name, value) - Add HTTP header
Example - Action with Form:
Example - File Generation:

Fields

collection.add_field(name, definition)

Add a computed field to the collection.
Definition Properties: Column Types:
  • 'String' - Text
  • 'Number' - Numeric value
  • 'Boolean' - True/false
  • 'Date' - Date with time
  • 'Dateonly' - Date without time
  • 'Enum' - Enumeration
  • 'Json' - JSON object
  • 'Uuid' - UUID
Example - Async Computed Field:

collection.import_field(name, options)

Import a field from a related collection.
Options:

collection.rename_field(current_name, new_name)

Rename a field in the exported schema.

collection.remove_field(*names)

Remove fields from the exported schema.

collection.replace_field_writing(name, definition)

Replace the write behavior of a field.

Segments

collection.add_segment(name, definition)

Add a segment (saved filter) to the collection.
Example - Static Segment:
Example - Dynamic Segment:
Example - Complex Segment:

Relationships

collection.add_many_to_one_relation(name, foreign_collection, options)

Add a many-to-one relationship.
Options:

collection.add_one_to_many_relation(name, foreign_collection, options)

Add a one-to-many relationship.
Options:

collection.add_one_to_one_relation(name, foreign_collection, options)

Add a one-to-one relationship.

collection.add_many_to_many_relation(name, foreign_collection, through_collection, options)

Add a many-to-many relationship.
Options:

Hooks

collection.add_hook(position, type, handler)

Add a hook to execute code before or after operations.
Hook Types:
  • 'List' - Before/after listing records
  • 'Create' - Before/after creating records
  • 'Update' - Before/after updating records
  • 'Delete' - Before/after deleting records
  • 'Aggregate' - Before/after aggregating data
Example - Before Hook:
Example - After Hook:

Charts

collection.add_chart(name, definition)

Add a chart to the collection.
Chart Types: Value Chart:
Distribution Chart:
Time-based Chart:
Percentage Chart:
Objective Chart:
Leaderboard Chart:

Search & Sorting

collection.replace_search(definition)

Replace the default search behavior.

Disable search functionality on the collection.

collection.replace_field_sorting(name, equivalent_sort)

Replace sorting implementation for a field.

Form Field Types

Action forms support various field types.
Available Types:
  • 'String' - Text input
  • 'Number' - Numeric input
  • 'Boolean' - Checkbox
  • 'Date' - Date picker
  • 'Dateonly' - Date without time
  • 'Enum' - Single selection
  • 'EnumList' - Multiple selection
  • 'File' - File upload
  • 'Json' - JSON editor
  • 'Collection' - Record picker
Collection Field:
Conditional Fields:

Complete Example


Available Operators

Comparison:
  • 'Equal', 'NotEqual'
  • 'GreaterThan', 'LessThan'
  • 'In', 'NotIn'
  • 'Present', 'Blank'
String:
  • 'Contains', 'NotContains'
  • 'StartsWith', 'EndsWith'
  • 'Like', 'ILike' (case-insensitive)
Date:
  • 'Before', 'After'
  • 'Today', 'Yesterday'
  • 'PreviousWeek', 'PreviousMonth', 'PreviousQuarter', 'PreviousYear'
  • 'Past', 'Future'

Context Objects

Caller

User information available in all contexts: