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-54-g00ecf 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-54-g00ecf From 9d2e1394bc09d4df3fce97d6151f5bfc3546fbe8 Mon Sep 17 00:00:00 2001 From: nfoy Date: Thu, 3 Apr 2014 16:48:58 -0400 Subject: Added Gravatar support for tournament's index view. --- app/views/tournaments/index.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/tournaments/index.html.erb b/app/views/tournaments/index.html.erb index 6a86955..e174de7 100644 --- a/app/views/tournaments/index.html.erb +++ b/app/views/tournaments/index.html.erb @@ -6,7 +6,7 @@ <%# Each tournament has a div for its listing %> <% @tournaments.each do |t| %>
    -
    Gravatar of Host Goes Here
    +
    <%= image_tag 'http://www.gravatar.com/avatar/' + Digest::MD5.hexdigest(t.hosts.first.email) + '?s=100&d=mm' %>
    -- cgit v1.2.3-54-g00ecf From 6cc7eb9e419c219d82d318ebb1bab8a853f73da5 Mon Sep 17 00:00:00 2001 From: tkimia Date: Thu, 3 Apr 2014 16:49:09 -0400 Subject: footer clears on both sides, has top-margin of 10. Tournament show screen repetitive info fixed --- app/assets/stylesheets/application.css | 5 +++++ app/views/tournaments/show.html.erb | 29 ----------------------------- 2 files changed, 5 insertions(+), 29 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index b3f5d46..dd7588a 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -23,3 +23,8 @@ .navbar{ background-color: white; } + +footer { + clear: both; + margin-top: 10px; +} \ No newline at end of file diff --git a/app/views/tournaments/show.html.erb b/app/views/tournaments/show.html.erb index d246f7a..9939d02 100644 --- a/app/views/tournaments/show.html.erb +++ b/app/views/tournaments/show.html.erb @@ -40,35 +40,6 @@ <%= @tournament.max_teams_per_match %>

    -

    - Set rounds: - <%= @tournament.set_rounds %> -

    - -

    - Min players per team: - <%= @tournament.min_players_per_team %> -

    - -

    - Max players per team: - <%= @tournament.max_players_per_team %> -

    - -

    - Min teams per match: - <%= @tournament.min_teams_per_match %> -

    - -

    - Max teams per match: - <%= @tournament.max_teams_per_match %> -

    - -

    - Set rounds: - <%= @tournament.set_rounds %> -

    Randomized teams: -- cgit v1.2.3-54-g00ecf From 051e61877b4c77a17fd4fa68dfca4d5e74a687df Mon Sep 17 00:00:00 2001 From: tkimia Date: Thu, 3 Apr 2014 17:17:12 -0400 Subject: fixed some of andrews weird stuff with starting a tournament --- app/controllers/tournaments_controller.rb | 14 +++++--------- app/models/tournament.rb | 8 ++++---- app/views/matches/index.html.erb | 4 ++-- app/views/tournaments/show.html.erb | 8 +++++--- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 9cf3404..4bba997 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -36,12 +36,7 @@ class TournamentsController < ApplicationController # GET /tournaments/1/edit def edit - if params['close_action'] == 'close' - @tournament.status = 1 - @tournament.save - @tournament.setup(@tournament) - redirect_to "/tournaments" - end + end # POST /tournaments @@ -96,7 +91,10 @@ class TournamentsController < ApplicationController format.html {redirect_to @tournament, notice: 'You were\'t a part of this tournament.' } format.json { render json: "Permission denied", status: :forbidden } end - when "open" + when "start" + @tournament.status = 1 + @tournament.save + @tournament.setup() respond_to do |format| if @tournament.setup format.html { render action: 'show', notice: 'You have joined this tournament.' } @@ -105,8 +103,6 @@ class TournamentsController < ApplicationController format.html { render action: 'permission_denied', status: :forbidden } format.json { render json: "Permission denied", status: :forbidden } end - #when "close" - # TODO else respond_to do |format| format.html { render action: 'show', notice: "Invalid action", status: :unprocessable_entity } diff --git a/app/models/tournament.rb b/app/models/tournament.rb index 4483535..6d92f3d 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -20,12 +20,12 @@ class Tournament < ActiveRecord::Base end def leave(user) - if players.include?(user) + if players.include?(user) && status == 0 players.delete(user) end end - def setup(tournament) + def setup() num_teams = (self.players.count/self.max_players_per_team).floor num_matches = num_teams - 1 for i in 1..num_matches @@ -33,9 +33,9 @@ class Tournament < ActiveRecord::Base end match_num = 0 team_num = 0 - self.players.each_slice(tournament.max_players_per_team) do |players| + self.players.each_slice(max_players_per_team) do |players| self.matches[match_num].teams.push(Team.create(users: players)) - if (team_num != 0 and team_num % tournament.max_teams_per_match == 0) + if (team_num != 0 and team_num % max_teams_per_match == 0) match_num += 1 team_num = 0 else diff --git a/app/views/matches/index.html.erb b/app/views/matches/index.html.erb index 0f86da6..babf45e 100644 --- a/app/views/matches/index.html.erb +++ b/app/views/matches/index.html.erb @@ -1,6 +1,6 @@

    <%= @tournament.name %> Matches

    - + <% if @tournament.hosts.include?(current_user) %> - <%= form_tag(tournaments_path.to_s + "/" + @tournament.id.to_s + "/edit", method: "get") do %> -