diff --git a/app/controllers/aspects_controller.rb b/app/controllers/aspects_controller.rb index 68c72f8061ca905fcec46a665dcec9eb61ab846f..081c4d48481e41066503bb5ac0b8a94c2da46a18 100644 --- a/app/controllers/aspects_controller.rb +++ b/app/controllers/aspects_controller.rb @@ -24,8 +24,9 @@ class AspectsController < ApplicationController @aspect_ids = @aspects.map{|a| a.id} @posts = StatusMessage.joins(:aspects).where(:pending => false, - :aspects => {:id => @aspect_ids}).includes({:person => :profile}, {:comments => {:person => :profile}}, :photos).select('DISTINCT `posts`.*').paginate( + :aspects => {:id => @aspect_ids}).includes(:comments, :photos).select('DISTINCT `posts`.*').paginate( :page => params[:page], :per_page => 15, :order => 'created_at DESC') + @fakes = PostsFake.new(@posts) @contacts = current_user.contacts.includes(:person => :profile).where(:pending => false) @@ -34,7 +35,6 @@ class AspectsController < ApplicationController end end - def create @aspect = current_user.aspects.create(params[:aspect]) if @aspect.valid? diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index e9640cac1d78751414134b7b40288221db053ca6..4f85ac35b30666e6811b4231affea492d4343c3c 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -62,6 +62,7 @@ class PeopleController < ApplicationController end @posts = current_user.posts_from(@person).where(:type => "StatusMessage").paginate :page => params[:page] + @fakes = PostsFake.new(@posts) respond_with @person, :locals => {:post_type => :all} diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 66ba0e26b2890e00f40c2f0f353687498ba7b59b..22a8308dc24ff26cdb50a9f0adfbc0b2e19500a4 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -26,13 +26,13 @@ module ApplicationHelper def aspects_with_post aspects, post aspects.select do |aspect| - aspect.posts.include?(post) + aspect.post_ids.include?(post.id) end end def aspects_without_post aspects, post aspects.reject do |aspect| - aspect.posts.include?(post) + aspect.post_ids.include?(post.id) end end diff --git a/app/views/aspects/_aspect_stream.haml b/app/views/aspects/_aspect_stream.haml index e1d313535cc56a26bac105318a40810679a27814..deb9cefee572c2a53775120813cc1ee285565438 100644 --- a/app/views/aspects/_aspect_stream.haml +++ b/app/views/aspects/_aspect_stream.haml @@ -5,5 +5,5 @@ = render 'shared/publisher', :aspect => aspect, :aspect_ids => aspect_ids #main_stream.stream{:data => {:guids => aspect_ids.join(',')}} - = render 'shared/stream', :posts => posts + = render 'shared/stream', :posts => fakes = will_paginate posts diff --git a/app/views/aspects/index.html.haml b/app/views/aspects/index.html.haml index a839b549bb5e18101ff1dc43d8c05aea975ad506..f6a4cff352ae84d13a15d71b4d3e090a1c95bddd 100644 --- a/app/views/aspects/index.html.haml +++ b/app/views/aspects/index.html.haml @@ -13,7 +13,8 @@ = render 'aspect_stream', :aspect => @aspect, :aspect_ids => @aspect_ids, - :posts => @posts + :posts => @posts, + :fakes => @fakes .span-7.last #home_user_badge diff --git a/app/views/aspects/index.js.erb b/app/views/aspects/index.js.erb index 16ec8f6c5a57c86ca85c9b63baf6f594507fb584..310bd0f8b866e8cd8f698db2fa4621697199a87d 100644 --- a/app/views/aspects/index.js.erb +++ b/app/views/aspects/index.js.erb @@ -1,4 +1,4 @@ -$('#aspect_stream_container').html("<%= escape_javascript(render('aspects/aspect_stream', :aspect => @aspect, :aspect_ids => @aspect_ids, :posts => @posts)) %>"); +$('#aspect_stream_container').html("<%= escape_javascript(render('aspects/aspect_stream', :aspect => @aspect, :aspect_ids => @aspect_ids, :posts => @posts, :fakes => @fakes)) %>"); $('#aspect_listings').html("<%= escape_javascript(render('aspects/aspect_listings', :aspects => @aspects, :contacts => @contacts)) %>"); $('a[rel*=facebox]').facebox(); diff --git a/app/views/people/show.html.haml b/app/views/people/show.html.haml index 893d4b54cf028126a6abce1971f496303d53d56b..677dfa7172f3b44f4442770679d828ccc52de4cd 100644 --- a/app/views/people/show.html.haml +++ b/app/views/people/show.html.haml @@ -55,7 +55,7 @@ = render 'photos/index', :photos => @posts - else #main_stream.stream - = render 'shared/stream', :posts => @posts + = render 'shared/stream', :posts => @fakes = will_paginate @posts diff --git a/config/application.rb b/config/application.rb index 9b73613e45e5d86b676d6096c8983e9cc833f9c2..fc51514cf10e48781ff15406e90515a3b002e538 100644 --- a/config/application.rb +++ b/config/application.rb @@ -10,6 +10,8 @@ require 'rails/all' Bundler.require(:default, Rails.env) if defined?(Bundler) require File.expand_path('../../lib/log_overrider', __FILE__) +require File.expand_path('../../lib/fake', __FILE__) + module Diaspora class Application < Rails::Application # Settings in config/environments/* take precedence over those specified here. diff --git a/lib/diaspora/user/querying.rb b/lib/diaspora/user/querying.rb index d5f132295c097ffe23fb9768be5457795fb789fe..7b2fe0a617d66a7c2a5930ccd215bffd6bd402e7 100644 --- a/lib/diaspora/user/querying.rb +++ b/lib/diaspora/user/querying.rb @@ -78,7 +78,7 @@ module Diaspora def posts_from(person) asp = Aspect.arel_table p = Post.arel_table - person.posts.includes(:aspects, :comments => {:person => :profile}).where( p[:public].eq(true).or(asp[:user_id].eq(self.id))).select('DISTINCT `posts`.*').order("posts.updated_at DESC") + person.posts.includes(:aspects, :comments).where( p[:public].eq(true).or(asp[:user_id].eq(self.id))).select('DISTINCT `posts`.*').order("posts.updated_at DESC") end end end diff --git a/lib/fake.rb b/lib/fake.rb new file mode 100644 index 0000000000000000000000000000000000000000..b462a44d2cd25e3af3c5c53c7503d4a6cadb3265 --- /dev/null +++ b/lib/fake.rb @@ -0,0 +1,48 @@ +class PostsFake + attr_reader :people_hash, :post_fakes + + def method_missing(method, *args, &block) + @post_fakes.send(method, *args, &block) + end + + def initialize(posts) + person_ids = [] + posts.each do |p| + person_ids << p.person_id + p.comments.each do |c| + person_ids << c.person_id + end + end + + people = Person.where(:id => person_ids).includes(:profile) + @people_hash = {} + people.each{|person| @people_hash[person.id] = person} + + @post_fakes = posts.map do |post| + f = Fake.new(post, self) + f.comments = post.comments.map do |comment| + Fake.new(comment, self) + end + f + end + end + + class Fake + attr_accessor :comments + def initialize(model, fakes_collection) + @fakes_collection = fakes_collection + @model = model + end + + def id + @model.id + end + def person + @fakes_collection.people_hash[@model.person_id] + end + + def method_missing(method, *args) + @model.send(method, *args) + end + end +end diff --git a/spec/controllers/aspects_controller_spec.rb b/spec/controllers/aspects_controller_spec.rb index d640bac07c9046ec2e6ebbd8eb4adb34a0ca821e..c84371e5d9c83bf1dbe9141348b61f2e357ffd10 100644 --- a/spec/controllers/aspects_controller_spec.rb +++ b/spec/controllers/aspects_controller_spec.rb @@ -303,4 +303,9 @@ describe AspectsController do @aspect0.contacts.include?(@contact).should be false end end + + describe "#hashes_for_posts" do + it 'returns only distinct people' do + end + end end diff --git a/spec/factories.rb b/spec/factories.rb index dca92bf5a59039f9517e3c17cd567040bfcb2591..f541ec34dd8dec6696a18cfdd9bb71844efaafc4 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -78,5 +78,6 @@ end Factory.define(:comment) do |comment| comment.sequence(:text) {|n| "#{n} cats"} + comment.association(:person) end diff --git a/spec/lib/fake_spec.rb b/spec/lib/fake_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..fc35ef8d7d236652dd5dcf9c89b9a6c23ad846c7 --- /dev/null +++ b/spec/lib/fake_spec.rb @@ -0,0 +1,53 @@ +require 'spec_helper' +describe PostsFake do + before do + @posts = [] + @people = [] + 4.times do + post = Factory(:status_message) + @people << post.person + 4.times do + comment = Factory(:comment, :post => post) + @people << comment.person + end + @posts << post + end + end + + describe '#initialize' do + before do + @posts_fake = PostsFake.new(@posts) + end + it 'sets @people_hash' do + @people.each do |person| + @posts_fake.people_hash[person.reload.id].should == person + end + @posts_fake.people_hash.length.should == @people.length + end + + it 'sets @post_fakes to an array of fakes' do + @posts_fake.post_fakes.each{|x| x.class.should be PostsFake::Fake} + end + end + describe PostsFake::Fake do + before do + @post = mock() + @fakes = mock() + @fake = PostsFake::Fake.new(@post, @fakes) + end + it 'refers to the parent collection for a person' do + @post.should_receive(:person_id) + @fakes.should_receive(:people_hash).and_return({}) + @fake.person + end + it 'refers to its comments array for comments' do + @fake.comments = [mock()] + @fake.comments + end + it 'refers to its post for any other field' do + @post.should_receive(:text) + @fake.text + end + end +end +