diff options
author | Luke Shumaker <LukeShu@sbcglobal.net> | 2014-03-06 18:55:38 -0500 |
---|---|---|
committer | Luke Shumaker <LukeShu@sbcglobal.net> | 2014-03-06 18:55:38 -0500 |
commit | 1788dde36e53c4ef16adc5db2d19f44797325496 (patch) | |
tree | 4d331750a288fcdc052df2ae7dff19c68cd24151 | |
parent | 39f373778e73c54952dddd11e87f4ba354471622 (diff) |
implement tournament joining
-rw-r--r-- | app/controllers/tournaments_controller.rb | 15 | ||||
-rw-r--r-- | app/models/tournament.rb | 19 | ||||
-rw-r--r-- | app/models/user.rb | 1 | ||||
-rw-r--r-- | app/views/tournaments/_selected.html.erb | 32 | ||||
-rw-r--r-- | app/views/tournaments/new.html.erb | 4 | ||||
-rw-r--r-- | app/views/tournaments/show.html.erb | 6 | ||||
-rw-r--r-- | 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? %> - <div id="error_explanation"> - <h2><%= pluralize(@tournament.errors.count, "error") %> prohibited this tournament from being saved:</h2> + <%= render "common/error_messages", :target => @tournament %> + <%= f.hidden_field(:game_id) %> - <ul> - <% @tournament.errors.full_messages.each do |msg| %> - <li><%= msg %></li> - <% end %> - </ul> - </div> - <% 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 %> - <p> - <label for=<%= name %>><%= name.capitalize.gsub('_', ' ') %></label> - <br /> - <input type="text" id=<%= name %> value=<%= value %>> - </p> + <p> + <%= f.label name %><br> + <%= f.text_field name %> + </p> <% 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 %> <div id='ajax-form'> - <% unless @game.nil? %> + <% unless @tournament.game.nil? %> <%= render 'selected' %> <% end %> </div> 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 %> + <p> <strong>Game:</strong> <%= @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 |