Module: Griddle::Upfile

Included in:
File
Defined in:
lib/griddle/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
20
# File 'lib/griddle/upfile.rb', line 8

def content_type
  type = (self.path.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase
  case type
  when %r"jp(e|g|eg)" 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 "js" then "application/js"
  when "csv", "xml", "css" then "text/#{type}"
  else "application/x-#{type}"
  end
end

#original_filenameObject

Returns the file’s normal name.



23
24
25
# File 'lib/griddle/upfile.rb', line 23

def original_filename
  File.basename(self.path)
end

#sizeObject

Returns the size of the file.



28
29
30
# File 'lib/griddle/upfile.rb', line 28

def size
  File.size(self)
end