Offline participation form: ask for missing/remaining pieces
All checks were successful
CI / scan_ruby (push) Successful in 18s
CI / scan_js (push) Successful in 14s
CI / lint (push) Successful in 15s
CI / test (push) Successful in 30s

This commit is contained in:
sto
2025-11-08 09:55:56 +01:00
parent 69a82f4d3f
commit d1df551a0c
12 changed files with 118 additions and 35 deletions

View File

@@ -35,7 +35,9 @@ class CompletionsController < ApplicationController
@completion = Completion.new(completion_params) @completion = Completion.new(completion_params)
@completion.contest_id = @contest.id @completion.contest_id = @contest.id
if @completion.save if @completion.save
extend_completions!(@completion.contestant) if @completion.display_time_from_start.present?
extend_completions!(@completion.contestant)
end
if @contestant && !params[:completion].key?(:message_id) if @contestant && !params[:completion].key?(:message_id)
redirect_to edit_contest_contestant_path(@contest, @contestant), notice: t("completions.new.notice") redirect_to edit_contest_contestant_path(@contest, @contestant), notice: t("completions.new.notice")
else else

View File

@@ -147,6 +147,8 @@ class ContestsController < ApplicationController
@offline.completed = true @offline.completed = true
@offline.end_time = Time.now() @offline.end_time = Time.now()
@offline.images.attach(params[:offline][:end_image]) @offline.images.attach(params[:offline][:end_image])
@offline.missing_pieces = params[:offline][:missing_pieces]
@offline.remaining_pieces = params[:offline][:remaining_pieces]
if @offline.save if @offline.save
if @contest.puzzles.length > 0 if @contest.puzzles.length > 0
dp = display_time(@offline.end_time.to_i - @offline.start_time.to_i) dp = display_time(@offline.end_time.to_i - @offline.start_time.to_i)
@@ -209,6 +211,6 @@ class ContestsController < ApplicationController
end end
def offline_end_params def offline_end_params
params.expect(offline: [ :completed, :end_image ]) params.expect(offline: [ :completed, :end_image, :remaining_pieces, :missing_pieces ])
end end
end end

View File

@@ -34,16 +34,17 @@ class Completion < ApplicationRecord
belongs_to :puzzle belongs_to :puzzle
belongs_to :message, optional: true belongs_to :message, optional: true
before_save :add_time_seconds before_save :add_time_seconds, if: -> { display_time_from_start.present? }
before_save :nullify_display_time before_save :nullify_display_time
validates :display_time_from_start, presence: true, format: { with: /\A(((\d\d|\d):\d\d|\d\d|\d):\d\d|\d\d|\d)\z/ }, if: -> { remaining_pieces == nil } validates :display_time_from_start, presence: true, format: { with: /\A(((\d\d|\d):\d\d|\d\d|\d):\d\d|\d\d|\d)\z/ }, if: -> { remaining_pieces == nil }
validates :contestant_id, uniqueness: { scope: :puzzle }, if: -> { contest.puzzles.size == 1 } validates :contestant_id, uniqueness: { scope: :puzzle }, if: -> { contest.puzzles.size == 1 }
validates :puzzle_id, uniqueness: { scope: :contestant }, if: -> { contest.puzzles.size > 1 } validates :puzzle_id, uniqueness: { scope: :contestant }, if: -> { contest.puzzles.size > 1 }
validate :remaining_pieces_is_correct validates :remaining_pieces, numericality: { only_integer: true }, if: -> { remaining_pieces.present? }
validate :remaining_pieces_is_correct, if: -> { remaining_pieces.present? }
def remaining_pieces_is_correct def remaining_pieces_is_correct
if self.remaining_pieces && self.remaining_pieces > self.puzzle.pieces if self.remaining_pieces > self.puzzle.pieces
errors.add(:remaining_pieces, "Cannot be greater than the number of pieces for this puzzle") errors.add(:remaining_pieces, "Cannot be greater than the number of pieces for this puzzle")
end end
end end

View File

