module CQL::ActiveRecord::TimestampManager

Overview

Module for managing timestamp fields (created_at, updated_at) Provides automatic timestamp handling following Active Record conventions

Features:

Example Using timestamp management (the users table has created_at/updated_at columns)

user = User.new("Alice", "alice@example.com")
user.save  # Automatically sets created_at and updated_at
user.touch # Updates updated_at without running callbacks

Defined in:

active_record/timestamp_manager.cr

Macro Summary

Macro Detail

macro apply_timestamps(attrs) #

Apply timestamp attributes to the record

  • @param attrs [Hash(Symbol, DB::Any)] Timestamp attributes to apply

Example Applying timestamps

apply_timestamps(timestamp_attrs)

[View source]
macro has_timestamp_column?(column_name) #

Check if the model has a timestamp column

  • @param column_name [Symbol] The timestamp column name
  • @return [CQL::BaseColumn?] The column, or nil if the table has none by that name

Example Checking for timestamp column

has_timestamp_column?(:created_at) # the column, or nil

[View source]
macro prepare_timestamps(is_new_record) #

Automatically set timestamp fields before save Sets created_at on create and updated_at on both create and update

  • @param is_new_record [Bool] Whether this is a new record
  • @return [Hash(Symbol, DB::Any)] Hash of timestamp attributes to update

Example Setting timestamps

timestamp_attrs = prepare_timestamps(new_record?)

[View source]
macro validate_timestamp_columns(fields) #

Validate that timestamp columns exist in the table

  • @param fields [Array(Symbol)] Field names to validate
  • @raise [CQL::Error] If any field doesn't exist in the table

Example Validating timestamp fields

validate_timestamp_columns([:created_at, :updated_at])

[View source]