115 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			115 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
class CompletionsController < ApplicationController
 | 
						|
  include CompletionsConcern
 | 
						|
 | 
						|
  before_action :set_contest
 | 
						|
  before_action :set_contestant
 | 
						|
  before_action :set_data, only: %i[ create edit new update ]
 | 
						|
  before_action :set_completion, only: %i[ destroy edit update ]
 | 
						|
 | 
						|
  def edit
 | 
						|
    authorize @contest
 | 
						|
 | 
						|
    if @contestant
 | 
						|
      @action_name = t("helpers.buttons.back_to_contestant")
 | 
						|
      @action_path = edit_contest_contestant_path(@contest, @contestant)
 | 
						|
    end
 | 
						|
  end
 | 
						|
 | 
						|
  def new
 | 
						|
    authorize @contest
 | 
						|
 | 
						|
    if @contestant
 | 
						|
      @action_name = t("helpers.buttons.back_to_contestant")
 | 
						|
      @action_path = edit_contest_contestant_path(@contest, @contestant)
 | 
						|
    end
 | 
						|
 | 
						|
    @completion = Completion.new
 | 
						|
    if params[:contestant_id]
 | 
						|
      @completion.contestant_id = params[:contestant_id]
 | 
						|
    end
 | 
						|
  end
 | 
						|
 | 
						|
  def create
 | 
						|
    authorize @contest
 | 
						|
 | 
						|
    @completion = Completion.new(completion_params)
 | 
						|
    @completion.contest_id = @contest.id
 | 
						|
    if @completion.save
 | 
						|
      extend_completions!(@completion.contestant)
 | 
						|
      if @contestant && !params[:completion].key?(:message_id)
 | 
						|
        redirect_to edit_contest_contestant_path(@contest, @contestant), notice: t("completions.new.notice")
 | 
						|
      else
 | 
						|
        redirect_to @contest, notice: t("completions.new.notice")
 | 
						|
      end
 | 
						|
    else
 | 
						|
      if params[:completion].key?(:message_id)
 | 
						|
        @message = Message.find(params[:completion][:message_id])
 | 
						|
        @action_name = t("helpers.buttons.back")
 | 
						|
        @action_path = contest_path(@contest)
 | 
						|
      elsif @contestant
 | 
						|
        @action_name = t("helpers.buttons.back_to_contestant")
 | 
						|
        @action_path = edit_contest_contestant_path(@contest, @contestant)
 | 
						|
      end
 | 
						|
      render :new, status: :unprocessable_entity
 | 
						|
    end
 | 
						|
  end
 | 
						|
 | 
						|
  def update
 | 
						|
    authorize @contest
 | 
						|
 | 
						|
    @completion.contestant_id = params[:contestant_id] if params[:contestant_id]
 | 
						|
    if @completion.update(completion_params)
 | 
						|
      extend_completions!(@completion.contestant)
 | 
						|
      if @contestant
 | 
						|
        redirect_to edit_contest_contestant_path(@contest, @contestant), notice: t("completions.edit.notice")
 | 
						|
      else
 | 
						|
        redirect_to @contest, notice: t("completions.edit.notice")
 | 
						|
      end
 | 
						|
    else
 | 
						|
      if @contestant
 | 
						|
        @action_name = t("helpers.buttons.back_to_contestant")
 | 
						|
        @action_path = edit_contest_contestant_path(@contest, @contestant)
 | 
						|
      end
 | 
						|
      render :edit, status: :unprocessable_entity
 | 
						|
    end
 | 
						|
  end
 | 
						|
 | 
						|
  def destroy
 | 
						|
    authorize @contest
 | 
						|
 | 
						|
    @completion.destroy
 | 
						|
    if params[:contestant_id]
 | 
						|
      redirect_to contest_contestant_path(@contest, params[:contestant_id]), notice: t("completions.destroy.notice")
 | 
						|
    else
 | 
						|
      redirect_to contest_path(@contest), notice: t("completions.destroy.notice")
 | 
						|
    end
 | 
						|
  end
 | 
						|
 | 
						|
  private
 | 
						|
 | 
						|
  def set_contest
 | 
						|
    @contest = Contest.find(params[:contest_id])
 | 
						|
  end
 | 
						|
 | 
						|
  def set_contestant
 | 
						|
    if params.key?(:contestant_id)
 | 
						|
      @contestant = Contestant.find(params[:contestant_id])
 | 
						|
    elsif params[:completion].key?(:contestant_id)
 | 
						|
      @contestant = Contestant.find(params[:completion][:contestant_id])
 | 
						|
    end
 | 
						|
  end
 | 
						|
 | 
						|
  def set_data
 | 
						|
    @contestants = @contest.contestants.order(:name)
 | 
						|
    @puzzles = @contest.puzzles
 | 
						|
  end
 | 
						|
 | 
						|
  def set_completion
 | 
						|
    @completion = Completion.find(params[:id])
 | 
						|
  end
 | 
						|
 | 
						|
  def completion_params
 | 
						|
    params.expect(completion: [ :display_time_from_start, :remaining_pieces, :contestant_id, :message_id, :puzzle_id ])
 | 
						|
  end
 | 
						|
end
 |