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
has_attachment :storage_key => 'store',
                  :backends => { 's3' => { :storage => :s3, :path_prefix => 'foo', :max_size => 5.kilobyte, :default => true },
                                 'local1' => { :storage => :file_system, :path_prefix => 'data/public' } }


104
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/technoweenie/attachment_fu.rb', line 104

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]        ||= :private
  options[:cloudfront]       ||= false
  options[:store_name]       ||= :default
  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)

  attr_accessor :thumbnail_resize_options

  parent_options = attachment_options || {}

  self.attachment_options = options
  # doing these shenanigans so that #attachment_options is available to processors and backends


  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] = case attachment_options[:storage]
      when :s3 then table_name
      when :cloud_files then table_name
      when :mogile_fs then table_name
      else File.join("public", table_name)
    end
  end
  attachment_options[:path_prefix]   = attachment_options[:path_prefix][1..-1] if options[:path_prefix].first == '/'

  association_options = { :foreign_key => 'parent_id' }
  if attachment_options[:association_options]
    association_options.merge!(attachment_options[:association_options])
  end
  with_options(association_options) 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

  self.attachment_backends ||= {}
  storage_klass_name = case options[:storage]
    when :mogile_fs
      "MogileFS"
    else
      options[:storage].to_s.classify
  end

  storage_klass = Technoweenie::AttachmentFu::Backends.const_get("#{storage_klass_name}Backend")

  self.attachment_backends[attachment_options[:store_name]] = {:klass => storage_klass, :options => attachment_options}
  storage_klass.included_in_base(self)

  # support syntax-sugar of "a = Attachment.new ; a.s3.authenticated_s3_url" for accessing store-specific stuff
  self.class_eval "def #{attachment_options[:store_name]}; get_storage_delegator(:#{attachment_options[:store_name]}); end"

  case attachment_options[:processor]
  when :none, nil
    processors = Technoweenie::AttachmentFu.default_processors.dup
    begin
      if processors.any?
        attachment_options[:processor] = processors.first
        processor_mod = Technoweenie::AttachmentFu::Processors.const_get("#{attachment_options[:processor].to_s.classify}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

#setup_attachment_fu(extra_opts = {}, config_filename = nil) ⇒ Object

helper method for has_attachment, for if you want to set up stuff from a yaml file



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/technoweenie/attachment_fu.rb', line 197

def setup_attachment_fu(extra_opts = {}, config_filename = nil)
  config_file ||= Rails.root.join("config/attachments.yml").to_s
  raise "No attachment_fu configuration found, tried #{config_file}" unless File.exist?(config_file)

  att_opts = YAML.load(ERB.new(File.read(config_file)).result)[Rails.env]
  raise "No attachment_fu configuration found for environment #{Rails.env}" unless att_opts

  arr = att_opts[self.name.tableize] || att_opts[:default]

  raise "No attachment_fu configuration found for table #{self.name.tableize}" unless arr
  arr = [arr] if arr.is_a?(Hash) # both flavors!
  arr.each do |val|
    options = val.symbolize_keys.merge(extra_opts)

    options[:thumbnails] = options[:thumbnails].symbolize_keys if options[:thumbnails]
    [:store_name, :storage].each { |k|
      options[k] = options.delete(k).to_sym if options[k]
    }

    has_attachment options
  end
end