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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -7,6 +7,8 @@ table.table.table-striped.table-hover
| Name
th scope="col"
| Completed puzzles
th scope="col"
| Total time
tbody
- @contestants.each_with_index do |contestant, index|
tr scope="row"
@ -15,4 +17,6 @@ table.table.table-striped.table-hover
td
= contestant.name
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.
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|
t.string "name", 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 "updated_at", 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 ["contestant_id"], name: "index_completions_on_contestant_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.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "display_time"
t.index ["contest_id"], name: "index_contestants_on_contest_id"
end

View File

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

View File

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

View File

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

View File

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