add image for mode

This commit is contained in:
Christophe Robillard 2026-03-19 19:04:16 +01:00
parent 2ea386e557
commit 497744d165
6 changed files with 113 additions and 10 deletions

View file

@ -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

View file

@ -1,3 +1,4 @@
class Mode < ApplicationRecord
belongs_to :user
has_one_attached :image
end

View file

@ -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

View file

@ -2,7 +2,7 @@
<div class="current-day column is-hidden-mobile">
<section class="m-4">
<figure class="image has-ratio">
<%= image_tag(@user.current_mode.label + ".jpg", "data-mood-target": "image") %>
<%= image_tag(@user.current_mode.image, "data-mood-target": "image") %>
</figure>
</section>
</div>
@ -35,7 +35,7 @@
<div class="is-flex is-flex-direction-column is-flex-wrap-wrap mb-3">
<% week.each do |mood| %>
<% mode = mode_for(mood, @user) %>
<div data-image="<%= asset_path(mode[:label] + ".jpg") %>" 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) %>"></div>
<div data-image="<%= mode[:image_url] %>" 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) %>"></div>
<% end %>
</div>
<% end %>

View file

@ -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

34
db/schema.rb generated
View file

@ -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"