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 !
other fixes, but most importantly: Don't try to remove thumbnails if there 
aren't any.  Closes #3 [ben stiglitz]

git-svn-id: 
http://svn.techno-weenie.net/projects/plugins/attachment_fu@2830 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Mon Apr 02 14:18:46 -0700 2007
commit  1c14574906dcdcbfa445b94558a7e0789ed68850
tree    06994293428a0af22839923ad890f9d57c2bc35b
parent  62d8d40549d5ab13f296f7666a75a9ed800c9551
0
...
1
2
3
4
5
 
 
 
6
...
1
2
3
 
4
5
6
7
8
0
@@ -1,4 +1,6 @@
0
 attachment-fu
0
 =====================
0
 
0
-Acts as Attachment rewrite, come back later please!
0
\ No newline at end of file
0
+It works! Mike Clark writes a mean tutorial:
0
+
0
+http://clarkware.com/cgi/blosxom/2007/02/24#FileUploadFu
0
\ No newline at end of file
...
44
45
46
 
 
 
 
 
47
48
49
50
 
51
52
53
54
55
56
 
 
 
 
57
58
 
59
60
61
 
62
63
 
64
65
66
...
68
69
70
71
 
72
73
74
...
79
80
81
82
 
 
 
 
 
83
84
85
86
87
88
89
90
91
...
384
385
386
 
 
 
 
 
387
388
389
...
44
45
46
47
48
49
50
51
52
53
 
 
54
55
56
 
 
 
 
57
58
59
60
61
 
62
63
64
 
65
66
67
68
69
70
71
...
73
74
75
 
76
77
78
79
...
84
85
86
 
87
88
89
90
91
92
93
94
 
 
 
95
96
97
...
390
391
392
393
394
395
396
397
398
399
400
0
@@ -44,23 +44,28 @@ module Technoweenie # :nodoc:
0
         options[:thumbnails] ||= {}
0
         options[:thumbnail_class] ||= self
0
         options[:s3_access] ||= :public_read
0
+ options[:content_type] = [options[:content_type]].flatten.collect! { |t| t == :image ? Technoweenie::AttachmentFu.content_types : t }.flatten unless options[:content_type].nil?
0
+
0
+ # doing these shenanigans so that #attachment_options is available to processors and backends
0
+ class_inheritable_accessor :attachment_options
0
+ self.attachment_options = options
0
 
0
         # only need to define these once on a class
0
- unless included_modules.include? InstanceMethods
0
- class_inheritable_accessor :attachment_options
0
+ unless included_modules.include?(InstanceMethods)
0
           attr_accessor :thumbnail_resize_options
0
 
0
- options[:storage] ||= (options[:file_system_path] || options[:path_prefix]) ? :file_system : :db_file
0
- options[:path_prefix] ||= options[:file_system_path]
0
- if options[:path_prefix].nil?
0
- options[:path_prefix] = options[:storage] == :s3 ? table_name : File.join("public", table_name)
0
+ attachment_options[:storage] ||= (attachment_options[:file_system_path] || attachment_options[:path_prefix]) ? :file_system : :db_file
0
+ attachment_options[:path_prefix] ||= attachment_options[:file_system_path]
0
+ if attachment_options[:path_prefix].nil?
0
+ attachment_options[:path_prefix] = attachment_options[:storage] == :s3 ? table_name : File.join("public", table_name)
0
           end
0
- options[:path_prefix] = options[:path_prefix][1..-1] if options[:path_prefix].first == '/'
0
+ attachment_options[:path_prefix] = attachment_options[:path_prefix][1..-1] if options[:path_prefix].first == '/'
0
 
0
           with_options :foreign_key => 'parent_id' do |m|
0
- m.has_many :thumbnails, :dependent => :destroy, :class_name => options[:thumbnail_class].to_s
0
+ m.has_many :thumbnails, :class_name => attachment_options[:thumbnail_class].to_s
0
             m.belongs_to :parent, :class_name => base_class.to_s
0
           end
0
+ before_destroy :destroy_thumbnails
0
 
0
           before_validation :set_size_from_temp_path
0
           after_save :after_process_attachment
0
@@ -68,7 +73,7 @@ module Technoweenie # :nodoc:
0
           extend ClassMethods
0
           include InstanceMethods
0
           include Technoweenie::AttachmentFu::Backends.const_get("#{options[:storage].to_s.classify}Backend")
0
- case options[:processor]
0
+ case attachment_options[:processor]
0
             when :none
0
             when nil
0
               processors = Technoweenie::AttachmentFu.default_processors.dup
0
@@ -79,13 +84,14 @@ module Technoweenie # :nodoc:
0
                 retry
0
               end
0
             else
0
- include Technoweenie::AttachmentFu::Processors.const_get("#{options[:processor].to_s.classify}Processor")
0
+ begin
0
+ include Technoweenie::AttachmentFu::Processors.const_get("#{options[:processor].to_s.classify}Processor")
0
+ rescue LoadError, MissingSourceFile
0
+ puts "Problems loading #{options[:processor]}Processor: #{$!}"
0
+ end
0
           end
0
           after_validation :process_attachment
0
         end
0
-
0
- options[:content_type] = [options[:content_type]].flatten.collect { |t| t == :image ? Technoweenie::AttachmentFu.content_types : t }.flatten unless options[:content_type].nil?
0
- self.attachment_options = options
0
       end
0
     end
0
 
0
@@ -384,6 +390,11 @@ module Technoweenie # :nodoc:
0
 
0
           return result
0
         end
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?
0
+ end
0
     end
0
   end
0
 end
...
34
35
36
 
 
 
 
37
38
39
...
128
129
130
131
132
133
 
 
 
 
134
135
136
...
34
35
36
37
38
39
40
41
42
43
...
132
133
134
 
 
 
