Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

move sides to constants in competitor model

  • Loading branch information...
commit ff8b07d84e23154a8b4268f5abf2495a25e9dd14 1 parent 5a9807c
@aserafin aserafin authored
Showing with 19 additions and 16 deletions.
  1. +3 −0  app/models/competitor.rb
  2. +16 −16 app/models/match.rb
View
3  app/models/competitor.rb
@@ -1,4 +1,7 @@
class Competitor < ActiveRecord::Base
+ HOME = 'home'
+ AWAY = 'away'
+
belongs_to :match
belongs_to :player
belongs_to :team
View
32 app/models/match.rb
@@ -5,8 +5,8 @@ class Match < ActiveRecord::Base
state_machine :state, initial: :pending do
- after_transition on: :finished, do: :after_finished
- after_transition on: :confirmed, do: :update_ratings
+ after_transition on: :finished, do: :send_confirmation_email
+ after_transition on: :confirmed, do: :after_confirmation
event :finish do
transition pending: :finished
@@ -17,30 +17,30 @@ class Match < ActiveRecord::Base
end
end
- def after_finished
- set_winners_and_loosers
- send_confirmation_email
+ def after_confirmation
+ set_winners
+ update_ratings
end
- def set_winners_and_loosers
+ def send_confirmation_email
+ competitors.each do |competitor|
+ MatchMailer.confirmation_request(competitor.player, self).deliver unless competitor.player.id.eql?(self.finished_by)
+ end
+ end
+
+ def set_winners
case score.result
- when 1 then self.winners = competitors.where(side: :home).pluck(:player_id)
+ when 1 then self.winners = competitors.where(side: Competitor::HOME).pluck(:player_id)
when 0.5 then self.winners = []
- when 0 then self.winners = competitors.where(side: :away).pluck(:player_id)
+ when 0 then self.winners = competitors.where(side: Competitor::AWAY).pluck(:player_id)
end
self.save
end
- def send_confirmation_email
- competitors.each do |competitor|
- MatchMailer.confirmation_request(competitor.player, self).deliver unless competitor.player.id.eql?(self.finished_by)
- end
- end
-
def update_ratings
- home_player = competitors.where(side: :home).first.player
- away_player = competitors.where(side: :away).first.player
+ home_player = competitors.where(side: Competitor::HOME).first.player
+ away_player = competitors.where(side: Competitor::AWAY).first.player
home_player.ratings.create value: tournament.rating.compute(score.result, home_player, away_player), rating_type: tournament.rating.class.to_s
away_player.ratings.create value: tournament.rating.compute(1 - score.result, away_player, home_player), rating_type: tournament.rating.class.to_s
Please sign in to comment.
Something went wrong with that request. Please try again.