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.rb

71 lines
1.9 KiB

class Competition < ApplicationRecord
has_many :competition_modules, dependent: :destroy
5 years ago
has_many :unhidden_competition_modules, -> { where(hidden: false) }, class_name: 'CompetitionModule'
has_many :competition_stages, dependent: :destroy
has_many :competition_stage_sections, dependent: :destroy
has_one :current_stage_section, -> { where('end_time > NOW()') }, class_name: 'CompetitionStageSection'
5 years ago
has_many :competition_teams, dependent: :destroy
has_many :team_members, dependent: :destroy
5 years ago
has_many :competition_staffs, dependent: :destroy
has_one :teacher_staff, -> { where(category: :teacher) }, class_name: 'CompetitionStaff'
has_one :member_staff, -> { where.not(category: :teacher) }, class_name: 'CompetitionStaff'
has_many :attachments, as: :container
after_create :create_competition_modules
# 是否上架
def published?
status?
end
# 是否为个人赛
def personal?
competition_staffs.maximum(:maximum) == 1
end
# 报名是否结束
def enroll_ended?
enroll_end_time.blank? || enroll_end_time < Time.now
end
# 是否已经报名
def enrolled?(user)
team_members.exists?(user_id: user.id)
end
5 years ago
# 是否禁止教师报名
def teacher_enroll_forbidden?
teacher_staff.blank? || teacher_staff.maximum.zero?
end
# 是否禁止学生报名
def member_enroll_forbidden?
member_staff.blank? || member_staff.maximum.zero?
end
# 老师是否能多次报名
def teacher_multiple_limited?
teacher_staff.mutiple_limited?
end
# 队员是否能多次报名
def member_multiple_limited?
member_staff.mutiple_limited?
end
private
def create_competition_modules
CompetitionModule.bulk_insert(*%i[competition_id name position]) do |worker|
%w(首页 报名 通知公告 排行榜 资料下载).each_with_index do |name, index|
worker.add(competition_id: id, name: name, position: index + 1)
end
end
end
end