161 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			161 lines
		
	
	
		
			4.6 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
 | |
| 
 | |
|     @action_name = t("helpers.buttons.back")
 | |
|     @action_path = contest_path(@contest)
 | |
|   end
 | |
| 
 | |
|   def new
 | |
|     authorize @contest
 | |
| 
 | |
|     @action_name = t("helpers.buttons.back")
 | |
|     @action_path = contest_path(@contest)
 | |
|     @contestant = Contestant.new
 | |
|   end
 | |
| 
 | |
|   def create
 | |
|     authorize @contest
 | |
| 
 | |
|     @contestant = Contestant.new(contestant_params)
 | |
|     @contestant.contest_id = @contest.id
 | |
|     if @contestant.save
 | |
|       update_contestant_categories
 | |
|       redirect_to contest_path(@contest), notice: t("contestants.new.notice")
 | |
|     else
 | |
|       @action_name = t("helpers.buttons.back")
 | |
|       @action_path = contest_path(@contest)
 | |
|       render :new, status: :unprocessable_entity
 | |
|     end
 | |
|   end
 | |
| 
 | |
|   def update
 | |
|     authorize @contest
 | |
| 
 | |
|     if @contestant.update(contestant_params)
 | |
|       update_contestant_categories
 | |
|       redirect_to @contest, notice: t("contestants.edit.notice")
 | |
|     else
 | |
|       @action_name = t("helpers.buttons.back")
 | |
|       @action_path = contest_path(@contest)
 | |
|       render :edit, status: :unprocessable_entity
 | |
|     end
 | |
|   end
 | |
| 
 | |
|   def destroy
 | |
|     authorize @contest
 | |
| 
 | |
|     @contestant.destroy
 | |
|     redirect_to contest_path(@contest), notice: t("contestants.destroy.notice")
 | |
|   end
 | |
| 
 | |
|   def import
 | |
|     authorize @contest
 | |
| 
 | |
|     @action_name = t("helpers.buttons.back")
 | |
|     @action_path = contest_path(@contest)
 | |
| 
 | |
|     @csv_import = CsvImport.new
 | |
|   end
 | |
| 
 | |
|   def upload_csv
 | |
|     authorize @contest
 | |
| 
 | |
|     @csv_import = CsvImport.new(params.require(:csv_import).permit(:file, :separator))
 | |
|     if @csv_import.save
 | |
|       redirect_to "/contests/#{@contest.id}/import/#{@csv_import.id}"
 | |
|     else
 | |
|       @action_name = t("helpers.buttons.back")
 | |
|       @action_path = contest_path(@contest)
 | |
|       render :import, status: :unprocessable_entity
 | |
|     end
 | |
|   end
 | |
| 
 | |
|   def convert_csv
 | |
|     authorize @contest
 | |
| 
 | |
|     @action_name = t("helpers.buttons.back")
 | |
|     @action_path = contest_path(@contest)
 | |
|     @csv_import = CsvImport.find(params[:id])
 | |
|     @content = JSON.parse(@csv_import.content)
 | |
|     @form = Forms::CsvConversionForm.new
 | |
|   end
 | |
| 
 | |
|   def finalize_import
 | |
|     authorize @contest
 | |
| 
 | |
|     @csv_import = CsvImport.find(params[:id])
 | |
|     @content = JSON.parse(@csv_import.content)
 | |
|     all_params = params.require(:forms_csv_conversion_form)
 | |
|     @form = Forms::CsvConversionForm.new(params.require(:forms_csv_conversion_form).permit(:email_column, :name_column))
 | |
|     if @form.valid?
 | |
|       @content.each_with_index do |row, i|
 | |
|         if all_params["row_#{i}".to_sym] == "1"
 | |
|           if @form.email_column == -1
 | |
|             Contestant.create(name: row[@form.name_column], contest: @contest)
 | |
|           else
 | |
|             logger.info("Email")
 | |
|             Contestant.create(name: row[@form.name_column], email: row[@form.email_column], contest: @contest)
 | |
|           end
 | |
|         end
 | |
|       end
 | |
|       redirect_to contest_path(@contest), notice: t("contestants.import.notice")
 | |
|     else
 | |
|       @action_name = t("helpers.buttons.back")
 | |
|       @action_path = contest_path(@contest)
 | |
|       render :convert_csv, status: :unprocessable_entity
 | |
|     end
 | |
|   end
 | |
| 
 | |
|   def export
 | |
|     authorize @contest
 | |
| 
 | |
|     @contestants = @contest.contestants.sort_by { |contestant| [
 | |
|       -contestant.completions.where(remaining_pieces: nil).size,
 | |
|       (contestant.completions.where(remaining_pieces: nil).size == @contest.puzzles.length ? 1 : 0) * contestant.time_seconds,
 | |
|       contestant.completions.size > 0 && contestant.completions[-1].remaining_pieces ? contestant.completions[-1].remaining_pieces : 1000000,
 | |
|       contestant.time_seconds
 | |
|     ] }
 | |
| 
 | |
|     respond_to do |format|
 | |
|       format.csv do
 | |
|         response.headers["Content-Type"] = "text/csv"
 | |
|         response.headers["Content-Disposition"] = "attachment; filename=export.csv"
 | |
|       end
 | |
|     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
 | |
| 
 | |
|   def update_contestant_categories
 | |
|     @contestant.categories.clear
 | |
|     @contest.categories.each do |category|
 | |
|       logger.info(params[:contestant]["category_#{category.id}".to_sym] == "1")
 | |
|       if params[:contestant].key?("category_#{category.id}".to_sym) && params[:contestant]["category_#{category.id}".to_sym] == "1"
 | |
|         @contestant.categories << category
 | |
|       end
 | |
|     end
 | |
|     @contestant.save
 | |
|   end
 | |
| end
 |