show moods for a year
This commit is contained in:
parent
20b2d3ce4a
commit
957cd3a908
2 changed files with 36 additions and 15 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
class MoodsController < ApplicationController
|
class MoodsController < ApplicationController
|
||||||
def index
|
def index
|
||||||
@mode = Mood.last&.mode || "croisiere"
|
@mode = Mood.last&.mode || "croisiere"
|
||||||
@mood_log = Mood.log || []
|
@mood_log = Mood.history_for_a_year || []
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,45 @@
|
||||||
class Mood < ApplicationRecord
|
class Mood < ApplicationRecord
|
||||||
def self.log
|
class << self
|
||||||
return if Mood.count < 1
|
def history_for_a_year
|
||||||
|
history(Date.today - 1.year, Date.today)
|
||||||
|
end
|
||||||
|
|
||||||
first_mood = Mood.order(:recorded_at).first.recorded_at.to_date
|
private
|
||||||
first_monday = first_mood - (first_mood.wday - 1)
|
def history(from, to)
|
||||||
|
return [] if Mood.count < 1
|
||||||
|
|
||||||
current_date = first_monday
|
first_monday = monday_of_the_week(first_mood_date(from))
|
||||||
current_mode = nil
|
|
||||||
log_mood = []
|
current_date = first_monday
|
||||||
Mood.order(:recorded_at).all.each do |mood|
|
current_mode = last_mode_before(current_date)
|
||||||
while current_date < mood.recorded_at.to_date do
|
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.to_s, current_mode ]
|
||||||
|
current_date += 1
|
||||||
|
end
|
||||||
|
current_mode = mood.mode
|
||||||
|
end
|
||||||
|
|
||||||
|
while current_date <= to.to_date do
|
||||||
log_mood << [ current_date.to_s, current_mode ]
|
log_mood << [ current_date.to_s, current_mode ]
|
||||||
current_date += 1
|
current_date += 1
|
||||||
end
|
end
|
||||||
current_mode = mood.mode
|
log_mood.each_slice(7).to_a
|
||||||
end
|
end
|
||||||
while current_date <= Date.today do
|
|
||||||
log_mood << [ current_date.to_s, current_mode ]
|
def first_mood_date(from)
|
||||||
current_date += 1
|
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
|
||||||
log_mood.each_slice(7).to_a.last(52)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue