2014-03-26 11:09:39 +00:00
# == Schema Information
#
# Table name: teams
#
2020-03-18 03:38:17 +00:00
# id :integer not null, primary key
2020-03-31 17:27:34 +00:00
# active :boolean default(TRUE), not null
2020-03-18 03:38:17 +00:00
# comment :string(255)
# country :string(255)
# irc :string(255)
# logo :string(255)
# name :string(255)
# recruiting :string(255)
# tag :string(255)
# teamers_count :integer
# web :string(255)
# created_at :datetime
# updated_at :datetime
# founder_id :integer
#
# Indexes
#
# index_teams_on_founder_id (founder_id)
2014-03-26 11:09:39 +00:00
#
2014-03-23 00:22:25 +00:00
class Team < ActiveRecord :: Base
include Extra
LOGOS = " logos "
STATUS_INACTIVE = 0
STATUS_ACTIVE = 1
2020-03-16 23:57:47 +00:00
#attr_protected :id, :active, :founder_id, :created_at, :updated_at
2014-03-23 00:22:25 +00:00
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 != '' " ) }
2020-03-28 20:39:41 +00:00
scope :not_in_contest , - > ( contest ) { joins ( :contests ) . where . not ( 'contests.id' : contest . id ) }
2014-03-23 00:22:25 +00:00
2020-03-26 02:26:30 +00:00
belongs_to :founder , :class_name = > " User " , :optional = > true
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
2019-08-24 18:17:14 +00:00
User . where ( team_id : self . id ) . each do | user |
user . update_attribute ( :team_id , nil )
end
2014-03-23 00:22:25 +00:00
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
2020-03-18 03:38:17 +00:00
def self . params ( params , cuser )
2020-03-25 02:30:37 +00:00
params . permit ( :team ) . except ( :id , :active , :founder_id , :created_at , :updated_at ) . permit!
2020-03-18 03:38:17 +00:00
end
2014-03-23 00:22:25 +00:00
end