68 lines
1.3 KiB
Ruby
68 lines
1.3 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
|
|
|
|
@title = "Contestant"
|
|
end
|
|
|
|
def new
|
|
authorize @contest
|
|
|
|
@contestant = Contestant.new
|
|
@title = "New contestant"
|
|
end
|
|
|
|
def create
|
|
authorize @contest
|
|
|
|
@contestant = Contestant.new(contestant_params)
|
|
@contestant.contest_id = @contest.id
|
|
if @contestant.save
|
|
redirect_to contest_path(@contest)
|
|
else
|
|
@title = "New contestant"
|
|
render :new, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def update
|
|
authorize @contest
|
|
|
|
if @contestant.update(contestant_params)
|
|
redirect_to @contest
|
|
else
|
|
@title = "Contestant"
|
|
render :edit, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
authorize @contest
|
|
|
|
@contestant.destroy
|
|
redirect_to contest_path(@contest)
|
|
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
|