rosa-build/app/models/project.rb

48 lines
1.2 KiB
Ruby
Raw Normal View History

2011-03-09 17:38:21 +00:00
class Project < ActiveRecord::Base
belongs_to :repository
2011-03-10 12:35:23 +00:00
2011-03-10 13:38:50 +00:00
validate :name, :uniqueness => true, :presence => true, :allow_nil => false, :allow_blank => false
validate :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
include Project::HasRepository
before_create :create_directory, :create_git_repo
2011-03-10 13:38:50 +00:00
# Redefining a method from Project::HasRepository module to reflect current situation
def git_repo_path
@git_repo_path ||= File.join(path, unixname + ".git")
2011-03-10 13:38:50 +00:00
end
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
p = Project.new
p.name = name
p.unixname = unixname
return p
end
2011-03-09 17:38:21 +00:00
protected
def build_path(dir)
File.join(repository.path, dir)
end
#TODO: Spec me
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
def create_git_repo
Git::Repository.create(git_repo_path)
end
2011-03-09 17:38:21 +00:00
end