2019-04-03 17:33:43 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Export is a reference to a file asynchronously generated by the system and downloadable by the user
|
2016-07-27 11:28:54 +02:00
|
|
|
class Export < ActiveRecord::Base
|
|
|
|
require 'fileutils'
|
|
|
|
|
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
validates :category, presence: true
|
|
|
|
validates :export_type, presence: true
|
|
|
|
validates :user, presence: true
|
|
|
|
|
|
|
|
after_commit :generate_and_send_export, on: [:create]
|
|
|
|
|
|
|
|
def file
|
|
|
|
dir = "exports/#{category}/#{export_type}"
|
|
|
|
|
|
|
|
# create directories if they doesn't exists (exports & type & id)
|
2019-04-03 17:33:43 +02:00
|
|
|
FileUtils.mkdir_p dir
|
|
|
|
"#{dir}/#{filename}"
|
2016-07-27 11:28:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def filename
|
2019-04-03 17:33:43 +02:00
|
|
|
"#{export_type}-#{id}_#{created_at.strftime('%d%m%Y')}.xlsx"
|
2016-07-27 11:28:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2019-04-03 17:33:43 +02:00
|
|
|
|
2016-07-27 11:28:54 +02:00
|
|
|
def generate_and_send_export
|
|
|
|
case category
|
2019-04-03 17:33:43 +02:00
|
|
|
when 'statistics'
|
|
|
|
StatisticsExportWorker.perform_async(id)
|
|
|
|
when 'users'
|
|
|
|
UsersExportWorker.perform_async(id)
|
|
|
|
when 'availabilities'
|
|
|
|
AvailabilitiesExportWorker.perform_async(id)
|
|
|
|
else
|
|
|
|
raise NoMethodError, "Unknown export service for #{category}/#{export_type}"
|
2016-07-27 11:28:54 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|