Skip to main content

IgniteResource

Derive Macro IgniteResource 

Source
#[derive(IgniteResource)]
{
    // Attributes available to this derive:
    #[ignite]
}
Expand description

Derives IgniteResource for an Orbit model. See ignite_macros for the attributes. Writes impl ignite_core::IgniteResource for a struct that models a table.

Struct attributes:

  • #[ignite(table = widgets)]: the table, required. Its name is the URL slug, and it must be the table of the model’s #[orbit(table = ...)] (or the default name of the struct). A mismatch is a compile error.
  • #[ignite(title = "Widgets")]: the heading and nav label. Defaults to the table name, capitalized.
  • #[ignite(singular = "Widget")]: the name of one record, for “New Widget”. Defaults to the title without a trailing s.
  • #[ignite(navigation_label = "All widgets")]: the label of the resource’s navigation entry, as Filament’s $navigationLabel. Defaults to the title.
  • #[ignite(navigation_group = "Shop")]: the navigation group the entry is listed under, as Filament’s $navigationGroup. Defaults to none: the entry is listed before the groups.
  • #[ignite(navigation_sort = 2)]: the entry’s place, as Filament’s $navigationSort: lower first. Defaults to none, which comes before any number.
  • #[ignite(navigation_icon = "post")]: the name of the entry’s icon, as Filament’s $navigationIcon: what the resource is (post, page, media, comment, users, settings, or a name of the app’s own), not a CSS class. The admin theme maps the name to its icon. Defaults to the theme’s generic resource icon.
  • #[ignite(conditions = post_conditions)]: the conditions that show or hide the form’s fields, and the live fields, as Filament’s visible(), hidden() and live(): the path of a fn() -> ignite_core::FieldConditions. Defaults to none: every field shows. A field that a condition may hide should be an Option, so it is NULL on create and keeps its stored value on update.

Field attributes:

  • primary_key: the key column. Defaults to a field named id.
  • label = "...": the label. Defaults to the field name, capitalized and spaced.
  • form_type = "text" | "textarea" | "password" | "number" | "checkbox" | "date": the input. Defaults from the type.
  • form_type = "email" | "url" | "color" | "link" | "wysiwyg" | "image" | "file" | "gallery": the field types for custom fields (see ignite_core::FieldKind). The field must be a String; an image or file may also be an integer, the media item’s id. A link and a gallery store JSON text.
  • form_type = "radio" with options = [...]: the choices as radio buttons. form_type = "post_object" | "taxonomy" | "user" with options = load_choices or belongs_to: a lookup of that kind. form_type = "relationship" with options = load_choices: several choices, stored as a JSON list in a String.
  • form_type = "repeater" | "group" | "clone" with fields = SUB_FIELDS, or form_type = "flexible_content" with blocks = BLOCKS: a structured field whose sub-fields are a &'static [ignite_core::Field] (or blocks a &'static [ignite_core::Block]) constant. The field must be a String, and stores JSON text.
  • options = [("draft", "Draft"), ...]: a select with a fixed list of choices. The field must be a String.
  • options = load_choices: a select whose choices come from an async function, async fn(&Database) -> Result<Vec<(String, String)>, orbit_orm::Error>, called each time the form is shown. The field must be a String or an integer.
  • belongs_to = Category, display = name: a relation. Category is the related model (an OrbitModel). The field stores the related row’s key, the select lists the related table’s rows by display, and the list shows that name. The field must be an i32 or i64.
  • step = "0.01": the browser’s step for an f32 or f64 input. The default is "any".
  • required or required = false: whether a blank value is rejected. An Option field is optional by default and required makes it required. A field that is not an Option is always required: required = false on it is a compile error (make it an Option instead). A bool is the exception: it is a checkbox and is never blank.
  • searchable, sortable: the list’s search and sort columns.
  • hidden_on_index: leaves the column out of the list.

Supported field types: String, i16, i32, i64, f32, f64, bool, chrono::NaiveDate, chrono::NaiveDateTime, and Option of any of them. The primary key must be i32 or i64. A String field is a text input (or a textarea, a password or a select, by attribute); an f32 or f64 is a decimal input; a NaiveDateTime is a date-and-time input; a bool is a checkbox.

Saving: create writes every field; an Option field left blank is NULL. Update writes every field the form sent: an Option field sent blank is written as NULL, so a user clears an optional value by emptying it on the edit form (since 0.3.0; before, a blank optional field was skipped on update and kept its value), and an Option field the form did not send at all is left out, so its stored value is kept. A password field is the exception: left blank on edit, it is not written, so the stored hash is kept. A checkbox is never blank: one the form did not send is false. A field that is not an Option is required. Errors are keyed by the field’s name in ignite_core::FieldErrors, whose keys are owned Strings since 0.3.0.

The generated input struct (<Model>Input) has a hidden __ignite_sent field that records which optional fields the form sent. validate and validate_update fill it. Code that builds the input with a struct literal must set it (since 0.3.0): __ignite_sent: Vec::new() keeps the stored value of every optional field that is None on update, and listing a field’s name, as in vec!["note"], writes NULL for it. An update that has nothing to write runs no query, and returns the row (or NotFound).