destroy notifications when marked as read

This commit is contained in:
rgarcia
2016-01-07 12:02:01 +01:00
parent b5e9113718
commit e2f419e625
12 changed files with 120 additions and 84 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -14,6 +14,7 @@ class Notification < ActiveRecord::Base
notifiable.created_at
end
def mark_as_read!
def mark_as_read
self.destroy
end
end

View File

@@ -0,0 +1,8 @@
<li id="<%= dom_id(notification) %>" class="notification">
<%= link_to notification do %>
<time><%= l notification.timestamp, format: :datetime %></time>&nbsp;&bull;&nbsp;
<span><%= notification.username %> </span>
<span><%= t("notifications.index.#{notification_action(notification)}") %></span>
<span><%= notification.notifiable.commentable.title %></span>
<% end %>
</li>

View File

@@ -1,11 +1,14 @@
<div class="row">
<% if @notifications.empty? %>
<div><%= t("notifications.index.empty_notifications") %></div>
<% else %>
<div class="row">
<ul>
<% @notifications.each do |notification| %>
<li>
<time><%= l notification.timestamp, format: :datetime %></time>&nbsp;&bull;&nbsp;
<%= notification.username %> <%= notification_text_for(notification) %>
<%= link_to notification.notifiable.commentable.title, notification.notifiable.commentable %>
</li>
<% end %>
<%= render @notifications %>
</ul>
</div>
<div>
<%= link_to t("notifications.index.mark_all_as_read"),
mark_all_as_read_notifications_path, method: :put %>
</div>
</div>
<% end %>

View File

@@ -267,9 +267,6 @@ en:
zero: "No votes"
one: "1 vote"
other: "%{count} votes"
notifications:
commented_on_your_debate: "commented on your debate"
replied_to_your_comment: "replied to your comment on"
comments_helper:
comment_link: "Comment"
comment_button: "Publish comment"
@@ -313,6 +310,12 @@ en:
user_permission_votes: "Participate on final voting"
user_permission_verify: "To perform all the actions verify your account."
user_permission_verify_info: "* Only for users on Madrid City Census."
notifications:
index:
mark_all_as_read: "Mark all as read"
empty_notifications: "There are no new notifications."
commented_on_your_debate: "commented on your debate"
replied_to_your_comment: "replied to your comment on"
simple_captcha:
placeholder: "Enter the text from the image"
label: "Enter the text from the image in the box below"

View File

@@ -267,9 +267,6 @@ es:
zero: "Sin votos"
one: "1 voto"
other: "%{count} votos"
notifications:
commented_on_your_debate: "ha comentado en tu debate"
replied_to_your_comment: "ha respondido a tu comentario en"
comments_helper:
comment_link: "Comentar"
comment_button: "Publicar comentario"
@@ -313,6 +310,12 @@ es:
user_permission_votes: "Participar en las votaciones finales*"
user_permission_verify: "Para poder realizar todas las acciones verifica tu cuenta."
user_permission_verify_info: "* Sólo usuarios empadronados en el municipio de Madrid."
notifications:
index:
mark_all_as_read: "Marcar todas como leídas"
empty_notifications: "No hay notificaciones nuevas."
commented_on_your_debate: "ha comentado en tu debate"
replied_to_your_comment: "ha respondido a tu comentario en"
simple_captcha:
placeholder: "Introduce el texto de la imagen"
label: "Introduce el texto de la imagen en la siguiente caja"

View File

@@ -75,7 +75,9 @@ Rails.application.routes.draw do
collection { get :erase }
end
resources :notifications, only: :index
resources :notifications, only: [:index, :show] do
collection { put :mark_all_as_read }
end
resource :verification, controller: "verification", only: [:show]

View File

@@ -1,21 +0,0 @@
require 'rails_helper'
describe NotificationsController do
describe "#index" do
let(:user) { create :user }
it "mark all notifications as read" do
notifications = [create(:notification, user: user), create(:notification, user: user)]
Notification.all.each do |notification|
expect(notification.read).to be false
end
sign_in user
get :index
Notification.all.each do |notification|
expect(notification.read).to be true
end
end
end
end

View File

