Module: FishTransactions::Callbacks
- 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.
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
86 87 88 |
# File 'lib/fish_transactions/callbacks.rb', line 86 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
97 98 99 |
# File 'lib/fish_transactions/callbacks.rb', line 97 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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/fish_transactions/callbacks.rb', line 46 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 |