class Job < ApplicationRecord
	
	require "securerandom"

	belongs_to :user, :foreign_key => "user_uid", :primary_key => "uid"
	before_validation :guid
	
	validates :uuid, :user_uid, presence: true
	validates :size, numericality: { only_integer: true, greater_than: 0 }, presence: true
	validates :file_name, format: { with: /([0-9a-zA-Z]\.tab{1})/ }, presence: true
	
	default_scope { order(created_at: :desc) }

	def path
		begin
			return Rails.root.join(
				"storage",
				self.uuid,
				self.file_name)
		rescue => e
			raise e
		end
	end

  private
  def guid
    self.uuid = SecureRandom.uuid if self.new_record?
  end
	
end