Skip to content

Commit

Permalink
Merge pull request #139 from notmarkmiranda/138-uncomplete-game-can-b…
Browse files Browse the repository at this point in the history
…e-edited

[138] Uncomplete game can be edited
  • Loading branch information
notmarkmiranda committed May 28, 2019
2 parents d48da43 + 7449b7d commit d3e494e
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 3 deletions.
12 changes: 12 additions & 0 deletions app/controllers/games_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ def show
@players = @game.completed? ? @game.players.in_place : @game.players.in_finishing_order
end

def edit
@game = Game.find(params[:id]).decorate
authorize @game
end

def update
@game = Game.find(params[:id]).decorate
authorize @game
@game.update(game_params)
redirect_to @game
end

def complete
@game = Game.find(params[:id])
authorize @game
Expand Down
12 changes: 12 additions & 0 deletions app/policies/game_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ def initialize(user, game)
@game = game
end

def can_edit?
user_is_admin? && game.not_completed?
end

def create?
user_is_admin?
end
Expand All @@ -18,6 +22,14 @@ def new?
user_is_admin?
end

def edit?
user_is_admin?
end

def update?
user_is_admin?
end

def uncomplete?
user_is_admin?
end
Expand Down
21 changes: 21 additions & 0 deletions app/views/games/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<div class="row justify-content-center">
<div class="col-md-4">
<div class="card border-primary">
<h4 class="card-header border-primary">Edit Game</h4>
<div class="card-body">
<%= form_for @game do |f| %>
<div class="form-group">
<%= f.label :date, 'Date' %>
<%= f.date_field :date, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :buy_in, 'Buy In Amount' %>
<%= f.number_field :buy_in, class: 'form-control' %>
</div>
<%= f.hidden_field 'season_id', value: @game.season.id %>
<%= f.submit 'Update Game', class: 'btn btn-outline-primary btn-block' %>
<% end %>
</div>
</div>
</div>
</div>
3 changes: 3 additions & 0 deletions app/views/games/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<div class="game-actions">
<%= link_to 'Return to League', league_path(@game.league), class: 'btn btn-outline-primary btn-sm mb-2 mr-2' %>
<%= @game.complete_or_uncomplete_buttons %>
<% if policy(@game).can_edit? %>
<%= link_to 'Edit Game', edit_game_path(@game), class: 'btn btn-outline-info btn-sm mb-2 ml-2' %>
<% end %>
</div>
<div class="card border-primary standings mb-4">
<div class="card-header header-text border-primary text-primary">
Expand Down
54 changes: 54 additions & 0 deletions spec/features/games/admin_can_edit_game_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'rails_helper'

describe 'Admin can edit a game' do
let(:game) { create(:game, buy_in: 15, date: Date.today, completed: false) }
let(:user) { game.league.user }

describe 'for an admin' do

describe 'for an uncomplete game' do
before do
stub_current_user user

visit game_path(game)

expect(page).to have_content "Buy In: $15"
expect(page).to have_content "Date: #{Date.today.strftime('%B %-e, %Y')}"
end

before do
click_link 'Edit Game'
end

it 'should update the buy in on a game' do
fill_in 'Buy In Amount', with: '100'
click_button 'Update Game'

expect(page).to have_content "Buy In: $100"
expect(page).not_to have_content "Buy In: $15"
end

it 'should update the date on a game' do
fill_in 'Date', with: Date.tomorrow
click_button 'Update Game'

expect(page).to have_content "Date: #{Date.tomorrow.strftime('%B %-e, %Y')}"
expect(page).not_to have_content "Date: #{Date.today.strftime('%B %-e, %Y')}"
end
end

describe 'for a complete game' do
before do
game.update(completed: true)
visit game_path(game)
end

it 'should not have a link to edit game' do
expect(page).to have_content("Date: #{Date.today.strftime('%B %-e, %Y')}")
expect(page).not_to have_link("Edit Game")
end
end
end

describe 'for a regular user'
end
72 changes: 69 additions & 3 deletions spec/requests/games_controller_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe 'Games Controller', type: :request do
let(:league) { create(:league) }
let(:user) { league.user }
let(:game) { create(:game, season: league.current_season) }
let(:game) { create(:game, season: league.current_season, buy_in: 1) }

describe 'GET#new' do
before do
Expand Down Expand Up @@ -47,7 +47,7 @@
expect {
post_games
}.to change { Game.count }.by(1)
expect(response.status).to eq(302)
expect(response).to have_http_status(302)
end
end
end
Expand All @@ -56,7 +56,73 @@
it 'renders the show template' do
get "/games/#{game.id}"

expect(response.status).to eq(200)
expect(response).to have_http_status(200)
end
end

describe 'GET#edit' do
before { stub_current_user(user) }

subject(:get_edit) { get "/games/#{game.id}/edit" }

describe 'As an admin' do
it 'renders the edit template' do
get_edit

expect(response).to have_http_status(200)
end
end

describe 'As a regular user' do
let(:membership) { create(:membership, league: league, role: 0) }
let(:user) { membership.user }

it 'redirects the user' do
get_edit

expect(response).to have_http_status(302)
end
end
end

describe 'PATCH#update' do
before { stub_current_user(user) }

let(:game_params) do
{ buy_in: 100}
end

subject(:patch_update) { patch "/games/#{game.id}", params: { game: game_params } }

describe 'As an admin' do
it 'updates the game' do
expect {
patch_update
}.to change { game.reload; game.buy_in }
end

it 'redirects the user' do
patch_update

expect(response).to have_http_status(302)
end
end

describe 'As a regular user' do
let(:membership) { create(:membership, league: league, role: 0) }
let(:user) { membership.user }

it 'does not update the game' do
expect {
patch_update
}.not_to change { game.reload; game.buy_in }
end

it 'redirects the user' do
patch_update

expect(response).to have_http_status(302)
end
end
end

Expand Down

0 comments on commit d3e494e

Please sign in to comment.