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

Notification CIConfirm #25

Closed
wants to merge 11 commits into from
9 changes: 9 additions & 0 deletions app/controllers/notifications_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class NotificationsController < ApplicationController
before_action :set_user

def index
@notifications = @user.notifications.includes(:user).latest.page(params[:page]).per(10)
end
end
2 changes: 1 addition & 1 deletion app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class ApplicationMailer < ActionMailer::Base
default from: 'Twitterクローン送信者'
default from: ENV['EMAIL']
layout 'mailer'
end
8 changes: 8 additions & 0 deletions app/mailers/notification_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class NotificationMailer < ApplicationMailer
def complete(notification:)
@notification = notification
mail(to: @notification.user_email, subject: 'Twitterクローン通知')
end
end
5 changes: 5 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# frozen_string_literal: true

class Comment < ApplicationRecord
include NotificationCreator

after_create :create_notification

belongs_to :user
belongs_to :post
has_many_attached :images
has_one :notification, as: :action, dependent: :destroy

validates :content, presence: true

Expand Down
8 changes: 8 additions & 0 deletions app/models/concerns/notification_creator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module NotificationCreator
def create_notification
notification = Notification.create!(user: post.user, action: self, action_type: self.class.name)
NotificationMailer.complete(notification:).deliver_later
end
end
5 changes: 5 additions & 0 deletions app/models/like.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# frozen_string_literal: true

class Like < ApplicationRecord
include NotificationCreator

after_create :create_notification

belongs_to :user
belongs_to :post
has_one :notification, as: :action, dependent: :destroy

validates :user_id, presence: true, uniqueness: { scope: :post_id }
end
14 changes: 14 additions & 0 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class Notification < ApplicationRecord
belongs_to :user
belongs_to :action, polymorphic: true

scope :latest, -> { order(created_at: :desc) }

delegate :email, to: :user, prefix: true

def sender
action.user
end
end
5 changes: 5 additions & 0 deletions app/models/repost.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# frozen_string_literal: true

class Repost < ApplicationRecord
include NotificationCreator

after_create :create_notification

belongs_to :user
belongs_to :post
has_one :notification, as: :action, dependent: :destroy

validates :user_id, presence: true, uniqueness: { scope: :post_id }
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class User < ApplicationRecord
has_many :rooms, through: :room_members

has_many :messages, dependent: :destroy
has_many :notifications, dependent: :destroy

with_options presence: true do
validates :name
Expand Down
7 changes: 7 additions & 0 deletions app/views/notification_mailer/complete.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
h1 Twitterクローン通知
- if @notification.action_type == 'Like'
= "#{@notification.sender.name}さんがあなたの投稿をいいねしました"
- elsif @notification.action_type == 'Comment'
= "#{@notification.sender.name}さんがあなたの投稿にコメントしました"
- elsif @notification.action_type == 'Repost'
= "#{@notification.sender.name}さんがあなたの投稿をリツイートしました"
1 change: 1 addition & 0 deletions app/views/notification_mailer/complete.text.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
' Notification#complete
42 changes: 42 additions & 0 deletions app/views/notifications/_notification.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
- if notification.action_type == 'Like'
.card
.card-body.container-grid
= link_to post_path(notification.action.post), data: { turbo: false}, class: "row post text-dark"
.row
.col-1.text-center.d-flex.align-items-center
i.bi-heart-fill.already-liking[style="font-size: 2rem"]
.col
- if notification.sender.photo.attached?
= image_tag notification.sender.photo, class: "rounded-circle img-fluid", style: " max-width: 7%; height: auto;"
.row
.col
= link_to "#{notification.sender.name}さん", user_path(notification.sender), data: { turbo: false}, class: "text-dark text-decoration-none fw-bold"
= "があなたの投稿をいいねしました"
- elsif notification.action_type == 'Comment'
.card
.card-body.container-grid
= link_to post_path(notification.action.post), data: { turbo: false}, class: "row post text-dark"
.row
.col-1.text-center.d-flex.align-items-center
i.bi-chat.text-muted[style="font-size: 2rem"]
.col
- if notification.sender.photo.attached?
= image_tag notification.sender.photo, class: "rounded-circle img-fluid", style: " max-width: 7%; height: auto;"
.row
.col
= link_to "#{notification.sender.name}さん", user_path(notification.sender), data: { turbo: false}, class: "text-dark text-decoration-none fw-bold"
= "があなたの投稿にコメントしました"
- elsif notification.action_type == 'Repost'
.card
.card-body.container-grid
= link_to post_path(notification.action.post), data: { turbo: false}, class: "row post text-dark"
.row
.col-1.text-center.d-flex.align-items-center
i.bi-repeat.already-reposting[style="font-size: 2rem"]
.col
- if notification.sender.photo.attached?
= image_tag notification.sender.photo, class: "rounded-circle img-fluid", style: " max-width: 7%; height: auto;"
.row
.col
= link_to "#{notification.sender.name}さん", user_path(notification.sender), data: { turbo: false}, class: "text-dark text-decoration-none fw-bold"
= "があなたの投稿をリツイートしました"
11 changes: 11 additions & 0 deletions app/views/notifications/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
= render 'shared/flash'
h3.fw-bold.my-3
| 通知
= paginate @notifications
- @notifications.each do |notification|
= render notification
2 changes: 1 addition & 1 deletion app/views/shared/_sidebar.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,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 notifications_path, data: { turbo: false }, class: "nav-link align-middle text-dark px-0 d-flex align-items-center"
i.fs-4.bi-bell.mx-2
span.ms-1.d-none.d-sm-inline[style="font-size:1.5rem"]
| 通知
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
resources :comments, only: %i[create]
resources :rooms, only: %i[index show]
resources :messages, only: %i[create]
resources :notifications, only: %i[index]

root 'posts#index'
end
12 changes: 12 additions & 0 deletions db/migrate/20230928081221_create_notifications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class CreateNotifications < ActiveRecord::Migration[7.0]
def change
create_table :notifications do |t|
t.references :user, null: false, foreign_key: true
t.references :action, polymorphic: true, null: false

t.timestamps
end
end
end
13 changes: 12 additions & 1 deletion db/schema.rb

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

4 changes: 2 additions & 2 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
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|
rand(1..5).times do |n|
Message.create!(
user_id: member_ids.sample,
room_id: room.id,
Expand Down Expand Up @@ -65,7 +65,7 @@
user_id: user_ids.reject { |id| id == post.user_id }.sample,
post_id: post.id
)
rand(0..10).times do |m|
rand(0..5).times do |m|
Comment.create!(
user_id: user_ids.reject { |id| id == post.user_id }.sample,
post_id: post.id,
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/notification_mailer/complete
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
NotificationMailer#complete

Hi, find me in app/views/notification_mailer/complete
9 changes: 9 additions & 0 deletions spec/mailers/previews/notification_mailer_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

# Preview all emails at http://localhost:3000/rails/mailers/notification_mailer
class NotificationMailerPreview < ActionMailer::Preview
# Preview this email at http://localhost:3000/rails/mailers/notification_mailer/complete
def complete
NotificationMailer.complete
end
end
7 changes: 7 additions & 0 deletions spec/models/notification_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 Notification, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
12 changes: 12 additions & 0 deletions spec/requests/notifications_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 'Notifications', type: :request do
describe 'GET /index' do
it 'returns http success' do
get '/notifications/index'
expect(response).to have_http_status(:success)
end
end
end
Loading