Module: AfterCommit::ConnectionAdapters

Defined in:
lib/after_commit/connection_adapters.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/after_commit/connection_adapters.rb', line 3

def self.included(base)
  base.class_eval do
    # The commit_db_transaction method gets called when the outermost
    # transaction finishes and everything inside commits. We want to
    # override it so that after this happens, any records that were saved
    # or destroyed within this transaction now get their after_commit
    # callback fired.
    def commit_db_transaction_with_callback
      increment_transaction_nesting_level
      committed = false
      result    = nil
      begin
        trigger_before_commit_callbacks
        trigger_before_commit_on_create_callbacks
        trigger_before_commit_on_update_callbacks
        trigger_before_commit_on_destroy_callbacks
        
        result = commit_db_transaction_without_callback
        committed = true
        
        trigger_after_commit_callbacks
        trigger_after_commit_on_create_callbacks
        trigger_after_commit_on_update_callbacks
        trigger_after_commit_on_destroy_callbacks
        result
      rescue
        if committed
          result
        else
          # Need to decrement the transaction nesting level before calling
          # rollback... to ensure it is not incremented twice
          decrement_transaction_nesting_level
          rollback_db_transaction
          increment_transaction_nesting_level
        end
      ensure
        AfterCommit.cleanup(self)
        decrement_transaction_nesting_level
      end
    end 
    alias_method_chain :commit_db_transaction, :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_db_transaction_with_callback
      increment_transaction_nesting_level
      begin
        result = nil
        trigger_before_rollback_callbacks
        result = rollback_db_transaction_without_callback
        trigger_after_rollback_callbacks
        result
      ensure
        AfterCommit.cleanup(self)
      end
      decrement_transaction_nesting_level
    end
    alias_method_chain :rollback_db_transaction, :callback

    def current_transaction_key
      [object_id, transaction_nesting_level]
    end

    def previous_transaction_key
      [object_id, transaction_nesting_level - 1]
    end
    
    protected
    
    def trigger_before_commit_callbacks
      AfterCommit.records(self).each do |record|
        record.send :callback, :before_commit
      end 
    end

    def trigger_before_commit_on_create_callbacks
      AfterCommit.created_records(self).each do |record|
        record.send :callback, :before_commit_on_create
      end 
    end
  
    def trigger_before_commit_on_update_callbacks
      AfterCommit.updated_records(self).each do |record|
        record.send :callback, :before_commit_on_update
      end 
    end
  
    def trigger_before_commit_on_destroy_callbacks
      AfterCommit.destroyed_records(self).each do |record|
        record.send :callback, :before_commit_on_destroy
      end 
    end

    def trigger_before_rollback_callbacks
      AfterCommit.records(self).each do |record|
        begin
          record.send :callback, :before_rollback
        rescue
          #
        end
      end 
    end

    def trigger_after_commit_callbacks
      # Trigger the after_commit callback for each of the committed
      # records.
      AfterCommit.records(self).each do |record|
        begin
          record.send :callback, :after_commit
        rescue
          #
        end
      end
    end
  
    def trigger_after_commit_on_create_callbacks
      # Trigger the after_commit_on_create callback for each of the committed
      # records.
      AfterCommit.created_records(self).each do |record|
        begin
          record.send :callback, :after_commit_on_create
        rescue
          #
        end
      end
    end
  
    def trigger_after_commit_on_update_callbacks
      # Trigger the after_commit_on_update callback for each of the committed
      # records.
      AfterCommit.updated_records(self).each do |record|
        begin
          record.send :callback, :after_commit_on_update
        rescue
          #
        end
      end
    end
  
    def trigger_after_commit_on_destroy_callbacks
      # Trigger the after_commit_on_destroy callback for each of the committed
      # records.
      AfterCommit.destroyed_records(self).each do |record|
        begin
          record.send :callback, :after_commit_on_destroy
        rescue
          #
        end
      end
    end

    def trigger_after_rollback_callbacks
      # Trigger the after_rollback callback for each of the committed
      # records.
      AfterCommit.records(self).each do |record|
        begin
          record.send :callback, :after_rollback
        rescue
        end
      end 
    end
    

    def transaction_nesting_level
      Thread.current[:after_commit_transaction_nesting_level] ||= 0
    end

    def increment_transaction_nesting_level
      Thread.current[:after_commit_transaction_nesting_level] ||= 0
      Thread.current[:after_commit_transaction_nesting_level] += 1
    end

    def decrement_transaction_nesting_level
      Thread.current[:after_commit_transaction_nesting_level] ||= 0
      Thread.current[:after_commit_transaction_nesting_level] -= 1
    end
  end 
end