From 7f8718af351cbf99b64e08afb81961bfa26501b0 Mon Sep 17 00:00:00 2001 From: Jacob Cody Wimer Date: Fri, 24 Jan 2014 15:16:33 -0500 Subject: [PATCH] Added matches scaffold and ascended views of weights and schools --- app/assets/javascripts/matches.js.coffee | 3 + app/assets/stylesheets/matches.css.scss | 3 + app/controllers/matches_controller.rb | 74 +++++++++++++++++++++ app/controllers/weights_controller.rb | 2 +- app/helpers/matches_helper.rb | 2 + app/models/match.rb | 3 + app/models/tournament.rb | 2 + app/models/wrestler.rb | 1 + app/views/matches/_form.html.erb | 45 +++++++++++++ app/views/matches/edit.html.erb | 6 ++ app/views/matches/index.html.erb | 39 +++++++++++ app/views/matches/index.json.jbuilder | 4 ++ app/views/matches/new.html.erb | 5 ++ app/views/matches/show.html.erb | 39 +++++++++++ app/views/matches/show.json.jbuilder | 1 + app/views/schools/show.html.erb | 2 +- app/views/weights/show.html.erb | 2 +- config/routes.rb | 2 + db/migrate/20140124174110_create_matches.rb | 15 +++++ db/schema.rb | 14 +++- test/controllers/matches_controller_test.rb | 49 ++++++++++++++ test/fixtures/matches.yml | 19 ++++++ test/helpers/matches_helper_test.rb | 4 ++ test/models/match_test.rb | 7 ++ 24 files changed, 339 insertions(+), 4 deletions(-) create mode 100644 app/assets/javascripts/matches.js.coffee create mode 100644 app/assets/stylesheets/matches.css.scss create mode 100644 app/controllers/matches_controller.rb create mode 100644 app/helpers/matches_helper.rb create mode 100644 app/models/match.rb create mode 100644 app/views/matches/_form.html.erb create mode 100644 app/views/matches/edit.html.erb create mode 100644 app/views/matches/index.html.erb create mode 100644 app/views/matches/index.json.jbuilder create mode 100644 app/views/matches/new.html.erb create mode 100644 app/views/matches/show.html.erb create mode 100644 app/views/matches/show.json.jbuilder create mode 100644 db/migrate/20140124174110_create_matches.rb create mode 100644 test/controllers/matches_controller_test.rb create mode 100644 test/fixtures/matches.yml create mode 100644 test/helpers/matches_helper_test.rb create mode 100644 test/models/match_test.rb diff --git a/app/assets/javascripts/matches.js.coffee b/app/assets/javascripts/matches.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/matches.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://coffeescript.org/ diff --git a/app/assets/stylesheets/matches.css.scss b/app/assets/stylesheets/matches.css.scss new file mode 100644 index 0000000..9b13d59 --- /dev/null +++ b/app/assets/stylesheets/matches.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Matches controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb new file mode 100644 index 0000000..eb1a87f --- /dev/null +++ b/app/controllers/matches_controller.rb @@ -0,0 +1,74 @@ +class MatchesController < ApplicationController + before_action :set_match, only: [:show, :edit, :update, :destroy] + + # GET /matches + # GET /matches.json + def index + @matches = Match.all + end + + # GET /matches/1 + # GET /matches/1.json + def show + end + + # GET /matches/new + def new + @match = Match.new + end + + # GET /matches/1/edit + def edit + end + + # POST /matches + # POST /matches.json + def create + @match = Match.new(match_params) + + respond_to do |format| + if @match.save + format.html { redirect_to @match, notice: 'Match was successfully created.' } + format.json { render action: 'show', status: :created, location: @match } + else + format.html { render action: 'new' } + format.json { render json: @match.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /matches/1 + # PATCH/PUT /matches/1.json + def update + respond_to do |format| + if @match.update(match_params) + format.html { redirect_to @match, notice: 'Match was successfully updated.' } + format.json { head :no_content } + else + format.html { render action: 'edit' } + format.json { render json: @match.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /matches/1 + # DELETE /matches/1.json + def destroy + @match.destroy + respond_to do |format| + format.html { redirect_to matches_url } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_match + @match = Match.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def match_params + params.require(:match).permit(:r_id, :g_id, :g_stat, :r_stat, :winner_id, :win_type, :score) + end +end diff --git a/app/controllers/weights_controller.rb b/app/controllers/weights_controller.rb index 9500cd8..906bdca 100644 --- a/app/controllers/weights_controller.rb +++ b/app/controllers/weights_controller.rb @@ -10,7 +10,7 @@ class WeightsController < ApplicationController # GET /weights/1 # GET /weights/1.json def show - @wrestler = Wrestler.all + @wrestlers = Wrestler.all @tournament = Tournament.find(@weight.tournament_id) end diff --git a/app/helpers/matches_helper.rb b/app/helpers/matches_helper.rb new file mode 100644 index 0000000..cc2ef83 --- /dev/null +++ b/app/helpers/matches_helper.rb @@ -0,0 +1,2 @@ +module MatchesHelper +end diff --git a/app/models/match.rb b/app/models/match.rb new file mode 100644 index 0000000..d63c787 --- /dev/null +++ b/app/models/match.rb @@ -0,0 +1,3 @@ +class Match < ActiveRecord::Base + belongs_to :tournament +end diff --git a/app/models/tournament.rb b/app/models/tournament.rb index b9944fa..4b3c4ed 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -1,4 +1,6 @@ class Tournament < ActiveRecord::Base has_many :schools, dependent: :destroy has_many :weights, dependent: :destroy + has_many :matches, dependent: :destroy + end diff --git a/app/models/wrestler.rb b/app/models/wrestler.rb index eeaf7c6..53bc010 100644 --- a/app/models/wrestler.rb +++ b/app/models/wrestler.rb @@ -1,4 +1,5 @@ class Wrestler < ActiveRecord::Base belongs_to :school belongs_to :weight + has_many :matches end diff --git a/app/views/matches/_form.html.erb b/app/views/matches/_form.html.erb new file mode 100644 index 0000000..e85d0c4 --- /dev/null +++ b/app/views/matches/_form.html.erb @@ -0,0 +1,45 @@ +<%= form_for(@match) do |f| %> + <% if @match.errors.any? %> +
+

