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
|
# File 'lib/after_commit/active_record.rb', line 3
def self.included(base)
base.class_eval do
class << self
def establish_connection_with_after_commit(spec = nil)
result = establish_connection_without_after_commit spec
include_after_commit_extensions
result
end
alias_method_chain :establish_connection, :after_commit
def include_after_commit_extensions
base = ::ActiveRecord::ConnectionAdapters::AbstractAdapter
Object.subclasses_of(base).each do |klass|
include_after_commit_extension klass
end
if defined?(JRUBY_VERSION) and defined?(JdbcSpec::MySQL)
include_after_commit_extension JdbcSpec::MySQL
end
end
private
def include_after_commit_extension(adapter)
additions = AfterCommit::ConnectionAdapters
unless adapter.included_modules.include?(additions)
adapter.send :include, additions
end
end
end
if respond_to?(:define_callbacks)
define_callbacks :after_commit,
:after_commit_on_create,
:after_commit_on_update,
:after_commit_on_destroy,
:after_rollback,
:before_commit,
:before_commit_on_create,
:before_commit_on_update,
:before_commit_on_destroy,
:before_rollback
else
class << self
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, callbacks)
end
def after_commit_on_update(*callbacks, &block)
callbacks << block if block_given?
write_inheritable_array(:after_commit_on_update, callbacks)
end
def after_commit_on_destroy(*callbacks, &block)
callbacks << block if block_given?
write_inheritable_array(:after_commit_on_destroy, callbacks)
end
def after_rollback(*callbacks, &block)
callbacks << block if block_given?
write_inheritable_array(:after_commit, callbacks)
end
def before_commit(*callbacks, &block)
callbacks << block if block_given?
write_inheritable_array(:before_commit, callbacks)
end
def before_commit_on_create(*callbacks, &block)
callbacks << block if block_given?
write_inheritable_array(:before_commit_on_create, callbacks)
end
def before_commit_on_update(*callbacks, &block)
callbacks << block if block_given?
write_inheritable_array(:before_commit_on_update, callbacks)
end
def before_commit_on_destroy(*callbacks, &block)
callbacks << block if block_given?
write_inheritable_array(:before_commit_on_destroy, callbacks)
end
def before_rollback(*callbacks, &block)
callbacks << block if block_given?
write_inheritable_array(:before_commit, callbacks)
end
end
end
after_create :add_committed_record_on_create
after_update :add_committed_record_on_update
after_destroy :add_committed_record_on_destroy
def add_committed_record_on_create
AfterCommit.record(self.class.connection, self)
AfterCommit.record_created(self.class.connection, self)
end
def add_committed_record_on_update
AfterCommit.record(self.class.connection, self)
AfterCommit.record_updated(self.class.connection, self)
end
def add_committed_record_on_destroy
AfterCommit.record(self.class.connection, self)
AfterCommit.record_destroyed(self.class.connection, self)
end
end
end
|