marcar notificaciones como leidas despues de ser vistas

This commit is contained in:
Julian Herrero
2015-10-06 20:48:46 +02:00
committed by rgarcia
parent d9ba3edc2a
commit 9a5f525dc9
4 changed files with 25 additions and 5 deletions

View File

@@ -4,6 +4,7 @@ class NotificationsController < ApplicationController
def index def index
@notifications = current_user.notifications.unread.recent.for_render @notifications = current_user.notifications.unread.recent.for_render
@notifications.each { |notification| notification.mark_as_read! }
end end
end end

View File

@@ -9,4 +9,8 @@ class Notification < ActiveRecord::Base
def timestamp def timestamp
activity.trackable.created_at activity.trackable.created_at
end end
def mark_as_read!
update_attribute :read, true
end
end end

View File

@@ -4,13 +4,18 @@ describe NotificationsController do
describe "#index" do describe "#index" do
let(:user) { create :user } let(:user) { create :user }
let(:notification) { create :notification, user: user }
it "assigns @notifications" do 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 sign_in user
get :index
get :index, debate: { title: 'A sample debate', description: 'this is a sample debate', terms_of_service: 1 } Notification.all.each do |notification|
expect(assigns(:notifications)).to eq user.notifications.unread.recent.for_render expect(notification.read).to be true
end
end end
end end
end end

View File

@@ -41,4 +41,14 @@ describe Notification do
expect(notification.timestamp).to eq comment.created_at expect(notification.timestamp).to eq comment.created_at
end end
end end
describe "#mark_as_read" do
it "set up read flag to true" do
notification = create :notification
expect(notification.read).to be false
notification.mark_as_read!
expect(notification.read).to be true
end
end
end end