Sha256: 9f46638ed211ae5db265f4efc190798cf1b7a9bb50b9a296db5fbd3985f321f7

Contents?: true

Size: 904 Bytes

Versions: 2

Compression:

Stored size: 904 Bytes

Contents

require "csv_uploader_widget/version"

module CsvUploaderWidget

  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

  def upload_local
    path = "#{Rails.root}/public/uploads/csv"
    FileUtils.mkdir_p(path) unless File.exists?(path)
    FileUtils.copy(@file.tempfile, path)
  end

  private

  def sanitize_filename(filename)
    return File.basename(filename)
  end

  def csv_file_format
    if self.content_type != "text/csv"
      errors.add(:file, 'File format should be only CSV.')
    end
  end

  NUM_BYTES_IN_MEGABYTE = 1048576
  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

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
csv_uploader_widget-0.1.1 lib/csv_uploader_widget.rb
csv_uploader_widget-0.1.0 lib/csv_uploader_widget.rb