From 0867bc68749873aa131c0f3b273771b5f70ae778 Mon Sep 17 00:00:00 2001 From: Tomer Kimia Date: Tue, 1 Apr 2014 17:14:05 -0400 Subject: players can now leave tournaments --- app/controllers/static_controller.rb | 3 +++ app/controllers/tournaments_controller.rb | 11 ++++++++++- app/models/tournament.rb | 6 ++++++ app/views/matches/index.html.erb | 17 +++++++++++++++-- app/views/tournaments/show.html.erb | 16 +++++++++++++--- config/routes.rb | 1 + 6 files changed, 48 insertions(+), 6 deletions(-) diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rb index 6fc9490..038cc19 100644 --- a/app/controllers/static_controller.rb +++ b/app/controllers/static_controller.rb @@ -1,4 +1,7 @@ class StaticController < ApplicationController def homepage end + + def test + end end diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 8d90758..2f04e1f 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -76,7 +76,16 @@ class TournamentsController < ApplicationController end format.html { render action: 'permission_denied', status: :forbidden } format.json { render json: "Permission denied", status: :forbidden } - end + end + when "leave" + respond_to do |format| + if @tournament.leave(current_user) + format.html {redirect_to tournaments_url, notice: 'You have left the tournament.' } + format.json { head :no_content } + end + format.html {redirect_to @tournament, notice: 'You were\'t a part of this tournament.' } + format.json { render json: "Permission denied", status: :forbidden } + end when "open" respond_to do |format| if @tournament.setup diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 69bedf5..21dc9bb 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -19,6 +19,12 @@ class Tournament < ActiveRecord::Base players.push(user) end + def leave(user) + if players.include?(user) + players.delete(user) + end + end + def setup(tournament) num_teams = (self.players.count/self.max_players_per_team).floor num_matches = num_teams - 1 diff --git a/app/views/matches/index.html.erb b/app/views/matches/index.html.erb index d862d57..f1019ec 100644 --- a/app/views/matches/index.html.erb +++ b/app/views/matches/index.html.erb @@ -1,5 +1,6 @@ -

Listing matches

+

<%= @tournament.name %> Matches

+ -<%= link_to 'New Match', new_tournament_match_path %> + + + <% @matches.each do |tournament | %> + + + + <% end %> + + \ No newline at end of file diff --git a/app/views/tournaments/show.html.erb b/app/views/tournaments/show.html.erb index cc0f0e3..df85dcf 100644 --- a/app/views/tournaments/show.html.erb +++ b/app/views/tournaments/show.html.erb @@ -71,10 +71,17 @@ <%= submit_tag("Join Tournamnet") %> <% end %> + +<% elsif @tournament.players.include?(current_user) %> + <%= form_tag(tournament_path(@tournament), method: "put") do %> + + <%= submit_tag("Leave Tournamnet") %> + <% end %> <% end %> <%# If user is the host, let them start the tournment %> <% if @tournament.hosts.include?(current_user) %> + <%= form_tag(tournaments_path.to_s + "/" + @tournament.id.to_s + "/edit", method: "get") do %> <% if @tournament.players.count >= @tournament.min_players_per_team * @tournament.min_teams_per_match %> @@ -83,9 +90,12 @@ <%= submit_tag("Start Tournament", disabled: true) %> <% end %>
- <%= link_to 'Edit', edit_tournament_path(@tournament) %> | - <%= link_to 'Back', tournaments_path %> -<% end %> + <%= link_to 'Edit', edit_tournament_path(@tournament) %> | + <%= link_to 'Back', tournaments_path %> | + <%= link_to 'Cancel Tournament', @tournament, method: :delete, data: { confirm: 'Are you sure?' } %> + <% end %> + + <%end %> diff --git a/config/routes.rb b/config/routes.rb index 5c5bece..70e5359 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,6 +20,7 @@ Leaguer::Application.routes.draw do root to: 'static#homepage' + get '/testsvg', to: 'static#test' # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". -- cgit v1.2.3