Module: Technoweenie::AttachmentFu::InstanceMethods
- Defined in:
- lib/technoweenie/attachment_fu.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#content_type=(new_type) ⇒ Object
Sets the content type.
-
#copy_to_temp_file(file) ⇒ Object
Copies the given file to a randomly named Tempfile.
-
#create_or_update_thumbnail(temp_file, file_name_suffix, *size) ⇒ Object
Creates or updates the thumbnail for the current attachment.
-
#create_temp_file ⇒ Object
Stub for creating a temp file from the attachment data.
-
#filename=(new_name) ⇒ Object
Sanitizes a filename.
-
#image? ⇒ Boolean
Checks whether the attachment’s content type is an image content type.
-
#image_size ⇒ Object
Returns the width/height in a suitable format for the image_tag helper: (100x100).
-
#save_attachment? ⇒ Boolean
Returns true if the attachment data will be written to the storage system on the next save.
-
#temp_data ⇒ Object
Gets the data from the latest temp file.
-
#temp_data=(data) ⇒ Object
Writes the given data to a Tempfile and adds it to the collection of temp files.
-
#temp_path ⇒ Object
Gets the latest temp path from the collection of temp paths.
-
#temp_path=(value) ⇒ Object
Adds a new temp_path to the array.
-
#temp_paths ⇒ Object
Gets an array of the currently used temp paths.
-
#thumbnail_class ⇒ Object
Returns the class used to create new thumbnails for this attachment.
-
#thumbnail_name_for(thumbnail = nil) ⇒ Object
Gets the thumbnail name for a filename.
-
#thumbnailable? ⇒ Boolean
Returns true/false if an attachment is thumbnailable.
-
#uploaded_data ⇒ Object
nil placeholder in case this field is used in a form.
-
#uploaded_data=(file_data) ⇒ Object
This method handles the uploaded file object.
-
#with_image(&block) ⇒ Object
Allows you to work with a processed representation (RMagick, ImageScience, etc) of the attachment in a block.
-
#write_to_temp_file(data) ⇒ Object
Writes the given file to a randomly named Tempfile.
Class Method Details
.included(base) ⇒ Object
212 213 214 |
# File 'lib/technoweenie/attachment_fu.rb', line 212 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.
259 260 261 |
# File 'lib/technoweenie/attachment_fu.rb', line 259 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.
338 339 340 |
# File 'lib/technoweenie/attachment_fu.rb', line 338 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.
244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/technoweenie/attachment_fu.rb', line 244 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")) returning find_or_initialize_thumbnail(file_name_suffix) do |thumb| thumb.attributes = { :content_type => content_type, :filename => thumbnail_name_for(file_name_suffix), :temp_path => temp_file, :thumbnail_resize_options => size } callback_with_args :before_thumbnail_saved, thumb thumb.save! end end |
#create_temp_file ⇒ Object
Stub for creating a temp file from the attachment data. This should be defined in the backend module.
348 |
# File 'lib/technoweenie/attachment_fu.rb', line 348 def create_temp_file() end |
#filename=(new_name) ⇒ Object
Sanitizes a filename.
264 265 266 |
# File 'lib/technoweenie/attachment_fu.rb', line 264 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
217 218 219 |
# File 'lib/technoweenie/attachment_fu.rb', line 217 def image? self.class.image?(content_type) end |
#image_size ⇒ Object
Returns the width/height in a suitable format for the image_tag helper: (100x100)
269 270 271 |
# File 'lib/technoweenie/attachment_fu.rb', line 269 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
274 275 276 |
# File 'lib/technoweenie/attachment_fu.rb', line 274 def File.file?(temp_path.to_s) end |
#temp_data ⇒ Object
Gets the data from the latest temp file. This will read the file into memory.
328 329 330 |
# File 'lib/technoweenie/attachment_fu.rb', line 328 def temp_data ? File.read(temp_path) : nil end |
#temp_data=(data) ⇒ Object
Writes the given data to a Tempfile and adds it to the collection of temp files.
333 334 335 |
# File 'lib/technoweenie/attachment_fu.rb', line 333 def temp_data=(data) self.temp_path = write_to_temp_file data unless data.nil? end |
#temp_path ⇒ Object
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.
308 309 310 311 |
# File 'lib/technoweenie/attachment_fu.rb', line 308 def temp_path p = temp_paths.first p.respond_to?(:path) ? p.path : p.to_s end |
#temp_path=(value) ⇒ Object
Adds a new temp_path to the array. This should take a string or a Tempfile. This class makes no attempt to remove the files, so Tempfiles should be used. Tempfiles remove themselves when they go out of scope. You can also use string paths for temporary files, such as those used for uploaded files in a web server.
322 323 324 325 |
# File 'lib/technoweenie/attachment_fu.rb', line 322 def temp_path=(value) temp_paths.unshift value temp_path end |
#temp_paths ⇒ Object
Gets an array of the currently used temp paths. Defaults to a copy of #full_filename.
314 315 316 317 |
# File 'lib/technoweenie/attachment_fu.rb', line 314 def temp_paths @temp_paths ||= (new_record? || !respond_to?(:full_filename) || !File.exist?(full_filename) ? [] : [copy_to_temp_file(full_filename)]) end |
#thumbnail_class ⇒ Object
Returns the class used to create new thumbnails for this attachment.
227 228 229 |
# File 'lib/technoweenie/attachment_fu.rb', line 227 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’
232 233 234 235 236 237 238 239 240 241 |
# File 'lib/technoweenie/attachment_fu.rb', line 232 def thumbnail_name_for(thumbnail = nil) return filename if thumbnail.blank? ext = nil basename = filename.gsub /\.\w+$/ do |s| ext = s; '' end # ImageScience doesn't create gif thumbnails, only pngs ext.sub!(/gif$/, 'png') if [: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.
222 223 224 |
# File 'lib/technoweenie/attachment_fu.rb', line 222 def thumbnailable? image? && respond_to?(:parent_id) && parent_id.nil? end |
#uploaded_data ⇒ Object
nil placeholder in case this field is used in a form.
279 |
# File 'lib/technoweenie/attachment_fu.rb', line 279 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.
292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/technoweenie/attachment_fu.rb', line 292 def uploaded_data=(file_data) return nil if file_data.nil? || file_data.size == 0 self.content_type = file_data.content_type self.filename = file_data.original_filename if respond_to?(:filename) if file_data.is_a?(StringIO) file_data.rewind self.temp_data = file_data.read else self.temp_path = file_data 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
356 357 358 |
# File 'lib/technoweenie/attachment_fu.rb', line 356 def with_image(&block) self.class.with_image(temp_path, &block) end |
#write_to_temp_file(data) ⇒ Object
Writes the given file to a randomly named Tempfile.
343 344 345 |
# File 'lib/technoweenie/attachment_fu.rb', line 343 def write_to_temp_file(data) self.class.write_to_temp_file data, random_tempfile_filename end |