<%= pluralize(@match.errors.count, "error") %> prohibited this match from being saved:

+ + +
+ <% end %> + +
+ <%= f.label :r_id %>
+ <%= f.number_field :r_id %> +
+
+ <%= f.label :g_id %>
+ <%= f.number_field :g_id %> +
+
+ <%= f.label :g_stat %>
+ <%= f.text_area :g_stat %> +
+
+ <%= f.label :r_stat %>
+ <%= f.text_area :r_stat %> +
+
+ <%= f.label :winner_id %>
+ <%= f.number_field :winner_id %> +
+
+ <%= f.label :win_type %>
+ <%= f.text_field :win_type %> +
+
+ <%= f.label :score %>
+ <%= f.text_field :score %> +
+
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/matches/edit.html.erb b/app/views/matches/edit.html.erb new file mode 100644 index 0000000..38b7d70 --- /dev/null +++ b/app/views/matches/edit.html.erb @@ -0,0 +1,6 @@ +

Editing match

+ +<%= render 'form' %> + +<%= link_to 'Show', @match %> | +<%= link_to 'Back', matches_path %> diff --git a/app/views/matches/index.html.erb b/app/views/matches/index.html.erb new file mode 100644 index 0000000..8389674 --- /dev/null +++ b/app/views/matches/index.html.erb @@ -0,0 +1,39 @@ +

Listing matches

+ + + + + + + + + + + + + + + + + + + <% @matches.each do |match| %> + + + + + + + + + + + + + <% end %> + +
RGG statR statWinnerWin typeScore
<%= match.r_id %><%= match.g_id %><%= match.g_stat %><%= match.r_stat %><%= match.winner_id %><%= match.win_type %><%= match.score %><%= link_to 'Show', match %><%= link_to 'Edit', edit_match_path(match) %><%= link_to 'Destroy', match, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Match', new_match_path %> diff --git a/app/views/matches/index.json.jbuilder b/app/views/matches/index.json.jbuilder new file mode 100644 index 0000000..6e705cc --- /dev/null +++ b/app/views/matches/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@matches) do |match| + json.extract! match, :id, :r_id, :g_id, :g_stat, :r_stat, :winner_id, :win_type, :score + json.url match_url(match, format: :json) +end diff --git a/app/views/matches/new.html.erb b/app/views/matches/new.html.erb new file mode 100644 index 0000000..bd4c78c --- /dev/null +++ b/app/views/matches/new.html.erb @@ -0,0 +1,5 @@ +

New match

+ +<%= render 'form' %> + +<%= link_to 'Back', matches_path %> diff --git a/app/views/matches/show.html.erb b/app/views/matches/show.html.erb new file mode 100644 index 0000000..6eddc70 --- /dev/null +++ b/app/views/matches/show.html.erb @@ -0,0 +1,39 @@ +

<%= notice %>

+ +

+ R: + <%= @match.r_id %> +

+ +

+ G: + <%= @match.g_id %> +

+ +

+ G stat: + <%= @match.g_stat %> +

+ +

+ R stat: + <%= @match.r_stat %> +

+ +

+ Winner: + <%= @match.winner_id %> +

+ +

+ Win type: + <%= @match.win_type %> +

+ +

+ Score: + <%= @match.score %> +

