From 36fd43db4c04f574cfd03ef1e5612862832e16ef Mon Sep 17 00:00:00 2001 From: Steven Fuchs Date: Tue, 17 Jan 2012 19:27:48 -0500 Subject: [PATCH] incorporated improvements from @maxwell from last pull request. --- app/controllers/notifications_controller.rb | 4 ++-- app/models/notification.rb | 3 +++ .../notifications_controller_spec.rb | 20 ++++++++++++++++--- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index c27df96c7..2e8109db6 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -11,8 +11,8 @@ class NotificationsController < VannaController def update(opts=params) note = Notification.where(:recipient_id => current_user.id, :id => opts[:id]).first if note - note.update_attributes(:unread => false) - {} + note.set_read_state(opts[:set_unread] != "true" ) + { :guid => note.id, :unread => note.unread } else Response.new :status => 404 end diff --git a/app/models/notification.rb b/app/models/notification.rb index 547899379..0e8dfe013 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -36,6 +36,9 @@ class Notification < ActiveRecord::Base self.recipient.mail(self.mail_job, self.recipient_id, actor.id, target.id) end + def set_read_state( read_state ) + self.update_attributes( :unread => !read_state ) + end def mail_job raise NotImplementedError.new('Subclass this.') diff --git a/spec/controllers/notifications_controller_spec.rb b/spec/controllers/notifications_controller_spec.rb index 3f1e64b1f..ffa54f8a8 100644 --- a/spec/controllers/notifications_controller_spec.rb +++ b/spec/controllers/notifications_controller_spec.rb @@ -14,10 +14,24 @@ describe NotificationsController do end describe '#update' do - it 'marks a notification as read' do - note = Factory(:notification, :recipient => @user) + it 'marks a notification as read if it gets no other information' do + note = mock_model( Notification ) + Notification.should_receive( :where ).and_return( [note] ) + note.should_receive( :set_read_state ).with( true ) @controller.update :id => note.id - Notification.first.unread.should == false + end + it 'marks a notification as read if it is told to' do + note = mock_model( Notification ) + Notification.should_receive( :where ).and_return( [note] ) + note.should_receive( :set_read_state ).with( true ) + @controller.update :id => note.id, :set_unread => "false" + end + + it 'marks a notification as unread if it is told to' do + note = mock_model( Notification ) + Notification.should_receive( :where ).and_return( [note] ) + note.should_receive( :set_read_state ).with( false ) + @controller.update :id => note.id, :set_unread => "true" end it 'only lets you read your own notifications' do