Skip to content

Commit eb56a3b

Browse files
committed
quasi-finish data model
1 parent a382a55 commit eb56a3b

23 files changed

+397
-1
lines changed

app/controllers/matches_controller.rb

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
class MatchesController < ApplicationController
2+
# GET /matches
3+
# GET /matches.json
4+
def index
5+
@matches = Match.all
6+
7+
respond_to do |format|
8+
format.html # index.html.erb
9+
format.json { render json: @matches }
10+
end
11+
end
12+
13+
# GET /matches/1
14+
# GET /matches/1.json
15+
def show
16+
@match = Match.find(params[:id])
17+
18+
respond_to do |format|
19+
format.html # show.html.erb
20+
format.json { render json: @match }
21+
end
22+
end
23+
24+
# GET /matches/new
25+
# GET /matches/new.json
26+
def new
27+
@match = Match.new
28+
29+
respond_to do |format|
30+
format.html # new.html.erb
31+
format.json { render json: @match }
32+
end
33+
end
34+
35+
# GET /matches/1/edit
36+
def edit
37+
@match = Match.find(params[:id])
38+
end
39+
40+
# POST /matches
41+
# POST /matches.json
42+
def create
43+
@match = Match.new(params[:match])
44+
45+
respond_to do |format|
46+
if @match.save
47+
format.html { redirect_to @match, notice: 'Match was successfully created.' }
48+
format.json { render json: @match, status: :created, location: @match }
49+
else
50+
format.html { render action: "new" }
51+
format.json { render json: @match.errors, status: :unprocessable_entity }
52+
end
53+
end
54+
end
55+
56+
# PUT /matches/1
57+
# PUT /matches/1.json
58+
def update
59+
@match = Match.find(params[:id])
60+
61+
respond_to do |format|
62+
if @match.update_attributes(params[:match])
63+
format.html { redirect_to @match, notice: 'Match was successfully updated.' }
64+
format.json { head :ok }
65+
else
66+
format.html { render action: "edit" }
67+
format.json { render json: @match.errors, status: :unprocessable_entity }
68+
end
69+
end
70+
end
71+
72+
# DELETE /matches/1
73+
# DELETE /matches/1.json
74+
def destroy
75+
@match = Match.find(params[:id])
76+
@match.destroy
77+
78+
respond_to do |format|
79+
format.html { redirect_to matches_url }
80+
format.json { head :ok }
81+
end
82+
end
83+
end

app/controllers/rounds_controller.rb

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
class RoundsController < ApplicationController
2+
respond_to :json
3+
4+
# GET /rounds
5+
# GET /rounds.json
6+
def index
7+
@rounds = Round.all
8+
9+
respond_to do |format|
10+
format.html # index.html.erb
11+
format.json { render json: @rounds }
12+
end
13+
end
14+
15+
# GET /rounds/1
16+
# GET /rounds/1.json
17+
def show
18+
@round = Round.find(params[:id])
19+
20+
respond_to do |format|
21+
format.html # show.html.erb
22+
format.json { render json: @round }
23+
end
24+
end
25+
26+
# GET /rounds/new
27+
# GET /rounds/new.json
28+
def new
29+
@round = Round.new
30+
31+
respond_to do |format|
32+
format.html # new.html.erb
33+
format.json { render json: @round }
34+
end
35+
end
36+
37+
# GET /rounds/1/edit
38+
def edit
39+
@round = Round.find(params[:id])
40+
end
41+
42+
# POST /rounds
43+
# POST /rounds.json
44+
def create
45+
@round = Round.new(params[:round])
46+
47+
respond_to do |format|
48+
if @round.save
49+
format.html { redirect_to @round, notice: 'Round was successfully created.' }
50+
format.json { render json: @round, status: :created, location: @round }
51+
else
52+
format.html { render action: "new" }
53+
format.json { render json: @round.errors, status: :unprocessable_entity }
54+
end
55+
end
56+
end
57+
58+
# PUT /rounds/1
59+
# PUT /rounds/1.json
60+
def update
61+
@round = Round.find(params[:id])
62+
63+
respond_to do |format|
64+
if @round.update_attributes(params[:round])
65+
format.html { redirect_to @round, notice: 'Round was successfully updated.' }
66+
format.json { head :ok }
67+
else
68+
format.html { render action: "edit" }
69+
format.json { render json: @round.errors, status: :unprocessable_entity }
70+
end
71+
end
72+
end
73+
74+
# DELETE /rounds/1
75+
# DELETE /rounds/1.json
76+
def destroy
77+
@round = Round.find(params[:id])
78+
@round.destroy
79+
80+
respond_to do |format|
81+
format.html { redirect_to rounds_url }
82+
format.json { head :ok }
83+
end
84+
end
85+
end

