52 lines
1.1 KiB
Ruby
52 lines
1.1 KiB
Ruby
class DayLogsController < ApplicationController
|
|
before_action :set_day_log, only: [ :edit, :update ]
|
|
|
|
def new
|
|
@day_log = DayLog.new
|
|
end
|
|
|
|
def create
|
|
@day_log = Current.user.day_logs.build(day_log_params)
|
|
|
|
if @day_log.save
|
|
handle_mood(@day_log.day, params[:day_log][:mode_id])
|
|
redirect_to root_path
|
|
else
|
|
render :new, status: :unprocessable_content
|
|
end
|
|
end
|
|
|
|
def edit
|
|
end
|
|
|
|
def update
|
|
if @day_log.update(day_log_params)
|
|
handle_mood(@day_log.day, params[:day_log][:mode_id])
|
|
redirect_to dashboard_path
|
|
else
|
|
render :edit, status: :unprocessable_content
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def set_day_log
|
|
@day_log = Current.user.day_logs.find(params[:id])
|
|
end
|
|
|
|
def handle_mood(day, mode_id)
|
|
return if mode_id.blank?
|
|
|
|
mood = Current.user.moods.find_by(recorded_at: day.beginning_of_day..day.end_of_day)
|
|
|
|
if mood
|
|
mood.update(mode_id: mode_id)
|
|
else
|
|
Current.user.moods.create(mode_id: mode_id, recorded_at: day.to_datetime)
|
|
end
|
|
end
|
|
|
|
def day_log_params
|
|
params.expect(day_log: [ :day, :info ])
|
|
end
|
|
end
|