add ordering to raw_visible_posts

This commit is contained in:
danielgrippi 2011-03-29 16:30:00 -07:00
parent 6df652a513
commit 371aa71ca8
2 changed files with 4 additions and 3 deletions

View file

@ -26,7 +26,7 @@ class AspectsController < ApplicationController
@selected_contacts = @aspects.map { |aspect| aspect.contacts }.flatten.uniq
@aspect_ids = @aspects.map { |a| a.id }
@posts = current_user.raw_visible_posts(:by_members_of => @aspect_ids, :type => 'StatusMessage').includes(
@posts = current_user.raw_visible_posts(:by_members_of => @aspect_ids, :type => 'StatusMessage', :order => session[:sort_order] + ' DESC').includes(
:comments, :likes, :dislikes).paginate(:page => params[:page], :per_page => 15, :order => session[:sort_order] + ' DESC')
@fakes = PostsFake.new(@posts)

View file

@ -13,6 +13,7 @@ module Diaspora
def raw_visible_posts(opts = {})
opts[:type] ||= ['StatusMessage', 'Photo']
opts[:limit] ||= 20
opts[:order] ||= 'created_at DESC'
posts_from_others = Post.joins(:contacts).where(:contacts => {:user_id => self.id})
posts_from_self = self.person.posts.joins(:aspect_visibilities => :aspect).where(:aspects => {:user_id => self.id})
@ -23,8 +24,8 @@ module Diaspora
posts_from_self = posts_from_self.where(:aspects => {:id => opts[:by_members_of]})
end
post_ids = posts_from_others.select('posts.id').limit(opts[:limit]).map{|p| p.id}
post_ids += posts_from_self.select('posts.id').limit(opts[:limit]).map{|p| p.id}
post_ids = posts_from_others.select('posts.id').limit(opts[:limit]).order(opts[:order]).map{|p| p.id}
post_ids += posts_from_self.select('posts.id').limit(opts[:limit]).order(opts[:order]).map{|p| p.id}
Post.where(:id => post_ids, :pending => false, :type => opts[:type]).select('DISTINCT `posts`.*').limit(opts[:limit])
end