module FishTransactions::Callbacks

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

Public Instance Methods

after_commit(&block) click to toggle source

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

# File lib/fish_transactions/callbacks.rb, line 128
def after_commit(&block)
  after_transaction(only: :commit, &block)
end
after_rollback(&block) click to toggle source

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

# 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) click to toggle source

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
# File lib/fish_transactions/callbacks.rb, line 88
def after_transaction(opts = {}, &block)

  # default options
  default_options = { only: nil, if_no_transaction: :run}
  opts = default_options.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
Also aliased as: after_tx
after_tx(opts = {}, &block)
Alias for: after_transaction