pub trait Action<R: Resource>: 'static {
type Input: FormInput;
const NAME: &'static str;
// Required method
fn run(
cx: &Cx,
records: &[R::Model],
input: Self::Input,
ex: &mut dyn Executor,
) -> impl Future<Output = Result<Outcome, WriteError<Self::Input>>> + Send;
// Provided methods
fn declare() -> ActionDef<R> { ... }
fn validate_input(
_cx: &Cx,
_input: &Self::Input,
) -> FieldErrors<Self::Input> { ... }
fn submits(submits: Submits<Self::Input>) -> Submits<Self::Input> { ... }
}Expand description
A mutation beyond create, update and delete: “publish”, “archive”, “resend the invite”.
An action runs on one record, from a button in its table row or in the header of its detail or
edit page, or on the selection, from the bulk bar: ActionDef::places says which.
ResourceDef::action declares it:
struct Publish;
impl Action<PostResource> for Publish {
type Input = ();
const NAME: &'static str = "publish";
fn declare() -> ActionDef<PostResource> {
ActionDef::new().can_run(|_cx, post: &Post| post.status != "published")
}
async fn run(
_cx: &Cx,
posts: &[Post],
_: (),
ex: &mut dyn Executor,
) -> Result<tablo_core::Outcome, tablo_core::WriteError<()>> {
Post::filter(tablo_core::filter_keys(posts))
.update()
.status("published".to_string())
.exec(ex)
.await?;
Ok(tablo_core::Outcome::done())
}
}The framework owns everything around run, as it does for
a delete:
- the route,
{list}/{key}/-/actions/{NAME}for one record and{list}/-/actions/{NAME}for the selection, and its CSRF check; - the transaction: the records are loaded through
scoped_queryinside it,runwrites through the same executor, and an error rolls everything back; - the policy:
Ability::RunAnywith the action’sNAME, asked before the body is read, thenAbility::ViewandAbility::Runon every loaded record beforerun. A recordViewrefuses fails the whole POST with 403; - the refusal: a row refused
Run, by the policy or byActionDef::can_run, answers 403, and a bulk selection runs the records that pass and reports the refused count as skipped. A selection that passes on none writes nothing and answers with an error notification; - the hooks, with
Change::Action, each record as loaded beforerunand as reloaded after it:Resource::before_commitin the transaction afterrun, thenResource::after_commitonce it commits; and the success notification.
An action that asks for an Input asks for it first: its button opens the
input’s form in a dialog over the page, whose submit runs the action with the parsed value. The
submit parses the input, asks validate_input and checks its choices
before the transaction opens, then re-checks a relationship choice inside it. A refused value
renders the input as a page with its errors and writes nothing; a key the input does not
declare answers 400. The page is titled with the label and the record’s title, or the record
count for a selection, and its Cancel returns to the page the button was on. Cancel and Escape
close the dialog, and opening it again starts from a blank form. A searchable relationship
choice searches {list}/-/actions/{NAME}/options, behind the checks the POST makes before it
loads a record. The dialog needs JavaScript.
A run from a detail or edit page lands back on that page, its query kept; one from a row or
the bulk bar lands on the list. An action that takes the record out of the resource’s
query or its View, such as an archive the list hides, lands on
that page’s 404: place it on rows and the bulk bar instead.
The bulk bar of a resource’s list whose rows span pages offers Select all matching: the
selection then takes every record the list’s search and filters select, on any page, rather
than the rows checked. The POST carries the list’s query, and the route re-runs it inside the
transaction, so it takes the records that match when it runs. A record the user may not view
is skipped as a refused one is, and more matches than
Limits::bulk_records write nothing. A filter the list would
ignore answers 400.
No place renders a button for an action the policy refuses RunAny, and no row or page renders
one for a record that fails View or Run. A record can_run refuses shows its button
disabled, saying why, when ActionDef::disabled_reason gives a reason, and none
otherwise. A row that no bulk action and no delete allows renders no checkbox.
Required Associated Constants§
Sourceconst NAME: &'static str
const NAME: &'static str
The action’s URL segment, distinct among the resource’s actions.
ResourceDef::action refuses to compile a name that is not
a single path segment: empty, . or .., or holding whitespace, a control character, a
quote, a backslash or one of / ? # % & = { } ( ).
RouterBuilderPanelExt::panel refuses a name that
another action of the resource shares.
struct Archive;
impl Action<PostResource> for Archive {
type Input = ();
const NAME: &'static str = "archive/all";
}
let def = ResourceDef::<PostResource>::new().action::<Archive>();Required Associated Types§
Required Methods§
Sourcefn run(
cx: &Cx,
records: &[R::Model],
input: Self::Input,
ex: &mut dyn Executor,
) -> impl Future<Output = Result<Outcome, WriteError<Self::Input>>> + Send
fn run( cx: &Cx, records: &[R::Model], input: Self::Input, ex: &mut dyn Executor, ) -> impl Future<Output = Result<Outcome, WriteError<Self::Input>>> + Send
Perform the action on records, the records of the row or selection that
passed Ability::Run and ActionDef::can_run, with the parsed input, through
the framework’s transaction ex, and say what the response does once it commits:
Outcome::done for the ActionDef::success notification,
landing back where the action ran from. filter_keys writes one
change to every record in one statement.
A rule that reads the database refuses the input with WriteError::Refused, keyed by
the input, as validate_input is: the transaction
rolls back and the input page renders with the errors. An action that asks for nothing has
no field to refuse, its WriteError<()>, and declines with
WriteError::declined instead, which rolls back and flashes the message.
Provided Methods§
Sourcefn declare() -> ActionDef<R>
fn declare() -> ActionDef<R>
Where the action’s button renders, how it reads on each record, and which records it runs
on. Defaults to ActionDef::new: a gray button on each row, page header and the bulk
bar, labelled after NAME, running without confirmation on every record the
policy allows.
The resource builds it once, when ResourceDef::action
adds the action, so it takes no request: what changes per record is a closure, such as
ActionDef::label_for or ActionDef::can_run, which receives cx.
Sourcefn validate_input(_cx: &Cx, _input: &Self::Input) -> FieldErrors<Self::Input>
fn validate_input(_cx: &Cx, _input: &Self::Input) -> FieldErrors<Self::Input>
Refuse an input run should not receive, such as a reason too short to
act on: each error names a field of the input, picked off its
Keys, and renders under its control. Defaults to none.
let mut errors = FieldErrors::<Closing>::new();
if closing.reason.trim().len() < 10 {
errors.add(|f| f.reason, "Say why in ten characters or more");
}It runs after the input parses and before the transaction opens, so it sees no record; a
rule that reads the database refuses from run.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".