2011-03-11 16:08:41 +00:00
|
|
|
class Repository < ActiveRecord::Base
|
|
|
|
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-11-01 22:33:20 +00:00
|
|
|
has_many :project_to_repositories, :validate => true, :dependent => :destroy
|
2011-10-13 16:55:03 +01:00
|
|
|
|
2011-11-19 11:41:11 +00:00
|
|
|
has_many :relations, :as => :target, :dependent => :destroy
|
2011-11-01 22:33:20 +00:00
|
|
|
has_many :objects, :as => :target, :class_name => 'Relation', :dependent => :destroy
|
2011-10-13 16:55:03 +01:00
|
|
|
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-11-28 15:41:42 +00:00
|
|
|
validates :description, :uniqueness => {:scope => :platform_id}, :presence => true
|
2012-01-28 01:04:11 +00:00
|
|
|
validates :name, :uniqueness => {:scope => :platform_id, :case_sensitive => false}, :presence => true, :format => { :with => /^[a-z0-9_\-]+$/ }
|
2011-11-03 12:49:14 +00:00
|
|
|
# validates :platform_id, :presence => true # if you uncomment this platform clone will not work
|
2011-03-11 16:08:41 +00:00
|
|
|
|
2011-03-31 01:25:18 +01:00
|
|
|
scope :recent, order("name ASC")
|
|
|
|
|
2011-11-03 20:41:06 +00:00
|
|
|
before_create :xml_rpc_create, :unless => lambda {Thread.current[:skip]}
|
2011-10-27 16:20:49 +01:00
|
|
|
before_destroy :xml_rpc_destroy
|
2011-12-01 14:20:24 +00:00
|
|
|
after_create :add_admin_relations
|
2011-03-11 16:08:41 +00:00
|
|
|
|
2011-11-28 15:41:42 +00:00
|
|
|
attr_accessible :description, :name #, :platform_id
|
2011-03-11 16:08:41 +00:00
|
|
|
|
2011-11-03 00:32:01 +00:00
|
|
|
def full_clone(attrs) # owner
|
|
|
|
clone.tap do |c| # dup
|
|
|
|
c.attributes = attrs
|
|
|
|
c.updated_at = nil; c.created_at = nil # :id = nil
|
|
|
|
c.projects = projects
|
|
|
|
end
|
2011-03-11 17:38:28 +00:00
|
|
|
end
|
|
|
|
|
2011-11-24 21:46:19 +00:00
|
|
|
include Modules::Models::Owner
|
2011-03-11 16:08:41 +00:00
|
|
|
|
2011-12-02 15:25:59 +00:00
|
|
|
class << self
|
|
|
|
def build_stub(platform)
|
|
|
|
rep = Repository.new
|
|
|
|
rep.platform = platform
|
|
|
|
rep
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-24 21:46:19 +00:00
|
|
|
protected
|
2011-10-18 16:00:06 +01:00
|
|
|
|
2011-04-07 10:27:50 +01:00
|
|
|
def xml_rpc_create
|
2011-11-28 15:41:42 +00:00
|
|
|
result = BuildServer.create_repo name, platform.name
|
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}."
|
2012-01-11 18:15:35 +00:00
|
|
|
end
|
2011-04-07 10:27:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def xml_rpc_destroy
|
2011-11-28 15:41:42 +00:00
|
|
|
result = BuildServer.delete_repo name, platform.name
|
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-12-01 09:29:04 +00:00
|
|
|
|
|
|
|
def add_admin_relations
|
|
|
|
platform.relations.where(:role => 'admin').each do |rel|
|
2011-12-08 14:03:56 +00:00
|
|
|
if !relations.exists?(:role => 'admin', :object_type => rel.object_type, :object_id => rel.object_id) && rel.object != owner
|
|
|
|
r = relations.build(:role => 'admin', :object_type => rel.object_type)
|
|
|
|
r.object_id = rel.object_id
|
|
|
|
r.save
|
|
|
|
end
|
2011-12-01 09:29:04 +00:00
|
|
|
end
|
|
|
|
end
|
2011-03-11 16:08:41 +00:00
|
|
|
end
|