module JamRuby
  class CrashDump < ActiveRecord::Base

    include JamRuby::S3ManagerMixin

    self.table_name = "crash_dumps"

    self.primary_key = 'id'

    belongs_to :user, :inverse_of => :crash_dumps, :class_name => "JamRuby::User"
    
    validates :client_type, presence: true
    validates :client_version, presence: true

    attr_accessor :user_email

    before_validation(:on => :create) do
      self.created_at ||= Time.now
      self.id = SecureRandom.uuid
      self.uri = "dumps/#{created_at.strftime('%Y-%m-%d')}/#{self.id}.zip"
    end
    
    def user_email 
      nil if user_id.nil?
      self.user.email
    end

    def sign_url(expiration_time = 3600 * 24 * 7, secure=true)
      s3_manager.sign_url(self[:ri], {:expires => expiration_time, :secure => secure})
    end
  end
end
