From e7bee77eea65e2744dc64a631e390f35b15055e6 Mon Sep 17 00:00:00 2001
From: Rikuoh <mail@riq0h.jp>
Date: Mon, 24 Feb 2025 23:09:54 +0900
Subject: [PATCH] =?UTF-8?q?=E3=83=A6=E3=83=BC=E3=82=B6=E3=81=94=E3=81=A8?=
 =?UTF-8?q?=E3=81=AB=E5=80=8B=E5=88=A5=E3=81=AE=E3=83=A1=E3=83=A2=E3=82=92?=
 =?UTF-8?q?=E8=A1=A8=E7=A4=BA=E3=81=99=E3=82=8B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 app/controllers/memos_controller.rb            | 16 +++++++++++-----
 app/models/memo.rb                             |  1 +
 app/models/user.rb                             |  1 +
 app/views/layouts/application.html.erb         |  6 ++++++
 db/migrate/20250224135719_add_user_to_memos.rb |  5 +++++
 db/schema.rb                                   |  5 ++++-
 6 files changed, 28 insertions(+), 6 deletions(-)
 create mode 100644 db/migrate/20250224135719_add_user_to_memos.rb

diff --git a/app/controllers/memos_controller.rb b/app/controllers/memos_controller.rb
index c9c52fc..c0c599f 100644
--- a/app/controllers/memos_controller.rb
+++ b/app/controllers/memos_controller.rb
@@ -1,8 +1,9 @@
 class MemosController < ApplicationController
   before_action :set_memo, only: %i[edit update destroy]
+  before_action :authenticate_user!
 
   def index
-    @q = Memo.ransack(params[:q])
+    @q = current_user.memos.ransack(params[:q])
     @memos = @q.result(distinct: true)
                .order(created_at: :desc)
                .page(params[:page])
@@ -15,13 +16,15 @@ class MemosController < ApplicationController
   end
 
   def new
-    @memo = Memo.new
+    @memo = current_user.memos.build
   end
 
-  def edit; end
+  def edit
+    @memo = current_user.memos.find(params[:id])
+  end
 
   def create
-    @memo = Memo.new(memo_params)
+    @memo = current_user.memos.build(memo_params)
     respond_to do |format|
       if @memo.save
         format.html { redirect_to memos_path, status: :see_other }
@@ -56,7 +59,10 @@ class MemosController < ApplicationController
   private
 
   def set_memo
-    @memo = Memo.find(params[:id])
+    @memo = current_user.memos.find_by(id: params[:id])
+    return unless @memo.nil?
+
+    redirect_to memos_path, alert: 'メモが見つかりませんでした'
   end
 
   def memo_params
diff --git a/app/models/memo.rb b/app/models/memo.rb
index a1692c1..42236a5 100644
--- a/app/models/memo.rb
+++ b/app/models/memo.rb
@@ -1,4 +1,5 @@
 class Memo < ApplicationRecord
+  belongs_to :user
   has_one_attached :image
   validates :content, presence: true
 
diff --git a/app/models/user.rb b/app/models/user.rb
index 4756799..419b274 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -3,4 +3,5 @@ class User < ApplicationRecord
   # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
   devise :database_authenticatable, :registerable,
          :recoverable, :rememberable, :validatable
+  has_many :memos
 end
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index 0c480de..8d888de 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -10,6 +10,12 @@
     <%= javascript_include_tag "application", "data-turbo-track": "reload", type: "module", as: "script" %>
   </head>
 
+  <% if current_user %>
+  <div class="fixed top-4 left-4 text-sm text-gray-600 bg-gray-100 px-3 py-1 rounded-full">
+    <%= current_user.email %>
+  </div>
+<% end %>
+
   <body class="bg-gray-50">
   <% if user_signed_in? %>
     <div class="fixed top-4 right-4">
diff --git a/db/migrate/20250224135719_add_user_to_memos.rb b/db/migrate/20250224135719_add_user_to_memos.rb
new file mode 100644
index 0000000..3c91196
--- /dev/null
+++ b/db/migrate/20250224135719_add_user_to_memos.rb
@@ -0,0 +1,5 @@
+class AddUserToMemos < ActiveRecord::Migration[8.0]
+  def change
+    add_reference :memos, :user, null: false, foreign_key: true
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 3cac0c0..f930cfa 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: 2025_02_24_005433) do
+ActiveRecord::Schema[8.0].define(version: 2025_02_24_135719) do
   create_table "active_storage_attachments", force: :cascade do |t|
     t.string "name", null: false
     t.string "record_type", null: false
@@ -43,6 +43,8 @@ ActiveRecord::Schema[8.0].define(version: 2025_02_24_005433) do
     t.text "content", null: false
     t.datetime "created_at", null: false
     t.datetime "updated_at", null: false
+    t.integer "user_id", null: false
+    t.index ["user_id"], name: "index_memos_on_user_id"
   end
 
   create_table "users", force: :cascade do |t|
@@ -59,4 +61,5 @@ ActiveRecord::Schema[8.0].define(version: 2025_02_24_005433) 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 "memos", "users"
 end