From 08318b8bcd32bae741e672899a33c6a7d52664c8 Mon Sep 17 00:00:00 2001 From: Josh Peek Date: Fri, 4 Apr 2008 20:26:42 +0000 Subject: [PATCH] Replaced callback method evaluation in AssociationCollection class to use ActiveSupport::Callbacks. Modified ActiveSupport::Callbacks::Callback#call to accept multiple arguments. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9225 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_controller/filters.rb | 10 +++++-- .../associations/association_collection.rb | 15 ++-------- activesupport/CHANGELOG | 2 ++ activesupport/lib/active_support/callbacks.rb | 28 ++++++++----------- 4 files changed, 23 insertions(+), 32 deletions(-) diff --git a/actionpack/lib/action_controller/filters.rb b/actionpack/lib/action_controller/filters.rb index 218a9c0b80..73721cd1ec 100644 --- a/actionpack/lib/action_controller/filters.rb +++ b/actionpack/lib/action_controller/filters.rb @@ -385,8 +385,14 @@ module ActionController #:nodoc: def call(controller, &block) if should_run_callback?(controller) - proc = filter_responds_to_before_and_after? ? around_proc : method - evaluate_method(proc, controller, &block) + method = filter_responds_to_before_and_after? ? around_proc : self.method + + # For around_filter do |controller, action| + if method.is_a?(Proc) && method.arity == 2 + evaluate_method(method, controller, block) + else + evaluate_method(method, controller, &block) + end else block.call end diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 3b8954e435..141fdcab3d 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -240,21 +240,10 @@ module ActiveRecord def callback(method, record) callbacks_for(method).each do |callback| - case callback - when Symbol - @owner.send(callback, record) - when Proc, Method - callback.call(@owner, record) - else - if callback.respond_to?(method) - callback.send(method, @owner, record) - else - raise ActiveRecordError, "Callbacks must be a symbol denoting the method to call, a string to be evaluated, a block to be invoked, or an object responding to the callback method." - end - end + ActiveSupport::Callbacks::Callback.new(method, callback, record).call(@owner, record) end end - + def callbacks_for(callback_name) full_callback_name = "#{callback_name}_for_#{@reflection.name}" @owner.class.read_inheritable_attribute(full_callback_name.to_sym) || [] diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index b2955ab69d..79522a0f0e 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Modified ActiveSupport::Callbacks::Callback#call to accept multiple arguments. + * Time #yesterday and #tomorrow behave correctly crossing DST boundary. Closes #7399 [sblackstone] * TimeWithZone: Adding tests for dst and leap day edge cases when advancing time [Geoff Buesing] diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 40d71af69d..329cc2fdc7 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -149,8 +149,8 @@ module ActiveSupport self.class.new(@kind, @method, @options.dup) end - def call(object, &block) - evaluate_method(method, object, &block) if should_run_callback?(object) + def call(*args, &block) + evaluate_method(method, *args, &block) if should_run_callback?(*args) rescue LocalJumpError raise ArgumentError, "Cannot yield from a Proc type filter. The Proc must take two " + @@ -158,24 +158,18 @@ module ActiveSupport end private - def evaluate_method(method, object, &block) + def evaluate_method(method, *args, &block) case method when Symbol - object.send(method, &block) + object = args.shift + object.send(method, *args, &block) when String - eval(method, object.instance_eval { binding }) + eval(method, args.first.instance_eval { binding }) when Proc, Method - case method.arity - when -1, 1 - method.call(object, &block) - when 2 - method.call(object, block) - else - raise ArgumentError, 'Callback blocks must take one or two arguments.' - end + method.call(*args, &block) else if method.respond_to?(kind) - method.send(kind, object, &block) + method.send(kind, *args, &block) else raise ArgumentError, "Callbacks must be a symbol denoting the method to call, a string to be evaluated, " + @@ -184,11 +178,11 @@ module ActiveSupport end end - def should_run_callback?(object) + def should_run_callback?(*args) if options[:if] - evaluate_method(options[:if], object) + evaluate_method(options[:if], *args) elsif options[:unless] - !evaluate_method(options[:unless], object) + !evaluate_method(options[:unless], *args) else true end