Add contestant categories to contests
All checks were successful
CI / scan_ruby (push) Successful in 1m4s
CI / scan_js (push) Successful in 3m20s
CI / lint (push) Successful in 15s
CI / test (push) Successful in 2m7s

This commit is contained in:
sto
2025-07-14 14:54:55 +02:00
parent ee476ab81b
commit 502649620b
12 changed files with 204 additions and 3 deletions

View File

@@ -0,0 +1,37 @@
class CategoriesController < ApplicationController
before_action :set_contest
before_action :set_category, only: %i[ destroy]
def create
authorize @contest
@category = Category.new(category_params)
@category.contest_id = @contest.id
if @category.save
redirect_to edit_contest_path(@contest), notice: t("categories.new.notice")
else
redirect_to edit_contest_path(@contest), notice: t("categories.new.error")
end
end
def destroy
authorize @contest
@category.destroy
redirect_to edit_contest_path(@contest), notice: t("categories.destroy.notice")
end
private
def set_contest
@contest = Contest.find(params[:contest_id])
end
def set_category
@category = Category.find(params[:id])
end
def category_params
params.expect(category: [ :name ])
end
end

24
app/models/category.rb Normal file
View File

@@ -0,0 +1,24 @@
# == Schema Information
#
# Table name: categories
#
# id :integer not null, primary key
# name :string
# created_at :datetime not null
# updated_at :datetime not null
# contest_id :integer not null
#
# Indexes
#
# index_categories_on_contest_id (contest_id)
#
# Foreign Keys
#
# contest_id (contest_id => contests.id)
#
class Category < ApplicationRecord
belongs_to :contest
has_and_belongs_to_many :contestants
validates :name, presence: true
end

View File

@@ -26,6 +26,7 @@ class Contest < ApplicationRecord
extend FriendlyId
belongs_to :user
has_many :categories
has_many :completions, dependent: :destroy
has_many :contestants, dependent: :destroy
has_many :puzzles, dependent: :destroy

View File

@@ -22,6 +22,7 @@
class Contestant < ApplicationRecord
belongs_to :contest
has_many :completions, dependent: :destroy
has_and_belongs_to_many :categories
before_validation :initialize_time_seconds_if_empty

View File

@@ -1,3 +1,4 @@
h4.mt-5 = t("contests.form.general")
= form_with model: contest do |form|
.row.mb-3
.col
@@ -20,7 +21,7 @@
= form.check_box :team, class: "form-check-input"
= form.label :team
.form-text = t("activerecord.attributes.contest.team_description")
.row.mb-3
.row.mb-3 style="display: none"
.col
.form-check.form-switch
= form.check_box :allow_registration, class: "form-check-input"
@@ -28,4 +29,37 @@
.form-text = t("activerecord.attributes.contest.allow_registration_description")
.row
.col
= form.submit submit_text, class: "btn btn-primary"
= form.submit submit_text, class: "btn btn-primary"
h4.mt-5 = t("contests.form.categories")
= form_with model: Category, url: "/contests/#{@contest.id}/categories" do |form|
- if @contest.categories.size > 0
.row
.col-6
table.table.table-striped.table-hover
thead
tr
th
= t("activerecord.attributes.category.name")
th
= t("activerecord.attributes.category.contestant_count")
tbody
- @contest.categories.each do |category|
tr.align-middle scope="row"
td
= category.name
td
= category.contestants.size
td
= link_to t("helpers.buttons.delete"), contest_category_path(@contest, category), data: { turbo_method: :delete }, class: "btn btn-sm btn-danger ms-2"
.row.mt-3
.col-4
.form-floating
= form.text_field :name, autocomplete: "off", value: nil, class: "form-control"
= form.label :name, class: "required"
= t("activerecord.attributes.category.new")
.row.mt-3
.col
= form.submit t("helpers.buttons.add"), class: "btn btn-primary"