Permalink
Browse files

add battles

  • Loading branch information...
1 parent 11712bc commit b074c0df65e8a2dcab0f9816ef9b3ae4bc625536 @mroth committed Dec 3, 2011
View
2 README
@@ -11,7 +11,7 @@ model BATTLE
- winner
- loser
belongs_to :winner, :class_name => 'Contender', :foreign_key => "winner_id"
- belongs_to :loser, :class_name => 'Contender', :foreign_key => "winner_id"
+ belongs_to :loser, :class_name => 'Contender', :foreign_key => "loser_id"
- voter?
View
3 app/assets/javascripts/battles.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/battles.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the battles controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
83 app/controllers/battles_controller.rb
@@ -0,0 +1,83 @@
+class BattlesController < ApplicationController
+ # GET /battles
+ # GET /battles.json
+ def index
+ @battles = Battle.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @battles }
+ end
+ end
+
+ # GET /battles/1
+ # GET /battles/1.json
+ def show
+ @battle = Battle.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render json: @battle }
+ end
+ end
+
+ # GET /battles/new
+ # GET /battles/new.json
+ def new
+ @battle = Battle.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @battle }
+ end
+ end
+
+ # GET /battles/1/edit
+ def edit
+ @battle = Battle.find(params[:id])
+ end
+
+ # POST /battles
+ # POST /battles.json
+ def create
+ @battle = Battle.new(params[:battle])
+
+ respond_to do |format|
+ if @battle.save
+ format.html { redirect_to @battle, notice: 'Battle was successfully created.' }
+ format.json { render json: @battle, status: :created, location: @battle }
+ else
+ format.html { render action: "new" }
+ format.json { render json: @battle.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /battles/1
+ # PUT /battles/1.json
+ def update
+ @battle = Battle.find(params[:id])
+
+ respond_to do |format|
+ if @battle.update_attributes(params[:battle])
+ format.html { redirect_to @battle, notice: 'Battle was successfully updated.' }
+ format.json { head :ok }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @battle.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /battles/1
+ # DELETE /battles/1.json
+ def destroy
+ @battle = Battle.find(params[:id])
+ @battle.destroy
+
+ respond_to do |format|
+ format.html { redirect_to battles_url }
+ format.json { head :ok }
+ end
+ end
+end
View
2 app/helpers/battles_helper.rb
@@ -0,0 +1,2 @@
+module BattlesHelper
+end
View
4 app/models/battle.rb
@@ -0,0 +1,4 @@
+class Battle < ActiveRecord::Base
+ belongs_to :winner, :class_name => 'Contender', :foreign_key => "winner_id"
+ belongs_to :loser, :class_name => 'Contender', :foreign_key => "loser_id"
+end
View
2 app/models/contender.rb
@@ -1,4 +1,6 @@
class Contender < ActiveRecord::Base
+ has_many :winning_battles, :class_name => 'Battle', :foreign_key => 'winner_id'#, :counter_cache => :wins
+ has_many :losing_battles, :class_name => 'Battle', :foreign_key => 'loser_id'#, :counter_cache => :loses
def banner
"banners/#{uname}.png"
View
29 app/views/battles/_form.html.erb
@@ -0,0 +1,29 @@
+<%= form_for(@battle) do |f| %>
+ <% if @battle.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@battle.errors.count, "error") %> prohibited this battle from being saved:</h2>
+
+ <ul>
+ <% @battle.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= f.label :winner_id %><br />
+ <%= f.number_field :winner_id %>
+ </div>
+ <div class="field">
+ <%= f.label :loser_id %><br />
+ <%= f.number_field :loser_id %>
+ </div>
+ <div class="field">
+ <%= f.label :voter %><br />
+ <%= f.text_field :voter %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
View
6 app/views/battles/edit.html.erb
@@ -0,0 +1,6 @@
+<h1>Editing battle</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @battle %> |
+<%= link_to 'Back', battles_path %>
View
27 app/views/battles/index.html.erb
@@ -0,0 +1,27 @@
+<h1>Listing battles</h1>
+
+<table>
+ <tr>
+ <th>Winner</th>
+ <th>Loser</th>
+ <th>Voter</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @battles.each do |battle| %>
+ <tr>
+ <td><%= battle.winner_id %></td>
+ <td><%= battle.loser_id %></td>
+ <td><%= battle.voter %></td>
+ <td><%= link_to 'Show', battle %></td>
+ <td><%= link_to 'Edit', edit_battle_path(battle) %></td>
+ <td><%= link_to 'Destroy', battle, confirm: 'Are you sure?', method: :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Battle', new_battle_path %>
View
5 app/views/battles/new.html.erb
@@ -0,0 +1,5 @@
+<h1>New battle</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', battles_path %>
View
20 app/views/battles/show.html.erb
@@ -0,0 +1,20 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+ <b>Winner:</b>
+ <%= @battle.winner_id %>
+</p>
+
+<p>
+ <b>Loser:</b>
+ <%= @battle.loser_id %>
+</p>
+
+<p>
+ <b>Voter:</b>
+ <%= @battle.voter %>
+</p>
+
+
+<%= link_to 'Edit', edit_battle_path(@battle) %> |
+<%= link_to 'Back', battles_path %>
View
2 config/routes.rb
@@ -1,4 +1,6 @@
Personalappeal::Application.routes.draw do
+ resources :battles
+
resources :contenders
# The priority is based upon order of creation:
View
11 db/migrate/20111203200206_create_battles.rb
@@ -0,0 +1,11 @@
+class CreateBattles < ActiveRecord::Migration
+ def change
+ create_table :battles do |t|
+ t.integer :winner_id
+ t.integer :loser_id
+ t.string :voter
+
+ t.timestamps
+ end
+ end
+end
View
10 db/schema.rb
@@ -11,7 +11,15 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20111203184826) do
+ActiveRecord::Schema.define(:version => 20111203200206) do
+
+ create_table "battles", :force => true do |t|
+ t.integer "winner_id"
+ t.integer "loser_id"
+ t.string "voter"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
create_table "contenders", :force => true do |t|
t.string "uname"
View
11 test/fixtures/battles.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ winner_id: 1
+ loser_id: 1
+ voter: MyString
+
+two:
+ winner_id: 1
+ loser_id: 1
+ voter: MyString
View
49 test/functional/battles_controller_test.rb
@@ -0,0 +1,49 @@
+require 'test_helper'
+
+class BattlesControllerTest < ActionController::TestCase
+ setup do
+ @battle = battles(:one)
+ end
+
+ test "should get index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:battles)
+ end
+
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+ test "should create battle" do
+ assert_difference('Battle.count') do
+ post :create, battle: @battle.attributes
+ end
+
+ assert_redirected_to battle_path(assigns(:battle))
+ end
+
+ test "should show battle" do
+ get :show, id: @battle.to_param
+ assert_response :success
+ end
+
+ test "should get edit" do
+ get :edit, id: @battle.to_param
+ assert_response :success
+ end
+
+ test "should update battle" do
+ put :update, id: @battle.to_param, battle: @battle.attributes
+ assert_redirected_to battle_path(assigns(:battle))
+ end
+
+ test "should destroy battle" do
+ assert_difference('Battle.count', -1) do
+ delete :destroy, id: @battle.to_param
+ end
+
+ assert_redirected_to battles_path
+ end
+end
View
7 test/unit/battle_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class BattleTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
View
4 test/unit/helpers/battles_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class BattlesHelperTest < ActionView::TestCase
+end

0 comments on commit b074c0d

Please sign in to comment.