0
@@ -142,6 +142,7 @@ module Technoweenie # :nodoc:
0
attachment_options[:thumbnail_class]
0
+ # Copies the given file path to a new tempfile, returning the closed tempfile.
0
def copy_to_temp_file(file, temp_base_name)
0
returning Tempfile.new(temp_base_name, Technoweenie::AttachmentFu.tempfile_path) do |tmp|
0
@@ -149,6 +150,7 @@ module Technoweenie # :nodoc:
0
+ # Writes the given data to a new tempfile, returning the closed tempfile.
0
def write_to_temp_file(data, temp_base_name)
0
returning Tempfile.new(temp_base_name, Technoweenie::AttachmentFu.tempfile_path) do |tmp|
0
@@ -163,10 +165,12 @@ module Technoweenie # :nodoc:
0
self.class.image?(content_type)
0
+ # Returns true/false if an attachment is thumbnailable. A thumbnailable attachment has an image content type and the parent_id attribute.
0
image? && respond_to?(:parent_id)
0
+ # Returns the class used to create new thumbnails for this attachment.
0
self.class.thumbnail_class
0
@@ -181,6 +185,41 @@ module Technoweenie # :nodoc:
0
"#{basename}_#{thumbnail}#{ext}"
0
+ # Creates or updates the thumbnail for the current attachment.
0
+ def create_or_update_thumbnail(temp_file, file_name_suffix, *size)
0
+ thumbnailable? || raise(ThumbnailError.new("Can't create a thumbnail if the content type is not an image or there is no parent_id column"))
0
+ returning find_or_initialize_thumbnail(file_name_suffix) do |thumb|
0
+ :content_type => content_type,
0
+ :filename => thumbnail_name_for(file_name_suffix),
0
+ :temp_path => temp_file,
0
+ :thumbnail_resize_options => size
0
+ callback_with_args :before_thumbnail_saved, thumb
0
+ # Sets the content type.
0
+ def content_type=(new_type)
0
+ write_attribute :content_type, new_type.to_s.strip
0
+ # Sanitizes a filename.
0
+ def filename=(new_name)
0
+ write_attribute :filename, sanitize_filename(new_name)
0
+ # Returns the width/height in a suitable format for the image_tag helper: (100x100)
0
+ [width.to_s, height.to_s] * 'x'
0
+ # Returns true if the attachment data will be written to the storage system on the next save
0
+ File.file?(temp_path.to_s)
0
# nil placeholder in case this field is used in a form.
0
def uploaded_data() nil; end
0
@@ -202,59 +241,52 @@ module Technoweenie # :nodoc:
0
self.temp_path = file_data.path
0
- # returns true if the attachment data will be written to the storage system on the next save
0
- File.file?(temp_path.to_s)
0
+ # Gets the latest temp path from the collection of temp paths. While working with an attachment,
0
+ # multiple Tempfile objects may be created for various processing purposes (resizing, for example).
0
+ # An array of all the tempfile objects is stored so that the Tempfile instance is held on to until
0
+ # it's not needed anymore. The collection is cleared after saving the attachment.
0
p.respond_to?(:path) ? p.path : p.to_s
0
+ # Gets an array of the currently used temp paths.
0
+ # Adds a new temp_path to the array. This should take a string or a Tempfile. This class makes no
0
+ # attempt to remove the files, so Tempfiles should be used. Tempfiles remove themselves when they go out of scope.
0
+ # You can also use string paths for temporary files, such as those used for uploaded files in a web server.
0
temp_paths.unshift value
0
+ # Gets the data from the latest temp file. This will read the file into memory.
0
save_attachment? ? File.read(temp_path) : nil
0
+ # Writes the given data to a Tempfile and adds it to the collection of temp files.
0
self.temp_path = write_to_temp_file data unless data.nil?
0
+ # Copies the given file to a randomly named Tempfile.
0
def copy_to_temp_file(file)
0
self.class.copy_to_temp_file file, random_tempfile_filename
0
+ # Writes the given file to a randomly named Tempfile.
0
def write_to_temp_file(data)
0
self.class.write_to_temp_file data, random_tempfile_filename
0
+ # Stub for creating a temp file from the attachment data. This should be defined in the backend module.
0
def create_temp_file() end
0
- # Sets the content type.
0
- def content_type=(new_type)
0
- write_attribute :content_type, new_type.to_s.strip
0
- # sanitizes a filename.
0
- def filename=(new_name)
0
- write_attribute :filename, sanitize_filename(new_name)
0
- # Returns the width/height in a suitable format for the image_tag helper: (100x100)
0
- [width.to_s, height.to_s] * 'x'
0
- # Allows you to work with an RMagick representation of the attachment in a block.
0
+ # Allows you to work with a processed representation (RMagick, ImageScience, etc) of the attachment in a block.
0
# @attachment.with_image do |img|
0
# self.data = img.thumbnail(100, 100).to_blob
0
@@ -263,23 +295,9 @@ module Technoweenie # :nodoc:
0
self.class.with_image(temp_path, &block)
0
- # Creates or updates the thumbnail for the current attachment.
0
- def create_or_update_thumbnail(temp_file, file_name_suffix, *size)
0
- thumbnailable? || raise(ThumbnailError.new("Can't create a thumbnail if the content type is not an image or there is no parent_id column"))
0
- returning find_or_initialize_thumbnail(file_name_suffix) do |thumb|
0
- :content_type => content_type,
0
- :filename => thumbnail_name_for(file_name_suffix),
0
- :temp_path => temp_file,
0
- :thumbnail_resize_options => size
0
- callback_with_args :before_thumbnail_saved, thumb
0
+ # Generates a unique filename for a Tempfile.
0
def random_tempfile_filename
0
"#{rand Time.now.to_i}#{filename || 'attachment'}"
0
@@ -297,6 +315,7 @@ module Technoweenie # :nodoc:
0
+ # before_validation callback.
0
def set_size_from_temp_path
0
self.size = File.size(temp_path) if save_attachment?
0
@@ -308,7 +327,8 @@ module Technoweenie # :nodoc:
0
errors.add attr_name, ActiveRecord::Errors.default_error_messages[:inclusion] unless enum.nil? || enum.include?(send(attr_name))
0
+ # Initializes a new thumbnail with the given suffix.
0
def find_or_initialize_thumbnail(file_name_suffix)
0
respond_to?(:parent_id) ?
0
thumbnail_class.find_or_initialize_by_thumbnail_and_parent_id(file_name_suffix.to_s, id) :
0
@@ -320,6 +340,7 @@ module Technoweenie # :nodoc:
0
@saved_attachment = save_attachment?
0
+ # Cleans up after processing. Thumbnails are created, the attachment is stored to the backend, and the temp_paths are cleared.
0
def after_process_attachment
0
if thumbnailable? && !attachment_options[:thumbnails].blank? && parent_id.nil?
0
@@ -333,6 +354,7 @@ module Technoweenie # :nodoc:
0
+ # Resizes the given processed img object with either the attachment resize options or the thumbnail resize options.
0
def resize_image_or_thumbnail!(img)
0
if (!respond_to?(:parent_id) || parent_id.nil?) && attachment_options[:resize_to] # parent image
0
resize_image(img, attachment_options[:resize_to])
Comments
No one has commented yet.