module CQL::ActiveRecord::Callbacks

Overview

The Callbacks module provides lifecycle hooks for Active Record models. It allows you to execute code at specific points in the model's lifecycle, similar to Rails ActiveRecord callbacks.

Features:

Example Using the Callbacks module

struct User
  include CQL::ActiveRecord::Model
  db_context AcmeDB, :users

  # Define attributes
  getter id : Int64?
  getter email : String
  getter name : String
  property password : String?
  property password_digest : String?

  # Define callbacks
  before_save :encrypt_password
  after_create :send_welcome_email

  # Constructor
  def initialize(@email : String, @name : String, @password : String? = nil)
  end

  # Callback methods
  private def encrypt_password
    if @password && !@password.empty?
      @password_digest = BCrypt::Password.create(@password).to_s
      @password = nil
    end
    true # Always return true to continue the chain
  end

  private def send_welcome_email
    # Send welcome email
    true
  end
end

Defined in:

active_record/callbacks.cr