Skip to content

Commit

Permalink
- Fixing rmagick_test issues
Browse files Browse the repository at this point in the history
- Backing out 963b9e4 for now
  • Loading branch information
nicksieger committed Mar 4, 2008
1 parent ee84748 commit 0962d41
Showing 1 changed file with 55 additions and 49 deletions.
104 changes: 55 additions & 49 deletions lib/technoweenie/attachment_fu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,60 +45,57 @@ def has_attachment(options = {})
options[:thumbnail_class] ||= self
options[:s3_access] ||= :public_read
options[:content_type] = [options[:content_type]].flatten.collect! { |t| t == :image ? Technoweenie::AttachmentFu.content_types : t }.flatten unless options[:content_type].nil?

unless options[:thumbnails].is_a?(Hash)
raise ArgumentError, ":thumbnails option should be a hash: e.g. :thumbnails => { :foo => '50x50' }"
end


extend ClassMethods unless (class << self; included_modules; end).include?(ClassMethods)
include InstanceMethods unless included_modules.include?(InstanceMethods)

parent_options = attachment_options || {}
# doing these shenanigans so that #attachment_options is available to processors and backends
class_inheritable_accessor :attachment_options
self.attachment_options = options

# only need to define these once on a class
unless included_modules.include?(InstanceMethods)
attr_accessor :thumbnail_resize_options
attr_accessor :thumbnail_resize_options

attachment_options[:storage] ||= (attachment_options[:file_system_path] || attachment_options[:path_prefix]) ? :file_system : :db_file
attachment_options[:storage] ||= parent_options[:storage]
attachment_options[:path_prefix] ||= attachment_options[:file_system_path]
if attachment_options[:path_prefix].nil?
attachment_options[:path_prefix] = attachment_options[:storage] == :s3 ? table_name : File.join("public", table_name)
end
attachment_options[:path_prefix] = attachment_options[:path_prefix][1..-1] if options[:path_prefix].first == '/'

with_options :foreign_key => 'parent_id' do |m|
m.has_many :thumbnails, :class_name => attachment_options[:thumbnail_class].to_s
m.belongs_to :parent, :class_name => base_class.to_s
end

attachment_options[:storage] ||= (attachment_options[:file_system_path] || attachment_options[:path_prefix]) ? :file_system : :db_file
attachment_options[:path_prefix] ||= attachment_options[:file_system_path]
if attachment_options[:path_prefix].nil?
attachment_options[:path_prefix] = attachment_options[:storage] == :s3 ? table_name : File.join("public", table_name)
storage_mod = Technoweenie::AttachmentFu::Backends.const_get("#{options[:storage].to_s.classify}Backend")
include storage_mod unless included_modules.include?(storage_mod)

case attachment_options[:processor]
when :none, nil
processors = Technoweenie::AttachmentFu.default_processors.dup
begin
if processors.any?
attachment_options[:processor] = "#{processors.first}Processor"
processor_mod = Technoweenie::AttachmentFu::Processors.const_get(attachment_options[:processor])
include processor_mod unless included_modules.include?(processor_mod)
end
rescue LoadError, MissingSourceFile
processors.shift
retry
end
attachment_options[:path_prefix] = attachment_options[:path_prefix][1..-1] if options[:path_prefix].first == '/'

with_options :foreign_key => 'parent_id' do |m|
m.has_many :thumbnails, :class_name => attachment_options[:thumbnail_class].to_s
m.belongs_to :parent, :class_name => base_class.to_s
end unless options[:thumbnails].empty?
before_destroy :destroy_thumbnails

before_validation :set_size_from_temp_path
after_save :after_process_attachment
after_destroy :destroy_file
extend ClassMethods
include InstanceMethods
include Technoweenie::AttachmentFu::Backends.const_get("#{options[:storage].to_s.classify}Backend")
case attachment_options[:processor]
when :none
when nil
processors = Technoweenie::AttachmentFu.default_processors.dup
begin
if processors.any?
attachment_options[:processor] = "#{processors.first}Processor"
include Technoweenie::AttachmentFu::Processors.const_get(attachment_options[:processor])
end
rescue LoadError, MissingSourceFile
processors.shift
retry
end
else
begin
include Technoweenie::AttachmentFu::Processors.const_get("#{options[:processor].to_s.classify}Processor")
rescue LoadError, MissingSourceFile
puts "Problems loading #{options[:processor]}Processor: #{$!}"
end
else
begin
processor_mod = Technoweenie::AttachmentFu::Processors.const_get("#{attachment_options[:processor].to_s.classify}Processor")
include processor_mod unless included_modules.include?(processor_mod)
rescue LoadError, MissingSourceFile
puts "Problems loading #{options[:processor]}Processor: #{$!}"
end
after_validation :process_attachment
end
end unless parent_options[:processor] # Don't let child override processor
end
end

Expand All @@ -116,11 +113,19 @@ def image?(content_type)
content_types.include?(content_type)
end

if defined?(::ActiveSupport::Callbacks)
def self.extended(base)
def self.extended(base)
base.class_inheritable_accessor :attachment_options
base.before_destroy :destroy_thumbnails
base.before_validation :set_size_from_temp_path
base.after_save :after_process_attachment
base.after_destroy :destroy_file
base.after_validation :process_attachment
if defined?(::ActiveSupport::Callbacks)
base.define_callbacks :after_resize, :after_attachment_saved, :before_thumbnail_saved
end
else
end

unless defined?(::ActiveSupport::Callbacks)
# Callback after an image has been resized.
#
# class Foo < ActiveRecord::Base
Expand Down Expand Up @@ -284,7 +289,8 @@ def temp_path

# Gets an array of the currently used temp paths. Defaults to a copy of #full_filename.
def temp_paths
@temp_paths ||= (new_record? || !File.exist?(full_filename)) ? [] : [copy_to_temp_file(full_filename)]
@temp_paths ||= (new_record? || !respond_to?(:full_filename) || !File.exist?(full_filename) ?
[] : [copy_to_temp_file(full_filename)])
end

# Adds a new temp_path to the array. This should take a string or a Tempfile. This class makes no
Expand Down

0 comments on commit 0962d41

Please sign in to comment.