Skip to main content

Action

Trait Action 

Source
pub trait Action<R: Resource>: 'static {
    const NAME: &'static str;
    const ROW: bool = true;
    const BULK: bool = true;

    // Required methods
    fn label(cx: &Cx) -> String;
    fn run(
        cx: &Cx,
        records: &[R::Model],
        ex: &mut dyn Executor,
    ) -> impl Future<Output = Result<()>> + Send;

    // Provided methods
    fn can_run(_cx: &Cx, _record: &R::Model) -> bool { ... }
    fn success(cx: &Cx, count: usize) -> String { ... }
}
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 row, or on the selection, from the bulk bar, or both (ROW, BULK). ResourceDef::action declares it:

struct Publish;

impl Action<PostResource> for Publish {
    const NAME: &'static str = "publish";

    fn label(_cx: &Cx) -> String {
        "Publish".to_string()
    }

    fn can_run(_cx: &Cx, post: &Post) -> bool {
        post.status != "published"
    }

    async fn run(_cx: &Cx, posts: &[Post], ex: &mut dyn Executor) -> Result<()> {
        for post in posts {
            Post::filter(Post::fields().id().eq(post.id))
                .update()
                .status("published".to_string())
                .exec(&mut *ex)
                .await?;
        }
        Ok(())
    }
}

The framework owns everything around run, as it does for a delete:

  • the route, {list}/{key}/-/actions/{NAME} for a row and {list}/-/actions/{NAME} for the selection, and its CSRF check;
  • the transaction: the records are loaded through scoped_query inside it, run writes through the same executor, and an error rolls everything back;
  • the policy: every record must pass Ability::View and can_run, checked on the loaded rows before run;
  • Resource::after_commit with Mutation::Action once the transaction commits, and the success notification.

A row whose record fails can_run renders no button for the action, and a row that no bulk action and no delete allows renders no checkbox.

Required Associated Constants§

Source

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 {
    const NAME: &'static str = "archive/all";
}

let def = ResourceDef::<PostResource>::new().action::<Archive>();

Provided Associated Constants§

Source

const ROW: bool = true

Whether a row renders the action’s button. Defaults to true.

Source

const BULK: bool = true

Whether the bulk bar renders the action for the selection. Defaults to true.

Required Methods§

Source

fn label(cx: &Cx) -> String

The button text.

Source

fn run( cx: &Cx, records: &[R::Model], ex: &mut dyn Executor, ) -> impl Future<Output = Result<()>> + Send

Perform the action on records, all of which passed can_run, through the framework’s transaction ex.

Provided Methods§

Source

fn can_run(_cx: &Cx, _record: &R::Model) -> bool

Whether the action may run on record.

Source

fn success(cx: &Cx, count: usize) -> String

The success notification after a commit. Defaults to the label and the record count: "Publish: 3 records".

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§