2011-03-11 16:08:41 +00:00
|
|
|
class Repository < ActiveRecord::Base
|
2011-10-23 22:39:44 +01:00
|
|
|
relationable :as => :target
|
|
|
|
|
2011-03-11 16:08:41 +00:00
|
|
|
belongs_to :platform
|
2011-10-17 15:20:35 +01:00
|
|
|
belongs_to :owner, :polymorphic => true
|
2011-10-13 23:35:16 +01:00
|
|
|
|
|
|
|
has_many :projects, :through => :project_to_repositories #, :dependent => :destroy
|
2011-10-19 14:14:53 +01:00
|
|
|
has_many :project_to_repositories, :validate => true
|
2011-10-13 16:55:03 +01:00
|
|
|
|
|
|
|
has_many :objects, :as => :target, :class_name => 'Relation'
|
|
|
|
has_many :members, :through => :objects, :source => :object, :source_type => 'User'
|
|
|
|
has_many :groups, :through => :objects, :source => :object, :source_type => 'Group'
|
2011-03-11 16:08:41 +00:00
|
|
|
|
2011-10-27 22:24:38 +01:00
|
|
|
validates :name, :uniqueness => {:scope => :platform_id}, :presence => true
|
|
|
|
validates :unixname, :uniqueness => {:scope => :platform_id}, :presence => true, :format => { :with => /^[a-zA-Z0-9\-.]+$/ }
|
2011-10-17 15:20:35 +01:00
|
|
|
validates :platform_id, :presence => true
|
2011-03-11 16:08:41 +00:00
|
|
|
|
2011-03-31 01:25:18 +01:00
|
|
|
scope :recent, order("name ASC")
|
|
|
|
|
2011-10-31 00:11:49 +00:00
|
|
|
after_create :make_owner_rel
|
|
|
|
before_save :check_owner_rel
|
2011-10-27 14:04:03 +01:00
|
|
|
#before_save :create_directory
|
|
|
|
#after_destroy :remove_directory
|
2011-10-27 16:20:49 +01:00
|
|
|
before_create :xml_rpc_create
|
|
|
|
before_destroy :xml_rpc_destroy
|
2011-03-11 16:08:41 +00:00
|
|
|
|
2011-10-28 22:03:30 +01:00
|
|
|
attr_accessible :name, :unixname, :platform_id
|
2011-10-27 14:04:03 +01:00
|
|
|
|
2011-10-28 12:16:32 +01:00
|
|
|
# def path
|
|
|
|
# build_path(unixname)
|
|
|
|
# end
|
2011-03-11 16:08:41 +00:00
|
|
|
|
2011-03-11 17:38:28 +00:00
|
|
|
def clone
|
|
|
|
r = Repository.new
|
|
|
|
r.name = name
|
|
|
|
r.unixname = unixname
|
|
|
|
r.projects = projects.map(&:clone)
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
|
2011-03-11 16:08:41 +00:00
|
|
|
protected
|
|
|
|
|
2011-10-28 12:16:32 +01:00
|
|
|
# def build_path(dir)
|
|
|
|
# File.join(platform.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)
|
|
|
|
# %w(release updates).each { |subrep| FileUtils.mkdir_p(path + subrep) }
|
|
|
|
# elsif unixname_changed?
|
|
|
|
# FileUtils.mv(build_path(unixname_was), buildpath(unixname))
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def remove_directory
|
|
|
|
# exists = File.exists?(path) && File.directory?(path)
|
|
|
|
# raise "Directory #{path} didn't exists" unless exists
|
|
|
|
# FileUtils.rm_rf(path)
|
|
|
|
# end
|
2011-10-18 16:00:06 +01:00
|
|
|
|
2011-04-07 10:27:50 +01:00
|
|
|
def xml_rpc_create
|
2011-04-14 12:24:41 +01:00
|
|
|
result = BuildServer.create_repo unixname, platform.unixname
|
2011-04-07 10:27:50 +01:00
|
|
|
if result == BuildServer::SUCCESS
|
|
|
|
return true
|
|
|
|
else
|
2011-10-27 23:42:56 +01:00
|
|
|
raise "Failed to create repository #{name} inside platform #{platform.name} with code #{result}."
|
2011-04-07 10:27:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def xml_rpc_destroy
|
2011-04-14 12:24:41 +01:00
|
|
|
result = BuildServer.delete_repo unixname, platform.unixname
|
2011-04-07 10:27:50 +01:00
|
|
|
if result == BuildServer::SUCCESS
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
raise "Failed to delete repository #{name} inside platform #{platform.name}."
|
|
|
|
end
|
|
|
|
end
|
2011-03-11 16:08:41 +00:00
|
|
|
|
2011-10-31 00:11:49 +00:00
|
|
|
def make_owner_rel
|
|
|
|
add_owner owner
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_owner_rel
|
|
|
|
if !new_record? and owner_id_changed?
|
|
|
|
remove_owner owner_type_was.classify.find(owner_id_was) if owner_type_was
|
|
|
|
add_owner owner if owner
|
2011-10-29 16:56:01 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-11 16:08:41 +00:00
|
|
|
end
|