From ad9ea9dc42a24f76607681cca02522f5e6393d1e Mon Sep 17 00:00:00 2001 From: Jacob Cody Wimer Date: Fri, 23 Oct 2015 08:53:29 -0400 Subject: [PATCH] First crack at setting an owner for the tournament --- app/controllers/matches_controller.rb | 10 ++++---- app/controllers/mats_controller.rb | 24 +++++++++---------- app/controllers/schools_controller.rb | 21 +++++++--------- app/controllers/static_pages_controller.rb | 15 ++++++++---- app/controllers/tournaments_controller.rb | 14 +++++------ app/controllers/weights_controller.rb | 22 +++++++---------- app/controllers/wrestlers_controller.rb | 21 +++++++--------- app/models/tournament.rb | 1 + app/models/user.rb | 2 ++ app/views/devise/registrations/new.html.erb | 2 -- app/views/tournaments/_form.html.erb | 1 + ...151023123932_add_user_id_to_tournaments.rb | 6 +++++ db/schema.rb | 5 +++- db/seeds.rb | 4 ++-- 14 files changed, 76 insertions(+), 72 deletions(-) create mode 100644 db/migrate/20151023123932_add_user_id_to_tournaments.rb diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb index bc3b687..c226bf9 100644 --- a/app/controllers/matches_controller.rb +++ b/app/controllers/matches_controller.rb @@ -19,13 +19,13 @@ class MatchesController < ApplicationController # GET /matches/1/edit def edit - if user_signed_in? - else - redirect_to root_path - end if params[:match] @match = Match.find (params[:match]) end + if current_user == @match.tournament.user + else + redirect_to root_path + end if @match @w1 = Wrestler.find(@match.w1) @w2 = Wrestler.find(@match.w2) @@ -55,7 +55,7 @@ class MatchesController < ApplicationController # PATCH/PUT /matches/1 # PATCH/PUT /matches/1.json def update - if user_signed_in? + if current_user == @match.tournament.user else redirect_to root_path end diff --git a/app/controllers/mats_controller.rb b/app/controllers/mats_controller.rb index fbe955f..88d0dc9 100644 --- a/app/controllers/mats_controller.rb +++ b/app/controllers/mats_controller.rb @@ -18,6 +18,9 @@ class MatsController < ApplicationController if params[:tournament] @tournament_field = params[:tournament] @tournament = Tournament.find(params[:tournament]) + if current_user != @tournament.user + redirect_to root_path + end end end @@ -29,12 +32,11 @@ class MatsController < ApplicationController # POST /mats # POST /mats.json def create - if user_signed_in? - else - redirect_to root_path - end @mat = Mat.new(mat_params) @tournament = Tournament.find(mat_params[:tournament_id]) + if current_user != @tournament.user + redirect_to root_path + end respond_to do |format| if @mat.save format.html { redirect_to @tournament, notice: 'Mat was successfully created.' } @@ -49,11 +51,10 @@ class MatsController < ApplicationController # PATCH/PUT /mats/1 # PATCH/PUT /mats/1.json def update - if user_signed_in? - else - redirect_to root_path - end @tournament = Tournament.find(@mat.tournament_id) + if current_user != @tournament.user + redirect_to root_path + end respond_to do |format| if @mat.update(mat_params) format.html { redirect_to @tournament, notice: 'Mat was successfully updated.' } @@ -68,11 +69,10 @@ class MatsController < ApplicationController # DELETE /mats/1 # DELETE /mats/1.json def destroy - if user_signed_in? - else - redirect_to root_path - end @tournament = Tournament.find(@mat.tournament_id) + if current_user != @tournament.user + redirect_to root_path + end @mat.destroy respond_to do |format| format.html { redirect_to @tournament } diff --git a/app/controllers/schools_controller.rb b/app/controllers/schools_controller.rb index 42f647f..4715e7a 100644 --- a/app/controllers/schools_controller.rb +++ b/app/controllers/schools_controller.rb @@ -32,12 +32,11 @@ class SchoolsController < ApplicationController # POST /schools # POST /schools.json def create - if user_signed_in? - else - redirect_to root_path - end @school = School.new(school_params) @tournament = Tournament.find(school_params[:tournament_id]) + if current_user != @tournament.user + redirect_to root_path + end respond_to do |format| if @school.save format.html { redirect_to @tournament, notice: 'School was successfully created.' } @@ -52,11 +51,10 @@ class SchoolsController < ApplicationController # PATCH/PUT /schools/1 # PATCH/PUT /schools/1.json def update - if user_signed_in? - else - redirect_to root_path - end @tournament = Tournament.find(@school.tournament_id) + if current_user != @tournament.user + redirect_to root_path + end respond_to do |format| if @school.update(school_params) format.html { redirect_to @tournament, notice: 'School was successfully updated.' } @@ -71,11 +69,10 @@ class SchoolsController < ApplicationController # DELETE /schools/1 # DELETE /schools/1.json def destroy - if user_signed_in? - else - redirect_to root_path - end @tournament = Tournament.find(@school.tournament_id) + if current_user != @tournament.user + redirect_to root_path + end @school.destroy respond_to do |format| format.html { redirect_to @tournament } diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index eb400de..77e45f9 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -68,12 +68,11 @@ class StaticPagesController < ApplicationController end def createCustomWeights - if user_signed_in? - else - redirect_to root_path - end @tournament = Tournament.find(params[:tournament]) - @custom = params[:customValue].to_s + if current_user != @tournament.user + redirect_to root_path + end + @custom = params[:customValue].to_s @tournament.createCustomWeights(@custom) redirect_to "/tournaments/#{@tournament.id}" @@ -92,6 +91,9 @@ class StaticPagesController < ApplicationController elsif user_signed_in? if params[:tournament] @tournament = Tournament.find(params[:tournament]) + if current_user != @tournament.user + redirect_to root_path + end end if @tournament @tournament.generateMatchups @@ -108,6 +110,9 @@ class StaticPagesController < ApplicationController end if params[:tournament] @tournament = Tournament.find(params[:tournament]) + if current_user != @tournament.user + redirect_to root_path + end @tournament_id = @tournament.id @tournament_name = @tournament.name end diff --git a/app/controllers/tournaments_controller.rb b/app/controllers/tournaments_controller.rb index 364e121..324802b 100644 --- a/app/controllers/tournaments_controller.rb +++ b/app/controllers/tournaments_controller.rb @@ -46,10 +46,9 @@ class TournamentsController < ApplicationController # PATCH/PUT /tournaments/1 # PATCH/PUT /tournaments/1.json def update - if user_signed_in? - else - redirect_to root_path - end + if current_user != @tournament.user + redirect_to root_path + end respond_to do |format| if @tournament.update(tournament_params) format.html { redirect_to @tournament, notice: 'Tournament was successfully updated.' } @@ -64,10 +63,9 @@ class TournamentsController < ApplicationController # DELETE /tournaments/1 # DELETE /tournaments/1.json def destroy - if user_signed_in? - else - redirect_to root_path - end + if current_user != @tournament.user + redirect_to root_path + end @tournament.destroy respond_to do |format| format.html { redirect_to tournaments_url } diff --git a/app/controllers/weights_controller.rb b/app/controllers/weights_controller.rb index 12a0c34..6fc3f3c 100644 --- a/app/controllers/weights_controller.rb +++ b/app/controllers/weights_controller.rb @@ -37,13 +37,11 @@ class WeightsController < ApplicationController # POST /weights # POST /weights.json def create - if user_signed_in? - else - redirect_to root_path - end @weight = Weight.new(weight_params) @tournament = Tournament.find(weight_params[:tournament_id]) - + if current_user != @tournament.user + redirect_to root_path + end respond_to do |format| if @weight.save format.html { redirect_to @tournament, notice: 'Weight was successfully created.' } @@ -58,11 +56,10 @@ class WeightsController < ApplicationController # PATCH/PUT /weights/1 # PATCH/PUT /weights/1.json def update - if user_signed_in? - else - redirect_to root_path - end @tournament = Tournament.find(@weight.tournament_id) + if current_user != @tournament.user + redirect_to root_path + end respond_to do |format| if @weight.update(weight_params) format.html { redirect_to @tournament, notice: 'Weight was successfully updated.' } @@ -77,11 +74,10 @@ class WeightsController < ApplicationController # DELETE /weights/1 # DELETE /weights/1.json def destroy - if user_signed_in? - else - redirect_to root_path - end @tournament = Tournament.find(@weight.tournament_id) + if current_user != @tournament.user + redirect_to root_path + end @weight.destroy respond_to do |format| format.html { redirect_to @tournament } diff --git a/app/controllers/wrestlers_controller.rb b/app/controllers/wrestlers_controller.rb index be87ce9..8454a32 100644 --- a/app/controllers/wrestlers_controller.rb +++ b/app/controllers/wrestlers_controller.rb @@ -43,11 +43,10 @@ class WrestlersController < ApplicationController # POST /wrestlers # POST /wrestlers.json def create - if user_signed_in? - else - redirect_to root_path - end @wrestler = Wrestler.new(wrestler_params) + if current_user != @wrestler.tournament.user + redirect_to root_path + end @school = School.find(wrestler_params[:school_id]) respond_to do |format| if @wrestler.save @@ -63,10 +62,9 @@ class WrestlersController < ApplicationController # PATCH/PUT /wrestlers/1 # PATCH/PUT /wrestlers/1.json def update - if user_signed_in? - else - redirect_to root_path - end + if current_user != @wrestler.tournament.user + redirect_to root_path + end @school = School.find(@wrestler.school_id) respond_to do |format| if @wrestler.update(wrestler_params) @@ -82,10 +80,9 @@ class WrestlersController < ApplicationController # DELETE /wrestlers/1 # DELETE /wrestlers/1.json def destroy - if user_signed_in? - else - redirect_to root_path - end + if current_user != @wrestler.tournament.user + redirect_to root_path + end @school = School.find(@wrestler.school_id) @wrestler.destroy respond_to do |format| diff --git a/app/models/tournament.rb b/app/models/tournament.rb index da20537..df4bb9e 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -8,6 +8,7 @@ class Tournament < ActiveRecord::Base has_many :mats, dependent: :destroy has_many :wrestlers, through: :weights has_many :matches, dependent: :destroy + belongs_to :user def tournament_types ["Pool to bracket"] diff --git a/app/models/user.rb b/app/models/user.rb index c822027..abb45d7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,8 @@ class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable + has_many :tournaments + devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable end diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index 22af7b3..67e15e9 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -1,5 +1,4 @@

