app.kluk.fr/app/controllers/day_logs_controller.rb

53 lines
1.1 KiB
Ruby
Raw Normal View History

2026-03-19 21:21:28 +01:00
class DayLogsController < ApplicationController
2026-03-20 19:28:54 +01:00
before_action :set_day_log, only: [ :edit, :update ]
2026-03-19 21:21:28 +01:00
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
2026-03-20 19:28:54 +01:00
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
2026-03-19 21:21:28 +01:00
end
end
private
2026-03-20 19:28:54 +01:00
def set_day_log
@day_log = Current.user.day_logs.find(params[:id])
end
2026-03-19 21:21:28 +01:00
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