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)
|
|
|
|
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)
|
2016-07-27 17:00:06 +02:00
|
|
|
when 'users'
|
|
|
|
UsersExportWorker.perform_async(self.id)
|
2016-07-27 11:28:54 +02:00
|
|
|
else
|
|
|
|
raise NoMethodError, "Unknown export service for #{category}/#{export_type}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|