Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 6bf114068b
Fetching contributors…

Cannot retrieve contributors at this time

59 lines (48 sloc) 1.29 kB
require 'active_record'
require 'ar_after_transaction/version'
module ARAfterTransaction
module ClassMethods
@@after_transaction_callbacks = []
def transaction(*args, &block)
clean = true
super
rescue Exception
clean = false
raise
ensure
unless transactions_open?
callbacks = delete_after_transaction_callbacks
callbacks.each(&:call) if clean
end
end
def after_transaction(&block)
if transactions_open?
@@after_transaction_callbacks << block
else
yield
end
end
def normally_open_transactions
@@normally_open_transactions ||= 0
end
def normally_open_transactions=(value)
@@normally_open_transactions = value
end
private
def transactions_open?
connection.open_transactions > normally_open_transactions
end
def delete_after_transaction_callbacks
result = @@after_transaction_callbacks
@@after_transaction_callbacks = []
result
end
end
module InstanceMethods
def after_transaction(&block)
self.class.after_transaction(&block)
end
end
end
ActiveRecord::Base.send(:extend, ARAfterTransaction::ClassMethods)
ActiveRecord::Base.send(:include, ARAfterTransaction::InstanceMethods)
Jump to Line
Something went wrong with that request. Please try again.