From b62c07359a9cbc7bad93c375c50d266a40dfe539 Mon Sep 17 00:00:00 2001 From: tkimia Date: Thu, 3 Apr 2014 16:24:36 -0400 Subject: Users can't join twice --- app/models/tournament.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 87b516e..4483535 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -9,7 +9,7 @@ class Tournament < ActiveRecord::Base end def joinable_by?(user) - return ((not user.nil?) and user.in_group?(:player) and open?) + return ((not user.nil?) and user.in_group?(:player) and open? and !players.include?(user)) end def join(user) -- cgit v1.2.3-2-g168b From dfe5dbd2ada1841b09f70bfd742c10ba878f74fe Mon Sep 17 00:00:00 2001 From: shumakl Date: Thu, 3 Apr 2014 16:44:29 -0400 Subject: Use the null object pattern for current_user when not logged in --- app/helpers/sessions_helper.rb | 4 ++-- app/models/user.rb | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb index 54e4b0d..89e5ef3 100644 --- a/app/helpers/sessions_helper.rb +++ b/app/helpers/sessions_helper.rb @@ -18,7 +18,7 @@ module SessionsHelper def current_user @token ||= Session.hash_token(cookies[:remember_token]) @session ||= Session.find_by(token: @token) - @current_user ||= (@session.nil? ? nil : @session.user) + @current_user ||= (@session.nil? ? NilUser.new : @session.user) end # checks if someone is currently signed in @@ -30,7 +30,7 @@ module SessionsHelper if signed_in? @session.destroy end - @current_user = nil + @current_user = NilUser.new cookies.delete(:remember_token) end diff --git a/app/models/user.rb b/app/models/user.rb index 277d885..016c155 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -91,3 +91,18 @@ class User < ActiveRecord::Base validates :password, length: { minimum: 6 } end +class NilUser + def nil? + return true + end + def can?(action) + return false + end + def method_missing(name, *args) + # Throw an error if User doesn't have this method + super unless User.new.respond_to?(name) + # User has this method -- return a blank value + # 'false' if the method ends with '?'; 'nil' otherwise. + name.ends_with?('?') ? false : nil + end +end -- cgit v1.2.3-2-g168b From dacab01db3ac4b9d9e2281daff74230a003f138e Mon Sep 17 00:00:00 2001 From: tkimia Date: Thu, 3 Apr 2014 16:45:05 -0400 Subject: small change in form (Set Rounds no longer viewable). AJAX fixed so that start tournament button enables or disables based on players here. --- app/views/tournaments/_selected.html.erb | 2 +- app/views/tournaments/show.html.erb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/app/views/tournaments/_selected.html.erb b/app/views/tournaments/_selected.html.erb index 8734b14..8a704b5 100644 --- a/app/views/tournaments/_selected.html.erb +++ b/app/views/tournaments/_selected.html.erb @@ -4,7 +4,7 @@ <% @chosen = Game.find_by(params[:game]) %> <% @tournament.attributes.each do |name, value| %> - <% if (name == "id") or (name =~ /.*_at$/) or (name == "game_id") or (name == "status") %> + <% if (name == "id") or (name =~ /.*_at$/) or (name == "game_id") or (name == "status") or (name == "set_rounds") %> <% next %> <% end %>

diff --git a/app/views/tournaments/show.html.erb b/app/views/tournaments/show.html.erb index b771701..d246f7a 100644 --- a/app/views/tournaments/show.html.erb +++ b/app/views/tournaments/show.html.erb @@ -134,11 +134,18 @@ function donehandle( tournament ) { $("#prog-bar").width( (pct_complete * 100) +"%"); $("#players-needed").text(here + " " + (here==1?"player has":"players have") + " signed up. " + needed + " players needed. "); players = ""; + + //creates the present user list for (var i = 0; i < tournament["players"].length; i++) { players = players+"

  • "+tournament["players"][i]["user_name"]+"
  • " } + + //updates the user list $("#tournament-users").html(players); + //if there are enough players to start, enable the button, else disable it. + $("input[value=\"Start Tournament\"]").prop('disabled', (pct_complete >= 1)? false : true); + } setTimeout(function(){$.ajax({url: "<%= url_for @tournament %>.json"}).done(donehandle)}, 3000); } -- cgit v1.2.3-2-g168b