module Avram::Callbacks

Direct including types

Defined in:

avram/callbacks.cr

Macro Summary

Macro Detail

macro after_commit(method_name, if _if = nil, unless _unless = nil) #

Run the given method after save and after successful transaction commit

Optionally you can pass an if or unless argument which allows you to run this conditionally. The symbol should reference a method you've defined that returns a truthy/falsey value

The newly saved record will be passed to the method.

class SaveComment < Comment::SaveOperation
  after_commit notify_post_author

  private def notify_post_author(comment : Comment)
    NewCommentNotificationEmail.new(comment, to: comment.author!).deliver_now
  end
end

[View source]
macro after_commit(if _if = nil, unless _unless = nil, &block) #

Run the given block after save and after successful transaction commit

The newly saved record will be passed to the method.

class SaveComment < Comment::SaveOperation
  after_commit do |comment|
    NewCommentNotificationEmail.new(comment, to: comment.author!).deliver_now
  end
end

[View source]
macro after_delete(method_name, if _if = nil, unless _unless = nil) #

Same as after_save but with a different name


[View source]
macro after_delete(if _if = nil, unless _unless = nil, &block) #

[View source]
macro after_run(method_name) #

Run the given method after run is called on an Operation. The return value of the run method is passed to method_name.

after_run :log_entry

private def log_entry(value)
  log_stuff(value)
end

[View source]
macro after_run(&block) #

Run the given block after the operation runs

The return value from run will be passed to this block.

class GenerateReport < Avram::Operation
  after_run do |value|
    value == "some report"
  end

  def run
    "some report"
  end
end

[View source]
macro after_save(method_name, if _if = nil, unless _unless = nil) #

Run the given method after save, but before transaction is committed

Optionally you can pass an if or unless argument which allows you to run this conditionally. The symbol should reference a method you've defined that returns a truthy/falsey value

This is a great place to do other database saves because if something goes wrong the whole transaction would be rolled back.

The newly saved record will be passed to the method.

class SaveComment < Comment::SaveOperation
  after_save touch_post

  private def touch_post(comment : Comment)
    SavePost.update!(comment.post!, updated_at: Time.utc)
  end
end

This is not a good place to do things like send messages, enqueue background jobs, or charge payments. Since the transaction could be rolled back the record may not be persisted to the database. Instead use after_commit


[View source]
macro after_save(if _if = nil, unless _unless = nil, &block) #

Run the given block after save, but before transaction is committed

Optionally you can pass an if or unless argument which allows you to run this conditionally. The symbol should reference a method you've defined that returns a truthy/falsey value

This is a great place to do other database saves because if something goes wrong the whole transaction would be rolled back.

The newly saved record will be passed to the method.

class SaveComment < Comment::SaveOperation
  after_save do |comment|
    SavePost.update!(comment.post!, updated_at: Time.utc)
  end
end

This is not a good place to do things like send messages, enqueue background jobs, or charge payments. Since the transaction could be rolled back the record may not be persisted to the database. Instead use after_commit


[View source]
macro before_delete(method_name, if _if = nil, unless _unless = nil) #

Same as before_save, but with a different name


[View source]
macro before_delete(if _if = nil, unless _unless = nil) #

[View source]
macro before_run(method_name) #

Run the given method before run is called on an Operation.

before_run :validate_inputs

private def validate_inputs
  validate_required data
end

[View source]
macro before_run #

Run the given block before run is called on an Operation.

before_run do
  validate_required data
end

[View source]
macro before_save(method_name, if _if = nil, unless _unless = nil) #

Run the given method before saving or creating for SaveOperation

This runs before saving and before the database transaction is started. You can set defaults, validate, or perform any other setup necessary for saving.

Optionally you can pass an if or unless argument which allows you to run this conditionally. The symbol should reference a method you've defined that returns a truthy/falsey value

before_save :run_validations
before_save :validate_can_internet, unless: :too_cool_for_school?

private def run_validations
  validate_required name, age
end

private def validate_can_internet
  validate_size_of age, min: 13
end

private def too_cool_for_school?
  [true, false].sample
end

[View source]
macro before_save(if _if = nil, unless _unless = nil) #

Run the given block before saving or creating for SaveOperation

This runs before saving and before the database transaction is started. You can set defaults, validate, or perform any other setup necessary for saving.

Optionally you can pass an if or unless argument which allows you to run this conditionally. The symbol should reference a method you've defined that returns a truthy/falsey value

before_save(unless: :skip_callback?) do
  validate_required name, age
end

private def skip_callback?
  false
end

[View source]
macro conditional_error_for_block_callbacks(callback, condition) #

[View source]