puzzle-scoreboard/app/controllers/contestants_controller.rb
sto 7ce684ced9
Some checks failed
CI / scan_ruby (push) Successful in 15s
CI / scan_js (push) Successful in 12s
CI / lint (push) Successful in 12s
CI / test (push) Has been cancelled
Move completions methods to a concern
2025-03-23 09:25:32 +01:00

72 lines
1.4 KiB
Ruby

class ContestantsController < ApplicationController
include CompletionsConcern
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)
extend_completions!(@completions)
@completions
end
def contestant_params
params.expect(contestant: [ :email, :name ])
end
end