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 !
- Fixing edge callback changes issue (see 
http://dev.rubyonrails.org/changeset/8664)
- Fix rmagick assertion
nicksieger (author)
Tue Mar 04 09:15:36 -0800 2008
commit  ee8474831d3181596220f2b970a707730b851950
tree    b1157b674c4bbfbe2fc03f6108e11030b7b5a77c
parent  b0da755535a196c8a12e4f0c58cfc5cafda4f6e2
...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
131
132
133
134
135
136
137
138
139
140
141
142
 
 
 
 
 
 
 
 
 
 
 
 
143
144
145
146
147
148
149
150
151
152
153
 
 
 
 
 
 
 
 
 
 
 
154
155
156
...
397
398
399
 
 
400
401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402
403
 
404
405
406
...
116
117
118
 
 
 
 
 
 
 
 
 
 
 
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
 
 
 
 
 
 
 
 
 
 
 
 
136
137
138
139
140
141
142
143
144
145
146
147
148
 
 
 
 
 
 
 
 
 
 
149
150
151
152
153
154
155
156
157
158
159
160
161
162
...
403
404
405
406
407
408
 
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
 
429
430
431
432
0
@@ -116,41 +116,47 @@ module Technoweenie # :nodoc:
0
         content_types.include?(content_type)
0
       end
0
 
0
- # Callback after an image has been resized.
0
- #
0
- # class Foo < ActiveRecord::Base
0
- # acts_as_attachment
0
- # after_resize do |record, img|
0
- # record.aspect_ratio = img.columns.to_f / img.rows.to_f
0
- # end
0
- # end
0
- def after_resize(&block)
0
- write_inheritable_array(:after_resize, [block])
0
- end
0
+ if defined?(::ActiveSupport::Callbacks)
0
+ def self.extended(base)
0
+ base.define_callbacks :after_resize, :after_attachment_saved, :before_thumbnail_saved
0
+ end
0
+ else
0
+ # Callback after an image has been resized.
0
+ #
0
+ # class Foo < ActiveRecord::Base
0
+ # acts_as_attachment
0
+ # after_resize do |record, img|
0
+ # record.aspect_ratio = img.columns.to_f / img.rows.to_f
0
+ # end
0
+ # end
0
+ def after_resize(&block)
0
+ write_inheritable_array(:after_resize, [block])
0
+ end
0
 
0
- # Callback after an attachment has been saved either to the file system or the DB.
0
- # Only called if the file has been changed, not necessarily if the record is updated.
0
- #
0
- # class Foo < ActiveRecord::Base
0
- # acts_as_attachment
0
- # after_attachment_saved do |record|
0
- # ...
0
- # end
0
- # end
0
- def after_attachment_saved(&block)
0
- write_inheritable_array(:after_attachment_saved, [block])
0
- end
0
+ # Callback after an attachment has been saved either to the file system or the DB.
0
+ # Only called if the file has been changed, not necessarily if the record is updated.
0
+ #
0
+ # class Foo < ActiveRecord::Base
0
+ # acts_as_attachment
0
+ # after_attachment_saved do |record|
0
+ # ...
0
+ # end
0
+ # end
0
+ def after_attachment_saved(&block)
0
+ write_inheritable_array(:after_attachment_saved, [block])
0
+ end
0
 
0
- # Callback before a thumbnail is saved. Use this to pass any necessary extra attributes that may be required.
0
- #
0
- # class Foo < ActiveRecord::Base
0
- # acts_as_attachment
0
- # before_thumbnail_saved do |record, thumbnail|
0
- # ...
0
- # end
0
- # end
0
- def before_thumbnail_saved(&block)
0
- write_inheritable_array(:before_thumbnail_saved, [block])
0
+ # Callback before a thumbnail is saved. Use this to pass any necessary extra attributes that may be required.
0
+ #
0
+ # class Foo < ActiveRecord::Base
0
+ # acts_as_attachment
0
+ # before_thumbnail_saved do |record, thumbnail|
0
+ # ...
0
+ # end
0
+ # end
0
+ def before_thumbnail_saved(&block)
0
+ write_inheritable_array(:before_thumbnail_saved, [block])
0
+ end
0
       end
0
 
0
       # Get the thumbnail class, which is the current attachment class by default.
0
@@ -397,10 +403,30 @@ module Technoweenie # :nodoc:
0
             result = callback.call(self, arg)
0
             return false if result == false
0
           end
0
+ result
0
+ end
0
 
0
- return result
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
+ 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
+ end
0
+ end
0
         end
0
-
0
+
0
         # Removes the thumbnails for the attachment, if it has any
0
         def destroy_thumbnails
0
           self.thumbnails.each { |thumbnail| thumbnail.destroy } if thumbnailable?
...
162
163
164
165
 
166
167
168
...
162
163
164
 
165
166
167
168
0
@@ -162,7 +162,7 @@ class RmagickTest < Test::Unit::TestCase
0
       assert_equal 'rails_thumb.png', attachment.thumbnails.first.filename
0
       assert_equal attachment.thumbnails.first.full_filename, attachment.full_filename(attachment.thumbnails.first.thumbnail),
0
         "#full_filename does not use thumbnail class' path."
0
- assert_equal attachment.destroy attachment
0
+ assert_equal attachment.destroy, attachment
0
     end
0
     
0
     test_against_subclass :test_should_use_thumbnail_subclass, ImageWithThumbsClassFileAttachment

Comments

    No one has commented yet.