Add completions
This commit is contained in:
parent
44507bb85c
commit
a03907f756
66
app/controllers/completions_controller.rb
Normal file
66
app/controllers/completions_controller.rb
Normal file
@ -0,0 +1,66 @@
|
||||
class CompletionsController < ApplicationController
|
||||
before_action :set_contest
|
||||
before_action :set_data, only: %i[ create edit new update ]
|
||||
before_action :set_completion, only: %i[ destroy edit update ]
|
||||
|
||||
def edit
|
||||
@title = "Edit completion"
|
||||
end
|
||||
|
||||
def new
|
||||
@completion = Completion.new
|
||||
if params[:contestant_id]
|
||||
@completion.contestant_id = params[:contestant_id]
|
||||
end
|
||||
@title = "New completion"
|
||||
end
|
||||
|
||||
def create
|
||||
@completion = Completion.new(completion_params)
|
||||
@completion.contest_id = @contest.id
|
||||
if @completion.save
|
||||
redirect_to contest_path(@contest)
|
||||
else
|
||||
logger = Logger.new(STDOUT)
|
||||
logger.info(@completion.errors.full_messages)
|
||||
@title = "New completion"
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
if params[:contestant_id]
|
||||
@completion.contestant_id = params[:contestant_id]
|
||||
end
|
||||
if @completion.update(completion_params)
|
||||
redirect_to @contest
|
||||
else
|
||||
@title = "Edit completion"
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@completion.destroy
|
||||
redirect_to contest_path(@contest)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_contest
|
||||
@contest = Contest.find(params[:contest_id])
|
||||
end
|
||||
|
||||
def set_data
|
||||
@contestants = @contest.contestants
|
||||
@puzzles = @contest.puzzles
|
||||
end
|
||||
|
||||
def set_completion
|
||||
@completion = Completion.find(params[:id])
|
||||
end
|
||||
|
||||
def completion_params
|
||||
params.expect(completion: [ :time_seconds, :contestant_id, :puzzle_id ])
|
||||
end
|
||||
end
|
@ -1,6 +1,7 @@
|
||||
class ContestantsController < ApplicationController
|
||||
before_action :set_contest
|
||||
before_action :set_contestant, only: %i[ destroy edit update]
|
||||
before_action :set_completions, only: %i[edit update ]
|
||||
|
||||
def edit
|
||||
@title = "Edit contestant"
|
||||
@ -26,6 +27,7 @@ class ContestantsController < ApplicationController
|
||||
if @contestant.update(contestant_params)
|
||||
redirect_to @contest
|
||||
else
|
||||
@title = "Edit contestant"
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
@ -45,6 +47,10 @@ class ContestantsController < ApplicationController
|
||||
@contestant = Contestant.find(params[:id])
|
||||
end
|
||||
|
||||
def set_completions
|
||||
@completions = @contestant.completions
|
||||
end
|
||||
|
||||
def contestant_params
|
||||
params.expect(contestant: [ :email, :name ])
|
||||
end
|
||||
|
@ -26,6 +26,7 @@ class PuzzlesController < ApplicationController
|
||||
if @puzzle.update(puzzle_params)
|
||||
redirect_to @contest
|
||||
else
|
||||
@title = "Edit contest puzzle"
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
2
app/helpers/completions_helper.rb
Normal file
2
app/helpers/completions_helper.rb
Normal file
@ -0,0 +1,2 @@
|
||||
module CompletionsHelper
|
||||
end
|
7
app/models/completion.rb
Normal file
7
app/models/completion.rb
Normal file
@ -0,0 +1,7 @@
|
||||
class Completion < ApplicationRecord
|
||||
belongs_to :contest
|
||||
belongs_to :contestant
|
||||
belongs_to :puzzle
|
||||
|
||||
validates :time_seconds, presence: true
|
||||
end
|
@ -1,5 +1,7 @@
|
||||
class Contest < ApplicationRecord
|
||||
belongs_to :user
|
||||
|
||||
has_many :completions, dependent: :destroy
|
||||
has_many :contestants, dependent: :destroy
|
||||
has_many :puzzles, dependent: :destroy
|
||||
end
|
||||
|
@ -1,4 +1,7 @@
|
||||
class Contestant < ApplicationRecord
|
||||
belongs_to :contest
|
||||
|
||||
has_many :completions
|
||||
|
||||
validates :name, presence: true
|
||||
end
|
||||
|
@ -1,6 +1,9 @@
|
||||
class Puzzle < ApplicationRecord
|
||||
belongs_to :contest
|
||||
|
||||
has_many :completions
|
||||
has_one_attached :image
|
||||
|
||||
validates :name, presence: true
|
||||
validates :brand, presence: true
|
||||
end
|
||||
|
19
app/views/completions/_form.html.slim
Normal file
19
app/views/completions/_form.html.slim
Normal file
@ -0,0 +1,19 @@
|
||||
= form_with model: completion, url: url, method: method do |form|
|
||||
.row.mb-3
|
||||
.col
|
||||
.form-floating
|
||||
= form.text_field :time_seconds, autocomplete: "off", class: "form-control"
|
||||
= form.label :time_seconds, class: "required"
|
||||
.row.mb-3
|
||||
.col
|
||||
.form-floating
|
||||
= form.select :contestant_id, @contestants.map { |contestant| [contestant.name, contestant.id] }, {}, class: "form-select"
|
||||
= form.label :contestant_id
|
||||
.row.mb-3
|
||||
.col
|
||||
.form-floating
|
||||
= form.select :puzzle_id, @puzzles.map { |puzzle| ["#{puzzle.name} - #{puzzle.brand}", puzzle.id] }, {}, class: "form-select"
|
||||
= form.label :puzzle_id
|
||||
.row
|
||||
.col
|
||||
= form.submit submit_text, class: "btn btn-primary"
|
1
app/views/completions/edit.html.slim
Normal file
1
app/views/completions/edit.html.slim
Normal file
@ -0,0 +1 @@
|
||||
= render "form", contest: @contest, completion: @completion, submit_text: "Save", method: :patch, url: "/contests/#{@contest.id}/completions/#{@completion.id}"
|
1
app/views/completions/new.html.slim
Normal file
1
app/views/completions/new.html.slim
Normal file
@ -0,0 +1 @@
|
||||
= render "form", completion: @completion, submit_text: "Create", method: :post, url: "/contests/#{@contest.id}/completions"
|
@ -1,3 +1,7 @@
|
||||
.row
|
||||
.col
|
||||
h3 Informations
|
||||
|
||||
= form_with model: contestant, url: url, method: method do |form|
|
||||
.row.mb-3
|
||||
.col
|
||||
@ -14,4 +18,27 @@
|
||||
.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"
|
||||
= form.submit submit_text, class: "btn btn-primary"
|
||||
|
||||
- if method == :patch
|
||||
.row.mt-5
|
||||
.col
|
||||
h3 Completions
|
||||
table.table.table-striped.table-hover
|
||||
thead
|
||||
tr
|
||||
th scope="col"
|
||||
| Time since start
|
||||
th scope="col"
|
||||
| Puzzle
|
||||
tbody
|
||||
- @completions.each do |completion|
|
||||
tr scope="row"
|
||||
td
|
||||
= link_to completion.time_seconds, edit_contest_completion_path(@contest, completion, contestant.id)
|
||||
td
|
||||
= completion.puzzle.name
|
||||
.row
|
||||
.col
|
||||
a.btn.btn-primary href=new_contest_completion_path(@contest, contestant_id: contestant.id)
|
||||
| Add completion
|
||||
|
@ -11,7 +11,7 @@
|
||||
| Edit contest
|
||||
|
||||
.row.mb-4
|
||||
.col-sm-6
|
||||
.col-8
|
||||
.row
|
||||
.col
|
||||
h4
|
||||
@ -33,21 +33,25 @@
|
||||
.col
|
||||
a.btn.btn-primary href=new_contest_puzzle_path(@contest)
|
||||
| Add puzzle
|
||||
.col-sm-6
|
||||
.col-4
|
||||
.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)
|
||||
table.table.table-striped.table-hover
|
||||
thead
|
||||
tr
|
||||
th scope="col"
|
||||
| Name
|
||||
th scope="col"
|
||||
| Completed puzzles
|
||||
tbody
|
||||
- @contestants.each do |contestant|
|
||||
tr scope="row"
|
||||
td
|
||||
= link_to contestant.name, edit_contest_contestant_path(@contest, contestant)
|
||||
td
|
||||
= contestant.completions.length
|
||||
.row
|
||||
.col
|
||||
a.btn.btn-primary href=new_contest_contestant_path(@contest)
|
||||
|
@ -9,6 +9,7 @@ Rails.application.routes.draw do
|
||||
root "contests#index"
|
||||
|
||||
resources :contests do
|
||||
resources :completions
|
||||
resources :contestants
|
||||
resources :puzzles
|
||||
end
|
||||
|
11
db/migrate/20250320082658_create_completions.rb
Normal file
11
db/migrate/20250320082658_create_completions.rb
Normal file
@ -0,0 +1,11 @@
|
||||
class CreateCompletions < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
create_table :completions do |t|
|
||||
t.integer :time_seconds
|
||||
t.belongs_to :contestant, null: false, foreign_key: true
|
||||
t.belongs_to :puzzle, null: false, foreign_key: true
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
class AddContestRefToCompletion < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
add_reference :completions, :contest, null: false, foreign_key: true
|
||||
end
|
||||
end
|
17
db/schema.rb
generated
17
db/schema.rb
generated
@ -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_080142) do
|
||||
ActiveRecord::Schema[8.0].define(version: 2025_03_20_093759) do
|
||||
create_table "active_storage_attachments", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.string "record_type", null: false
|
||||
@ -39,6 +39,18 @@ ActiveRecord::Schema[8.0].define(version: 2025_03_20_080142) do
|
||||
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
|
||||
end
|
||||
|
||||
create_table "completions", force: :cascade do |t|
|
||||
t.integer "time_seconds"
|
||||
t.integer "contestant_id", null: false
|
||||
t.integer "puzzle_id", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "contest_id", null: false
|
||||
t.index ["contest_id"], name: "index_completions_on_contest_id"
|
||||
t.index ["contestant_id"], name: "index_completions_on_contestant_id"
|
||||
t.index ["puzzle_id"], name: "index_completions_on_puzzle_id"
|
||||
end
|
||||
|
||||
create_table "contestants", force: :cascade do |t|
|
||||
t.string "name"
|
||||
t.string "email"
|
||||
@ -87,6 +99,9 @@ ActiveRecord::Schema[8.0].define(version: 2025_03_20_080142) 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 "completions", "contestants"
|
||||
add_foreign_key "completions", "contests"
|
||||
add_foreign_key "completions", "puzzles"
|
||||
add_foreign_key "contestants", "contests"
|
||||
add_foreign_key "contests", "users"
|
||||
add_foreign_key "puzzles", "contests"
|
||||
|
7
test/controllers/completions_controller_test.rb
Normal file
7
test/controllers/completions_controller_test.rb
Normal file
@ -0,0 +1,7 @@
|
||||
require "test_helper"
|
||||
|
||||
class CompletionsControllerTest < ActionDispatch::IntegrationTest
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
11
test/fixtures/completions.yml
vendored
Normal file
11
test/fixtures/completions.yml
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
one:
|
||||
time_seconds: 1
|
||||
contestant: one
|
||||
puzzle: one
|
||||
|
||||
two:
|
||||
time_seconds: 1
|
||||
contestant: two
|
||||
puzzle: two
|
7
test/models/completion_test.rb
Normal file
7
test/models/completion_test.rb
Normal file
@ -0,0 +1,7 @@
|
||||
require "test_helper"
|
||||
|
||||
class CompletionTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user