Permalink
Browse files

Merge remote branch 'jasoncodes/master' into jasoncodes

Conflicts:
	lib/after_commit/test_bypass.rb
  • Loading branch information...
2 parents 5101f1e + 33dcb50 commit ae311feff4ea3c1b8358c945ee8e74648b8a2db0 @pat committed May 20, 2010
View
@@ -1 +1,6 @@
require 'after_commit'
+
+# enable TestBypass by default in RAILS_ENV == 'test'
+ActiveRecord::Base.class_eval do
+ include AfterCommit::TestBypass if RAILS_ENV == 'test'
+end
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] ||= {}
@@ -62,6 +72,7 @@ def self.collection(collection, connection)
end
end
+require 'after_commit/active_support_callbacks'
require 'after_commit/active_record'
require 'after_commit/connection_adapters'
require 'after_commit/after_savepoint'
@@ -31,91 +31,55 @@ def include_after_commit_extension(adapter)
end
end
- # The define_callbacks method was added post Rails 2.0.2 - if it
- # doesn't exist, we define the callback manually
- if respond_to?(:define_callbacks)
- define_callbacks :after_commit,
- :after_commit_on_create,
- :after_commit_on_update,
- :after_commit_on_destroy,
- :after_rollback,
- :before_commit,
- :before_commit_on_create,
- :before_commit_on_update,
- :before_commit_on_destroy,
- :before_rollback
- else
- class << self
- # Handle after_commit callbacks - call all the registered callbacks.
- def after_commit(*callbacks, &block)
- callbacks << block if block_given?
- write_inheritable_array(:after_commit, callbacks)
- end
-
- def after_commit_on_create(*callbacks, &block)
- callbacks << block if block_given?
- write_inheritable_array(:after_commit_on_create, callbacks)
- end
-
- def after_commit_on_update(*callbacks, &block)
- callbacks << block if block_given?
- write_inheritable_array(:after_commit_on_update, callbacks)
- end
-
- def after_commit_on_destroy(*callbacks, &block)
- callbacks << block if block_given?
- write_inheritable_array(:after_commit_on_destroy, callbacks)
- end
-
- def after_rollback(*callbacks, &block)
- callbacks << block if block_given?
- write_inheritable_array(:after_commit, callbacks)
- end
-
- def before_commit(*callbacks, &block)
- callbacks << block if block_given?
- write_inheritable_array(:before_commit, callbacks)
- end
-
- def before_commit_on_create(*callbacks, &block)
- callbacks << block if block_given?
- write_inheritable_array(:before_commit_on_create, callbacks)
- end
-
- def before_commit_on_update(*callbacks, &block)
- callbacks << block if block_given?
- write_inheritable_array(:before_commit_on_update, callbacks)
- end
-
- 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
-
+ 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
+ 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
- AfterCommit.record(self.class.connection, self)
- AfterCommit.record_created(self.class.connection, self)
+ 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
- AfterCommit.record(self.class.connection, self)
- AfterCommit.record_updated(self.class.connection, self)
+ 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
- AfterCommit.record(self.class.connection, self)
- AfterCommit.record_destroyed(self.class.connection, self)
+ add_committed_record
+ if have_callback? :before_commit_on_destroy, :after_commit_on_destroy
+ AfterCommit.record_destroyed(self.class.connection, self)
+ end
end
end
end
@@ -0,0 +1,59 @@
+if defined? ActiveSupport::Callbacks
+
+ module AfterCommit
+ module ActiveSupportCallbacks
+ def self.included(base)
+
+ base::Callback.class_eval do
+ def have_callback?
+ true
+ end
+ end
+
+ base::CallbackChain.class_eval do
+ def have_callback?
+ any? &:have_callback?
+ end
+ end
+
+ base.class_eval do
+ def have_callback?(*callbacks)
+ self.class.observers.size > 0 or
+ self.class.count_observers > 0 or
+ callbacks.any? do |callback|
+ self.class.send("#{callback}_callback_chain").have_callback?
+ end
+ end
+ end
+
+ end
+ end
+ end
+ ActiveSupport::Callbacks.send(:include, AfterCommit::ActiveSupportCallbacks)
+
+else
+
+ class ActiveRecord::Base
+
+ def self.define_callbacks(*names)
+ names.each do |name|
+ instance_eval <<-RUBY
+ def #{name}(*callbacks, &block)
+ callbacks << block if block_given?
+ write_inheritable_array(:#{name}, callbacks)
+ end
+ RUBY
+ end
+ end
+
+ def have_callback?(*names)
+ self.class.observers.size > 0 or
+ self.class.count_observers > 0 or
+ names.any? do |name|
+ !self.class.read_inheritable_attribute(name).blank?
+ end
+ end
+
+ end
+
+end
@@ -40,6 +40,7 @@ def release_savepoint_with_callback
begin
trigger_before_commit_callbacks
trigger_before_commit_on_create_callbacks
+ trigger_before_commit_on_save_callbacks
trigger_before_commit_on_update_callbacks
trigger_before_commit_on_destroy_callbacks
@@ -48,6 +49,7 @@ def release_savepoint_with_callback
trigger_after_commit_callbacks
trigger_after_commit_on_create_callbacks
+ trigger_after_commit_on_save_callbacks
trigger_after_commit_on_update_callbacks
trigger_after_commit_on_destroy_callbacks
rescue
@@ -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
rescue
@@ -102,6 +104,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
@@ -138,6 +146,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.
Oops, something went wrong.

0 comments on commit ae311fe

Please sign in to comment.