2011-04-11 09:35:08 +01:00
|
|
|
class Product < ActiveRecord::Base
|
2011-04-11 18:05:40 +01:00
|
|
|
NEVER_BUILT = 2
|
|
|
|
BUILD_COMPLETED = 0
|
|
|
|
BUILD_FAILED = 1
|
|
|
|
|
2011-04-14 18:04:32 +01:00
|
|
|
ATTRS_TO_CLONE = [ 'build_path', 'build', 'build', 'counter', 'ks', 'menu', 'tar' ]
|
|
|
|
|
2011-04-28 11:20:04 +01:00
|
|
|
validates :name, :presence => true, :uniqueness => {:scope => :platform_id}
|
2011-04-11 09:35:08 +01:00
|
|
|
validates :platform_id, :presence => true
|
|
|
|
validates :build_status, :inclusion => { :in => [ NEVER_BUILT, BUILD_COMPLETED, BUILD_FAILED ] }
|
|
|
|
|
|
|
|
belongs_to :platform
|
|
|
|
|
2011-04-14 08:23:08 +01:00
|
|
|
has_attached_file :tar
|
|
|
|
validates_attachment_content_type :tar, :content_type => ["application/gnutar", "application/x-compressed", "application/x-gzip"], :message => I18n.t('layout.products.invalid_content_type')
|
|
|
|
|
|
|
|
after_validation :merge_tar_errors
|
|
|
|
|
2011-04-11 17:55:52 +01:00
|
|
|
scope :recent, order("name ASC")
|
2011-04-14 08:23:08 +01:00
|
|
|
|
2011-04-14 11:28:24 +01:00
|
|
|
before_save :destroy_tar?
|
|
|
|
|
|
|
|
def delete_tar
|
|
|
|
@delete_tar ||= "0"
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_tar=(value)
|
|
|
|
@delete_tar = value
|
|
|
|
end
|
|
|
|
|
2011-04-14 18:04:32 +01:00
|
|
|
def can_clone?
|
2011-04-15 10:23:12 +01:00
|
|
|
is_template
|
2011-04-14 18:04:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_build?
|
2011-04-15 10:23:12 +01:00
|
|
|
!is_template
|
2011-04-14 18:04:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def clone_from!(template)
|
2011-04-15 10:23:12 +01:00
|
|
|
raise "Only templates can be cloned" unless template.can_clone?
|
2011-04-14 18:04:32 +01:00
|
|
|
attrs = ATTRS_TO_CLONE.inject({}) {|result, attr|
|
|
|
|
result[attr] = template.send(attr)
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
self.attributes = attrs
|
|
|
|
end
|
|
|
|
|
2011-04-28 15:32:11 +01:00
|
|
|
def cron_command
|
|
|
|
self.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def cron_tab
|
2011-04-28 15:36:19 +01:00
|
|
|
@cron_tab ||= self[:cron_tab].present? ? self[:cron_tab] : "*\t*\t*\t*\t*"
|
2011-04-28 15:32:11 +01:00
|
|
|
end
|
|
|
|
|
2011-04-14 08:23:08 +01:00
|
|
|
protected
|
|
|
|
|
2011-04-14 11:28:24 +01:00
|
|
|
def destroy_tar?
|
|
|
|
self.tar.clear if @delete_tar == "1"
|
|
|
|
end
|
|
|
|
|
2011-04-14 08:23:08 +01:00
|
|
|
def merge_tar_errors
|
|
|
|
errors[:tar] += errors[:tar_content_type]
|
|
|
|
errors[:tar_content_type] = []
|
|
|
|
end
|
2011-04-14 11:28:24 +01:00
|
|
|
|
|
|
|
|
2011-04-11 09:35:08 +01:00
|
|
|
end
|