MSSM like notification are concatinated, and backfilled some like tests

This commit is contained in:
Maxwell Salzberg 2011-05-22 13:59:33 -07:00
parent a68031179b
commit d4d3b1e44c
6 changed files with 58 additions and 5 deletions

View file

@ -19,6 +19,7 @@ class Like < ActiveRecord::Base
belongs_to :author, :class_name => 'Person' belongs_to :author, :class_name => 'Person'
validates_uniqueness_of :post_id, :scope => :author_id validates_uniqueness_of :post_id, :scope => :author_id
validates_presence_of :author, :post
def diaspora_handle def diaspora_handle
self.author.diaspora_handle self.author.diaspora_handle

View file

@ -18,7 +18,7 @@ class Notification < ActiveRecord::Base
def self.notify(recipient, target, actor) def self.notify(recipient, target, actor)
if target.respond_to? :notification_type if target.respond_to? :notification_type
if note_type = target.notification_type(recipient, actor) if note_type = target.notification_type(recipient, actor)
if target.is_a? Comment if(target.is_a? Comment) || (target.is_a? Like)
n = note_type.concatenate_or_create(recipient, target.post, actor, note_type) n = note_type.concatenate_or_create(recipient, target.post, actor, note_type)
else else
n = note_type.make_notification(recipient, target, actor, note_type) n = note_type.make_notification(recipient, target, actor, note_type)
@ -33,6 +33,8 @@ class Notification < ActiveRecord::Base
def email_the_user(target, actor) def email_the_user(target, actor)
self.recipient.mail(self.mail_job, self.recipient_id, actor.id, target.id) self.recipient.mail(self.mail_job, self.recipient_id, actor.id, target.id)
end end
def mail_job def mail_job
raise NotImplementedError.new('Subclass this.') raise NotImplementedError.new('Subclass this.')
end end
@ -43,9 +45,7 @@ private
:target_type => target.class.base_class, :target_type => target.class.base_class,
:recipient_id => recipient.id, :recipient_id => recipient.id,
:unread => true).first :unread => true).first
unless n.actors.include?(actor) n.actors = n.actors | [actor]
n.actors << actor
end
n.unread = true n.unread = true
n.save! n.save!
@ -58,7 +58,7 @@ private
def self.make_notification(recipient, target, actor, notification_type) def self.make_notification(recipient, target, actor, notification_type)
n = notification_type.new(:target => target, n = notification_type.new(:target => target,
:recipient_id => recipient.id) :recipient_id => recipient.id)
n.actors << actor n.actors = n.actors | [actor]
n.unread = false if target.is_a? Request n.unread = false if target.is_a? Request
n.save! n.save!
n n

View file

@ -35,6 +35,11 @@ Factory.define :searchable_person, :parent => :person do |p|
end end
end end
Factory.define :like do |x|
x.association :author, :factory => :person
x.association :post, :factory => :status_message
end
Factory.define :user do |u| Factory.define :user do |u|
u.sequence(:username) { |n| "bob#{n}#{r_str}" } u.sequence(:username) { |n| "bob#{n}#{r_str}" }
u.sequence(:email) { |n| "bob#{n}#{r_str}@pivotallabs.com" } u.sequence(:email) { |n| "bob#{n}#{r_str}@pivotallabs.com" }

View file

@ -0,0 +1,21 @@
require 'spec_helper'
describe NotificationsHelper do
describe '#notification_people_link' do
describe 'for a like' do
it 'displays #{list of actors}' do
@user = Factory(:user)
@person = Factory(:person)
p = Factory(:status_message, :author => @user.person)
person2 = Factory(:person)
notification = Notification.notify(@user, Factory(:like, :author => @person, :post => p), @person)
notification2 = Notification.notify(@user, Factory(:like, :author => person2, :post => p), person2)
output = notification_people_link(notification2)
output.should include person2.name
output.should include @person.name
end
end
end
end

View file

@ -15,6 +15,10 @@ describe Like do
@status = bob.post(:status_message, :text => "hello", :to => @alices_aspect.id) @status = bob.post(:status_message, :text => "hello", :to => @alices_aspect.id)
end end
it 'has a valid factory' do
Factory(:like).should be_valid
end
describe 'User#like' do describe 'User#like' do
it "should be able to like on one's own status" do it "should be able to like on one's own status" do
alice.like(1, :on => @status) alice.like(1, :on => @status)

View file

@ -92,6 +92,28 @@ describe Notification do
end end
end end
context 'multiple likes' do
it 'concatinates the like notifications' do
p = Factory(:status_message, :author => @user.person)
person2 = Factory(:person)
notification = Notification.notify(@user, Factory(:like, :author => @person, :post => p), @person)
notification2 = Notification.notify(@user, Factory(:like, :author => person2, :post => p), person2)
notification.id.should == notification2.id
end
end
context 'multiple comments' do
it 'concatinates the comment notifications' do
p = Factory(:status_message, :author => @user.person)
person2 = Factory(:person)
notification = Notification.notify(@user, Factory(:comment, :author => @person, :post => p), @person)
notification2 = Notification.notify(@user, Factory(:comment, :author => person2, :post => p), person2)
notification.id.should == notification2.id
end
end
context 'multiple people' do context 'multiple people' do
before do before do