From 21463582d4126af1a1c68bf39eaf3eec32b165ce Mon Sep 17 00:00:00 2001
From: Raphael Sofaer <raphael@joindiaspora.com>
Date: Sat, 4 Jun 2011 15:48:10 -0700
Subject: [PATCH] Fix pagination on notifications page, it was getting all
 notifications for a user

---
 app/controllers/notifications_controller.rb      | 16 ++++++++++++++--
 .../controllers/notifications_controller_spec.rb | 11 +++++++++--
 2 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb
index d6453c78e6..729c7b882f 100644
--- a/app/controllers/notifications_controller.rb
+++ b/app/controllers/notifications_controller.rb
@@ -19,8 +19,20 @@ class NotificationsController < ApplicationController
 
   def index
     @aspect = :notification
-    @notifications = Notification.find(:all, :conditions => {:recipient_id => current_user.id},
-                                       :order => 'created_at desc', :include => [:target, {:actors => :profile}]).paginate :page => params[:page], :per_page => 25
+    conditions = {:recipient_id => current_user.id}
+    page = params[:page] || 1
+    @notifications = WillPaginate::Collection.create(page, 25, Notification.where(conditions).count ) do |pager|
+      result = Notification.find(:all,
+                                 :conditions => conditions,
+                                 :order => 'created_at desc',
+                                 :include => [:target, {:actors => :profile}],
+                                 :limit => pager.per_page,
+                                 :offset => pager.offset
+                                )
+
+      pager.replace(result)
+    end
+
     @group_days = @notifications.group_by{|note| I18n.l(note.created_at, :format => I18n.t('date.formats.fullmonth_day')) }
     respond_with @notifications
   end
diff --git a/spec/controllers/notifications_controller_spec.rb b/spec/controllers/notifications_controller_spec.rb
index 8c9107c60d..4dcb50fc8a 100644
--- a/spec/controllers/notifications_controller_spec.rb
+++ b/spec/controllers/notifications_controller_spec.rb
@@ -43,16 +43,23 @@ describe NotificationsController do
   end
 
   describe '#index' do
-    it 'paginates the notifications' do
+    before do
       26.times do
         Factory(:notification, :recipient => @user)
       end
+    end
 
+    it 'paginates the notifications' do
       get :index
       assigns[:notifications].count.should == 25
 
       get :index, :page => 2
       assigns[:notifications].count.should == 1
     end
+
+    it 'eager loads the target' do
+      get :index
+      assigns[:notifications].each{ |note| note.loaded_target?.should be_true }
+    end
   end
-end
\ No newline at end of file
+end
-- 
GitLab