Customizing Fields
Terrazzo ships with 19 field types. Each field knows how to serialize itself for JSON and has three display modes: index (table cell), show (detail view), and form (input).
Setting Options
Use .with_options to configure any field:
ATTRIBUTE_TYPES = {
name: Field::String.with_options(truncate: 30),
price: Field::Number.with_options(prefix: "$", decimals: 2),
status: Field::Select.with_options(collection: -> { MyModel.statuses.keys }),
author: Field::BelongsTo.with_options(scope: ->(users) { users.where(role: :author) }),
}Field Types
Field::String
name: Field::String- Index: Truncated to 50 characters (configurable with
truncateoption) - Show: Full text
- Form: Text input
Options: truncate (default: 50)
Field::Text
description: Field::Text- Index: Truncated
- Show: Full text
- Form: Textarea
Field::Number
price: Field::Number.with_options(prefix: "$", decimals: 2)Options: prefix, suffix, decimals, multiplier, format
The format option uses ActiveSupport::NumberHelper for advanced formatting:
price: Field::Number.with_options(
format: { formatter: :number_to_currency, formatter_options: { unit: "€" } }
)
percentage: Field::Number.with_options(
format: { formatter: :number_to_percentage, formatter_options: { precision: 1 } }
)Field::Money
price: Field::Money
amount: Field::Money.with_options(prefix: "€", decimals: 2)Like Number but defaults to 2 decimal places. Options: prefix, suffix, decimals. Money uses the number frontend renderer, so eject fields/number when you want to customize built-in money rendering.
Field::Boolean
active: Field::Boolean- Index/Show: Check or X badge
- Form: Checkbox that submits
"1"when checked and"0"when unchecked
Field::Date / Field::DateTime / Field::Time
published_on: Field::Date
created_at: Field::DateTime
starts_at: Field::TimeOptions:
format— Rubystrftimeformat string (e.g.,"%b %d, %Y"). When set, the value is formatted server-side.timezone— Timezone name (e.g.,"Eastern Time (US & Canada)") to convert the value before formatting. Defaults toTime.zone.
Field::Email
email: Field::EmailRenders as a clickable mailto: link on index and show pages.
Field::Url
website: Field::UrlRenders as an external link on index and show pages.
Field::Password
password: Field::PasswordRenders as •••••••• on index and show pages. Password input on forms.
Field::Select
status: Field::Select.with_options(
collection: %w[draft published archived]
)Options: collection — array, proc, or ActiveRecord enum name.
Field::RichText
body: Field::RichTextFor Action Text fields. Renders HTML on show, textarea on form.
Field::BelongsTo
customer: Field::BelongsTo- Index/Show: Link to the associated record
- Form: Select dropdown with all available records
Options: scope (proc to filter records), order (e.g., order: :name to sort dropdown options), searchable_fields, includes. Use the clear-selection button to submit an empty value.
Field::HasMany
orders: Field::HasMany- Index: Count badge
- Show: Table with collection attributes and per-row actions
- Form: Multi-select
Options:
collection_attributes— array of attributes to show in the has_many table on the show page. Defaults to the associated dashboard'sCOLLECTION_ATTRIBUTES. Use this to show fewer or different columns:rubyorders: Field::HasMany.with_options( collection_attributes: [:id, :address_city, :created_at] )scope— proc to filter recordsper_page— number of rows per page in the nested show-page table. Defaults to5; invalid values fall back to5.sort_by— attribute to sort related items on show page (e.g.,sort_by: :created_at)direction— sort direction,:asc(default) or:descincludes— eager-load nested associations (e.g.,includes: [:author])
Field::HasOne
profile: Field::HasOne- Index/Show: Link to the associated record
- Form: Read-only
Options: scope
Field::Polymorphic
commentable: Field::Polymorphic.with_options(
classes: ["Post", "Comment"]
)- Form: Grouped select (by type, then by record)
Options:
classes— array of model class names this association can point toorder— sort candidate resources per class (e.g.,order: :name)
Field::Hstore
metadata: Field::Hstore
settings: Field::Hstore.with_options(truncate: 50)Renders PostgreSQL hstore columns as interactive key-value pair editors. Index shows a truncated preview, show displays each key-value pair with badges, and the form provides an add/remove row editor.
Options: truncate (default: 80)
Field::Asset
avatar: Field::AssetFor Active Storage has_one_attached fields. Shows the filename on index and show pages, file input on forms. The dashboard generator auto-detects has_one_attached declarations.
Enabling Search
All field types default to searchable: false. Enable search on specific fields using .with_options(searchable: true):
ATTRIBUTE_TYPES = {
name: Field::String.with_options(searchable: true),
email: Field::Email.with_options(searchable: true),
owner: Field::BelongsTo.with_options(searchable: true, searchable_fields: ["name"]),
}Search converts numeric and JSON values to text. You can also search store_accessor fields in JSON columns. For association fields, searchable_fields lists columns or store accessors on the associated model. If you omit it, Terrazzo searches all available conventional display columns: name, title, and email. Association searches return unique parent records even when multiple associated rows match. Search treats %, _, and backslashes as literal characters, not SQL wildcards. Search keeps the controller's resource scope, including explicit removal of a model's default scope.
Note: Unlike Administrate, Terrazzo does not auto-enable search on String/Text/Email fields. This avoids accidentally exposing sensitive data in search queries. Always opt in explicitly.
Field Capabilities
| Type | Class | Searchable | Sortable | Eager Load |
|---|---|---|---|---|
| String | Field::String | No | Yes | No |
| Text | Field::Text | No | Yes | No |
| Number | Field::Number | No | Yes | No |
| Money | Field::Money | No | Yes | No |
| Boolean | Field::Boolean | No | Yes | No |
| Date | Field::Date | No | Yes | No |
| DateTime | Field::DateTime | No | Yes | No |
| Time | Field::Time | No | Yes | No |
Field::Email | No | Yes | No | |
| URL | Field::Url | No | Yes | No |
| Password | Field::Password | No | No | No |
| Select | Field::Select | No | Yes | No |
| Rich Text | Field::RichText | No | No | Yes |
| BelongsTo | Field::BelongsTo | No | Yes | Yes |
| HasMany | Field::HasMany | No | No | Yes |
| HasOne | Field::HasOne | No | No | Yes |
| Polymorphic | Field::Polymorphic | No | No | Yes |
| Hstore | Field::Hstore | No | No | No |
| Asset | Field::Asset | No | No | Yes |
Creating a Custom Field
Use the field generator to scaffold a custom field type:
rails g terrazzo:field GravatarThis creates:
app/fields/terrazzo/field/gravatar.rb— Ruby field classapp/views/admin/fields/gravatar/IndexField.jsx— table cell componentapp/views/admin/fields/gravatar/ShowField.jsx— detail view componentapp/views/admin/fields/gravatar/FormField.jsx— form input component
It also creates or updates the app-level field and UI barrels, then registers the components in app/views/admin/fields/index.js so both packaged pages and ejected pages can render the custom field. The generated admin entrypoint imports this barrel before Superglue renders.
The Ruby class
The generated class inherits from Field::Base. Override serialize_value(mode) to control what data is sent to the frontend for each mode (:index, :show, :form):
module Terrazzo
module Field
class Gravatar < Base
def serialize_value(mode)
return nil if data.blank?
email = data.downcase.strip
hash = Digest::MD5.hexdigest(email)
case mode
when :index
{ url: "https://gravatar.com/avatar/#{hash}?s=32", email: email }
when :show
{ url: "https://gravatar.com/avatar/#{hash}?s=128", email: email }
when :form
email
end
end
class << self
def searchable?
false
end
def sortable?
false
end
end
end
end
endUse serializable_options to pass field configuration to the frontend:
def serializable_options
{ size: options.fetch(:size, 128) }
endThe JSX components
Each component receives the serialized value and options as props. Form components also receive attribute, label, hint, input, and required. Index components receive cellOptions when the dashboard returns metadata from collection_cell_options.
For example, ShowField.jsx:
import React from "react";
export function ShowField({ value, options }) {
if (!value) return null;
return (
<img
src={value.url}
alt={value.email}
className="rounded-full"
width={options.size || 128}
/>
);
}Using the field
Reference it in your dashboard like any built-in field:
ATTRIBUTE_TYPES = {
email: Field::Gravatar,
# or with options:
email: Field::Gravatar.with_options(size: 64),
}Base Field API
All field types inherit from Field::Base.
| Method | Description |
|---|---|
#field_type | Returns the underscored type name (e.g., "string", "belongs_to") |
#serialize_value(mode) | Returns the serialized value for :index, :show, or :form mode |
#serializable_options | Returns a hash of options sent to the React component |
#required? | true if the model has a presence validator on this attribute |
.searchable? | Whether this field type supports search (default: false) |
.sortable? | Whether this field type supports sorting (default: true) |
.eager_load? | Whether to eager-load this association (default: false) |
.with_options(opts) | Returns a deferred field with merged options |
.permitted_attribute | Returns the attribute name for strong parameters |
Association option queries
HasMany and BelongsTo form options contain only selected records. Each form field also receives optionsUrl. A GET request to this URL accepts search and returns resourceOptions with the first 10 matches. The query loads at most 10 candidates. Type a more specific query to find other records; there are no page controls or total counts.
The URL uses the parent resource's new or edit action. The controller checks the same parent authorization as the form and permits only fields in that action's form attributes. The response contains no form data. If a host controller overrides new or edit, call render_association_options("create") or render_association_options("update") after setting @resource and checking authorization when params[:association].present?.
The scope option still accepts a relation method or a lambda that receives the associated class. For a scope that depends on the parent record, override candidate_scope and return an Active Record relation. Do not convert that relation to an array. Override option_for(record) to change the option label or add metadata. Each option starts with [label, key_as_string]. The query preserves the association primary key.
Use searchable_fields to specify database columns or store accessors. Without this option, search uses available name, title, email, slug, code, original_filename, and id fields. Search escapes SQL wildcard characters. Set order to control the result order; the association key breaks ties. includes preloads associations needed by custom labels. Selected records remain visible even if the candidate scope no longer includes them.
Custom form components can import AssociationSelect from terrazzo/components. Pass value, options, input, label, and required; add multiple for a collection. onChange(value, option) receives the selected key or key list and the chosen option, or null when a selection is removed. The component preserves selections across searches, cancels stale requests, and provides retry controls. Hidden inputs keep the existing Rails parameter names and empty-value behavior.
The combobox uses a Radix popover and cmdk with Tailwind styles. The search panel matches the field width. Multiple selections appear as removable chips inside the field. Arrow keys move through the matches, Enter selects a match, and Escape closes the panel. Server results are not filtered again in the browser. The field, panel, selected rows, and chips use semantic color tokens for light and dark modes. Install cmdk 1.1.1 or newer with the other frontend peer dependencies.
During a search, the previous result rows stay in place and cannot be selected. The list has a minimum height for its initial and empty states. The search icon changes to a spinner in the same fixed-width slot while input is debounced for 200 ms and the request runs. The search icon returns when the request completes. Loading announcements use a screen-reader status region. The panel changes height only when actual results or an error replace its contents; loading text does not replace the list.
Upgrade the gem and npm package together. Replace custom association dropdowns that expect a complete resourceOptions list. Regenerate or update ejected form fields and the AssociationSelect component.