2017-05-01 14:57:32 +00:00
|
|
|
class MatchProposal < ActiveRecord::Base
|
|
|
|
|
|
|
|
STATUS_PENDING = 0
|
|
|
|
STATUS_REVOKED = 1
|
|
|
|
STATUS_REJECTED = 2
|
2017-05-11 20:26:18 +00:00
|
|
|
STATUS_CONFIRMED = 3
|
2017-05-01 14:57:32 +00:00
|
|
|
|
|
|
|
belongs_to :match
|
|
|
|
belongs_to :team
|
2017-07-07 15:10:28 +00:00
|
|
|
has_many :confirmed_by, class_name: 'Team', uniq: true
|
2017-05-01 14:57:32 +00:00
|
|
|
attr_accessible :proposed_time, :status
|
|
|
|
|
|
|
|
validates_presence_of :match, :team, :proposed_time
|
|
|
|
|
2017-05-11 20:26:18 +00:00
|
|
|
def status_strings
|
|
|
|
{STATUS_PENDING => 'Pending',
|
|
|
|
STATUS_REVOKED => 'Revoked',
|
|
|
|
STATUS_REJECTED => 'Rejected',
|
|
|
|
STATUS_CONFIRMED => 'Confirmed'}
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_create? cuser
|
2017-07-07 15:10:28 +00:00
|
|
|
return false unless cuser && match
|
|
|
|
return true if cuser.admin?
|
|
|
|
match.can_make_proposal?(cuser)
|
2017-05-11 20:26:18 +00:00
|
|
|
end
|
|
|
|
|
2017-06-30 12:03:46 +00:00
|
|
|
def can_update? cuser, params = {}
|
|
|
|
return false unless cuser && match && match.can_make_proposal?(cuser)
|
2017-07-07 15:10:28 +00:00
|
|
|
return true if cuser.admin?
|
|
|
|
|
|
|
|
if params.key?(:status) && (status !=(new_status = params[:status].to_i))
|
|
|
|
return false if new_status == STATUS_PENDING
|
|
|
|
case this.status
|
|
|
|
when STATUS_REVOKED
|
|
|
|
return false
|
|
|
|
when STATUS_PENDING
|
|
|
|
case new_status
|
|
|
|
when STATUS_CONFIRMED, STATUS_REJECTED
|
|
|
|
return false unless this.team != cuser.team
|
|
|
|
# TODO: Use time depending on rules
|
|
|
|
return false if (STATUS_CONFIRMED.to_s == new_status) && (proposed_time < 20.minutes.from_now)
|
|
|
|
when STATUS_REVOKED
|
|
|
|
return false unless (this.team == cuser.team)
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
when STATUS_CONFIRMED, STATUS_REJECTED
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
2017-06-30 12:03:46 +00:00
|
|
|
end
|
2017-08-12 22:06:16 +00:00
|
|
|
|
2017-06-30 12:03:46 +00:00
|
|
|
true
|
2017-05-01 14:57:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_destroy?
|
|
|
|
cuser && cuser.admin?
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|