2012-01-30 20:39:34 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
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
|
|
|
|
2012-02-20 23:13:05 +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-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
|
2012-02-20 23:13:05 +00:00
|
|
|
has_many :platforms, :through => :targets, :source => :target, :source_type => 'Platform', :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
|
2011-11-24 21:46:19 +00:00
|
|
|
validates :uname, :presence => true, :uniqueness => {:case_sensitive => false}, :format => { :with => /^[a-z0-9_]+$/ }
|
2011-10-26 21:57:51 +01:00
|
|
|
validate { errors.add(:uname, :taken) if User.where('uname LIKE ?', uname).present? }
|
|
|
|
|
2012-03-08 00:32:12 +00:00
|
|
|
scope :search_order, order("CHAR_LENGTH(uname) ASC")
|
2012-03-30 23:45:05 +01:00
|
|
|
scope :without, lambda {|l| where("groups.id NOT IN (?)", Array(l))}
|
2012-03-28 00:58:03 +01:00
|
|
|
scope :search, lambda {|q| where("uname ILIKE ?", "%#{q.strip}%")}
|
|
|
|
scope :opened, where('1=1')
|
2012-03-06 15:53:04 +00:00
|
|
|
scope :by_owner, lambda {|owner| where(:owner_id => owner.id)}
|
|
|
|
scope :by_admin, lambda {|admin| joins(:relations).where(:'relations.role' => 'admin', :'relations.target_id' => admin.id, :'relations.target_type' => 'User')}
|
2012-02-27 09:03:28 +00:00
|
|
|
|
2012-03-21 13:52:35 +00:00
|
|
|
attr_accessible :description
|
2012-03-02 10:39:48 +00:00
|
|
|
attr_readonly :own_projects_count
|
2011-10-26 21:57:51 +01:00
|
|
|
|
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
|
|
|
|
|
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)
|
|
|
|
(by_owner(user) | by_admin(user)).collect { |el| [el.name, el.id] }
|
|
|
|
end
|
|
|
|
|
2012-03-02 10:39:48 +00:00
|
|
|
def name
|
|
|
|
uname
|
|
|
|
end
|
|
|
|
|
2011-11-30 15:27:19 +00:00
|
|
|
protected
|
2012-02-20 23:13:05 +00:00
|
|
|
|
|
|
|
def add_owner_to_members
|
|
|
|
Relation.create_with_role(self.owner, self, 'admin')
|
|
|
|
# members << self.owner if !members.exists?(:id => self.owner.id)
|
|
|
|
end
|
2011-10-13 16:55:03 +01:00
|
|
|
end
|