Triggers

Clear provides a way to create triggers on different time of the lifecycle of the model.

Example usage

class User
    include Clear::Model

    column first_name : String
    column last_name : String

    def full_name
        {first_name, last_name}.join(" ")
    end

    after :create, :send_email

    before(:update) { |m| m.as(User).updated_at = Time.local }

    def send_email
        EmailManager.send_email(subject: "welcome #{full_name} !", body: "...")
    end
end

Caveats

  • Calling before/after with a block will return a Clear::Model as argument. Therefore, you must cast the variable (m.as(User) in example above).

  • before/after :action, :method must be pointing to public method. If the method is private, the call will fail.

Trigger list

Last updated