2012-10-03 17:38:42 +01:00
|
|
|
class Group < Avatar
|
2014-01-21 04:51:49 +00:00
|
|
|
belongs_to :owner, class_name: 'User'
|
2011-10-26 21:57:51 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
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
|
2011-10-13 16:55:03 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
has_many :members, through: :actors, source: :actor, source_type: 'User', autosave: true
|
|
|
|
has_many :projects, through: :targets, source: :target, source_type: 'Project', autosave: true
|
2012-02-20 23:13:05 +00:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
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
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
validates :owner, presence: true
|
|
|
|
validates :uname, presence: true, uniqueness: {case_sensitive: false}, format: {with: /\A[a-z0-9_]+\z/}, reserved_name: true
|
2012-05-02 10:18:07 +01:00
|
|
|
validate { errors.add(:uname, :taken) if User.by_uname(uname).present? }
|
2011-10-26 21:57:51 +01:00
|
|
|
|
2012-03-28 00:58:03 +01:00
|
|
|
scope :opened, where('1=1')
|
2014-01-21 04:51:49 +00:00
|
|
|
scope :by_owner, lambda {|owner| where(owner_id: owner.id)}
|
2012-04-26 02:38:33 +01:00
|
|
|
scope :by_admin, lambda {|admin| joins(:actors).where(:'relations.role' => 'admin', :'relations.actor_id' => admin.id, :'relations.actor_type' => 'User')}
|
2013-05-07 15:56:13 +01:00
|
|
|
scope :by_admin_and_writer, lambda {|actor| joins(:actors).where(:'relations.role' => ['admin', 'writer'], :'relations.actor_id' => actor.id, :'relations.actor_type' => 'User')}
|
2012-02-27 09:03:28 +00:00
|
|
|
|
2012-05-02 10:18:07 +01:00
|
|
|
attr_accessible :uname, :description
|
|
|
|
attr_readonly :uname
|
2011-10-26 21:57:51 +01:00
|
|
|
|
2014-01-21 04:51:49 +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
|
|
|
|
|
2012-05-02 10:18:07 +01:00
|
|
|
include Modules::Models::ActsLikeMember
|
2011-11-24 21:46:19 +00:00
|
|
|
include Modules::Models::PersonalRepository
|
2012-02-20 23:13:05 +00:00
|
|
|
# include Modules::Models::Owner
|
2011-11-30 15:27:19 +00:00
|
|
|
|
2012-02-27 09:03:28 +00:00
|
|
|
def self.can_own_project(user)
|
2013-05-07 15:56:13 +01:00
|
|
|
(by_owner(user) | by_admin_and_writer(user))
|
2012-02-27 09:03:28 +00:00
|
|
|
end
|
|
|
|
|
2012-03-02 10:39:48 +00:00
|
|
|
def name
|
|
|
|
uname
|
|
|
|
end
|
|
|
|
|
2012-10-16 14:03:18 +01:00
|
|
|
def add_member(member, role = 'admin')
|
2012-10-17 14:46:16 +01:00
|
|
|
Relation.add_member(member, self, role, :actors)
|
2012-10-16 14:03:18 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_member(member)
|
|
|
|
Relation.remove_member(member, self)
|
|
|
|
end
|
|
|
|
|
2012-12-27 18:27:50 +00:00
|
|
|
def system?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2013-04-10 11:23:27 +01:00
|
|
|
def fullname
|
|
|
|
return description.present? ? "#{uname} (#{description})" : uname
|
|
|
|
end
|
|
|
|
|
2011-11-30 15:27:19 +00:00
|
|
|
protected
|
2012-02-20 23:13:05 +00:00
|
|
|
|
|
|
|
def add_owner_to_members
|
2014-01-21 04:51:49 +00:00
|
|
|
Relation.create_with_role(self.owner, self, 'admin') # members << self.owner if !members.exists?(id: self.owner.id)
|
2012-02-20 23:13:05 +00:00
|
|
|
end
|
2011-10-13 16:55:03 +01:00
|
|
|
end
|