2025-08-05 16:11:24 +02:00
|
|
|
|
module MoodsHelper
|
2026-09-29 07:35:53 +02:00
|
|
|
|
# État d'un jour du calendrier :
|
|
|
|
|
|
# :filled une tasse a été posée ce jour-là
|
|
|
|
|
|
# :guessed pas de tasse posée, on suppose que le mode de la veille continue
|
|
|
|
|
|
# :unknown pas de tasse posée, et l'utilisateur ne veut pas de supposition
|
|
|
|
|
|
# :empty avant le premier enregistrement
|
|
|
|
|
|
# :future après aujourd'hui
|
2026-03-21 17:35:06 +01:00
|
|
|
|
def day_status(mood, user)
|
2026-09-29 07:35:53 +02:00
|
|
|
|
return :future if mood[:recorded_at].to_date > Date.current
|
2026-03-21 17:35:06 +01:00
|
|
|
|
return :filled if mood[:mode].present?
|
|
|
|
|
|
return :guessed if mood[:guess].present? && user.guess?
|
|
|
|
|
|
return :unknown if mood[:guess].present? && !user.guess?
|
|
|
|
|
|
:empty
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2026-03-21 20:23:34 +01:00
|
|
|
|
def mode_for(mood, user)
|
|
|
|
|
|
case day_status(mood, user)
|
|
|
|
|
|
when :filled then mood[:mode]
|
|
|
|
|
|
when :guessed then mood[:guess]
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
2026-09-29 07:35:53 +02:00
|
|
|
|
|
|
|
|
|
|
# Les années du calendrier, la plus récente d'abord ; les mois dans l'ordre à l'intérieur.
|
|
|
|
|
|
def history_by_year(history)
|
|
|
|
|
|
history.group_by { |month| month[:month].year }.sort_by { |year, _| -year }
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def find_day(history, date)
|
|
|
|
|
|
history.each do |month|
|
|
|
|
|
|
month[:weeks].each do |week|
|
|
|
|
|
|
week.each { |mood| return mood if mood[:recorded_at].to_date == date }
|
|
|
|
|
|
end
|
2026-02-07 13:05:24 +01:00
|
|
|
|
end
|
2026-09-29 07:35:53 +02:00
|
|
|
|
nil
|
2026-02-07 13:05:24 +01:00
|
|
|
|
end
|
2026-03-14 16:35:48 +01:00
|
|
|
|
|
2026-09-29 07:35:53 +02:00
|
|
|
|
def date_longue(date)
|
|
|
|
|
|
l(date, format: "%A %-d %B %Y")
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2026-09-29 16:57:53 +02:00
|
|
|
|
# « de robi », mais « d’Anne » : élision devant une voyelle ou un h muet
|
|
|
|
|
|
def de_nom(nom)
|
|
|
|
|
|
nom.match?(/\A[aeiouyhàâéèêëîïôöùûü]/i) ? "d’#{nom}" : "de #{nom}"
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2026-09-29 07:35:53 +02:00
|
|
|
|
def libelle_statut(status)
|
2026-03-21 17:35:06 +01:00
|
|
|
|
case status
|
2026-09-29 07:35:53 +02:00
|
|
|
|
when :guessed then "supposé"
|
|
|
|
|
|
when :unknown then "pas de tasse posée"
|
2026-03-21 17:35:06 +01:00
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2026-09-29 07:54:25 +02:00
|
|
|
|
# Un jour du calendrier : un bouton si on peut le choisir, sinon une simple case.
|
|
|
|
|
|
# Le journal n'est exposé que s'il est demandé (jamais sur la page publique).
|
|
|
|
|
|
def case_jour(mood, user, interactive:, selected: false, journal: false)
|
2026-09-29 07:35:53 +02:00
|
|
|
|
date = mood[:recorded_at].to_date
|
|
|
|
|
|
status = day_status(mood, user)
|
|
|
|
|
|
mode = mode_for(mood, user)
|
2026-09-29 07:54:25 +02:00
|
|
|
|
day_log = mood[:day_log] if journal
|
2026-09-29 07:35:53 +02:00
|
|
|
|
|
|
|
|
|
|
description = [ date_longue(date), mode&.label, libelle_statut(status), ("journal écrit" if day_log) ].compact.join(", ")
|
|
|
|
|
|
classes = [ "jour", "jour-#{status}" ]
|
|
|
|
|
|
classes << "jour-journal" if day_log
|
|
|
|
|
|
classes << "jour-aujourdhui" if date == Date.current
|
|
|
|
|
|
options = { class: classes, style: ("--humeur: #{mode.color}" if mode), title: description }
|
|
|
|
|
|
|
|
|
|
|
|
if interactive && status != :future
|
|
|
|
|
|
tag.button(type: "button", **options,
|
|
|
|
|
|
tabindex: selected ? 0 : -1,
|
|
|
|
|
|
"aria-label": description,
|
|
|
|
|
|
"aria-pressed": selected.to_s,
|
|
|
|
|
|
data: {
|
|
|
|
|
|
day: date.iso8601,
|
|
|
|
|
|
date: date_longue(date),
|
|
|
|
|
|
mode: mode&.label,
|
|
|
|
|
|
statut: libelle_statut(status),
|
|
|
|
|
|
couleur: mode&.color,
|
|
|
|
|
|
image: (mode || Mode.new).image_url,
|
|
|
|
|
|
info: day_log&.info,
|
|
|
|
|
|
action: "calendrier#choisir keydown->calendrier#naviguer"
|
|
|
|
|
|
})
|
|
|
|
|
|
else
|
|
|
|
|
|
tag.span(**options, "aria-hidden": true)
|
|
|
|
|
|
end
|
2026-03-14 16:35:48 +01:00
|
|
|
|
end
|
2025-08-05 16:11:24 +02:00
|
|
|
|
end
|