2012-01-30 20:39:34 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
2011-04-11 09:35:08 +01:00
|
|
|
class Product < ActiveRecord::Base
|
2011-11-11 19:11:27 +00:00
|
|
|
ATTRS_TO_CLONE = [ 'build_path', 'build_script', 'counter', 'ks', 'menu', 'tar', 'use_cron', 'cron_tab' ]
|
2011-04-14 18:04:32 +01:00
|
|
|
|
2011-04-11 09:35:08 +01:00
|
|
|
belongs_to :platform
|
2011-11-11 17:13:09 +00:00
|
|
|
has_many :product_build_lists, :dependent => :destroy
|
2011-04-11 09:35:08 +01:00
|
|
|
|
2011-11-24 21:46:19 +00:00
|
|
|
after_validation :merge_tar_errors
|
|
|
|
before_save :destroy_tar?
|
|
|
|
|
2011-04-14 08:23:08 +01:00
|
|
|
has_attached_file :tar
|
|
|
|
|
2012-02-29 09:41:42 +00:00
|
|
|
validates_attachment_content_type :tar, :content_type => ["application/gnutar", "application/x-compressed", "application/x-gzip", "application/x-bzip", "application/x-bzip2", "application/x-tar", "application/octet-stream"], :message => I18n.t('layout.invalid_content_type')
|
2011-11-24 21:46:19 +00:00
|
|
|
validates :name, :presence => true, :uniqueness => {:scope => :platform_id}
|
2011-04-14 08:23:08 +01:00
|
|
|
|
2011-04-11 17:55:52 +01:00
|
|
|
scope :recent, order("name ASC")
|
2011-04-14 08:23:08 +01:00
|
|
|
|
2012-05-02 10:18:07 +01:00
|
|
|
attr_accessible :name, :counter, :ks, :menu, :tar, :cron_tab, :use_cron, :description, :build_script, :delete_tar
|
2012-04-03 13:21:39 +01:00
|
|
|
attr_readonly :platform_id
|
|
|
|
|
2011-04-14 11:28:24 +01:00
|
|
|
def delete_tar
|
|
|
|
@delete_tar ||= "0"
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_tar=(value)
|
|
|
|
@delete_tar = value
|
|
|
|
end
|
|
|
|
|
2011-04-14 18:04:32 +01:00
|
|
|
def clone_from!(template)
|
|
|
|
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 16:22:25 +01:00
|
|
|
@cron_tab ||= self[:cron_tab].present? ? self[:cron_tab] : "* * * * *"
|
|
|
|
end
|
|
|
|
|
|
|
|
["minutes", "hours", "days", "months", "weekdays"].each_with_index do |meth, index|
|
|
|
|
class_eval <<-EOF
|
|
|
|
def cron_tab_#{meth}
|
|
|
|
value = cron_tab.split(/\s+/)[#{index}]
|
|
|
|
value == "*" ? [] : value.split(/\s*,*\s*/).collect{|x| x.to_i }
|
|
|
|
end
|
|
|
|
|
|
|
|
EOF
|
2011-04-28 15:32:11 +01:00
|
|
|
end
|
|
|
|
|
2012-02-21 21:27:11 +00:00
|
|
|
def full_clone(attrs = {})
|
2011-11-03 00:32:01 +00:00
|
|
|
clone.tap do |c| # dup
|
2012-02-22 20:24:29 +00:00
|
|
|
c.platform_id = nil
|
|
|
|
attrs.each {|k,v| c.send("#{k}=", v)}
|
|
|
|
c.updated_at = nil; c.created_at = nil # :id = nil
|
2011-11-03 00:32:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-14 08:23:08 +01:00
|
|
|
protected
|
|
|
|
|
2011-12-28 02:57:42 +00:00
|
|
|
def destroy_tar?
|
|
|
|
self.tar.clear if @delete_tar == "1"
|
|
|
|
end
|
2011-12-01 09:29:04 +00:00
|
|
|
|
2011-12-28 02:57:42 +00:00
|
|
|
def merge_tar_errors
|
|
|
|
errors[:tar] += errors[:tar_content_type]
|
|
|
|
errors[:tar_content_type] = []
|
|
|
|
end
|
2011-04-11 09:35:08 +01:00
|
|
|
end
|