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

38 lines
942 B
Ruby
Raw Normal View History

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)
FileUtils::mkdir_p dir
"#{dir}/#{self.filename}"
end
def filename
"#{export_type}-#{self.id}_#{self.created_at.strftime('%d%m%Y')}.xlsx"
end
private
def generate_and_send_export
case category
when 'statistics'
StatisticsExportWorker.perform_async(self.id)
when 'users'
UsersExportWorker.perform_async(self.id)
2017-03-02 12:34:28 +01:00
when 'availabilities'
AvailabilitiesExportWorker.perform_async(self.id)
else
raise NoMethodError, "Unknown export service for #{category}/#{export_type}"
end
end
end