135
136
137
138
139
140
141
0
@@ -34,6 +34,10 @@ module Technoweenie # :nodoc:
0
       # access_key_id: <your key>
0
       # secret_access_key: <your key>
0
       #
0
+ # You can change the location of the config path by passing a full path to the :s3_config_path option.
0
+ #
0
+ # has_attachment :storage => :s3, :s3_config_path => (RAILS_ROOT + '/config/s3.yml')
0
+ #
0
       # === Required configuration parameters
0
       #
0
       # * <tt>:access_key_id</tt> - The access key id for your S3 account. Provided by Amazon.
0
@@ -128,9 +132,10 @@ module Technoweenie # :nodoc:
0
           end
0
 
0
           begin
0
- @@s3_config = YAML.load_file(RAILS_ROOT + '/config/amazon_s3.yml')[ENV['RAILS_ENV']].symbolize_keys
0
- rescue
0
- raise ConfigFileNotFoundError.new('File RAILS_ROOT/config/amazon_s3.yml not found')
0
+ @@s3_config_path = base.attachment_options[:s3_config_path] || (RAILS_ROOT + '/config/amazon_s3.yml')
0
+ @@s3_config = YAML.load_file(@@s3_config_path)[ENV['RAILS_ENV']].symbolize_keys
0
+ #rescue
0
+ # raise ConfigFileNotFoundError.new('File %s not found' % @@s3_config_path)
0
           end
0
 
0
           @@bucket_name = s3_config[:bucket_name]
...
2
3
4
5
 
6
7
8
...
85
86
87
88
 
89
90
91
92
 
93
94
95
96
 
97
98
99
...
2
3
4
 
5
6
7
8
...
85
86
87
 
88
89
90
91
 
92
93
94
95
 
96
97
98
99
0
@@ -2,7 +2,7 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_hel
0
 require 'net/http'
0
 
0
 class S3Test < Test::Unit::TestCase
0
- if File.exist?(RAILS_ROOT + '/config/amazon_s3.yml')
0
+ if File.exist?(File.join(File.dirname(__FILE__), '../../amazon_s3.yml'))
0
     include BaseAttachmentTests
0
     attachment_model S3Attachment
0
 
0
@@ -85,15 +85,15 @@ class S3Test < Test::Unit::TestCase
0
       end
0
       
0
       def s3_protocol
0
- Technoweenie::AttachmentFu::Backends::S3.protocol
0
+ Technoweenie::AttachmentFu::Backends::S3Backend.protocol
0
       end
0
       
0
       def s3_hostname
0
- Technoweenie::AttachmentFu::Backends::S3.hostname
0
+ Technoweenie::AttachmentFu::Backends::S3Backend.hostname
0
       end
0
 
0
       def s3_port_string
0
- Technoweenie::AttachmentFu::Backends::S3.port_string
0
+ Technoweenie::AttachmentFu::Backends::S3Backend.port_string
0
       end
0
   else
0
     def test_flunk_s3
...
114
115
116
117
 
118
119
120
...
123
124
125
 
126
...
114
115
116
 
117
118
119
120
...
123
124
125
126
127
0
@@ -114,7 +114,7 @@ end
0
 
0
 begin
0
   class S3Attachment < ActiveRecord::Base
0
- has_attachment :storage => :s3, :processor => :rmagick
0
+ has_attachment :storage => :s3, :processor => :rmagick, :s3_config_path => File.join(File.dirname(__FILE__), '../amazon_s3.yml')
0
     validates_as_attachment
0
   end
0
 
0
@@ -123,4 +123,5 @@ begin
0
     validates_as_attachment
0
   end
0
 rescue Technoweenie::AttachmentFu::Backends::S3Backend::ConfigFileNotFoundError
0
+ puts "S3 error: #{$!}"
0
 end
...
9
10
11
12
 
13
14
15
...
21
22
23
24
 
25
26
27
...
9
10
11
 
12
13
14
15
...
21
22
23
 
24
25
26
27
0
@@ -9,7 +9,7 @@ class ImageScienceTest < Test::Unit::TestCase
0
       assert_valid attachment
0
       assert attachment.image?
0
       # test image science thumbnail
0
- assert_equal 43, attachment.width
0
+ assert_equal 42, attachment.width
0
       assert_equal 55, attachment.height
0
       
0
       thumb = attachment.thumbnails.detect { |t| t.filename =~ /_thumb/ }
0
@@ -21,7 +21,7 @@ class ImageScienceTest < Test::Unit::TestCase
0
       
0
       # test geometry string
0
       assert_equal 31, geo.width
0
- assert_equal 40, geo.height
0
+ assert_equal 41, geo.height
0
     end
0
   else
0
     def test_flunk
...
10
11
12
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
15
16
...
10
11
12
 
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
0
@@ -10,7 +10,28 @@ require 'action_controller/test_process'
0
 
0
 config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
0
 ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
0
-ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite'])
0
+
0
+db_adapter = ENV['DB']
0
+
0
+# no db passed, try one of these fine config-free DBs before bombing.
0
+db_adapter ||=
0
+ begin
0
+ require 'rubygems'
0
+ require 'sqlite'
0
+ 'sqlite'
0
+ rescue MissingSourceFile
0
+ begin
0
+ require 'sqlite3'
0
+ 'sqlite3'
0
+ rescue MissingSourceFile
0
+ end
0
+ end
0
+
0
+if db_adapter.nil?
0
+ raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
0
+end
0
+
0
+ActiveRecord::Base.establish_connection(config[db_adapter])
0
 
0
 load(File.dirname(__FILE__) + "/schema.rb")
0
 

Comments

    No one has commented yet.