+ +<%= link_to 'Edit', edit_match_path(@match) %> | +<%= link_to 'Back', matches_path %> diff --git a/app/views/matches/show.json.jbuilder b/app/views/matches/show.json.jbuilder new file mode 100644 index 0000000..85765af --- /dev/null +++ b/app/views/matches/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @match, :id, :r_id, :g_id, :g_stat, :r_stat, :winner_id, :win_type, :score, :created_at, :updated_at diff --git a/app/views/schools/show.html.erb b/app/views/schools/show.html.erb index 6d61f0f..463af33 100644 --- a/app/views/schools/show.html.erb +++ b/app/views/schools/show.html.erb @@ -42,7 +42,7 @@ - <% @wrestlers.each do |wrestler| %> + <% @wrestlers.order("weight_id asc").each do |wrestler| %> <% if wrestler.school_id == @school.id %> <%= wrestler.name %> diff --git a/app/views/weights/show.html.erb b/app/views/weights/show.html.erb index 1d1ebae..81e073e 100644 --- a/app/views/weights/show.html.erb +++ b/app/views/weights/show.html.erb @@ -21,7 +21,7 @@ - <% @wrestler.each do |wrestler| %> + <% @wrestlers.order("original_seed asc").each do |wrestler| %> <% if wrestler.weight_id == @weight.id %> <%= wrestler.name %> diff --git a/config/routes.rb b/config/routes.rb index 2bd771a..109ffb4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Wrestling::Application.routes.draw do + resources :matches + devise_for :users resources :tournaments diff --git a/db/migrate/20140124174110_create_matches.rb b/db/migrate/20140124174110_create_matches.rb new file mode 100644 index 0000000..fb24169 --- /dev/null +++ b/db/migrate/20140124174110_create_matches.rb @@ -0,0 +1,15 @@ +class CreateMatches < ActiveRecord::Migration + def change + create_table :matches do |t| + t.integer :r_id + t.integer :g_id + t.text :g_stat + t.text :r_stat + t.integer :winner_id + t.string :win_type + t.string :score + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index c3a9400..09226d4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,19 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140124021031) do +ActiveRecord::Schema.define(version: 20140124174110) do + + create_table "matches", force: true do |t| + t.integer "r_id" + t.integer "g_id" + t.text "g_stat" + t.text "r_stat" + t.integer "winner_id" + t.string "win_type" + t.string "score" + t.datetime "created_at" + t.datetime "updated_at" + end create_table "schools", force: true do |t| t.string "name" diff --git a/test/controllers/matches_controller_test.rb b/test/controllers/matches_controller_test.rb new file mode 100644 index 0000000..ac84152 --- /dev/null +++ b/test/controllers/matches_controller_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' + +class MatchesControllerTest < ActionController::TestCase + setup do + @match = matches(:one) + end + + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:matches) + end + + test "should get new" do + get :new + assert_response :success + end + + test "should create match" do + assert_difference('Match.count') do + post :create, match: { g_id: @match.g_id, g_stat: @match.g_stat, r_id: @match.r_id, r_stat: @match.r_stat, score: @match.score, win_type: @match.win_type, winner_id: @match.winner_id } + end + + assert_redirected_to match_path(assigns(:match)) + end + + test "should show match" do + get :show, id: @match + assert_response :success + end + + test "should get edit" do + get :edit, id: @match + assert_response :success + end + + test "should update match" do + patch :update, id: @match, match: { g_id: @match.g_id, g_stat: @match.g_stat, r_id: @match.r_id, r_stat: @match.r_stat, score: @match.score, win_type: @match.win_type, winner_id: @match.winner_id } + assert_redirected_to match_path(assigns(:match)) + end + + test "should destroy match" do + assert_difference('Match.count', -1) do + delete :destroy, id: @match + end + + assert_redirected_to matches_path + end +end diff --git a/test/fixtures/matches.yml b/test/fixtures/matches.yml new file mode 100644 index 0000000..35e0532 --- /dev/null +++ b/test/fixtures/matches.yml @@ -0,0 +1,19 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + r_id: 1 + g_id: 1 + g_stat: MyText + r_stat: MyText + winner_id: 1 + win_type: MyString + score: MyString + +two: + r_id: 1 + g_id: 1 + g_stat: MyText + r_stat: MyText + winner_id: 1 + win_type: MyString + score: MyString diff --git a/test/helpers/matches_helper_test.rb b/test/helpers/matches_helper_test.rb new file mode 100644 index 0000000..4cc472f --- /dev/null +++ b/test/helpers/matches_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class MatchesHelperTest < ActionView::TestCase +end diff --git a/test/models/match_test.rb b/test/models/match_test.rb new file mode 100644 index 0000000..14436b1 --- /dev/null +++ b/test/models/match_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class MatchTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end