Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Anybody can add a new team to a running contest now.

  • Loading branch information...
commit 181bee1561e463b360f14c8ea16858f434c936af 1 parent 1a7fc07
@andmej authored
View
3  app/assets/javascripts/teams.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
View
3  app/assets/stylesheets/teams.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the teams controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
32 app/controllers/teams_controller.rb
@@ -0,0 +1,32 @@
+class TeamsController < ApplicationController
+ before_filter :load_contest
+ before_filter :redirect_if_contest_finished
+ load_and_authorize_resource
+
+ def new
+ end
+
+ def create
+ @team = Team.find_by_username(params[:team][:username]) || Team.create(params[:team])
+ if @team.valid?
+ @contest.add_team!(@team)
+ redirect_to @contest, :notice => "Team added!"
+ else
+ render :new
+ end
+ end
+
+ protected
+
+ def load_contest
+ @contest = Contest.find(params[:contest_id])
+ authorize! :read, @contest
+ end
+
+ def redirect_if_contest_finished
+ if @contest.finished?
+ redirect_to @contest, :alert => "This contest has finished. You can't add new teams anymore!"
+ end
+ end
+
+end
View
2  app/helpers/teams_helper.rb
@@ -0,0 +1,2 @@
+module TeamsHelper
+end
View
1  app/models/ability.rb
@@ -4,6 +4,7 @@ class Ability
def initialize(user)
can :read, :all
can :manage, :session
+ can :create, :team
if user.present?
can :manage, :all
View
4 app/models/contest.rb
@@ -28,6 +28,10 @@ def problems_attributes=(attributes)
end
end
+ def add_team!(some_team)
+ teams << some_team unless teams.include?(some_team)
+ end
+
def duration # in minutes
(end_date - start_date) / 60
end
View
2  app/models/team.rb
@@ -2,5 +2,5 @@ class Team < ActiveRecord::Base
has_and_belongs_to_many :contests
has_many :submissions, :dependent => :destroy
- validate :username, :presence => true, :unique => true
+ validates :username, :presence => true, :uniqueness => true
end
View
6 app/views/contests/show.html.erb
@@ -28,6 +28,12 @@
<%= render "scoreboard" %>
</div>
+<% unless @contest.finished? %>
+ <p>
+ <%= link_to "Add new team", new_contest_team_path(@contest) %>
+ </p>
+<% end %>
+
<h3>Last 10 submissions</h3>
<%= render "submissions/submissions" %>
View
5 app/views/layouts/application.html.erb
@@ -9,8 +9,11 @@
</head>
<body>
<div id="container">
+ <div>
<% if current_user %>
- <div>You are currently logged in. <%= link_to "Log out.", session_path, :method => :delete %></div>
+ You are currently logged in. <%= link_to "Log out.", session_path, :method => :delete %>
+ <% else %>
+ <%= link_to "Log in", new_session_path %> to make changes.
<% end %>
<% flash.each do |name, msg| %>
View
13 app/views/teams/new.html.erb
@@ -0,0 +1,13 @@
+<% title "Add team to #{@contest.name}" %>
+
+<%= form_for [@contest, @team] do |f| %>
+ <%= f.error_messages %>
+
+ <div>
+ <%= f.label :username %>
+ <%= f.text_field :username %>
+ </div>
+
+ <%= f.submit "Add team" %>
+
+<% end %>
View
6 config/routes.rb
@@ -1,11 +1,9 @@
Contests::Application.routes.draw do
- resources :problems
- resources :teams
-
resources :contests do
resources :submissions, :only => :index
+ resources :teams
end
-
+
resource :session
root :to => "contests#index"
end
View
7 test/functional/teams_controller_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class TeamsControllerTest < ActionController::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
View
4 test/unit/helpers/teams_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class TeamsHelperTest < ActionView::TestCase
+end

0 comments on commit 181bee1

Please sign in to comment.
Something went wrong with that request. Please try again.