2014-03-26 11:09:39 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: teamers
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# comment :string(255)
|
|
|
|
# rank :integer not null
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2020-03-18 03:38:17 +00:00
|
|
|
# team_id :integer not null
|
|
|
|
# user_id :integer not null
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_teamers_on_team_id (team_id)
|
|
|
|
# index_teamers_on_user_id (user_id)
|
2014-03-26 11:09:39 +00:00
|
|
|
#
|
|
|
|
|
2014-03-23 00:22:25 +00:00
|
|
|
class Teamer < ActiveRecord::Base
|
|
|
|
include Extra
|
|
|
|
|
|
|
|
RANK_REMOVED = -2
|
|
|
|
RANK_JOINER = -1
|
|
|
|
RANK_MEMBER = 0
|
|
|
|
RANK_DEPUTEE = 1
|
|
|
|
RANK_LEADER = 2
|
|
|
|
|
2020-03-16 23:57:47 +00:00
|
|
|
#attr_protected :id, :created_at, :updated_at, :version
|
2014-03-23 00:22:25 +00:00
|
|
|
|
|
|
|
validates_length_of :comment, :in => 0..15, :allow_blank => true
|
|
|
|
validates_uniqueness_of :user_id, :scope => [:team_id, :rank]
|
|
|
|
validates_presence_of :user, :team
|
|
|
|
#validate_on_create:validate_team
|
|
|
|
#validate_on_create:validate_contests
|
|
|
|
validate :validate_team
|
|
|
|
|
2019-06-03 19:38:24 +00:00
|
|
|
scope :basic, -> { includes(:user).order("rank DESC, created_at ASC") }
|
|
|
|
scope :past, -> { where("teamers.rank = ?", RANK_REMOVED) }
|
|
|
|
scope :joining,-> { where("teamers.rank = ?", RANK_JOINER) }
|
|
|
|
scope :present, -> { where("teamers.rank >= ?", RANK_JOINER) }
|
|
|
|
scope :active, -> { where("teamers.rank >= ?", RANK_MEMBER) }
|
|
|
|
scope :leaders, -> { where("teamers.rank >= ?", RANK_DEPUTEE) }
|
|
|
|
scope :of_team, -> (team) { where("teamers.team_id" => team.id) }
|
2020-03-18 04:41:13 +00:00
|
|
|
scope :active_teams, -> { includes(:team).where(teams: {active: true}) }
|
2019-06-03 19:38:24 +00:00
|
|
|
scope :unique_by_team, -> { group("user_id, team_id") }
|
|
|
|
scope :ordered, -> { order("rank DESC, created_at ASC") }
|
|
|
|
scope :historic, -> (user, time) {
|
|
|
|
where("user_id = ? AND created_at < ? AND ((updated_at > ? AND rank = ?) OR rank >= ?)",
|
|
|
|
user.id, time.utc, time.utc, RANK_REMOVED, RANK_MEMBER) }
|
|
|
|
|
2020-03-26 02:26:30 +00:00
|
|
|
belongs_to :user, :optional => true
|
|
|
|
belongs_to :team, :optional => true
|
2019-06-03 19:38:24 +00:00
|
|
|
has_many :other_teamers, -> { where("teamers.id != ?", object_id) }, :through => :user, :source => :teamers
|
|
|
|
has_many :contesters, :through => :team
|
|
|
|
|
|
|
|
before_create :init_variables
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
user.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def ranks
|
|
|
|
{RANK_JOINER => "Joining", RANK_MEMBER => "Member", RANK_DEPUTEE => "Deputee", RANK_LEADER => "Leader"}
|
|
|
|
end
|
2020-03-23 02:10:31 +00:00
|
|
|
|
|
|
|
def rank_s
|
|
|
|
ranks[rank]
|
|
|
|
end
|
2019-06-03 19:38:24 +00:00
|
|
|
|
|
|
|
def validate_team
|
|
|
|
if user.teamers.of_team(team).present.count > 0
|
|
|
|
errors.add :team, I18n.t(:teams_join_twice)
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
2019-06-03 19:38:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def validate_contests
|
|
|
|
# TODO
|
|
|
|
end
|
|
|
|
|
|
|
|
def init_variables
|
|
|
|
self.rank = RANK_JOINER unless self.rank
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
user.update_attribute :team, nil if user.team == team
|
|
|
|
if rank == Teamer::RANK_JOINER
|
|
|
|
super
|
|
|
|
else
|
|
|
|
update_attribute :rank, Teamer::RANK_REMOVED
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|
2019-06-03 19:38:24 +00:00
|
|
|
end
|
2014-03-23 00:22:25 +00:00
|
|
|
|
2019-06-03 19:38:24 +00:00
|
|
|
def can_create? cuser, params
|
|
|
|
cuser and Verification.contain params, [:user_id, :team_id]
|
|
|
|
end
|
2014-03-23 00:22:25 +00:00
|
|
|
|
2019-06-03 19:38:24 +00:00
|
|
|
def can_update? cuser
|
|
|
|
cuser and cuser.admin?
|
|
|
|
end
|
2014-03-23 00:22:25 +00:00
|
|
|
|
2019-06-03 19:38:24 +00:00
|
|
|
def can_destroy? cuser
|
|
|
|
cuser and (user == cuser or team.is_leader? cuser or cuser.admin?)
|
|
|
|
end
|
2020-03-18 03:38:17 +00:00
|
|
|
|
|
|
|
def self.params(params, cuser)
|
|
|
|
params.require(:teamer).permit(:comment, :rank, :team_id, :user_id)
|
|
|
|
end
|
2014-03-23 00:22:25 +00:00
|
|
|
end
|