module
Avram::SaveCallbacks
Included Modules
Direct including types
Defined in:
avram/callbacks/save_callbacks.crMacro Summary
-
after_save(method_name, if _if = nil, unless _unless = nil)
Run the given method after save, but before transaction is committed
-
after_save(if _if = nil, unless _unless = nil, &block)
Run the given block after save, but before transaction is committed
-
before_save(method_name, if _if = nil, unless _unless = nil)
Run the given method before saving or creating for
SaveOperation
-
before_save(if _if = nil, unless _unless = nil)
Run the given block before saving or creating for
SaveOperation
Macros inherited from module Avram::AfterCommitCallback
after_commit(method_name, if _if = nil, unless _unless = nil)after_commit(if _if = nil, unless _unless = nil, &block) after_commit
Macros inherited from module Avram::CallbackHelpers
conditional_error_for_block_callbacks(callback, condition)
conditional_error_for_block_callbacks
Macro Detail
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
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
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
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