2012-01-30 20:39:34 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
2011-03-11 16:08:41 +00:00
|
|
|
class Repository < ActiveRecord::Base
|
|
|
|
belongs_to :platform
|
2011-10-13 23:35:16 +01:00
|
|
|
|
2012-02-22 12:35:40 +00:00
|
|
|
has_many :project_to_repositories, :dependent => :destroy, :validate => true
|
|
|
|
has_many :projects, :through => :project_to_repositories
|
2011-10-13 16:55:03 +01:00
|
|
|
|
2012-02-22 12:35:40 +00:00
|
|
|
validates :description, :presence => true
|
|
|
|
validates :name, :uniqueness => {:scope => :platform_id, :case_sensitive => false}, :presence => true, :format => {:with => /^[a-z0-9_\-]+$/}
|
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]}
|
2012-02-22 22:57:43 +00:00
|
|
|
before_destroy :xml_rpc_destroy, :unless => lambda {Thread.current[:skip]}
|
2011-03-11 16:08:41 +00:00
|
|
|
|
2012-05-02 10:18:07 +01:00
|
|
|
attr_accessible :name, :description
|
2012-04-02 16:43:59 +01:00
|
|
|
attr_readonly :name, :platform_id
|
2011-03-11 16:08:41 +00:00
|
|
|
|
2012-02-22 20:24:29 +00:00
|
|
|
def base_clone(attrs = {})
|
2011-11-03 00:32:01 +00:00
|
|
|
clone.tap do |c| # dup
|
2012-02-22 20:24:29 +00:00
|
|
|
c.platform_id = nil
|
|
|
|
attrs.each {|k,v| c.send("#{k}=", v)}
|
|
|
|
c.updated_at = nil; c.created_at = nil # :id = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def clone_relations(from)
|
|
|
|
with_skip do
|
|
|
|
from.projects.find_each {|p| self.projects << p}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def full_clone(attrs = {})
|
|
|
|
base_clone(attrs).tap do |c|
|
|
|
|
with_skip {c.save} and c.delay.clone_relations(self)
|
2011-11-03 00:32:01 +00:00
|
|
|
end
|
2011-03-11 17:38:28 +00:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
2012-02-20 23:13:05 +00:00
|
|
|
def xml_rpc_create
|
|
|
|
result = BuildServer.create_repo name, platform.name
|
|
|
|
if result == BuildServer::SUCCESS
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
raise "Failed to create repository #{name} inside platform #{platform.name} with code #{result}."
|
2011-04-07 10:27:50 +01:00
|
|
|
end
|
2012-02-20 23:13:05 +00:00
|
|
|
end
|
2011-12-01 09:29:04 +00:00
|
|
|
|
2012-02-20 23:13:05 +00:00
|
|
|
def xml_rpc_destroy
|
|
|
|
result = BuildServer.delete_repo name, platform.name
|
|
|
|
if result == BuildServer::SUCCESS
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
raise "Failed to delete repository #{name} inside platform #{platform.name} with code #{result}."
|
2011-12-01 09:29:04 +00:00
|
|
|
end
|
2012-02-20 23:13:05 +00:00
|
|
|
end
|
2011-03-11 16:08:41 +00:00
|
|
|
end
|