2014-03-26 11:09:39 +00:00
# == Schema Information
#
# Table name: teams
#
# id :integer not null, primary key
# name :string(255)
# irc :string(255)
# web :string(255)
# tag :string(255)
# country :string(255)
# comment :string(255)
# created_at :datetime
# updated_at :datetime
# logo :string(255)
# founder_id :integer
# active :boolean default(TRUE), not null
# recruiting :string(255)
#
2014-03-23 00:22:25 +00:00
class Team < ActiveRecord :: Base
include Extra
LOGOS = " logos "
STATUS_INACTIVE = 0
STATUS_ACTIVE = 1
attr_protected :id , :active , :founder_id , :created_at , :updated_at
validates_presence_of :name , :tag
validates_length_of :name , :tag , :in = > 2 .. 20
validates_length_of :irc , :maximum = > 20 , :allow_blank = > true
validates_length_of :web , :maximum = > 50 , :allow_blank = > true
2015-08-15 19:26:57 +00:00
validates_format_of :country , :with = > / \ A[A-Z]{2} \ z / , :allow_blank = > true
2014-03-23 00:22:25 +00:00
validates_length_of [ :comment , :recruiting ] , :in = > 0 .. 75 , :allow_blank = > true
2019-06-03 19:38:24 +00:00
scope :with_teamers_num , - > ( num ) {
select ( " teams.*, COUNT(T.id) AS teamers_num " ) .
joins ( " LEFT JOIN teamers T ON T.team_id = teams.id AND T.rank >= #{ Teamer :: RANK_MEMBER } " ) .
group ( " teams.id " ) .
having ( " teamers_num >= ? " , num ) }
scope :non_empty_teams , - > { joins ( :teamers ) . where ( " teamers.rank >= #{ Teamer :: RANK_MEMBER } " ) . distinct }
scope :with_teamers , - > { includes ( :teamers ) }
scope :active , - > { where ( active : true ) }
scope :inactive , - > { where ( active : false ) }
scope :ordered , - > { order ( " name " ) }
scope :recruiting , - > { Â where ( " recruiting IS NOT NULL AND recruiting != '' " ) }
2014-03-23 00:22:25 +00:00
belongs_to :founder , :class_name = > " User "
2019-06-03 19:38:24 +00:00
has_many :active_teamers , - > { where ( " rank >= ? " , Teamer :: RANK_MEMBER ) }
2019-09-24 19:55:34 +00:00
has_many :teamers , :dependent = > :destroy , :counter_cache = > true
2019-06-03 19:38:24 +00:00
has_many :leaders , - > { where ( " rank = ? " , Teamer :: RANK_LEADER ) } , :class_name = > " Teamer "
2014-03-23 00:22:25 +00:00
has_many :contesters , :dependent = > :destroy
2019-06-03 19:38:24 +00:00
has_many :contests , - > { where ( " contesters.active " , true ) } , :through = > :contesters
2014-03-23 00:22:25 +00:00
has_many :received_messages , :class_name = > " Message " , :as = > " recipient "
has_many :sent_messages , :class_name = > " Message " , :as = > " sender "
has_many :matches , :through = > :contesters
2019-06-03 19:38:24 +00:00
has_many :matches_finished , - > { where ( " (score1 != 0 OR score2 != 0) " ) } ,
:through = > :contesters , :source = > :matches
has_many :matches_won , - > { where ( " ((score1 > score2 AND contester1_id = contesters.id) OR (score2 > score1 AND contester2_id = contesters.id)) AND (score1 != 0 OR score2 != 0) " ) } ,
:through = > :contesters , :source = > :matches
has_many :matches_lost , - > { where ( " ((score1 < score2 AND contester1_id = contesters.id) OR (score2 < score1 AND contester2_id = contesters.id)) AND (score1 != 0 OR score2 != 0) " ) } ,
:through = > :contesters , :source = > :matches
has_many :matches_draw , - > { where ( " (score1 = score2 AND score1 > 0) AND (score1 != 0 OR score2 != 0) " ) } ,
:through = > :contesters , :source = > :matches
2014-03-23 00:22:25 +00:00
mount_uploader :logo , TeamUploader
before_create :init_variables
after_create :add_leader
def to_s
name
end
def leaders_s
leaders . join ( " , " )
end
def init_variables
self . active = true
self . recruiting = nil
end
def add_leader
teamer = Teamer . new
teamer . user = founder
teamer . team = self
teamer . rank = Teamer :: RANK_LEADER
teamer . save
founder . update_attribute :team_id , self . id
end
2014-04-27 02:11:47 +00:00
def self . search ( search )
2019-06-03 19:38:24 +00:00
search ? where ( " LOWER(name) LIKE LOWER(?) " , " % #{ search } % " ) : all
2014-04-27 02:11:47 +00:00
end
2014-03-23 00:22:25 +00:00
def destroy
if matches . count > 0
update_attribute :active , false
teamers . update_all [ " rank = ? " , Teamer :: RANK_REMOVED ]
else
super
end
end
def recover
update_attribute :active , true
end
def is_leader? user
teamers . leaders . exists? ( :user_id = > user . id )
end
def can_create? cuser
cuser and ! cuser . banned? ( Ban :: TYPE_MUTE )
end
def can_update? cuser
2015-06-21 21:43:16 +00:00
cuser and ( is_leader? cuser or cuser . admin? )
2014-03-23 00:22:25 +00:00
end
def can_destroy? cuser
cuser and cuser . admin?
end
end