mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Show correct opponent in the games list
This commit is contained in:
parent
b122c4d44f
commit
9b47a85d8f
@ -37,7 +37,8 @@ defmodule Chess.Store.Game do
|
||||
def for_user(query, user) do
|
||||
query
|
||||
|> where([game], user_id: ^user.id)
|
||||
|> preload(:opponent)
|
||||
|> or_where([game], opponent_id: ^user.id)
|
||||
|> preload([:user, :opponent])
|
||||
end
|
||||
|
||||
def ordered(query) do
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
<%= for game <- @games do %>
|
||||
<tr>
|
||||
<td>
|
||||
<%= link "Game with #{game.opponent.username}", to: game_path(@conn, :show, game), class: "btn btn-default btn-xs" %>
|
||||
<%= link "Game with #{opponent(@conn, game).username}", to: game_path(@conn, :show, game), class: "btn btn-default btn-xs" %>
|
||||
</td>
|
||||
<td>
|
||||
Started on <%= Timex.format!(game.inserted_at, "%b %e at %I:%M %P", :strftime) %>
|
||||
|
||||
@ -1,3 +1,13 @@
|
||||
defmodule ChessWeb.GameView do
|
||||
use ChessWeb, :view
|
||||
|
||||
import ChessWeb.LayoutView, only: [current_user: 1]
|
||||
|
||||
def opponent(conn, game) do
|
||||
if current_user(conn).id == game.user_id do
|
||||
game.opponent
|
||||
else
|
||||
game.user
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -15,7 +15,7 @@ defmodule Chess.GamesTest do
|
||||
create_user("zelda", "ganonsucks")
|
||||
|
||||
session
|
||||
|> login()
|
||||
|> create_user_and_login()
|
||||
|> visit("/games")
|
||||
|> click(link("New game"))
|
||||
|> select("game[opponent_id]", option: "zelda")
|
||||
@ -32,7 +32,7 @@ defmodule Chess.GamesTest do
|
||||
create_game_for(user, opponent)
|
||||
|
||||
session
|
||||
|> login()
|
||||
|> create_user_and_login()
|
||||
|> visit("/games")
|
||||
|> click(link("New game"))
|
||||
|> select("game[opponent_id]", option: "urbosa")
|
||||
@ -44,11 +44,25 @@ defmodule Chess.GamesTest do
|
||||
|> assert_has(link("Game with urbosa"))
|
||||
end
|
||||
|
||||
test "can see games as an opponent", %{session: session} do
|
||||
opponent = create_user("urbosa", "gerudoqueen")
|
||||
|
||||
user = create_user("zelda", "ganonsucks")
|
||||
create_game_for(user, opponent)
|
||||
|
||||
session
|
||||
|> login("urbosa", "gerudoqueen")
|
||||
|
||||
session
|
||||
|> assert_has(css(".table tr", count: 1))
|
||||
|> assert_has(link("Game with zelda"))
|
||||
end
|
||||
|
||||
test "can move a piece", %{session: session} do
|
||||
create_user("zelda", "ganonsucks")
|
||||
|
||||
session
|
||||
|> login()
|
||||
|> create_user_and_login()
|
||||
|> visit("/games")
|
||||
|> click(link("New game"))
|
||||
|> select("game[opponent_id]", option: "zelda")
|
||||
@ -65,13 +79,18 @@ defmodule Chess.GamesTest do
|
||||
|> assert_has(square_containing("f4-r3", "white.pawn"))
|
||||
end
|
||||
|
||||
defp login(session) do
|
||||
defp create_user_and_login(session) do
|
||||
create_user("link", "ilovezelda")
|
||||
|
||||
session
|
||||
|> login("link", "ilovezelda")
|
||||
end
|
||||
|
||||
defp login(session, username, password) do
|
||||
session
|
||||
|> visit("/session/new")
|
||||
|> fill_in(text_field("Username"), with: "link")
|
||||
|> fill_in(text_field("Password"), with: "ilovezelda")
|
||||
|> fill_in(text_field("Username"), with: username)
|
||||
|> fill_in(text_field("Password"), with: password)
|
||||
|> click(button("Log in"))
|
||||
end
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user