yipdw-after_commit 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ test.sqlite3
2
+ pkg
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,16 @@
1
+ after_commit
2
+ ===========
3
+
4
+ A Ruby on Rails plugin to add after_commit callbacks. The callbacks that are provided can be used
5
+ to trigger events that run only after the entire transaction is complete. This is beneficial
6
+ in situations where you are doing asynchronous processing and need committed objects.
7
+
8
+ The following callbacks are provided:
9
+
10
+ * (1) after_commit
11
+ * (2) after_commit_on_create
12
+ * (3) after_commit_on_update
13
+ * (4) after_commit_on_destroy
14
+
15
+ The after_commit callback is run for any object that has just been committed. You can obtain finer
16
+ callback control by using the additional <tt>after_commit_on_*</tt> callbacks.
data/Rakefile ADDED
@@ -0,0 +1,41 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "after_commit"
9
+ gem.summary = %Q{after_commit callback for ActiveRecord}
10
+ gem.description = %Q{
11
+ A Ruby on Rails plugin to add an after_commit callback. This can be used to trigger things only after the entire transaction is complete.
12
+ }
13
+ gem.email = "yipdw@northwestern.edu"
14
+ gem.homepage = "http://github.com/yipdw/after_commit"
15
+ gem.authors = ["Nick Muerdter", "David Yip"]
16
+ gem.add_development_dependency "thoughtbot-shoulda"
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+
24
+ desc 'Default: run unit tests.'
25
+ task :default => :test
26
+
27
+ desc 'Test the after_commit plugin.'
28
+ Rake::TestTask.new(:test) do |t|
29
+ t.libs << 'lib'
30
+ t.pattern = 'test/**/*_test.rb'
31
+ t.verbose = true
32
+ end
33
+
34
+ desc 'Generate documentation for the after_commit plugin.'
35
+ Rake::RDocTask.new(:rdoc) do |rdoc|
36
+ rdoc.rdoc_dir = 'rdoc'
37
+ rdoc.title = 'AfterCommit'
38
+ rdoc.options << '--line-numbers' << '--inline-source'
39
+ rdoc.rdoc_files.include('README')
40
+ rdoc.rdoc_files.include('lib/**/*.rb')
41
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
data/init.rb ADDED
@@ -0,0 +1,5 @@
1
+ ActiveRecord::Base.send(:include, AfterCommit::ActiveRecord)
2
+
3
+ Object.subclasses_of(ActiveRecord::ConnectionAdapters::AbstractAdapter).each do |klass|
4
+ klass.send(:include, AfterCommit::ConnectionAdapters)
5
+ end
@@ -0,0 +1,87 @@
1
+ module AfterCommit
2
+ module ActiveRecord
3
+ # Based on the code found in Thinking Sphinx:
4
+ # http://ts.freelancing-gods.com/ which was based on code written by Eli
5
+ # Miller:
6
+ # http://elimiller.blogspot.com/2007/06/proper-cache-expiry-with-aftercommit.html
7
+ # with slight modification from Joost Hietbrink. And now me! Whew.
8
+ def self.included(base)
9
+ base.class_eval do
10
+ # The define_callbacks method was added post Rails 2.0.2 - if it
11
+ # doesn't exist, we define the callback manually
12
+ if respond_to?(:define_callbacks)
13
+ define_callbacks :after_commit,
14
+ :after_commit_on_create,
15
+ :after_commit_on_update,
16
+ :after_commit_on_destroy
17
+ else
18
+ class << self
19
+ # Handle after_commit callbacks - call all the registered callbacks.
20
+ def after_commit(*callbacks, &block)
21
+ callbacks << block if block_given?
22
+ write_inheritable_array(:after_commit, callbacks)
23
+ end
24
+
25
+ def after_commit_on_create(*callbacks, &block)
26
+ callbacks << block if block_given?
27
+ write_inheritable_array(:after_commit_on_create, callbacks)
28
+ end
29
+
30
+ def after_commit_on_update(*callbacks, &block)
31
+ callbacks << block if block_given?
32
+ write_inheritable_array(:after_commit_on_update, callbacks)
33
+ end
34
+
35
+ def after_commit_on_destroy(*callbacks, &block)
36
+ callbacks << block if block_given?
37
+ write_inheritable_array(:after_commit_on_destroy, callbacks)
38
+ end
39
+ end
40
+ end
41
+
42
+ after_save :add_committed_record
43
+ after_create :add_committed_record_on_create
44
+ after_update :add_committed_record_on_update
45
+ after_destroy :add_committed_record_on_destroy
46
+
47
+ # We need to keep track of records that have been saved or destroyed
48
+ # within this transaction.
49
+ def add_committed_record
50
+ AfterCommit.committed_records << self
51
+ end
52
+
53
+ def add_committed_record_on_create
54
+ AfterCommit.committed_records_on_create << self
55
+ end
56
+
57
+ def add_committed_record_on_update
58
+ AfterCommit.committed_records_on_update << self
59
+ end
60
+
61
+ def add_committed_record_on_destroy
62
+ AfterCommit.committed_records << self
63
+ AfterCommit.committed_records_on_destroy << self
64
+ end
65
+
66
+ # Wraps a call to the private callback method so that the the
67
+ # after_commit callback can be made from the ConnectionAdapters when
68
+ # the commit for the transaction has finally succeeded.
69
+ def after_commit_callback
70
+ callback(:after_commit)
71
+ end
72
+
73
+ def after_commit_on_create_callback
74
+ callback(:after_commit_on_create)
75
+ end
76
+
77
+ def after_commit_on_update_callback
78
+ callback(:after_commit_on_update)
79
+ end
80
+
81
+ def after_commit_on_destroy_callback
82
+ callback(:after_commit_on_destroy)
83
+ end
84
+ end
85
+ end
86
+ end
87
+ 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
@@ -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,67 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class MockRecord < ActiveRecord::Base
4
+ attr_accessor :after_commit_on_create_called
5
+ attr_accessor :after_commit_on_update_called
6
+ attr_accessor :after_commit_on_destroy_called
7
+
8
+ after_commit_on_create :do_create
9
+ def do_create
10
+ self.after_commit_on_create_called = true
11
+ end
12
+
13
+ after_commit_on_update :do_update
14
+ def do_update
15
+ self.after_commit_on_update_called = true
16
+ end
17
+
18
+ after_commit_on_create :do_destroy
19
+ def do_destroy
20
+ self.after_commit_on_destroy_called = true
21
+ end
22
+ end
23
+
24
+ class UnsavableRecord < ActiveRecord::Base
25
+ attr_accessor :after_commit_called
26
+
27
+ set_table_name 'mock_records'
28
+
29
+ protected
30
+
31
+ def after_initialize
32
+ self.after_commit_called = false
33
+ end
34
+
35
+ def after_save
36
+ raise
37
+ end
38
+
39
+ after_commit :after_commit
40
+
41
+ def after_commit
42
+ self.after_commit_called = true
43
+ end
44
+ end
45
+
46
+ class AfterCommitTest < Test::Unit::TestCase
47
+ def test_after_commit_on_create_is_called
48
+ assert_equal true, MockRecord.create!.after_commit_on_create_called
49
+ end
50
+
51
+ def test_after_commit_on_update_is_called
52
+ record = MockRecord.create!
53
+ record.save
54
+ assert_equal true, record.after_commit_on_update_called
55
+ end
56
+
57
+ def test_after_commit_on_destroy_is_called
58
+ assert_equal true, MockRecord.create!.destroy.after_commit_on_destroy_called
59
+ end
60
+
61
+ def test_after_commit_does_not_trigger_when_transaction_rolls_back
62
+ record = UnsavableRecord.new
63
+ begin; record.save; rescue; end
64
+
65
+ assert_equal false, record.after_commit_called
66
+ end
67
+ end
@@ -0,0 +1,60 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class ObservableMockRecord < ActiveRecord::Base
4
+ set_table_name 'mock_records'
5
+
6
+ attr_accessor :after_commit_called
7
+ attr_accessor :after_commit_on_create_called
8
+ attr_accessor :after_commit_on_update_called
9
+ attr_accessor :after_commit_on_destroy_called
10
+ end
11
+
12
+ class ObservableMockRecordObserver < ActiveRecord::Observer
13
+ def after_commit(model)
14
+ model.after_commit_called = true
15
+ end
16
+
17
+ def after_commit_on_create(model)
18
+ model.after_commit_on_create_called = true
19
+ end
20
+
21
+ def after_commit_on_update(model)
22
+ model.after_commit_on_update_called = true
23
+ end
24
+
25
+ def after_commit_on_destroy(model)
26
+ model.after_commit_on_destroy_called = true
27
+ end
28
+ end
29
+
30
+ class ObserverTest < Test::Unit::TestCase
31
+ def setup
32
+ ObservableMockRecordObserver.instance
33
+ end
34
+
35
+ def test_after_commit_is_called
36
+ record = ObservableMockRecord.new
37
+ record.save
38
+
39
+ assert record.after_commit_called
40
+ end
41
+
42
+ def test_after_commit_on_create_is_called
43
+ record = ObservableMockRecord.create
44
+
45
+ assert record.after_commit_on_create_called
46
+ end
47
+
48
+ def test_after_commit_on_update_is_called
49
+ record = ObservableMockRecord.create
50
+ record.save
51
+
52
+ assert record.after_commit_on_update_called
53
+ end
54
+
55
+ def test_after_commit_on_destroy_is_called
56
+ record = ObservableMockRecord.create.destroy
57
+
58
+ assert record.after_commit_on_destroy_called
59
+ end
60
+ end
@@ -0,0 +1,23 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
2
+ require 'test/unit'
3
+ require 'rubygems'
4
+ require 'activerecord'
5
+ require 'after_commit'
6
+ require 'after_commit/active_record'
7
+ require 'after_commit/connection_adapters'
8
+
9
+ begin
10
+ require 'sqlite3'
11
+ rescue LoadError
12
+ gem 'sqlite3-ruby'
13
+ retry
14
+ end
15
+
16
+ ActiveRecord::Base.establish_connection({"adapter" => "sqlite3", "database" => 'test.sqlite3'})
17
+ begin
18
+ ActiveRecord::Base.connection.execute("drop table mock_records");
19
+ rescue
20
+ end
21
+ ActiveRecord::Base.connection.execute("create table mock_records(id int)");
22
+
23
+ require File.dirname(__FILE__) + '/../init.rb'
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yipdw-after_commit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nick Muerdter
8
+ - David Yip
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-09-01 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: thoughtbot-shoulda
18
+ type: :development
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ version:
26
+ description: A Ruby on Rails plugin to add an after_commit callback. This can be used to trigger things only after the entire transaction is complete.
27
+ email: yipdw@northwestern.edu
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - LICENSE
34
+ - README
35
+ files:
36
+ - .gitignore
37
+ - LICENSE
38
+ - README
39
+ - Rakefile
40
+ - VERSION
41
+ - init.rb
42
+ - lib/after_commit.rb
43
+ - lib/after_commit/active_record.rb
44
+ - lib/after_commit/connection_adapters.rb
45
+ - test/after_commit_test.rb
46
+ - test/observer_test.rb
47
+ - test/test_helper.rb
48
+ has_rdoc: true
49
+ homepage: http://github.com/yipdw/after_commit
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --charset=UTF-8
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.2.0
71
+ signing_key:
72
+ specification_version: 2
73
+ summary: after_commit callback for ActiveRecord
74
+ test_files:
75
+ - test/after_commit_test.rb
76
+ - test/observer_test.rb
77
+ - test/test_helper.rb