Skip to content

Commit

Permalink
Merge pull request #201 from TreinaDev/feat/marcar-notificacoes-como-…
Browse files Browse the repository at this point in the history
…lida

Feat/marcar notificacoes como lida
  • Loading branch information
hreis1 authored Feb 13, 2024
2 parents 98268cf + 5c66ade commit 2da01f6
Show file tree
Hide file tree
Showing 24 changed files with 206 additions and 63 deletions.
3 changes: 3 additions & 0 deletions app/assets/stylesheets/application.bootstrap.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ $theme-colors: (
"light": rgba(255, 251, 251, 0.603),
"dark": #1b1b1b
);

$primary: #a130fd;

@import 'bootstrap/scss/bootstrap';
@import 'bootstrap-icons/font/bootstrap-icons';
@import 'actiontext.css';
Expand Down
39 changes: 39 additions & 0 deletions app/controllers/notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,44 @@ class NotificationsController < ApplicationController

def index
@notifications = current_user.profile.notifications.order(created_at: :desc)
@notifications.map { |n| n.seen! if n.unseen? }
end

def update
@notification = Notification.find(params[:id])
@notification.clicked!
redirect_to_notification
end

private

def redirect_to_notification
return redirect_to_invitation if @notification.notifiable.is_a? Invitation
return redirect_to_comment if @notification.notifiable.is_a? Comment
return redirect_to_connection if @notification.notifiable.is_a? Connection
return redirect_to_post if @notification.notifiable.is_a? Post

redirect_to_like if @notification.notifiable.is_a? Like
end

def redirect_to_invitation
redirect_to invitation_path(@notification.notifiable)
end

def redirect_to_comment
redirect_to post_path(@notification.notifiable.post)
end

def redirect_to_connection
redirect_to profile_path(@notification.notifiable.follower)
end

def redirect_to_post
redirect_to post_path(@notification.notifiable)
end

def redirect_to_like
likeable = @notification.notifiable.likeable
redirect_to post_path(likeable.is_a?(Comment) ? likeable.post : likeable)
end
end
2 changes: 2 additions & 0 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Notification < ApplicationRecord
belongs_to :profile
belongs_to :notifiable, polymorphic: true

enum status: { unseen: 0, seen: 5, clicked: 10 }
end
3 changes: 1 addition & 2 deletions app/models/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ class Profile < ApplicationRecord
enum privacy: { private_profile: 0, public_profile: 10 }
enum status: { inactive: 0, active: 5 }

delegate :full_name, to: :user
delegate :email, to: :user
delegate :full_name, :email, to: :user

