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' %> <% if current_user.admin? %>