public
Description: Treat an ActiveRecord model as a file attachment, storing its patch, size, content type, etc.
Homepage: http://weblog.techno-weenie.net
Clone URL: git://github.com/technoweenie/attachment_fu.git
Click here to lend your support to: attachment_fu and make a donation at www.pledgie.com !
Some tweaks to support Rails 2.0 and Rails 2.1 due to 
ActiveSupport::Callback changes.
technoweenie (author)
Sat Mar 22 13:39:39 -0700 2008
commit  416fbb0017fa4ecaccfca4bcada592d694d532e1
tree    0de3e2c574271386049b7379986118f0d8e0e811
parent  18c0b2d5c9c153d6d71dec97e04e155d7faffa99
...
 
 
 
 
1
2
3
...
1
2
3
4
5
6
7
0
@@ -1,3 +1,7 @@
0
+* Mar 22 2008 *
0
+* Some tweaks to support Rails 2.0 and Rails 2.1 due to ActiveSupport::Callback changes.
0
+ Thanks to http://blog.methodmissing.com/2008/1/19/edge-callback-refactorings-attachment_fu/
0
+
0
 * Feb. 26, 2008 *
0
 * remove breakpoint from test_helper, makes test suite crazy (at least Rails 2+) [Rob Sanheim]
0
 * make S3 test really optional [Rob Sanheim]
...
208
209
210
 
 
 
 
211
212
213
...
419
420
421
422
423
 
 
 
 
 
 
 
 
 
 
424
425
426
427
428
 
429
430
431
432
433
434
435
436
 
 
 
437
438
439
440
441
442
443
444
445
446
447
448
449
 
 
 
 
 
 
 
 
 
450
 
451
452
453
...
208
209
210
211
212
213
214
215
216
217
...
423
424
425
 
 
426
427
428
429
430
431
432
433
434
435
436
 
 
 
 
437
438
 
 
439
 
 
 
 
440
441
442
443
 
 
 
 
 
 
 
 
 
 
 
 
444
445
446
447
448
449
450
451
452
453
454
455
456
457
0
@@ -208,6 +208,10 @@ module Technoweenie # :nodoc:
0
     end
0
 
0
     module InstanceMethods
0
+ def self.included(base)
0
+ base.define_callbacks *[:after_resize, :after_attachment_saved, :before_thumbnail_saved] if base.respond_to?(:define_callbacks)
0
+ end
0
+
0
       # Checks whether the attachment's content type is an image content type
0
       def image?
0
         self.class.image?(content_type)
0
@@ -419,35 +423,35 @@ module Technoweenie # :nodoc:
0
 
0
         # Yanked from ActiveRecord::Callbacks, modified so I can pass args to the callbacks besides self.
0
         # Only accept blocks, however
0
- def callback_with_args(method, arg = self)
0
- notify(method)
0
+ if ActiveSupport.const_defined?(:Callbacks)
0
+ # Rails 2.1 and beyond!
0
+ def callback_with_args(method, arg = self)
0
+ notify(method)
0
+
0
+ result = run_callbacks(method, { :object => arg }) { |result, object| result == false }
0
+
0
+ if result != false && respond_to_without_attributes?(method)
0
+ result = send(method)
0
+ end
0
 
0
- result = nil
0
- callbacks_for(method).each do |callback|
0
- result = callback.call(self, arg)
0
- return false if result == false
0
+ result
0
           end
0
- result
0
- end
0
 
0
- # Rather ugly monkey-patch of AS::Callbacks to support taking an arg
0
- if defined?(::ActiveSupport::Callbacks)
0
- def callbacks_for(method) #:nodoc: compatibility method
0
- self.class.send("#{method}_callback_chain")
0
+ def run_callbacks(kind, options = {}, &block)
0
+ options.reverse_merge!( :object => self )
0
+ self.class.send("#{kind}_callback_chain").run(options[:object], options, &block)
0
           end
0
- class ::ActiveSupport::Callbacks::Callback
0
- # Make callbacks accept arguments, but only pass them along to procs for now
0
- def call(object, *args)
0
- if should_run_callback?(object)
0
- case method
0
- when Proc
0
- args = [object, *args].compact
0
- method.call(*args)
0
- else
0
- evaluate_method(method, object)
0
- end
0
- end
0
+ else
0
+ # Rails 2.0
0
+ def callback_with_args(method, arg = self)
0
+ notify(method)
0
+
0
+ result = nil
0
+ callbacks_for(method).each do |callback|
0
+ result = callback.call(self, arg)
0
+ return false if result == false
0
             end
0
+ result
0
           end
0
         end
0
 

Comments

    No one has commented yet.