From 32a2264af502be0b3b4b0b842192a38c6e0d67ff Mon Sep 17 00:00:00 2001
From: Raphael <raphael@joindiaspora.com>
Date: Mon, 6 Dec 2010 13:35:54 -0800
Subject: [PATCH] Refactor peoplecontroller index

---
 app/controllers/people_controller.rb       | 30 ++++++++----
 app/views/people/index.html.haml           |  8 +---
 spec/controllers/people_controller_spec.rb | 54 ++++++++++++++++++++--
 spec/models/person_spec.rb                 |  2 +-
 4 files changed, 74 insertions(+), 20 deletions(-)

diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb
index 61d9a43da4..a6f530fba0 100644
--- a/app/controllers/people_controller.rb
+++ b/app/controllers/people_controller.rb
@@ -12,18 +12,32 @@ class PeopleController < ApplicationController
     @aspect = :search
 
     @people = Person.search(params[:q]).paginate :page => params[:page], :per_page => 25, :order => 'created_at DESC'
-    @requests = Request.all(:to_id.in => @people.map{|p| p.id}, :from_id => current_user.person.id)
-    
-    #only do it if it is an email address
-    if params[:q].try(:match, Devise.email_regexp)
-      webfinger(params[:q])
-    end
-    
     if @people.count == 1
       redirect_to @people.first
     else
-      respond_with @people
+      @hashes = hashes_for_people(@people, @aspects)
+      #only do it if it is an email address
+      if params[:q].try(:match, Devise.email_regexp)
+        webfinger(params[:q])
+      end
+    end
+  end
+  def hashes_for_people people, aspects
+    ids = people.map{|p| p.id}
+    requests = {}
+    Request.all(:to_id.in => ids, :from_id => current_user.person.id).each do |r|
+      requests[r.to_id] = r
+    end
+    contacts = {}
+    Contact.all(:user_id => current_user.id, :person_id.in => ids).each do |contact|
+      contacts[contact.person_id] = contact
     end
+    people.map{|p|
+      {:person => p,
+        :contact => contacts[p.id],
+        :request => requests[p.id],
+        :aspects => aspects}
+    }
   end
 
   def show
diff --git a/app/views/people/index.html.haml b/app/views/people/index.html.haml
index 98f43fe403..d3cf5ac5eb 100644
--- a/app/views/people/index.html.haml
+++ b/app/views/people/index.html.haml
@@ -8,11 +8,7 @@
     %u= params[:q]
 
   %ul{:class => 'stream people', :id => 'people_stream'}
-    - for person in @people
-      = render 'people/person',
-        :person => person,
-        :aspects => @aspects,
-        :contact => @contacts.detect{|contact| contact.person_id == person.id},
-        :request => @requests.detect{|request| request.to_id == person.id}
+    - for hash in @hashes
+      = render :partial => 'people/person', :locals => hash
 
   = will_paginate @people
diff --git a/spec/controllers/people_controller_spec.rb b/spec/controllers/people_controller_spec.rb
index 2a2de0b9a8..eb3a8c2b96 100644
--- a/spec/controllers/people_controller_spec.rb
+++ b/spec/controllers/people_controller_spec.rb
@@ -14,22 +14,66 @@ describe PeopleController do
     sign_in :user, user
   end
 
+  describe '#hashes_from_people' do
+    before do
+      @everyone = []
+      10.times do
+        @everyone << Factory.create(:person)
+      end
+
+      user.send_contact_request_to(@everyone[3], aspect)
+      user.send_contact_request_to(@everyone[2], aspect)
+      user.activate_contact(@everyone[4], aspect)
+      user.activate_contact(@everyone[5], aspect)
+
+      @people = Person.search('eugene')
+      @people.length.should == 10
+      @hashes = @controller.hashes_for_people(@people, user.aspects)
+    end
+    it 'has the correct result for no relationship' do
+      hash = @hashes.first
+      hash[:person].should == @people.first
+      hash[:contact].should be_false
+      hash[:request].should be_false
+      hash[:aspects].should == user.aspects
+    end
+    it 'has the correct result for a connected person' do
+      hash = @hashes[4]
+      hash[:person].should == @people[4]
+      hash[:contact].should be_true
+      hash[:request].should be_false
+      hash[:aspects].should == user.aspects
+    end
+    it 'has the correct result for a requested person' do
+      hash = @hashes[2]
+      hash[:person].should == @people[2]
+      hash[:contact].should be_false
+      hash[:request].should be_true
+      hash[:aspects].should == user.aspects
+    end
+  end
   describe '#index' do
     before do
       @eugene = Factory.create(:person, :profile => {:first_name => "Eugene", :last_name => "w"})
       @korth  = Factory.create(:person, :profile => {:first_name => "Evan", :last_name => "Korth"})
     end
 
-    it "yields search results for substring of person name" do
+    it "assigns hashes" do
+      eugene2 = Factory.create(:person, :profile => {:first_name => "Eugene", :last_name => "w"})
       get :index, :q => "Eu"
-      assigns[:people].should include @eugene
+      assigns[:hashes][0][:person].should == @eugene
+      assigns[:hashes][0][:person].should == eugene2
+    end
+    it "assigns people" do
+      eugene2 = Factory.create(:person, :profile => {:first_name => "Eugene", :last_name => "w"})
+      get :index, :q => "Eu"
+      assigns[:people].should == [@eugene, eugene2]
     end
-
     it 'shows a contact' do
       user2 = make_user
       connect_users(user, aspect, user2, user2.aspects.create(:name => 'Neuroscience'))
       get :index, :q => user2.person.profile.first_name.to_s
-      assigns[:people].should include user2.person
+      response.should redirect_to user2.person
     end
 
     it 'shows a non-contact' do
@@ -37,7 +81,7 @@ describe PeopleController do
       user2.person.profile.searchable = true
       user2.save
       get :index, :q => user2.person.profile.first_name.to_s
-      assigns[:people].should include user2.person
+      response.should redirect_to user2.person
     end
 
     it "redirects to person page if there is exactly one match" do
diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb
index a04229b935..841cbed70b 100644
--- a/spec/models/person_spec.rb
+++ b/spec/models/person_spec.rb
@@ -140,7 +140,7 @@ describe Person do
     end
   end
 
-  describe '#search' do
+  describe '.search' do
     before do
       @connected_person_one   = Factory.create(:person)
       @connected_person_two   = Factory.create(:person)
-- 
GitLab