Module: AfterCommit::TestConnectionAdapters

Defined in:
lib/after_commit/after_savepoint.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/after_commit/after_savepoint.rb', line 35

def self.included(base)
  base.class_eval do

    def after_callback_transaction_committed?
      committed = (Thread.current[:after_callback_committed] || {})[unique_transaction_key]
      return !committed.nil? && committed
    end

    def after_callback_mark_committed committed          
      (Thread.current[:after_callback_committed] ||= {})[unique_transaction_key] = committed
    end

    def after_callback_cleanup_committed
      (Thread.current[:after_callback_committed] ||= {})[unique_transaction_key] = nil
    end
    
    def release_savepoint_with_callback
      increment_transaction_pointer
      after_callback_mark_committed false
      begin
        trigger_before_commit_callbacks
        trigger_before_commit_on_create_callbacks
        trigger_before_commit_on_save_callbacks
        trigger_before_commit_on_update_callbacks
        trigger_before_commit_on_destroy_callbacks
        
        release_savepoint_without_callback
        after_callback_mark_committed true
        trigger_after_commit_callbacks
        trigger_after_commit_on_create_callbacks
        trigger_after_commit_on_save_callbacks
        trigger_after_commit_on_update_callbacks
        trigger_after_commit_on_destroy_callbacks
        AfterCommit.cleanup(self)
        after_callback_cleanup_committed
      ensure
        decrement_transaction_pointer
      end
    end 
    alias_method_chain :release_savepoint, :callback

    # In the event the transaction fails and rolls back, nothing inside
    # should recieve the after_commit callback, but do fire the after_rollback
    # callback for each record that failed to be committed.
    def rollback_to_savepoint_with_callback
      increment_transaction_pointer
      begin
        # Only rollback if we have not already released rollback
        unless after_callback_transaction_committed?
          trigger_before_rollback_callbacks
          rollback_to_savepoint_without_callback
          trigger_after_rollback_callbacks
        end
      ensure
        AfterCommit.cleanup(self)
        after_callback_cleanup_committed
        decrement_transaction_pointer
      end         
    end
    alias_method_chain :rollback_to_savepoint, :callback
  end
end