From 0599def2376be83eb29dbbea0dbde06f309227a7 Mon Sep 17 00:00:00 2001 From: sto Date: Fri, 27 Jun 2025 09:23:25 +0200 Subject: [PATCH] Add number of pieces to puzzles --- app/controllers/puzzles_controller.rb | 2 +- app/models/puzzle.rb | 2 ++ app/views/contests/scoreboard.html.slim | 22 ++----------------- app/views/contests/show.html.slim | 10 ++++++--- app/views/puzzles/_form.html.slim | 5 +++++ config/locales/en.yml | 17 ++++++++------ config/locales/fr.yml | 17 ++++++++------ .../20250627070407_add_pieces_to_puzzle.rb | 12 ++++++++++ db/schema.rb | 3 ++- 9 files changed, 51 insertions(+), 39 deletions(-) create mode 100644 db/migrate/20250627070407_add_pieces_to_puzzle.rb diff --git a/app/controllers/puzzles_controller.rb b/app/controllers/puzzles_controller.rb index 025029a..3ba86ca 100644 --- a/app/controllers/puzzles_controller.rb +++ b/app/controllers/puzzles_controller.rb @@ -61,6 +61,6 @@ class PuzzlesController < ApplicationController end def puzzle_params - params.expect(puzzle: [ :brand, :name, :image ]) + params.expect(puzzle: [ :brand, :name, :image, :pieces ]) end end diff --git a/app/models/puzzle.rb b/app/models/puzzle.rb index 2fad557..216b7ca 100644 --- a/app/models/puzzle.rb +++ b/app/models/puzzle.rb @@ -5,6 +5,7 @@ # id :integer not null, primary key # brand :string # name :string +# pieces :integer not null # created_at :datetime not null # updated_at :datetime not null # contest_id :integer not null @@ -24,4 +25,5 @@ class Puzzle < ApplicationRecord has_one_attached :image validates :name, presence: true + validates :pieces, presence: true end diff --git a/app/views/contests/scoreboard.html.slim b/app/views/contests/scoreboard.html.slim index ff0be43..4a6bbe7 100644 --- a/app/views/contests/scoreboard.html.slim +++ b/app/views/contests/scoreboard.html.slim @@ -29,16 +29,9 @@ css: = image_tag(puzzle.image, style: "max-height: 200px; object-fit: contain") if puzzle.image.attached? .mt-2.fs-6 style="text-align: center" => "#{puzzle.name} -" - = puzzle.brand + = "#{puzzle.brand} #{puzzle.pieces}p" .row .col-6.d-flex.flex-column style="height: calc(100vh - 180px)" - / = form_with model: Contest do |form| - / .row.mb-3 - / .col - / .form-check.form-switch - / = form.check_box :refresh, class: "form-check-input" - / = form.label t("contests.scoreboard.refresh") - .d-flex.flex-column style="overflow-y: auto" table.table.table-striped.table-hover thead @@ -76,7 +69,7 @@ css: = puzzle.name .fs-6 style="margin-left: 15px" b - = puzzle.brand + = "#{puzzle.brand} - #{puzzle.pieces}p" - else .d-flex.flex-column style="height: calc(100vh - 180px)" @@ -84,17 +77,6 @@ css: - @contest.puzzles.each do |puzzle| = image_tag(puzzle.image, class: "img-fluid ms-3 me-3", style: "max-height: 220px") if puzzle.image.attached? - .mb-3 style="display: inline-flex" - a.btn.btn-primary href="/public/#{@contest.friendly_id}" - = t("helpers.buttons.refresh") - - / = form_with model: Contest do |form| - / .row.mb-3 - / .col - / .form-check.form-switch - / = form.check_box :refresh, class: "form-check-input" - / = form.label t("contests.scoreboard.refresh") - .d-flex.flex-column style="overflow-y: auto" table.table.table-striped.table-hover thead diff --git a/app/views/contests/show.html.slim b/app/views/contests/show.html.slim index 51fc07d..0e45950 100644 --- a/app/views/contests/show.html.slim +++ b/app/views/contests/show.html.slim @@ -43,12 +43,14 @@ javascript: table.table.table-striped.table-hover thead tr - th scope="col" + th = t("activerecord.attributes.puzzle.image") - th scope="col" + th = t("activerecord.attributes.puzzle.name") - th scope="col" + th = t("activerecord.attributes.puzzle.brand") + th + = t("activerecord.attributes.puzzle.pieces") tbody - @puzzles.each do |puzzle| tr.align-middle scope="row" @@ -58,6 +60,8 @@ javascript: = puzzle.name td = puzzle.brand + td + = puzzle.pieces td a.btn.btn-sm.btn-secondary href=edit_contest_puzzle_path(@contest, puzzle) = t("helpers.buttons.edit") diff --git a/app/views/puzzles/_form.html.slim b/app/views/puzzles/_form.html.slim index 70de990..76ac9f2 100644 --- a/app/views/puzzles/_form.html.slim +++ b/app/views/puzzles/_form.html.slim @@ -15,6 +15,11 @@ .form-floating = form.text_field :brand, autocomplete: "off", class: "form-control" = form.label :brand, class: "required" + .row.mb-3 + .col + .form-floating + = form.number_field :pieces, autocomplete: "off", class: "form-control" + = form.label :pieces, class: "required" .row.mb-3 .col .form-text.mb-1 diff --git a/config/locales/en.yml b/config/locales/en.yml index 52e467f..8e64008 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -62,14 +62,15 @@ en: file: File separator: Separator message: - author: "Author" - processed: "Processed?" - text: "Content" - time: "Time" + author: Author + processed: Processed? + text: Content + time: Time puzzle: - brand: "Brand" - image: "Image" - name: "Name" + brand: Brand + image: Image + name: Name + pieces: Number of pieces session: email_address: Email address password: Password @@ -107,6 +108,8 @@ en: attributes: name: blank: The puzzle name cannot be empty + pieces: + blank: It's mandatory to provide the number of pieces user: attributes: email_address: diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 35f15f5..0d3441e 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -33,14 +33,15 @@ fr: file: Fichier separator: Délimiteur message: - author: "Auteur.ice" - processed: "Traité ?" - text: "Contenu" - time: "Temps" + author: Auteur.ice + processed: Traité ? + text: Contenu + time: Temps puzzle: - brand: "Marque" - image: "Image" - name: "Nom" + brand: Marque + image: Image + name: Nom + pieces: Nombre de pièces session: email_address: Adresse email password: Mot de passe @@ -78,6 +79,8 @@ fr: attributes: name: blank: Le nom du puzzle est obligatoire + pieces: + blank: Il est obligatoire d'indiquer le nombre de pièces user: attributes: email_address: diff --git a/db/migrate/20250627070407_add_pieces_to_puzzle.rb b/db/migrate/20250627070407_add_pieces_to_puzzle.rb new file mode 100644 index 0000000..b896c87 --- /dev/null +++ b/db/migrate/20250627070407_add_pieces_to_puzzle.rb @@ -0,0 +1,12 @@ +class AddPiecesToPuzzle < ActiveRecord::Migration[8.0] + def change + add_column :puzzles, :pieces, :integer + + Puzzle.find_each do |puzzle| + puzzle.pieces = 500 + puzzle.save + end + + change_column_null :puzzles, :pieces, false + end +end diff --git a/db/schema.rb b/db/schema.rb index 49268c3..2069187 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_25_075513) do +ActiveRecord::Schema[8.0].define(version: 2025_06_27_070407) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -115,6 +115,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_06_25_075513) do t.datetime "updated_at", null: false t.integer "contest_id", null: false t.string "brand" + t.integer "pieces", null: false t.index ["contest_id"], name: "index_puzzles_on_contest_id" end