Skip to content

Instantly share code, notes, and snippets.

@lifo
Created May 26, 2009 10:53
Show Gist options
  • Save lifo/118008 to your computer and use it in GitHub Desktop.
Save lifo/118008 to your computer and use it in GitHub Desktop.
diff --git a/activesupport/lib/active_support/new_callbacks.rb b/activesupport/lib/active_support/new_callbacks.rb
index 039d0fa..8d60653 100644
--- a/activesupport/lib/active_support/new_callbacks.rb
+++ b/activesupport/lib/active_support/new_callbacks.rb
@@ -287,6 +287,14 @@ module ActiveSupport
when Proc
@klass.send(:define_method, method_name, &filter)
method_name << (filter.arity == 1 ? "(self)" : "")
+ when Method
+ @klass.send(:define_method, "#{method_name}_method") { filter }
+ @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
+ def #{method_name}(&blk)
+ #{method_name}_method.call(self, &blk)
+ end
+ RUBY_EVAL
+ method_name
when String
@klass.class_eval <<-RUBY_EVAL
def #{method_name}
@@ -298,11 +306,16 @@ module ActiveSupport
kind, name = @kind, @name
@klass.send(:define_method, "#{method_name}_object") { filter }
- if kind == :around
+ case kind
+ when :around
@klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def #{method_name}(&blk)
- if :#{kind} == :around && #{method_name}_object.respond_to?(:filter)
+ if #{method_name}_object.respond_to?(:filter)
#{method_name}_object.send("filter", self, &blk)
+ elsif #{method_name}_object.respond_to?(:before) && #{method_name}_object.respond_to?(:after)
+ #{method_name}_object.send("before", self, &blk)
+ blk.call
+ #{method_name}_object.send("after", self, &blk)
else
#{method_name}_object.send("#{kind}_#{name}", self, &blk)
end
@@ -311,7 +324,13 @@ module ActiveSupport
else
@klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def #{method_name}(&blk)
- #{method_name}_object.send("#{kind}_#{name}", self, &blk)
+ if #{method_name}_object.respond_to?(:filter)
+ #{method_name}_object.send("filter", self, &blk)
+ elsif #{method_name}_object.respond_to?(:#{kind})
+ #{method_name}_object.send("#{kind}", self, &blk)
+ else
+ #{method_name}_object.send("#{kind}_#{name}", self, &blk)
+ end
end
RUBY_EVAL
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment