78 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			78 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
| class ContestantsController < ApplicationController
 | |
|   before_action :set_contest
 | |
|   before_action :set_contestant, only: %i[ destroy edit update]
 | |
|   before_action :set_completions, only: %i[edit update ]
 | |
| 
 | |
|   def edit
 | |
|     authorize @contest
 | |
|   end
 | |
| 
 | |
|   def new
 | |
|     authorize @contest
 | |
| 
 | |
|     @contestant = Contestant.new
 | |
|   end
 | |
| 
 | |
|   def create
 | |
|     authorize @contest
 | |
| 
 | |
|     @contestant = Contestant.new(contestant_params)
 | |
|     @contestant.contest_id = @contest.id
 | |
|     if @contestant.save
 | |
|       redirect_to contest_path(@contest)
 | |
|     else
 | |
|       render :new, status: :unprocessable_entity
 | |
|     end
 | |
|   end
 | |
| 
 | |
|   def update
 | |
|     authorize @contest
 | |
| 
 | |
|     if @contestant.update(contestant_params)
 | |
|       redirect_to @contest
 | |
|     else
 | |
|       render :edit, status: :unprocessable_entity
 | |
|     end
 | |
|   end
 | |
| 
 | |
|   def destroy
 | |
|     authorize @contest
 | |
| 
 | |
|     @contestant.destroy
 | |
|     redirect_to contest_path(@contest)
 | |
|   end
 | |
| 
 | |
|   def import
 | |
|     authorize @contest
 | |
| 
 | |
|     if params[:csv_import]
 | |
|       @csv_import = CsvImport.new(params.require(:csv_import).permit(:file, :separator))
 | |
|       if @csv_import.save
 | |
|         @csv_import = CsvImport.new
 | |
|       else
 | |
|         render :import, status: :unprocessable_entity
 | |
|       end
 | |
|     else
 | |
|       @csv_import = CsvImport.new
 | |
|     end
 | |
|   end
 | |
| 
 | |
|   private
 | |
| 
 | |
|   def set_contest
 | |
|     @contest = Contest.find(params[:contest_id])
 | |
|   end
 | |
| 
 | |
|   def set_contestant
 | |
|     @contestant = Contestant.find(params[:id])
 | |
|   end
 | |
| 
 | |
|   def set_completions
 | |
|     @completions = @contestant.completions.order(:time_seconds)
 | |
|   end
 | |
| 
 | |
|   def contestant_params
 | |
|     params.expect(contestant: [ :email, :name ])
 | |
|   end
 | |
| end
 |