diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index d6453c78e6db9a98560610ef8101725c4a65833d..729c7b882f5c106859c5b1e6490353c3ed33c959 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 8c9107c60dec9d961e20961c4fa6b6a042a57f44..4dcb50fc8ad350d527cb1d4d853f35a8735b68a0 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