diff --git a/app/controllers/completions_controller.rb b/app/controllers/completions_controller.rb index 77e03a8..cbd9b57 100644 --- a/app/controllers/completions_controller.rb +++ b/app/controllers/completions_controller.rb @@ -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" diff --git a/app/controllers/concerns/completions_concern.rb b/app/controllers/concerns/completions_concern.rb index db8b1e1..989cb7c 100644 --- a/app/controllers/concerns/completions_concern.rb +++ b/app/controllers/concerns/completions_concern.rb @@ -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 diff --git a/app/controllers/contestants_controller.rb b/app/controllers/contestants_controller.rb index 805b129..444acc2 100644 --- a/app/controllers/contestants_controller.rb +++ b/app/controllers/contestants_controller.rb @@ -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 diff --git a/app/controllers/contests_controller.rb b/app/controllers/contests_controller.rb index cf473ca..ecca06e 100644 --- a/app/controllers/contests_controller.rb +++ b/app/controllers/contests_controller.rb @@ -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 diff --git a/app/models/completion.rb b/app/models/completion.rb index 01c9c8e..bced86e 100644 --- a/app/models/completion.rb +++ b/app/models/completion.rb @@ -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 } diff --git a/app/models/contestant.rb b/app/models/contestant.rb index 7540ccd..cb93165 100644 --- a/app/models/contestant.rb +++ b/app/models/contestant.rb @@ -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 diff --git a/app/views/contests/scoreboard.html.slim b/app/views/contests/scoreboard.html.slim index d1615ea..a45d418 100644 --- a/app/views/contests/scoreboard.html.slim +++ b/app/views/contests/scoreboard.html.slim @@ -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 \ No newline at end of file + = contestant.completions.length + td + = contestant.display_time \ No newline at end of file diff --git a/db/migrate/20250326153650_add_display_times_to_completions.rb b/db/migrate/20250326153650_add_display_times_to_completions.rb new file mode 100644 index 0000000..5e3e2e4 --- /dev/null +++ b/db/migrate/20250326153650_add_display_times_to_completions.rb @@ -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 diff --git a/db/migrate/20250326153736_add_display_time_to_contestants.rb b/db/migrate/20250326153736_add_display_time_to_contestants.rb new file mode 100644 index 0000000..9f9cd1d --- /dev/null +++ b/db/migrate/20250326153736_add_display_time_to_contestants.rb @@ -0,0 +1,5 @@ +class AddDisplayTimeToContestants < ActiveRecord::Migration[8.0] + def change + add_column :contestants, :display_time, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 00ec1c3..5e235cd 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_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 diff --git a/test/fixtures/completions.yml b/test/fixtures/completions.yml index d365f1e..b44c878 100644 --- a/test/fixtures/completions.yml +++ b/test/fixtures/completions.yml @@ -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 # diff --git a/test/fixtures/contestants.yml b/test/fixtures/contestants.yml index 5570362..145d214 100644 --- a/test/fixtures/contestants.yml +++ b/test/fixtures/contestants.yml @@ -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 # diff --git a/test/models/completion_test.rb b/test/models/completion_test.rb index 55ac270..5bb4577 100644 --- a/test/models/completion_test.rb +++ b/test/models/completion_test.rb @@ -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 # diff --git a/test/models/contestant_test.rb b/test/models/contestant_test.rb index a4b3df4..10b7185 100644 --- a/test/models/contestant_test.rb +++ b/test/models/contestant_test.rb @@ -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 #