module UploadDocumentsTool
Constants
- NUM_BYTES_IN_MEGABYTE
- REQUIRED_VALIDATORS
- VERSION
Public Class Methods
new(params = {})
click to toggle source
Calls superclass method
# File lib/upload_documents_tool.rb, line 8 def initialize(params = {}) @file = params.delete(:file) super if @file self.filename = sanitize_filename(@file.original_filename) self.content_type = @file.content_type self.file_contents = @file.read end end
Public Instance Methods
upload_local()
click to toggle source
# File lib/upload_documents_tool.rb, line 18 def upload_local path = "#{Rails.root}/public/uploads/documents" FileUtils.mkdir_p(path) unless File.exists?(path) FileUtils.copy(@file.tempfile, path) end
Private Instance Methods
document_file_format()
click to toggle source
# File lib/upload_documents_tool.rb, line 30 def document_file_format unless ['application/pdf','application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'text/plain', 'text/csv', 'application/octet-stream'].include? self.content_type errors.add(:file, 'Invalid file format.') end end
file_size_under_one_mb()
click to toggle source
# File lib/upload_documents_tool.rb, line 41 def file_size_under_one_mb if (@file.size.to_f / NUM_BYTES_IN_MEGABYTE) > 1 errors.add(:file, 'File size cannot be over one megabyte.') end end
sanitize_filename(filename)
click to toggle source
# File lib/upload_documents_tool.rb, line 26 def sanitize_filename(filename) File.basename(filename) end