Module: ARAfterTransactionConnection

Defined in:
lib/ar_after_transaction.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/ar_after_transaction.rb', line 33

def self.included(base)
  base.class_eval do
    attr_accessor :normally_open_transactions
    attr_accessor :after_transaction_callbacks

    alias_method :transaction_without_after, :transaction
    alias_method :transaction, :transaction_with_after
  end
end

Instance Method Details

#after_transaction(&block) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/ar_after_transaction.rb', line 61

def after_transaction(&block)
  if transactions_open?
    self.after_transaction_callbacks ||= []
    self.after_transaction_callbacks << block
  else
    yield
  end
end

#transaction_with_after(**args) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ar_after_transaction.rb', line 43

def transaction_with_after(**args)
  clean = true
  transaction_without_after(**args) do
    yield
  rescue ActiveRecord::Rollback
    clean = false
    raise
  end
rescue Exception
  clean = false
  raise
ensure
  unless transactions_open?
    callbacks = delete_after_transaction_callbacks
    callbacks.each(&:call) if clean
  end
end