Module: Paperclip::Upfile

Included in:
File
Defined in:
lib/dm-paperclip/upfile.rb

Overview

The Upfile module is a convenience module for adding uploaded-file-type methods to the File class. Useful for testing.

user.avatar = File.new("test/test_avatar.jpg")

Instance Method Summary collapse

Instance Method Details

#content_typeObject

Infer the MIME-type of the file from the extension.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dm-paperclip/upfile.rb', line 8

def content_type
  type = (self.path.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase
  case type
  when %r"jpe?g"                 then "image/jpeg"
  when %r"tiff?"                 then "image/tiff"
  when %r"png", "gif", "bmp"     then "image/#{type}"
  when "txt"                     then "text/plain"
  when %r"html?"                 then "text/html"
  when "csv", "xml", "css", "js" then "text/#{type}"
  else "application/x-#{type}"
  end
end

#original_filenameObject

Returns the file’s normal name.



22
23
24
# File 'lib/dm-paperclip/upfile.rb', line 22

def original_filename
  File.basename(self.path)
end

#sizeObject

Returns the size of the file.



27
28
29
# File 'lib/dm-paperclip/upfile.rb', line 27

def size
  File.size(self)
end