diff --git a/app/controllers/contests_controller.rb b/app/controllers/contests_controller.rb index 26a2100..7757577 100644 --- a/app/controllers/contests_controller.rb +++ b/app/controllers/contests_controller.rb @@ -15,7 +15,7 @@ class ContestsController < ApplicationController @title = I18n.t("contests.show.title", name: @contest.name) @action_name = t("helpers.buttons.edit") @action_path = edit_contest_path(@contest) - @contestants = @contest.contestants.order(:name) + @contestants = @contest.contestants.sort_by { |contestant| [ -contestant.completions.size, contestant.time_seconds ] } @puzzles = @contest.puzzles.order(:id) @messages = @contest.messages.order(:time_seconds) set_badges @@ -66,7 +66,7 @@ class ContestsController < ApplicationController authorize @contest @title = I18n.t("contests.scoreboard.title", name: @contest.name) - @contestants = @contest.contestants.order(:name) + @contestants = @contest.contestants.sort_by { |contestant| [ -contestant.completions.size, contestant.time_seconds ] } @puzzles = @contest.puzzles.order(:id) render :scoreboard end diff --git a/app/models/contestant.rb b/app/models/contestant.rb index cb93165..2b10992 100644 --- a/app/models/contestant.rb +++ b/app/models/contestant.rb @@ -6,6 +6,7 @@ # display_time :string # email :string # name :string +# time_seconds :integer # created_at :datetime not null # updated_at :datetime not null # contest_id :integer not null @@ -22,5 +23,14 @@ class Contestant < ApplicationRecord belongs_to :contest has_many :completions + before_create :initialize_time_seconds + validates :name, presence: true + validates :time_seconds, presence: true + + private + + def initialize_time_seconds + self.time_seconds = 0 + end end diff --git a/db/migrate/20250618122655_add_time_seconds_to_contestant.rb b/db/migrate/20250618122655_add_time_seconds_to_contestant.rb new file mode 100644 index 0000000..3da1424 --- /dev/null +++ b/db/migrate/20250618122655_add_time_seconds_to_contestant.rb @@ -0,0 +1,15 @@ +class AddTimeSecondsToContestant < ActiveRecord::Migration[8.0] + def change + add_column :contestants, :time_seconds, :integer + + Contestant.find_each do |contestant| + contestant.time_seconds = 0 + contestant.completions.each do |completion| + contestant.time_seconds += completion.time_seconds + end + contestant.save + end + + change_column_null :contestants, :time_seconds, true + end +end diff --git a/db/schema.rb b/db/schema.rb index dee96f3..0700294 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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_05_17_131707) do +ActiveRecord::Schema[8.0].define(version: 2025_06_18_122655) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -60,6 +60,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_05_17_131707) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "display_time" + t.integer "time_seconds" t.index ["contest_id"], name: "index_contestants_on_contest_id" end diff --git a/test/fixtures/contestants.yml b/test/fixtures/contestants.yml index 145d214..47c2a50 100644 --- a/test/fixtures/contestants.yml +++ b/test/fixtures/contestants.yml @@ -8,6 +8,7 @@ # display_time :string # email :string # name :string +# time_seconds :integer # created_at :datetime not null # updated_at :datetime not null # contest_id :integer not null diff --git a/test/models/contestant_test.rb b/test/models/contestant_test.rb index 10b7185..cdc1230 100644 --- a/test/models/contestant_test.rb +++ b/test/models/contestant_test.rb @@ -6,6 +6,7 @@ # display_time :string # email :string # name :string +# time_seconds :integer # created_at :datetime not null # updated_at :datetime not null # contest_id :integer not null