Skip to content
Browse files

Add before_rollback

  • Loading branch information...
1 parent acc0211 commit ac9474ad9100e362bc0b02d207295d66c4672af4 @trotter committed Apr 23, 2009
Showing with 19 additions and 1 deletion.
  1. +7 −1 lib/after_commit/active_record.rb
  2. +12 −0 lib/after_commit/connection_adapters.rb
View
8 lib/after_commit/active_record.rb
@@ -17,7 +17,8 @@ def self.included(base)
:before_commit,
:before_commit_on_create,
:before_commit_on_update,
- :before_commit_on_destroy
+ :before_commit_on_destroy,
+ :before_rollback
else
class << self
# Handle after_commit callbacks - call all the registered callbacks.
@@ -65,6 +66,11 @@ def before_commit_on_destroy(*callbacks, &block)
callbacks << block if block_given?
write_inheritable_array(:before_commit_on_destroy, callbacks)
end
+
+ def before_rollback(*callbacks, &block)
+ callbacks << block if block_given?
+ write_inheritable_array(:before_commit, callbacks)
+ end
end
end
View
12 lib/after_commit/connection_adapters.rb
@@ -24,6 +24,7 @@ def commit_db_transaction_with_callback
# should recieve the after_commit callback, but do fire the after_rollback
# callback for each record that failed to be committed.
def rollback_db_transaction_with_callback
+ trigger_before_rollback_callbacks
rollback_db_transaction_without_callback
trigger_after_rollback_callbacks
@@ -66,6 +67,17 @@ def trigger_before_commit_on_destroy_callbacks
end
end
+ def trigger_before_rollback_callbacks
+ if AfterCommit.committed_records.any?
+ AfterCommit.committed_records.each do |record|
+ begin
+ record.send(:callback, :before_rollback)
+ rescue
+ end
+ end
+ end
+ end
+
def trigger_after_commit_callbacks
# Trigger the after_commit callback for each of the committed
# records.

0 comments on commit ac9474a

Please sign in to comment.
Something went wrong with that request. Please try again.