This is the official documentation of the agent_ruby Ruby agent.
In Forest Admin, pages that show lists of records have a free-text search bar on top of them.
Search modes
2 search modes are supported: "normal" and "extended".
All searches start by being a "normal" search.
If the records users are looking for are not found, it is possible to trigger an "extended" search from the footer.
Default behavior
Natively, the search behavior is to seek value occurrences within columns of the Collection (in normal mode), and columns of the Collections of direct relations (in extended mode).
By default, Forest Admin will search only in specific columns, depending on their type:
Column Type
Default search behavior
Enum
Column is equal to the search string (case-insensitive).
Number
Column is equal to the search string (if the search string is numeric).
String
Column contains the search string (case-insensitive).
Uuid
Column is equal to the search string.
Other types
Column is ignored by the default search handler.
Customization
If you want to make a column searchable, you must define the right operator to allow the search to be performed. Please refer to the Operators to support table to know which operator to define.
Alternatively, you may want to change how the search behaves in your admin panel.
For instance:
search only on the columns that are relevant to your use case.
use full-text indexes (i.e. Postgres tsquery and tsvector, Algolia, Elastic search, ...)
To customize the search behavior, you must define a handler that returns a ConditionTree.
Making the search case-sensitive by default
In this example, we use the searchExtended condition to toggle between case-sensitive and insensitive searches.
include ForestAdminDatasourceToolkit::Components::Query::ConditionTree
@create_agent.customize_collection('people') do |collection|
collection.replace_search do |search_string, extended_search|
operator = extended_search ? OPERATORS::CONTAINS : OPERATORS::I_CONTAINS
{
aggregator: 'Or',
conditions: [
{ field: 'firstName', operator: operator, value: search_string },
{ field: 'lastName', operator: operator, value: search_string },
]
}
end
end
Changing searched columns
include ForestAdminDatasourceToolkit::Components::Query::ConditionTree
@create_agent.customize_collection('product') do |collection|
product_reference_regexp = /^[a-f]{16}$/i
barcode_regexp = /^[0-9]{10}$/i
collection.replace_search do |search_string, extended_search|
# User is searching using a product reference.
if product_reference_regexp.match?(search_string)
return { field: 'reference', operator: OPERATORS::EQUAL, value: search_string }
end
# User is search a barcode
if barcode_regexp.match?(search_string)
return { field: 'barCode', operator: OPERATORS::EQUAL, value: search_string }
end
# User is searching something else, let's search in the product name only
if !extended_search
return { field: 'name', operator: OPERATORS::CONTAINS, value: search_string }
end
# In "extended" mode, we search on name, description and brand name
{
aggregator: 'Or',
conditions: [
{ field: 'name', operator: OPERATORS::CONTAINS, value: search_string },
{ field: 'description', operator: OPERATORS::CONTAINS, value: search_string },
{ field: 'brand:name', operator: OPERATORS::EQUAL, value: search_string },
]
}
end
end
Calling an external API
If your data is indexed using a SaaS, an external store, or a full-text index, you can call it in the search handler.
require 'algolia'
include ForestAdminDatasourceToolkit::Components::Query::ConditionTree
@create_agent.customize_collection('product') do |collection|
collection.replace_search do |search_string, extended_search|
client = Algolia::Search::Client.create('YourApplicationID', 'YourWriteAPIKey')
index = client.init_index('test_index')
hits = index.search(
search_string,
{
attributesToRetrieve: ['id'],
hitsPerPage: 50,
}
)
{
field: 'id',
operator: OPERATORS::IN,
value: hits.map { |hit| hit['id'] }
}
end
end
Disable the search
By default, the search bar is displayed when at least one field supports the operator used for search based on its type (see this table).
You can remove the search bar by disabling the search on a collection:
@create_agent.customize_collection('product') do |collection|
collection.disable_search
end