Agent Setup
Creating an Agent
The Ruby agent is designed for Rails applications and automatically introspects your data models.config/initializers/forest_admin_rails.rb, secrets and configurationapp/lib/forest_admin_rails/create_agent.rb, datasource setup and collection customizations
config/initializers/forest_admin_rails.rb):
app/lib/forest_admin_rails/create_agent.rb):
ForestAdminRails.configure):
| Option | Type | Required | Description |
|---|---|---|---|
auth_secret | String | Yes | Your FOREST_AUTH_SECRET |
env_secret | String | Yes | Your FOREST_ENV_SECRET |
forest_server_url | String | No | Forest server URL (default: production) |
Customizing Collections
agent.customize_collection(name, &block)
Customize a specific collection with the provided block.| Parameter | Type | Description |
|---|---|---|
name | String | Collection name |
block | Block | Customization block |
Datasources
agent.add_datasource(datasource, options = )
Add a datasource to the agent. Called insideForestAdminRails::CreateAgent.setup!.
| Option | Type | Description |
|---|---|---|
include | Array<String> | Collections to include |
exclude | Array<String> | Collections to exclude |
rename | Hash | Rename collections |
Actions
collection.add_action(name, definition)
Add an action to the collection.| Property | Type | Description |
|---|---|---|
scope | Symbol | Action scope: :Single, :Bulk, or :Global |
execute | Proc | Action execution handler |
form | Array | Dynamic form configuration |
description | String | Action description |
generate_file | Boolean | Whether action returns a file |
submit_button_label | String | Custom button text |
context.collection- Collection instancecontext.filter- Filter for selected recordscontext.caller- User who triggered the actioncontext.form_values- Form values submittedcontext.get_records(fields)- Get multiple recordscontext.get_record(fields)- Get single record (Single scope)context.get_record_ids- Get IDs of selected recordscontext.has_field_changed(field_name)- Check if form field changed
result_builder.success(message, options = {})- Success responseoptions[:html]- Custom HTML to displayoptions[:invalidated]- Array of collection names to refresh
result_builder.error(message, options = {})- Error responseresult_builder.webhook(url, method, headers, body)- Trigger webhookresult_builder.file(stream, filename, mime_type)- Return file downloadresult_builder.redirect_to(path)- Redirect to URLresult_builder.set_header(name, value)- Add HTTP header
Fields
collection.add_field(name, definition)
Add a computed field to the collection.| Property | Type | Required | Description |
|---|---|---|---|
column_type | String | Yes | Field data type |
dependencies | Array | Yes | Fields needed for computation |
get_values | Proc | Yes | Value computation function |
default_value | Any | No | Default value |
enum_values | Array | No | Enum options (if type is Enum) |
'String'- Text'Number'- Numeric value'Boolean'- True/false'Date'- Date with time'Dateonly'- Date without time'Enum'- Enumeration'Json'- JSON object'Uuid'- UUID
collection.import_field(name, options)
Import a field from a related collection.| Option | Type | Description |
|---|---|---|
path | String | Relationship path (e.g., ‘author:full_name’) |
readonly | Boolean | Whether field is read-only |
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.Relationships
collection.add_many_to_one_relation(name, foreign_collection, options)
Add a many-to-one relationship.| Option | Type | Description |
|---|---|---|
foreign_key | String | Foreign key field name |
foreign_key_target | String | Target field (default: ‘id’) |
collection.add_one_to_many_relation(name, foreign_collection, options)
Add a one-to-many relationship.| Option | Type | Description |
|---|---|---|
origin_key | String | Foreign key in related collection |
origin_key_target | String | Target field (default: ‘id’) |
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.| Option | Type | Description |
|---|---|---|
origin_key | String | Foreign key to origin collection |
foreign_key | String | Foreign key to target collection |
origin_key_target | String | Origin target field |
foreign_key_target | String | Foreign target field |
Hooks
collection.add_hook(position, type, handler)
Add a hook to execute code before or after operations.'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
Charts
collection.add_chart(name, definition)
Add a chart to the collection.Search & Sorting
collection.replace_search(definition)
Replace the default search behavior.collection.disable_search
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.'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
Complete Example
Available Operators
Comparison:'Equal','NotEqual''GreaterThan','LessThan''In','NotIn''Present','Blank'
'Contains','NotContains''StartsWith','EndsWith''Like','ILike'(case-insensitive)
'Before','After''Today','Yesterday''PreviousWeek','PreviousMonth','PreviousQuarter','PreviousYear''Past','Future'