76 lines
1.7 KiB
Ruby
76 lines
1.7 KiB
Ruby
class CompletionsController < ApplicationController
|
|
include CompletionsConcern
|
|
|
|
before_action :set_contest
|
|
before_action :set_data, only: %i[ create edit new update ]
|
|
before_action :set_completion, only: %i[ destroy edit update ]
|
|
|
|
def edit
|
|
authorize @contest
|
|
end
|
|
|
|
def new
|
|
authorize @contest
|
|
|
|
@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)
|
|
redirect_to contest_path(@contest)
|
|
else
|
|
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)
|
|
redirect_to @contest
|
|
else
|
|
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])
|
|
else
|
|
redirect_to contest_path(@contest)
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def set_contest
|
|
@contest = Contest.find(params[:contest_id])
|
|
end
|
|
|
|
def set_data
|
|
@contestants = @contest.contestants
|
|
@puzzles = @contest.puzzles
|
|
end
|
|
|
|
def set_completion
|
|
@completion = Completion.find(params[:id])
|
|
end
|
|
|
|
def completion_params
|
|
params.expect(completion: [ :display_time_from_start, :contestant_id, :puzzle_id ])
|
|
end
|
|
end
|