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

42 lines
1.0 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
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}"
end
def filename
2019-04-03 17:33:43 +02:00
"#{export_type}-#{id}_#{created_at.strftime('%d%m%Y')}.xlsx"
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)
else
raise NoMethodError, "Unknown export service for #{category}/#{export_type}"
end
end
end