diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/tournaments_controller.rb | 17 | ||||
-rw-r--r-- | app/models/tournament.rb | 21 | ||||
-rw-r--r-- | app/models/user.rb | 1 | ||||
-rw-r--r-- | app/views/tournaments/new.html.erb | 3 | ||||
-rw-r--r-- | app/views/tournaments/show.html.erb | 6 |
5 files changed, 41 insertions, 7 deletions
diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 4f79d44..8ed8cc9 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 @@ -63,13 +63,26 @@ 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 @tournament = Tournament.find(params[:id]) end - def check_perms + def check_perms unless (signed_in? and current_user.in_group?(:host)) respond_to do |format| format.html { render action: 'permission_denied', status: :forbidden } diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 5e8ddfe..ca7fade 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -1,4 +1,21 @@ class Tournament < ActiveRecord::Base - belongs_to :game - has_many :matches + belongs_to :game + has_many :matches + 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 9288ef6..6405c8e 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/new.html.erb b/app/views/tournaments/new.html.erb index 1e80147..8c74068 100644 --- a/app/views/tournaments/new.html.erb +++ b/app/views/tournaments/new.html.erb @@ -1,8 +1,5 @@ -<%= javascript_include_tag :defaults %> - <h1>New Tournament</h1> - <%= form_tag(new_tournament_path, method: "get") do %> <%= select_tag('game', options_from_collection_for_select(@games, 'id', 'name', @tournament.game.nil? || @tournament.game.id), diff --git a/app/views/tournaments/show.html.erb b/app/views/tournaments/show.html.erb index 4f97c7c..85ed026 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 %> |