Take another shot at fixing pagination

This commit is contained in:
Raphael Sofaer 2011-04-05 13:59:02 -07:00
parent 4037ce48c5
commit 746a0c38b3
4 changed files with 12 additions and 10 deletions

View file

@ -23,11 +23,15 @@ class AspectsController < ApplicationController
redirect_to getting_started_path redirect_to getting_started_path
return return
end end
params[:page] = params[:page] ? params[:page].to_i : 1
@selected_contacts = @aspects.map { |aspect| aspect.contacts }.flatten.uniq @selected_contacts = @aspects.map { |aspect| aspect.contacts }.flatten.uniq
@aspect_ids = @aspects.map { |a| a.id } @aspect_ids = @aspects.map { |a| a.id }
@posts = current_user.raw_visible_posts(:by_members_of => @aspect_ids, :type => 'StatusMessage', :order => session[:sort_order] + ' DESC', :page => params[:page]).includes( @posts = current_user.raw_visible_posts(:by_members_of => @aspect_ids, :type => 'StatusMessage', :order => session[:sort_order] + ' DESC', :page => params[:page]).includes(
:comments, :mentions, :likes, :dislikes).paginate(:page => params[:page], :per_page => 15, :order => "posts.#{session[:sort_order]} DESC") :comments, :mentions, :likes, :dislikes)
@fakes = PostsFake.new(@posts) @fakes = PostsFake.new(@posts)
@contact_count = current_user.contacts.count @contact_count = current_user.contacts.count

View file

@ -12,8 +12,6 @@
#main_stream.stream{:data => {:guids => aspect_ids.join(',')}} #main_stream.stream{:data => {:guids => aspect_ids.join(',')}}
- if posts.length > 0 - if posts.length > 0
= render 'shared/stream', :posts => fakes = render 'shared/stream', :posts => fakes
%a.paginate =link_to(t('more'), aspects_path(:a_ids => params[:a_ids], :page => params[:page] + 1), :class => 'paginate')
= t("more")
- else - else
= render 'aspects/no_posts_message', :post_count => posts.length = render 'aspects/no_posts_message', :post_count => posts.length
= will_paginate posts

View file

@ -15,11 +15,11 @@ module Diaspora
def raw_visible_posts(opts = {}) def raw_visible_posts(opts = {})
opts = opts.dup opts = opts.dup
opts[:type] ||= ['StatusMessage', 'Photo'] opts[:type] ||= ['StatusMessage', 'Photo']
opts[:limit] ||= 20 opts[:limit] ||= 15
opts[:order] ||= 'updated_at DESC' opts[:order] ||= 'updated_at DESC'
opts[:hidden] ||= false opts[:hidden] ||= false
order_with_table = 'posts.' + opts[:order] order_with_table = 'posts.' + opts[:order]
opts[:offset] = opts[:page] ? opts[:limit] * opts[:page] - 1 : 0 opts[:offset] = opts[:page].nil? || opts[:page] == 1 ? 0 : opts[:limit] * (opts[:page] - 1)
select_clause ='posts.id, posts.updated_at AS updated_at, posts.created_at AS created_at' select_clause ='posts.id, posts.updated_at AS updated_at, posts.created_at AS created_at'
posts_from_others = Post.joins(:contacts).where( :post_visibilities => {:hidden => opts[:hidden]}, :contacts => {:user_id => self.id}) posts_from_others = Post.joins(:contacts).where( :post_visibilities => {:hidden => opts[:hidden]}, :contacts => {:user_id => self.id})
@ -37,7 +37,7 @@ module Diaspora
all_posts = "(#{posts_from_others.to_sql}) UNION (#{posts_from_self.to_sql}) ORDER BY #{opts[:order]} LIMIT #{opts[:limit]}" all_posts = "(#{posts_from_others.to_sql}) UNION (#{posts_from_self.to_sql}) ORDER BY #{opts[:order]} LIMIT #{opts[:limit]}"
post_ids = Post.connection.execute(all_posts).map{|r| r.first} post_ids = Post.connection.execute(all_posts).map{|r| r.first}
Post.where(:id => post_ids, :pending => false, :type => opts[:type]).select('DISTINCT posts.*').limit(opts[:limit]).order(order_with_table).offset(opts[:offset]) Post.where(:id => post_ids, :pending => false, :type => opts[:type]).select('DISTINCT posts.*').limit(opts[:limit]).order(order_with_table)
end end
def visible_photos def visible_photos

View file

@ -2,9 +2,9 @@ $(document).ready(function() {
var scrolled = 0; var scrolled = 0;
$('#main_stream').infinitescroll({ $('#main_stream').infinitescroll({
navSelector : ".pagination", navSelector : ".paginate",
// selector for the paged navigation (it will be hidden) // selector for the paged navigation (it will be hidden)
nextSelector : ".pagination a.next_page", nextSelector : ".paginate",
// selector for the NEXT link (to page 2) // selector for the NEXT link (to page 2)
itemSelector : "#main_stream .stream_element", itemSelector : "#main_stream .stream_element",
// selector for all items you'll retrieve // selector for all items you'll retrieve