def self.advanced_search(search_query)
left_outer_joins(:job_categories, :personal_info, :user).where(
Expand Down
2 changes: 1 addition & 1 deletion app/views/connections_mailer/notify_follow.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<p><%= I18n.t('notifications.new_follower_mail', follower_name: @notification.notifiable.follower.full_name) %></p>
<p><%= @notification.notifiable.follower.full_name %> <%= t('notifications.started_following_you') %></p>
20 changes: 10 additions & 10 deletions app/views/notifications/_comment.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<% if notification.notifiable.post.user != current_user %>
<p>
<%= link_to notification.notifiable.user.full_name,
profile_path(notification.notifiable.user.profile) %>
<%= t('notifications.index.commented_on_post') %>
"<%= link_to notification.notifiable.post.title,
post_path(notification.notifiable.post, anchor: "comment_#{notification.notifiable.id}") %>"
<% if notification.notifiable.post.user == current_user %>
<%= notification.notifiable.user.full_name %>
<%= t('notifications.commented_on_your_post') %>
<%= notification.notifiable.post.title %>
<% else %>
<p>
<%= link_to notification.notifiable.user.full_name, profile_path(notification.notifiable.user.profile) %> <%= t('notifications.index.commented_on_your_post') %>
<% end %>
<%= notification.notifiable.user.full_name %>
<%= t('notifications.commented_on_post') %>
<%= link_to notification.notifiable.post.title,
post_path(notification.notifiable.post,
anchor: "comment_#{notification.notifiable.id}") %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/notifications/_connection.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<p><%= link_to notification.notifiable.follower.user.full_name, profile_path(notification.notifiable.follower) %> começou a te seguir
<%= notification.notifiable.follower.full_name %> <%= t('notifications.started_following_you') %>
5 changes: 1 addition & 4 deletions app/views/notifications/_invitation.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
<p>
Você recebeu um convite para
<%= link_to notification.notifiable.project_title,
invitation_path(notification.notifiable) %>
<%= t('notifications.new_invitation_to') %> <%= notification.notifiable.project_title %>
4 changes: 2 additions & 2 deletions app/views/notifications/_like.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% if notification.notifiable.likeable.is_a?Post %>
<p><%= link_to notification.notifiable.user.full_name, profile_path(notification.notifiable.user.profile) %> curtiu sua publicação
<%= notification.notifiable.user.full_name %> <%= t('notifications.liked_your_post') %>
<% else %>
<p><%= link_to notification.notifiable.user.full_name, profile_path(notification.notifiable.user.profile) %> curtiu seu comentário
<%= notification.notifiable.user.full_name %> <%= t('notifications.liked_your_comment') %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/notifications/_post.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<p><%= notification.notifiable.user.full_name %> fez uma <%= link_to 'publicação', post_path(notification.notifiable) %>
<%= notification.notifiable.user.full_name %> <%= t('notifications.published_a_new_post') %>
26 changes: 14 additions & 12 deletions app/views/notifications/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<div class="container d-flex flex-column align-items-center">
<h2><%= Notification.model_name.human(count: @notifications.count) %></h2>
<% if @notifications&.any? %>
<% @notifications.each do |notification| %>
<div class="list-group">
<%= render(
partial: "#{notification.notifiable.class.name.downcase}" ,
locals: { notification: notification }
)%>
<%= t('.time_ago', time: time_ago_in_words(notification.notifiable.created_at)) %>
</div>
<% end %>
<h2><%= Notification.model_name.human(count: 2) %></h2>
<% if @notifications.any? %>
<ul class="list-group list-group-action">
<% @notifications.each do |notification| %>
<li class="list-group-item <%= 'list-group-item-primary' if notification.seen? %> ">
<%= button_to notification_path(notification), method: :patch, class: 'btn btn-link' do %>
<%= render( partial: "#{notification.notifiable.class.name.downcase}",
locals: { notification: notification } ) %>
<%= t('notifications.time_ago', time: time_ago_in_words(notification.notifiable.created_at)) %>
<% end %>
</li>
<% end %>
</ul>
<% else %>
<p><%= t('.no_notifications') %></p>
<p><%= t('notifications.no_notification') %></p>
<% end %>
</div>
7 changes: 6 additions & 1 deletion app/views/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@
</li>
<li class="dropdown-item">
<%= link_to notifications_path, class: 'nav-link' do %>
Notificações <span class="badge bg-primary"><%= current_user.profile.notifications.count %></span>
Notificações
<% if current_user.profile.notifications.unseen.count > 0 %>
<span class="badge bg-primary">
<%= current_user.profile.notifications.unseen.count %>
</span>
<% end %>
<% end %>
</li>
<% if current_user.admin? %>
Expand Down
17 changes: 9 additions & 8 deletions config/locales/notifications.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ pt-BR:
notification:
one: 'Notificação'
other: 'Notificações'

notifications:
new_follower_mail: '%{follower_name} te seguiu'
new_notificaiton: 'Você recebeu uma nova notificação!'
index:
no_notifications: 'Nenhuma notificação encontrada'
commented_on_post: 'comentou no post'
commented_on_your_post: 'comentou em sua publicação'
time_ago: 'há %{time}'
no_notification: 'Nenhuma notificação'
published_a_new_post: 'fez uma publicação'
liked_your_post: 'curtiu sua publicação'
liked_your_comment: 'curtiu seu comentário'
commented_on_your_post: 'comentou em sua publicação:'
commented_on_post: 'comentou na publicação:'
started_following_you: 'começou a seguir você'
new_invitation_to: 'Você recebeu um convite para'
time_ago: 'há %{time}'
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
post '/projects', to: 'projects#create_invitation_request', as: 'invitation_request'

resources :job_categories, only: %i[index create destroy]
resources :notifications, only: %i[index]
resources :notifications, only: %i[index update]

resources :posts, only: %i[new create] do
resources :comments, only: %i[create]
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240212195517_add_status_to_notifications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddStatusToNotifications < ActiveRecord::Migration[7.1]
def change
add_column :notifications, :status, :integer, default: 0
end
end
5 changes: 5 additions & 0 deletions db/migrate/20240212195603_remove_read_from_notifications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveReadFromNotifications < ActiveRecord::Migration[7.1]
def change
remove_column :notifications, :read, :boolean
end
end
6 changes: 3 additions & 3 deletions db/schema.rb

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

2 changes: 1 addition & 1 deletion spec/mailer/connections_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
expect(mail.subject).to include 'Alguém seguiu seu perfil'
expect(mail.to).to eq [followed_profile.email]
expect(mail.from).to eq ['[email protected]']
expect(mail.body).to include 'Danilo Martins te seguiu'
expect(mail.body).to include 'Danilo Martins começou a seguir você'
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
login_as interested_user
visit notifications_path

expect(page).to have_content 'Gabriel comentou no post "Meu segundo post" há 10 minutos'
expect(page).to have_content 'Gabriel comentou no post "Meu primeiro post" há aproximadamente 10 horas'
expect(page).to have_content 'Gabriel comentou na publicação: Meu segundo post há 10 minutos'
expect(page).to have_content 'Gabriel comentou na publicação: Meu primeiro post há aproximadamente 10 horas'
expect(page).to have_link post_one.title, href: post_path(post_one, anchor: "comment_#{comment_one.id}")
expect(page).to have_link post_two.title, href: post_path(post_two, anchor: "comment_#{comment_two.id}")
end
Expand Down
42 changes: 37 additions & 5 deletions spec/system/notifications/user_sees_comments_notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
user = create(:user)
post = create(:post, user:)
other_user = create(:user)
comment = create(:comment, post:, user: other_user)
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)
expect(page).to have_content "#{other_user.full_name} comentou em sua publicação: #{post.title}"
end

