Add offline model and "new" form/controller
Some checks failed
CI / scan_ruby (push) Successful in 18s
CI / scan_js (push) Successful in 15s
CI / lint (push) Failing after 14s
CI / test (push) Failing after 15m10s

This commit is contained in:
sto
2025-10-29 17:20:38 +01:00
parent a8f1ffd920
commit 35ad7da355
10 changed files with 158 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
class ContestsController < ApplicationController
before_action :set_contest, only: %i[ destroy edit show update ]
skip_before_action :require_authentication, only: %i[ scoreboard ]
skip_before_action :require_authentication, only: %i[ scoreboard offline_new ]
def index
authorize :contest
@@ -99,6 +99,21 @@ class ContestsController < ApplicationController
render :scoreboard
end
def offline_new
@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)
@offline = Offline.new
end
private
def set_badges

View File

@@ -31,6 +31,7 @@ class Contest < ApplicationRecord
has_many :contestants, dependent: :destroy
has_many :puzzles, dependent: :destroy
has_many :messages, dependent: :destroy
has_many :offlines, dependent: :destroy
friendly_id :name, use: :slugged

27
app/models/offline.rb Normal file
View File

@@ -0,0 +1,27 @@
# == Schema Information
#
# Table name: offlines
#
# id :integer not null, primary key
# end_time :datetime
# start_time :datetime not null
# created_at :datetime not null
# updated_at :datetime not null
# contest_id :integer not null
#
# Indexes
#
# index_offlines_on_contest_id (contest_id)
#
# Foreign Keys
#
# contest_id (contest_id => contests.id)
#
class Offline < ApplicationRecord
belongs_to :contest
has_one_attached :start_image
has_one_attached :end_image
validates :start_time, presence: true
end

View File

@@ -47,6 +47,22 @@ class ContestPolicy < ApplicationPolicy
record.user.id == user.id || user.admin?
end
def offline_new?
true
end
def offline_create?
true
end
def offline_edit?
true
end
def offline_update?
true
end
def scoreboard?
true
end

View File

@@ -0,0 +1,25 @@
= form_with model: @offline, url: "/public/#{@contest.id}/offline" do |form|
.row.mb-3
.col
.form-text.mb-1
= t("puzzles.image_select")
= form.file_field :start_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-3
.col
= form.submit t("helpers.buttons.add"), class: "btn btn-primary"