From 1788dde36e53c4ef16adc5db2d19f44797325496 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 6 Mar 2014 18:55:38 -0500 Subject: implement tournament joining --- app/controllers/tournaments_controller.rb | 15 ++++++++++++++- app/models/tournament.rb | 19 +++++++++++++++++- app/models/user.rb | 1 + app/views/tournaments/_selected.html.erb | 32 ++++++++----------------------- app/views/tournaments/new.html.erb | 4 ++-- app/views/tournaments/show.html.erb | 6 ++++++ config/routes.rb | 8 +++++--- 7 files changed, 54 insertions(+), 31 deletions(-) diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 915c072..5e79d7a 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -1,5 +1,5 @@ class TournamentsController < ApplicationController - before_action :set_tournament, only: [:show, :edit, :update, :destroy] + before_action :set_tournament, only: [:show, :edit, :update, :destroy, :join] before_action :check_perms, only: [:new, :create, :edit, :update, :destroy] # GET /tournaments @@ -64,6 +64,19 @@ class TournamentsController < ApplicationController end end + # POST /tournaments/1/join + # POST /tournaments/1/join.json + def join + respond_to do |format| + if @tournament.join(current_user) + format.html { redirect_to @tournament, notice: 'You have joined this tournament.' } + format.json { head :no_content } + end + format.html { render action: 'permission_denied', status: :forbidden } + format.json { render json: "Permission denied", status: :forbidden } + end + end + private # Use callbacks to share common setup or constraints between actions. def set_tournament diff --git a/app/models/tournament.rb b/app/models/tournament.rb index cc915a0..afdd27e 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -1,3 +1,20 @@ class Tournament < ActiveRecord::Base - belongs_to :game + belongs_to :game + has_many :users, :through => :user_tournament_pair + + def open? + return true + end + + def joinable_by?(user) + return ((not user.nil?) and user.in_group?(:player) and open?) + end + + def join(user) + unless joinable?(user) + return false + end + pair = new_user_tournament_pair(user: user) + return pair.save + end end diff --git a/app/models/user.rb b/app/models/user.rb index 079b870..976ecf4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,5 @@ class User < ActiveRecord::Base + has_many :tournaments, :through => :user_tournament_pair before_save { self.email = email.downcase } before_save { self.user_name = user_name } diff --git a/app/views/tournaments/_selected.html.erb b/app/views/tournaments/_selected.html.erb index 4277d8e..551dc40 100644 --- a/app/views/tournaments/_selected.html.erb +++ b/app/views/tournaments/_selected.html.erb @@ -1,31 +1,15 @@ <%= form_for(@tournament) do |f| %> - <% if @tournament.errors.any? %> -
-

<%= pluralize(@tournament.errors.count, "error") %> prohibited this tournament from being saved:

+ <%= render "common/error_messages", :target => @tournament %> + <%= f.hidden_field(:game_id) %> - -
- <% end %> - - <%# this is the dynamic script to output fields to the form %> - <% @chosen = Game.find(@game) %> - <% @chosen.attributes.each do |name, value| %> - <% if name == "id" %> + <% @tournament.attributes.each do |name, value| %> + <% if (name == "id") or (name =~ /.*_at$/) %> <% next %> <% end %> - <% if name == "created_at" %> - <% break %> - <% end %> -

- -
- value=<%= value %>> -

+

+ <%= f.label name %>
+ <%= f.text_field name %> +

<% end %> - <%= f.submit %> <% end %> diff --git a/app/views/tournaments/new.html.erb b/app/views/tournaments/new.html.erb index 6c3fefc..e007e31 100644 --- a/app/views/tournaments/new.html.erb +++ b/app/views/tournaments/new.html.erb @@ -2,13 +2,13 @@ <%= form_tag(new_tournament_path, method: "get") do %> <%= select_tag('game', - options_from_collection_for_select(@games, 'id', 'name', @game.nil? || @game.id), + options_from_collection_for_select(@games, 'id', 'name', @tournament.game.nil? || @tournament.game.id), :prompt => "Select a Game Type") %> <%= submit_tag("Select", :class => "btn-warning btn-lg") %> <% end %>
- <% unless @game.nil? %> + <% unless @tournament.game.nil? %> <%= render 'selected' %> <% end %>
diff --git a/app/views/tournaments/show.html.erb b/app/views/tournaments/show.html.erb index 0d9dd10..1470d80 100644 --- a/app/views/tournaments/show.html.erb +++ b/app/views/tournaments/show.html.erb @@ -1,3 +1,9 @@ +<% if @tournament.joinable_by?(current_user) %> + <%= form_tag(tournament_page(@tournament)+"/join", method: "get") do %> + <%= submit_tag("Join") %> + <% end %> +<% end %> +

Game: <%= @tournament.game %> diff --git a/config/routes.rb b/config/routes.rb index 2409eb2..fc9d45a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,9 +15,11 @@ Leaguer::Application.routes.draw do resources :matches - resources :tournaments - - #set 'selected' to: 'tournaments#selected' via: 'get' + resources :tournaments do + collection do + post 'join' + end + end resources :servers -- cgit v1.2.3-2-g168b