can edit selected day

This commit is contained in:
Christophe Robillard 2026-03-21 16:16:54 +01:00
parent 46e6014fea
commit a9f72b60af
5 changed files with 70 additions and 14 deletions

View file

@ -1,8 +1,9 @@
class DayLogsController < ApplicationController class DayLogsController < ApplicationController
before_action :set_day_log, only: [ :edit, :update ] before_action :set_day_log, only: [ :update ]
def new def edit
@day_log = DayLog.new @day_log = Current.user.day_logs.find_or_initialize_by(day: params[:day])
@mood = Current.user.moods.find_by(recorded_at: params[:day].to_date.beginning_of_day..params[:day].to_date.end_of_day)
end end
def create def create
@ -16,9 +17,6 @@ class DayLogsController < ApplicationController
end end
end end
def edit
end
def update def update
if @day_log.update(day_log_params) if @day_log.update(day_log_params)
handle_mood(@day_log.day, params[:day_log][:mode_id]) handle_mood(@day_log.day, params[:day_log][:mode_id])

View file

@ -1,16 +1,19 @@
import { Controller } from '@hotwired/stimulus' import { Controller } from '@hotwired/stimulus'
export default class extends Controller { export default class extends Controller {
static targets = [ "image", "modeDay" ]; static targets = [ "image", "modeDay", "dayLogLink" ];
updateDayInfo(event) { updateDayInfo(event) {
const image = this.imageTarget; const image = this.imageTarget;
const modeDay = this.modeDayTarget; const modeDay = this.modeDayTarget;
const modeDayContent = event.target.dataset.day + ' : ' + event.target.dataset.mode; const day = event.target.dataset.day;
const modeDayContent = day + ' : ' + event.target.dataset.mode;
image.src = event.target.dataset.image; image.src = event.target.dataset.image;
modeDay.textContent = modeDayContent; modeDay.textContent = modeDayContent;
event.target.className = "selected-day " + event.target.dataset.mode; event.target.className = "selected-day " + event.target.dataset.mode;
this.dayLogLinkTarget.href = `/day_logs/edit?day=${day}`
if (this.lastTarget) { if (this.lastTarget) {
this.lastTarget.className = "day " + this.lastTarget.dataset.mode; this.lastTarget.className = "day " + this.lastTarget.dataset.mode;
} }

View file

@ -23,8 +23,10 @@
<div class="title is-4"> <div class="title is-4">
<span class="icon"><i class="fa-regular fa-calendar"></i></span> <span class="icon"><i class="fa-regular fa-calendar"></i></span>
<span data-mood-target="modeDay">Aujourd'hui : <%= @user.current_mode.label %></span> <span data-mood-target="modeDay">Aujourd'hui : <%= @user.current_mode.label %></span>
<%= link_to "Journal", edit_day_log_for_day_path(day: Date.today), "data-mood-target": "dayLogLink" %>
</div> </div>
</div> </div>
<div class="logs"> <div class="logs">
<div class="is-flex is-flex-direction-row is-flex-wrap-wrap mb-3"> <div class="is-flex is-flex-direction-row is-flex-wrap-wrap mb-3">
<% @user.history.each do |month| %> <% @user.history.each do |month| %>

View file

@ -22,5 +22,6 @@ Rails.application.routes.draw do
get "/moods", to: "moods#index", as: :dashboard get "/moods", to: "moods#index", as: :dashboard
resources :day_logs, only: [ :new, :create, :edit, :update ] get "/day_logs/edit", to: "day_logs#edit", as: :edit_day_log_for_day
resources :day_logs, only: [ :create, :update ]
end end

View file

@ -5,10 +5,10 @@ describe "DayLogs", type: :request do
let(:mode) { create(:mode, user: user) } let(:mode) { create(:mode, user: user) }
let(:day) { Date.parse("2026-01-15") } let(:day) { Date.parse("2026-01-15") }
describe "GET /day_logs/new" do describe "GET /day_logs/edit" do
context "when not authenticated" do context "when not authenticated" do
it "redirects to the login page" do it "redirects to the login page" do
get new_day_log_path get edit_day_log_for_day_path(day: day)
expect(response).to redirect_to(new_session_path) expect(response).to redirect_to(new_session_path)
end end
end end
@ -16,9 +16,26 @@ describe "DayLogs", type: :request do
context "when authenticated" do context "when authenticated" do
before { login_as(user) } before { login_as(user) }
it "returns http success" do context "with an existing day_log for the day" do
get new_day_log_path let!(:day_log) { create(:day_log, user: user, day: day) }
expect(response).to have_http_status(:success)
it "returns http success" do
get edit_day_log_for_day_path(day: day)
expect(response).to have_http_status(:success)
end
end
context "without an existing day_log for the day" do
it "returns http success" do
get edit_day_log_for_day_path(day: day)
expect(response).to have_http_status(:success)
end
it "does not persist a day_log" do
expect {
get edit_day_log_for_day_path(day: day)
}.not_to change(DayLog, :count)
end
end end
end end
end end
@ -126,6 +143,41 @@ describe "DayLogs", type: :request do
end end
end end
describe "GET /day_logs/edit" do
context "when not authenticated" do
it "redirects to the login page" do
get edit_day_log_for_day_path(day: day)
expect(response).to redirect_to(new_session_path)
end
end
context "when authenticated" do
before { login_as(user) }
context "with an existing day_log for the day" do
let!(:day_log) { create(:day_log, user: user, day: day) }
it "returns http success" do
get edit_day_log_for_day_path(day: day)
expect(response).to have_http_status(:success)
end
end
context "without an existing day_log for the day" do
it "returns http success" do
get edit_day_log_for_day_path(day: day)
expect(response).to have_http_status(:success)
end
it "does not persist a day_log" do
expect {
get edit_day_log_for_day_path(day: day)
}.not_to change(DayLog, :count)
end
end
end
end
describe "PATCH /day_logs/:id" do describe "PATCH /day_logs/:id" do
let!(:day_log) { create(:day_log, user: user, day: day) } let!(:day_log) { create(:day_log, user: user, day: day) }