Module: FishTransactions::ActiveRecordMods::Base

Defined in:
lib/fish_transactions/active_record_mods/base.rb

Overview

Modifications for ActiveRecord::Base class All methods here will be class level methods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

When extended, this module will:

  • alias the :transaction method and them override it



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fish_transactions/active_record_mods/base.rb', line 17

def self.extended( base )

  base.class_eval do
    class << self
      # create an alias for the original +transaction+ method
      alias_method :original_ar_transaction, :transaction
      # and we 'rename' the new method as transaction
      alias_method :transaction, :transaction_with_callbacks
    end
  end

end

Instance Method Details

#callbacksObject

read-access to callbacks storage



61
62
63
# File 'lib/fish_transactions/active_record_mods/base.rb', line 61

def callbacks
  connection.fish_callbacks_storage ||= FishTransactions::CallbacksStorage.new
end

#transaction_with_callbacks(*args) ⇒ Object

Wrapper of original transaction. Captures and then raises Rollback exception to know there were a rollback



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fish_transactions/active_record_mods/base.rb', line 36

def transaction_with_callbacks(*args)

  committed = true

  original_ar_transaction(*args) do
    begin
      yield
    rescue ActiveRecord::Rollback
      committed = false
      raise
    end
  end
rescue Exception
  committed = false
  raise
ensure
  if committed
    callbacks.committed!
  else
    callbacks.rolledback!
  end
end