1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/models/export.rb

44 lines
1.1 KiB
Ruby
Raw Normal View History

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
2020-03-25 10:16:47 +01:00
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)
2019-04-03 17:33:43 +02:00
FileUtils.mkdir_p dir
"#{dir}/#{filename}"
end
def filename
"#{export_type}-#{id}_#{created_at.strftime('%d%m%Y')}.#{extension}"
end
private
2019-04-03 17:33:43 +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)
when 'accounting'
AccountingExportWorker.perform_async(id)
2019-04-03 17:33:43 +02:00
else
raise NoMethodError, "Unknown export service for #{category}/#{export_type}"
end
end
end