Skip to main content

Callbacks

Trait Callbacks 

Source
pub trait Callbacks: Send {
    // Provided methods
    fn validate_record(&self, errors: &mut Errors) { ... }
    fn before_validation(&mut self) -> impl Future<Output = Result<()>> + Send { ... }
    fn before_save(&mut self) -> impl Future<Output = Result<()>> + Send { ... }
    fn before_create(&mut self) -> impl Future<Output = Result<()>> + Send { ... }
    fn before_update(&mut self) -> impl Future<Output = Result<()>> + Send { ... }
    fn after_create(&mut self) -> impl Future<Output = Result<()>> + Send { ... }
    fn after_update(&mut self) -> impl Future<Output = Result<()>> + Send { ... }
    fn after_save(&mut self) -> impl Future<Output = Result<()>> + Send { ... }
    fn before_destroy(&mut self) -> impl Future<Output = Result<()>> + Send { ... }
    fn after_destroy(&mut self) -> impl Future<Output = Result<()>> + Send { ... }
}
Expand description

Implemented via #[derive(Model)].

use glacier::Model;

#[derive(Model)]
struct BlogPost {
    id: i64,
    title: String,
    #[glacier(skip)]
    cached_html: Option<String>,
}

assert_eq!(BlogPost::TABLE, "blog_posts");
assert_eq!(BlogPost::COLUMNS, &["id", "title"]);

Hooks around saving and destroying a record, run by Model::save/Model::update (in Rails’ order: before_validation, validations, before_save, before_create or before_update, the write, after_create or after_update, after_save) and Model::destroy. Each does nothing unless implemented; an error stops the save and is returned.

ⓘ
#[glacier::model(callbacks)]
pub struct Post { title: String, slug: String }

impl Callbacks for Post {
    async fn before_save(&mut self) -> glacier::Result<()> {
        self.slug = slugify(&self.title);
        Ok(())
    }
}

The changeset path (Post::create, update_changeset) has no callbacks, like Ecto.

Provided Methods§

Source

fn validate_record(&self, errors: &mut Errors)

The model’s own checks, beyond its fields’ validations, as Rails’ validate do … end: add to errors and the save fails with them. Runs on every record save, after before_validation.

Source

fn before_validation(&mut self) -> impl Future<Output = Result<()>> + Send

Source

fn before_save(&mut self) -> impl Future<Output = Result<()>> + Send

Source

fn before_create(&mut self) -> impl Future<Output = Result<()>> + Send

Source

fn before_update(&mut self) -> impl Future<Output = Result<()>> + Send

Source

fn after_create(&mut self) -> impl Future<Output = Result<()>> + Send

Source

fn after_update(&mut self) -> impl Future<Output = Result<()>> + Send

Source

fn after_save(&mut self) -> impl Future<Output = Result<()>> + Send

Source

fn before_destroy(&mut self) -> impl Future<Output = Result<()>> + Send

Source

fn after_destroy(&mut self) -> impl Future<Output = Result<()>> + Send

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§