Permalink
Browse files

Add before_commit_on_save and after_commit_on_save.

These callbacks are the after_commit equivalent of ActiveRecord's after_save callback. i.e. they are called both on create and save but not destroy.
  • Loading branch information...
1 parent 30791e8 commit 3face9718726b9ed0267b366459a7b1a14aec683 @jasoncodes jasoncodes committed May 16, 2010
View
@@ -14,6 +14,11 @@ def self.record_updated(connection, record)
add_to_collection :committed_records_on_update, connection, record
end
+ def self.record_saved(connection, record)
+ prepare_collection :committed_records_on_save, connection
+ add_to_collection :committed_records_on_save, connection, record
+ end
+
def self.record_destroyed(connection, record)
prepare_collection :committed_records_on_destroy, connection
add_to_collection :committed_records_on_destroy, connection, record
@@ -31,6 +36,10 @@ def self.updated_records(connection)
collection :committed_records_on_update, connection
end
+ def self.saved_records(connection)
+ collection :committed_records_on_save, connection
+ end
+
def self.destroyed_records(connection)
collection :committed_records_on_destroy, connection
end
@@ -40,6 +49,7 @@ def self.cleanup(connection)
:committed_records,
:committed_records_on_create,
:committed_records_on_update,
+ :committed_records_on_save,
:committed_records_on_destroy
].each do |collection|
Thread.current[collection] ||= {}
@@ -34,16 +34,19 @@ def include_after_commit_extension(adapter)
define_callbacks :after_commit,
:after_commit_on_create,
:after_commit_on_update,
+ :after_commit_on_save,
:after_commit_on_destroy,
:after_rollback,
:before_commit,
:before_commit_on_create,
:before_commit_on_update,
+ :before_commit_on_save,
:before_commit_on_destroy,
:before_rollback
after_create :add_committed_record_on_create
after_update :add_committed_record_on_update
+ after_save :add_committed_record_on_save
after_destroy :add_committed_record_on_destroy
def add_committed_record
@@ -66,6 +69,12 @@ def add_committed_record_on_update
end
end
+ def add_committed_record_on_save
+ if have_callback? :before_commit_on_save, :after_commit_on_save
+ AfterCommit.record_saved(self.class.connection, self)
+ end
+ end
+
def add_committed_record_on_destroy
add_committed_record
if have_callback? :before_commit_on_destroy, :after_commit_on_destroy
@@ -27,6 +27,7 @@ def commit_db_transaction_with_callback
trigger_before_commit_callbacks
trigger_before_commit_on_create_callbacks
trigger_before_commit_on_update_callbacks
+ trigger_before_commit_on_save_callbacks
trigger_before_commit_on_destroy_callbacks
result = commit_db_transaction_without_callback
@@ -35,6 +36,7 @@ def commit_db_transaction_with_callback
trigger_after_commit_callbacks
trigger_after_commit_on_create_callbacks
trigger_after_commit_on_update_callbacks
+ trigger_after_commit_on_save_callbacks
trigger_after_commit_on_destroy_callbacks
result
ensure
@@ -91,6 +93,12 @@ def trigger_before_commit_on_update_callbacks
end
end
+ def trigger_before_commit_on_save_callbacks
+ AfterCommit.saved_records(self).each do |record|
+ record.send :callback, :before_commit_on_save
+ end
+ end
+
def trigger_before_commit_on_destroy_callbacks
AfterCommit.destroyed_records(self).each do |record|
record.send :callback, :before_commit_on_destroy
@@ -127,6 +135,14 @@ def trigger_after_commit_on_update_callbacks
end
end
+ def trigger_after_commit_on_save_callbacks
+ # Trigger the after_commit_on_save callback for each of the committed
+ # records.
+ AfterCommit.saved_records(self).each do |record|
+ record.send :callback, :after_commit_on_save
+ end
+ end
+
def trigger_after_commit_on_destroy_callbacks
# Trigger the after_commit_on_destroy callback for each of the committed
# records.
@@ -7,8 +7,8 @@
module AfterCommit::TestBypass
def self.included(klass)
klass.class_eval do
- [:add_committed_record_on_create, :add_committed_record_on_update, :add_committed_record_on_destroy].each do |method|
- remove_method(method)
+ %w(create update save destroy).each do |action|
+ remove_method "add_committed_record_on_#{action}"
end
end
end
@@ -23,6 +23,10 @@ def add_committed_record_on_update
callback :after_commit_on_update
end
+ def add_committed_record_on_save
+ callback :after_commit_on_save
+ end
+
def add_committed_record_on_destroy
callback :after_commit
callback :after_commit_on_destroy
View
@@ -3,7 +3,7 @@
class MockRecord < ActiveRecord::Base
PHASES = %w(before after)
- ACTIONS = %w(create update destroy)
+ ACTIONS = %w(create update save destroy)
PHASES.each do |phase|
ACTIONS.each do |action|
@@ -92,6 +92,7 @@ def update_counts
AfterCommit.records(connection) +
AfterCommit.created_records(connection) +
AfterCommit.updated_records(connection) +
+ AfterCommit.saved_records(connection) +
AfterCommit.destroyed_records(connection)
all_records.uniq!
self.total_count = all_records.size
@@ -114,6 +115,17 @@ def test_before_commit_on_update_is_not_called_for_create
assert_nil MockRecord.create!.before_commit_on_update_called
end
+ def test_before_commit_on_save_is_called_for_create
+ assert_equal true, MockRecord.create!.before_commit_on_save_called
+ end
+
+ def test_before_commit_on_save_is_called_for_update
+ record = MockRecord.create!
+ record.before_commit_on_save_called = false
+ record.save
+ assert_equal true, record.before_commit_on_save_called
+ end
+
def test_before_commit_on_destroy_is_called
assert_equal true, MockRecord.create!.destroy.before_commit_on_destroy_called
end
@@ -133,6 +145,17 @@ def test_after_commit_on_update_is_not_called_for_create
assert_nil MockRecord.create!.after_commit_on_update_called
end
+ def test_after_commit_on_save_is_called_for_create
+ assert_equal true, MockRecord.create!.after_commit_on_save_called
+ end
+
+ def test_after_commit_on_save_is_called_for_update
+ record = MockRecord.create!
+ record.after_commit_on_save_called = false
+ record.save
+ assert_equal true, record.after_commit_on_save_called
+ end
+
def test_after_commit_on_destroy_is_called
assert_equal true, MockRecord.create!.destroy.after_commit_on_destroy_called
end

0 comments on commit 3face97

Please sign in to comment.