module JamRuby
  class ArtifactUpdate < ActiveRecord::Base

    DEFAULT_ENVIRONMENT = 'public'

    PRODUCTS = ['JamClient/Win32', 'JamClient/MacOSX']
    
    self.primary_key = 'id'
    attr_accessible :version, :uri, :sha1, :environment, :product


    mount_uploader :uri, ArtifactUploader

    validates :version, :presence => true
    validates :uri, :presence => true
    validates :sha1, :presence => true
    validates :size, :presence => true
    validates :environment, :presence => true
    validates :product, :inclusion => {:in => PRODUCTS}

    before_validation do
      if uri.present? && uri_changed?
        self.size = uri.file.size
        self.sha1 = Digest::MD5.hexdigest(File.read(uri.current_path))
      end
    end

  end
end
