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_queryinside it,runwrites through the same executor, and an error rolls everything back; - the policy: every record must pass
Ability::Viewandcan_run, checked on the loaded rows beforerun; Resource::after_commitwithMutation::Actiononce 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§
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 {
const NAME: &'static str = "archive/all";
}
let def = ResourceDef::<PostResource>::new().action::<Archive>();Provided Associated Constants§
Required Methods§
Provided Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".