Skip to content

Commit

Permalink
Merge remote-tracking branch 'aurorafeint/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
pat committed Aug 10, 2013
2 parents 6db5198 + 2c7e3e6 commit e64355e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 66 deletions.
24 changes: 10 additions & 14 deletions lib/after_commit/active_record.rb
Expand Up @@ -9,28 +9,24 @@ def establish_connection_with_after_commit(spec = nil)
result
end
alias_method_chain :establish_connection, :after_commit

def include_after_commit_extensions
base = ::ActiveRecord::ConnectionAdapters::AbstractAdapter
Object.subclasses_of(base).each do |klass|
include_after_commit_extension klass
end

if defined?(JRUBY_VERSION) and defined?(JdbcSpec::MySQL)
include_after_commit_extension JdbcSpec::MySQL
end
end

private

def include_after_commit_extension(adapter)
additions = AfterCommit::ConnectionAdapters
unless adapter.included_modules.include?(additions)
adapter.send :include, additions
end
end
end

define_callbacks :after_commit,
:after_commit_on_create,
:after_commit_on_update,
Expand All @@ -43,38 +39,38 @@ def include_after_commit_extension(adapter)
: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
if have_callback? :before_commit, :after_commit, :before_rollback, :after_rollback
AfterCommit.record(self.class.connection, self)
end
end

def add_committed_record_on_create
add_committed_record
if have_callback? :before_commit_on_create, :after_commit_on_create
AfterCommit.record_created(self.class.connection, self)
end
end

def add_committed_record_on_update
add_committed_record
if have_callback? :before_commit_on_update, :after_commit_on_update
AfterCommit.record_updated(self.class.connection, self)
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
Expand Down
87 changes: 35 additions & 52 deletions lib/after_commit/connection_adapters.rb
Expand Up @@ -2,35 +2,18 @@ module AfterCommit
module ConnectionAdapters
def self.included(base)
base.class_eval do

if method_defined?(:transaction)
def transaction_with_callback(*args, &block)
# @disable_rollback is set to false at the start of the
# outermost call to #transaction. After committing, it is
# set to true to prevent exceptions causing a spurious
# rollback.
outermost_call = @disable_rollback.nil?
@disable_rollback = false if outermost_call
transaction_without_callback(*args, &block)
ensure
@disable_rollback = nil if outermost_call
end
alias_method_chain :transaction, :callback

elsif method_defined?(:begin_db_transaction)
def begin_db_transaction_with_callback(*args, &block)
# @disable_rollback is set to false at the start of the
# outermost call to #transaction. After committing, it is
# set to true to prevent exceptions causing a spurious
# rollback.
outermost_call = @disable_rollback.nil?
@disable_rollback = false if outermost_call
begin_db_transaction_without_callback(*args, &block)
ensure
@disable_rollback = nil if outermost_call
end
alias_method_chain :begin_db_transaction, :callback
end
def transaction_with_callback(*args, &block)
# @disable_rollback is set to false at the start of the
# outermost call to #transaction. After committing, it is
# set to true to prevent exceptions causing a spurious
# rollback.
outermost_call = @disable_rollback.nil?
@disable_rollback = false if outermost_call
transaction_without_callback(*args, &block)
ensure
@disable_rollback = nil if outermost_call
end
alias_method_chain :transaction, :callback

# The commit_db_transaction method gets called when the outermost
# transaction finishes and everything inside commits. We want to
Expand Down Expand Up @@ -63,7 +46,7 @@ def commit_db_transaction_with_callback
decrement_transaction_pointer
@already_decremented = true
end

# We still want to raise the exception.
raise
ensure
Expand Down Expand Up @@ -92,51 +75,51 @@ def rollback_db_transaction_with_callback
decrement_transaction_pointer
end
alias_method_chain :rollback_db_transaction, :callback

def unique_transaction_key
[object_id, transaction_pointer]
end

def old_transaction_key
[object_id, transaction_pointer - 1]
end

protected

def trigger_before_commit_callbacks
AfterCommit.records(self).each do |record|
record.send :callback, :before_commit
end
end
end

def trigger_before_commit_on_create_callbacks
AfterCommit.created_records(self).each do |record|
record.send :callback, :before_commit_on_create
end
end
end

def trigger_before_commit_on_update_callbacks
AfterCommit.updated_records(self).each do |record|
record.send :callback, :before_commit_on_update
end
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
end
end
end

def trigger_before_rollback_callbacks
AfterCommit.records(self).each do |record|
record.send :callback, :before_rollback
end
end
end

def trigger_after_commit_callbacks
Expand All @@ -146,31 +129,31 @@ def trigger_after_commit_callbacks
record.send :callback, :after_commit
end
end

def trigger_after_commit_on_create_callbacks
# Trigger the after_commit_on_create callback for each of the committed
# records.
AfterCommit.created_records(self).each do |record|
record.send :callback, :after_commit_on_create
end
end

def trigger_after_commit_on_update_callbacks
# Trigger the after_commit_on_update callback for each of the committed
# records.
AfterCommit.updated_records(self).each do |record|
record.send :callback, :after_commit_on_update
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.
Expand All @@ -184,24 +167,24 @@ def trigger_after_rollback_callbacks
# records.
AfterCommit.records(self).each do |record|
record.send :callback, :after_rollback
end
end
end

def transaction_pointer
Thread.current[:after_commit_pointer] ||= 0
end

def increment_transaction_pointer
Thread.current[:after_commit_pointer] ||= 0
Thread.current[:after_commit_pointer] += 1
end

def decrement_transaction_pointer
Thread.current[:after_commit_pointer] ||= 0
Thread.current[:after_commit_pointer] -= 1
end

end
end
end
end
end
end

0 comments on commit e64355e

Please sign in to comment.