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 !
extract some file utility methods
technoweenie (author)
Sat Mar 15 15:10:12 -0700 2008
commit  8b529ec4c9a80ff9340ff24c4003716222b5801e
tree    6be25e1240aa1488aa0ba338657ca53885c6f204
parent  776ba411e543c5bdb344cc7fc599a51063b6426b
...
1
2
 
3
...
 
1
2
3
0
@@ -1 +1 @@
0
-AttachmentFu.init
0
\ No newline at end of file
0
+AttachmentFu.setup ActiveRecord::Base
0
\ No newline at end of file
...
19
20
21
22
23
 
 
24
25
26
...
94
95
96
97
98
99
100
101
102
103
104
105
106
107
 
 
 
108
109
110
...
113
114
115
116
117
118
119
120
121
122
 
123
124
125
126
127
128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
130
...
19
20
21
 
 
22
23
24
25
26
...
94
95
96
 
 
 
 
 
 
 
 
 
 
 
97
98
99
100
101
102
...
105
106
107
 
 
 
 
 
 
 
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
0
@@ -19,8 +19,8 @@ module AttachmentFu
0
     @root_path = value
0
   end
0
 
0
- def self.init
0
- class << ActiveRecord::Base
0
+ def self.setup(klass)
0
+ class << klass
0
       def is_attachment(options = {})
0
         include AttachmentFu
0
         self.attachment_path = options[:path] || File.join("public", table_name)
0
@@ -94,17 +94,9 @@ module AttachmentFu
0
   end
0
   
0
   def temp_path=(value)
0
- self.size = value.is_a?(String) || !value.respond_to?(:size) ? File.size(value) : value.size
0
- self.filename ||= begin
0
- if value.respond_to?(:basename)
0
- value.basename.to_s
0
- elsif value.respond_to?(:path)
0
- File.basename(value.path)
0
- else
0
- File.basename(value)
0
- end
0
- end
0
- @temp_path = value
0
+ self.size = value.is_a?(String) || !value.respond_to?(:size) ? File.size(value) : value.size
0
+ self.filename ||= basename_for value
0
+ @temp_path = value
0
   end
0
   
0
   def delete_attachment
0
@@ -113,17 +105,31 @@ module AttachmentFu
0
 
0
 protected
0
   def save_attachment
0
- old_path = if @temp_path.respond_to?(:path)
0
- @temp_path.path
0
- elsif @temp_path.respond_to?(:realpath)
0
- @temp_path.realpath.to_s
0
- elsif @temp_path
0
- @temp_path.to_s
0
- end
0
+ old_path = full_path_for @temp_path
0
     return if old_path.nil?
0
     FileUtils.mkdir_p(File.dirname(full_filename))
0
     FileUtils.mv(old_path, full_filename)
0
     File.chmod(0644, full_filename)
0
     @temp_path = nil
0
   end
0
+
0
+ # Could be a string, Pathname, Tempfile, who knows?
0
+ def full_path_for(path)
0
+ if path.respond_to?(:path)
0
+ path.path
0
+ elsif path.respond_to?(:realpath)
0
+ path.realpath.to_s
0
+ elsif path
0
+ path.to_s
0
+ end
0
+ end
0
+
0
+ # Could be a string, Pathname, Tempfile, who knows?
0
+ def basename_for(path)
0
+ if path.respond_to?(:basename)
0
+ path.basename.to_s
0
+ else
0
+ File.basename(path.respond_to?(:path) ? path.path : path)
0
+ end
0
+ end
0
 end
0
\ No newline at end of file
...
1
2
3
4
 
5
6
7
...
10
11
12
13
14
 
 
15
16
17
...
1
2
3
 
4
5
6
7
...
10
11
12
 
 
13
14
15
16
17
0
@@ -1,7 +1,7 @@
0
 require 'rubygems'
0
 
0
 dir = File.dirname(__FILE__)
0
-rails_app_spec = "#{dir}/../../../../config/environment.rb"
0
+rails_app = "#{dir}/../../../../config/environment.rb"
0
 vendor_rspec = "#{dir}/../../rspec/lib"
0
 
0
 if File.exist?(vendor_rspec)
0
@@ -10,8 +10,8 @@ else
0
   gem 'rspec'
0
 end
0
 
0
-if File.exist?(rails_app_spec)
0
- require rails_app_spec
0
+if File.exist?(rails_app)
0
+ require rails_app
0
 else
0
   raise "TODO: attempt to load activerecord and activesupport from gems"
0
   # also, establish connection with sqlite3 or use DB env var as path to database.yml

Comments

    No one has commented yet.