can edit selected day
This commit is contained in:
parent
46e6014fea
commit
a9f72b60af
5 changed files with 70 additions and 14 deletions
|
|
@ -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])
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,8 +23,10 @@
|
|||
<div class="title is-4">
|
||||
<span class="icon"><i class="fa-regular fa-calendar"></i></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 class="logs">
|
||||
<div class="is-flex is-flex-direction-row is-flex-wrap-wrap mb-3">
|
||||
<% @user.history.each do |month| %>
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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) }
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue