generated from happiness-chain/rails_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from KOH6/message
メッセージ機能
- Loading branch information
Showing
23 changed files
with
315 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.