module Jennifer::Model::Callback

Overview

Callbacks are hooks into the life cycle of a model object that allow you to trigger logic before or after an alteration of the object state.

class User < Jennifer::Model::Base
  # mapping()

  before_save :notify
  after_create :send_email, if: admin?

  def notify
    # ...
  end

  def admin?
    role == "admin"
  end
end

Direct including types

Defined in:

jennifer/model/callback.cr

Macro Summary

Macro Detail

macro after_commit(*names, on) #

Defines callbacks which are called after a record has been created, updated, or destroyed.

You can specify that the callback should only be fired by a certain action with the :on option:

after_commit :var, on: :save
after_commit :foo, on: :create
after_commit :bar, on: :update
after_commit :baz, on: :destroy

[View source]
macro after_create(*names) #

Defines callbacks which are called after Base.save on new objects that haven’t been saved yet (no record exists).

Note that these callbacks is still wrapped in the transaction around save.


[View source]
macro after_destroy(*names) #

Defines callbacks which are called after Base.destroy.


[View source]
macro after_initialize(*names) #

Defines callbacks which are called after Base.build call.


[View source]
macro after_rollback(*names, on) #

Defines callbacks which are called after a create, update, or destroy are rolled back.

Please check the documentation of after_commit macro for options.


[View source]
macro after_save(*names) #

Defines callbacks which are called after Base.save (regardless of whether it's a create or update save).

Note that these callbacks are still wrapped in the transaction around save.


[View source]
macro after_update(*names) #

Defines callbacks which are called after Base.save on existing objects that have a record.

Note that these callbacks are still wrapped in the transaction around save.


[View source]
macro after_validation(*names) #

Defines callbacks which are called after Base.validate! (which is part of the Base.save call).


[View source]
macro before_create(*names) #

Defines callbacks which are called before Base.save on new objects that haven’t been saved yet (no record exists).

Note that these callbacks are already wrapped in the transaction around save.


[View source]
macro before_destroy(*names) #

Defines callbacks which are called before Base.destroy


[View source]
macro before_save(*names) #

Defines callbacks which are called before Base.save (regardless of whether it's a create or update save).

Note that these callbacks are already wrapped in the transaction around save.


[View source]
macro before_update(*names) #

Defines callbacks which are called before Base.save on existing objects that have a record.

Note that these callbacks are already wrapped in the transaction around save.


[View source]
macro before_validation(*names) #

Defines callbacks which are called before Base.validate! (which is part of the Base.save call).


[View source]