Skip to content

Commit

Permalink
Merge branch 'dev' into dividebyzero/#294/event-picture
Browse files Browse the repository at this point in the history
  • Loading branch information
nikriek authored Jan 22, 2018
2 parents ff049f9 + edd911c commit 0c9f543
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
16 changes: 9 additions & 7 deletions app/models/match.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ def round
def has_points?
points_home.present? && points_away.present?
end

def has_scores?
score_home.present? && score_away.present?
end


def has_winner?
has_points? && points_home != points_away
end
Expand Down Expand Up @@ -99,10 +95,16 @@ def set_points(home, away)
self.points_away = away
end

def score_changed?
score_home_changed? || score_away_changed?
end

def calculate_points
return if !has_scores? || has_points?
return unless score_changed?

if score_home > score_away
if score_home.blank? || score_away.blank?
set_points(nil, nil)
elsif score_home > score_away
set_points(3, 0)
elsif score_home < score_away
set_points(0, 3)
Expand Down
7 changes: 7 additions & 0 deletions spec/factories/matches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
points_home nil
end

trait :with_home_winning do
points_away 0
points_home 3
score_away 0
score_home 3
end

trait :with_tournament do
association :event, factory: :tournament
end
Expand Down
24 changes: 24 additions & 0 deletions spec/models/match_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@
it { is_expected.not_to change { match.points_away } }
end

context 'only team home has a score' do
let(:match) { FactoryBot.build(:match, :empty_points, score_home: 1, score_away: nil) }
it { is_expected.not_to change { match.points_home } }
it { is_expected.not_to change { match.points_away } }
end

context 'only team away has a score' do
let(:match) { FactoryBot.build(:match, :empty_points, score_home: nil, score_away: 1) }
it { is_expected.not_to change { match.points_home } }
it { is_expected.not_to change { match.points_away } }
end

context 'team home has a higher score' do
let(:match) { FactoryBot.build(:match, :empty_points, score_home: 1, score_away: 0) }
it { is_expected.to change { match.points_home }.from(nil).to(3) }
Expand All @@ -61,5 +73,17 @@
it { is_expected.to change { match.points_home }.from(nil).to(1) }
it { is_expected.to change { match.points_away }.from(nil).to(1) }
end

context 'team home and team away have scores' do
let!(:match) { FactoryBot.create(:match, :with_home_winning) }

before(:each) do
match.score_home = 0
match.score_away = 1
end

it { is_expected.to change { match.points_home }.from(3).to(0) }
it { is_expected.to change { match.points_away }.from(0).to(3) }
end
end
end

0 comments on commit 0c9f543

Please sign in to comment.