integralimpressions-after_commit 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +20 -0
- data/README +34 -0
- data/Rakefile +57 -0
- data/lib/after_commit.rb +9 -0
- data/lib/after_commit/active_record.rb +88 -0
- data/lib/after_commit/after_commit.rb +33 -0
- data/lib/after_commit/connection_adapters.rb +103 -0
- metadata +72 -0
data/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2008 Nick Muerdter
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
= after_commit
|
|
2
|
+
|
|
3
|
+
A Ruby on Rails plugin and Ruby Gem to add after_commit callbacks to ActiveRecord. The callbacks that are provided can be used to trigger events that run only after the entire transaction is complete. This is beneficial in situations where you are doing asynchronous processing and need committed objects.
|
|
4
|
+
|
|
5
|
+
The following callbacks are provided:
|
|
6
|
+
|
|
7
|
+
* (1) after_commit
|
|
8
|
+
* (2) after_commit_on_create
|
|
9
|
+
* (3) after_commit_on_update
|
|
10
|
+
* (4) after_commit_on_destroy
|
|
11
|
+
|
|
12
|
+
The after_commit callback is run for any object that has just been committed. You can obtain finer
|
|
13
|
+
callback control by using the additional <tt>after_commit_on_*</tt> callbacks.
|
|
14
|
+
|
|
15
|
+
== Plugin Usage
|
|
16
|
+
|
|
17
|
+
Plugin code is at http://github.com/delynn/after_commit/tree/master
|
|
18
|
+
|
|
19
|
+
script/plugin install git://github.com/delynn/after_commit.git
|
|
20
|
+
|
|
21
|
+
== Installing the Gem
|
|
22
|
+
|
|
23
|
+
If you are using Merb or another Ruby implementation and require a gem install, use the following command:
|
|
24
|
+
|
|
25
|
+
sudo gem install #######
|
|
26
|
+
|
|
27
|
+
The gem can be added to a project by including the code:
|
|
28
|
+
|
|
29
|
+
require 'after_commit'
|
|
30
|
+
|
|
31
|
+
The thinking-sphinx gem already includes this line, so it should not be needed for use with this package.
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
data/Rakefile
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
require 'rake'
|
|
2
|
+
require 'rake/testtask'
|
|
3
|
+
require 'rake/rdoctask'
|
|
4
|
+
require 'rake/gempackagetask'
|
|
5
|
+
|
|
6
|
+
PLUGIN = "after_commit"
|
|
7
|
+
NAME = PLUGIN
|
|
8
|
+
GEM_VERSION = "0.0.1"
|
|
9
|
+
AUTHORS = [ 'Eli Miller', 'Joost Hietbrink', 'DeLynn Barry', 'Kane Baccigalupi']
|
|
10
|
+
EMAIL = "kane@integralimpressions.com"
|
|
11
|
+
HOMEPAGE = "http://github.com/integralimpressions/after_commit"
|
|
12
|
+
SUMMARY = %q{after_commit is an ActiveRecord gem to add an after_commit callback. This can be used to trigger things only after the entire transaction is complete.}
|
|
13
|
+
|
|
14
|
+
windows = (PLATFORM =~ /win32|cygwin/)
|
|
15
|
+
|
|
16
|
+
SUDO = windows ? "" : "sudo"
|
|
17
|
+
|
|
18
|
+
spec = Gem::Specification.new do |s|
|
|
19
|
+
s.name = NAME
|
|
20
|
+
s.version = GEM_VERSION
|
|
21
|
+
s.platform = Gem::Platform::RUBY
|
|
22
|
+
s.has_rdoc = true
|
|
23
|
+
s.extra_rdoc_files = [ "LICENSE", "README" ]
|
|
24
|
+
s.summary = SUMMARY
|
|
25
|
+
s.description = s.summary
|
|
26
|
+
s.authors = AUTHORS
|
|
27
|
+
s.email = EMAIL
|
|
28
|
+
s.homepage = HOMEPAGE
|
|
29
|
+
#s.add_dependency("activerecord", ">=1.0")
|
|
30
|
+
#s.add_dependency("activesupport", ">=1.0")
|
|
31
|
+
s.require_path = 'lib'
|
|
32
|
+
s.autorequire = PLUGIN
|
|
33
|
+
s.files = %w(LICENSE README Rakefile) + Dir.glob("{lib,specs}/**/*")
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
desc "Build gemspec file"
|
|
37
|
+
task :build do
|
|
38
|
+
File.open('after_commit.gemspec', 'w') { |f| f.write spec.to_ruby }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
|
42
|
+
pkg.gem_spec = spec
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
desc "Run :package and install resulting .gem"
|
|
46
|
+
task :install => [:package] do
|
|
47
|
+
sh %{#{SUDO} gem install pkg/#{NAME}-#{GEM_VERSION} --no-rdoc --no-ri}
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
desc 'Generate documentation for the after_commit gem.'
|
|
51
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
52
|
+
rdoc.rdoc_dir = 'rdoc'
|
|
53
|
+
rdoc.title = 'AfterCommit'
|
|
54
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
|
55
|
+
rdoc.rdoc_files.include('README')
|
|
56
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
57
|
+
end
|
data/lib/after_commit.rb
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
require 'after_commit/after_commit'
|
|
2
|
+
require 'after_commit/active_record'
|
|
3
|
+
require 'after_commit/connection_adapters'
|
|
4
|
+
|
|
5
|
+
ActiveRecord::Base.send(:include, AfterCommit::ActiveRecord)
|
|
6
|
+
|
|
7
|
+
Object.subclasses_of(ActiveRecord::ConnectionAdapters::AbstractAdapter).each do |klass|
|
|
8
|
+
klass.send(:include, AfterCommit::ConnectionAdapters)
|
|
9
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# Based on the code found in Thinking Sphinx:
|
|
2
|
+
# http://ts.freelancing-gods.com/ which was based on code written by Eli Miller:
|
|
3
|
+
# http://elimiller.blogspot.com/2007/06/proper-cache-expiry-with-aftercommit.html
|
|
4
|
+
# with slight modification from Joost Hietbrink, and DeLynn Barry.
|
|
5
|
+
# Change to gem packaging for use with Merb/AR by Kane Baccigalupi.
|
|
6
|
+
|
|
7
|
+
module AfterCommit
|
|
8
|
+
module ActiveRecord
|
|
9
|
+
def self.included(base)
|
|
10
|
+
base.class_eval do
|
|
11
|
+
# The define_callbacks method was added post Rails 2.0.2 - if it
|
|
12
|
+
# doesn't exist, we define the callback manually
|
|
13
|
+
if respond_to?(:define_callbacks)
|
|
14
|
+
define_callbacks :after_commit,
|
|
15
|
+
:after_commit_on_create,
|
|
16
|
+
:after_commit_on_update,
|
|
17
|
+
:after_commit_on_destroy
|
|
18
|
+
else
|
|
19
|
+
class << self
|
|
20
|
+
# Handle after_commit callbacks - call all the registered callbacks.
|
|
21
|
+
def after_commit(*callbacks, &block)
|
|
22
|
+
callbacks << block if block_given?
|
|
23
|
+
write_inheritable_array(:after_commit, callbacks)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def after_commit_on_create(*callbacks, &block)
|
|
27
|
+
callbacks << block if block_given?
|
|
28
|
+
write_inheritable_array(:after_commit_on_create_callback, callbacks)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def after_commit_on_update(*callbacks, &block)
|
|
32
|
+
callbacks << block if block_given?
|
|
33
|
+
write_inheritable_array(:after_commit_on_update_callback, callbacks)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def after_commit_on_destroy(*callbacks, &block)
|
|
37
|
+
callbacks << block if block_given?
|
|
38
|
+
write_inheritable_array(:after_commit_on_destroy_callback, callbacks)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
after_save :add_committed_record
|
|
44
|
+
after_create :add_committed_record_on_create
|
|
45
|
+
after_update :add_committed_record_on_update
|
|
46
|
+
after_destroy :add_committed_record_on_destroy
|
|
47
|
+
|
|
48
|
+
# We need to keep track of records that have been saved or destroyed
|
|
49
|
+
# within this transaction.
|
|
50
|
+
def add_committed_record
|
|
51
|
+
AfterCommit.committed_records << self
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def add_committed_record_on_create
|
|
55
|
+
AfterCommit.committed_records_on_create << self
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def add_committed_record_on_update
|
|
59
|
+
AfterCommit.committed_records_on_update << self
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def add_committed_record_on_destroy
|
|
63
|
+
AfterCommit.committed_records << self
|
|
64
|
+
AfterCommit.committed_records_on_destroy << self
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Wraps a call to the private callback method so that the the
|
|
68
|
+
# after_commit callback can be made from the ConnectionAdapters when
|
|
69
|
+
# the commit for the transaction has finally succeeded.
|
|
70
|
+
def after_commit_callback
|
|
71
|
+
callback(:after_commit)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def after_commit_on_create_callback
|
|
75
|
+
callback(:after_commit_on_create)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def after_commit_on_update_callback
|
|
79
|
+
callback(:after_commit_on_update)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def after_commit_on_destroy_callback
|
|
83
|
+
callback(:after_commit_on_destroy)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
module AfterCommit
|
|
2
|
+
def self.committed_records
|
|
3
|
+
@@committed_records ||= []
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
def self.committed_records=(committed_records)
|
|
7
|
+
@@committed_records = committed_records
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def self.committed_records_on_create
|
|
11
|
+
@@committed_records_on_create ||= []
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.committed_records_on_create=(committed_records)
|
|
15
|
+
@@committed_records_on_create = committed_records
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.committed_records_on_update
|
|
19
|
+
@@committed_records_on_update ||= []
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.committed_records_on_update=(committed_records)
|
|
23
|
+
@@committed_records_on_update = committed_records
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.committed_records_on_destroy
|
|
27
|
+
@@committed_records_on_destroy ||= []
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.committed_records_on_destroy=(committed_records)
|
|
31
|
+
@@committed_records_on_destroy = committed_records
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
module AfterCommit
|
|
2
|
+
module ConnectionAdapters
|
|
3
|
+
def self.included(base)
|
|
4
|
+
base.class_eval do
|
|
5
|
+
# The commit_db_transaction method gets called when the outermost
|
|
6
|
+
# transaction finishes and everything inside commits. We want to
|
|
7
|
+
# override it so that after this happens, any records that were saved
|
|
8
|
+
# or destroyed within this transaction now get their after_commit
|
|
9
|
+
# callback fired.
|
|
10
|
+
def commit_db_transaction_with_callback
|
|
11
|
+
commit_db_transaction_without_callback
|
|
12
|
+
trigger_after_commit_callbacks
|
|
13
|
+
trigger_after_commit_on_create_callbacks
|
|
14
|
+
trigger_after_commit_on_update_callbacks
|
|
15
|
+
trigger_after_commit_on_destroy_callbacks
|
|
16
|
+
end
|
|
17
|
+
alias_method_chain :commit_db_transaction, :callback
|
|
18
|
+
|
|
19
|
+
# In the event the transaction fails and rolls back, nothing inside
|
|
20
|
+
# should recieve the after_commit callback.
|
|
21
|
+
def rollback_db_transaction_with_callback
|
|
22
|
+
rollback_db_transaction_without_callback
|
|
23
|
+
|
|
24
|
+
AfterCommit.committed_records = []
|
|
25
|
+
AfterCommit.committed_records_on_create = []
|
|
26
|
+
AfterCommit.committed_records_on_update = []
|
|
27
|
+
AfterCommit.committed_records_on_destroy = []
|
|
28
|
+
end
|
|
29
|
+
alias_method_chain :rollback_db_transaction, :callback
|
|
30
|
+
|
|
31
|
+
protected
|
|
32
|
+
def trigger_after_commit_callbacks
|
|
33
|
+
# Trigger the after_commit callback for each of the committed
|
|
34
|
+
# records.
|
|
35
|
+
if AfterCommit.committed_records.any?
|
|
36
|
+
AfterCommit.committed_records.each do |record|
|
|
37
|
+
begin
|
|
38
|
+
record.after_commit_callback
|
|
39
|
+
rescue
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Make sure we clear out our list of committed records now that we've
|
|
45
|
+
# triggered the callbacks for each one.
|
|
46
|
+
AfterCommit.committed_records = []
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def trigger_after_commit_on_create_callbacks
|
|
50
|
+
# Trigger the after_commit_on_create callback for each of the committed
|
|
51
|
+
# records.
|
|
52
|
+
if AfterCommit.committed_records_on_create.any?
|
|
53
|
+
AfterCommit.committed_records_on_create.each do |record|
|
|
54
|
+
begin
|
|
55
|
+
record.after_commit_on_create_callback
|
|
56
|
+
rescue
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Make sure we clear out our list of committed records now that we've
|
|
62
|
+
# triggered the callbacks for each one.
|
|
63
|
+
AfterCommit.committed_records_on_create = []
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def trigger_after_commit_on_update_callbacks
|
|
67
|
+
# Trigger the after_commit_on_update callback for each of the committed
|
|
68
|
+
# records.
|
|
69
|
+
if AfterCommit.committed_records_on_update.any?
|
|
70
|
+
AfterCommit.committed_records_on_update.each do |record|
|
|
71
|
+
begin
|
|
72
|
+
record.after_commit_on_update_callback
|
|
73
|
+
rescue
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Make sure we clear out our list of committed records now that we've
|
|
79
|
+
# triggered the callbacks for each one.
|
|
80
|
+
AfterCommit.committed_records_on_update = []
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def trigger_after_commit_on_destroy_callbacks
|
|
84
|
+
# Trigger the after_commit_on_destroy callback for each of the committed
|
|
85
|
+
# records.
|
|
86
|
+
if AfterCommit.committed_records_on_destroy.any?
|
|
87
|
+
AfterCommit.committed_records_on_destroy.each do |record|
|
|
88
|
+
begin
|
|
89
|
+
record.after_commit_on_destroy_callback
|
|
90
|
+
rescue
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Make sure we clear out our list of committed records now that we've
|
|
96
|
+
# triggered the callbacks for each one.
|
|
97
|
+
AfterCommit.committed_records_on_destroy = []
|
|
98
|
+
end
|
|
99
|
+
#end protected
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: integralimpressions-after_commit
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Eli Miller
|
|
8
|
+
- Joost Hietbrink
|
|
9
|
+
- DeLynn Barry
|
|
10
|
+
- Kane Baccigalupi
|
|
11
|
+
autorequire: after_commit
|
|
12
|
+
bindir: bin
|
|
13
|
+
cert_chain: []
|
|
14
|
+
|
|
15
|
+
date: 2008-10-23 00:00:00 -07:00
|
|
16
|
+
default_executable:
|
|
17
|
+
dependencies:
|
|
18
|
+
- !ruby/object:Gem::Dependency
|
|
19
|
+
name: activerecord
|
|
20
|
+
version_requirement:
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: "1.0"
|
|
26
|
+
version:
|
|
27
|
+
description: after_commit is an ActiveRecord gem to add an after_commit callback. This can be used to trigger things only after the entire transaction is complete.
|
|
28
|
+
email: kane@integralimpressions.com
|
|
29
|
+
executables: []
|
|
30
|
+
|
|
31
|
+
extensions: []
|
|
32
|
+
|
|
33
|
+
extra_rdoc_files:
|
|
34
|
+
- LICENSE
|
|
35
|
+
- README
|
|
36
|
+
files:
|
|
37
|
+
- LICENSE
|
|
38
|
+
- README
|
|
39
|
+
- Rakefile
|
|
40
|
+
- lib/after_commit
|
|
41
|
+
- lib/after_commit/active_record.rb
|
|
42
|
+
- lib/after_commit/after_commit.rb
|
|
43
|
+
- lib/after_commit/connection_adapters.rb
|
|
44
|
+
- lib/after_commit.rb
|
|
45
|
+
has_rdoc: true
|
|
46
|
+
homepage: http://github.com/integralimpressions/after_commit
|
|
47
|
+
post_install_message:
|
|
48
|
+
rdoc_options: []
|
|
49
|
+
|
|
50
|
+
require_paths:
|
|
51
|
+
- lib
|
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
|
+
requirements:
|
|
54
|
+
- - ">="
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
version: "0"
|
|
57
|
+
version:
|
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
|
+
requirements:
|
|
60
|
+
- - ">="
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: "0"
|
|
63
|
+
version:
|
|
64
|
+
requirements: []
|
|
65
|
+
|
|
66
|
+
rubyforge_project:
|
|
67
|
+
rubygems_version: 1.2.0
|
|
68
|
+
signing_key:
|
|
69
|
+
specification_version: 2
|
|
70
|
+
summary: after_commit is an ActiveRecord gem to add an after_commit callback. This can be used to trigger things only after the entire transaction is complete.
|
|
71
|
+
test_files: []
|
|
72
|
+
|