summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/controllers/tournaments_controller.rb44
-rw-r--r--app/models/tournament.rb22
-rw-r--r--app/models/user.rb2
-rw-r--r--app/models/user_tournament_pair.rb4
-rw-r--r--app/views/tournaments/new.html.erb3
-rw-r--r--app/views/tournaments/show.html.erb7
6 files changed, 68 insertions, 14 deletions
diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb
index 4f79d44..6fc3ad6 100644
--- a/app/controllers/tournaments_controller.rb
+++ b/app/controllers/tournaments_controller.rb
@@ -1,6 +1,6 @@
class TournamentsController < ApplicationController
- before_action :set_tournament, only: [:show, :edit, :update, :destroy]
- before_action :check_perms, only: [:new, :create, :edit, :update, :destroy]
+ before_action :set_tournament, only: [:show, :edit, :update, :destroy, :join]
+ before_action :check_perms, only: [:new, :create, :edit, :destroy]
# GET /tournaments
# GET /tournaments.json
@@ -42,13 +42,39 @@ class TournamentsController < ApplicationController
# PATCH/PUT /tournaments/1
# PATCH/PUT /tournaments/1.json
def update
- respond_to do |format|
- if @tournament.update(tournament_params)
- format.html { redirect_to @tournament, notice: 'Tournament was successfully updated.' }
- format.json { head :no_content }
+ require 'pp'
+ pp params
+ if params[:update_action].nil?
+ check_perms
+ respond_to do |format|
+ if @tournament.update(tournament_params)
+ format.html { redirect_to @tournament, notice: 'Tournament was successfully updated.' }
+ format.json { head :no_content }
+ else
+ format.html { render action: 'edit' }
+ format.json { render json: @tournament.errors, status: :unprocessable_entity }
+ end
+ end
+ else
+ case params[:update_action]
+ when "join"
+ respond_to do |format|
+ if @tournament.join(current_user)
+ format.html { render action: 'show', 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
+ #when "open"
+ # TODO
+ #when "close"
+ # TODO
else
- format.html { render action: 'edit' }
- format.json { render json: @tournament.errors, status: :unprocessable_entity }
+ respond_to do |format|
+ format.html { render action: 'show', notice: "Invalid action", status: :unprocessable_entity }
+ format.json { render json: @tournament.errors, status: :unprocessable_entity }
+ end
end
end
end
@@ -69,7 +95,7 @@ class TournamentsController < ApplicationController
@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..26dec72 100644
--- a/app/models/tournament.rb
+++ b/app/models/tournament.rb
@@ -1,4 +1,22 @@
class Tournament < ActiveRecord::Base
- belongs_to :game
- has_many :matches
+ belongs_to :game
+ has_many :matches
+ has_many :user_tournament_pairs
+ has_many :users, :through => :user_tournament_pairs
+
+ 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_by?(user)
+ return false
+ end
+ pair = UserTournamentPair.new(tournament: self, user: user)
+ return pair.save
+ end
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 9288ef6..bad7f7b 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -1,4 +1,6 @@
class User < ActiveRecord::Base
+ has_many :user_tournament_pairs
+ has_many :tournaments, :through => :user_tournament_pairs
before_save { self.email = email.downcase }
before_save { self.user_name = user_name }
diff --git a/app/models/user_tournament_pair.rb b/app/models/user_tournament_pair.rb
new file mode 100644
index 0000000..b2676e5
--- /dev/null
+++ b/app/models/user_tournament_pair.rb
@@ -0,0 +1,4 @@
+class UserTournamentPair < ActiveRecord::Base
+ belongs_to :user
+ belongs_to :tournament
+end
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..91c1961 100644
--- a/app/views/tournaments/show.html.erb
+++ b/app/views/tournaments/show.html.erb
@@ -1,3 +1,10 @@
+<% if @tournament.joinable_by?(current_user) %>
+ <%= form_tag(tournament_path(@tournament), method: "put") do %>
+ <input type="hidden" name="update_action" value="join">
+ <%= submit_tag("Join") %>
+ <% end %>
+<% end %>
+
<p>
<strong>Game:</strong>
<%= @tournament.game %>