Offline participation: implement completed puzzle methods
Some checks failed
CI / scan_ruby (push) Successful in 20s
CI / scan_js (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / test (push) Has been cancelled

This commit is contained in:
sto
2025-10-31 11:55:28 +01:00
parent 37a65526e4
commit ff5f387a87
13 changed files with 110 additions and 17 deletions

View File

@@ -1,7 +1,7 @@
class ContestsController < ApplicationController
before_action :set_contest, only: %i[ destroy edit show update ]
before_action :offline_setup, only: %i[ offline_new offline_create ]
skip_before_action :require_authentication, only: %i[ scoreboard offline_new offline_create ]
before_action :offline_setup, only: %i[ offline_new offline_create offline_edit offline_update offline_completed ]
skip_before_action :require_authentication, only: %i[ scoreboard offline_new offline_create offline_edit offline_update offline_completed ]
def index
authorize :contest
@@ -111,12 +111,47 @@ class ContestsController < ApplicationController
@offline.contest = @contest
@offline.start_time = Time.now()
if @offline.save
redirect_to "/public/#{@contest.friendly_id}/offline/test"
redirect_to "/public/#{@contest.friendly_id}/offline/#{@offline.generate_token_for(:token)}"
else
render :offline_new, status: :unprocessable_entity
end
end
def offline_edit
authorize @contest
@offline = Offline.find_by_token_for(:token, params[:token])
if !@offline
not_found and return
end
end
def offline_update
authorize @contest
@offline = Offline.find_by_token_for(:token, params[:token])
if !@offline
not_found and return
end
@offline.completed = true
@offline.images.attach(params[:offline][:end_image])
if @offline.save
redirect_to "/public/#{@contest.friendly_id}/offline/#{@offline.generate_token_for(:token)}/completed"
else
render :offline_edit, status: :unprocessable_entity
end
end
def offline_completed
authorize @contest
@offline = Offline.find_by_token_for(:token, params[:token])
if !@offline
not_found and return
end
end
private
def offline_setup
@@ -150,6 +185,10 @@ class ContestsController < ApplicationController
end
def offline_start_params
params.expect(offline: [ :name, :start_image ])
params.expect(offline: [ :name, :images ])
end
def offline_end_params
params.expect(offline: [ :completed, :end_image ])
end
end

View File

@@ -3,6 +3,7 @@
# Table name: offlines
#
# id :integer not null, primary key
# completed :boolean
# end_time :datetime
# name :string not null
# start_time :datetime not null
@@ -21,20 +22,25 @@
class Offline < ApplicationRecord
belongs_to :contest
has_one_attached :start_image
has_one_attached :end_image
has_many_attached :images
generates_token_for :token
validates :name, presence: true
validates :start_time, presence: true
validate :end_image_is_present
validate :start_image_is_present
def end_image_is_present
if self.completed && self.images.length < 2
errors.add(:end_image, I18n.t("activerecord.errors.models.offline.attributes.end_image.blank"))
end
end
def start_image_is_present
logger = Logger.new(STDOUT)
logger.info "TESTddfzefzef"
logger.info self.start_image.attached?
if !self.start_image.attached?
errors.add(:start_image, I18n.t("activerecord.errors.models.offline.attributes.start_image.blank"))
if !self.images.attached?
errors.add(:images, I18n.t("activerecord.errors.models.offline.attributes.start_image.blank"))
end
end
end

View File

@@ -67,6 +67,10 @@ class ContestPolicy < ApplicationPolicy
offline?
end
def offline_completed?
offline?
end
def scoreboard?
record.public
end

View File

@@ -0,0 +1 @@
| TODO

View File

@@ -0,0 +1,26 @@
= form_with model: @offline, url: "/public/#{@contest.friendly_id}/offline/#{@offline.generate_token_for(:token)}" do |form|
= form.hidden_field :completed
.row.mb-3
.col
.form-text.mb-1
= t("offlines.end_image_select")
= form.file_field :end_image, accept: "image/*", class: "form-control"
.form-text.error-message style="display: none;" id="image-error-message"
= t("puzzles.form.file_too_big")
javascript:
function setMaxUploadSize() {
const el = document.querySelector('input[type="file"]');
el.onchange = function() {
if(this.files[0].size > 2 * 1024 * 1024) {
document.getElementById('image-error-message').style.display = 'block';
this.value = "";
} else {
document.getElementById('image-error-message').style.display = 'none';
}
};
}
setMaxUploadSize();
.row.mt-4
.col
= form.submit t("helpers.buttons.end"), class: "btn btn-primary"

View File

@@ -8,7 +8,7 @@
.col
.form-text.mb-1
= t("offlines.start_image_select")
= form.file_field :start_image, accept: "image/*", class: "form-control"
= form.file_field :images, accept: "image/*", class: "form-control"
.form-text.error-message style="display: none;" id="image-error-message"
= t("puzzles.form.file_too_big")
javascript: