add day logs controller

This commit is contained in:
Christophe Robillard 2026-03-19 21:21:28 +01:00
parent 5c67b91302
commit 8a5ed6efc7
7 changed files with 116 additions and 1 deletions

View file

@ -0,0 +1,21 @@
class DayLogsController < ApplicationController
def new
@day_log = DayLog.new
end
def create
@day_log = Current.user.day_logs.build(day_log_params)
if @day_log.save
redirect_to root_path
else
render :new, status: :unprocessable_entity
end
end
private
def day_log_params
params.expect(day_log: [ :day, :info ])
end
end

View file

@ -3,6 +3,7 @@ class User < ApplicationRecord
has_many :sessions, dependent: :destroy has_many :sessions, dependent: :destroy
has_many :moods, -> { order "recorded_at" } has_many :moods, -> { order "recorded_at" }
has_many :modes has_many :modes
has_many :day_logs
normalizes :email_address, with: ->(e) { e.strip.downcase } normalizes :email_address, with: ->(e) { e.strip.downcase }

View file

@ -0,0 +1,15 @@
%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
= f.submit

View file

@ -21,4 +21,6 @@ Rails.application.routes.draw do
patch "/invite/:token", to: "invitations#update", as: :invitation patch "/invite/:token", to: "invitations#update", as: :invitation
get "/moods", to: "moods#index", as: :dashboard get "/moods", to: "moods#index", as: :dashboard
resources :day_logs, only: [ :new, :create ]
end end

View file

@ -2,6 +2,7 @@ FactoryBot.define do
factory :user do factory :user do
sequence(:email_address) { |n| "user#{n}@example.com" } sequence(:email_address) { |n| "user#{n}@example.com" }
sequence(:username) { |n| "user#{n}" } sequence(:username) { |n| "user#{n}" }
password_digest { BCrypt::Password.create('password123') } password { "obanonalors" }
password_digest { BCrypt::Password.create(password) }
end end
end end

View file

@ -0,0 +1,63 @@
require 'rails_helper'
RSpec.describe "DayLogs", type: :request do
let(:user) { create(:user) }
let(:day) { Date.new(2026, 1, 15) }
describe "GET /day_logs/new" do
context "when not authenticated" do
it "redirects to the login page" do
get new_day_log_path
expect(response).to redirect_to(new_session_path)
end
end
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)
end
end
end
describe "POST /day_logs" do
context "when not authenticated" do
it "redirects to the login page" do
post day_logs_path, params: { day_log: { day: day, info: "Une info" } }
expect(response).to redirect_to(new_session_path)
end
end
context "when authenticated" do
before { login_as(user) }
context "with valid params" do
it "creates a day_log" do
expect {
post day_logs_path, params: { day_log: { day: day, info: "Une info" } }
}.to change(DayLog, :count).by(1)
end
it "redirects after creation" do
post day_logs_path, params: { day_log: { day: day, info: "Une info" } }
expect(response).to have_http_status(:redirect)
end
end
context "with invalid params" do
it "does not create a day_log" do
expect {
post day_logs_path, params: { day_log: { day: nil, info: nil } }
}.not_to change(DayLog, :count)
end
it "returns unprocessable entity" do
post day_logs_path, params: { day_log: { day: nil, info: nil } }
expect(response).to have_http_status(:unprocessable_entity)
end
end
end
end
end

View file

@ -0,0 +1,12 @@
module AuthenticationHelper
def login_as(user)
post session_path, params: {
email_address: user.email_address,
password: user.password
}
end
end
RSpec.configure do |config|
config.include AuthenticationHelper, type: :request
end