Module: FishTransactions::Callbacks
- Included in:
- FishTransactions
- Defined in:
- lib/fish_transactions/callbacks.rb
Overview
Fish Transactions Callbacks
This module allows to add transaction callbacks anywhere. You just need to include this module and you will able to use it.
Example of usage:
class SomeClass
include FishTransactions::Callbacks
# ...
def some_method
# ...
ActiveRecord::Base.transaction do
# executes some code
puts "runs within transaction"
after_commit do
# things to do after commit
puts "runs after commit"
end
# executes more code
puts "again runs within transaction"
end
# ...
end
# ...
end
will output
runs within transaction
again runs within transaction
runs after commit
Instance Method Summary collapse
-
#after_commit(&block) ⇒ Object
Executes some code only after current transactions does commit.
-
#after_rollback(&block) ⇒ Object
Executes some code only after current transaction does rollback.
-
#after_transaction(opts = {}, &block) ⇒ Object
(also: #after_tx)
Allows to execute any block of code after transaction completes.
Instance Method Details
#after_commit(&block) ⇒ Object
Executes some code only after current transactions does commit. If no transaction is actually open, the code runs immediately.
Shortcut for after_transaction(only: :commit, if_no_transaction: :run) do ... end
Please use #after_transaction for more options
128 129 130 |
# File 'lib/fish_transactions/callbacks.rb', line 128 def after_commit(&block) after_transaction(only: :commit, &block) end |
#after_rollback(&block) ⇒ Object
Executes some code only after current transaction does rollback. If no transaction is actually open, the code does not runs.
Shortcut for after_transaction(only: :rollback, if_no_transaction: :skip) do ... end
Please use #after_transaction for more options
139 140 141 |
# File 'lib/fish_transactions/callbacks.rb', line 139 def after_rollback(&block) after_transaction(only: :rollback, if_no_transaction: :skip, &block) end |
#after_transaction(opts = {}, &block) ⇒ Object Also known as: after_tx
Allows to execute any block of code after transaction completes. If no transaction is actually open, the code runs immediately.
Accepts the following options that modifies this behavior:
-
:only
- Execute this code only on commit or only on rollback. Accepts one of the following symbols::commit
,:rollback
. -
:if_no_transaction
- Specifies what to do if there is no active transaction. Accepts one of the following symbols::run
(default),:skip
(do not run).
Example of use:
ActiveRecord::Base.transaction do
# executes some code
puts "runs within transaction"
after_transaction do
# things to do after transaction
puts "runs after transaction"
end
# executes more code
puts "again runs within transaction"
end
will output
runs within transaction
again runs within transaction
runs after transaction
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/fish_transactions/callbacks.rb', line 88 def after_transaction(opts = {}, &block) # default options = { only: nil, if_no_transaction: :run} opts = .merge(opts) # normalize opts to string keys normalized = opts.dup opts.each{ |k,v| normalized[k.to_s] = v } opts = normalized if ActiveRecord::Base.connection.open_transactions > 0 callbacks = ActiveRecord::Base.callbacks case opts['only'] when :commit callbacks.store(:commit,&block) when :rollback callbacks.store(:rollback,&block) else # both cases callbacks.store(:commit,&block) callbacks.store(:rollback,&block) end else if opts['if_no_transaction'] == :run block.call end end end |