1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-15 00:24:09 +01:00
fab-manager/app/models/export.rb
2020-03-25 12:35:09 +01:00

44 lines
1.1 KiB
Ruby

# frozen_string_literal: true
# Export is a reference to a file asynchronously generated by the system and downloadable by the user
class Export < ApplicationRecord
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)
FileUtils.mkdir_p dir
"#{dir}/#{filename}"
end
def filename
"#{export_type}-#{id}_#{created_at.strftime('%d%m%Y')}.#{extension}"
end
private
def generate_and_send_export
case category
when 'statistics'
StatisticsExportWorker.perform_async(id)
when 'users'
UsersExportWorker.perform_async(id)
when 'availabilities'
AvailabilitiesExportWorker.perform_async(id)
when 'accounting'
AccountingExportWorker.perform_async(id)
else
raise NoMethodError, "Unknown export service for #{category}/#{export_type}"
end
end
end