@@ -2,15 +2,17 @@
# #
# Table name: offlines # Table name: offlines
# #
# id :integer not null, primary key # id :integer not null, primary key
# completed :boolean # completed :boolean
# end_time :datetime # end_time :datetime
# name :string not null # missing_pieces :integer
# start_time :datetime not null # name :string not null
# created_at :datetime not null # remaining_pieces :integer
# updated_at :datetime not null # start_time :datetime not null
# contest_id :integer not null # created_at :datetime not null
# contestant_id :integer # updated_at :datetime not null
# contest_id :integer not null
# contestant_id :integer
# #
# Indexes # Indexes
# #
@@ -36,7 +38,27 @@ class Offline < ApplicationRecord
validate :end_image_is_present validate :end_image_is_present
validate :start_image_is_present validate :start_image_is_present
validates :missing_pieces, numericality: { only_integer: true }, if: -> { missing_pieces.present? }
validates :remaining_pieces, numericality: { only_integer: true }, if: -> { remaining_pieces.present? }
validate :missing_pieces_is_correct, if: -> { missing_pieces.present? }
validate :remaining_pieces_is_correct, if: -> { remaining_pieces.present? }
def missing_pieces_is_correct
if self.contest.puzzles.length > 0 && self.missing_pieces > self.contest.puzzles[0].pieces
errors.add(:remaining_pieces, "Cannot be greater than the number of pieces for this puzzle")
end
end
def remaining_pieces_is_correct
if self.contest.puzzles.length > 0 && self.remaining_pieces > self.contest.puzzles[0].pieces
errors.add(:remaining_pieces, "Cannot be greater than the number of pieces for this puzzle")
end
end
def end_image_is_present def end_image_is_present
logger = Logger.new(STDOUT)
logger.info(self.missing_pieces)
logger.info(self.missing_pieces.present?)
if self.completed && self.images.length < 2 if self.completed && self.images.length < 2
errors.add(:end_image, I18n.t("activerecord.errors.models.offline.attributes.end_image.blank")) errors.add(:end_image, I18n.t("activerecord.errors.models.offline.attributes.end_image.blank"))
end end

View File

@@ -35,6 +35,20 @@
} }
setMaxUploadSize(); setMaxUploadSize();
.row.mt-4.mb-3
.col
.form-floating
= form.text_field :missing_pieces, autocomplete: "off", class: "form-control"
= form.label :missing_pieces
.form-text
= t("offlines.form.missing_pieces")
.row.mb-3
.col
.form-floating
= form.text_field :remaining_pieces, autocomplete: "off", class: "form-control"
= form.label :remaining_pieces
.form-text
= t("offlines.form.remaining_pieces")
.row.mt-4 .row.mt-4
.col .col
= form.submit t("helpers.buttons.end"), class: "btn btn-primary" = form.submit t("helpers.buttons.end"), class: "btn btn-primary"

View File

@@ -78,6 +78,8 @@ en:
time: Time time: Time
offline: offline:
name: Your name name: Your name
missing_pieces: Missing pieces
remaining_pieces: Remaining pieces
puzzle: puzzle:
brand: Brand brand: Brand
image: Image image: Image
@@ -102,6 +104,9 @@ en:
invalid: "Allowed formats: xx:xx:xx, x:xx:xx, xx:xx, x:xx, xx" invalid: "Allowed formats: xx:xx:xx, x:xx:xx, xx:xx, x:xx, xx"
puzzle_id: puzzle_id:
taken: "This contestant has already completed this puzzle" taken: "This contestant has already completed this puzzle"
remaining_pieces:
not_an_integer: This is not an integer
not_a_number: This is not an integer
contest: contest:
attributes: attributes:
name: name:
@@ -120,8 +125,14 @@ en:
attributes: attributes:
end_image: end_image:
blank: Please upload an image blank: Please upload an image
missing_pieces:
not_an_integer: This is not an integer
not_a_number: This is not an integer
name: name:
blank: Please enter a name for your participation blank: Please enter a name for your participation
remaining_pieces:
not_an_integer: This is not an integer
not_a_number: This is not an integer
start_image: start_image:
blank: Please upload an image blank: Please upload an image
puzzle: puzzle:
@@ -256,7 +267,9 @@ en:
form: form:
already_submitted: You have already completed the puzzle already_submitted: You have already completed the puzzle
completed_message: Thanks for your participation! completed_message: Thanks for your participation!
end_image_select: Take a photo of your completed puzzle end_image_select: Take a photo of your completed puzzle, or on the state it is if you decide to give up
missing_pieces: If completed, indicate the number of missing pieces, if any
remaining_pieces: If your puzzle isn't complete, indicate here the number of remaining pieces to assemble
start_image_select: Take a photo of the puzzle with the provided code written on a paper before starting it start_image_select: Take a photo of the puzzle with the provided code written on a paper before starting it
start_message: Let's go! start_message: Let's go!
puzzles: puzzles:

View File

