Clear ORM
  • Welcome to Clear
  • Introduction
    • Setup
  • Model
    • Defining your model
      • Describing your columns
      • Primary Keys
      • Converters
    • Associations
      • belongs_to
      • has_many
      • has_many through
      • has_one
    • Lifecycle
      • Persistence
      • Validations
      • Triggers
    • Batchs operations
      • Bulk update
      • Bulk insert & delete
    • Transactions & Save Points
      • Transaction & Savepoints
      • Connection pool
    • Locks
  • Querying
    • The collection object
      • Filter the query
        • Filter the query – The Expression Engine
        • Find, First, Last, Offset, Limit
        • Aggregation
        • Ordering & Group by
      • Fetching the query
        • Each and Fetch
        • Cursored fetching
        • Model extra attributes
      • Joins
      • Eager Loading
      • Window and CTE
      • Scopes
    • Writing low-level SQL
      • Select Clause
      • Insert Clause
      • Delete Clause
  • Migrations
    • Manage migrations
    • Call migration script
    • Migration CLI
  • Additional and advanced features
    • JSONB
    • Symbol vs String
    • Enums
    • BCrypt
    • Full Text Search
    • Handling multi-connection
  • Other resources
    • API Documentation
    • Inline documentation
    • Github repository
    • Credits
    • Benchmark
Powered by GitBook
On this page
  • Example usage
  • Caveats
  • Trigger list
  1. Model
  2. Lifecycle

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

Trigger symbol

Description

:validate

Is triggered before and after calling valid? method

:save

Is triggered before and after calling save method

:delete

Is triggered before and after destroying a model

:create

Is triggered before and after calling save method, if the model is not yet persisted and save execute INSERT request.

:update

Is triggered before and after calling save method, if the model is already existing and save execute UPDATE request.

:creation_commited

Note: PLANNED FEATURE NOT YET IMPLEMENTED. Is triggered when a transaction is commited, for each model which has been created during the lifetime of a transaction

:update_commited

Note: PLANNED FEATURE NOT YET IMPLEMENTED. Is triggered when a transaction is commited, for each model which has been updated during the lifetime of a transaction

:delete_commited

Note: PLANNED FEATURE NOT YET IMPLEMENTED. Is triggered when a transaction is commited, for each model which has been destroyed during the lifetime of a transaction

PreviousValidationsNextBatchs operations

Last updated 5 years ago