From 962915829f5e56772bf33033ed71c621092c4b5e Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Thu, 19 Mar 2026 19:33:52 +0100 Subject: [PATCH] add day_log model --- app/models/day_log.rb | 6 +++++ db/migrate/20260319181607_create_day_logs.rb | 13 +++++++++++ db/schema.rb | 13 ++++++++++- spec/factories/day_logs.rb | 7 ++++++ spec/models/day_log_spec.rb | 24 ++++++++++++++++++++ 5 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 app/models/day_log.rb create mode 100644 db/migrate/20260319181607_create_day_logs.rb create mode 100644 spec/factories/day_logs.rb create mode 100644 spec/models/day_log_spec.rb diff --git a/app/models/day_log.rb b/app/models/day_log.rb new file mode 100644 index 0000000..3436a50 --- /dev/null +++ b/app/models/day_log.rb @@ -0,0 +1,6 @@ +class DayLog < ApplicationRecord + belongs_to :user + + validates :day, presence: true + validates :info, presence: true +end diff --git a/db/migrate/20260319181607_create_day_logs.rb b/db/migrate/20260319181607_create_day_logs.rb new file mode 100644 index 0000000..0693614 --- /dev/null +++ b/db/migrate/20260319181607_create_day_logs.rb @@ -0,0 +1,13 @@ +class CreateDayLogs < ActiveRecord::Migration[8.0] + def change + create_table :day_logs do |t| + t.date :day, null: false + t.text :info, null: false + t.references :user, null: false, foreign_key: true + + t.index :day, unique: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 86844f0..c7a6b6f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2026_03_15_183258) do +ActiveRecord::Schema[8.0].define(version: 2026_03_19_181607) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -39,6 +39,16 @@ ActiveRecord::Schema[8.0].define(version: 2026_03_15_183258) do t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true end + create_table "day_logs", force: :cascade do |t| + t.date "day", null: false + t.text "info", null: false + t.integer "user_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["day"], name: "index_day_logs_on_day", unique: true + t.index ["user_id"], name: "index_day_logs_on_user_id" + end + create_table "modes", force: :cascade do |t| t.string "label" t.string "color" @@ -92,6 +102,7 @@ ActiveRecord::Schema[8.0].define(version: 2026_03_15_183258) do add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" + add_foreign_key "day_logs", "users" add_foreign_key "modes", "users" add_foreign_key "moods", "modes" add_foreign_key "moods", "users" diff --git a/spec/factories/day_logs.rb b/spec/factories/day_logs.rb new file mode 100644 index 0000000..e38c22b --- /dev/null +++ b/spec/factories/day_logs.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :day_log do + day { "2026-03-19" } + info { "Arrivée à Paris" } + user { association :user } + end +end diff --git a/spec/models/day_log_spec.rb b/spec/models/day_log_spec.rb new file mode 100644 index 0000000..f563c5b --- /dev/null +++ b/spec/models/day_log_spec.rb @@ -0,0 +1,24 @@ +require 'rails_helper' + +describe DayLog, type: :model do + subject { build(:day_log) } + + it { is_expected.to be_valid } + + describe "validations" do + it "is invalid without a day" do + subject.day = nil + expect(subject).not_to be_valid + end + + it "is invalid without info" do + subject.info = nil + expect(subject).not_to be_valid + end + + it "is invalid without a user" do + subject.user = nil + expect(subject).not_to be_valid + end + end +end