Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

48 lines (39 sloc) 1.827 kB
module Uploads
MAX_FILE_SIZE = 25.megabytes
FILE_TYPES = {
document: ['application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.oasis.opendocument.text', 'text/plain'],
spreadsheet: ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.oasis.opendocument.spreadsheet', 'text/csv'],
powerpoint: ['application/vnd.ms-powerpoint'], # application/vnd.openxmlformats-officedocument.presentationml.presentation
pdf: ['application/pdf'],
jpg: ['image/jpeg'],
gif: ['image/gif'],
png: ['image/png'],
zip: ['application/zip']
}.freeze
def self.included(klass)
klass.class_eval do
scope :images, -> { where("upload_content_type REGEXP '^image\/'") }
scope :files, -> { where("upload_content_type NOT REGEXP '^image\/'") }
validates_attachment_presence :upload,
message: "can't be blank"
validates_attachment_content_type :upload,
content_type: FILE_TYPES.values.flatten,
message: 'is not a valid content type'
validates_attachment_size :upload,
less_than: MAX_FILE_SIZE,
message: 'must be less than :max megabytes'
def upload_basename
upload_file_name.gsub(/#{File.extname(upload_file_name)}$/, "")
end
def upload_extension
File.extname(upload_file_name).gsub(/^\.+/, "")
end
def image?
!(upload_content_type =~ /^image.*/).nil?
end
def file_type_icon
FILE_TYPES.select { |k,v| v.include?(upload_content_type) }.first.first
end
end
end
end
Jump to Line
Something went wrong with that request. Please try again.