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:
- Before/after callbacks for validation, save, create, update and destroy
- Halting the chain when a callback returns false (
savethen returns false) - Several callbacks per event, run in declaration order
Example Using the Callbacks module
class Member
include CQL::ActiveRecord::Model(Int64)
db_context AcmeDB, :users
property name : String = ""
property email : String = ""
before_validation :normalize_email
after_create :send_welcome_email
def initialize(@name : String = "", @email : String = "")
end
private def normalize_email
@email = @email.downcase.strip # any value but false continues the chain
end
private def send_welcome_email
puts "Welcome, #{@email}"
end
end