destroy notifications when marked as read
This commit is contained in:
@@ -1,9 +1,26 @@
|
||||
class NotificationsController < ApplicationController
|
||||
before_action :authenticate_user!
|
||||
load_and_authorize_resource class: "User"
|
||||
after_action :mark_as_read, only: :show
|
||||
skip_authorization_check
|
||||
|
||||
def index
|
||||
@notifications = current_user.notifications.recent.for_render
|
||||
@notifications = current_user.notifications.unread.recent.for_render
|
||||
end
|
||||
|
||||
def show
|
||||
@notification = current_user.notifications.find(params[:id])
|
||||
redirect_to url_for(@notification.notifiable.commentable)
|
||||
end
|
||||
|
||||
def mark_all_as_read
|
||||
current_user.notifications.each { |notification| notification.mark_as_read }
|
||||
redirect_to notifications_path
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def mark_as_read
|
||||
@notification.mark_as_read
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
module NotificationsHelper
|
||||
|
||||
def notification_text_for(notification)
|
||||
if notification.notifiable.reply?
|
||||
t("comments.notifications.replied_to_your_comment")
|
||||
else
|
||||
t("comments.notifications.commented_on_your_debate")
|
||||
end
|
||||
def notification_action(notification)
|
||||
notification.notifiable.reply? ? "replied_to_your_comment" : "commented_on_your_debate"
|
||||
end
|
||||
|
||||
def notifications_class_for(user)
|
||||
|
||||
@@ -14,6 +14,7 @@ class Notification < ActiveRecord::Base
|
||||
notifiable.created_at
|
||||
end
|
||||
|
||||
def mark_as_read!
|
||||
def mark_as_read
|
||||
self.destroy
|
||||
end
|
||||
end
|
||||
8
app/views/notifications/_notification.html.erb
Normal file
8
app/views/notifications/_notification.html.erb
Normal file
@@ -0,0 +1,8 @@
|
||||
<li id="<%= dom_id(notification) %>" class="notification">
|
||||
<%= link_to notification do %>
|
||||
<time><%= l notification.timestamp, format: :datetime %></time> •
|
||||
<span><%= notification.username %> </span>
|
||||
<span><%= t("notifications.index.#{notification_action(notification)}") %></span>
|
||||
<span><%= notification.notifiable.commentable.title %></span>
|
||||
<% end %>
|
||||
</li>
|
||||
@@ -1,11 +1,14 @@
|
||||
<div class="row">
|
||||
<ul>
|
||||
<% @notifications.each do |notification| %>
|
||||
<li>
|
||||
<time><%= l notification.timestamp, format: :datetime %></time> •
|
||||
<%= notification.username %> <%= notification_text_for(notification) %>
|
||||
<%= link_to notification.notifiable.commentable.title, notification.notifiable.commentable %>
|
||||
</li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<% if @notifications.empty? %>
|
||||
<div><%= t("notifications.index.empty_notifications") %></div>
|
||||
<% else %>
|
||||
<div class="row">
|
||||
<ul>
|
||||
<%= render @notifications %>
|
||||
</ul>
|
||||
|
||||
<div>
|
||||
<%= link_to t("notifications.index.mark_all_as_read"),
|
||||
mark_all_as_read_notifications_path, method: :put %>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
Reference in New Issue
Block a user