From 67d2ef41b341bcfa1d5d947f400fb55da33d646c Mon Sep 17 00:00:00 2001 From: sto Date: Wed, 18 Jun 2025 18:42:04 +0200 Subject: [PATCH] Add indicator for processed messages --- app/controllers/completions_controller.rb | 2 +- app/controllers/concerns/completions_concern.rb | 2 +- app/models/completion.rb | 6 +++++- app/models/contest.rb | 2 +- app/models/message.rb | 1 + app/views/completions/_form.html.slim | 1 + app/views/contests/show.html.slim | 17 +++++++++++++---- config/locales/en.yml | 3 ++- config/locales/fr.yml | 3 ++- ...50618155041_add_message_ref_to_completion.rb | 5 +++++ db/schema.rb | 5 ++++- test/fixtures/completions.yml | 3 +++ test/models/completion_test.rb | 3 +++ 13 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 db/migrate/20250618155041_add_message_ref_to_completion.rb diff --git a/app/controllers/completions_controller.rb b/app/controllers/completions_controller.rb index 0123eba..e1d66fb 100644 --- a/app/controllers/completions_controller.rb +++ b/app/controllers/completions_controller.rb @@ -70,6 +70,6 @@ class CompletionsController < ApplicationController end def completion_params - params.expect(completion: [ :display_time_from_start, :contestant_id, :puzzle_id ]) + params.expect(completion: [ :display_time_from_start, :contestant_id, :message_id, :puzzle_id ]) end end diff --git a/app/controllers/concerns/completions_concern.rb b/app/controllers/concerns/completions_concern.rb index 989cb7c..654c94d 100644 --- a/app/controllers/concerns/completions_concern.rb +++ b/app/controllers/concerns/completions_concern.rb @@ -26,6 +26,6 @@ module CompletionsConcern 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)) + contestant.update(display_time: display_time(current_time_from_start), time_seconds: current_time_from_start) end end diff --git a/app/models/completion.rb b/app/models/completion.rb index b9f7dd2..03a1eda 100644 --- a/app/models/completion.rb +++ b/app/models/completion.rb @@ -10,28 +10,32 @@ # updated_at :datetime not null # contest_id :integer not null # contestant_id :integer not null +# message_id :integer # puzzle_id :integer not null # # Indexes # # index_completions_on_contest_id (contest_id) # index_completions_on_contestant_id (contestant_id) +# index_completions_on_message_id (message_id) # index_completions_on_puzzle_id (puzzle_id) # # Foreign Keys # # contest_id (contest_id => contests.id) # contestant_id (contestant_id => contestants.id) +# message_id (message_id => messages.id) # puzzle_id (puzzle_id => puzzles.id) # class Completion < ApplicationRecord belongs_to :contest belongs_to :contestant belongs_to :puzzle + belongs_to :message, optional: true before_save :add_time_seconds - validates :display_time_from_start, presence: true, format: { with: /\A((\d\d|\d):\d\d|\d\d|\d):\d\d\z/ } + validates :display_time_from_start, presence: true, format: { with: /\A(((\d\d|\d):\d\d|\d\d|\d):\d\d|\d\d|\d)\z/ } validates :puzzle_id, uniqueness: { scope: :contestant } def add_time_seconds diff --git a/app/models/contest.rb b/app/models/contest.rb index addd177..de962e8 100644 --- a/app/models/contest.rb +++ b/app/models/contest.rb @@ -27,7 +27,7 @@ class Contest < ApplicationRecord has_many :completions, dependent: :destroy has_many :contestants, dependent: :destroy has_many :puzzles, dependent: :destroy - has_many :messages + has_many :messages, dependent: :destroy friendly_id :name, use: :slugged diff --git a/app/models/message.rb b/app/models/message.rb index db17498..d79f2a3 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -21,6 +21,7 @@ # class Message < ApplicationRecord belongs_to :contest + has_many :completions, dependent: :nullify validates :author, presence: true validates :text, presence: true diff --git a/app/views/completions/_form.html.slim b/app/views/completions/_form.html.slim index 0158edd..8029cbb 100644 --- a/app/views/completions/_form.html.slim +++ b/app/views/completions/_form.html.slim @@ -1,5 +1,6 @@ = form_with model: completion, url: url, method: method do |form| - if @message + = form.hidden_field :message_id, value: @message.id .row.mb-3 .col h4 = t("messages.singular").capitalize diff --git a/app/views/contests/show.html.slim b/app/views/contests/show.html.slim index e2f423e..931b1ec 100644 --- a/app/views/contests/show.html.slim +++ b/app/views/contests/show.html.slim @@ -18,7 +18,7 @@ = link_to "#{message_url}?token=#{@contest.generate_token_for(:token)}" .row.mb-4 - .col-6 + .col-7 .row .col h4 @@ -38,7 +38,7 @@ - @puzzles.each do |puzzle| tr.align-middle scope="row" td - = image_tag(puzzle.image, class: "img-fluid", style: "max-width: 140px;") if puzzle.image.attached? + = image_tag(puzzle.image, class: "img-fluid", style: "max-height: 48px;") if puzzle.image.attached? td = puzzle.name td @@ -57,15 +57,24 @@ table.table.table-striped.table-hover thead tr + th scope="col" style="white-space: nowrap" + = t("activerecord.attributes.message.processed") th scope="col" = t("activerecord.attributes.message.time") th scope="col" = t("activerecord.attributes.message.author") - th scope="col" + th.w-25 scope="col" = t("activerecord.attributes.message.text") + th.w-25 scope="col" tbody - @messages.each do |message| tr.align-middle scope="row" + td style="text-align: center" + - if message.completions.size > 0 + + + + td = message.display_time td @@ -81,7 +90,7 @@ = t("helpers.buttons.add_completion") td = link_to t("helpers.buttons.delete"), contest_message_path(@contest, message), data: { turbo_method: :delete }, class: "btn btn-sm btn-danger" - .col-6 + .col-5 .row .col h4 diff --git a/config/locales/en.yml b/config/locales/en.yml index ebaf11b..9a8921c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -48,6 +48,7 @@ en: name: "Name" message: author: "Author" + processed: "Processed?" text: "Content" time: "Time" puzzle: @@ -64,7 +65,7 @@ en: completion: attributes: display_time_from_start: - invalid: "Allowed formats: xx:xx:xx, x:xx:xx, xx:xx, x:xx" + invalid: "Allowed formats: xx:xx:xx, x:xx:xx, xx:xx, x:xx, xx" puzzle_id: taken: "This contestant has already completed this puzzle" csv_import: diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 69783a9..4d734df 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -19,6 +19,7 @@ fr: name: "Nom" message: author: "Auteur.ice" + processed: "Traité ?" text: "Contenu" time: "Temps" puzzle: @@ -35,7 +36,7 @@ fr: completion: attributes: display_time_from_start: - invalid: "Formats autorisés: xx:xx:xx, x:xx:xx, xx:xx, x:xx" + invalid: "Formats autorisés: xx:xx:xx, x:xx:xx, xx:xx, x:xx, xx" puzzle_id: taken: "Ce.tte participant.e a déjà complété ce puzzle" csv_import: diff --git a/db/migrate/20250618155041_add_message_ref_to_completion.rb b/db/migrate/20250618155041_add_message_ref_to_completion.rb new file mode 100644 index 0000000..0cf63c1 --- /dev/null +++ b/db/migrate/20250618155041_add_message_ref_to_completion.rb @@ -0,0 +1,5 @@ +class AddMessageRefToCompletion < ActiveRecord::Migration[8.0] + def change + add_reference :completions, :message, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 0700294..b501226 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_06_18_122655) do +ActiveRecord::Schema[8.0].define(version: 2025_06_18_155041) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -48,8 +48,10 @@ ActiveRecord::Schema[8.0].define(version: 2025_06_18_122655) do t.integer "contest_id", null: false t.string "display_time_from_start" t.string "display_relative_time" + t.integer "message_id" t.index ["contest_id"], name: "index_completions_on_contest_id" t.index ["contestant_id"], name: "index_completions_on_contestant_id" + t.index ["message_id"], name: "index_completions_on_message_id" t.index ["puzzle_id"], name: "index_completions_on_puzzle_id" end @@ -138,6 +140,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_06_18_122655) do add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "completions", "contestants" add_foreign_key "completions", "contests" + add_foreign_key "completions", "messages" add_foreign_key "completions", "puzzles" add_foreign_key "contestants", "contests" add_foreign_key "contests", "users" diff --git a/test/fixtures/completions.yml b/test/fixtures/completions.yml index b44c878..69b4b96 100644 --- a/test/fixtures/completions.yml +++ b/test/fixtures/completions.yml @@ -12,18 +12,21 @@ # updated_at :datetime not null # contest_id :integer not null # contestant_id :integer not null +# message_id :integer # puzzle_id :integer not null # # Indexes # # index_completions_on_contest_id (contest_id) # index_completions_on_contestant_id (contestant_id) +# index_completions_on_message_id (message_id) # index_completions_on_puzzle_id (puzzle_id) # # Foreign Keys # # contest_id (contest_id => contests.id) # contestant_id (contestant_id => contestants.id) +# message_id (message_id => messages.id) # puzzle_id (puzzle_id => puzzles.id) # completion_one: diff --git a/test/models/completion_test.rb b/test/models/completion_test.rb index 5bb4577..8d48a84 100644 --- a/test/models/completion_test.rb +++ b/test/models/completion_test.rb @@ -10,18 +10,21 @@ # updated_at :datetime not null # contest_id :integer not null # contestant_id :integer not null +# message_id :integer # puzzle_id :integer not null # # Indexes # # index_completions_on_contest_id (contest_id) # index_completions_on_contestant_id (contestant_id) +# index_completions_on_message_id (message_id) # index_completions_on_puzzle_id (puzzle_id) # # Foreign Keys # # contest_id (contest_id => contests.id) # contestant_id (contestant_id => contestants.id) +# message_id (message_id => messages.id) # puzzle_id (puzzle_id => puzzles.id) # require "test_helper"