Add contestants
Some checks are pending
CI / scan_ruby (push) Waiting to run
CI / scan_js (push) Waiting to run
CI / lint (push) Waiting to run
CI / test (push) Waiting to run

This commit is contained in:
sto 2025-03-20 09:19:39 +01:00
parent 658c50fd04
commit 44507bb85c
18 changed files with 145 additions and 25 deletions

View File

@ -0,0 +1,51 @@
class ContestantsController < ApplicationController
before_action :set_contest
before_action :set_contestant, only: %i[ destroy edit update]
def edit
@title = "Edit contestant"
end
def new
@contestant = Contestant.new
@title = "New contestant"
end
def create
@contestant = Contestant.new(contestant_params)
@contestant.contest_id = @contest.id
if @contestant.save
redirect_to contest_path(@contest)
else
@title = "New contestant"
render :new, status: :unprocessable_entity
end
end
def update
if @contestant.update(contestant_params)
redirect_to @contest
else
render :edit, status: :unprocessable_entity
end
end
def destroy
@contestant.destroy
redirect_to contest_path(@contest)
end
private
def set_contest
@contest = Contest.find(params[:contest_id])
end
def set_contestant
@contestant = Contestant.find(params[:id])
end
def contestant_params
params.expect(contestant: [ :email, :name ])
end
end

View File

@ -8,6 +8,7 @@ class ContestsController < ApplicationController
def show
@title = @contest.name
@contestants = @contest.contestants
@puzzles = @contest.puzzles
set_badges
end

View File

@ -1,13 +1,6 @@
class PuzzlesController < ApplicationController
before_action :set_contest
before_action :set_puzzle, only: %i[ edit destroy show update]
def index
@puzzles = Puzzle.all
end
def show
end
before_action :set_puzzle, only: %i[ destroy edit update]
def edit
@title = "Edit contest puzzle"

View File

@ -0,0 +1,2 @@
module ContestantsHelper
end

View File

@ -1,4 +1,5 @@
class Contest < ApplicationRecord
belongs_to :user
has_many :contestants, dependent: :destroy
has_many :puzzles, dependent: :destroy
end

4
app/models/contestant.rb Normal file
View File

@ -0,0 +1,4 @@
class Contestant < ApplicationRecord
belongs_to :contest
validates :name, presence: true
end

View File

@ -0,0 +1,17 @@
= form_with model: contestant, url: url, method: method do |form|
.row.mb-3
.col
.form-floating
= form.text_field :name, autocomplete: "off", class: "form-control"
= form.label :name, class: "required"
.row.mb-3
.col
.form-floating
= form.text_field :email, autocomplete: "off", class: "form-control"
= form.label :email
.form-text Optional. Fill this only if you intend to send emails through this app.
.row.mt-4
.col
- if method == :patch
= link_to "Delete", contest_contestant_path(contest, contestant), data: { turbo_method: :delete }, class: "btn btn-danger me-2"
= form.submit submit_text, class: "btn btn-primary"

View File

@ -0,0 +1 @@
= render "form", contest: @contest, contestant: @contestant, submit_text: "Save", method: :patch, url: "/contests/#{@contest.id}/contestants/#{@contestant.id}"

View File

@ -0,0 +1 @@
= render "form", contest: @contest, contestant: @contestant, submit_text: "Add", method: :post, url: "/contests/#{@contest.id}/contestants"

View File

@ -34,4 +34,21 @@
a.btn.btn-primary href=new_contest_puzzle_path(@contest)
| Add puzzle
.col-sm-6
h4 Teams
.row
.col
h4
| Contestants
.row.row-cols-1.row-cols-md-3.g-4.mb-4
- @contestants.each do |contestant|
.col
css:
.card:hover { background-color: lightblue; }
.card.h-100
.card-header
= contestant.name
.card-body
a.stretched-link href=edit_contest_contestant_path(@contest, contestant)
.row
.col
a.btn.btn-primary href=new_contest_contestant_path(@contest)
| Add contestant

View File

@ -1,8 +0,0 @@
h1 Puzzles
= link_to "New puzzle", new_puzzle_path
div
- @puzzles.each do |puzzle|
div
= link_to puzzle.name, puzzle

View File

@ -1,7 +0,0 @@
h1 = @puzzle.name
= link_to "Back", puzzles_path
= image_tag @puzzle.image if @puzzle.image.attached?
= button_to "Delete", @puzzle, method: :delete, data: { turbo_confirm: "Are you suuure??" }

View File

@ -9,6 +9,7 @@ Rails.application.routes.draw do
root "contests#index"
resources :contests do
resources :contestants
resources :puzzles
end
resources :passwords, param: :token

View File

@ -0,0 +1,11 @@
class CreateContestants < ActiveRecord::Migration[8.0]
def change
create_table :contestants do |t|
t.string :name
t.string :email
t.belongs_to :contest, null: false, foreign_key: true
t.timestamps
end
end
end

12
db/schema.rb generated
View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2025_03_20_075601) do
ActiveRecord::Schema[8.0].define(version: 2025_03_20_080142) do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
@ -39,6 +39,15 @@ ActiveRecord::Schema[8.0].define(version: 2025_03_20_075601) do
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end
create_table "contestants", force: :cascade do |t|
t.string "name"
t.string "email"
t.integer "contest_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["contest_id"], name: "index_contestants_on_contest_id"
end
create_table "contests", force: :cascade do |t|
t.string "name"
t.integer "user_id", null: false
@ -78,6 +87,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_03_20_075601) do
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "contestants", "contests"
add_foreign_key "contests", "users"
add_foreign_key "puzzles", "contests"
add_foreign_key "sessions", "users"

View File

@ -0,0 +1,7 @@
require "test_helper"
class ContestantsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

11
test/fixtures/contestants.yml vendored Normal file
View File

@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: MyString
email: MyString
contest: one
two:
name: MyString
email: MyString
contest: two

View File

@ -0,0 +1,7 @@
require "test_helper"
class ContestantTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end