Save display times in the db
All checks were successful
CI / scan_ruby (push) Successful in 17s
CI / scan_js (push) Successful in 11s
CI / lint (push) Successful in 12s
CI / test (push) Successful in 41s

This commit is contained in:
sto 2025-03-26 17:00:06 +01:00
parent c98caeea92
commit a5d165c4b3
14 changed files with 78 additions and 56 deletions

View File

@ -1,4 +1,6 @@
class CompletionsController < ApplicationController class CompletionsController < ApplicationController
include CompletionsConcern
before_action :set_contest before_action :set_contest
before_action :set_data, only: %i[ create edit new update ] before_action :set_data, only: %i[ create edit new update ]
before_action :set_completion, only: %i[ destroy edit update ] before_action :set_completion, only: %i[ destroy edit update ]
@ -25,6 +27,7 @@ class CompletionsController < ApplicationController
@completion = Completion.new(completion_params) @completion = Completion.new(completion_params)
@completion.contest_id = @contest.id @completion.contest_id = @contest.id
if @completion.save if @completion.save
extend_completions!(@completion.contestant)
redirect_to contest_path(@contest) redirect_to contest_path(@contest)
else else
logger = Logger.new(STDOUT) logger = Logger.new(STDOUT)
@ -41,6 +44,7 @@ class CompletionsController < ApplicationController
@completion.contestant_id = params[:contestant_id] @completion.contestant_id = params[:contestant_id]
end end
if @completion.update(completion_params) if @completion.update(completion_params)
extend_completions!(@completion.contestant)
redirect_to @contest redirect_to @contest
else else
@title = "Edit completion" @title = "Edit completion"

View File

@ -19,12 +19,13 @@ module CompletionsConcern
pad(seconds) pad(seconds)
end end
def extend_completions!(completions) def extend_completions!(contestant)
current_time_from_start = 0 current_time_from_start = 0
@completions.each do |completion| contestant.completions.order(:time_seconds).each do |completion|
completion.display_time_from_start = display_time(completion.time_seconds) completion.update(display_time_from_start: display_time(completion.time_seconds),
completion.display_relative_time = display_time(completion.time_seconds - current_time_from_start) display_relative_time: display_time(completion.time_seconds - current_time_from_start))
current_time_from_start += completion.time_seconds current_time_from_start = completion.time_seconds
end end
contestant.update(display_time: display_time(current_time_from_start))
end end
end end

View File

@ -1,6 +1,4 @@
class ContestantsController < ApplicationController class ContestantsController < ApplicationController
include CompletionsConcern
before_action :set_contest before_action :set_contest
before_action :set_contestant, only: %i[ destroy edit update] before_action :set_contestant, only: %i[ destroy edit update]
before_action :set_completions, only: %i[edit update ] before_action :set_completions, only: %i[edit update ]
@ -61,8 +59,6 @@ class ContestantsController < ApplicationController
def set_completions def set_completions
@completions = @contestant.completions.order(:time_seconds) @completions = @contestant.completions.order(:time_seconds)
extend_completions!(@completions)
@completions
end end
def contestant_params def contestant_params

View File

@ -1,6 +1,4 @@
class ContestsController < ApplicationController class ContestsController < ApplicationController
include CompletionsConcern
before_action :set_contest, only: %i[ destroy edit show update ] 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 ]
@ -70,7 +68,6 @@ class ContestsController < ApplicationController
@title = @contest.name @title = @contest.name
@contestants = @contest.contestants.order(:name) @contestants = @contest.contestants.order(:name)
@puzzles = @contest.puzzles.order(:id) @puzzles = @contest.puzzles.order(:id)
extend_completions!(@contest.completions)
render :scoreboard render :scoreboard
end end

View File

@ -2,13 +2,15 @@
# #
# Table name: completions # Table name: completions
# #
# id :integer not null, primary key # id :integer not null, primary key
# time_seconds :integer # display_relative_time :string
# created_at :datetime not null # display_time_from_start :string
# updated_at :datetime not null # time_seconds :integer
# contest_id :integer not null # created_at :datetime not null
# contestant_id :integer not null # updated_at :datetime not null
# puzzle_id :integer not null # contest_id :integer not null
# contestant_id :integer not null
# puzzle_id :integer not null
# #
# Indexes # Indexes
# #
@ -27,8 +29,6 @@ class Completion < ApplicationRecord
belongs_to :contestant belongs_to :contestant
belongs_to :puzzle belongs_to :puzzle
attr_accessor :display_time_from_start, :display_relative_time
validates :time_seconds, presence: true validates :time_seconds, presence: true
validates_numericality_of :time_seconds validates_numericality_of :time_seconds
validates :puzzle_id, uniqueness: { scope: :contestant } validates :puzzle_id, uniqueness: { scope: :contestant }

View File

