module Lustra::Model::HasHooks

Overview

This module provides the callback system for Lustra::Model, allowing you to hook into model lifecycle events like :create, :update, :validate, etc.

Direct including types

Defined in:

lustra/model/modules/has_hooks.cr

Macro Summary

Instance Method Summary

Macro Detail

macro after(event_name, method_name) #

Macro version of after - calls a method instead of a block

Example: after(:create, :send_welcome_email) Equivalent to: after(:create) { |model| model.as(User).send_welcome_email }

The macro automatically casts to the correct type for you.


[View source]
macro before(event_name, method_name) #

Macro version of before - calls a method instead of a block

Example: before(:validate, :sanitize_data) Equivalent to: before(:validate) { |model| model.as(User).sanitize_data }

The macro automatically casts to the correct type for you.


[View source]

Instance Method Detail

def trigger_after_events(event_name) #

Triggers the events hooked after event_name


[View source]
def trigger_before_events(event_name) #

Triggers the events hooked before event_name


[View source]
def with_triggers(event_name, &) #

This performs theses operations:

  • Call triggers before the event
  • Yield the given block
  • Call triggers after the event
model.with_triggers("email_sent") do |m|
  model.send_email
end

Returns self


[View source]