Module: AfterCommit::ActiveRecord

Defined in:
lib/after_commit/active_record.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
# File 'lib/after_commit/active_record.rb', line 9

def self.included(base)
  base.class_eval do
    # The define_callbacks method was added post Rails 2.0.2 - if it
    # doesn't exist, we define the callback manually
    if respond_to?(:define_callbacks)
      define_callbacks  :after_commit,
                        :after_commit_on_create,
                        :after_commit_on_update,
                        :after_commit_on_destroy
    else
      class << self
        # Handle after_commit callbacks - call all the registered callbacks.
        def after_commit(*callbacks, &block)
          callbacks << block if block_given?
          write_inheritable_array(:after_commit, callbacks)
        end
        
        def after_commit_on_create(*callbacks, &block)
          callbacks << block if block_given?
          write_inheritable_array(:after_commit_on_create_callback, callbacks)
        end
        
        def after_commit_on_update(*callbacks, &block)
          callbacks << block if block_given?
          write_inheritable_array(:after_commit_on_update_callback, callbacks)
        end
        
        def after_commit_on_destroy(*callbacks, &block)
          callbacks << block if block_given?
          write_inheritable_array(:after_commit_on_destroy_callback, callbacks)
        end
      end
    end

    after_save    :add_committed_record
    after_create  :add_committed_record_on_create
    after_update  :add_committed_record_on_update
    after_destroy :add_committed_record_on_destroy

    # We need to keep track of records that have been saved or destroyed
    # within this transaction.
    def add_committed_record
      AfterCommit.committed_records << self
    end
    
    def add_committed_record_on_create
      AfterCommit.committed_records_on_create << self
    end
    
    def add_committed_record_on_update
      AfterCommit.committed_records_on_update << self
    end
    
    def add_committed_record_on_destroy
      AfterCommit.committed_records << self
      AfterCommit.committed_records_on_destroy << self
    end

    # Wraps a call to the private callback method so that the the
    # after_commit callback can be made from the ConnectionAdapters when
    # the commit for the transaction has finally succeeded. 
    def after_commit_callback
      callback(:after_commit)
    end
    
    def after_commit_on_create_callback
      callback(:after_commit_on_create)
    end
    
    def after_commit_on_update_callback
      callback(:after_commit_on_update)
    end
    
    def after_commit_on_destroy_callback
      callback(:after_commit_on_destroy)
    end
  end
end