Skip to content

Commit 2da01f6

Browse files
authored
Merge pull request #201 from TreinaDev/feat/marcar-notificacoes-como-lida
Feat/marcar notificacoes como lida
2 parents 98268cf + 5c66ade commit 2da01f6

24 files changed

+206
-63
lines changed

app/assets/stylesheets/application.bootstrap.scss

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ $theme-colors: (
88
"light": rgba(255, 251, 251, 0.603),
99
"dark": #1b1b1b
1010
);
11+
12+
$primary: #a130fd;
13+
1114
@import 'bootstrap/scss/bootstrap';
1215
@import 'bootstrap-icons/font/bootstrap-icons';
1316
@import 'actiontext.css';

app/controllers/notifications_controller.rb

+39
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,44 @@ class NotificationsController < ApplicationController
33

44
def index
55
@notifications = current_user.profile.notifications.order(created_at: :desc)
6+
@notifications.map { |n| n.seen! if n.unseen? }
7+
end
8+
9+
def update
10+
@notification = Notification.find(params[:id])
11+
@notification.clicked!
12+
redirect_to_notification
13+
end
14+
15+
private
16+
17+
def redirect_to_notification
18+
return redirect_to_invitation if @notification.notifiable.is_a? Invitation
19+
return redirect_to_comment if @notification.notifiable.is_a? Comment
20+
return redirect_to_connection if @notification.notifiable.is_a? Connection
21+
return redirect_to_post if @notification.notifiable.is_a? Post
22+
23+
redirect_to_like if @notification.notifiable.is_a? Like
24+
end
25+
26+
def redirect_to_invitation
27+
redirect_to invitation_path(@notification.notifiable)
28+
end
29+
30+
def redirect_to_comment
31+
redirect_to post_path(@notification.notifiable.post)
32+
end
33+
34+
def redirect_to_connection
35+
redirect_to profile_path(@notification.notifiable.follower)
36+
end
37+
38+
def redirect_to_post
39+
redirect_to post_path(@notification.notifiable)
40+
end
41+
42+
def redirect_to_like
43+
likeable = @notification.notifiable.likeable
44+
redirect_to post_path(likeable.is_a?(Comment) ? likeable.post : likeable)
645
end
746
end

app/models/notification.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Notification < ApplicationRecord
22
belongs_to :profile
33
belongs_to :notifiable, polymorphic: true
4+
5+
enum status: { unseen: 0, seen: 5, clicked: 10 }
46
end

app/models/profile.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ class Profile < ApplicationRecord
3838
enum privacy: { private_profile: 0, public_profile: 10 }
3939
enum status: { inactive: 0, active: 5 }
4040

41-
delegate :full_name, to: :user
42-
delegate :email, to: :user
41+
delegate :full_name, :email, to: :user
4342

