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):
Customizing Collections
agent.customize_collection(name, &block)
Customize a specific collection with the provided block.Datasources
agent.add_datasource(datasource, options = )
Add a datasource to the agent. Called insideForestAdminRails::CreateAgent.setup!.
Example with Mongoid:
Actions
collection.add_action(name, definition)
Add an action to the collection.
Execute Block:
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.
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
collection.import_field(name, options)
Import a field from a related collection.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.collection.add_one_to_many_relation(name, foreign_collection, options)
Add a one-to-many relationship.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.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'