Save display times in the db
This commit is contained in:
parent
c98caeea92
commit
a5d165c4b3
@ -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"
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -3,6 +3,8 @@
|
||||
# Table name: completions
|
||||
#
|
||||
# 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
|
||||
@ -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 }
|
||||
|
@ -3,6 +3,7 @@
|
||||
# Table name: contestants
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# display_time :string
|
||||
# email :string
|
||||
# name :string
|
||||
# created_at :datetime not null
|
||||
@ -19,7 +20,6 @@
|
||||
#
|
||||
class Contestant < ApplicationRecord
|
||||
belongs_to :contest
|
||||
|
||||
has_many :completions
|
||||
|
||||
validates :name, presence: true
|
||||
|
@ -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"
|
||||
@ -16,3 +18,5 @@ table.table.table-striped.table-hover
|
||||
= contestant.name
|
||||
td
|
||||
= contestant.completions.length
|
||||
td
|
||||
= contestant.display_time
|
@ -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
|
@ -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
5
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_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
|
||||
|
||||
|
2
test/fixtures/completions.yml
vendored
2
test/fixtures/completions.yml
vendored
@ -5,6 +5,8 @@
|
||||
# Table name: completions
|
||||
#
|
||||
# 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
|
||||
|
1
test/fixtures/contestants.yml
vendored
1
test/fixtures/contestants.yml
vendored
@ -5,6 +5,7 @@
|
||||
# Table name: contestants
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# display_time :string
|
||||
# email :string
|
||||
# name :string
|
||||
# created_at :datetime not null
|
||||
|
@ -3,6 +3,8 @@
|
||||
# Table name: completions
|
||||
#
|
||||
# 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
|
||||
|
@ -3,6 +3,7 @@
|
||||
# Table name: contestants
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# display_time :string
|
||||
# email :string
|
||||
# name :string
|
||||
# created_at :datetime not null
|
||||
|
Loading…
x
Reference in New Issue
Block a user