2011-03-09 17:38:21 +00:00
|
|
|
class Platform < ActiveRecord::Base
|
2011-03-31 00:27:14 +01:00
|
|
|
belongs_to :parent, :class_name => 'Platform', :foreign_key => 'parent_platform_id'
|
2011-03-11 16:08:41 +00:00
|
|
|
has_many :repositories, :dependent => :destroy
|
2011-04-11 09:35:08 +01:00
|
|
|
has_many :products, :dependent => :destroy
|
2011-03-10 10:45:38 +00:00
|
|
|
|
2011-03-31 00:34:13 +01:00
|
|
|
validates :name, :presence => true, :uniqueness => true
|
|
|
|
validates :unixname, :uniqueness => true, :presence => true, :format => { :with => /^[a-zA-Z0-9\-.]+$/ }, :allow_nil => false, :allow_blank => false
|
2011-03-09 17:38:21 +00:00
|
|
|
|
2011-04-07 10:10:46 +01:00
|
|
|
before_create :xml_rpc_create
|
|
|
|
before_destroy :xml_rpc_destroy
|
2011-03-09 17:38:21 +00:00
|
|
|
|
2011-04-11 09:35:08 +01:00
|
|
|
|
2011-03-11 09:39:34 +00:00
|
|
|
def path
|
2011-03-11 10:25:50 +00:00
|
|
|
build_path(unixname)
|
2011-03-11 09:39:34 +00:00
|
|
|
end
|
|
|
|
|
2011-03-11 17:38:28 +00:00
|
|
|
def clone(new_name, new_unixname)
|
|
|
|
p = Platform.new
|
|
|
|
p.name = new_name
|
|
|
|
p.unixname = new_unixname
|
|
|
|
p.parent = self
|
|
|
|
p.repositories = repositories.map(&:clone)
|
|
|
|
p.save!
|
|
|
|
return p
|
|
|
|
end
|
|
|
|
|
2011-03-17 14:47:16 +00:00
|
|
|
def name
|
|
|
|
released? ? "#{self[:name]} #{I18n.t("layout.platforms.released_suffix")}" : self[:name]
|
|
|
|
end
|
|
|
|
|
2011-03-09 17:38:21 +00:00
|
|
|
protected
|
|
|
|
|
2011-03-11 10:25:50 +00:00
|
|
|
def build_path(dir)
|
|
|
|
File.join(APP_CONFIG['root_path'], dir)
|
|
|
|
end
|
|
|
|
|
2011-04-07 10:10:46 +01:00
|
|
|
def git_path(dir)
|
|
|
|
File.join(build_path(dir), 'git')
|
|
|
|
end
|
|
|
|
|
2011-03-11 10:25:50 +00:00
|
|
|
def create_directory
|
|
|
|
exists = File.exists?(path) && File.directory?(path)
|
|
|
|
raise "Directory #{path} already exists" if exists
|
|
|
|
if new_record?
|
|
|
|
FileUtils.mkdir_p(path)
|
|
|
|
elsif unixname_changed?
|
2011-04-07 10:10:46 +01:00
|
|
|
FileUtils.mv(build_path(unixname_was), build_path(unixname))
|
2011-03-11 10:25:50 +00:00
|
|
|
end
|
2011-03-09 17:38:21 +00:00
|
|
|
end
|
2011-04-07 10:10:46 +01:00
|
|
|
|
|
|
|
def xml_rpc_create
|
|
|
|
result = BuildServer.add_platform name, build_path(unixname), [], git_path(unixname)
|
|
|
|
if result == BuildServer::SUCCESS
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
raise "Failed to create platform #{name}. Path: #{build_path(unixname)}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def xml_rpc_destroy
|
|
|
|
result = BuildServer.delete_platform name
|
|
|
|
if result == BuildServer::SUCCESS
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
raise "Failed to delete platform #{name}."
|
|
|
|
end
|
|
|
|
end
|
2011-03-09 17:38:21 +00:00
|
|
|
end
|