From 1ca18ed5ece1c2814b203025bb858a2c05094ee2 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Thu, 29 Jan 2026 17:05:34 +0100 Subject: [PATCH] get moods by user --- app/controllers/concerns/authentication.rb | 5 +++ app/controllers/moods_controller.rb | 4 +- app/models/mood.rb | 44 ---------------------- app/models/user.rb | 43 +++++++++++++++++++++ app/views/moods/index.html.erb | 12 +++--- config/environments/development.rb | 2 + 6 files changed, 58 insertions(+), 52 deletions(-) diff --git a/app/controllers/concerns/authentication.rb b/app/controllers/concerns/authentication.rb index 3538f48..e3fdef6 100644 --- a/app/controllers/concerns/authentication.rb +++ b/app/controllers/concerns/authentication.rb @@ -4,6 +4,7 @@ module Authentication included do before_action :require_authentication helper_method :authenticated? + helper_method :current_user end class_methods do @@ -17,6 +18,10 @@ module Authentication resume_session end + def current_user + authenticated?&.user + end + def require_authentication resume_session || request_authentication end diff --git a/app/controllers/moods_controller.rb b/app/controllers/moods_controller.rb index 1589791..e4611ad 100644 --- a/app/controllers/moods_controller.rb +++ b/app/controllers/moods_controller.rb @@ -1,6 +1,6 @@ class MoodsController < ApplicationController def index - @mode = Mood.last&.mode || "croisiere" - @mood_log = Mood.history_for_a_year || [] + @mode = current_user.moods.last&.mode || "croisiere" + @history = current_user.history end end diff --git a/app/models/mood.rb b/app/models/mood.rb index 2f1de4c..2b6a392 100644 --- a/app/models/mood.rb +++ b/app/models/mood.rb @@ -1,47 +1,3 @@ class Mood < ApplicationRecord belongs_to :user - - class << self - def history_for_a_year - history(Date.today - 1.year, Date.today) - end - - private - def history(from, to) - return [] if Mood.count < 1 - - first_monday = monday_of_the_week(first_mood_date(from)) - - current_date = first_monday - current_mode = last_mode_before(current_date) - log_mood = [] - mood_range = Mood.order(:recorded_at).where(recorded_at: (first_monday..to)) - mood_range.each do |mood| - while current_date < mood.recorded_at.to_date do - log_mood << [ current_date, current_mode ] - current_date += 1 - end - current_mode = mood.mode - end - - while current_date <= to.to_date do - log_mood << [ current_date, current_mode ] - current_date += 1 - end - log_mood.each_slice(7).to_a - end - - def first_mood_date(from) - Mood.order(recorded_at: :asc).where("recorded_at >= ?", from).first.recorded_at.to_date - end - - def monday_of_the_week(date) - date - date.wday + 1 - end - - def last_mode_before(date) - mood = Mood.where("recorded_at < ?", date).last - mood&.mode || "croisiere" - end - end end diff --git a/app/models/user.rb b/app/models/user.rb index e07b5f0..8318888 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -29,4 +29,47 @@ class User < ApplicationRecord def invitation_token generate_token_for(:invitation) end + + def history + history_range(Mood.first.recorded_at.to_date, Date.today) + end + + private + + def history_range(from, to) + return [] if Mood.count < 1 + + first_monday = monday_of_the_week(first_mood_date(from)) + + current_date = first_monday + current_mode = last_mode_before(current_date) + log_mood = [] + mood_range = Mood.order(:recorded_at).where(user: self, recorded_at: (first_monday..to)) + mood_range.each do |mood| + while current_date < mood.recorded_at.to_date do + log_mood << { day: current_date, mode: current_mode } + current_date += 1 + end + current_mode = mood.mode + end + + while current_date <= to.to_date do + log_mood << { day: current_date, mode: current_mode } + current_date += 1 + end + log_mood.each_slice(7).to_a + end + + def first_mood_date(from) + Mood.order(recorded_at: :asc).where("user_id = :user_id AND recorded_at >= :from", user_id: self.id, from:).first.recorded_at.to_date + end + + def monday_of_the_week(date) + date - date.wday + 1 + end + + def last_mode_before(last) + mood = Mood.where("user_id = :user_id AND recorded_at < :last", user_id: self.id, last: last).last + mood&.mode || "croisiere" + end end diff --git a/app/views/moods/index.html.erb b/app/views/moods/index.html.erb index 45bbeba..5415abe 100644 --- a/app/views/moods/index.html.erb +++ b/app/views/moods/index.html.erb @@ -39,17 +39,17 @@
- <% @mood_log.each do |week| %> + <% @history.each do |week| %>
- <% day = week.first[0] %> - <% if day.day < 8 && day.monday? %> - <%= l(day, format: "%b") %> + <% mood = week.first %> + <% if mood[:day].day < 8 %> + <%= l(mood[:day], format: "%b") %> <% end %>
<% week.each do |d| %> - <% if d[1] %> -
" data-mode="<%= d[1] %>" data-day="<%= l d[0] %>" data-action="click->mood#updateDayInfo mouseover->mood#updateDayInfo mouseleave->mood#updateDayInfo" title="<%= d[0] %> : <%= d[1] %>" class="day <%= d[1] %>">
+ <% if mood[:mode] %> +
" data-mode="<%= mood[:mode] %>" data-day="<%= l mood[:day] %>" data-action="click->mood#updateDayInfo mouseover->mood#updateDayInfo mouseleave->mood#updateDayInfo" title="<%= mood[:day] %> : <%= mood[:mode] %>" class="day <%= mood[:mode] %>">
<% else %>
<% end %> diff --git a/config/environments/development.rb b/config/environments/development.rb index 4cc21c4..f632a6a 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -69,4 +69,6 @@ Rails.application.configure do # Apply autocorrection by RuboCop to files generated by `bin/rails generate`. # config.generators.apply_rubocop_autocorrect_after_generate! + # + config.hosts << "bipodokrat:3000" end