moodtracker/mood.rb

29 lines
743 B
Ruby
Raw Normal View History

2024-06-02 20:19:02 +02:00
class Mood < ActiveRecord::Base
2024-09-22 20:50:55 +02:00
default_scope { order(:recorded_at) }
2024-06-02 20:19:02 +02:00
validates_presence_of :mode
validates_presence_of :recorded_at
def self.log
return if Mood.count < 1
first_mood = Mood.first.recorded_at.to_date
first_monday = first_mood - (first_mood.wday - 1)
current_date = first_monday
current_mode = nil
log_mood = []
Mood.all.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 <= Date.today do
log_mood << [ current_date.to_s, current_mode ]
current_date += 1
end
log_mood.each_slice(7).to_a
end
end