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 rmagick_test issues
- Backing out 963b9e4889bc930e2191c882656b5c0930822a8d for now
nicksieger (author)
Tue Mar 04 09:56:54 -0800 2008
commit  0962d416ab5a9474c4948883f0a0e861a5201907
tree    305731cd95025720d21902165fb763fc177fbd60
parent  ee8474831d3181596220f2b970a707730b851950
...
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
 
 
 
 
 
 
99
100
101
 
102
103
104
...
116
117
118
119
120
 
 
 
 
 
 
 
 
121
122
123
 
 
 
124
125
126
...
284
285
286
287
 
 
288
289
290
...
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
72
73
74
 
 
 
 
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
92
93
94
95
96
97
 
 
98
99
100
101
...
113
114
115
 
 
116
117
118
119
120
121
122
123
124
125
 
126
127
128
129
130
131
...
289
290
291
 
292
293
294
295
296
0
@@ -45,60 +45,57 @@ module Technoweenie # :nodoc:
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
+
0
         unless options[:thumbnails].is_a?(Hash)
0
           raise ArgumentError, ":thumbnails option should be a hash: e.g. :thumbnails => { :foo => '50x50' }"
0
         end
0
-
0
+
0
+ extend ClassMethods unless (class << self; included_modules; end).include?(ClassMethods)
0
+ include InstanceMethods unless included_modules.include?(InstanceMethods)
0
+
0
+ parent_options = attachment_options || {}
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
- attr_accessor :thumbnail_resize_options
0
+ attr_accessor :thumbnail_resize_options
0
+
0
+ attachment_options[:storage] ||= (attachment_options[:file_system_path] || attachment_options[:path_prefix]) ? :file_system : :db_file
0
+ attachment_options[:storage] ||= parent_options[:storage]
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
+ 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, :class_name => attachment_options[:thumbnail_class].to_s
0
+ m.belongs_to :parent, :class_name => base_class.to_s
0
+ end
0
 
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
+ storage_mod = Technoweenie::AttachmentFu::Backends.const_get("#{options[:storage].to_s.classify}Backend")
0
+ include storage_mod unless included_modules.include?(storage_mod)
0
+
0
+ case attachment_options[:processor]
0
+ when :none, nil
0
+ processors = Technoweenie::AttachmentFu.default_processors.dup
0
+ begin
0
+ if processors.any?
0
+ attachment_options[:processor] = "#{processors.first}Processor"
0
+ processor_mod = Technoweenie::AttachmentFu::Processors.const_get(attachment_options[:processor])
0
+ include processor_mod unless included_modules.include?(processor_mod)
0
+ end
0
+ rescue LoadError, MissingSourceFile
0
+ processors.shift
0
+ retry
0
           end
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, :class_name => attachment_options[:thumbnail_class].to_s
0
- m.belongs_to :parent, :class_name => base_class.to_s
0
- end unless options[:thumbnails].empty?
0
- before_destroy :destroy_thumbnails
0
-
0
- before_validation :set_size_from_temp_path
0
- after_save :after_process_attachment
0
- after_destroy :destroy_file
0
- extend ClassMethods
0
- include InstanceMethods
0
- include Technoweenie::AttachmentFu::Backends.const_get("#{options[:storage].to_s.classify}Backend")
0
- case attachment_options[:processor]
0
- when :none
0
- when nil
0
- processors = Technoweenie::AttachmentFu.default_processors.dup
0
- begin
0
- if processors.any?
0
- attachment_options[:processor] = "#{processors.first}Processor"
0
- include Technoweenie::AttachmentFu::Processors.const_get(attachment_options[:processor])
0
- end
0
- rescue LoadError, MissingSourceFile
0
- processors.shift
0
- retry
0
- end
0
- else
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
+ else
0
+ begin
0
+ processor_mod = Technoweenie::AttachmentFu::Processors.const_get("#{attachment_options[:processor].to_s.classify}Processor")
0
+ include processor_mod unless included_modules.include?(processor_mod)
0
+ rescue LoadError, MissingSourceFile
0
+ puts "Problems loading #{options[:processor]}Processor: #{$!}"
0
           end
0
- after_validation :process_attachment
0
- end
0
+ end unless parent_options[:processor] # Don't let child override processor
0
       end
0
     end
0
 
0
@@ -116,11 +113,19 @@ module Technoweenie # :nodoc:
0
         content_types.include?(content_type)
0
       end
0
 
0
- if defined?(::ActiveSupport::Callbacks)
0
- def self.extended(base)
0
+ def self.extended(base)
0
+ base.class_inheritable_accessor :attachment_options
0
+ base.before_destroy :destroy_thumbnails
0
+ base.before_validation :set_size_from_temp_path
0
+ base.after_save :after_process_attachment
0
+ base.after_destroy :destroy_file
0
+ base.after_validation :process_attachment
0
+ if defined?(::ActiveSupport::Callbacks)
0
           base.define_callbacks :after_resize, :after_attachment_saved, :before_thumbnail_saved
0
         end
0
- else
0
+ end
0
+
0
+ unless defined?(::ActiveSupport::Callbacks)
0
         # Callback after an image has been resized.
0
         #
0
         # class Foo < ActiveRecord::Base
0
@@ -284,7 +289,8 @@ module Technoweenie # :nodoc:
0
       
0
       # Gets an array of the currently used temp paths. Defaults to a copy of #full_filename.
0
       def temp_paths
0
- @temp_paths ||= (new_record? || !File.exist?(full_filename)) ? [] : [copy_to_temp_file(full_filename)]
0
+ @temp_paths ||= (new_record? || !respond_to?(:full_filename) || !File.exist?(full_filename) ?
0
+ [] : [copy_to_temp_file(full_filename)])
0
       end
0
       
0
       # Adds a new temp_path to the array. This should take a string or a Tempfile. This class makes no

Comments

    No one has commented yet.