-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathafter_commit.rb
More file actions
92 lines (75 loc) · 2.14 KB
/
after_commit.rb
File metadata and controls
92 lines (75 loc) · 2.14 KB
1
2
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
require 'rubygems'
require 'ruby-debug'
Debugger.start
module AfterCommit
@@records = {}
@@records_queue = []
@@record_methods = {
:all => :committed_records,
:create => :committed_records_on_create,
:update => :committed_records_on_update,
:destroy => :committed_records_on_destroy
}
@@callback_methods = {
:all => :after_commit_callback,
:create => :after_commit_on_create_callback,
:update => :after_commit_on_update_callback,
:destroy => :after_commit_on_destroy_callback
}
def self.records
@@records
end
# Push a new variable holder onto our stack.
def self.push
@@records_queue.push @@records
@@records = {}
end
def self.pop
@@records = @@records_queue.pop
end
# Send callbacks of this type after a commit.
# We push a new variable holder on each record, then pop it off, which avoids
# an infinite loop whereby an on_commit callback makes a new transaction
# (like in creating a BackgrounDRb record)
def self.callback(crud_method, &block)
record_method = @@record_methods[crud_method]
callback_method = @@callback_methods[crud_method]
committed_records = send(record_method)
unless committed_records.empty?
committed_records.each do |record|
push
record.send(callback_method)
pop
end
end
records[crud_method] = []
end
def self.clear!
@@records = {}
@@records_queue = []
end
def self.committed_records
records[:all] ||= []
end
def self.committed_records=(committed_records)
records[:all] = committed_records
end
def self.committed_records_on_create
records[:create] ||= []
end
def self.committed_records_on_create=(committed_records)
records[:create] = committed_records
end
def self.committed_records_on_update
records[:update] ||= []
end
def self.committed_records_on_update=(committed_records)
records[:update] = committed_records
end
def self.committed_records_on_destroy
records[:destroy] ||= []
end
def self.committed_records_on_destroy=(committed_records)
records[:destroy] = committed_records
end
end