Permalink
Browse files

Restore support for Rails versions before 2.0.2 (without define_callb…

…acks).
  • Loading branch information...
1 parent 3face97 commit 33dcb50415de015cf9e8bdeb4ccb2882476362b4 @jasoncodes jasoncodes committed May 16, 2010
Showing with 51 additions and 22 deletions.
  1. +0 −1 lib/after_commit.rb
  2. +51 −21 lib/after_commit/active_support_callbacks.rb
View
@@ -77,6 +77,5 @@ def self.collection(collection, connection)
require 'after_commit/connection_adapters'
require 'after_commit/test_bypass'
-ActiveSupport::Callbacks.send(:include, AfterCommit::ActiveSupportCallbacks)
ActiveRecord::Base.send(:include, AfterCommit::ActiveRecord)
ActiveRecord::Base.include_after_commit_extensions
@@ -1,29 +1,59 @@
-module AfterCommit
- module ActiveSupportCallbacks
- def self.included(base)
-
- base::Callback.class_eval do
- def have_callback?
- true
+if defined? ActiveSupport::Callbacks
+
+ module AfterCommit
+ module ActiveSupportCallbacks
+ def self.included(base)
+
+ base::Callback.class_eval do
+ def have_callback?
+ true
+ end
end
- end
-
- base::CallbackChain.class_eval do
- def have_callback?
- any? &:have_callback?
+
+ base::CallbackChain.class_eval do
+ def have_callback?
+ any? &:have_callback?
+ end
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?
+
+ 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

0 comments on commit 33dcb50

Please sign in to comment.