2011-10-13 16:55:03 +01:00
|
|
|
class Group < ActiveRecord::Base
|
|
|
|
belongs_to :owner, :class_name => 'User'
|
2011-10-26 21:57:51 +01:00
|
|
|
|
2011-10-18 16:00:06 +01:00
|
|
|
has_many :own_projects, :as => :owner, :class_name => 'Project'
|
2011-10-13 16:55:03 +01:00
|
|
|
|
2011-11-19 11:41:11 +00:00
|
|
|
has_many :relations, :as => :object, :dependent => :destroy
|
2011-10-13 16:55:03 +01:00
|
|
|
has_many :objects, :as => :target, :class_name => 'Relation'
|
|
|
|
has_many :targets, :as => :object, :class_name => 'Relation'
|
2011-10-26 21:57:51 +01:00
|
|
|
has_many :roles, :through => :targets
|
2011-10-13 16:55:03 +01:00
|
|
|
|
2011-10-16 21:39:45 +01:00
|
|
|
has_many :members, :through => :objects, :source => :object, :source_type => 'User', :autosave => true
|
2011-10-13 16:55:03 +01:00
|
|
|
has_many :projects, :through => :targets, :source => :target, :source_type => 'Project', :autosave => true
|
|
|
|
has_many :platforms, :through => :targets, :source => :target, :source_type => 'Platform', :autosave => true
|
|
|
|
has_many :repositories, :through => :targets, :source => :target, :source_type => 'Repository', :autosave => true
|
2011-10-16 21:39:45 +01:00
|
|
|
|
2011-11-15 21:58:27 +00:00
|
|
|
validates :name, :owner, :presence => true
|
|
|
|
validates :uname, :presence => true, :uniqueness => {:case_sensitive => false}, :format => { :with => /^[a-z0-9_]+$/ }, :allow_nil => false, :allow_blank => false
|
2011-10-26 21:57:51 +01:00
|
|
|
validate { errors.add(:uname, :taken) if User.where('uname LIKE ?', uname).present? }
|
|
|
|
|
|
|
|
attr_readonly :uname
|
|
|
|
|
|
|
|
delegate :ssh_key, :to => :owner
|
2011-10-18 16:00:06 +01:00
|
|
|
|
2011-10-27 14:04:03 +01:00
|
|
|
include PersonalRepository
|
|
|
|
|
2011-10-31 01:02:12 +00:00
|
|
|
after_create :make_owner_rel
|
|
|
|
before_save :check_owner_rel
|
2011-10-18 16:00:06 +01:00
|
|
|
|
2011-10-28 14:10:29 +01:00
|
|
|
protected
|
|
|
|
|
2011-11-19 12:11:44 +00:00
|
|
|
def make_owner_rel
|
|
|
|
r = relations.build :object_id => owner.id, :object_type => 'User', :role => 'admin'
|
|
|
|
r.save
|
|
|
|
end
|
2011-10-31 01:02:12 +00:00
|
|
|
|
2011-11-19 12:11:44 +00:00
|
|
|
def check_owner_rel
|
|
|
|
if !new_record? and owner_id_changed?
|
|
|
|
relations.by_object(owner).delete_all if owner_type_was
|
|
|
|
make_owner_rel if owner
|
2011-10-31 01:02:12 +00:00
|
|
|
end
|
2011-11-19 12:11:44 +00:00
|
|
|
end
|
2011-10-31 01:02:12 +00:00
|
|
|
|
2011-10-13 16:55:03 +01:00
|
|
|
end
|