From 5967f01dab9970f2ce5b13d1e65f6b5cfff3d85e Mon Sep 17 00:00:00 2001
From: Raphael Sofaer <raphael@joindiaspora.com>
Date: Wed, 3 Aug 2011 10:52:41 -0700
Subject: [PATCH] Take out PostsFake, now that we ajax in comments it's not
 worth the extra complications

---
 app/controllers/aspects_controller.rb       |  5 +-
 app/controllers/people_controller.rb        |  2 +-
 app/controllers/tags_controller.rb          |  3 +-
 app/helpers/likes_helper.rb                 |  3 --
 config/application.rb                       |  2 -
 lib/fake.rb                                 | 52 ------------------
 spec/controllers/aspects_controller_spec.rb | 28 +++++-----
 spec/controllers/people_controller_spec.rb  | 16 +++---
 spec/controllers/tags_controller_spec.rb    | 12 ++---
 spec/lib/fake_spec.rb                       | 58 ---------------------
 10 files changed, 32 insertions(+), 149 deletions(-)
 delete mode 100644 lib/fake.rb
 delete mode 100644 spec/lib/fake_spec.rb

diff --git a/app/controllers/aspects_controller.rb b/app/controllers/aspects_controller.rb
index a13b47fcbc..9764a0e73e 100644
--- a/app/controllers/aspects_controller.rb
+++ b/app/controllers/aspects_controller.rb
@@ -42,13 +42,12 @@ class AspectsController < ApplicationController
     end
 
     @aspect_ids = @aspects.map { |a| a.id }
-    posts = current_user.visible_posts(:by_members_of => @aspect_ids,
+    @posts = current_user.visible_posts(:by_members_of => @aspect_ids,
                                            :type => ['StatusMessage','Reshare', 'ActivityStreams::Photo'],
                                            :order => session[:sort_order] + ' DESC',
                                            :max_time => params[:max_time].to_i
-                          ).includes(:mentions => {:person => :profile})
+                          ).includes(:mentions => {:person => :profile}, :author => :profile)
 
-    @posts = PostsFake.new(posts)
     if params[:only_posts]
       render :partial => 'shared/stream', :locals => {:posts => @posts}
     else
diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb
index 389729ccac..a627d2498e 100644
--- a/app/controllers/people_controller.rb
+++ b/app/controllers/people_controller.rb
@@ -101,7 +101,7 @@ class PeopleController < ApplicationController
           @commenting_disabled = true
           @posts = @person.posts.where(:type => ["StatusMessage", "Reshare", "ActivityStreams::Photo"], :public => true).includes(:comments).limit(15).where(StatusMessage.arel_table[:created_at].lt(max_time)).order('posts.created_at DESC')
         end
-        @posts = PostsFake.new(@posts)
+        @posts.includes(:author => :profile)
       end
 
       if params[:only_posts]
diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb
index 0088c5ba9e..ce590a6ad9 100644
--- a/app/controllers/tags_controller.rb
+++ b/app/controllers/tags_controller.rb
@@ -60,9 +60,8 @@ class TagsController < ApplicationController
 
     max_time = params[:max_time] ? Time.at(params[:max_time].to_i) : Time.now
     @posts = @posts.where(StatusMessage.arel_table[:created_at].lt(max_time))
-    @posts = @posts.includes(:comments, :photos).order('posts.created_at DESC').limit(15)
+    @posts = @posts.includes({:author => :profile}, :comments, :photos).order('posts.created_at DESC').limit(15)
 
-    @posts = PostsFake.new(@posts)
     @commenting_disabled = true
 
     if params[:only_posts]
diff --git a/app/helpers/likes_helper.rb b/app/helpers/likes_helper.rb
index 20614e1c26..b478dc51cf 100644
--- a/app/helpers/likes_helper.rb
+++ b/app/helpers/likes_helper.rb
@@ -9,9 +9,6 @@ module LikesHelper
   end
 
   def like_action(target, current_user=current_user)
