diff --git a/app/models/mention.rb b/app/models/mention.rb index 0d27dc4a2..7c9deb3be 100644 --- a/app/models/mention.rb +++ b/app/models/mention.rb @@ -10,6 +10,8 @@ class Mention < ActiveRecord::Base after_create :notify_recipient + after_destroy :delete_notification + def notify_recipient Notification.notify(person.owner, self, post.person) unless person.remote? end @@ -18,4 +20,8 @@ class Mention < ActiveRecord::Base def notification_type(*args) 'mentioned' end + + def delete_notification + Notification.where(:target_type => self.class, :target_id => self.id).delete_all + end end diff --git a/app/models/notification.rb b/app/models/notification.rb index fdad5a759..473005b85 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -48,11 +48,13 @@ class Notification < ActiveRecord::Base private def self.concatenate_or_create(recipient, target, actor, action) if n = Notification.where(:target_id => target.id, + :target_type => target.type, :action => action, :recipient_id => recipient.id).first unless n.actors.include?(actor) n.actors << actor end + n.unread = true n.save! n @@ -62,7 +64,7 @@ private end def self.make_notification(recipient, target, actor, action) - n = Notification.new(:target_id => target.id, + n = Notification.new(:target => target, :action => action, :recipient_id => recipient.id) n.actors << actor diff --git a/db/schema.rb b/db/schema.rb index a1012b771..e3d444120 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110209204702) do +ActiveRecord::Schema.define(:version => 20110211021925) do create_table "aspect_memberships", :force => true do |t| t.integer "aspect_id", :null => false diff --git a/spec/models/mention_spec.rb b/spec/models/mention_spec.rb index e691b5a26..7f5b27644 100644 --- a/spec/models/mention_spec.rb +++ b/spec/models/mention_spec.rb @@ -29,5 +29,20 @@ describe Mention do Mention.new.notification_type.should == 'mentioned' end end + + describe 'after destroy' do + it 'destroys a notification' do + @user = alice + @sm = Factory(:status_message) + @m = Mention.create(:person => @user.person, :post=> @sm) + + + pp Notification.first + + lambda{ + @m.destroy + }.should change(Notification, :count).by(-1) + end + end end