Module: Paperclip::Upfile

Included in:
File
Defined in:
lib/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_type ⇒ Object

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



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

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

#original_filename ⇒ Object

Returns the file's normal name.



19
20
21
# File 'lib/paperclip/upfile.rb', line 19

def original_filename
  File.basename(self.path)
end

#size ⇒ Object

Returns the size of the file.



24
25
26
# File 'lib/paperclip/upfile.rb', line 24

def size
  File.size(self)
end