get moods by user

This commit is contained in:
Christophe Robillard 2026-01-29 17:05:34 +01:00
parent c3c475b989
commit 1ca18ed5ec
6 changed files with 58 additions and 52 deletions

View file

@ -4,6 +4,7 @@ module Authentication
included do included do
before_action :require_authentication before_action :require_authentication
helper_method :authenticated? helper_method :authenticated?
helper_method :current_user
end end
class_methods do class_methods do
@ -17,6 +18,10 @@ module Authentication
resume_session resume_session
end end
def current_user
authenticated?&.user
end
def require_authentication def require_authentication
resume_session || request_authentication resume_session || request_authentication
end end

View file

@ -1,6 +1,6 @@
class MoodsController < ApplicationController class MoodsController < ApplicationController
def index def index
@mode = Mood.last&.mode || "croisiere" @mode = current_user.moods.last&.mode || "croisiere"
@mood_log = Mood.history_for_a_year || [] @history = current_user.history
end end
end end

View file

@ -1,47 +1,3 @@
class Mood < ApplicationRecord class Mood < ApplicationRecord
belongs_to :user 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 end

View file

@ -29,4 +29,47 @@ class User < ApplicationRecord
def invitation_token def invitation_token
generate_token_for(:invitation) generate_token_for(:invitation)
end 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 end

View file

@ -39,17 +39,17 @@
</div> </div>
<div class="logs"> <div class="logs">
<div class="is-flex is-flex-wrap-wrap"> <div class="is-flex is-flex-wrap-wrap">
<% @mood_log.each do |week| %> <% @history.each do |week| %>
<div class="is-flex is-flex-direction-column is-flex-wrap-wrap mb-3"> <div class="is-flex is-flex-direction-column is-flex-wrap-wrap mb-3">
<div class="is-size-7 day"> <div class="is-size-7 day">
<% day = week.first[0] %> <% mood = week.first %>
<% if day.day < 8 && day.monday? %> <% if mood[:day].day < 8 %>
<%= l(day, format: "%b") %> <%= l(mood[:day], format: "%b") %>
<% end %> <% end %>
</div> </div>
<% week.each do |d| %> <% week.each do |d| %>
<% if d[1] %> <% if mood[:mode] %>
<div data-image="<%= asset_path(d[1] + ".jpg") %>" 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] %>"></div> <div data-image="<%= asset_path(mood[:mode] + ".jpg") %>" 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] %>"></div>
<% else %> <% else %>
<div class="day"></div> <div class="day"></div>
<% end %> <% end %>

View file

@ -69,4 +69,6 @@ Rails.application.configure do
# Apply autocorrection by RuboCop to files generated by `bin/rails generate`. # Apply autocorrection by RuboCop to files generated by `bin/rails generate`.
# config.generators.apply_rubocop_autocorrect_after_generate! # config.generators.apply_rubocop_autocorrect_after_generate!
#
config.hosts << "bipodokrat:3000"
end end