Module: Technoweenie::AttachmentFu::ActMethods

Defined in:
lib/technoweenie/attachment_fu.rb

Instance Method Summary collapse

Instance Method Details

#has_attachment(options = {}) ⇒ Object

  • :keep_profile By default image EXIF data will be stripped to minimize image size. For small thumbnails this proivides important savings. Picture quality is not affected. Set to false if you want to keep the image profile as is. ImageScience will allways keep EXIF data.

Examples:

has_attachment :max_size => 1.kilobyte
has_attachment :size => 1.megabyte..2.megabytes
has_attachment :content_type => 'application/pdf'
has_attachment :content_type => ['application/pdf', 'application/msword', 'text/plain']
has_attachment :content_type => :image, :resize_to => [50,50]
has_attachment :content_type => ['application/pdf', :image], :resize_to => 'x50'
has_attachment :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }
has_attachment :storage => :file_system, :path_prefix => 'public/files'
has_attachment :storage => :file_system, :path_prefix => 'public/files',
  :content_type => :image, :resize_to => [50,50]
has_attachment :storage => :file_system, :path_prefix => 'public/files',
  :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }
has_attachment :storage => :s3


41
42
43
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
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
105
# File 'lib/technoweenie/attachment_fu.rb', line 41

def has_attachment(options = {})
  # this allows you to redefine the acts' options for each subclass, however
  options[:min_size]         ||= 1
  options[:max_size]         ||= 1.megabyte
  options[:size]             ||= (options[:min_size]..options[:max_size])
  options[:thumbnails]       ||= {}
  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
  self.attachment_options = 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]}"
    m.belongs_to :parent, :class_name => "::#{base_class}" unless options[:thumbnails].empty?
  end

  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 Object, Exception
      raise unless load_related_exception?($!)

      processors.shift
      retry
    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 Object, Exception
      raise unless load_related_exception?($!)

      puts "Problems loading #{options[:processor]}Processor: #{$!}"
    end
  end unless parent_options[:processor] # Don't let child override processor
end