diff --git a/app/models/like.rb b/app/models/like.rb index a7363b60d..baf4b0ff0 100644 --- a/app/models/like.rb +++ b/app/models/like.rb @@ -19,6 +19,7 @@ class Like < ActiveRecord::Base belongs_to :author, :class_name => 'Person' validates_uniqueness_of :post_id, :scope => :author_id + validates_presence_of :author, :post def diaspora_handle self.author.diaspora_handle diff --git a/app/models/notification.rb b/app/models/notification.rb index 8349165b2..c6466005a 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -18,7 +18,7 @@ class Notification < ActiveRecord::Base def self.notify(recipient, target, actor) if target.respond_to? :notification_type 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) else n = note_type.make_notification(recipient, target, actor, note_type) @@ -33,6 +33,8 @@ class Notification < ActiveRecord::Base def email_the_user(target, actor) self.recipient.mail(self.mail_job, self.recipient_id, actor.id, target.id) end + + def mail_job raise NotImplementedError.new('Subclass this.') end @@ -43,9 +45,7 @@ private :target_type => target.class.base_class, :recipient_id => recipient.id, :unread => true).first - unless n.actors.include?(actor) - n.actors << actor - end + n.actors = n.actors | [actor] n.unread = true n.save! @@ -58,7 +58,7 @@ private def self.make_notification(recipient, target, actor, notification_type) n = notification_type.new(:target => target, :recipient_id => recipient.id) - n.actors << actor + n.actors = n.actors | [actor] n.unread = false if target.is_a? Request n.save! n diff --git a/spec/factories.rb b/spec/factories.rb index 72b8bd7b9..7b6173d09 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -35,6 +35,11 @@ Factory.define :searchable_person, :parent => :person do |p| end end +Factory.define :like do |x| + x.association :author, :factory => :person + x.association :post, :factory => :status_message +end + Factory.define :user do |u| u.sequence(:username) { |n| "bob#{n}#{r_str}" } u.sequence(:email) { |n| "bob#{n}#{r_str}@pivotallabs.com" } diff --git a/spec/helpers/notifications_helper_spec.rb b/spec/helpers/notifications_helper_spec.rb new file mode 100644 index 000000000..70bfc2e58 --- /dev/null +++ b/spec/helpers/notifications_helper_spec.rb @@ -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 diff --git a/spec/models/like_spec.rb b/spec/models/like_spec.rb index 8bdc4ca25..2bb4728f1 100644 --- a/spec/models/like_spec.rb +++ b/spec/models/like_spec.rb @@ -15,6 +15,10 @@ describe Like do @status = bob.post(:status_message, :text => "hello", :to => @alices_aspect.id) end + it 'has a valid factory' do + Factory(:like).should be_valid + end + describe 'User#like' do it "should be able to like on one's own status" do alice.like(1, :on => @status) diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index fe50ec026..f97b2b2b0 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -92,6 +92,28 @@ describe Notification do 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 before do