app/helpers/matches_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module MatchesHelper
2+
end

app/helpers/rounds_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module RoundsHelper
2+
end

app/models/bracket.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
class Bracket < ActiveRecord::Base
22
has_many :teams
3+
has_many :rounds
34
end

app/models/match.rb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Match < ActiveRecord::Base
2+
has_and_belongs_to_many :teams
3+
def winner
4+
self.teams.find { |t| t.id == self.winner_id }
5+
end
6+
def winner=(team)
7+
self.winner_id = team.id
8+
self.save
9+
end
10+
end

app/models/round.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Round < ActiveRecord::Base
2+
has_many :matches
3+
end

app/models/team.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
class Team < ActiveRecord::Base
22
has_one :bracket
3+
has_and_belongs_to_many :matches
34
end
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class CreateRounds < ActiveRecord::Migration
2+
def change
3+
create_table :rounds do |t|
4+
t.integer :rank
5+
t.timestamps
6+
end
7+
create_table :teams_matches do |t|
8+
t.references :team
9+
t.references :match
10+
end
11+
end
12+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class CreateMatches < ActiveRecord::Migration
2+
def change
3+
create_table :matches do |t|
4+
5+
t.timestamps
6+
end
7+
end
8+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class FixJoinTableName < ActiveRecord::Migration
2+
def up
3+
create_table :matches_teams do |t|
4+
t.references :team
5+
t.references :match
6+
end
7+
drop_table :teams_matches
8+
end
9+
10+
def down
11+
end
12+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class RemoveJoinTablePrimaryKey < ActiveRecord::Migration
2+
def up
3+
remove_column :matches_teams, :id
4+
end
5+
6+
def down
7+
end
8+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class AddWinnerKey < ActiveRecord::Migration
2+
def up
3+
add_column :matches, :winner_id, :int
4+
end
5+
6+
def down
7+
end
8+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddRoundIdToMatch < ActiveRecord::Migration
2+
def change
3+
add_column :matches, :round_id, :int
4+
end
5+
end

db/schema.rb

+19-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended to check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(:version => 20110926020748) do
14+
ActiveRecord::Schema.define(:version => 20110926031524) do
1515

1616
create_table "brackets", :force => true do |t|
1717
t.integer "user_id"
@@ -20,6 +20,24 @@
2020
t.datetime "updated_at"
2121
end
2222

23+
create_table "matches", :force => true do |t|
24+
t.datetime "created_at"
25+
t.datetime "updated_at"
26+
t.integer "winner_id"
27+
t.integer "round_id"
28+
end
29+
30+
create_table "matches_teams", :id => false, :force => true do |t|
31+
t.integer "team_id"
32+
t.integer "match_id"
33+
end
34+
35+
create_table "rounds", :force => true do |t|
36+
t.integer "rank"
37+
t.datetime "created_at"
38+
t.datetime "updated_at"
39+
end
40+
2341
create_table "teams", :force => true do |t|
2442
t.string "name"
2543
t.integer "seed"

test/fixtures/matches.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
2+
3+
# This model initially had no columns defined. If you add columns to the
4+
# model remove the '{}' from the fixture names and add the columns immediately
5+
# below each fixture, per the syntax in the comments below
6+
#
7+
one: {}
8+
# column: value
9+
#
10+
two: {}
11+
# column: value

test/fixtures/rounds.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
2+
3+
one:
4+
rank: 1
5+
6+
two:
7+
rank: 1
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'test_helper'
2+
3+
class MatchesControllerTest < ActionController::TestCase
4+
setup do
5+
@match = matches(:one)
6+
end
7+
8+
test "should get index" do
9+
get :index
10+
assert_response :success
11+
assert_not_nil assigns(:matches)
12+
end
13+
14+
test "should get new" do
15+
get :new
16+
assert_response :success
17+
end
18+
19+
test "should create match" do
20+
assert_difference('Match.count') do
21+
post :create, match: @match.attributes
22+
end
23+
24+
assert_redirected_to match_path(assigns(:match))
25+
end
26+
27+
test "should show match" do
28+
get :show, id: @match.to_param
29+
assert_response :success
30+
end
31+
32+
test "should get edit" do
33+
get :edit, id: @match.to_param
34+
assert_response :success
35+
end
36+
37+
test "should update match" do
38+
put :update, id: @match.to_param, match: @match.attributes
39+
assert_redirected_to match_path(assigns(:match))
40+
end
41+
42+
test "should destroy match" do
43+
assert_difference('Match.count', -1) do
44+
delete :destroy, id: @match.to_param
45+
end
46+
47+
assert_redirected_to matches_path
48+
end
49+
end

0 commit comments

Comments
 (0)