From cbfc2c377c3f1f5d6cf0d13faa8d4f4ee8d0027a Mon Sep 17 00:00:00 2001 From: KOH6 Date: Tue, 26 Sep 2023 21:45:50 +0900 Subject: [PATCH 01/14] =?UTF-8?q?=E3=83=A1=E3=83=83=E3=82=BB=E3=83=BC?= =?UTF-8?q?=E3=82=B8=E3=81=AB=E9=96=A2=E3=81=99=E3=82=8BModel,=20Migrate?= =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E3=82=92=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/group.rb | 4 +++ app/models/group_member.rb | 4 +++ app/models/message.rb | 6 ++++ app/models/user.rb | 7 ++++- db/migrate/20230926121616_create_groups.rb | 8 +++++ .../20230926121639_create_group_members.rb | 10 +++++++ db/migrate/20230926121703_create_messages.rb | 11 +++++++ db/schema.rb | 30 ++++++++++++++++++- db/seeds.rb | 19 +++++++++++- spec/models/group_member_spec.rb | 5 ++++ spec/models/group_spec.rb | 5 ++++ spec/models/message_spec.rb | 5 ++++ 12 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 app/models/group.rb create mode 100644 app/models/group_member.rb create mode 100644 app/models/message.rb create mode 100644 db/migrate/20230926121616_create_groups.rb create mode 100644 db/migrate/20230926121639_create_group_members.rb create mode 100644 db/migrate/20230926121703_create_messages.rb create mode 100644 spec/models/group_member_spec.rb create mode 100644 spec/models/group_spec.rb create mode 100644 spec/models/message_spec.rb diff --git a/app/models/group.rb b/app/models/group.rb new file mode 100644 index 0000000..a91badc --- /dev/null +++ b/app/models/group.rb @@ -0,0 +1,4 @@ +class Group < ApplicationRecord + has_many :group_members, dependent: :destroy + has_many :messages, dependent: :destroy +end diff --git a/app/models/group_member.rb b/app/models/group_member.rb new file mode 100644 index 0000000..5bdf6dc --- /dev/null +++ b/app/models/group_member.rb @@ -0,0 +1,4 @@ +class GroupMember < ApplicationRecord + belongs_to :user + belongs_to :group +end diff --git a/app/models/message.rb b/app/models/message.rb new file mode 100644 index 0000000..5a40a00 --- /dev/null +++ b/app/models/message.rb @@ -0,0 +1,6 @@ +class Message < ApplicationRecord + belongs_to :user + belongs_to :group + + validates :content, presence: true +end diff --git a/app/models/user.rb b/app/models/user.rb index 2e94b34..ddbace1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -36,10 +36,15 @@ class User < ApplicationRecord has_many :bookmarks, dependent: :destroy has_many :bookmarking_posts, through: :bookmarks, source: :post + has_many :group_members, dependent: :destroy + has_many :groups, through: :group_members + + has_many :messages, dependent: :destroy + with_options presence: true do validates :name validates :user_name, uniqueness: true - # 電話番号と誕生日はGithubでの新規登録時には無効化する。 + # 電話番号と誕生日は画面からの新規登録時以外は無効化する。 validates :phone, unless: -> { validation_context == :not_new_form } validates :birthdate, unless: -> { validation_context == :not_new_form } end diff --git a/db/migrate/20230926121616_create_groups.rb b/db/migrate/20230926121616_create_groups.rb new file mode 100644 index 0000000..28ade04 --- /dev/null +++ b/db/migrate/20230926121616_create_groups.rb @@ -0,0 +1,8 @@ +class CreateGroups < ActiveRecord::Migration[7.0] + def change + create_table :groups do |t| + + t.timestamps + end + end +end diff --git a/db/migrate/20230926121639_create_group_members.rb b/db/migrate/20230926121639_create_group_members.rb new file mode 100644 index 0000000..6bc7f67 --- /dev/null +++ b/db/migrate/20230926121639_create_group_members.rb @@ -0,0 +1,10 @@ +class CreateGroupMembers < ActiveRecord::Migration[7.0] + def change + create_table :group_members do |t| + t.references :user, null: false, foreign_key: true + t.references :group, null: false, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/migrate/20230926121703_create_messages.rb b/db/migrate/20230926121703_create_messages.rb new file mode 100644 index 0000000..46a71e0 --- /dev/null +++ b/db/migrate/20230926121703_create_messages.rb @@ -0,0 +1,11 @@ +class CreateMessages < ActiveRecord::Migration[7.0] + def change + create_table :messages do |t| + t.references :user, null: false, foreign_key: true + t.references :group, null: false, foreign_key: true + t.text :content, null: false + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 8ef970c..572409b 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[7.0].define(version: 2023_09_26_082634) do +ActiveRecord::Schema[7.0].define(version: 2023_09_26_121703) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -72,6 +72,20 @@ t.index ["follower_id"], name: "index_follows_on_follower_id" end + create_table "group_members", force: :cascade do |t| + t.bigint "user_id", null: false + t.bigint "group_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["group_id"], name: "index_group_members_on_group_id" + t.index ["user_id"], name: "index_group_members_on_user_id" + end + + create_table "groups", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "likes", force: :cascade do |t| t.bigint "user_id", null: false t.bigint "post_id", null: false @@ -82,6 +96,16 @@ t.index ["user_id"], name: "index_likes_on_user_id" end + create_table "messages", force: :cascade do |t| + t.bigint "user_id", null: false + t.bigint "group_id", null: false + t.text "content", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["group_id"], name: "index_messages_on_group_id" + t.index ["user_id"], name: "index_messages_on_user_id" + end + create_table "posts", force: :cascade do |t| t.text "content", null: false t.bigint "user_id", null: false @@ -144,8 +168,12 @@ add_foreign_key "comments", "users" add_foreign_key "follows", "users", column: "followee_id" add_foreign_key "follows", "users", column: "follower_id" + add_foreign_key "group_members", "groups" + add_foreign_key "group_members", "users" add_foreign_key "likes", "posts" add_foreign_key "likes", "users" + add_foreign_key "messages", "groups" + add_foreign_key "messages", "users" add_foreign_key "posts", "users" add_foreign_key "reposts", "posts" add_foreign_key "reposts", "users" diff --git a/db/seeds.rb b/db/seeds.rb index 1b79774..6148e21 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -24,6 +24,23 @@ user_ids << user.id end +user_ids.each do |user_id| + other_ids = user_ids.reject { |id| id == user_id }.sample(rand(1...USER_COUNT)) + + other_ids.each do |other_id| + group = Group.create! + member_ids = [user_id, other_id] + rand(0..15).times do |n| + Message.create!( + user_id: member_ids.sample, + group_id: group.id, + content: "テストコメント#{n}です。\nテストコメント#{n}です。\nテストコメント#{n}です。" + ) + end + member_ids.each { |member_id| GroupMember.create!(user_id: member_id, group_id: group.id)} + end +end + POST_COUNT.times do |n| post = Post.create!( user_id: user_ids.sample, @@ -43,7 +60,7 @@ user_id: user_ids.reject { |id| id == post.user_id }.sample, post_id: post.id ) - rand(1..10).times do |m| + rand(0..10).times do |m| Comment.create!( user_id: user_ids.reject { |id| id == post.user_id }.sample, post_id: post.id, diff --git a/spec/models/group_member_spec.rb b/spec/models/group_member_spec.rb new file mode 100644 index 0000000..aebf030 --- /dev/null +++ b/spec/models/group_member_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe GroupMember, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb new file mode 100644 index 0000000..be100cf --- /dev/null +++ b/spec/models/group_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Group, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/message_spec.rb b/spec/models/message_spec.rb new file mode 100644 index 0000000..d74b6a8 --- /dev/null +++ b/spec/models/message_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Message, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end From d5fae6a3dd5c2899a04ad267518f704803befe1a Mon Sep 17 00:00:00 2001 From: KOH6 Date: Tue, 26 Sep 2023 21:52:44 +0900 Subject: [PATCH 02/14] =?UTF-8?q?=E3=82=B3=E3=83=B3=E3=83=88=E3=83=AD?= =?UTF-8?q?=E3=83=BC=E3=83=A9=E3=83=BC=E3=81=AE=E3=81=B2=E3=81=AA=E5=BD=A2?= =?UTF-8?q?=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/groups_controller.rb | 10 ++++++++++ app/views/groups/index.html.slim | 2 ++ app/views/groups/show.html.slim | 2 ++ app/views/shared/_sidebar.html.slim | 2 +- config/routes.rb | 2 ++ spec/requests/groups_spec.rb | 18 ++++++++++++++++++ 6 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 app/controllers/groups_controller.rb create mode 100644 app/views/groups/index.html.slim create mode 100644 app/views/groups/show.html.slim create mode 100644 spec/requests/groups_spec.rb diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb new file mode 100644 index 0000000..476cb51 --- /dev/null +++ b/app/controllers/groups_controller.rb @@ -0,0 +1,10 @@ +class GroupsController < ApplicationController + def index + end + + def show + end + + def create + end +end diff --git a/app/views/groups/index.html.slim b/app/views/groups/index.html.slim new file mode 100644 index 0000000..e6f6eed --- /dev/null +++ b/app/views/groups/index.html.slim @@ -0,0 +1,2 @@ +h1 Groups#index +p Find me in app/views/groups/index.html.slim diff --git a/app/views/groups/show.html.slim b/app/views/groups/show.html.slim new file mode 100644 index 0000000..e523947 --- /dev/null +++ b/app/views/groups/show.html.slim @@ -0,0 +1,2 @@ +h1 Groups#show +p Find me in app/views/groups/show.html.slim diff --git a/app/views/shared/_sidebar.html.slim b/app/views/shared/_sidebar.html.slim index 6265f7b..d374165 100644 --- a/app/views/shared/_sidebar.html.slim +++ b/app/views/shared/_sidebar.html.slim @@ -18,7 +18,7 @@ span.ms-1.d-none.d-sm-inline[style="font-size:1.5rem"] | 通知 li - a.nav-link.align-middle.text-dark.px-0.d-flex.align-items-center[href="#"] + = link_to groups_path, data: { turbo: false }, class: "nav-link align-middle text-dark px-0 d-flex align-items-center" i.fs-4.bi-envelope.mx-2 span.ms-1.d-none.d-sm-inline[style="font-size:1.5rem"] | メッセージ diff --git a/config/routes.rb b/config/routes.rb index 12a6b12..e64f1dc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,10 +15,12 @@ resources :users, only: %i[show edit update] do resources :follows, only: %i[create destroy] + resources :groups, only: %i[create] end resources :bookmarks, only: %i[index] resources :comments, only: %i[create] + resources :groups, only: %i[index show] root 'posts#index' end diff --git a/spec/requests/groups_spec.rb b/spec/requests/groups_spec.rb new file mode 100644 index 0000000..b5d55d9 --- /dev/null +++ b/spec/requests/groups_spec.rb @@ -0,0 +1,18 @@ +require 'rails_helper' + +RSpec.describe "Groups", type: :request do + describe "GET /index" do + it "returns http success" do + get "/groups/index" + expect(response).to have_http_status(:success) + end + end + + describe "GET /show" do + it "returns http success" do + get "/groups/show" + expect(response).to have_http_status(:success) + end + end + +end From 318ac78d209d12ee144b13c3f5d9e7add36bd586 Mon Sep 17 00:00:00 2001 From: KOH6 Date: Wed, 27 Sep 2023 18:04:44 +0900 Subject: [PATCH 03/14] =?UTF-8?q?=E9=83=A8=E5=B1=8B=E3=81=AE=E8=A1=A8?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/groups_controller.rb | 7 +++++++ app/views/groups/index.html.slim | 13 +++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 476cb51..9f764d4 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -1,5 +1,12 @@ class GroupsController < ApplicationController + before_action :set_user + def index + @groups = current_user.groups + @other_users = @groups.map do |group| + group.group_members.where.not(user_id: current_user.id).first.user + end + end def show diff --git a/app/views/groups/index.html.slim b/app/views/groups/index.html.slim index e6f6eed..06215dc 100644 --- a/app/views/groups/index.html.slim +++ b/app/views/groups/index.html.slim @@ -1,2 +1,11 @@ -h1 Groups#index -p Find me in app/views/groups/index.html.slim +.container-fluid + = render 'shared/navigation' + .row.flex-nowrap + = render 'shared/sidebar', :user => @user + .col + = render 'shared/flash' + h3.fw-bold.my-3 + | メッセージ + - @other_users.each do |other_user| + = "#{other_user.name}の部屋" + .br From 09092a2a5646b17d56156c47f6f327a2e37df34f Mon Sep 17 00:00:00 2001 From: KOH6 Date: Wed, 27 Sep 2023 23:39:15 +0900 Subject: [PATCH 04/14] =?UTF-8?q?=E3=83=A1=E3=83=83=E3=82=BB=E3=83=BC?= =?UTF-8?q?=E3=82=B8=E8=A9=B3=E7=B4=B0=E8=A1=A8=E7=A4=BA=E6=A9=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/groups_controller.rb | 24 +++++++++++++++++------- app/models/message.rb | 2 ++ app/views/groups/index.html.slim | 8 +++++--- app/views/groups/show.html.slim | 23 +++++++++++++++++++++-- db/seeds.rb | 7 ++++++- 5 files changed, 51 insertions(+), 13 deletions(-) diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 9f764d4..ae8dcbe 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -1,17 +1,27 @@ class GroupsController < ApplicationController before_action :set_user + before_action :set_group_infos, only: %i[index show] - def index - @groups = current_user.groups - @other_users = @groups.map do |group| - group.group_members.where.not(user_id: current_user.id).first.user - end - - end + def index; end def show + group = current_user.groups.find(params[:id]) + @messages = group.messages.includes(:user).latest end def create end + + private + + def set_group_infos + @group_infos = [] + groups = current_user.groups.distinct + groups.map do |group| + group_info = {} + group_info[:group_id] = group.id + group_info[:user] = group.group_members.where.not(user_id: current_user.id).first.user + @group_infos << group_info + end + end end diff --git a/app/models/message.rb b/app/models/message.rb index 5a40a00..100dfe3 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -3,4 +3,6 @@ class Message < ApplicationRecord belongs_to :group validates :content, presence: true + + scope :latest, -> { order(created_at: :desc) } end diff --git a/app/views/groups/index.html.slim b/app/views/groups/index.html.slim index 06215dc..373df9f 100644 --- a/app/views/groups/index.html.slim +++ b/app/views/groups/index.html.slim @@ -6,6 +6,8 @@ = render 'shared/flash' h3.fw-bold.my-3 | メッセージ - - @other_users.each do |other_user| - = "#{other_user.name}の部屋" - .br + - @group_infos.each do |group_info| + = link_to group_path(group_info[:group_id]), data: { turbo: false } + = "#{group_info[:user].name}の部屋" + = "#{group_info[:group_id]}のグループID" + hr diff --git a/app/views/groups/show.html.slim b/app/views/groups/show.html.slim index e523947..062e3cf 100644 --- a/app/views/groups/show.html.slim +++ b/app/views/groups/show.html.slim @@ -1,2 +1,21 @@ -h1 Groups#show -p Find me in app/views/groups/show.html.slim +.container-fluid + = render 'shared/navigation' + .row.flex-nowrap + = render 'shared/sidebar', :user => @user + .col + = render 'shared/flash' + h3.fw-bold.my-3 + | メッセージ + - @group_infos.each do |group_info| + = link_to group_path(group_info[:group_id]), data: { turbo: false } + = "#{group_info[:user].name}の部屋" + = "#{group_info[:group_id]}のグループID" + hr + .col + h3.fw-bold.my-3 + | メッセージ詳細 + - @messages.each do |message| + = "投稿者:#{message.user.name}" + = "内容:#{message.content}" + = "投稿日時:#{l message.created_at}" + hr diff --git a/db/seeds.rb b/db/seeds.rb index 6148e21..6b6de1b 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -28,7 +28,12 @@ other_ids = user_ids.reject { |id| id == user_id }.sample(rand(1...USER_COUNT)) other_ids.each do |other_id| - group = Group.create! + groups = GroupMember.where(user_id:).map(&:group) + other_groups = [] + groups.each do |group| + other_groups << group if group.group_members.find_by(user_id: other_id) + end + group = other_groups.size.zero? ? Group.create! : other_groups.first member_ids = [user_id, other_id] rand(0..15).times do |n| Message.create!( From 3d3fba1f3aa028ef979ee1d3443a2dbaf226f22c Mon Sep 17 00:00:00 2001 From: KOH6 Date: Thu, 28 Sep 2023 09:33:56 +0900 Subject: [PATCH 05/14] =?UTF-8?q?Groups=E3=83=86=E3=83=BC=E3=83=96?= =?UTF-8?q?=E3=83=AB=E3=82=92Rooms=E3=83=86=E3=83=BC=E3=83=96=E3=83=AB?= =?UTF-8?q?=E3=81=ABrename?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/groups_controller.rb | 27 ------------- app/controllers/rooms_controller.rb | 27 +++++++++++++ app/models/group.rb | 4 -- app/models/group_member.rb | 4 -- app/models/message.rb | 2 +- app/models/room.rb | 4 ++ app/models/room_member.rb | 4 ++ app/models/user.rb | 4 +- app/views/groups/index.html.slim | 13 ------- app/views/rooms/index.html.slim | 13 +++++++ app/views/{groups => rooms}/show.html.slim | 8 ++-- app/views/shared/_sidebar.html.slim | 2 +- config/routes.rb | 4 +- db/migrate/20230926121616_create_groups.rb | 8 ---- db/migrate/20230926121616_create_rooms.rb | 8 ++++ .../20230926121639_create_group_members.rb | 10 ----- .../20230926121639_create_room_members.rb | 10 +++++ db/migrate/20230926121703_create_messages.rb | 2 +- db/schema.rb | 38 +++++++++---------- db/seeds.rb | 14 +++---- ...oup_member_spec.rb => room_member_spec.rb} | 2 +- spec/models/{group_spec.rb => room_spec.rb} | 2 +- .../{groups_spec.rb => rooms_spec.rb} | 2 +- 23 files changed, 106 insertions(+), 106 deletions(-) delete mode 100644 app/controllers/groups_controller.rb create mode 100644 app/controllers/rooms_controller.rb delete mode 100644 app/models/group.rb delete mode 100644 app/models/group_member.rb create mode 100644 app/models/room.rb create mode 100644 app/models/room_member.rb delete mode 100644 app/views/groups/index.html.slim create mode 100644 app/views/rooms/index.html.slim rename app/views/{groups => rooms}/show.html.slim (67%) delete mode 100644 db/migrate/20230926121616_create_groups.rb create mode 100644 db/migrate/20230926121616_create_rooms.rb delete mode 100644 db/migrate/20230926121639_create_group_members.rb create mode 100644 db/migrate/20230926121639_create_room_members.rb rename spec/models/{group_member_spec.rb => room_member_spec.rb} (65%) rename spec/models/{group_spec.rb => room_spec.rb} (69%) rename spec/requests/{groups_spec.rb => rooms_spec.rb} (88%) diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb deleted file mode 100644 index ae8dcbe..0000000 --- a/app/controllers/groups_controller.rb +++ /dev/null @@ -1,27 +0,0 @@ -class GroupsController < ApplicationController - before_action :set_user - before_action :set_group_infos, only: %i[index show] - - def index; end - - def show - group = current_user.groups.find(params[:id]) - @messages = group.messages.includes(:user).latest - end - - def create - end - - private - - def set_group_infos - @group_infos = [] - groups = current_user.groups.distinct - groups.map do |group| - group_info = {} - group_info[:group_id] = group.id - group_info[:user] = group.group_members.where.not(user_id: current_user.id).first.user - @group_infos << group_info - end - end -end diff --git a/app/controllers/rooms_controller.rb b/app/controllers/rooms_controller.rb new file mode 100644 index 0000000..778c698 --- /dev/null +++ b/app/controllers/rooms_controller.rb @@ -0,0 +1,27 @@ +class RoomsController < ApplicationController + before_action :set_user + before_action :set_room_infos, only: %i[index show] + + def index; end + + def show + room = current_user.rooms.find(params[:id]) + @messages = room.messages.includes(:user).latest + end + + def create + end + + private + + def set_room_infos + @room_infos = [] + rooms = current_user.rooms.distinct + rooms.map do |room| + room_info = {} + room_info[:room_id] = room.id + room_info[:user] = room.room_members.where.not(user_id: current_user.id).first.user + @room_infos << room_info + end + end +end diff --git a/app/models/group.rb b/app/models/group.rb deleted file mode 100644 index a91badc..0000000 --- a/app/models/group.rb +++ /dev/null @@ -1,4 +0,0 @@ -class Group < ApplicationRecord - has_many :group_members, dependent: :destroy - has_many :messages, dependent: :destroy -end diff --git a/app/models/group_member.rb b/app/models/group_member.rb deleted file mode 100644 index 5bdf6dc..0000000 --- a/app/models/group_member.rb +++ /dev/null @@ -1,4 +0,0 @@ -class GroupMember < ApplicationRecord - belongs_to :user - belongs_to :group -end diff --git a/app/models/message.rb b/app/models/message.rb index 100dfe3..7cf7ec9 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -1,6 +1,6 @@ class Message < ApplicationRecord belongs_to :user - belongs_to :group + belongs_to :room validates :content, presence: true diff --git a/app/models/room.rb b/app/models/room.rb new file mode 100644 index 0000000..d80d02c --- /dev/null +++ b/app/models/room.rb @@ -0,0 +1,4 @@ +class Room < ApplicationRecord + has_many :room_members, dependent: :destroy + has_many :messages, dependent: :destroy +end diff --git a/app/models/room_member.rb b/app/models/room_member.rb new file mode 100644 index 0000000..46a90ac --- /dev/null +++ b/app/models/room_member.rb @@ -0,0 +1,4 @@ +class RoomMember < ApplicationRecord + belongs_to :user + belongs_to :room +end diff --git a/app/models/user.rb b/app/models/user.rb index ddbace1..8d73d6a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -36,8 +36,8 @@ class User < ApplicationRecord has_many :bookmarks, dependent: :destroy has_many :bookmarking_posts, through: :bookmarks, source: :post - has_many :group_members, dependent: :destroy - has_many :groups, through: :group_members + has_many :room_members, dependent: :destroy + has_many :rooms, through: :room_members has_many :messages, dependent: :destroy diff --git a/app/views/groups/index.html.slim b/app/views/groups/index.html.slim deleted file mode 100644 index 373df9f..0000000 --- a/app/views/groups/index.html.slim +++ /dev/null @@ -1,13 +0,0 @@ -.container-fluid - = render 'shared/navigation' - .row.flex-nowrap - = render 'shared/sidebar', :user => @user - .col - = render 'shared/flash' - h3.fw-bold.my-3 - | メッセージ - - @group_infos.each do |group_info| - = link_to group_path(group_info[:group_id]), data: { turbo: false } - = "#{group_info[:user].name}の部屋" - = "#{group_info[:group_id]}のグループID" - hr diff --git a/app/views/rooms/index.html.slim b/app/views/rooms/index.html.slim new file mode 100644 index 0000000..b45290a --- /dev/null +++ b/app/views/rooms/index.html.slim @@ -0,0 +1,13 @@ +.container-fluid + = render 'shared/navigation' + .row.flex-nowrap + = render 'shared/sidebar', :user => @user + .col + = render 'shared/flash' + h3.fw-bold.my-3 + | メッセージ + - @room_infos.each do |room_info| + = link_to room_path(room_info[:room_id]), data: { turbo: false } + = "#{room_info[:user].name}の部屋" + = "#{room_info[:room_id]}のグループID" + hr diff --git a/app/views/groups/show.html.slim b/app/views/rooms/show.html.slim similarity index 67% rename from app/views/groups/show.html.slim rename to app/views/rooms/show.html.slim index 062e3cf..96c7081 100644 --- a/app/views/groups/show.html.slim +++ b/app/views/rooms/show.html.slim @@ -6,10 +6,10 @@ = render 'shared/flash' h3.fw-bold.my-3 | メッセージ - - @group_infos.each do |group_info| - = link_to group_path(group_info[:group_id]), data: { turbo: false } - = "#{group_info[:user].name}の部屋" - = "#{group_info[:group_id]}のグループID" + - @room_infos.each do |room_info| + = link_to room_path(room_info[:room_id]), data: { turbo: false } + = "#{room_info[:user].name}の部屋" + = "#{room_info[:room_id]}のグループID" hr .col h3.fw-bold.my-3 diff --git a/app/views/shared/_sidebar.html.slim b/app/views/shared/_sidebar.html.slim index d374165..fe11f1e 100644 --- a/app/views/shared/_sidebar.html.slim +++ b/app/views/shared/_sidebar.html.slim @@ -18,7 +18,7 @@ span.ms-1.d-none.d-sm-inline[style="font-size:1.5rem"] | 通知 li - = link_to groups_path, data: { turbo: false }, class: "nav-link align-middle text-dark px-0 d-flex align-items-center" + = link_to rooms_path, data: { turbo: false }, class: "nav-link align-middle text-dark px-0 d-flex align-items-center" i.fs-4.bi-envelope.mx-2 span.ms-1.d-none.d-sm-inline[style="font-size:1.5rem"] | メッセージ diff --git a/config/routes.rb b/config/routes.rb index e64f1dc..03df74a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,12 +15,12 @@ resources :users, only: %i[show edit update] do resources :follows, only: %i[create destroy] - resources :groups, only: %i[create] + resources :rooms, only: %i[create] end resources :bookmarks, only: %i[index] resources :comments, only: %i[create] - resources :groups, only: %i[index show] + resources :rooms, only: %i[index show] root 'posts#index' end diff --git a/db/migrate/20230926121616_create_groups.rb b/db/migrate/20230926121616_create_groups.rb deleted file mode 100644 index 28ade04..0000000 --- a/db/migrate/20230926121616_create_groups.rb +++ /dev/null @@ -1,8 +0,0 @@ -class CreateGroups < ActiveRecord::Migration[7.0] - def change - create_table :groups do |t| - - t.timestamps - end - end -end diff --git a/db/migrate/20230926121616_create_rooms.rb b/db/migrate/20230926121616_create_rooms.rb new file mode 100644 index 0000000..93fbab7 --- /dev/null +++ b/db/migrate/20230926121616_create_rooms.rb @@ -0,0 +1,8 @@ +class CreateRooms < ActiveRecord::Migration[7.0] + def change + create_table :rooms do |t| + + t.timestamps + end + end +end diff --git a/db/migrate/20230926121639_create_group_members.rb b/db/migrate/20230926121639_create_group_members.rb deleted file mode 100644 index 6bc7f67..0000000 --- a/db/migrate/20230926121639_create_group_members.rb +++ /dev/null @@ -1,10 +0,0 @@ -class CreateGroupMembers < ActiveRecord::Migration[7.0] - def change - create_table :group_members do |t| - t.references :user, null: false, foreign_key: true - t.references :group, null: false, foreign_key: true - - t.timestamps - end - end -end diff --git a/db/migrate/20230926121639_create_room_members.rb b/db/migrate/20230926121639_create_room_members.rb new file mode 100644 index 0000000..55d75d7 --- /dev/null +++ b/db/migrate/20230926121639_create_room_members.rb @@ -0,0 +1,10 @@ +class CreateRoomMembers < ActiveRecord::Migration[7.0] + def change + create_table :room_members do |t| + t.references :user, null: false, foreign_key: true + t.references :room, null: false, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/migrate/20230926121703_create_messages.rb b/db/migrate/20230926121703_create_messages.rb index 46a71e0..9feb9b7 100644 --- a/db/migrate/20230926121703_create_messages.rb +++ b/db/migrate/20230926121703_create_messages.rb @@ -2,7 +2,7 @@ class CreateMessages < ActiveRecord::Migration[7.0] def change create_table :messages do |t| t.references :user, null: false, foreign_key: true - t.references :group, null: false, foreign_key: true + t.references :room, null: false, foreign_key: true t.text :content, null: false t.timestamps diff --git a/db/schema.rb b/db/schema.rb index 572409b..93e588b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -72,20 +72,6 @@ t.index ["follower_id"], name: "index_follows_on_follower_id" end - create_table "group_members", force: :cascade do |t| - t.bigint "user_id", null: false - t.bigint "group_id", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["group_id"], name: "index_group_members_on_group_id" - t.index ["user_id"], name: "index_group_members_on_user_id" - end - - create_table "groups", force: :cascade do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end - create_table "likes", force: :cascade do |t| t.bigint "user_id", null: false t.bigint "post_id", null: false @@ -98,11 +84,11 @@ create_table "messages", force: :cascade do |t| t.bigint "user_id", null: false - t.bigint "group_id", null: false + t.bigint "room_id", null: false t.text "content", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.index ["group_id"], name: "index_messages_on_group_id" + t.index ["room_id"], name: "index_messages_on_room_id" t.index ["user_id"], name: "index_messages_on_user_id" end @@ -124,6 +110,20 @@ t.index ["user_id"], name: "index_reposts_on_user_id" end + create_table "room_members", force: :cascade do |t| + t.bigint "user_id", null: false + t.bigint "room_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["room_id"], name: "index_room_members_on_room_id" + t.index ["user_id"], name: "index_room_members_on_user_id" + end + + create_table "rooms", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false @@ -168,13 +168,13 @@ add_foreign_key "comments", "users" add_foreign_key "follows", "users", column: "followee_id" add_foreign_key "follows", "users", column: "follower_id" - add_foreign_key "group_members", "groups" - add_foreign_key "group_members", "users" add_foreign_key "likes", "posts" add_foreign_key "likes", "users" - add_foreign_key "messages", "groups" + add_foreign_key "messages", "rooms" add_foreign_key "messages", "users" add_foreign_key "posts", "users" add_foreign_key "reposts", "posts" add_foreign_key "reposts", "users" + add_foreign_key "room_members", "rooms" + add_foreign_key "room_members", "users" end diff --git a/db/seeds.rb b/db/seeds.rb index 6b6de1b..3545b9b 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -28,21 +28,21 @@ other_ids = user_ids.reject { |id| id == user_id }.sample(rand(1...USER_COUNT)) other_ids.each do |other_id| - groups = GroupMember.where(user_id:).map(&:group) - other_groups = [] - groups.each do |group| - other_groups << group if group.group_members.find_by(user_id: other_id) + rooms = RoomMember.where(user_id:).map(&:room) + other_rooms = [] + rooms.each do |room| + other_rooms << room if room.room_members.find_by(user_id: other_id) end - group = other_groups.size.zero? ? Group.create! : other_groups.first + room = other_rooms.size.zero? ? Room.create! : other_rooms.first member_ids = [user_id, other_id] rand(0..15).times do |n| Message.create!( user_id: member_ids.sample, - group_id: group.id, + room_id: room.id, content: "テストコメント#{n}です。\nテストコメント#{n}です。\nテストコメント#{n}です。" ) end - member_ids.each { |member_id| GroupMember.create!(user_id: member_id, group_id: group.id)} + member_ids.each { |member_id| RoomMember.create!(user_id: member_id, room_id: room.id)} end end diff --git a/spec/models/group_member_spec.rb b/spec/models/room_member_spec.rb similarity index 65% rename from spec/models/group_member_spec.rb rename to spec/models/room_member_spec.rb index aebf030..a5c5fb4 100644 --- a/spec/models/group_member_spec.rb +++ b/spec/models/room_member_spec.rb @@ -1,5 +1,5 @@ require 'rails_helper' -RSpec.describe GroupMember, type: :model do +RSpec.describe RoomMember, type: :model do pending "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/models/group_spec.rb b/spec/models/room_spec.rb similarity index 69% rename from spec/models/group_spec.rb rename to spec/models/room_spec.rb index be100cf..dc75c5e 100644 --- a/spec/models/group_spec.rb +++ b/spec/models/room_spec.rb @@ -1,5 +1,5 @@ require 'rails_helper' -RSpec.describe Group, type: :model do +RSpec.describe Room, type: :model do pending "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/requests/groups_spec.rb b/spec/requests/rooms_spec.rb similarity index 88% rename from spec/requests/groups_spec.rb rename to spec/requests/rooms_spec.rb index b5d55d9..4c3a7f0 100644 --- a/spec/requests/groups_spec.rb +++ b/spec/requests/rooms_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe "Groups", type: :request do +RSpec.describe "Rooms", type: :request do describe "GET /index" do it "returns http success" do get "/groups/index" From 336e89bbef6cc245b0dc514534686cb0dee999be Mon Sep 17 00:00:00 2001 From: KOH6 Date: Thu, 28 Sep 2023 10:16:09 +0900 Subject: [PATCH 06/14] =?UTF-8?q?=E6=97=A2=E5=AD=98=E9=83=A8=E5=B1=8B?= =?UTF-8?q?=E5=8F=96=E5=BE=97=E5=87=A6=E7=90=86=E3=82=92Model=E3=81=B8?= =?UTF-8?q?=E7=A7=BB=E5=8B=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/room.rb | 12 ++++++++++++ db/seeds.rb | 16 ++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/app/models/room.rb b/app/models/room.rb index d80d02c..bc1e7fd 100644 --- a/app/models/room.rb +++ b/app/models/room.rb @@ -1,4 +1,16 @@ class Room < ApplicationRecord has_many :room_members, dependent: :destroy has_many :messages, dependent: :destroy + + def self.search_existing_room(user_id:, other_id:) + existing_room = nil + rooms = RoomMember.where(user_id:).map(&:room) + rooms.each do |room| + if room.room_members.find_by(user_id: other_id) + existing_room = room + break + end + end + existing_room + end end diff --git a/db/seeds.rb b/db/seeds.rb index 3545b9b..2c5344e 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -28,21 +28,21 @@ other_ids = user_ids.reject { |id| id == user_id }.sample(rand(1...USER_COUNT)) other_ids.each do |other_id| - rooms = RoomMember.where(user_id:).map(&:room) - other_rooms = [] - rooms.each do |room| - other_rooms << room if room.room_members.find_by(user_id: other_id) - end - room = other_rooms.size.zero? ? Room.create! : other_rooms.first member_ids = [user_id, other_id] + existing_room = Room.search_existing_room(user_id:, other_id:) + if existing_room + room = existing_room + else + room = Room.create! + member_ids.each { |member_id| RoomMember.create!(user_id: member_id, room_id: room.id)} + end rand(0..15).times do |n| Message.create!( user_id: member_ids.sample, room_id: room.id, - content: "テストコメント#{n}です。\nテストコメント#{n}です。\nテストコメント#{n}です。" + content: (existing_room ? "既存部屋です。" : "") + "テストコメント#{n}です。\nテストコメント#{n}です。\nテストコメント#{n}です。" ) end - member_ids.each { |member_id| RoomMember.create!(user_id: member_id, room_id: room.id)} end end From a0dd7886a06bee1af95fb93aeeec894325b15c24 Mon Sep 17 00:00:00 2001 From: KOH6 Date: Thu, 28 Sep 2023 10:16:51 +0900 Subject: [PATCH 07/14] =?UTF-8?q?rubocop=E9=81=A9=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/rooms_controller.rb | 5 +++-- app/models/message.rb | 2 ++ app/models/room.rb | 2 ++ app/models/room_member.rb | 2 ++ db/migrate/20230926121616_create_rooms.rb | 7 +++---- .../20230926121639_create_room_members.rb | 2 ++ db/migrate/20230926121703_create_messages.rb | 2 ++ db/seeds.rb | 4 ++-- spec/models/message_spec.rb | 2 ++ spec/models/room_member_spec.rb | 2 ++ spec/models/room_spec.rb | 2 ++ spec/requests/rooms_spec.rb | 17 +++++++++-------- 12 files changed, 33 insertions(+), 16 deletions(-) diff --git a/app/controllers/rooms_controller.rb b/app/controllers/rooms_controller.rb index 778c698..349d081 100644 --- a/app/controllers/rooms_controller.rb +++ b/app/controllers/rooms_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class RoomsController < ApplicationController before_action :set_user before_action :set_room_infos, only: %i[index show] @@ -9,8 +11,7 @@ def show @messages = room.messages.includes(:user).latest end - def create - end + def create; end private diff --git a/app/models/message.rb b/app/models/message.rb index 7cf7ec9..e84cdd7 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Message < ApplicationRecord belongs_to :user belongs_to :room diff --git a/app/models/room.rb b/app/models/room.rb index bc1e7fd..30475ab 100644 --- a/app/models/room.rb +++ b/app/models/room.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Room < ApplicationRecord has_many :room_members, dependent: :destroy has_many :messages, dependent: :destroy diff --git a/app/models/room_member.rb b/app/models/room_member.rb index 46a90ac..0a3fedb 100644 --- a/app/models/room_member.rb +++ b/app/models/room_member.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class RoomMember < ApplicationRecord belongs_to :user belongs_to :room diff --git a/db/migrate/20230926121616_create_rooms.rb b/db/migrate/20230926121616_create_rooms.rb index 93fbab7..cc917ca 100644 --- a/db/migrate/20230926121616_create_rooms.rb +++ b/db/migrate/20230926121616_create_rooms.rb @@ -1,8 +1,7 @@ +# frozen_string_literal: true + class CreateRooms < ActiveRecord::Migration[7.0] def change - create_table :rooms do |t| - - t.timestamps - end + create_table :rooms, &:timestamps end end diff --git a/db/migrate/20230926121639_create_room_members.rb b/db/migrate/20230926121639_create_room_members.rb index 55d75d7..8c09c4f 100644 --- a/db/migrate/20230926121639_create_room_members.rb +++ b/db/migrate/20230926121639_create_room_members.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class CreateRoomMembers < ActiveRecord::Migration[7.0] def change create_table :room_members do |t| diff --git a/db/migrate/20230926121703_create_messages.rb b/db/migrate/20230926121703_create_messages.rb index 9feb9b7..25011a5 100644 --- a/db/migrate/20230926121703_create_messages.rb +++ b/db/migrate/20230926121703_create_messages.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class CreateMessages < ActiveRecord::Migration[7.0] def change create_table :messages do |t| diff --git a/db/seeds.rb b/db/seeds.rb index 2c5344e..286b38e 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -34,13 +34,13 @@ room = existing_room else room = Room.create! - member_ids.each { |member_id| RoomMember.create!(user_id: member_id, room_id: room.id)} + member_ids.each { |member_id| RoomMember.create!(user_id: member_id, room_id: room.id) } end rand(0..15).times do |n| Message.create!( user_id: member_ids.sample, room_id: room.id, - content: (existing_room ? "既存部屋です。" : "") + "テストコメント#{n}です。\nテストコメント#{n}です。\nテストコメント#{n}です。" + content: (existing_room ? '既存部屋です。' : '') + "テストコメント#{n}です。\nテストコメント#{n}です。\nテストコメント#{n}です。" ) end end diff --git a/spec/models/message_spec.rb b/spec/models/message_spec.rb index d74b6a8..85ca47f 100644 --- a/spec/models/message_spec.rb +++ b/spec/models/message_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rails_helper' RSpec.describe Message, type: :model do diff --git a/spec/models/room_member_spec.rb b/spec/models/room_member_spec.rb index a5c5fb4..77e1c0d 100644 --- a/spec/models/room_member_spec.rb +++ b/spec/models/room_member_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rails_helper' RSpec.describe RoomMember, type: :model do diff --git a/spec/models/room_spec.rb b/spec/models/room_spec.rb index dc75c5e..1b9c3b0 100644 --- a/spec/models/room_spec.rb +++ b/spec/models/room_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rails_helper' RSpec.describe Room, type: :model do diff --git a/spec/requests/rooms_spec.rb b/spec/requests/rooms_spec.rb index 4c3a7f0..bfc0914 100644 --- a/spec/requests/rooms_spec.rb +++ b/spec/requests/rooms_spec.rb @@ -1,18 +1,19 @@ +# frozen_string_literal: true + require 'rails_helper' -RSpec.describe "Rooms", type: :request do - describe "GET /index" do - it "returns http success" do - get "/groups/index" +RSpec.describe 'Rooms', type: :request do + describe 'GET /index' do + it 'returns http success' do + get '/groups/index' expect(response).to have_http_status(:success) end end - describe "GET /show" do - it "returns http success" do - get "/groups/show" + describe 'GET /show' do + it 'returns http success' do + get '/groups/show' expect(response).to have_http_status(:success) end end - end From 7e052cf5441de3cd006e7310773b529b6f828604 Mon Sep 17 00:00:00 2001 From: KOH6 Date: Thu, 28 Sep 2023 10:37:20 +0900 Subject: [PATCH 08/14] =?UTF-8?q?=E6=8A=95=E7=A8=BF=E3=81=AE=E3=83=AA?= =?UTF-8?q?=E3=83=B3=E3=82=AF=E6=8A=BC=E4=B8=8B=E3=81=A7=E9=83=A8=E5=B1=8B?= =?UTF-8?q?=E7=94=BB=E9=9D=A2=E3=81=AB=E9=81=B7=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/rooms_controller.rb | 13 ++++++++++++- app/views/posts/_post_detail.html.slim | 5 +++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/controllers/rooms_controller.rb b/app/controllers/rooms_controller.rb index 349d081..95c9e4b 100644 --- a/app/controllers/rooms_controller.rb +++ b/app/controllers/rooms_controller.rb @@ -11,7 +11,18 @@ def show @messages = room.messages.includes(:user).latest end - def create; end + def create + member_ids = [current_user.id, params[:user_id]] + existing_room = Room.search_existing_room(user_id: current_user.id, other_id: params[:user_id]) + if existing_room + room = existing_room + else + room = Room.create + member_ids.each { |member_id| RoomMember.create(user_id: member_id, room_id: room.id) } + end + + redirect_to action: :show, id: room.id + end private diff --git a/app/views/posts/_post_detail.html.slim b/app/views/posts/_post_detail.html.slim index 8ff8bc2..37d62ad 100644 --- a/app/views/posts/_post_detail.html.slim +++ b/app/views/posts/_post_detail.html.slim @@ -25,6 +25,11 @@ = link_to user_follows_path(post.user), data: { "turbo-method": :post }, class: "text-dark text-decoration-none d-flex align-items-center" i.bi-person-add.mx-2 = "@#{post.user_user_name}さんをフォロー" + li + - if post.user != current_user + = link_to user_rooms_path(post.user), data: { "turbo-method": :post }, class: "text-dark text-decoration-none d-flex align-items-center text-center" + i.bi-envelope.mx-2 + = "@#{post.user_user_name}さんにメッセージを送る" .d-flex.flex-row - if post.content? span.card-text.my-2 From 9c744c3c1a11e2312ac172d06c4b0dd43a64cd80 Mon Sep 17 00:00:00 2001 From: KOH6 Date: Thu, 28 Sep 2023 10:51:40 +0900 Subject: [PATCH 09/14] =?UTF-8?q?=E3=83=A1=E3=83=83=E3=82=BB=E3=83=BC?= =?UTF-8?q?=E3=82=B8=E6=8A=95=E7=A8=BF=E6=A9=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/messages_controller.rb | 17 +++++++++++++++++ app/controllers/rooms_controller.rb | 2 +- app/models/room.rb | 2 ++ app/views/rooms/show.html.slim | 9 ++++++++- config/routes.rb | 1 + spec/requests/messages_spec.rb | 11 +++++++++++ 6 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 app/controllers/messages_controller.rb create mode 100644 spec/requests/messages_spec.rb diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb new file mode 100644 index 0000000..cdbe2ea --- /dev/null +++ b/app/controllers/messages_controller.rb @@ -0,0 +1,17 @@ +class MessagesController < ApplicationController + def create + @message = current_user.messages.build(message_params) + if @message.save + redirect_to request.referer + else + redirect_to request.referer, flash: { danger: 'メッセージ投稿に失敗しました。本文を確認してください。' } + end + end + + private + + def message_params + permit_params = params.require(:message).permit(:content) + permit_params.merge(room_id: params[:room_id]) + end +end diff --git a/app/controllers/rooms_controller.rb b/app/controllers/rooms_controller.rb index 95c9e4b..459178c 100644 --- a/app/controllers/rooms_controller.rb +++ b/app/controllers/rooms_controller.rb @@ -28,7 +28,7 @@ def create def set_room_infos @room_infos = [] - rooms = current_user.rooms.distinct + rooms = current_user.rooms.distinct.latest rooms.map do |room| room_info = {} room_info[:room_id] = room.id diff --git a/app/models/room.rb b/app/models/room.rb index 30475ab..d42e405 100644 --- a/app/models/room.rb +++ b/app/models/room.rb @@ -4,6 +4,8 @@ class Room < ApplicationRecord has_many :room_members, dependent: :destroy has_many :messages, dependent: :destroy + scope :latest, -> { order(created_at: :desc) } + def self.search_existing_room(user_id:, other_id:) existing_room = nil rooms = RoomMember.where(user_id:).map(&:room) diff --git a/app/views/rooms/show.html.slim b/app/views/rooms/show.html.slim index 96c7081..8c3e79b 100644 --- a/app/views/rooms/show.html.slim +++ b/app/views/rooms/show.html.slim @@ -3,7 +3,6 @@ .row.flex-nowrap = render 'shared/sidebar', :user => @user .col - = render 'shared/flash' h3.fw-bold.my-3 | メッセージ - @room_infos.each do |room_info| @@ -14,8 +13,16 @@ .col h3.fw-bold.my-3 | メッセージ詳細 + = render 'shared/flash' - @messages.each do |message| = "投稿者:#{message.user.name}" = "内容:#{message.content}" = "投稿日時:#{l message.created_at}" hr + = form_with url: messages_path(room_id: params[:id]), model: Message.new , data: { turbo: false }, locale: true do |f| + .card.mb-3 + .card-body + = f.text_area :content, class: 'form-control', placeholder: '新しいメッセージを作成', rows: 1 + button.btn.btn-primary.px-4.text-white.fw-bold[type="submit"] + | メッセージを送る + diff --git a/config/routes.rb b/config/routes.rb index 03df74a..2b70c0a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,6 +21,7 @@ resources :bookmarks, only: %i[index] resources :comments, only: %i[create] resources :rooms, only: %i[index show] + resources :messages, only: %i[create] root 'posts#index' end diff --git a/spec/requests/messages_spec.rb b/spec/requests/messages_spec.rb new file mode 100644 index 0000000..eb9a741 --- /dev/null +++ b/spec/requests/messages_spec.rb @@ -0,0 +1,11 @@ +require 'rails_helper' + +RSpec.describe "Messages", type: :request do + describe "GET /create" do + it "returns http success" do + get "/messages/create" + expect(response).to have_http_status(:success) + end + end + +end From 1d592ddb7aad39f48f57cd99f1f92539c852e418 Mon Sep 17 00:00:00 2001 From: KOH6 Date: Thu, 28 Sep 2023 10:58:53 +0900 Subject: [PATCH 10/14] =?UTF-8?q?=E9=83=A8=E5=B1=8B=E4=B8=80=E8=A6=A7?= =?UTF-8?q?=E3=82=92partial=E3=81=AB=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/rooms_controller.rb | 2 +- app/models/message.rb | 2 +- app/views/rooms/_room_infos.html.slim | 7 +++++++ app/views/rooms/index.html.slim | 9 ++------- app/views/rooms/show.html.slim | 9 ++------- 5 files changed, 13 insertions(+), 16 deletions(-) create mode 100644 app/views/rooms/_room_infos.html.slim diff --git a/app/controllers/rooms_controller.rb b/app/controllers/rooms_controller.rb index 459178c..70b4520 100644 --- a/app/controllers/rooms_controller.rb +++ b/app/controllers/rooms_controller.rb @@ -8,7 +8,7 @@ def index; end def show room = current_user.rooms.find(params[:id]) - @messages = room.messages.includes(:user).latest + @messages = room.messages.includes(:user).old end def create diff --git a/app/models/message.rb b/app/models/message.rb index e84cdd7..8979fca 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -6,5 +6,5 @@ class Message < ApplicationRecord validates :content, presence: true - scope :latest, -> { order(created_at: :desc) } + scope :old, -> { order(created_at: :asc) } end diff --git a/app/views/rooms/_room_infos.html.slim b/app/views/rooms/_room_infos.html.slim new file mode 100644 index 0000000..c500b16 --- /dev/null +++ b/app/views/rooms/_room_infos.html.slim @@ -0,0 +1,7 @@ +h3.fw-bold.my-3 + | メッセージ +- @room_infos.each do |room_info| + = link_to room_path(room_info[:room_id]), data: { turbo: false } + = "#{room_info[:user].name}の部屋" + = "#{room_info[:room_id]}のグループID" + hr \ No newline at end of file diff --git a/app/views/rooms/index.html.slim b/app/views/rooms/index.html.slim index b45290a..e9203ce 100644 --- a/app/views/rooms/index.html.slim +++ b/app/views/rooms/index.html.slim @@ -4,10 +4,5 @@ = render 'shared/sidebar', :user => @user .col = render 'shared/flash' - h3.fw-bold.my-3 - | メッセージ - - @room_infos.each do |room_info| - = link_to room_path(room_info[:room_id]), data: { turbo: false } - = "#{room_info[:user].name}の部屋" - = "#{room_info[:room_id]}のグループID" - hr + = render 'room_infos', :room_infos => @room_infos + .col diff --git a/app/views/rooms/show.html.slim b/app/views/rooms/show.html.slim index 8c3e79b..5ea15c1 100644 --- a/app/views/rooms/show.html.slim +++ b/app/views/rooms/show.html.slim @@ -3,13 +3,8 @@ .row.flex-nowrap = render 'shared/sidebar', :user => @user .col - h3.fw-bold.my-3 - | メッセージ - - @room_infos.each do |room_info| - = link_to room_path(room_info[:room_id]), data: { turbo: false } - = "#{room_info[:user].name}の部屋" - = "#{room_info[:room_id]}のグループID" - hr + = render 'shared/flash' + = render 'room_infos', :room_infos => @room_infos .col h3.fw-bold.my-3 | メッセージ詳細 From c03a99fd7ece20b7c908ecc585ef03e7e9887223 Mon Sep 17 00:00:00 2001 From: KOH6 Date: Thu, 28 Sep 2023 11:44:22 +0900 Subject: [PATCH 11/14] =?UTF-8?q?=E9=83=A8=E5=B1=8B=E4=B8=80=E8=A6=A7?= =?UTF-8?q?=E3=81=AB=E3=82=B9=E3=82=BF=E3=82=A4=E3=83=AB=E9=81=A9=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/rooms_controller.rb | 6 +++--- app/views/rooms/_room_info.html.slim | 15 +++++++++++++++ app/views/rooms/_room_infos.html.slim | 7 ------- app/views/rooms/index.html.slim | 7 +++++-- app/views/rooms/show.html.slim | 7 +++++-- 5 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 app/views/rooms/_room_info.html.slim delete mode 100644 app/views/rooms/_room_infos.html.slim diff --git a/app/controllers/rooms_controller.rb b/app/controllers/rooms_controller.rb index 70b4520..c61b1ff 100644 --- a/app/controllers/rooms_controller.rb +++ b/app/controllers/rooms_controller.rb @@ -28,11 +28,11 @@ def create def set_room_infos @room_infos = [] - rooms = current_user.rooms.distinct.latest + rooms = current_user.rooms.distinct.includes(:room_members).latest rooms.map do |room| room_info = {} - room_info[:room_id] = room.id - room_info[:user] = room.room_members.where.not(user_id: current_user.id).first.user + room_info[:room] = room + room_info[:user] = room.room_members.where.not(user_id: current_user.id).includes(:user).first.user @room_infos << room_info end end diff --git a/app/views/rooms/_room_info.html.slim b/app/views/rooms/_room_info.html.slim new file mode 100644 index 0000000..734d9c5 --- /dev/null +++ b/app/views/rooms/_room_info.html.slim @@ -0,0 +1,15 @@ +.card + .card-body.container-grid + = link_to room_path(room_info[:room].id), data: { turbo: false }, class: "row post text-dark" + .col-1.d-flex.align-items-center + - if room_info[:user].photo.attached? + = image_tag room_info[:user].photo, class: "rounded-circle img-fluid", style: " max-width: 250%; height: auto;" + .col + .d-flex + p.fw-bold + = room_info[:user].name + p.text-muted.mx-2 + | @ + = room_info[:user].user_name + .text-muted + = l room_info[:room].created_at \ No newline at end of file diff --git a/app/views/rooms/_room_infos.html.slim b/app/views/rooms/_room_infos.html.slim deleted file mode 100644 index c500b16..0000000 --- a/app/views/rooms/_room_infos.html.slim +++ /dev/null @@ -1,7 +0,0 @@ -h3.fw-bold.my-3 - | メッセージ -- @room_infos.each do |room_info| - = link_to room_path(room_info[:room_id]), data: { turbo: false } - = "#{room_info[:user].name}の部屋" - = "#{room_info[:room_id]}のグループID" - hr \ No newline at end of file diff --git a/app/views/rooms/index.html.slim b/app/views/rooms/index.html.slim index e9203ce..9687c46 100644 --- a/app/views/rooms/index.html.slim +++ b/app/views/rooms/index.html.slim @@ -2,7 +2,10 @@ = render 'shared/navigation' .row.flex-nowrap = render 'shared/sidebar', :user => @user - .col + .col.border-end = render 'shared/flash' - = render 'room_infos', :room_infos => @room_infos + h3.fw-bold.my-3 + | メッセージ + - @room_infos.each do |room_info| + = render 'room_info', room_info: .col diff --git a/app/views/rooms/show.html.slim b/app/views/rooms/show.html.slim index 5ea15c1..a637309 100644 --- a/app/views/rooms/show.html.slim +++ b/app/views/rooms/show.html.slim @@ -2,9 +2,12 @@ = render 'shared/navigation' .row.flex-nowrap = render 'shared/sidebar', :user => @user - .col + .col.border-end = render 'shared/flash' - = render 'room_infos', :room_infos => @room_infos + h3.fw-bold.my-3 + | メッセージ + - @room_infos.each do |room_info| + = render 'room_info', room_info: .col h3.fw-bold.my-3 | メッセージ詳細 From 43dd957ff5753455c2065155f50013aabcf6ce6f Mon Sep 17 00:00:00 2001 From: KOH6 Date: Thu, 28 Sep 2023 11:51:03 +0900 Subject: [PATCH 12/14] =?UTF-8?q?=E9=81=B8=E6=8A=9E=E3=81=95=E3=82=8C?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=82=8B=E9=83=A8=E5=B1=8B=E3=81=AB=E8=83=8C?= =?UTF-8?q?=E6=99=AF=E8=89=B2=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/rooms/_room_info.html.slim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/rooms/_room_info.html.slim b/app/views/rooms/_room_info.html.slim index 734d9c5..59fc8b0 100644 --- a/app/views/rooms/_room_info.html.slim +++ b/app/views/rooms/_room_info.html.slim @@ -1,5 +1,5 @@ .card - .card-body.container-grid + div class="card-body container-grid #{'bg-light' if params[:id].to_i == room_info[:room].id}" = link_to room_path(room_info[:room].id), data: { turbo: false }, class: "row post text-dark" .col-1.d-flex.align-items-center - if room_info[:user].photo.attached? From 5679f7d66334d7569552db42f7d6c13b23d9cac2 Mon Sep 17 00:00:00 2001 From: KOH6 Date: Thu, 28 Sep 2023 12:29:35 +0900 Subject: [PATCH 13/14] =?UTF-8?q?=E9=80=81=E4=BF=A1=E8=80=85=E3=81=AB?= =?UTF-8?q?=E3=82=88=E3=82=8B=E3=83=A1=E3=83=83=E3=82=BB=E3=83=BC=E3=82=B8?= =?UTF-8?q?=E3=81=AE=E8=A1=A8=E7=A4=BA=E3=82=8F=E3=81=91=E3=80=81=E9=80=81?= =?UTF-8?q?=E4=BF=A1=E3=83=95=E3=82=A9=E3=83=BC=E3=83=A0=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/rooms_controller.rb | 2 +- app/views/messages/_message.html.slim | 20 ++++++++++++++++++++ app/views/rooms/_room_info.html.slim | 4 ++-- app/views/rooms/index.html.slim | 2 +- app/views/rooms/show.html.slim | 12 +++++------- 5 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 app/views/messages/_message.html.slim diff --git a/app/controllers/rooms_controller.rb b/app/controllers/rooms_controller.rb index c61b1ff..577ec8c 100644 --- a/app/controllers/rooms_controller.rb +++ b/app/controllers/rooms_controller.rb @@ -8,7 +8,7 @@ def index; end def show room = current_user.rooms.find(params[:id]) - @messages = room.messages.includes(:user).old + @messages = room.messages.includes(:user).old.page(params[:page]).per(7) end def create diff --git a/app/views/messages/_message.html.slim b/app/views/messages/_message.html.slim new file mode 100644 index 0000000..701bad4 --- /dev/null +++ b/app/views/messages/_message.html.slim @@ -0,0 +1,20 @@ +- if message.user != current_user + .container-grid.my-2 + .row + .col-7 + .card.border.rounded.bg-light + .card-body + = safe_join(message.content.split("\n"),tag(:br)) + .text-muted + = l message.created_at + .col +- else + .container-grid.my-2 + .row + .col + .col-7.align-self-end + .card.border.rounded.bg-primary + .card-body.text-white + = safe_join(message.content.split("\n"),tag(:br)) + .text-muted.text-end + = l message.created_at \ No newline at end of file diff --git a/app/views/rooms/_room_info.html.slim b/app/views/rooms/_room_info.html.slim index 59fc8b0..84dd58c 100644 --- a/app/views/rooms/_room_info.html.slim +++ b/app/views/rooms/_room_info.html.slim @@ -1,9 +1,9 @@ .card div class="card-body container-grid #{'bg-light' if params[:id].to_i == room_info[:room].id}" = link_to room_path(room_info[:room].id), data: { turbo: false }, class: "row post text-dark" - .col-1.d-flex.align-items-center + .col-3.d-flex.align-items-center - if room_info[:user].photo.attached? - = image_tag room_info[:user].photo, class: "rounded-circle img-fluid", style: " max-width: 250%; height: auto;" + = image_tag room_info[:user].photo, class: "rounded-circle img-fluid", style: " max-width: 100%; height: auto;" .col .d-flex p.fw-bold diff --git a/app/views/rooms/index.html.slim b/app/views/rooms/index.html.slim index 9687c46..d1fcd92 100644 --- a/app/views/rooms/index.html.slim +++ b/app/views/rooms/index.html.slim @@ -2,7 +2,7 @@ = render 'shared/navigation' .row.flex-nowrap = render 'shared/sidebar', :user => @user - .col.border-end + .col-3.border-end = render 'shared/flash' h3.fw-bold.my-3 | メッセージ diff --git a/app/views/rooms/show.html.slim b/app/views/rooms/show.html.slim index a637309..bd24d50 100644 --- a/app/views/rooms/show.html.slim +++ b/app/views/rooms/show.html.slim @@ -2,7 +2,7 @@ = render 'shared/navigation' .row.flex-nowrap = render 'shared/sidebar', :user => @user - .col.border-end + .col-3.border-end = render 'shared/flash' h3.fw-bold.my-3 | メッセージ @@ -13,14 +13,12 @@ | メッセージ詳細 = render 'shared/flash' - @messages.each do |message| - = "投稿者:#{message.user.name}" - = "内容:#{message.content}" - = "投稿日時:#{l message.created_at}" - hr + = render message + = paginate @messages = form_with url: messages_path(room_id: params[:id]), model: Message.new , data: { turbo: false }, locale: true do |f| .card.mb-3 .card-body = f.text_area :content, class: 'form-control', placeholder: '新しいメッセージを作成', rows: 1 - button.btn.btn-primary.px-4.text-white.fw-bold[type="submit"] - | メッセージを送る + button.btn.btn-primary.text-white.rounded-circle.fw-bold.float-end.my-2[type="submit"] + i.bi-send From 258b75942d822385a5a3db0e48b796c4504961a4 Mon Sep 17 00:00:00 2001 From: KOH6 Date: Thu, 28 Sep 2023 12:29:55 +0900 Subject: [PATCH 14/14] =?UTF-8?q?rubocop=E9=81=A9=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/messages_controller.rb | 2 ++ spec/requests/messages_spec.rb | 11 ++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index cdbe2ea..a058f83 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class MessagesController < ApplicationController def create @message = current_user.messages.build(message_params) diff --git a/spec/requests/messages_spec.rb b/spec/requests/messages_spec.rb index eb9a741..3f82a2f 100644 --- a/spec/requests/messages_spec.rb +++ b/spec/requests/messages_spec.rb @@ -1,11 +1,12 @@ +# frozen_string_literal: true + require 'rails_helper' -RSpec.describe "Messages", type: :request do - describe "GET /create" do - it "returns http success" do - get "/messages/create" +RSpec.describe 'Messages', type: :request do + describe 'GET /create' do + it 'returns http success' do + get '/messages/create' expect(response).to have_http_status(:success) end end - end