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 !
get rid of self.included(base) monkeying, fix some temp_path race 
condition
technoweenie (author)
Tue Sep 30 18:06:28 -0700 2008
commit  1f11c0aee00c8e6b91fe86b723c9ff837ffaae04
tree    b85a08293edafc6c1043d89ee27ec23a0dbd7812
parent  31a44f58b2a12917a2d4c1fdcee7e95dfc48e7a2
...
52
53
54
55
 
56
57
58
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
61
62
...
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
...
128
129
130
131
132
133
134
 
 
 
 
 
135
136
137
...
52
53
54
 
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
...
107
108
109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
111
112
...
132
133
134
 
 
 
 
135
136
137
138
139
140
141
142
0
@@ -52,11 +52,33 @@ module AttachmentFu
0
     # # PLUS any fields that your tasks may use.
0
     # end
0
     def is_attachment(options = {}, &block)
0
- include AttachmentFu::InstanceMethods
0
+ setup_attachment_fu_on(self)
0
       self.queued_attachment = options[:queued]
0
       self.attachment_path = options[:path] || attachment_path || File.join("public", table_name)
0
       self.attachment_tasks(&block)
0
     end
0
+
0
+ def setup_attachment_fu_on(klass)
0
+ class << klass
0
+ attr_writer :attachment_tasks
0
+
0
+ def attachment_tasks(&block)
0
+ @attachment_tasks ||= superclass.respond_to?(:attachment_tasks) ? superclass.attachment_tasks.copy : AttachmentFu::Tasks.new(self)
0
+ @attachment_tasks.instance_eval(&block) if block
0
+ @attachment_tasks
0
+ end
0
+ end
0
+
0
+ klass.class_eval do
0
+ include AttachmentFu::InstanceMethods
0
+ attr_reader :temp_path
0
+ class_inheritable_accessor :queued_attachment
0
+ class_inheritable_accessor :attachment_path
0
+ before_create :set_new_attachment
0
+ after_save :save_attachment
0
+ after_destroy :delete_attachment
0
+ end
0
+ end
0
   end
0
 
0
   # joined with #attachment_path to get the full path
0
@@ -85,24 +107,6 @@ module AttachmentFu
0
 
0
   # This mixin is included in attachment classes by AttachmentFu::SetupMethods.is_attachment.
0
   module InstanceMethods
0
- def self.included(base)
0
- class << base
0
- attr_writer :attachment_tasks
0
-
0
- def attachment_tasks(&block)
0
- @attachment_tasks ||= superclass.respond_to?(:attachment_tasks) ? superclass.attachment_tasks.copy : AttachmentFu::Tasks.new(self)
0
- @attachment_tasks.instance_eval(&block) if block
0
- @attachment_tasks
0
- end
0
- end
0
- base.send :attr_reader, :temp_path
0
- base.send :class_inheritable_accessor, :queued_attachment
0
- base.send :class_inheritable_accessor, :attachment_path
0
- base.before_create :set_new_attachment
0
- base.after_save :save_attachment
0
- base.after_destroy :delete_attachment
0
- end
0
-
0
     # Strips filename of any funny characters.
0
     def filename=(value)
0
       strip_filename value if value
0
@@ -128,10 +132,11 @@ module AttachmentFu
0
       self.filename = file_data.original_filename
0
       if file_data.respond_to?(:rewind) # it's an IO object
0
         file_data.rewind
0
- self.temp_path = Tempfile.new(filename)
0
- temp_path.binmode
0
- temp_path << file_data.read
0
- temp_path.rewind
0
+ tmp = Tempfile.new(filename)
0
+ tmp.binmode
0
+ tmp << file_data.read
0
+ tmp.rewind
0
+ self.temp_path = tmp
0
       else
0
         self.temp_path = file_data
0
       end

Comments

    No one has commented yet.