@@ -49,6 +49,8 @@ fr:
time: Temps time: Temps
offline: offline:
name: Ton nom ou pseudo name: Ton nom ou pseudo
missing_pieces: Pièces manquantes
remaining_pieces: Pièces restantes
puzzle: puzzle:
brand: Marque brand: Marque
image: Image image: Image
@@ -73,6 +75,9 @@ fr:
invalid: "Formats autorisés: xx:xx:xx, x:xx:xx, xx:xx, x:xx, xx" invalid: "Formats autorisés: xx:xx:xx, x:xx:xx, xx:xx, x:xx, xx"
puzzle_id: puzzle_id:
taken: "Ce.tte participant.e a déjà complété ce puzzle" taken: "Ce.tte participant.e a déjà complété ce puzzle"
remaining_pieces:
not_an_integer: Ce n'est pas un nombre entier
not_a_number: Ce n'est pas un nombre entier
contest: contest:
attributes: attributes:
name: name:
@@ -91,8 +96,14 @@ fr:
attributes: attributes:
end_image: end_image:
blank: Tu dois inclure cette image pour pouvoir valider ton puzzle complété blank: Tu dois inclure cette image pour pouvoir valider ton puzzle complété
missing_pieces:
not_an_integer: Ce n'est pas un entier
not_a_number: Ce n'est pas un entier
name: name:
blank: Tu dois entrer un nom pour pouvoir participer blank: Tu dois entrer un nom pour pouvoir participer
remaining_pieces:
not_an_integer: Ce n'est pas un entier
not_a_number: Ce n'est pas un entier
start_image: start_image:
blank: Tu dois inclure cette image pour pouvoir participer blank: Tu dois inclure cette image pour pouvoir participer
puzzle: puzzle:
@@ -227,7 +238,9 @@ fr:
form: form:
already_submitted: Tu as déjà complété ton puzzle already_submitted: Tu as déjà complété ton puzzle
completed_message: Merci pour ta participation ! completed_message: Merci pour ta participation !
end_image_select: Prends une photo du puzzle une fois complété end_image_select: Prends une photo du puzzle une fois complété, ou de l'état actuel si tu choisis de t'arrêter là
missing_pieces: Si complété, indique le nombre de pièces manquantes s'il y en a
remaining_pieces: Si tu as choisis de t'arrêter avant la fin du puzzle, indique ici le nombre de pièces restantes à assembler
start_image_select: Prends une photo du puzzle avant de le commencer, avec le code donné par l'organisateur.ice écrit sur du papier start_image_select: Prends une photo du puzzle avant de le commencer, avec le code donné par l'organisateur.ice écrit sur du papier
start_message: C'est parti ! start_message: C'est parti !
puzzles: puzzles:

View File

@@ -0,0 +1,5 @@
class AddMissingPiecesToOffline < ActiveRecord::Migration[8.0]
def change
add_column :offlines, :missing_pieces, :integer
end
end

View File

@@ -0,0 +1,5 @@
class AddRemainingPiecesToOffline < ActiveRecord::Migration[8.0]
def change
add_column :offlines, :remaining_pieces, :integer
end
end

4
db/schema.rb generated
View File

@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2025_11_05_153647) do ActiveRecord::Schema[8.0].define(version: 2025_11_08_082751) do
create_table "active_storage_attachments", force: :cascade do |t| create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false t.string "name", null: false
t.string "record_type", null: false t.string "record_type", null: false
@@ -135,6 +135,8 @@ ActiveRecord::Schema[8.0].define(version: 2025_11_05_153647) do
t.string "name", null: false t.string "name", null: false
t.boolean "completed" t.boolean "completed"
t.integer "contestant_id" t.integer "contestant_id"
t.integer "missing_pieces"
t.integer "remaining_pieces"
t.index ["contest_id"], name: "index_offlines_on_contest_id" t.index ["contest_id"], name: "index_offlines_on_contest_id"
t.index ["contestant_id"], name: "index_offlines_on_contestant_id" t.index ["contestant_id"], name: "index_offlines_on_contestant_id"
end end

View File

@@ -2,15 +2,17 @@
# #
# Table name: offlines # Table name: offlines
# #
# id :integer not null, primary key # id :integer not null, primary key
# completed :boolean # completed :boolean
# end_time :datetime # end_time :datetime
# name :string not null # missing_pieces :integer
# start_time :datetime not null # name :string not null
# created_at :datetime not null # remaining_pieces :integer
# updated_at :datetime not null # start_time :datetime not null
# contest_id :integer not null # created_at :datetime not null
# contestant_id :integer # updated_at :datetime not null
# contest_id :integer not null
# contestant_id :integer
# #
# Indexes # Indexes
# #

View File

@@ -2,15 +2,17 @@
# #
# Table name: offlines # Table name: offlines
# #
# id :integer not null, primary key # id :integer not null, primary key
# completed :boolean # completed :boolean
# end_time :datetime # end_time :datetime
# name :string not null # missing_pieces :integer
# start_time :datetime not null # name :string not null
# created_at :datetime not null # remaining_pieces :integer
# updated_at :datetime not null # start_time :datetime not null
# contest_id :integer not null # created_at :datetime not null
# contestant_id :integer # updated_at :datetime not null
# contest_id :integer not null
# contestant_id :integer
# #
# Indexes # Indexes
# #