Sign up

-<% if user_signed_in? %> <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %> <%= devise_error_messages! %> @@ -13,7 +12,6 @@ <%= f.password_field :password_confirmation %>
<%= f.submit "Sign up" %>
- <% end %> <% end %> <%= render "devise/shared/links" %> diff --git a/app/views/tournaments/_form.html.erb b/app/views/tournaments/_form.html.erb index edb6cb0..2f92f85 100644 --- a/app/views/tournaments/_form.html.erb +++ b/app/views/tournaments/_form.html.erb @@ -33,6 +33,7 @@

+ <%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.submit 'Submit',:class=>"btn btn-success" %>
diff --git a/db/migrate/20151023123932_add_user_id_to_tournaments.rb b/db/migrate/20151023123932_add_user_id_to_tournaments.rb new file mode 100644 index 0000000..66926a3 --- /dev/null +++ b/db/migrate/20151023123932_add_user_id_to_tournaments.rb @@ -0,0 +1,6 @@ +class AddUserIdToTournaments < ActiveRecord::Migration + def change + add_column :tournaments, :user_id, :integer + add_index :tournaments, :user_id + end +end diff --git a/db/schema.rb b/db/schema.rb index e9443cb..466c9d4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150630170119) do +ActiveRecord::Schema.define(version: 20151023123932) do create_table "matches", force: :cascade do |t| t.integer "w1" @@ -66,8 +66,11 @@ ActiveRecord::Schema.define(version: 20150630170119) do t.datetime "updated_at" t.text "tournament_type" t.text "weigh_in_ref" + t.integer "user_id" end + add_index "tournaments", ["user_id"], name: "index_tournaments_on_user_id" + create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false diff --git a/db/seeds.rb b/db/seeds.rb index 1d4bac2..ce18142 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -6,8 +6,8 @@ # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # Mayor.create(name: 'Emanuel', city: cities.first) if Rails.env.development? - User.create(email: 'test@test.com', password: 'password', password_confirmation: 'password') - Tournament.create(id: 200, name: 'test', address: 'some place', director: 'some guy', director_email: 'hismail@email.com', tournament_type: 'Pool to bracket') + User.create(id: 1, email: 'test@test.com', password: 'password', password_confirmation: 'password') + Tournament.create(id: 200, name: 'test', address: 'some place', director: 'some guy', director_email: 'hismail@email.com', tournament_type: 'Pool to bracket', user_id: 1) School.create(id: 200, name: 'Central Crossing', tournament_id: 200) School.create(id: 201, name: 'Turd Town', tournament_id: 200) School.create(id: 202, name: 'Shit Show', tournament_id: 200)