mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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
This commit is contained in:
parent
36b8073ff5
commit
08318b8bcd
4 changed files with 23 additions and 32 deletions
|
@ -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
|
||||
|
|
|
@ -240,18 +240,7 @@ 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
|
||||
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue