From 497744d165ec49e0d00052d67e750eca94db4038 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Thu, 19 Mar 2026 19:04:16 +0100 Subject: [PATCH] add image for mode --- app/helpers/moods_helper.rb | 4 +- app/models/mode.rb | 1 + app/services/mood_calendar_service.rb | 23 ++++++-- app/views/moods/index.html.erb | 4 +- ...te_active_storage_tables.active_storage.rb | 57 +++++++++++++++++++ db/schema.rb | 34 ++++++++++- 6 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 db/migrate/20260315183258_create_active_storage_tables.active_storage.rb diff --git a/app/helpers/moods_helper.rb b/app/helpers/moods_helper.rb index 6a49c77..0ff7816 100644 --- a/app/helpers/moods_helper.rb +++ b/app/helpers/moods_helper.rb @@ -1,9 +1,9 @@ module MoodsHelper def mode_for(mood, user) if user.guess? - mood[:mode] || mood[:guess] || { label: "unknown", color: "white" } + mood[:mode] || mood[:guess] || { label: "unknown", color: "white", image_url: "unknown.jpg" } else - mood[:mode] || { label: "unknown", color: "white" } + mood[:mode] || { label: "unknown", color: "white", image_url: "unknown.jpg" } end end diff --git a/app/models/mode.rb b/app/models/mode.rb index 5b84d24..f0f1b2f 100644 --- a/app/models/mode.rb +++ b/app/models/mode.rb @@ -1,3 +1,4 @@ class Mode < ApplicationRecord belongs_to :user + has_one_attached :image end diff --git a/app/services/mood_calendar_service.rb b/app/services/mood_calendar_service.rb index bb54b97..5b76f00 100644 --- a/app/services/mood_calendar_service.rb +++ b/app/services/mood_calendar_service.rb @@ -1,11 +1,26 @@ # app/services/mood_calendar_service.rb class MoodCalendarService def self.generate_calendar(moods, start_date: nil, end_date: Date.current) - # Convertir la relation ActiveRecord en tableau de hash + # Preload modes with their image attachments + mode_ids = moods.joins(:mode).pluck("modes.id").uniq + modes_by_id = Mode.where(id: mode_ids) + .with_attached_image + .index_by(&:id) + data = moods.joins(:mode) - .order(:recorded_at) - .pluck(:recorded_at, "modes.label", "modes.color") - .map { |recorded_at, label, color| { mode: { label: label, color: color }, recorded_at: recorded_at } } + .order(:recorded_at) + .pluck(:recorded_at, "modes.label", "modes.color", "modes.id") + .map do |recorded_at, label, color, mode_id| + mode = modes_by_id[mode_id] + { + mode: { + label: label, + color: color, + image_url: mode&.image&.attached? ? Rails.application.routes.url_helpers.rails_blob_url(mode.image, only_path: true) : nil + }, + recorded_at: recorded_at + } + end # Convertir la relation ActiveRecord en tableau de hash if data.empty? start_date = Date.current diff --git a/app/views/moods/index.html.erb b/app/views/moods/index.html.erb index 0063f55..5f4dfab 100644 --- a/app/views/moods/index.html.erb +++ b/app/views/moods/index.html.erb @@ -2,7 +2,7 @@
- <%= image_tag(@user.current_mode.label + ".jpg", "data-mood-target": "image") %> + <%= image_tag(@user.current_mode.image, "data-mood-target": "image") %>
@@ -35,7 +35,7 @@
<% week.each do |mood| %> <% mode = mode_for(mood, @user) %> -
" data-mode="<%= mode[:label] %>" data-day="<%= l mood[:recorded_at].to_date %>" data-action="click->mood#updateDayInfo" title="<%= l(mood[:recorded_at].to_date) %> : <%= mode[:label] %>" class="day" style="<%= style_for_mode(mode) %>">
+
<% end %>
<% end %> diff --git a/db/migrate/20260315183258_create_active_storage_tables.active_storage.rb b/db/migrate/20260315183258_create_active_storage_tables.active_storage.rb new file mode 100644 index 0000000..6bd8bd0 --- /dev/null +++ b/db/migrate/20260315183258_create_active_storage_tables.active_storage.rb @@ -0,0 +1,57 @@ +# This migration comes from active_storage (originally 20170806125915) +class CreateActiveStorageTables < ActiveRecord::Migration[7.0] + def change + # Use Active Record's configured type for primary and foreign keys + primary_key_type, foreign_key_type = primary_and_foreign_key_types + + create_table :active_storage_blobs, id: primary_key_type do |t| + t.string :key, null: false + t.string :filename, null: false + t.string :content_type + t.text :metadata + t.string :service_name, null: false + t.bigint :byte_size, null: false + t.string :checksum + + if connection.supports_datetime_with_precision? + t.datetime :created_at, precision: 6, null: false + else + t.datetime :created_at, null: false + end + + t.index [ :key ], unique: true + end + + create_table :active_storage_attachments, id: primary_key_type do |t| + t.string :name, null: false + t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type + t.references :blob, null: false, type: foreign_key_type + + if connection.supports_datetime_with_precision? + t.datetime :created_at, precision: 6, null: false + else + t.datetime :created_at, null: false + end + + t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true + t.foreign_key :active_storage_blobs, column: :blob_id + end + + create_table :active_storage_variant_records, id: primary_key_type do |t| + t.belongs_to :blob, null: false, index: false, type: foreign_key_type + t.string :variation_digest, null: false + + t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true + t.foreign_key :active_storage_blobs, column: :blob_id + end + end + + private + def primary_and_foreign_key_types + config = Rails.configuration.generators + setting = config.options[config.orm][:primary_key_type] + primary_key_type = setting || :primary_key + foreign_key_type = setting || :bigint + [ primary_key_type, foreign_key_type ] + end +end diff --git a/db/schema.rb b/db/schema.rb index 71684c4..86844f0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,35 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2026_03_14_150724) do +ActiveRecord::Schema[8.0].define(version: 2026_03_15_183258) do + create_table "active_storage_attachments", force: :cascade do |t| + t.string "name", null: false + t.string "record_type", null: false + t.bigint "record_id", null: false + t.bigint "blob_id", null: false + t.datetime "created_at", null: false + t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id" + t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true + end + + create_table "active_storage_blobs", force: :cascade do |t| + t.string "key", null: false + t.string "filename", null: false + t.string "content_type" + t.text "metadata" + t.string "service_name", null: false + t.bigint "byte_size", null: false + t.string "checksum" + t.datetime "created_at", null: false + t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true + end + + create_table "active_storage_variant_records", force: :cascade do |t| + t.bigint "blob_id", null: false + t.string "variation_digest", null: false + t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true + end + create_table "modes", force: :cascade do |t| t.string "label" t.string "color" @@ -37,7 +65,7 @@ ActiveRecord::Schema[8.0].define(version: 2026_03_14_150724) do t.datetime "updated_at", default: -> { "CURRENT_DATE" }, null: false t.string "chip_id" t.integer "user_id" - t.index ["identifier"], name: "index_rfid_tags_on_identifier" + t.index ["identifier"], name: "index_rfid_tags_on_identifier", unique: true t.index ["user_id"], name: "index_rfid_tags_on_user_id" end @@ -62,6 +90,8 @@ ActiveRecord::Schema[8.0].define(version: 2026_03_14_150724) do t.index ["email_address"], name: "index_users_on_email_address", unique: true end + add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" + add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "modes", "users" add_foreign_key "moods", "modes" add_foreign_key "moods", "users"