diff --git a/app/controllers/completions_controller.rb b/app/controllers/completions_controller.rb index 1c4b2e5..02edb08 100644 --- a/app/controllers/completions_controller.rb +++ b/app/controllers/completions_controller.rb @@ -35,7 +35,9 @@ class CompletionsController < ApplicationController @completion = Completion.new(completion_params) @completion.contest_id = @contest.id 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) redirect_to edit_contest_contestant_path(@contest, @contestant), notice: t("completions.new.notice") else diff --git a/app/controllers/contests_controller.rb b/app/controllers/contests_controller.rb index f09fbf0..d28dbe1 100644 --- a/app/controllers/contests_controller.rb +++ b/app/controllers/contests_controller.rb @@ -147,6 +147,8 @@ class ContestsController < ApplicationController @offline.completed = true @offline.end_time = Time.now() @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 @contest.puzzles.length > 0 dp = display_time(@offline.end_time.to_i - @offline.start_time.to_i) @@ -209,6 +211,6 @@ class ContestsController < ApplicationController end def offline_end_params - params.expect(offline: [ :completed, :end_image ]) + params.expect(offline: [ :completed, :end_image, :remaining_pieces, :missing_pieces ]) end end diff --git a/app/models/completion.rb b/app/models/completion.rb index ec1bf26..43e557c 100644 --- a/app/models/completion.rb +++ b/app/models/completion.rb @@ -34,16 +34,17 @@ class Completion < ApplicationRecord belongs_to :puzzle 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 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 :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 - 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") end end diff --git a/app/models/offline.rb b/app/models/offline.rb index 9f366b9..0f89c6a 100644 --- a/app/models/offline.rb +++ b/app/models/offline.rb @@ -2,15 +2,17 @@ # # Table name: offlines # -# id :integer not null, primary key -# completed :boolean -# end_time :datetime -# name :string not null -# start_time :datetime not null -# created_at :datetime not null -# updated_at :datetime not null -# contest_id :integer not null -# contestant_id :integer +# id :integer not null, primary key +# completed :boolean +# end_time :datetime +# missing_pieces :integer +# name :string not null +# remaining_pieces :integer +# start_time :datetime not null +# created_at :datetime not null +# updated_at :datetime not null +# contest_id :integer not null +# contestant_id :integer # # Indexes # @@ -36,7 +38,27 @@ class Offline < ApplicationRecord validate :end_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 + logger = Logger.new(STDOUT) + logger.info(self.missing_pieces) + logger.info(self.missing_pieces.present?) if self.completed && self.images.length < 2 errors.add(:end_image, I18n.t("activerecord.errors.models.offline.attributes.end_image.blank")) end diff --git a/app/views/contests/offline_edit.html.slim b/app/views/contests/offline_edit.html.slim index edaa24e..54e3920 100644 --- a/app/views/contests/offline_edit.html.slim +++ b/app/views/contests/offline_edit.html.slim @@ -35,6 +35,20 @@ } 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 .col = form.submit t("helpers.buttons.end"), class: "btn btn-primary" \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 678ad61..75199e9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -78,6 +78,8 @@ en: time: Time offline: name: Your name + missing_pieces: Missing pieces + remaining_pieces: Remaining pieces puzzle: brand: Brand image: Image @@ -102,6 +104,9 @@ en: invalid: "Allowed formats: xx:xx:xx, x:xx:xx, xx:xx, x:xx, xx" puzzle_id: 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: attributes: name: @@ -120,8 +125,14 @@ en: attributes: end_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: 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: blank: Please upload an image puzzle: @@ -256,7 +267,9 @@ en: form: already_submitted: You have already completed the puzzle 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_message: Let's go! puzzles: diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 429a344..39d559e 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -49,6 +49,8 @@ fr: time: Temps offline: name: Ton nom ou pseudo + missing_pieces: Pièces manquantes + remaining_pieces: Pièces restantes puzzle: brand: Marque image: Image @@ -73,6 +75,9 @@ fr: 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" + remaining_pieces: + not_an_integer: Ce n'est pas un nombre entier + not_a_number: Ce n'est pas un nombre entier contest: attributes: name: @@ -91,8 +96,14 @@ fr: attributes: end_image: 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: 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: blank: Tu dois inclure cette image pour pouvoir participer puzzle: @@ -227,7 +238,9 @@ fr: form: already_submitted: Tu as déjà complété ton puzzle 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_message: C'est parti ! puzzles: diff --git a/db/migrate/20251108082730_add_missing_pieces_to_offline.rb b/db/migrate/20251108082730_add_missing_pieces_to_offline.rb new file mode 100644 index 0000000..a6a6b52 --- /dev/null +++ b/db/migrate/20251108082730_add_missing_pieces_to_offline.rb @@ -0,0 +1,5 @@ +class AddMissingPiecesToOffline < ActiveRecord::Migration[8.0] + def change + add_column :offlines, :missing_pieces, :integer + end +end diff --git a/db/migrate/20251108082751_add_remaining_pieces_to_offline.rb b/db/migrate/20251108082751_add_remaining_pieces_to_offline.rb new file mode 100644 index 0000000..6231f13 --- /dev/null +++ b/db/migrate/20251108082751_add_remaining_pieces_to_offline.rb @@ -0,0 +1,5 @@ +class AddRemainingPiecesToOffline < ActiveRecord::Migration[8.0] + def change + add_column :offlines, :remaining_pieces, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 420beaa..e0b9923 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_11_05_153647) do +ActiveRecord::Schema[8.0].define(version: 2025_11_08_082751) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", 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.boolean "completed" t.integer "contestant_id" + t.integer "missing_pieces" + t.integer "remaining_pieces" t.index ["contest_id"], name: "index_offlines_on_contest_id" t.index ["contestant_id"], name: "index_offlines_on_contestant_id" end diff --git a/spec/factories/offlines.rb b/spec/factories/offlines.rb index f0c4aba..2bc58ab 100644 --- a/spec/factories/offlines.rb +++ b/spec/factories/offlines.rb @@ -2,15 +2,17 @@ # # Table name: offlines # -# id :integer not null, primary key -# completed :boolean -# end_time :datetime -# name :string not null -# start_time :datetime not null -# created_at :datetime not null -# updated_at :datetime not null -# contest_id :integer not null -# contestant_id :integer +# id :integer not null, primary key +# completed :boolean +# end_time :datetime +# missing_pieces :integer +# name :string not null +# remaining_pieces :integer +# start_time :datetime not null +# created_at :datetime not null +# updated_at :datetime not null +# contest_id :integer not null +# contestant_id :integer # # Indexes # diff --git a/spec/models/offline_spec.rb b/spec/models/offline_spec.rb index 375ca67..63d7055 100644 --- a/spec/models/offline_spec.rb +++ b/spec/models/offline_spec.rb @@ -2,15 +2,17 @@ # # Table name: offlines # -# id :integer not null, primary key -# completed :boolean -# end_time :datetime -# name :string not null -# start_time :datetime not null -# created_at :datetime not null -# updated_at :datetime not null -# contest_id :integer not null -# contestant_id :integer +# id :integer not null, primary key +# completed :boolean +# end_time :datetime +# missing_pieces :integer +# name :string not null +# remaining_pieces :integer +# start_time :datetime not null +# created_at :datetime not null +# updated_at :datetime not null +# contest_id :integer not null +# contestant_id :integer # # Indexes #