diff --git a/app/controllers/day_logs_controller.rb b/app/controllers/day_logs_controller.rb index 3d073f4..0bf495d 100644 --- a/app/controllers/day_logs_controller.rb +++ b/app/controllers/day_logs_controller.rb @@ -1,8 +1,9 @@ class DayLogsController < ApplicationController - before_action :set_day_log, only: [ :edit, :update ] + before_action :set_day_log, only: [ :update ] - def new - @day_log = DayLog.new + def edit + @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 def create @@ -16,9 +17,6 @@ class DayLogsController < ApplicationController end end - def edit - end - def update if @day_log.update(day_log_params) handle_mood(@day_log.day, params[:day_log][:mode_id]) diff --git a/app/javascript/controllers/mood_controller.js b/app/javascript/controllers/mood_controller.js index 382aaa5..0ec087c 100644 --- a/app/javascript/controllers/mood_controller.js +++ b/app/javascript/controllers/mood_controller.js @@ -1,16 +1,19 @@ import { Controller } from '@hotwired/stimulus' export default class extends Controller { - static targets = [ "image", "modeDay" ]; + static targets = [ "image", "modeDay", "dayLogLink" ]; updateDayInfo(event) { const image = this.imageTarget; 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; modeDay.textContent = modeDayContent; event.target.className = "selected-day " + event.target.dataset.mode; + this.dayLogLinkTarget.href = `/day_logs/edit?day=${day}` + if (this.lastTarget) { this.lastTarget.className = "day " + this.lastTarget.dataset.mode; } diff --git a/app/views/moods/index.html.erb b/app/views/moods/index.html.erb index 5f4dfab..2621ce6 100644 --- a/app/views/moods/index.html.erb +++ b/app/views/moods/index.html.erb @@ -23,8 +23,10 @@
Aujourd'hui : <%= @user.current_mode.label %> + <%= link_to "Journal", edit_day_log_for_day_path(day: Date.today), "data-mood-target": "dayLogLink" %>
+
<% @user.history.each do |month| %> diff --git a/config/routes.rb b/config/routes.rb index 36f48c0..ffc80b7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -22,5 +22,6 @@ Rails.application.routes.draw do 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 diff --git a/spec/requests/day_logs_spec.rb b/spec/requests/day_logs_spec.rb index ef68b22..daa6285 100644 --- a/spec/requests/day_logs_spec.rb +++ b/spec/requests/day_logs_spec.rb @@ -5,10 +5,10 @@ describe "DayLogs", type: :request do let(:mode) { create(:mode, user: user) } let(:day) { Date.parse("2026-01-15") } - describe "GET /day_logs/new" do + describe "GET /day_logs/edit" do context "when not authenticated" 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) end end @@ -16,9 +16,26 @@ describe "DayLogs", type: :request do context "when authenticated" do before { login_as(user) } - it "returns http success" do - get new_day_log_path - expect(response).to have_http_status(:success) + 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 @@ -126,6 +143,41 @@ describe "DayLogs", type: :request do 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 let!(:day_log) { create(:day_log, user: user, day: day) }