diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 2fb715287..199893bd7 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -4,6 +4,7 @@ class NotificationsController < ApplicationController def index @notifications = current_user.notifications.unread.recent.for_render + @notifications.each { |notification| notification.mark_as_read! } end end diff --git a/app/models/notification.rb b/app/models/notification.rb index aff5c38fa..0f387f61f 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -9,4 +9,8 @@ class Notification < ActiveRecord::Base def timestamp activity.trackable.created_at end + + def mark_as_read! + update_attribute :read, true + end end diff --git a/spec/controllers/notifications_controller_spec.rb b/spec/controllers/notifications_controller_spec.rb index c4ff5f247..fa6104a9c 100644 --- a/spec/controllers/notifications_controller_spec.rb +++ b/spec/controllers/notifications_controller_spec.rb @@ -4,13 +4,18 @@ describe NotificationsController do describe "#index" do 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 - - get :index, debate: { title: 'A sample debate', description: 'this is a sample debate', terms_of_service: 1 } - expect(assigns(:notifications)).to eq user.notifications.unread.recent.for_render + get :index + Notification.all.each do |notification| + expect(notification.read).to be true + end end end end diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index ee5f00e6d..8d5e3dd97 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -41,4 +41,14 @@ describe Notification do expect(notification.timestamp).to eq comment.created_at 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