puzzle-scoreboard/app/controllers/contests_controller.rb
sto 1b34d10dee
All checks were successful
CI / scan_ruby (push) Successful in 19s
CI / scan_js (push) Successful in 13s
CI / lint (push) Successful in 13s
CI / test (push) Successful in 43s
Improve public scoreboard UI + make it responsive
2025-06-26 10:53:21 +02:00

102 lines
2.6 KiB
Ruby

class ContestsController < ApplicationController
before_action :set_contest, only: %i[ destroy edit show update ]
skip_before_action :require_authentication, only: %i[ scoreboard ]
def index
authorize :contest
@contests = current_user.contests
@title = I18n.t("contests.index.title", username: current_user.username)
end
def show
authorize @contest
@title = I18n.t("contests.show.title", name: @contest.name)
@action_name = t("helpers.buttons.edit")
@action_path = edit_contest_path(@contest)
@contestants = @contest.contestants.sort_by { |contestant| [ -contestant.completions.size, contestant.time_seconds ] }
@puzzles = @contest.puzzles.order(:id)
@messages = @contest.messages.order(:time_seconds)
set_badges
end
def edit
authorize @contest
@action_name = t("helpers.buttons.back")
@action_path = contest_path(@contest)
end
def new
authorize :contest
@contest = Contest.new
end
def create
authorize :contest
@contest = Contest.new(contest_params)
@contest.user_id = current_user.id
if @contest.save
redirect_to @contest, notice: t("contests.new.notice")
else
render :new, status: :unprocessable_entity
end
end
def update
authorize @contest
if @contest.update(contest_params)
redirect_to @contest, notice: t("contests.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
@contest.destroy
redirect_to contests_path, notice: t("contests.destroy.notice")
end
def scoreboard
@contest = Contest.find_by(slug: params[:id])
unless @contest && @contest.public
skip_authorization
not_found and return
end
authorize @contest
I18n.locale = @contest.lang
@title = I18n.t("contests.scoreboard.title", name: @contest.name)
@contestants = @contest.contestants.sort_by { |contestant| [ -contestant.completions.size, contestant.time_seconds ] }
@puzzles = @contest.puzzles.order(:id)
@action_name = t("helpers.buttons.refresh")
@action_path = "/public/#{@contest.friendly_id}"
render :scoreboard
end
private
def set_badges
@badges = []
@badges.push(t("helpers.badges.team")) if @contest.team
@badges.push(t("helpers.badges.registration")) if @contest.allow_registration
end
def set_contest
@contest = Contest.find(params[:id])
end
def contest_params
params.expect(contest: [ :lang, :name, :public, :team, :allow_registration ])
end
end