4443
def self.advanced_search(search_query)
4544
left_outer_joins(:job_categories, :personal_info, :user).where(
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<p><%= I18n.t('notifications.new_follower_mail', follower_name: @notification.notifiable.follower.full_name) %></p>
1+
<p><%= @notification.notifiable.follower.full_name %> <%= t('notifications.started_following_you') %></p>
+10-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<% if notification.notifiable.post.user != current_user %>
2-
<p>
3-
<%= link_to notification.notifiable.user.full_name,
4-
profile_path(notification.notifiable.user.profile) %>
5-
<%= t('notifications.index.commented_on_post') %>
6-
"<%= link_to notification.notifiable.post.title,
7-
post_path(notification.notifiable.post, anchor: "comment_#{notification.notifiable.id}") %>"
1+
<% if notification.notifiable.post.user == current_user %>
2+
<%= notification.notifiable.user.full_name %>
3+
<%= t('notifications.commented_on_your_post') %>
4+
<%= notification.notifiable.post.title %>
85
<% else %>
9-
<p>
10-
<%= link_to notification.notifiable.user.full_name, profile_path(notification.notifiable.user.profile) %> <%= t('notifications.index.commented_on_your_post') %>
11-
<% end %>
6+
<%= notification.notifiable.user.full_name %>
7+
<%= t('notifications.commented_on_post') %>
8+
<%= link_to notification.notifiable.post.title,
9+
post_path(notification.notifiable.post,
10+
anchor: "comment_#{notification.notifiable.id}") %>
11+
<% end %>
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<p><%= link_to notification.notifiable.follower.user.full_name, profile_path(notification.notifiable.follower) %> começou a te seguir
1+
<%= notification.notifiable.follower.full_name %> <%= t('notifications.started_following_you') %>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
<p>
2-
Você recebeu um convite para
3-
<%= link_to notification.notifiable.project_title,
4-
invitation_path(notification.notifiable) %>
1+
<%= t('notifications.new_invitation_to') %> <%= notification.notifiable.project_title %>
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<% if notification.notifiable.likeable.is_a?Post %>
2-
<p><%= link_to notification.notifiable.user.full_name, profile_path(notification.notifiable.user.profile) %> curtiu sua publicação
2+
<%= notification.notifiable.user.full_name %> <%= t('notifications.liked_your_post') %>
33
<% else %>
4-
<p><%= link_to notification.notifiable.user.full_name, profile_path(notification.notifiable.user.profile) %> curtiu seu comentário
4+
<%= notification.notifiable.user.full_name %> <%= t('notifications.liked_your_comment') %>
55
<% end %>
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<p><%= notification.notifiable.user.full_name %> fez uma <%= link_to 'publicação', post_path(notification.notifiable) %>
1+
<%= notification.notifiable.user.full_name %> <%= t('notifications.published_a_new_post') %>
+14-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<div class="container d-flex flex-column align-items-center">
2-
<h2><%= Notification.model_name.human(count: @notifications.count) %></h2>
3-
<% if @notifications&.any? %>
4-
<% @notifications.each do |notification| %>
5-
<div class="list-group">
6-
<%= render(
7-
partial: "#{notification.notifiable.class.name.downcase}" ,
8-
locals: { notification: notification }
9-
)%>
10-
<%= t('.time_ago', time: time_ago_in_words(notification.notifiable.created_at)) %>
11-
</div>
12-
<% end %>
2+
<h2><%= Notification.model_name.human(count: 2) %></h2>
3+
<% if @notifications.any? %>
4+
<ul class="list-group list-group-action">
5+
<% @notifications.each do |notification| %>
6+
<li class="list-group-item <%= 'list-group-item-primary' if notification.seen? %> ">
7+
<%= button_to notification_path(notification), method: :patch, class: 'btn btn-link' do %>
8+
<%= render( partial: "#{notification.notifiable.class.name.downcase}",
9+
locals: { notification: notification } ) %>
10+
<%= t('notifications.time_ago', time: time_ago_in_words(notification.notifiable.created_at)) %>
11+
<% end %>
12+
</li>
13+
<% end %>
14+
</ul>
1315
<% else %>
14-
<p><%= t('.no_notifications') %></p>
16+
<p><%= t('notifications.no_notification') %></p>
1517
<% end %>
1618
</div>

app/views/shared/_navbar.html.erb

+6-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@
4444
</li>
4545
<li class="dropdown-item">
4646
<%= link_to notifications_path, class: 'nav-link' do %>
47-
Notificações <span class="badge bg-primary"><%= current_user.profile.notifications.count %></span>
47+
Notificações
48+
<% if current_user.profile.notifications.unseen.count > 0 %>
49+
<span class="badge bg-primary">
50+
<%= current_user.profile.notifications.unseen.count %>
51+
</span>
52+
<% end %>
4853
<% end %>
4954
</li>
5055
<% if current_user.admin? %>

config/locales/notifications.pt-BR.yml

+9-8
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ pt-BR:
44
notification:
55
one: 'Notificação'
66
other: 'Notificações'
7-
87
notifications:
9-
new_follower_mail: '%{follower_name} te seguiu'
10-
new_notificaiton: 'Você recebeu uma nova notificação!'
11-
index:
12-
no_notifications: 'Nenhuma notificação encontrada'
13-
commented_on_post: 'comentou no post'
14-
commented_on_your_post: 'comentou em sua publicação'
15-
time_ago: 'há %{time}'
8+
no_notification: 'Nenhuma notificação'
9+
published_a_new_post: 'fez uma publicação'
10+
liked_your_post: 'curtiu sua publicação'
11+
liked_your_comment: 'curtiu seu comentário'
12+
commented_on_your_post: 'comentou em sua publicação:'
13+
commented_on_post: 'comentou na publicação:'
14+
started_following_you: 'começou a seguir você'
15+
new_invitation_to: 'Você recebeu um convite para'
16+
time_ago: 'há %{time}'

config/routes.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
post '/projects', to: 'projects#create_invitation_request', as: 'invitation_request'
1515

1616
resources :job_categories, only: %i[index create destroy]
17-
resources :notifications, only: %i[index]
17+
resources :notifications, only: %i[index update]
1818

1919
resources :posts, only: %i[new create] do
2020
resources :comments, only: %i[create]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddStatusToNotifications < ActiveRecord::Migration[7.1]
2+
def change
3+
add_column :notifications, :status, :integer, default: 0
4+
end
5+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class RemoveReadFromNotifications < ActiveRecord::Migration[7.1]
2+
def change
3+
remove_column :notifications, :read, :boolean
4+
end
5+
end

db/schema.rb

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/mailer/connections_mailer_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
expect(mail.subject).to include 'Alguém seguiu seu perfil'
1515
expect(mail.to).to eq [followed_profile.email]
1616
expect(mail.from).to eq ['[email protected]']
17-
expect(mail.body).to include 'Danilo Martins te seguiu'
17+
expect(mail.body).to include 'Danilo Martins começou a seguir você'
1818
end
1919
end
2020
end

spec/system/notifications/user_is_notified_about_post_of_interest_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
login_as interested_user
4242
visit notifications_path
4343

44-
expect(page).to have_content 'Gabriel comentou no post "Meu segundo post" há 10 minutos'
45-
expect(page).to have_content 'Gabriel comentou no post "Meu primeiro post" há aproximadamente 10 horas'
44+
expect(page).to have_content 'Gabriel comentou na publicação: Meu segundo post há 10 minutos'
45+
expect(page).to have_content 'Gabriel comentou na publicação: Meu primeiro post há aproximadamente 10 horas'
4646
expect(page).to have_link post_one.title, href: post_path(post_one, anchor: "comment_#{comment_one.id}")
4747
expect(page).to have_link post_two.title, href: post_path(post_two, anchor: "comment_#{comment_two.id}")
4848
end

spec/system/notifications/user_sees_comments_notification_spec.rb

+37-5
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
user = create(:user)
66
post = create(:post, user:)
77
other_user = create(:user)
8-
comment = create(:comment, post:, user: other_user)
8+
create(:comment, post:, user: other_user)
99

1010
login_as user
1111
visit notifications_path
1212

1313
expect(Notification.count).to eq 1
1414
expect(page).to have_current_path notifications_path
15-
expect(page).to have_content 'comentou em sua publicação'
16-
expect(page).to have_link comment.user.profile.full_name, href: profile_path(comment.user.profile)
15+
expect(page).to have_content "#{other_user.full_name} comentou em sua publicação: #{post.title}"
1716
end
1817

1918
it 'e não vê notificação de seu comentário' do
@@ -43,8 +42,8 @@
4342
end
4443

4544
expect(page).to have_current_path notifications_path
46-
expect(page).to have_content 'curtiu seu comentário'
47-
expect(page).to have_link like.user.profile.full_name, href: profile_path(like.user.profile)
45+
expect(page).to have_content "#{like.user.full_name} curtiu seu comentário"
46+
expect(Notification.last).to be_seen
4847
end
4948

5049
it 'e não recebe ao curtir seu próprio comentário' do
@@ -63,4 +62,37 @@
6362
expect(page).not_to have_content 'curtiu seu comentário'
6463
expect(page).not_to have_link like.user.profile.full_name, href: profile_path(like.user.profile)
6564
end
65+
66+
context 'ao clicar na notificação' do
67+
it 'de comentário é redirecionado para a página do post' do
68+
user = create(:user)
69+
post = create(:post, user:)
70+
comment = create(:comment, post:)
71+
72+
login_as user
73+
visit notifications_path
74+
click_on comment.user.full_name
75+
76+
expect(page).to have_current_path post_path(post)
77+
expect(page).to have_content post.title
78+
expect(page).to have_content comment.message
79+
expect(Notification.last).to be_clicked
80+
end
81+
82+
it 'de curtida é redirecionado para a página do post' do
83+
user = create(:user)
84+
post = create(:post)
85+
comment = create(:comment, post:, user:)
86+
like = create(:like, likeable: comment)
87+
88+
login_as user
89+
visit notifications_path
90+
click_on like.user.full_name
91+
92+
expect(page).to have_current_path post_path(post)
93+
expect(page).to have_content post.title
94+
expect(page).to have_content comment.message
95+
expect(Notification.last).to be_clicked
96+
end
97+
end
6698
end

spec/system/notifications/user_sees_invitation_notification_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
expect(page).to have_current_path notifications_path
1212
expect(page).to have_content "Você recebeu um convite para #{invitation.project_title} há 1 dia"
13-
expect(page).to have_link invitation.project_title, href: invitation_path(invitation)
1413
end
1514

1615
it 'e não vê convites de outros usuários' do
@@ -28,7 +27,7 @@
2827
expect(page).to_not have_content "Você recebeu um convite para #{other_user_invitation.project_title}"
2928
end
3029

31-
it 'ao clicar na notificação é redirecionado para a página de convites' do
30+
it 'ao clicar na notificação é redirecionado para a página do convite' do
3231
user = create(:user)
3332
invitation = create(:invitation, profile: user.profile)
3433

@@ -38,5 +37,6 @@
3837

3938
expect(page).to have_current_path invitation_path(invitation)
4039
expect(page).to have_content invitation.project_title
40+
expect(Notification.last).to be_clicked
4141
end
4242
end

spec/system/notifications/user_sees_new_follower_notification_spec.rb

+14-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,19 @@
1010
visit notifications_path
1111

1212
expect(page).to have_current_path notifications_path
13-
expect(page).to have_content 'Paulo começou a te seguir'
14-
expect(page).to have_link follower.full_name, href: profile_path(follower)
13+
expect(page).to have_content 'Paulo começou a seguir você'
14+
end
15+
16+
it 'ao clicar na notificação redireciona para o perfil do seguidor' do
17+
follower = create(:user, full_name: 'Paulo')
18+
followed = create(:user, full_name: 'Ana')
19+
Connection.create!(followed_profile: followed.profile, follower: follower.profile)
20+
21+
login_as followed
22+
visit notifications_path
23+
click_on 'Paulo começou a seguir você'
24+
25+
expect(page).to have_current_path profile_path(follower)
26+
expect(Notification.last).to be_clicked
1527
end
1628
end

0 commit comments

Comments
 (0)