module SimpleCSVUploder

Constants

NUM_BYTES_IN_MEGABYTE
VERSION

Attributes

file[RW]

Public Class Methods

new(params = {}) click to toggle source
Calls superclass method
# File lib/SimpleCSVUploder.rb, line 6
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/SimpleCSVUploder.rb, line 16
def upload_local
  path = "#{Rails.root}/public/uploads/csv"
  FileUtils.mkdir_p(path) unless File.exists?(path)
  FileUtils.copy(@file.tempfile, path)
end

Private Instance Methods

check_duplication_filename() click to toggle source
# File lib/SimpleCSVUploder.rb, line 28
def check_duplication_filename
  if Simpledocupload::Document.where(filename: self.filename).present?
    errors.add(:file, 'File already exists.')
  end
end
csv_file_format() click to toggle source
# File lib/SimpleCSVUploder.rb, line 34
def csv_file_format
  if self.content_type != "text/csv"
   errors.add(:file, 'File format should be only CSV.')
  end
end
file_size_under_one_mb() click to toggle source
# File lib/SimpleCSVUploder.rb, line 41
def file_size_under_one_mb
  if !@file.nil?
    if (@file.size.to_f / NUM_BYTES_IN_MEGABYTE) > 1
      errors.add(:file, 'File size cannot be over one megabyte.')
    end
  end
end
sanitize_filename(filename) click to toggle source
# File lib/SimpleCSVUploder.rb, line 24
def sanitize_filename(filename)
  return File.basename(filename)
end