Module: Technoweenie::AttachmentFu::InstanceMethods

Defined in:
lib/technoweenie/attachment_fu.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



279
280
281
# File 'lib/technoweenie/attachment_fu.rb', line 279

def self.included(base)
  base.define_callbacks *[:after_resize, :after_attachment_saved, :before_thumbnail_saved] if base.respond_to?(:define_callbacks)
end

Instance Method Details

#content_type=(new_type) ⇒ Object

Sets the content type.



334
335
336
# File 'lib/technoweenie/attachment_fu.rb', line 334

def content_type=(new_type)
  write_attribute :content_type, new_type.to_s.strip
end

#copy_to_temp_file(file) ⇒ Object

Copies the given file to a randomly named Tempfile.



412
413
414
# File 'lib/technoweenie/attachment_fu.rb', line 412

def copy_to_temp_file(file)
  self.class.copy_to_temp_file file, random_tempfile_filename
end

#create_or_update_thumbnail(temp_file, file_name_suffix, *size) ⇒ Object

Creates or updates the thumbnail for the current attachment.



318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/technoweenie/attachment_fu.rb', line 318

def create_or_update_thumbnail(temp_file, file_name_suffix, *size)
  thumbnailable? || raise(ThumbnailError.new("Can't create a thumbnail if the content type is not an image or there is no parent_id column"))
  find_or_initialize_thumbnail(file_name_suffix).tap do |thumb|
    thumb.temp_paths.unshift temp_file
    attributes = {
      :content_type =>             content_type,
      :filename =>                 thumbnail_name_for(file_name_suffix),
      :thumbnail_resize_options => size
    }
    attributes.each{ |a, v| thumb.send "#{a}=", v }
    callback_with_args :before_thumbnail_saved, thumb
    thumb.save!
  end
end

#create_temp_fileObject

Stub for creating a temp file from the attachment data. This should be defined in the backend module.



422
# File 'lib/technoweenie/attachment_fu.rb', line 422

def create_temp_file() end

#filename=(new_name) ⇒ Object

Sanitizes a filename.



339
340
341
# File 'lib/technoweenie/attachment_fu.rb', line 339

def filename=(new_name)
  write_attribute :filename, sanitize_filename(new_name)
end

#image?Boolean

Checks whether the attachment’s content type is an image content type

Returns:

  • (Boolean)


284
285
286
# File 'lib/technoweenie/attachment_fu.rb', line 284

def image?
  self.class.image?(content_type)
end

#image_sizeObject

Returns the width/height in a suitable format for the image_tag helper: (100x100)



344
345
346
# File 'lib/technoweenie/attachment_fu.rb', line 344

def image_size
  [width.to_s, height.to_s] * 'x'
end

#save_attachment?Boolean

Returns true if the attachment data will be written to the storage system on the next save

Returns:

  • (Boolean)


349
350
351
# File 'lib/technoweenie/attachment_fu.rb', line 349

def save_attachment?
  File.file?(temp_path.class == String ? temp_path : temp_path.to_filename)
end

#set_temp_data(data) ⇒ Object

Writes the given data to a Tempfile and adds it to the collection of temp files.



407
408
409
# File 'lib/technoweenie/attachment_fu.rb', line 407

def set_temp_data(data)
  temp_paths.unshift write_to_temp_file data unless data.nil?
end

#temp_dataObject

Gets the data from the latest temp file. This will read the file into memory.



402
403
404
# File 'lib/technoweenie/attachment_fu.rb', line 402

def temp_data
  save_attachment? ? File.read(temp_path) : nil
end

#temp_pathObject

Gets the latest temp path from the collection of temp paths. While working with an attachment, multiple Tempfile objects may be created for various processing purposes (resizing, for example). An array of all the tempfile objects is stored so that the Tempfile instance is held on to until it’s not needed anymore. The collection is cleared after saving the attachment.



390
391
392
393
# File 'lib/technoweenie/attachment_fu.rb', line 390

