correctly handle empty case

This commit is contained in:
Sam 2014-02-13 17:27:35 +11:00
parent e8aa85d783
commit bf957513ec

View File

@ -91,7 +91,7 @@ class Notification < ActiveRecord::Base
def self.recent_report(user, count = nil) def self.recent_report(user, count = nil)
notifications = user.notifications.recent(count).includes(:topic) notifications = user.notifications.recent(count).includes(:topic).to_a
if notifications.present? if notifications.present?
notifications += user.notifications notifications += user.notifications
@ -99,17 +99,19 @@ class Notification < ActiveRecord::Base
.where(read: false, notification_type: Notification.types[:private_message]) .where(read: false, notification_type: Notification.types[:private_message])
.where('id < ?', notifications.last.id) .where('id < ?', notifications.last.id)
.limit(count) .limit(count)
end
notifications.sort do |x,y| notifications.sort do |x,y|
if x.unread_pm? && !y.unread_pm? if x.unread_pm? && !y.unread_pm?
-1 -1
elsif y.unread_pm? && !x.unread_pm? elsif y.unread_pm? && !x.unread_pm?
1 1
else else
y.created_at <=> x.created_at y.created_at <=> x.created_at
end end
end.take(count) end.take(count)
else
[]
end
end end