mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-03-25 01:14:43 +00:00
Added a feature to generate uuid links for coaches to submit their school lineups.
This commit is contained in:
@@ -10,14 +10,19 @@ class ApplicationController < ActionController::Base
|
||||
end
|
||||
|
||||
rescue_from CanCan::AccessDenied do |exception|
|
||||
# flash[:error] = "Access denied!"
|
||||
redirect_to '/static_pages/not_allowed'
|
||||
end
|
||||
|
||||
|
||||
protected
|
||||
|
||||
# In Rails 4.2 and above
|
||||
def verified_request?
|
||||
super || valid_authenticity_token?(session, request.headers['X-XSRF-TOKEN'])
|
||||
end
|
||||
|
||||
# Override current_ability to pass school_permission_key
|
||||
# @school_permission_key needs to be defined on the controller
|
||||
def current_ability
|
||||
@current_ability ||= Ability.new(current_user, @school_permission_key)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,7 +2,7 @@ class SchoolsController < ApplicationController
|
||||
before_action :set_school, only: [:import_baumspage_roster, :show, :edit, :update, :destroy, :stats]
|
||||
before_action :check_access_director, only: [:new,:create,:destroy]
|
||||
before_action :check_access_delegate, only: [:import_baumspage_roster, :update,:edit]
|
||||
before_action :check_read_access, only: [:show]
|
||||
before_action :check_read_access, only: [:show, :stats]
|
||||
|
||||
def stats
|
||||
@tournament = @school.tournament
|
||||
@@ -93,24 +93,37 @@ class SchoolsController < ApplicationController
|
||||
end
|
||||
|
||||
def check_access_director
|
||||
if params[:tournament]
|
||||
if params[:tournament].present?
|
||||
@tournament = Tournament.find(params[:tournament])
|
||||
elsif params[:school]
|
||||
elsif params[:school].present?
|
||||
@tournament = Tournament.find(params[:school]["tournament_id"])
|
||||
elsif @school
|
||||
@tournament = @school.tournament
|
||||
elsif school_params
|
||||
@tournament = Tournament.find(school_params[:tournament_id])
|
||||
end
|
||||
|
||||
authorize! :manage, @tournament
|
||||
end
|
||||
|
||||
def check_access_delegate
|
||||
if params[:school].present?
|
||||
if school_params[:school_permission_key].present?
|
||||
@school_permission_key = params[:school_permission_key]
|
||||
end
|
||||
end
|
||||
|
||||
if params[:school_permission_key].present?
|
||||
@school_permission_key = params[:school_permission_key]
|
||||
end
|
||||
|
||||
authorize! :manage, @school
|
||||
end
|
||||
|
||||
def check_read_access
|
||||
# set @school_permission_key for use in ability
|
||||
if params[:school_permission_key].present?
|
||||
@school_permission_key = params[:school_permission_key]
|
||||
end
|
||||
|
||||
authorize! :read, @school
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
class TournamentsController < ApplicationController
|
||||
before_action :set_tournament, only: [:reset_bout_board,:calculate_team_scores,:bout_sheets,:swap,:weigh_in_sheet,:error,:teampointadjust,:remove_teampointadjust,:remove_school_delegate,:remove_delegate,:school_delegate,:delegate,:matches,:weigh_in,:weigh_in_weight,:create_custom_weights,:show,:edit,:update,:destroy,:up_matches,:no_matches,:team_scores,:brackets,:generate_matches,:bracket,:all_brackets]
|
||||
before_action :check_access_manage, only: [:reset_bout_board,:calculate_team_scores,:swap,:weigh_in_sheet,:teampointadjust,:remove_teampointadjust,:remove_school_delegate,:school_delegate,:weigh_in,:weigh_in_weight,:create_custom_weights,:update,:edit,:generate_matches,:matches]
|
||||
before_action :set_tournament, only: [:delete_school_keys, :generate_school_keys,:reset_bout_board,:calculate_team_scores,:bout_sheets,:swap,:weigh_in_sheet,:error,:teampointadjust,:remove_teampointadjust,:remove_school_delegate,:remove_delegate,:school_delegate,:delegate,:matches,:weigh_in,:weigh_in_weight,:create_custom_weights,:show,:edit,:update,:destroy,:up_matches,:no_matches,:team_scores,:brackets,:generate_matches,:bracket,:all_brackets]
|
||||
before_action :check_access_manage, only: [:delete_school_keys, :generate_school_keys,:reset_bout_board,:calculate_team_scores,:swap,:weigh_in_sheet,:teampointadjust,:remove_teampointadjust,:remove_school_delegate,:school_delegate,:weigh_in,:weigh_in_weight,:create_custom_weights,:update,:edit,:generate_matches,:matches]
|
||||
before_action :check_access_destroy, only: [:destroy,:delegate,:remove_delegate]
|
||||
before_action :check_tournament_errors, only: [:generate_matches]
|
||||
before_action :check_for_matches, only: [:up_matches,:bracket,:all_brackets]
|
||||
@@ -281,6 +281,18 @@ class TournamentsController < ApplicationController
|
||||
redirect_to tournament_path(@tournament), notice: "Successfully reset the bout board."
|
||||
end
|
||||
|
||||
def generate_school_keys
|
||||
@tournament.schools.each do |school|
|
||||
school.update(permission_key: SecureRandom.uuid)
|
||||
end
|
||||
redirect_to school_delegate_path(@tournament), notice: "School permission keys generated successfully."
|
||||
end
|
||||
|
||||
def delete_school_keys
|
||||
@tournament.schools.update_all(permission_key: nil)
|
||||
redirect_to school_delegate_path(@tournament), notice: "All school permission keys have been deleted."
|
||||
end
|
||||
|
||||
private
|
||||
# Use callbacks to share common setup or constraints between actions.
|
||||
def set_tournament
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
class WrestlersController < ApplicationController
|
||||
before_action :set_wrestler, only: [:show, :edit, :update, :destroy, :update_pool]
|
||||
before_action :check_access, only: [:new,:create,:update,:destroy,:edit,:update_pool]
|
||||
|
||||
|
||||
|
||||
before_action :check_access, only: [:new, :create, :update, :destroy, :edit, :update_pool]
|
||||
before_action :check_read_access, only: [:show]
|
||||
|
||||
# GET /wrestlers/1
|
||||
# GET /wrestlers/1.json
|
||||
@@ -16,133 +14,145 @@ class WrestlersController < ApplicationController
|
||||
# GET /wrestlers/new
|
||||
def new
|
||||
@wrestler = Wrestler.new
|
||||
if params[:school]
|
||||
@school = School.find(params[:school])
|
||||
end
|
||||
if @school
|
||||
@tournament = Tournament.find(@school.tournament_id)
|
||||
end
|
||||
if @tournament
|
||||
@weights = Weight.where(tournament_id: @tournament.id).sort_by{|w| w.max}
|
||||
end
|
||||
|
||||
@school = School.find_by(id: params[:school]) if params[:school]
|
||||
# Save the key into an instance variable so the view can use it.
|
||||
@school_permission_key = params[:school_permission_key].presence
|
||||
@tournament = @school.tournament if @school
|
||||
@weights = @tournament.weights.sort_by(&:max) if @tournament
|
||||
end
|
||||
|
||||
# GET /wrestlers/1/edit
|
||||
def edit
|
||||
@tournament = @wrestler.tournament
|
||||
@weight = @wrestler.weight
|
||||
@weights = @school.tournament.weights.sort_by{|w| w.max}
|
||||
@school = @wrestler.school
|
||||
@weights = @school.tournament.weights.sort_by(&:max)
|
||||
end
|
||||
|
||||
# POST /wrestlers
|
||||
# POST /wrestlers.json
|
||||
def create
|
||||
@wrestler = Wrestler.new(wrestler_params)
|
||||
@school = School.find(wrestler_params[:school_id])
|
||||
@weights = @school.tournament.weights
|
||||
@school = School.find_by(id: wrestler_params[:school_id])
|
||||
# IMPORTANT: Get the key from wrestler_params (not from params directly)
|
||||
@school_permission_key = wrestler_params[:school_permission_key].presence
|
||||
@weights = @school.tournament.weights if @school
|
||||
|
||||
# Remove the key from attributes so it isn’t assigned to the model.
|
||||
@wrestler = Wrestler.new(wrestler_params.except(:school_permission_key))
|
||||
|
||||
respond_to do |format|
|
||||
if @wrestler.save
|
||||
if session[:return_path]
|
||||
format.html { redirect_to session.delete(:return_path), notice: 'Wrestler was successfully created.' }
|
||||
else
|
||||
format.html { redirect_to @school, notice: 'Wrestler was successfully created.' }
|
||||
format.json { render action: 'show', status: :created, location: @wrestler }
|
||||
end
|
||||
redirect_path = session[:return_path] || school_path(@school)
|
||||
format.html { redirect_to append_permission_key(redirect_path), notice: 'Wrestler was successfully created.' }
|
||||
format.json { render :show, status: :created, location: @wrestler }
|
||||
else
|
||||
format.html { render action: 'new' }
|
||||
format.html { render :new }
|
||||
format.json { render json: @wrestler.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# PATCH/PUT /wrestlers/1
|
||||
# PATCH/PUT /wrestlers/1.json
|
||||
def update
|
||||
@tournament = @wrestler.tournament
|
||||
@weight = @wrestler.weight
|
||||
@weights = @tournament.weights.sort_by{|w| w.max}
|
||||
@school = @wrestler.school
|
||||
@weights = @tournament.weights.sort_by(&:max)
|
||||
|
||||
respond_to do |format|
|
||||
if @wrestler.update(wrestler_params)
|
||||
if session[:return_path]
|
||||
format.html { redirect_to session.delete(:return_path), notice: 'Wrestler was successfully updated.' }
|
||||
else
|
||||
format.html { redirect_to @school, notice: 'Wrestler was successfully updated.' }
|
||||
format.json { render action: 'show', status: :created, location: @wrestler }
|
||||
end
|
||||
if @wrestler.update(wrestler_params.except(:school_permission_key))
|
||||
redirect_path = session[:return_path] || school_path(@school)
|
||||
format.html { redirect_to append_permission_key(redirect_path), notice: 'Wrestler was successfully updated.' }
|
||||
format.json { render :show, status: :ok, location: @wrestler }
|
||||
else
|
||||
format.html { render action: 'edit' }
|
||||
format.html { render :edit }
|
||||
format.json { render json: @wrestler.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def update_pool
|
||||
@tournament = @wrestler.tournament
|
||||
@weight = @wrestler.weight
|
||||
@weights = @tournament.weights.sort_by{|w| w.max}
|
||||
@school = @wrestler.school
|
||||
if params[:wrestler]['pool']
|
||||
@wrestler.pool = params[:wrestler]['pool']
|
||||
respond_to do |format|
|
||||
message = "Wrestler has successfully been switched to a new pool. Matches for that weight are now in a weird state. Please re-generate matches when you are done with all of your changes."
|
||||
if @wrestler.update(wrestler_params)
|
||||
format.html { redirect_to "/tournaments/#{@tournament.id}/brackets/#{@wrestler.weight.id}/", notice: message }
|
||||
format.json { head :no_content }
|
||||
else
|
||||
format.html { render action: 'edit' }
|
||||
format.json { render json: @wrestler.errors, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /wrestlers/1
|
||||
# DELETE /wrestlers/1.json
|
||||
def destroy
|
||||
@school = @wrestler.school
|
||||
@wrestler.destroy
|
||||
message = "Wrestler was successfully deleted. This action has removed all matches. Please re-generate matches if you already had matches."
|
||||
|
||||
respond_to do |format|
|
||||
message = "Wrestler was successfully deleted. This action has removed all matches. Please re-generate matches if you already had matches."
|
||||
if session[:return_path]
|
||||
format.html { redirect_to session.delete(:return_path), notice: message }
|
||||
else
|
||||
format.html { redirect_to @school, notice: message }
|
||||
format.json { head :no_content }
|
||||
end
|
||||
redirect_path = session[:return_path] || school_path(@school)
|
||||
format.html { redirect_to append_permission_key(redirect_path), notice: message }
|
||||
format.json { head :no_content }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
# Use callbacks to share common setup or constraints between actions.
|
||||
def set_wrestler
|
||||
@wrestler = Wrestler.where(:id => params[:id]).includes(:school, :weight, :tournament, :matches).first
|
||||
|
||||
def set_wrestler
|
||||
@wrestler = Wrestler.includes(:school, :weight, :tournament, :matches).find_by(id: params[:id])
|
||||
end
|
||||
|
||||
def wrestler_params
|
||||
params.require(:wrestler).permit(:name, :school_id, :weight_id, :seed, :original_seed, :season_win,
|
||||
:season_loss, :criteria, :extra, :offical_weight, :pool, :school_permission_key)
|
||||
end
|
||||
|
||||
def check_access
|
||||
if params[:school].present?
|
||||
@school = School.find(params[:school])
|
||||
#@tournament = Tournament.find(@school.tournament.id)
|
||||
elsif params[:wrestler].present?
|
||||
if params[:wrestler]["school_id"].present?
|
||||
@school = School.find(params[:wrestler]["school_id"])
|
||||
if wrestler_params[:school_permission_key].present?
|
||||
@school_permission_key = wrestler_params[:school_permission_key]
|
||||
end
|
||||
else
|
||||
@wrestler = Wrestler.find(params[:wrestler]["id"])
|
||||
@school = @wrestler.school
|
||||
end
|
||||
elsif @wrestler
|
||||
@school = @wrestler.school
|
||||
end
|
||||
|
||||
# Never trust parameters from the scary internet, only allow the white list through.
|
||||
def wrestler_params
|
||||
params.require(:wrestler).permit(:name, :school_id, :weight_id, :seed, :original_seed, :season_win, :season_loss,:criteria,:extra,:offical_weight,:pool)
|
||||
# set @school_permission_key for use in ability
|
||||
if params[:school_permission_key].present?
|
||||
@school_permission_key = params[:school_permission_key]
|
||||
end
|
||||
def check_access
|
||||
if params[:school]
|
||||
@school = School.find(params[:school])
|
||||
#@tournament = Tournament.find(@school.tournament.id)
|
||||
elsif params[:wrestler]
|
||||
if params[:wrestler]["school_id"]
|
||||
@school = School.find(params[:wrestler]["school_id"])
|
||||
else
|
||||
@wrestler = Wrestler.find(params[:wrestler]["id"])
|
||||
@school = @wrestler.school
|
||||
end
|
||||
#@tournament = Tournament.find(@school.tournament.id)
|
||||
elsif @wrestler
|
||||
@school = @wrestler.school
|
||||
#@tournament = @wrestler.tournament
|
||||
elsif wrestler_params
|
||||
@school = School.find(wrestler_params[:school_id])
|
||||
end
|
||||
authorize! :manage, @school
|
||||
authorize! :manage, @school
|
||||
end
|
||||
|
||||
def check_read_access
|
||||
if params[:school]
|
||||
@school = School.find(params[:school])
|
||||
elsif params[:wrestler].present?
|
||||
if params[:wrestler]["school_id"].present?
|
||||
@school = School.find(params[:wrestler]["school_id"])
|
||||
else
|
||||
@wrestler = Wrestler.find(params[:wrestler]["id"])
|
||||
@school = @wrestler.school
|
||||
end
|
||||
if wrestler_params[:school_permission_key].present?
|
||||
@school_permission_key = wrestler_params[:school_permission_key]
|
||||
end
|
||||
elsif @wrestler
|
||||
@school = @wrestler.school
|
||||
end
|
||||
|
||||
# set @school_permission_key for use in ability
|
||||
if params[:school_permission_key].present?
|
||||
@school_permission_key = params[:school_permission_key]
|
||||
end
|
||||
authorize! :read, @school
|
||||
end
|
||||
|
||||
# Helper method to append school_permission_key to redirects if it exists.
|
||||
def append_permission_key(path)
|
||||
return path unless @school_permission_key.present?
|
||||
|
||||
# If path is an ActiveRecord object, convert to URL.
|
||||
path = school_path(path) if path.is_a?(School)
|
||||
uri = URI.parse(path)
|
||||
query_params = Rack::Utils.parse_nested_query(uri.query || "")
|
||||
query_params["school_permission_key"] = @school_permission_key
|
||||
uri.query = query_params.to_query
|
||||
uri.to_s
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user