From 46e6014fea199f1f3893f80e2e46d9d4670faeb8 Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Fri, 20 Mar 2026 19:28:54 +0100 Subject: [PATCH] update day_log --- app/controllers/day_logs_controller.rb | 20 +++++- app/views/day_logs/_form.html.haml | 20 ++++++ app/views/day_logs/edit.html.haml | 3 + app/views/day_logs/new.html.haml | 20 +----- config/routes.rb | 2 +- spec/requests/day_logs_spec.rb | 95 +++++++++++++++++++++++++- 6 files changed, 137 insertions(+), 23 deletions(-) create mode 100644 app/views/day_logs/_form.html.haml create mode 100644 app/views/day_logs/edit.html.haml diff --git a/app/controllers/day_logs_controller.rb b/app/controllers/day_logs_controller.rb index 45aeba2..3d073f4 100644 --- a/app/controllers/day_logs_controller.rb +++ b/app/controllers/day_logs_controller.rb @@ -1,4 +1,6 @@ class DayLogsController < ApplicationController + before_action :set_day_log, only: [ :edit, :update ] + def new @day_log = DayLog.new end @@ -10,12 +12,28 @@ class DayLogsController < ApplicationController handle_mood(@day_log.day, params[:day_log][:mode_id]) redirect_to root_path else - render :new, status: :unprocessable_entity + 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? diff --git a/app/views/day_logs/_form.html.haml b/app/views/day_logs/_form.html.haml new file mode 100644 index 0000000..c97e7a3 --- /dev/null +++ b/app/views/day_logs/_form.html.haml @@ -0,0 +1,20 @@ += form_with model: day_log do |f| + - @day_log.errors.full_messages.each do |msg| + %p= msg + + .field + = f.label :day + = f.date_field :day + + .field + = f.label :info + = f.text_area :info + + .field + = label_tag :mode_id, "Mode" + = select_tag "day_log[mode_id]", + options_from_collection_for_select(Current.user.modes, :id, :label), + include_blank: true + + = f.submit + diff --git a/app/views/day_logs/edit.html.haml b/app/views/day_logs/edit.html.haml new file mode 100644 index 0000000..1f3fcaa --- /dev/null +++ b/app/views/day_logs/edit.html.haml @@ -0,0 +1,3 @@ +%h1 Modifier le journal + += render 'form', day_log: @day_log diff --git a/app/views/day_logs/new.html.haml b/app/views/day_logs/new.html.haml index 0bf5050..6a6e055 100644 --- a/app/views/day_logs/new.html.haml +++ b/app/views/day_logs/new.html.haml @@ -1,21 +1,3 @@ %h1 Nouveau journal -= form_with model: @day_log do |f| - - @day_log.errors.full_messages.each do |msg| - %p= msg - - .field - = f.label :day - = f.date_field :day - - .field - = f.label :info - = f.text_area :info - - .field - = label_tag :mode_id, "Mode" - = select_tag "day_log[mode_id]", - options_from_collection_for_select(Current.user.modes, :id, :label), - include_blank: true - - = f.submit += render 'form', day_log: @day_log diff --git a/config/routes.rb b/config/routes.rb index 97b69df..36f48c0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -22,5 +22,5 @@ Rails.application.routes.draw do get "/moods", to: "moods#index", as: :dashboard - resources :day_logs, only: [ :new, :create ] + resources :day_logs, only: [ :new, :create, :edit, :update ] end diff --git a/spec/requests/day_logs_spec.rb b/spec/requests/day_logs_spec.rb index 40b6e77..ef68b22 100644 --- a/spec/requests/day_logs_spec.rb +++ b/spec/requests/day_logs_spec.rb @@ -98,9 +98,100 @@ describe "DayLogs", type: :request do }.not_to change(DayLog, :count) end - it "returns unprocessable entity" do + it "returns unprocessable content" do post day_logs_path, params: { day_log: { day: nil, info: nil } } - expect(response).to have_http_status(:unprocessable_entity) + expect(response).to have_http_status(:unprocessable_content) + end + end + end + end + + describe "GET /day_logs/:id/edit" do + let!(:day_log) { create(:day_log, user: user, day: day) } + + context "when not authenticated" do + it "redirects to the login page" do + get edit_day_log_path(day_log) + expect(response).to redirect_to(new_session_path) + end + end + + context "when authenticated" do + before { login_as(user) } + + it "returns http success" do + get edit_day_log_path(day_log) + expect(response).to have_http_status(:success) + end + end + end + + describe "PATCH /day_logs/:id" do + let!(:day_log) { create(:day_log, user: user, day: day) } + + context "when not authenticated" do + it "redirects to the login page" do + patch day_log_path(day_log), params: { day_log: { info: "Nouvelle info" } } + expect(response).to redirect_to(new_session_path) + end + end + + context "when authenticated" do + before { login_as(user) } + + context "with valid params" do + context "without mode" do + it "updates the day_log" do + patch day_log_path(day_log), params: { day_log: { info: "Nouvelle info" } } + expect(day_log.reload.info).to eq("Nouvelle info") + end + + it "does not create a mood" do + expect { + patch day_log_path(day_log), params: { day_log: { info: "Nouvelle info" } } + }.not_to change(Mood, :count) + end + + it "redirects to dashboard" do + patch day_log_path(day_log), params: { day_log: { info: "Nouvelle info" } } + expect(response).to redirect_to(dashboard_path) + end + end + + context "with mode, no existing mood" do + it "creates a mood" do + expect { + patch day_log_path(day_log), params: { day_log: { info: "Nouvelle info", mode_id: mode.id } } + }.to change(Mood, :count).by(1) + end + end + + context "with mode, existing mood" do + let!(:existing_mood) { create(:mood, user: user, mode: mode, recorded_at: DateTime.parse("2026-01-15 08:00:00")) } + let(:other_mode) { create(:mode, user: user) } + + it "does not create a new mood" do + expect { + patch day_log_path(day_log), params: { day_log: { info: "Nouvelle info", mode_id: other_mode.id } } + }.not_to change(Mood, :count) + end + + it "updates the existing mood mode" do + patch day_log_path(day_log), params: { day_log: { info: "Nouvelle info", mode_id: other_mode.id } } + expect(existing_mood.reload.mode).to eq(other_mode) + end + end + end + + context "with invalid params" do + it "does not update the day_log" do + patch day_log_path(day_log), params: { day_log: { info: nil } } + expect(day_log.reload.info).not_to be_nil + end + + it "returns unprocessable content" do + patch day_log_path(day_log), params: { day_log: { info: nil } } + expect(response).to have_http_status(:unprocessable_content) end end end