def temp_path
  p = temp_paths.first
  p.respond_to?(:path) ? p.path : p.to_s
end

#temp_pathsObject

Gets an array of the currently used temp paths. Defaults to a copy of #full_filename.



396
397
398
399
# File 'lib/technoweenie/attachment_fu.rb', line 396

def temp_paths
  @temp_paths ||= (new_record? || !respond_to?(:full_filename) || !File.exist?(full_filename) ?
    [] : [copy_to_temp_file(full_filename)])
end

#thumbnail_classObject

Returns the class used to create new thumbnails for this attachment.



294
295
296
# File 'lib/technoweenie/attachment_fu.rb', line 294

def thumbnail_class
  self.class.thumbnail_class
end

#thumbnail_name_for(thumbnail = nil) ⇒ Object

Gets the thumbnail name for a filename. ‘foo.jpg’ becomes ‘foo_thumbnail.jpg’



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/technoweenie/attachment_fu.rb', line 299

def thumbnail_name_for(thumbnail = nil)
  if thumbnail.blank?
    if filename.nil?
      return ''
    else
      return filename
    end
  end

  ext = nil
  basename = filename.gsub /\.\w+$/ do |s|
    ext = s; ''
  end
  # ImageScience doesn't create gif thumbnails, only pngs
  ext.sub!(/gif$/i, 'png') if attachment_options[:processor] == "ImageScience"
  "#{basename}_#{thumbnail}#{ext}"
end

#thumbnailable?Boolean

Returns true/false if an attachment is thumbnailable. A thumbnailable attachment has an image content type and the parent_id attribute.

Returns:

  • (Boolean)


289
290
291
# File 'lib/technoweenie/attachment_fu.rb', line 289

def thumbnailable?
  image? && respond_to?(:parent_id) && parent_id.nil?
end

#uploaded_dataObject

nil placeholder in case this field is used in a form.



354
# File 'lib/technoweenie/attachment_fu.rb', line 354

def uploaded_data() nil; end

#uploaded_data=(file_data) ⇒ Object

This method handles the uploaded file object. If you set the field name to uploaded_data, you don’t need any special code in your controller.

<% form_for :attachment, :html => { :multipart => true } do |f| -%>
  <p><%= f.file_field :uploaded_data %></p>
  <p><%= submit_tag :Save %>
<% end -%>

@attachment = Attachment.create! params[:attachment]

TODO: Allow it to work with Merb tempfiles too.



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/technoweenie/attachment_fu.rb', line 367

def uploaded_data=(file_data)
  if file_data.respond_to?(:content_type)
    return nil if file_data.size == 0
    self.content_type = file_data.content_type
    self.filename     = file_data.original_filename if respond_to?(:filename)
  else
    return nil if file_data.blank? || file_data['size'] == 0
    self.content_type = file_data['content_type']
    self.filename =  file_data['filename']
    file_data = file_data['tempfile']
  end
  if file_data.is_a?(StringIO)
    file_data.rewind
    set_temp_data file_data.read
  else
    file_data.respond_to?(:tempfile) ? self.temp_paths.unshift( file_data.tempfile.path ) : self.temp_paths.unshift( file_data.path )
  end
end

#with_image(&block) ⇒ Object

Allows you to work with a processed representation (RMagick, ImageScience, etc) of the attachment in a block.

@attachment.with_image do |img|
  self.data = img.thumbnail(100, 100).to_blob
end


430
431
432
433
434
435
436
437
# File 'lib/technoweenie/attachment_fu.rb', line 430

def with_image(&block)
  # Write out the temporary data if it is not present
  if temp_data.nil?
    self.temp_data = current_data
  end

  self.class.with_image(temp_path, &block)
end

#write_to_temp_file(data) ⇒ Object

Writes the given file to a randomly named Tempfile.



417
418
419
# File 'lib/technoweenie/attachment_fu.rb', line 417

def write_to_temp_file(data)
  self.class.write_to_temp_file data, random_tempfile_filename
end