@@ -21,10 +21,10 @@ feature "Notifications" do
expect(page).to have_xpath "//a[@class='with_notifications' and @href='#{notifications_path}' and text()='Notificaciones']"
click_link "Notificaciones"
expect(page).to have_css ".notification", count: 1
expect(page).to have_content user.username
expect(page).to have_content I18n.t("comments.notifications.commented_on_your_debate")
expect(page).to_not have_content I18n.t("comments.notifications.replied_to_your_comment")
expect(page).to have_link debate.title, href: debate_path(debate)
expect(page).to have_content "commented on your debate"
expect(page).to have_xpath "//a[@href='#{notification_path(Notification.last)}']"
end
scenario "User replied to my comment", :js do
@@ -48,10 +48,10 @@ feature "Notifications" do
expect(page).to have_xpath "//a[@class='with_notifications' and @href='#{notifications_path}' and text()='Notificaciones']"
visit notifications_path
expect(page).to have_css ".notification", count: 1
expect(page).to have_content user.username
expect(page).to have_content I18n.t("comments.notifications.replied_to_your_comment")
expect(page).to_not have_content I18n.t("comments.notifications.commented_on_your_debate")
expect(page).to have_link debate.title, href: debate_path(debate)
expect(page).to have_content "replied to your comment on"
expect(page).to have_xpath "//a[@href='#{notification_path(Notification.last)}']"
end
scenario "Author commented on his own debate", :js do
@@ -66,10 +66,7 @@ feature "Notifications" do
expect(page).to have_xpath "//a[@class='without_notifications' and @href='#{notifications_path}' and text()='Notificaciones']"
click_link "Notificaciones"
expect(page).to_not have_content user.username
expect(page).to_not have_content I18n.t("comments.notifications.commented_on_your_debate")
expect(page).to_not have_content I18n.t("comments.notifications.replied_to_your_comment")
expect(page).to_not have_link debate.title, href: debate_path(debate)
expect(page).to have_css ".notification", count: 0
end
scenario "Author replied to his own comment", :js do
@@ -89,9 +86,47 @@ feature "Notifications" do
expect(page).to have_xpath "//a[@class='without_notifications' and @href='#{notifications_path}' and text()='Notificaciones']"
visit notifications_path
expect(page).to_not have_content user.username
expect(page).to_not have_content I18n.t("comments.notifications.replied_to_your_comment")
expect(page).to_not have_content I18n.t("comments.notifications.commented_on_your_debate")
expect(page).to_not have_link debate.title, href: debate_path(debate)
expect(page).to have_css ".notification", count: 0
end
context "mark as read" do
scenario "mark a single notification as read" do
user = create :user
notification = create :notification, user: user
login_as user
visit notifications_path
expect(page).to have_css ".notification", count: 1
first(".notification a").click
visit notifications_path
expect(page).to have_css ".notification", count: 0
end
scenario "mark all notifications as read" do
user = create :user
2.times { create :notification, user: user }
login_as user
visit notifications_path
expect(page).to have_css ".notification", count: 2
click_link "Mark all as read"
expect(page).to have_css ".notification", count: 0
expect(current_path).to eq(notifications_path)
end
end
scenario "no notifications" do
login_as user
visit notifications_path
expect(page).to have_content "There are no new notifications"
end
end

View File

@@ -2,7 +2,7 @@ require 'rails_helper'
describe NotificationsHelper do
describe "#notification_text_for" do
describe "#notification_action" do
let(:debate) { create :debate }
let(:debate_comment) { create :comment, commentable: debate }
let(:comment_reply) { create :comment, commentable: debate, parent: debate_comment }
@@ -10,14 +10,14 @@ describe NotificationsHelper do
context "when action was comment on a debate" do
it "returns correct text when someone comments on your debate" do
notification = create :notification, notifiable: debate_comment
expect(notification_text_for(notification)).to eq "commented on your debate"
expect(notification_action(notification)).to eq "commented_on_your_debate"
end
end
context "when action was comment on a debate" do
it "returns correct text when someone replies to your comment" do
notification = create :notification, notifiable: comment_reply
expect(notification_text_for(notification)).to eq "replied to your comment on"
expect(notification_action(notification)).to eq "replied_to_your_comment"
end
end
end
@@ -25,20 +25,13 @@ describe NotificationsHelper do
describe "#notifications_class_for" do
let(:user) { create :user }
context "when user doesn't have any notification" do
context "when user doesn't have notifications" do
it "returns class 'without_notifications'" do
expect(notifications_class_for(user)).to eq "without_notifications"
end
end
context "when user doesn't have unread notifications" do
it "returns class 'without_notifications'" do
notification = create :notification, user: user, read: true
expect(notifications_class_for(user)).to eq "without_notifications"
end
end
context "when user has unread notifications" do
context "when user has notifications" do
it "returns class 'with_notifications'" do
notification = create :notification, user: user
expect(notifications_class_for(user)).to eq "with_notifications"

View File

@@ -4,12 +4,8 @@ describe Notification do
describe "#unread (scope)" do
it "returns only unread notifications" do
unread_notification = create :notification
read_notification = create :notification, read: true
unread_notifications = Notification.unread
expect(unread_notifications.size).to be 1
expect(unread_notifications.first).to eq unread_notification
2.times { create :notification }
expect(Notification.unread.size).to be 2
end
end
@@ -42,12 +38,12 @@ describe Notification do
end
describe "#mark_as_read" do
it "set up read flag to true" do
it "destroys notification" do
notification = create :notification
expect(notification.read).to be false
expect(Notification.unread.size).to eq 1
notification.mark_as_read!
expect(notification.read).to be true
notification.mark_as_read
expect(Notification.unread.size).to eq 0
end
end