-
-    target = target.model if target.instance_of?(PostsFake::Fake)
-
     if target.instance_of?(Comment)
       if current_user.liked?(target)
         link_to t('shared.stream_element.unlike'), comment_like_path(target, current_user.like_for(target)), :method => :delete, :class => 'unlike', :remote => true
diff --git a/config/application.rb b/config/application.rb
index e3c13ad267..d5cf713344 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -20,8 +20,6 @@ Bundler.require(:default, Rails.env) if defined?(Bundler)
 # use newrelic if configured via config/newrelic.yml
 require 'newrelic_rpm' if File.exists?(File.expand_path('../newrelic.yml', __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/fake.rb b/lib/fake.rb
deleted file mode 100644
index 3466e018d9..0000000000
--- a/lib/fake.rb
+++ /dev/null
@@ -1,52 +0,0 @@
-class PostsFake
-  attr_reader :people_hash, :post_fakes
-  delegate :length, :each, :to_ary, :last, :to => :post_fakes
-
-  def initialize(posts)
-    author_ids = []
-    posts.each do |p|
-      author_ids << p.author_id
-    end
-
-    people = Person.where(:id => author_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
-    end
-  end
-
-  def models
-    self.post_fakes.map{|a| a.model }
-  end
-
-  class Fake
-    attr_reader :model
-    def initialize(model, fakes_collection)
-      @fakes_collection = fakes_collection
-      @model = model
-    end
-
-    def id
-      @model.id
-    end
-
-    def to_s
-      @model.id.to_s
-    end
-
-    def author
-      @fakes_collection.people_hash[@model.author_id]
-    end
-
-    def respond_to?(*args)
-      super(*args) || model.respond_to?(*args)
-    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 f0b5f3d799..c9d9ba89ea 100644
--- a/spec/controllers/aspects_controller_spec.rb
+++ b/spec/controllers/aspects_controller_spec.rb
@@ -127,19 +127,19 @@ describe AspectsController do
 
         it "pulls back non hidden posts" do
           get :index
-          assigns[:posts].models.include?(@status).should be_true
+          assigns[:posts].include?(@status).should be_true
         end
         it "does not pull back hidden posts" do
           @vis.update_attributes( :hidden => true )
           get :index
-          assigns[:posts].models.include?(@status).should be_false
+          assigns[:posts].include?(@status).should be_false
         end
       end
 
       describe 'infinite scroll' do
         it 'renders with the infinite scroll param' do
           get :index, :only_posts => true
-          assigns[:posts].models.include?(@posts.first).should be_true
+          assigns[:posts].include?(@posts.first).should be_true
           response.should be_success
         end
 
@@ -148,51 +148,51 @@ describe AspectsController do
       describe "ordering" do
         it "orders posts by updated_at by default" do
           get :index
-          assigns(:posts).models.should == @posts
+          assigns(:posts).should == @posts
         end
 
         it "orders posts by created_at on request" do
           get :index, :sort_order => 'created_at'
-          assigns(:posts).models.should == @posts.reverse
+          assigns(:posts).should == @posts.reverse
         end
 
         it "remembers your sort order and lets you override the memory" do
           get :index, :sort_order => "created_at"
-          assigns(:posts).models.should == @posts.reverse
+          assigns(:posts).should == @posts.reverse
           get :index
-          assigns(:posts).models.should == @posts.reverse
+          assigns(:posts).should == @posts.reverse
           get :index, :sort_order => "updated_at"
-          assigns(:posts).models.should == @posts
+          assigns(:posts).should == @posts
         end
 
         it "doesn't allow SQL injection" do
           get :index, :sort_order => "\"; DROP TABLE users;"
-          assigns(:posts).models.should == @posts
+          assigns(:posts).should == @posts
           get :index, :sort_order => "created_at"
-          assigns(:posts).models.should == @posts.reverse
+          assigns(:posts).should == @posts.reverse
         end
       end
 
       it "returns all posts by default" do
         alice.aspects.reload
         get :index
-        assigns(:posts).models.length.should == 2
+        assigns(:posts).length.should == 2
       end
 
       it "posts include reshares" do
         reshare = alice.post(:reshare, :public => true, :root_guid => Factory(:status_message, :public => true).guid, :to => alice.aspects)
         get :index
-        assigns[:posts].post_fakes.map{|x| x.id}.should include(reshare.id)
+        assigns[:posts].map{|x| x.id}.should include(reshare.id)
       end
 
       it "can filter to a single aspect" do
         get :index, :a_ids => [@alices_aspect_2.id.to_s]
-        assigns(:posts).models.length.should == 1
+        assigns(:posts).length.should == 1
       end
 
       it "can filter to multiple aspects" do
         get :index, :a_ids => [@alices_aspect_1.id.to_s, @alices_aspect_2.id.to_s]
-        assigns(:posts).models.length.should == 2
+        assigns(:posts).length.should == 2
       end
     end
 
diff --git a/spec/controllers/people_controller_spec.rb b/spec/controllers/people_controller_spec.rb
index 6b7e73735d..90ad69e71d 100644
--- a/spec/controllers/people_controller_spec.rb
+++ b/spec/controllers/people_controller_spec.rb
@@ -161,7 +161,7 @@ describe PeopleController do
         @user.post(:status_message, :text => "public", :to => 'all', :public => true)
         @user.reload.posts.length.should == 3
         get :show, :id => @user.person.to_param
-        assigns(:posts).models.should =~ @user.posts
+        assigns(:posts).map(&:id).should =~ @user.posts.map(&:id)
       end
 
       it "renders the comments on the user's posts" do
@@ -208,17 +208,17 @@ describe PeopleController do
         it "posts include reshares" do
           reshare = @user.post(:reshare, :public => true, :root_guid => Factory(:status_message, :public => true).guid, :to => alice.aspects)
           get :show, :id => @user.person.id
-          assigns[:posts].post_fakes.map{|x| x.id}.should include(reshare.id)
+          assigns[:posts].map{|x| x.id}.should include(reshare.id)
         end
 
         it "assigns only public posts" do
           get :show, :id => @person.id
-          assigns[:posts].models.should =~ @public_posts
+          assigns[:posts].map(&:id).should =~ @public_posts.map(&:id)
         end
 
         it 'is sorted by created_at desc' do
           get :show, :id => @person.id
-          assigns[:posts].models.should == @public_posts.sort_by{|p| p.created_at}.reverse
+          assigns[:posts].should == @public_posts.sort_by{|p| p.created_at}.reverse
         end
       end
 
@@ -255,13 +255,13 @@ describe PeopleController do
         bob.reload.posts.length.should == 4
 
         get :show, :id => @person.id
-        assigns(:posts).models.should =~ posts_user_can_see
+        assigns(:posts).map(&:id).should =~ posts_user_can_see.map(&:id)
       end
 
       it "posts include reshares" do
         reshare = @user.post(:reshare, :public => true, :root_guid => Factory(:status_message, :public => true).guid, :to => alice.aspects)
         get :show, :id => @user.person.id
-        assigns[:posts].post_fakes.map{|x| x.id}.should include(reshare.id)
+        assigns[:posts].map{|x| x.id}.should include(reshare.id)
       end
 
       it 'sets @commenting_disabled to true' do
@@ -293,13 +293,13 @@ describe PeopleController do
         eve.reload.posts.length.should == 3
 
         get :show, :id => @person.id
-        assigns[:posts].models.should =~ [public_post]
+        assigns[:posts].map(&:id).should =~ [public_post].map(&:id)
       end
 
         it "posts include reshares" do
           reshare = @user.post(:reshare, :public => true, :root_guid => Factory(:status_message, :public => true).guid, :to => alice.aspects)
           get :show, :id => @user.person.id
-          assigns[:posts].post_fakes.map{|x| x.id}.should include(reshare.id)
+          assigns[:posts].map{|x| x.id}.should include(reshare.id)
         end
 
       it 'sets @commenting_disabled to true' do
diff --git a/spec/controllers/tags_controller_spec.rb b/spec/controllers/tags_controller_spec.rb
index 70d5a7f516..ee3abbd1ca 100644
--- a/spec/controllers/tags_controller_spec.rb
+++ b/spec/controllers/tags_controller_spec.rb
@@ -45,21 +45,21 @@ describe TagsController do
       it 'displays your own post' do
         my_post = alice.post(:status_message, :text => "#what", :to => 'all')
         get :show, :name => 'what'
-        assigns(:posts).models.should == [my_post]
+        assigns(:posts).should == [my_post]
         response.status.should == 200
       end
 
       it "displays a friend's post" do
         other_post = bob.post(:status_message, :text => "#hello", :to => 'all')
         get :show, :name => 'hello'
-        assigns(:posts).models.should == [other_post]
+        assigns(:posts).should == [other_post]
         response.status.should == 200
       end
 
       it 'displays a public post' do
         other_post = eve.post(:status_message, :text => "#hello", :public => true, :to => 'all')
         get :show, :name => 'hello'
-        assigns(:posts).models.should == [other_post]
+        assigns(:posts).should == [other_post]
         response.status.should == 200
       end
 
@@ -67,7 +67,7 @@ describe TagsController do
         stranger = Factory(:user_with_aspect)
         stranger_post = stranger.post(:status_message, :text => "#hello", :public => true, :to => 'all')
         get :show, :name => 'hello'
-        assigns(:posts).models.should == [stranger_post]
+        assigns(:posts).should == [stranger_post]
       end
 
       it 'displays a post with a comment containing the tag search' do
@@ -76,7 +76,7 @@ describe TagsController do
         other_post = bob.post(:status_message, :text => "sup y'all", :to => 'all')
         Factory(:comment, :text => "#hello", :post => other_post)
         get :show, :name => 'hello'
-        assigns(:posts).models.should == [other_post]
+        assigns(:posts).should == [other_post]
         response.status.should == 200
       end
 
@@ -117,7 +117,7 @@ describe TagsController do
 
         it "assigns the right set of posts" do
           get :show, :name => 'what'
-          assigns[:posts].models.should == [@post]
+          assigns[:posts].should == [@post]
         end
 
         it 'succeeds with comments' do
diff --git a/spec/lib/fake_spec.rb b/spec/lib/fake_spec.rb
deleted file mode 100644
index 1e37c15a24..0000000000
--- a/spec/lib/fake_spec.rb
+++ /dev/null
@@ -1,58 +0,0 @@
-require 'spec_helper'
-describe PostsFake do
-  before do
-    @posts = []
-    @people = []
-    4.times do
-      post = Factory(:status_message)
-      @people << post.author
-      4.times do
-        comment = Factory(:comment, :post => post)
-        comment.author
-      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
-    include Rails.application.routes.url_helpers
-    before do
-      @post = mock()
-      @fakes = mock()
-      @fake = PostsFake::Fake.new(@post, @fakes)
-    end
-    it 'refers to the parent collection for an author' do
-      @post.should_receive(:author_id)
-      @fakes.should_receive(:people_hash).and_return({})
-      @fake.author
-    end
-    it 'refers to its post for any other field' do
-      @post.should_receive(:text)
-      @fake.text
-    end
-
-
-    it 'works with url helpers' do
-      sm = Factory(:status_message)
-      fake = PostsFake::Fake.new(sm, @fakes)
-
-      post_path(fake).should == post_path(sm)
-    end
-  end
-end
-
-- 
GitLab