You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/models/competition_team.rb

73 lines
1.7 KiB

class CompetitionTeam < ApplicationRecord
6 years ago
CODE_CHARS = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z).freeze
belongs_to :user
belongs_to :competition
has_many :team_members, dependent: :destroy
6 years ago
has_many :users, through: :team_members, source: :user
5 years ago
has_many :competition_scores, dependent: :destroy
has_many :members, -> { without_teachers }, class_name: 'TeamMember'
has_many :teachers, -> { only_teachers }, class_name: 'TeamMember'
6 years ago
has_many :competition_prize_users, dependent: :destroy
6 years ago
def group_team_type?
team_type.zero?
end
def personal_team_type?
team_type == 1
end
def en_team_type
group_team_type? ? 'group' : 'personal'
end
def generate_invite_code
code = CODE_CHARS.sample(6).join
while self.class.exists?(invite_code: code)
code = CODE_CHARS.sample(6).join
end
self.invite_code = code
6 years ago
code
end
5 years ago
def teachers_info
info = ""
teachers.each do |teacher|
teacher_info = "#{teacher.user.real_name}/#{teacher.user.school_name}"
info += info == "" ? teacher_info : "#{teacher_info}"
end
info
end
5 years ago
def teachers_name
teachers.map{|teacher| teacher.user.real_name}.join(",")
end
def members_name
members.map{|member| member.user.real_name}.join(",")
end
def all_prize_approved?
!competition_prize_users.exists?(status: :pending)
end
def certificate_exists?
Util::FileManage.exists?(self, self.class.certificate_suffix)
end
def certificate_url
Util::FileManage.source_disk_file_url(self, self.class.certificate_suffix)
end
def certificate_path
Util::FileManage.source_disk_filename(self, self.class.certificate_suffix)
end
def self.certificate_suffix
'_CERT'
end
end