2011-03-09 17:38:21 +00:00
|
|
|
class Project < ActiveRecord::Base
|
2011-03-11 16:08:41 +00:00
|
|
|
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
|
|
|
|
2011-03-10 12:38:42 +00:00
|
|
|
include Project::HasRepository
|
|
|
|
|
2011-03-15 11:24:45 +00:00
|
|
|
before_create :create_directory, :create_git_repo
|
2011-03-11 10:25:50 +00:00
|
|
|
|
2011-03-10 13:38:50 +00:00
|
|
|
# Redefining a method from Project::HasRepository module to reflect current situation
|
|
|
|
def git_repo_path
|
2011-03-11 16:08:41 +00:00
|
|
|
@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
|
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
|
|
|
|
p = Project.new
|
|
|
|
p.name = name
|
|
|
|
p.unixname = unixname
|
|
|
|
return p
|
|
|
|
end
|
|
|
|
|
2011-03-09 17:38:21 +00:00
|
|
|
protected
|
|
|
|
|
2011-03-11 10:25:50 +00:00
|
|
|
def build_path(dir)
|
2011-03-11 16:08:41 +00:00
|
|
|
File.join(repository.path, dir)
|
2011-03-11 10:25:50 +00:00
|
|
|
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
|
2011-03-15 11:24:45 +00:00
|
|
|
|
|
|
|
def create_git_repo
|
|
|
|
Git::Repository.create(git_repo_path)
|
|
|
|
end
|
2011-03-09 17:38:21 +00:00
|
|
|
end
|