it 'e não vê notificação de seu comentário' do
Expand Down Expand Up @@ -43,8 +42,8 @@
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)
expect(page).to have_content "#{like.user.full_name} curtiu seu comentário"
expect(Notification.last).to be_seen
end

it 'e não recebe ao curtir seu próprio comentário' do
Expand All @@ -63,4 +62,37 @@
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

context 'ao clicar na notificação' do
it 'de comentário é redirecionado para a página do post' do
user = create(:user)
post = create(:post, user:)
comment = create(:comment, post:)

login_as user
visit notifications_path
click_on comment.user.full_name

expect(page).to have_current_path post_path(post)
expect(page).to have_content post.title
expect(page).to have_content comment.message
expect(Notification.last).to be_clicked
end

it 'de curtida é redirecionado para a página do post' do
user = create(:user)
post = create(:post)
comment = create(:comment, post:, user:)
like = create(:like, likeable: comment)

login_as user
visit notifications_path
click_on like.user.full_name

expect(page).to have_current_path post_path(post)
expect(page).to have_content post.title
expect(page).to have_content comment.message
expect(Notification.last).to be_clicked
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

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 não vê convites de outros usuários' do
Expand All @@ -28,7 +27,7 @@
expect(page).to_not have_content "Você recebeu um convite para #{other_user_invitation.project_title}"
end

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

Expand All @@ -38,5 +37,6 @@

expect(page).to have_current_path invitation_path(invitation)
expect(page).to have_content invitation.project_title
expect(Notification.last).to be_clicked
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@
visit notifications_path

expect(page).to have_current_path notifications_path
expect(page).to have_content 'Paulo começou a te seguir'
expect(page).to have_link follower.full_name, href: profile_path(follower)
expect(page).to have_content 'Paulo começou a seguir você'
end

it 'ao clicar na notificação redireciona para o perfil do seguidor' do
follower = create(:user, full_name: 'Paulo')
followed = create(:user, full_name: 'Ana')
Connection.create!(followed_profile: followed.profile, follower: follower.profile)

login_as followed
visit notifications_path
click_on 'Paulo começou a seguir você'

expect(page).to have_current_path profile_path(follower)
expect(Notification.last).to be_clicked
end
end
Loading

0 comments on commit 2da01f6

Please sign in to comment.