@ -2,12 +2,13 @@
# #
# Table name: contestants # Table name: contestants
# #
# id :integer not null, primary key # id :integer not null, primary key
# email :string # display_time :string
# name :string # email :string
# created_at :datetime not null # name :string
# updated_at :datetime not null # created_at :datetime not null
# contest_id :integer not null # updated_at :datetime not null
# contest_id :integer not null
# #
# Indexes # Indexes
# #
@ -19,7 +20,6 @@
# #
class Contestant < ApplicationRecord class Contestant < ApplicationRecord
belongs_to :contest belongs_to :contest
has_many :completions has_many :completions
validates :name, presence: true validates :name, presence: true

View File

@ -7,6 +7,8 @@ table.table.table-striped.table-hover
| Name | Name
th scope="col" th scope="col"
| Completed puzzles | Completed puzzles
th scope="col"
| Total time
tbody tbody
- @contestants.each_with_index do |contestant, index| - @contestants.each_with_index do |contestant, index|
tr scope="row" tr scope="row"
@ -15,4 +17,6 @@ table.table.table-striped.table-hover
td td
= contestant.name = contestant.name
td td
= contestant.completions.length = contestant.completions.length
td
= contestant.display_time

View File

@ -0,0 +1,6 @@
class AddDisplayTimesToCompletions < ActiveRecord::Migration[8.0]
def change
add_column :completions, :display_time_from_start, :string
add_column :completions, :display_relative_time, :string
end
end

View File

@ -0,0 +1,5 @@
class AddDisplayTimeToContestants < ActiveRecord::Migration[8.0]
def change
add_column :contestants, :display_time, :string
end
end

5
db/schema.rb generated
View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2025_03_22_164205) do ActiveRecord::Schema[8.0].define(version: 2025_03_26_153736) do
create_table "active_storage_attachments", force: :cascade do |t| create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false t.string "name", null: false
t.string "record_type", null: false t.string "record_type", null: false
@ -46,6 +46,8 @@ ActiveRecord::Schema[8.0].define(version: 2025_03_22_164205) do
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.integer "contest_id", null: false t.integer "contest_id", null: false
t.string "display_time_from_start"
t.string "display_relative_time"
t.index ["contest_id"], name: "index_completions_on_contest_id" t.index ["contest_id"], name: "index_completions_on_contest_id"
t.index ["contestant_id"], name: "index_completions_on_contestant_id" t.index ["contestant_id"], name: "index_completions_on_contestant_id"
t.index ["puzzle_id"], name: "index_completions_on_puzzle_id" t.index ["puzzle_id"], name: "index_completions_on_puzzle_id"
@ -57,6 +59,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_03_22_164205) do
t.integer "contest_id", null: false t.integer "contest_id", null: false
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "display_time"
t.index ["contest_id"], name: "index_contestants_on_contest_id" t.index ["contest_id"], name: "index_contestants_on_contest_id"
end end

View File

@ -4,13 +4,15 @@
# #
# Table name: completions # Table name: completions
# #
# id :integer not null, primary key # id :integer not null, primary key
# time_seconds :integer # display_relative_time :string
# created_at :datetime not null # display_time_from_start :string
# updated_at :datetime not null # time_seconds :integer
# contest_id :integer not null # created_at :datetime not null
# contestant_id :integer not null # updated_at :datetime not null
# puzzle_id :integer not null # contest_id :integer not null
# contestant_id :integer not null
# puzzle_id :integer not null
# #
# Indexes # Indexes
# #

View File

@ -4,12 +4,13 @@
# #
# Table name: contestants # Table name: contestants
# #
# id :integer not null, primary key # id :integer not null, primary key
# email :string # display_time :string
# name :string # email :string
# created_at :datetime not null # name :string
# updated_at :datetime not null # created_at :datetime not null
# contest_id :integer not null # updated_at :datetime not null
# contest_id :integer not null
# #
# Indexes # Indexes
# #

View File

@ -2,13 +2,15 @@
# #
# Table name: completions # Table name: completions
# #
# id :integer not null, primary key # id :integer not null, primary key
# time_seconds :integer # display_relative_time :string
# created_at :datetime not null # display_time_from_start :string
# updated_at :datetime not null # time_seconds :integer
# contest_id :integer not null # created_at :datetime not null
# contestant_id :integer not null # updated_at :datetime not null
# puzzle_id :integer not null # contest_id :integer not null
# contestant_id :integer not null
# puzzle_id :integer not null
# #
# Indexes # Indexes
# #

View File

@ -2,12 +2,13 @@
# #
# Table name: contestants # Table name: contestants
# #
# id :integer not null, primary key # id :integer not null, primary key
# email :string # display_time :string
# name :string # email :string
# created_at :datetime not null # name :string
# updated_at :datetime not null # created_at :datetime not null
# contest_id :integer not null # updated_at :datetime not null
# contest_id :integer not null
# #
# Indexes # Indexes
# #