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 #26 from KOH6/notification
通知機能
- Loading branch information
Showing
22 changed files
with
176 additions
and
5 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,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 |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationMailer < ActionMailer::Base | ||
default from: 'Twitterクローン送信者' | ||
default from: ENV['EMAIL'] | ||
layout 'mailer' | ||
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,8 @@ | ||
# frozen_string_literal: true | ||
|
||
class NotificationMailer < ApplicationMailer | ||
def complete(notification:) | ||
@notification = notification | ||
mail(to: @notification.user_email, subject: 'Twitterクローン通知') | ||
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
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,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 |
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 |
---|---|---|
@@ -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 |
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,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 |
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 |
---|---|---|
@@ -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 |
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 @@ | ||
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}さんがあなたの投稿をリツイートしました" |
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 @@ | ||
' Notification#complete |
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,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" | ||
= "があなたの投稿をリツイートしました" |
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 | ||
= render 'shared/flash' | ||
h3.fw-bold.my-3 | ||
| 通知 | ||
= paginate @notifications | ||
- @notifications.each do |notification| | ||
= render notification |
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,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 |
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,3 @@ | ||
NotificationMailer#complete | ||
|
||
Hi, find me in app/views/notification_mailer/complete |
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,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 |
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 Notification, 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 '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 |