diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb
index 9ba316f..1d30a17 100644
--- a/app/controllers/notifications_controller.rb
+++ b/app/controllers/notifications_controller.rb
@@ -2,6 +2,6 @@ class NotificationsController < ApplicationController
before_action :authenticate_user!
def index
- @notifications = current_user.profile.notifications
+ @notifications = current_user.profile.notifications.order(created_at: :desc)
end
end
diff --git a/app/models/comment.rb b/app/models/comment.rb
index d8f18d1..7a42d29 100644
--- a/app/models/comment.rb
+++ b/app/models/comment.rb
@@ -4,4 +4,16 @@ class Comment < ApplicationRecord
belongs_to :user
has_many :likes, as: :likeable, dependent: :destroy
has_many :reports, as: :reportable, dependent: :destroy
+ has_one :notification, as: :notifiable, dependent: :destroy
+
+ after_create :create_notification
+
+ private
+
+ def create_notification
+ comment_author = user.profile
+ return if comment_author == post.user.profile
+
+ Notification.create(profile: post.user.profile, notifiable: self)
+ end
end
diff --git a/app/models/like.rb b/app/models/like.rb
index 8444064..3bae494 100644
--- a/app/models/like.rb
+++ b/app/models/like.rb
@@ -10,6 +10,8 @@ class Like < ApplicationRecord
def create_notification
post_author = likeable.user.profile
+ return if user == post_author.user
+
Notification.create(profile: post_author, notifiable: self)
end
end
diff --git a/app/views/notifications/_comment.html.erb b/app/views/notifications/_comment.html.erb
new file mode 100644
index 0000000..683889d
--- /dev/null
+++ b/app/views/notifications/_comment.html.erb
@@ -0,0 +1 @@
+
<%= link_to notification.notifiable.user.full_name, profile_path(notification.notifiable.user.profile) %> comentou em sua publicação
\ No newline at end of file
diff --git a/app/views/notifications/_connection.html.erb b/app/views/notifications/_connection.html.erb
new file mode 100644
index 0000000..4e2666a
--- /dev/null
+++ b/app/views/notifications/_connection.html.erb
@@ -0,0 +1 @@
+
<%= link_to notification.notifiable.follower.user.full_name, profile_path(notification.notifiable.follower) %> começou a te seguir
\ No newline at end of file
diff --git a/app/views/notifications/_invitation.html.erb b/app/views/notifications/_invitation.html.erb
new file mode 100644
index 0000000..b618f6b
--- /dev/null
+++ b/app/views/notifications/_invitation.html.erb
@@ -0,0 +1 @@
+
Você recebeu um convite para <%= link_to notification.notifiable.project_title, invitation_path(notification.notifiable) %>
\ No newline at end of file
diff --git a/app/views/notifications/_like.html.erb b/app/views/notifications/_like.html.erb
new file mode 100644
index 0000000..4a428bc
--- /dev/null
+++ b/app/views/notifications/_like.html.erb
@@ -0,0 +1,5 @@
+<% if notification.notifiable.likeable.is_a?Post %>
+
<%= link_to notification.notifiable.user.full_name, profile_path(notification.notifiable.user.profile) %> curtiu sua publicação
+<% else %>
+
<%= link_to notification.notifiable.user.full_name, profile_path(notification.notifiable.user.profile) %> curtiu seu comentário
+<% end %>
\ No newline at end of file
diff --git a/app/views/notifications/_post.html.erb b/app/views/notifications/_post.html.erb
new file mode 100644
index 0000000..9e2e784
--- /dev/null
+++ b/app/views/notifications/_post.html.erb
@@ -0,0 +1 @@
+
<%= notification.notifiable.user.full_name %> fez uma <%= link_to 'publicação', post_path(notification.notifiable) %>
\ No newline at end of file
diff --git a/app/views/notifications/index.html.erb b/app/views/notifications/index.html.erb
index ad40532..7d8e41c 100644
--- a/app/views/notifications/index.html.erb
+++ b/app/views/notifications/index.html.erb
@@ -1,20 +1,15 @@
Notificações
<% if @notifications&.any? %>
-
<% @notifications.each do |notification| %>
- <% if notification.notifiable.is_a?Post %>
-
<%= notification.notifiable.user.full_name %> fez uma <%= link_to 'publicação', post_path(notification.notifiable) %>
- <% elsif notification.notifiable.is_a?Invitation %>
-
Você recebeu um convite para <%= link_to notification.notifiable.project_title, invitation_path(notification.notifiable) %>
- <% elsif notification.notifiable.is_a?Connection %>
-
<%= link_to notification.notifiable.follower.user.full_name, profile_path(notification.notifiable.follower) %> começou a te seguir
- <% end %>
+ <%= render(
+ partial: "#{notification.notifiable.class.name.downcase}" ,
+ locals: { notification: notification }
+ )%>
há <%= distance_of_time_in_words_to_now(notification.notifiable.created_at) %>
<% end %>
-
<% else %>
Nenhuma notificação encontrada
<% end %>
diff --git a/app/views/shared/_navbar.html.erb b/app/views/shared/_navbar.html.erb
index 592fda6..f43cb13 100644
--- a/app/views/shared/_navbar.html.erb
+++ b/app/views/shared/_navbar.html.erb
@@ -40,7 +40,9 @@
<%= link_to 'Convites', invitations_path, class: 'nav-link' %>
- <%= link_to 'Notificações', notifications_path, class: 'nav-link' %>
+ <%= link_to notifications_path, class: 'nav-link' do %>
+ Notificações <%= current_user.profile.notifications.count %>
+ <% end %>
<% if current_user.admin? %>
diff --git a/db/schema.rb b/db/schema.rb
index 491ae51..1a7c28c 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -159,7 +159,7 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "pin", default: 0
- t.datetime "edited_at", default: "2024-02-12 13:53:59"
+ t.datetime "edited_at", default: "2024-02-10 11:45:48"
t.integer "status", default: 0
t.datetime "published_at"
t.string "old_status"
diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb
index ecc304e..d85a72b 100644
--- a/spec/models/comment_spec.rb
+++ b/spec/models/comment_spec.rb
@@ -8,4 +8,24 @@
expect(comment.errors[:message]).to include('não pode ficar em branco')
end
end
+
+ describe '#create_notification' do
+ it 'cria notificação para o dono da publicação' do
+ user = create(:user)
+ other_user = create(:user)
+ post = create(:post, user:)
+ comment = create(:comment, user: other_user, post:)
+
+ expect(Notification.count).to eq 1
+ expect(Notification.last.notifiable).to eq comment
+ end
+
+ it 'não cria notificação para o dono do comentário' do
+ user = create(:user)
+ post = create(:post, user:)
+ create(:comment, user:, post:)
+
+ expect(Notification.count).to eq 0
+ end
+ end
end
diff --git a/spec/models/like_spec.rb b/spec/models/like_spec.rb
index eb1b11d..6e57143 100644
--- a/spec/models/like_spec.rb
+++ b/spec/models/like_spec.rb
@@ -38,4 +38,24 @@
expect(like).to be_valid
end
end
+
+ describe '#create_notification' do
+ it 'cria notificação para o dono da publicação' do
+ user = create(:user)
+ other_user = create(:user)
+ post = create(:post, user:)
+ like = create(:like, likeable: post, user: other_user)
+
+ expect(Notification.count).to eq 1
+ expect(Notification.last.notifiable).to eq like
+ end
+
+ it 'não cria notificação para o dono do like' do
+ user = create(:user)
+ post = create(:post, user:)
+ create(:like, likeable: post, user:)
+
+ expect(Notification.count).to eq 0
+ end
+ end
end
diff --git a/spec/system/notifications/user_sees_comments_notification_spec.rb b/spec/system/notifications/user_sees_comments_notification_spec.rb
new file mode 100644
index 0000000..1180386
--- /dev/null
+++ b/spec/system/notifications/user_sees_comments_notification_spec.rb
@@ -0,0 +1,66 @@
+require 'rails_helper'
+
+describe 'Usuário vê notificação de comentário' do
+ it 'ao comentarem na sua publicação' do
+ user = create(:user)
+ post = create(:post, user:)
+ other_user = create(:user)
+ comment = create(:comment, post:, user: other_user)
+
+ login_as user
+ visit notifications_path
+
+ expect(Notification.count).to eq 1
+ expect(page).to have_current_path notifications_path
+ expect(page).to have_content 'comentou em sua publicação'
+ expect(page).to have_link comment.user.profile.full_name, href: profile_path(comment.user.profile)
+ end
+
+ it 'e não vê notificação de seu comentário' do
+ user = create(:user)
+ post = create(:post, user:)
+ comment = create(:comment, post:, user:)
+
+ login_as user
+ visit notifications_path
+
+ expect(Notification.count).to eq 0
+ expect(page).not_to have_content 'comentou em sua publicação'
+ expect(page).not_to have_link comment.user.profile.full_name, href: profile_path(comment.user.profile)
+ end
+
+ it 'ao curtirem seu comentário' do
+ user = create(:user)
+ post = create(:post)
+ comment = create(:comment, post:, user:)
+ like = create(:like, likeable: comment)
+
+ login_as user
+ visit root_path
+ click_button class: 'dropdown-toggle'
+ within 'nav' do
+ click_on 'Notificações'
+ end
+
+ expect(page).to have_current_path notifications_path
+ expect(page).to have_content 'curtiu seu comentário'
+ expect(page).to have_link like.user.profile.full_name, href: profile_path(like.user.profile)
+ end
+
+ it 'e não recebe ao curtir seu próprio comentário' do
+ user = create(:user)
+ post = create(:post)
+ comment = create(:comment, post:, user:)
+ like = create(:like, likeable: comment, user:)
+
+ login_as user
+ visit root_path
+ click_button class: 'dropdown-toggle'
+ within 'nav' do
+ click_on 'Notificações'
+ end
+
+ expect(page).not_to have_content 'curtiu seu comentário'
+ expect(page).not_to have_link like.user.profile.full_name, href: profile_path(like.user.profile)
+ end
+end
diff --git a/spec/system/notifications/user_sees_invitation_notification_spec.rb b/spec/system/notifications/user_sees_invitation_notification_spec.rb
index cd37393..6bca12c 100644
--- a/spec/system/notifications/user_sees_invitation_notification_spec.rb
+++ b/spec/system/notifications/user_sees_invitation_notification_spec.rb
@@ -6,33 +6,13 @@
invitation = create(:invitation, profile: user.profile, created_at: 1.day.ago)
login_as user
- visit root_path
- click_button class: 'dropdown-toggle'
- within 'nav' do
- click_on 'Notificações'
- end
+ visit notifications_path
expect(page).to have_current_path notifications_path
expect(page).to have_content "Você recebeu um convite para #{invitation.project_title} há 1 dia"
expect(page).to have_link invitation.project_title, href: invitation_path(invitation)
end
- it 'e deve estar logado' do
- visit notifications_path
-
- expect(page).to have_current_path new_user_session_path
- end
-
- it 'e não tem convites' do
- user = create(:user)
-
- login_as user
-
- visit notifications_path
-
- expect(page).to have_content 'Nenhuma notificação'
- end
-
it 'e não vê convites de outros usuários' do
user = create(:user)
invitation = create(:invitation, profile: user.profile)
diff --git a/spec/system/notifications/user_sees_new_follower_notification_spec.rb b/spec/system/notifications/user_sees_new_follower_notification_spec.rb
index be96372..9970da4 100644
--- a/spec/system/notifications/user_sees_new_follower_notification_spec.rb
+++ b/spec/system/notifications/user_sees_new_follower_notification_spec.rb
@@ -7,14 +7,10 @@
Connection.create!(followed_profile: followed.profile, follower: follower.profile)
login_as followed
- visit root_path
- click_button class: 'dropdown-toggle'
- within 'nav' do
- click_on 'Notificações'
- end
+ visit notifications_path
expect(page).to have_current_path notifications_path
- expect(page).to have_content 'começou a te seguir'
+ expect(page).to have_content 'Paulo começou a te seguir'
expect(page).to have_link follower.full_name, href: profile_path(follower)
end
end
diff --git a/spec/system/notifications/user_sees_notifications_spec.rb b/spec/system/notifications/user_sees_notifications_spec.rb
new file mode 100644
index 0000000..5e739a1
--- /dev/null
+++ b/spec/system/notifications/user_sees_notifications_spec.rb
@@ -0,0 +1,58 @@
+require 'rails_helper'
+
+describe 'Usuário vê notificações' do
+ it 'com sucesso' do
+ user = create(:user)
+ other_user = create(:user)
+ Connection.create!(follower: user.profile, followed_profile: other_user.profile)
+ Connection.create!(follower: other_user.profile, followed_profile: user.profile)
+ post = create(:post, user:)
+
+ new_post_notification_job_spy = spy(NewPostNotificationJob)
+ stub_const('NewPostNotificationJob', new_post_notification_job_spy)
+
+ create(:post, user: other_user)
+ create(:like, likeable: post, user: other_user)
+ create(:comment, post:, user: other_user)
+ comment_user = create(:comment, post:, user:)
+ create(:like, likeable: comment_user, user: other_user)
+ create(:invitation, profile: user.profile)
+
+ login_as user
+ visit root_path
+ click_button class: 'dropdown-toggle'
+ within 'nav' do
+ click_on 'Notificações'
+ end
+
+ expect(page).to have_current_path notifications_path
+ expect(new_post_notification_job_spy).to have_received(:perform_later)
+ expect(user.profile.notifications.count).to eq 5
+ expect(page).to have_content 'começou a te seguir'
+ expect(page).to have_content 'curtiu sua publicação'
+ expect(page).to have_content 'comentou em sua publicação'
+ expect(page).to have_content 'curtiu seu comentário'
+ expect(page).to have_content 'Você recebeu um convite'
+ expect(page.body.index('começou a te seguir')).to be > page.body.index('curtiu sua publicação')
+ expect(page.body.index('curtiu sua publicação')).to be > page.body.index('comentou em sua publicação')
+ expect(page.body.index('comentou em sua publicação')).to be > page.body.index('curtiu seu comentário')
+ expect(page.body.index('curtiu seu comentário')).to be > page.body.index('Você recebeu um convite')
+ end
+
+ it 'deve estar logado' do
+ visit notifications_path
+
+ expect(page).to have_current_path new_user_session_path
+ end
+
+ it 'e não tem notificações' do
+ user = create(:user)
+
+ login_as user
+
+ visit notifications_path
+
+ expect(page).to have_content 'Nenhuma notificação'
+ expect(Notification.count).to eq 0
+ end
+end
diff --git a/spec/system/notifications/user_sees_post_notification_spec.rb b/spec/system/notifications/user_sees_post_notification_spec.rb
index 89576ab..28de556 100644
--- a/spec/system/notifications/user_sees_post_notification_spec.rb
+++ b/spec/system/notifications/user_sees_post_notification_spec.rb
@@ -1,18 +1,58 @@
require 'rails_helper'
-describe 'Nova publicação envia notificação para seguidores' do
- it 'com sucesso' do
+describe 'Usuário vê notificações de publicações' do
+ it 'e visualiza a notificação' do
follower = create(:user, full_name: 'Paulo')
followed = create(:user, full_name: 'Ana')
Connection.create!(followed_profile: followed.profile, follower: follower.profile)
- new_post_notification_job_spy = spy(NewPostNotificationJob)
- stub_const('NewPostNotificationJob', new_post_notification_job_spy)
- create(:post, user: followed)
+ post = create(:post, user: followed)
+
+ NewPostNotificationJob.perform_now(post)
login_as follower
visit notifications_path
- expect(page).to have_current_path notifications_path
- expect(new_post_notification_job_spy).to have_received(:perform_later)
+ expect(page).to have_content 'Ana fez uma publicação'
+ expect(page).to have_link 'publicação', href: post_path(post)
+ end
+
+ context 'nova publicação notifica seguidores' do
+ it 'com sucesso' do
+ follower = create(:user, full_name: 'Paulo')
+ followed = create(:user, full_name: 'Ana')
+ Connection.create!(followed_profile: followed.profile, follower: follower.profile)
+ new_post_notification_job_spy = spy(NewPostNotificationJob)
+ stub_const('NewPostNotificationJob', new_post_notification_job_spy)
+ create(:post, user: followed)
+
+ expect(new_post_notification_job_spy).to have_received(:perform_later)
+ end
+ end
+
+ context 'e vê curtidas' do
+ it 'em seu post' do
+ user = create(:user)
+ post = create(:post, user:)
+ like = create(:like, likeable: post)
+
+ login_as user
+ visit notifications_path
+
+ expect(page).to have_current_path notifications_path
+ expect(page).to have_content 'curtiu sua publicação'
+ expect(page).to have_link like.user.full_name, href: profile_path(like.user.profile)
+ end
+
+ it 'e não recebe quando curte sua própria publicação' do
+ user = create(:user)
+ post = create(:post, user:)
+ like = create(:like, likeable: post, user:)
+
+ login_as user
+ visit notifications_path
+
+ expect(page).not_to have_content 'curtiu sua publicação'
+ expect(page).not_to have_link like.user.profile.full_name, href: profile_path(like.user.profile)
+ end
end
end