rosa-build/app/models/platform.rb

44 lines
1.2 KiB
Ruby
Raw Normal View History

2011-03-09 17:38:21 +00:00
class Platform < ActiveRecord::Base
belongs_to :parent, :class_name => 'Platform', :foreign_key => 'parent_platform_id'
has_many :repositories, :dependent => :destroy
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
before_create :create_directory
2011-03-09 17:38:21 +00:00
2011-03-11 09:39:34 +00:00
def path
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
def build_path(dir)
File.join(APP_CONFIG['root_path'], dir)
end
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?
FileUtils.mv(build_path(unixname_was), buildpath(unixname))
end
2011-03-09 17:38:21 +00:00
end
end