Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 9225

Show
Ignore:
Timestamp:
04/04/08 20:26:42 (8 months ago)
Author:
josh
Message:

Replaced callback method evaluation in AssociationCollection class to use ActiveSupport::Callbacks. Modified ActiveSupport::Callbacks::Callback#call to accept multiple arguments.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_controller/filters.rb

    r9080 r9225  
    386386      def call(controller, &block) 
    387387        if should_run_callback?(controller) 
    388           proc = filter_responds_to_before_and_after? ? around_proc : method 
    389           evaluate_method(proc, controller, &block) 
     388          method = filter_responds_to_before_and_after? ? around_proc : self.method 
     389 
     390          # For around_filter do |controller, action| 
     391          if method.is_a?(Proc) && method.arity == 2 
     392            evaluate_method(method, controller, block) 
     393          else 
     394            evaluate_method(method, controller, &block) 
     395          end 
    390396        else 
    391397          block.call 
  • trunk/activerecord/lib/active_record/associations/association_collection.rb

    r9200 r9225  
    241241        def callback(method, record) 
    242242          callbacks_for(method).each do |callback| 
    243             case callback 
    244               when Symbol 
    245                 @owner.send(callback, record) 
    246               when Proc, Method 
    247                 callback.call(@owner, record) 
    248               else 
    249                 if callback.respond_to?(method) 
    250                   callback.send(method, @owner, record) 
    251                 else 
    252                   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." 
    253                 end 
    254             end 
    255           end 
    256         end 
    257          
     243            ActiveSupport::Callbacks::Callback.new(method, callback, record).call(@owner, record) 
     244          end 
     245        end 
     246 
    258247        def callbacks_for(callback_name) 
    259248          full_callback_name = "#{callback_name}_for_#{@reflection.name}" 
  • trunk/activesupport/CHANGELOG

    r9221 r9225  
    11*SVN* 
     2 
     3* Modified ActiveSupport::Callbacks::Callback#call to accept multiple arguments. 
    24 
    35* Time #yesterday and #tomorrow behave correctly crossing DST boundary. Closes #7399 [sblackstone] 
  • trunk/activesupport/lib/active_support/callbacks.rb

    r9055 r9225  
    150150      end 
    151151 
    152       def call(object, &block) 
    153         evaluate_method(method, object, &block) if should_run_callback?(object
     152      def call(*args, &block) 
     153        evaluate_method(method, *args, &block) if should_run_callback?(*args
    154154      rescue LocalJumpError 
    155155        raise ArgumentError, 
     
    159159 
    160160      private 
    161         def evaluate_method(method, object, &block) 
     161        def evaluate_method(method, *args, &block) 
    162162          case method 
    163163            when Symbol 
    164               object.send(method, &block) 
     164              object = args.shift 
     165              object.send(method, *args, &block) 
    165166            when String 
    166               eval(method, object.instance_eval { binding }) 
     167              eval(method, args.first.instance_eval { binding }) 
    167168            when Proc, Method 
    168               case method.arity 
    169                 when -1, 1 
    170                   method.call(object, &block) 
    171                 when 2 
    172                   method.call(object, block) 
    173                 else 
    174                   raise ArgumentError, 'Callback blocks must take one or two arguments.' 
    175               end 
     169              method.call(*args, &block) 
    176170            else 
    177171              if method.respond_to?(kind) 
    178                 method.send(kind, object, &block) 
     172                method.send(kind, *args, &block) 
    179173              else 
    180174                raise ArgumentError, 
     
    185179        end 
    186180 
    187         def should_run_callback?(object
     181        def should_run_callback?(*args
    188182          if options[:if] 
    189             evaluate_method(options[:if], object
     183            evaluate_method(options[:if], *args
    190184          elsif options[:unless] 
    191             !evaluate_method(options[:unless], object
     185            !evaluate_method(options[:unless], *args
    192186          else 
    193187            true