outoftime-after_create 0.1.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 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.
@@ -0,0 +1,36 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the after_commit plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the after_commit plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'AfterCommit'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+
24
+ begin
25
+ gem 'technicalpickles-jeweler', '~> 1.0.1'
26
+ require 'jeweler'
27
+ Jeweler::Tasks.new do |s|
28
+ s.name = 'after_create'
29
+ s.summary = 'A Ruby on Rails plugin to add an after_commit callback'
30
+ s.homepage = 'http://github.com/GUI/after_commit'
31
+ s.description = 'A Ruby on Rails plugin to add an after_commit callback'
32
+ s.authors = ['Eli Miller', 'Joost Heitbrink', 'DeLynn Berry', 'Xavier Shay', 'Mat Brown']
33
+ s.files = FileList['[A-Z]*', '{lib,test,rails}/**/*']
34
+ s.has_rdoc = false
35
+ end
36
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,42 @@
1
+ module AfterCommit
2
+ # COMMITTED_RECORDS[connection_object_id][event]
3
+ COMMITTED_RECORDS = Hash.new do |h, k|
4
+ h[k] = Hash.new do |h2, k2|
5
+ h2[k2] = []
6
+ end
7
+ end
8
+
9
+ class <<self
10
+ def record_created(connection, record)
11
+ COMMITTED_RECORDS[connection.object_id][:created] << record
12
+ end
13
+
14
+ def record_updated(connection, record)
15
+ COMMITTED_RECORDS[connection.object_id][:updated] << record
16
+ end
17
+
18
+ def record_destroyed(connection, record)
19
+ COMMITTED_RECORDS[connection.object_id][:destroyed] << record
20
+ end
21
+
22
+ def created_records(connection)
23
+ COMMITTED_RECORDS[connection.object_id][:created]
24
+ end
25
+
26
+ def updated_records(connection)
27
+ COMMITTED_RECORDS[connection.object_id][:updated]
28
+ end
29
+
30
+ def destroyed_records(connection)
31
+ COMMITTED_RECORDS[connection.object_id][:destroyed]
32
+ end
33
+
34
+ def touched_records(connection)
35
+ COMMITTED_RECORDS[connection.object_id].values.flatten
36
+ end
37
+
38
+ def cleanup(connection)
39
+ COMMITTED_RECORDS.delete(connection.object_id)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,79 @@
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_create :add_committed_record_on_create
43
+ after_update :add_committed_record_on_update
44
+ after_destroy :add_committed_record_on_destroy
45
+
46
+ def add_committed_record_on_create
47
+ AfterCommit.record_created(self.class.connection, self)
48
+ end
49
+
50
+ def add_committed_record_on_update
51
+ AfterCommit.record_updated(self.class.connection, self)
52
+ end
53
+
54
+ def add_committed_record_on_destroy
55
+ AfterCommit.record_destroyed(self.class.connection, self)
56
+ end
57
+
58
+ # Wraps a call to the private callback method so that the the
59
+ # after_commit callback can be made from the ConnectionAdapters when
60
+ # the commit for the transaction has finally succeeded.
61
+ def after_commit_callback
62
+ callback(:after_commit)
63
+ end
64
+
65
+ def after_commit_on_create_callback
66
+ callback(:after_commit_on_create)
67
+ end
68
+
69
+ def after_commit_on_update_callback
70
+ callback(:after_commit_on_update)
71
+ end
72
+
73
+ def after_commit_on_destroy_callback
74
+ callback(:after_commit_on_destroy)
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,70 @@
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
+ begin
12
+ commit_db_transaction_without_callback
13
+ trigger_after_commit_callbacks
14
+ trigger_after_commit_on_create_callbacks
15
+ trigger_after_commit_on_update_callbacks
16
+ trigger_after_commit_on_destroy_callbacks
17
+ ensure
18
+ AfterCommit.cleanup(self)
19
+ end
20
+ end
21
+ alias_method_chain :commit_db_transaction, :callback
22
+
23
+ # In the event the transaction fails and rolls back, nothing inside
24
+ # should recieve the after_commit callback.
25
+ def rollback_db_transaction_with_callback
26
+ begin
27
+ rollback_db_transaction_without_callback
28
+ ensure
29
+ AfterCommit.cleanup(self)
30
+ end
31
+ end
32
+ alias_method_chain :rollback_db_transaction, :callback
33
+
34
+ protected
35
+
36
+ def trigger_after_commit_callbacks
37
+ # Trigger the after_commit callback for each of the committed
38
+ # records.
39
+ AfterCommit.touched_records(self).each do |record|
40
+ record.after_commit_callback
41
+ end
42
+ end
43
+
44
+ def trigger_after_commit_on_create_callbacks
45
+ # Trigger the after_commit_on_create callback for each of the committed
46
+ # records.
47
+ AfterCommit.created_records(self).each do |record|
48
+ record.after_commit_on_create_callback
49
+ end
50
+ end
51
+
52
+ def trigger_after_commit_on_update_callbacks
53
+ # Trigger the after_commit_on_update callback for each of the committed
54
+ # records.
55
+ AfterCommit.updated_records(self).each do |record|
56
+ record.after_commit_on_update_callback
57
+ end
58
+ end
59
+
60
+ def trigger_after_commit_on_destroy_callbacks
61
+ # Trigger the after_commit_on_destroy callback for each of the committed
62
+ # records.
63
+ AfterCommit.destroyed_records(self).each do |record|
64
+ record.after_commit_on_destroy_callback
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -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,53 @@
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
+ ActiveRecord::Base.establish_connection({"adapter" => "sqlite3", "database" => 'test.sqlite3'})
10
+ begin
11
+ ActiveRecord::Base.connection.execute("drop table mock_records");
12
+ rescue
13
+ end
14
+ ActiveRecord::Base.connection.execute("create table mock_records(id int)");
15
+
16
+ require File.dirname(__FILE__) + '/../rails/init.rb'
17
+
18
+ class MockRecord < ActiveRecord::Base
19
+ attr_accessor :after_commit_on_create_called
20
+ attr_accessor :after_commit_on_update_called
21
+ attr_accessor :after_commit_on_destroy_called
22
+
23
+ after_commit_on_create :do_create
24
+ def do_create
25
+ self.after_commit_on_create_called = true
26
+ end
27
+
28
+ after_commit_on_update :do_update
29
+ def do_update
30
+ self.after_commit_on_update_called = true
31
+ end
32
+
33
+ after_commit_on_create :do_destroy
34
+ def do_destroy
35
+ self.after_commit_on_destroy_called = true
36
+ end
37
+ end
38
+
39
+ class AfterCommitTest < Test::Unit::TestCase
40
+ def test_after_commit_on_create_is_called
41
+ assert_equal true, MockRecord.create!.after_commit_on_create_called
42
+ end
43
+
44
+ def test_after_commit_on_update_is_called
45
+ record = MockRecord.create!
46
+ record.save
47
+ assert_equal true, record.after_commit_on_update_called
48
+ end
49
+
50
+ def test_after_commit_on_destroy_is_called
51
+ assert_equal true, MockRecord.create!.destroy.after_commit_on_destroy_called
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: outoftime-after_create
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Eli Miller
8
+ - Joost Heitbrink
9
+ - DeLynn Berry
10
+ - Xavier Shay
11
+ - Mat Brown
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2009-06-22 00:00:00 -07:00
17
+ default_executable:
18
+ dependencies: []
19
+
20
+ description: A Ruby on Rails plugin to add an after_commit callback
21
+ email:
22
+ executables: []
23
+
24
+ extensions: []
25
+
26
+ extra_rdoc_files:
27
+ - LICENSE
28
+ - README
29
+ files:
30
+ - LICENSE
31
+ - README
32
+ - Rakefile
33
+ - VERSION
34
+ - lib/after_commit.rb
35
+ - lib/after_commit/active_record.rb
36
+ - lib/after_commit/connection_adapters.rb
37
+ - rails/init.rb
38
+ - test/after_commit_test.rb
39
+ has_rdoc: false
40
+ homepage: http://github.com/GUI/after_commit
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.2.0
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: A Ruby on Rails plugin to add an after_commit callback
65
+ test_files:
66
+ - test/after_commit_test.rb