get moods by user
This commit is contained in:
parent
c3c475b989
commit
1ca18ed5ec
6 changed files with 58 additions and 52 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -39,17 +39,17 @@
|
|||
</div>
|
||||
<div class="logs">
|
||||
<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-size-7 day">
|
||||
<% 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 %>
|
||||
</div>
|
||||
<% week.each do |d| %>
|
||||
<% if d[1] %>
|
||||
<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>
|
||||
<% if mood[:mode] %>
|
||||
<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 %>
|
||||
<div class="day"></div>
|
||||
<% end %>
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue