Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

メッセージ機能 #24

Merged
merged 14 commits into from
Sep 29, 2023
19 changes: 19 additions & 0 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

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
39 changes: 39 additions & 0 deletions app/controllers/rooms_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

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).old.page(params[:page]).per(7)
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

def set_room_infos
@room_infos = []
rooms = current_user.rooms.distinct.includes(:room_members).latest
rooms.map do |room|
room_info = {}
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
end
10 changes: 10 additions & 0 deletions app/models/message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class Message < ApplicationRecord
belongs_to :user
belongs_to :room

validates :content, presence: true

scope :old, -> { order(created_at: :asc) }
end
20 changes: 20 additions & 0 deletions app/models/room.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

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)
rooms.each do |room|
if room.room_members.find_by(user_id: other_id)
existing_room = room
break
end
end
existing_room
end
end
6 changes: 6 additions & 0 deletions app/models/room_member.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class RoomMember < ApplicationRecord
belongs_to :user
belongs_to :room
end
7 changes: 6 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ class User < ApplicationRecord
has_many :bookmarks, dependent: :destroy
has_many :bookmarking_posts, through: :bookmarks, source: :post

has_many :room_members, dependent: :destroy
has_many :rooms, through: :room_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
Expand Down
20 changes: 20 additions & 0 deletions app/views/messages/_message.html.slim
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions app/views/posts/_post_detail.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions app/views/rooms/_room_info.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.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-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: 100%; 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
11 changes: 11 additions & 0 deletions app/views/rooms/index.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.container-fluid
= render 'shared/navigation'
.row.flex-nowrap
= render 'shared/sidebar', :user => @user
.col-3.border-end
= render 'shared/flash'
h3.fw-bold.my-3
| メッセージ
- @room_infos.each do |room_info|
= render 'room_info', room_info:
.col
24 changes: 24 additions & 0 deletions app/views/rooms/show.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.container-fluid
= render 'shared/navigation'
.row.flex-nowrap
= render 'shared/sidebar', :user => @user
.col-3.border-end
= render 'shared/flash'
h3.fw-bold.my-3
| メッセージ
- @room_infos.each do |room_info|
= render 'room_info', room_info:
.col
h3.fw-bold.my-3
| メッセージ詳細
= render 'shared/flash'
- @messages.each do |message|
= 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.text-white.rounded-circle.fw-bold.float-end.my-2[type="submit"]
i.bi-send

2 changes: 1 addition & 1 deletion app/views/shared/_sidebar.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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 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"]
| メッセージ
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@

resources :users, only: %i[show edit update] do
resources :follows, only: %i[create destroy]
resources :rooms, only: %i[create]
end

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
7 changes: 7 additions & 0 deletions db/migrate/20230926121616_create_rooms.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class CreateRooms < ActiveRecord::Migration[7.0]
def change
create_table :rooms, &:timestamps
end
end
12 changes: 12 additions & 0 deletions db/migrate/20230926121639_create_room_members.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

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
13 changes: 13 additions & 0 deletions db/migrate/20230926121703_create_messages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class CreateMessages < ActiveRecord::Migration[7.0]
def change
create_table :messages do |t|
t.references :user, null: false, foreign_key: true
t.references :room, null: false, foreign_key: true
t.text :content, null: false

t.timestamps
end
end
end
30 changes: 29 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 23 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@
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|
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: (existing_room ? '既存部屋です。' : '') + "テストコメント#{n}です。\nテストコメント#{n}です。\nテストコメント#{n}です。"
)
end
end
end

POST_COUNT.times do |n|
post = Post.create!(
user_id: user_ids.sample,
Expand All @@ -43,7 +65,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,
Expand Down
7 changes: 7 additions & 0 deletions spec/models/message_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Message, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
7 changes: 7 additions & 0 deletions spec/models/room_member_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe RoomMember, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
7 changes: 7 additions & 0 deletions spec/models/room_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Room, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
12 changes: 12 additions & 0 deletions spec/requests/messages_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +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'
expect(response).to have_http_status(:success)
end
end
end
Loading
Loading