2019-09-24 17:42:50 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'file_size_validator'
|
|
|
|
|
|
|
|
# An Import is a file uploaded by an user that provides some data to the database.
|
|
|
|
# Currently, this is used to import some users from a CSV file
|
2020-03-25 10:16:47 +01:00
|
|
|
class Import < ApplicationRecord
|
2019-09-24 17:42:50 +02:00
|
|
|
mount_uploader :attachment, ImportUploader
|
|
|
|
|
2019-09-25 16:37:42 +02:00
|
|
|
belongs_to :user
|
2019-09-24 17:42:50 +02:00
|
|
|
|
|
|
|
validates :attachment, file_size: { maximum: Rails.application.secrets.max_import_size&.to_i || 5.megabytes.to_i }
|
2019-09-26 17:05:51 +02:00
|
|
|
validates :attachment, file_mime_type: { content_type: %w[text/csv text/comma-separated-values application/vnd.ms-excel] }
|
2019-09-25 16:37:42 +02:00
|
|
|
|
|
|
|
after_commit :proceed_import, on: [:create]
|
|
|
|
|
2019-09-26 17:05:51 +02:00
|
|
|
def results_hash
|
|
|
|
YAML.safe_load(results, [Symbol]) if results
|
|
|
|
end
|
|
|
|
|
2019-09-25 16:37:42 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def proceed_import
|
|
|
|
case category
|
|
|
|
when 'members'
|
|
|
|
MembersImportWorker.perform_async(id)
|
|
|
|
else
|
|
|
|
raise NoMethodError, "Unknown import service for #{category}"
|
|
|
|
end
|
|
|
|
end
|
2019-09-24 17:42:50 +02:00
|
|
|
end
|