Skip to content
This repository was archived by the owner on Jan 9, 2024. It is now read-only.

Commit 93cff2e

Browse files
committed
Add #winner and #loser associations to Game
1 parent d6de4da commit 93cff2e

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

app/models/game.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ class Game < ApplicationRecord
77
has_many :ships, through: :players
88
has_many :words, dependent: :destroy
99

10+
has_one :loser, -> { where(won: false ) }, class_name: 'Player'
11+
has_one :winner, -> { where(won: true ) }, class_name: 'Player'
12+
1013
def to_param
1114
slug
1215
end

app/views/games/finished.html.erb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
</div>
1010
</div>
1111
<div class="too_late">
12-
<% if @game.players.include?(current_player) && current_player.won %>
12+
<% case current_player
13+
when @game.winner %>
1314
<h1>Game Finished, Congratulations! You WON!</h1>
1415
<p>
1516
You're the master of the <strong><%= @game.name %></strong> room
1617
where you defeated <strong><%= @opponent.nickname %></strong>.
1718
</p>
18-
<% elsif @game.players.include?(current_player) %>
19+
<% when @game.loser %>
1920
<h1>Game Finished, You LOSE!</h1>
2021
<p>
2122
Today wasn't a good day for you in the <strong><%= @game.name %></strong> room.

spec/models/game_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "Game", type: :model do
4+
subject(:game) { Game.new }
5+
6+
it { is_expected.to have_one(:loser).class_name(Player).conditions(won: false) }
7+
it { is_expected.to have_one(:winner).class_name(Player).conditions(won: true) }
8+
end

spec/requests/games_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,28 +117,28 @@
117117
zim.update!(game: game, won: false)
118118
end
119119

120-
it 'displays that game is finished' do
120+
it 'tells that the game is finished' do
121121
get "/games/#{game.to_param}"
122122
expect(response.body).to include("finished")
123123
end
124124

125-
context 'wen signed in player won the game' do
125+
context 'when the signed-in player won the game' do
126126
before :each do
127127
allow_any_instance_of(ApplicationController).to receive(:current_player).and_return(rico)
128128
end
129129

130-
it 'displays that user won the game' do
130+
it 'tells the user that he won' do
131131
get "/games/#{game.to_param}"
132132
expect(response.body).to include("WON")
133133
end
134134
end
135135

136-
context 'when signed in player lost the game' do
136+
context 'when the signed-in player lost the game' do
137137
before :each do
138138
allow_any_instance_of(ApplicationController).to receive(:current_player).and_return(zim)
139139
end
140140

141-
it 'displays that user lost the game' do
141+
it 'tells the user that he lost' do
142142
get "/games/#{game.to_param}"
143143
expect(response.body).to include("LOSE")
144144
end

0 commit comments

Comments
 (0)