rosa-build/app/models/group.rb

48 lines
1.8 KiB
Ruby
Raw Normal View History

2012-01-30 20:39:34 +00:00
# -*- encoding : utf-8 -*-
class Group < Avatar
belongs_to :owner, :class_name => 'User'
has_many :relations, :as => :actor, :dependent => :destroy, :dependent => :destroy
has_many :actors, :as => :target, :class_name => 'Relation', :dependent => :destroy
has_many :targets, :as => :actor, :class_name => 'Relation', :dependent => :destroy
has_many :members, :through => :actors, :source => :actor, :source_type => 'User', :autosave => true
has_many :projects, :through => :targets, :source => :target, :source_type => 'Project', :autosave => true
has_many :own_projects, :as => :owner, :class_name => 'Project', :dependent => :destroy
has_many :own_platforms, :as => :owner, :class_name => 'Platform', :dependent => :destroy
2011-10-16 21:39:45 +01:00
2012-03-02 10:39:48 +00:00
validates :owner, :presence => true
validates :uname, :presence => true, :uniqueness => {:case_sensitive => false}, :format => {:with => /^[a-z0-9_]+$/}, :reserved_name => true
validate { errors.add(:uname, :taken) if User.by_uname(uname).present? }
scope :opened, where('1=1')
scope :by_owner, lambda {|owner| where(:owner_id => owner.id)}
scope :by_admin, lambda {|admin| joins(:actors).where(:'relations.role' => 'admin', :'relations.actor_id' => admin.id, :'relations.actor_type' => 'User')}
attr_accessible :uname, :description
attr_readonly :uname
2012-03-22 11:47:56 +00:00
delegate :email, :to => :owner
2011-10-18 16:00:06 +01:00
2011-11-30 15:27:19 +00:00
after_create :add_owner_to_members
include Modules::Models::ActsLikeMember
include Modules::Models::PersonalRepository
# include Modules::Models::Owner
2011-11-30 15:27:19 +00:00
def self.can_own_project(user)
(by_owner(user) | by_admin(user))
end
2012-03-02 10:39:48 +00:00
def name
uname
end
2011-11-30 15:27:19 +00:00
protected
def add_owner_to_members
Relation.create_with_role(self.owner, self, 'admin') # members << self.owner if !members.exists?(:id => self.owner.id)
end
end