From 63a88ea113d46be85eeb7d756bf61594a4bfc592 Mon Sep 17 00:00:00 2001 From: sto Date: Tue, 18 Nov 2025 11:15:23 +0100 Subject: [PATCH] Add method to update contestants through admin UI --- app/controllers/users_controller.rb | 22 ++++++++++++++++++++++ app/models/completion.rb | 6 +++++- app/policies/user_policy.rb | 4 ++++ app/views/users/index.html.slim | 3 ++- config/routes.rb | 2 ++ 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index a35a439..9fe3c88 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,6 @@ class UsersController < ApplicationController + include CompletionsConcern + before_action :set_user, only: %i[ destroy edit show update ] def index @@ -51,6 +53,26 @@ class UsersController < ApplicationController authorize @user end + def update_contestants + authorize :user + + total = 0 + updated = 0 + Contestant.all.each do |contestant| + if contestant.completions.length > 0 + total += 1 + contestant.completions.each do |completion| + completion.save + end + if extend_completions!(contestant) + updated += 1 + end + end + end + + redirect_to users_path, notice: "Updated contestants: #{updated}/#{total}" + end + private def set_user diff --git a/app/models/completion.rb b/app/models/completion.rb index a024860..56721a4 100644 --- a/app/models/completion.rb +++ b/app/models/completion.rb @@ -68,7 +68,11 @@ class Completion < ApplicationRecord self.time_seconds = arr[0].to_i end else - self.time_seconds = self.contest.duration_seconds + if self.contest.duration_seconds.present? + self.time_seconds = self.contest.duration_seconds + else + self.time_seconds = 2 * 3600 + end self.display_time_from_start = display_time(self.time_seconds) end end diff --git a/app/policies/user_policy.rb b/app/policies/user_policy.rb index e642423..bb8eacd 100644 --- a/app/policies/user_policy.rb +++ b/app/policies/user_policy.rb @@ -26,4 +26,8 @@ class UserPolicy < ApplicationPolicy def destroy? user.admin? end + + def update_contestants? + user.admin? + end end diff --git a/app/views/users/index.html.slim b/app/views/users/index.html.slim index 14c2ae1..80968df 100644 --- a/app/views/users/index.html.slim +++ b/app/views/users/index.html.slim @@ -24,4 +24,5 @@ table.table.table-striped.table-hover .row .col a.btn.btn-primary href=new_user_path - | New user \ No newline at end of file + | New user + = button_to "Update contestants", "/update_contestants", method: :post, class: "mt-3 btn btn-success" \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 8ae2cf6..2be6864 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -36,6 +36,8 @@ Rails.application.routes.draw do post "connect", to: "messages#connect" post "message", to: "messages#create" + post "update_contestants", to: "users#update_contestants" + get "public/:id", to: "contests#scoreboard" get "public/:id/offline", to: "contests#offline_new" post "public/:id/offline", to: "contests#offline_create"