From 3c34749dd775e63fda9d6b7e8b564a4a5011a18e Mon Sep 17 00:00:00 2001
From: Raphael Sofaer <raphael@joindiaspora.com>
Date: Fri, 12 Aug 2011 10:13:29 -0700
Subject: [PATCH] Put a select limit on the person query on contactsController
 index, since we know exactly what we're doing with the data

---
 app/controllers/contacts_controller.rb |  5 ++---
 app/models/person.rb                   |  3 ++-
 spec/models/person_spec.rb             | 21 ++++++++++++++++++++-
 3 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/app/controllers/contacts_controller.rb b/app/controllers/contacts_controller.rb
index 6ec8d65db0..c1c321b546 100644
--- a/app/controllers/contacts_controller.rb
+++ b/app/controllers/contacts_controller.rb
@@ -24,12 +24,11 @@ class ContactsController < ApplicationController
       format.html { @contacts = sort_and_paginate_profiles(@contacts) }
       format.mobile { @contacts = sort_and_paginate_profiles(@contacts) }
       format.json {
-        @people = Person.joins(:contacts => :aspect_memberships).
-          select('DISTINCT people.*').
+        @people = Person.for_json.joins(:contacts => :aspect_memberships).
           where(:contacts => { :user_id => current_user.id },
                 :aspect_memberships => { :aspect_id => params[:aspect_ids] })
 
-        render :json => @people.includes(:profile).to_json
+        render :json => @people.to_json
       }
     end
   end
diff --git a/app/models/person.rb b/app/models/person.rb
index d2674bbfc0..84f9b41e0c 100644
--- a/app/models/person.rb
+++ b/app/models/person.rb
@@ -45,6 +45,7 @@ class Person < ActiveRecord::Base
   scope :searchable, joins(:profile).where(:profiles => {:searchable => true})
   scope :remote, where('people.owner_id IS NULL')
   scope :local, where('people.owner_id IS NOT NULL')
+  scope :for_json, select('DISTINCT people.id, people.diaspora_handle').includes(:profile)
 
   def self.featured_users
     AppConfig[:featured_users].present? ? Person.where(:diaspora_handle => AppConfig[:featured_users]) : []
@@ -63,7 +64,7 @@ class Person < ActiveRecord::Base
     p
   end
 
-  
+
   def self.search_query_string(query)
     query = query.downcase
 
diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb
index 13d9477d9b..63a8861b36 100644
--- a/spec/models/person_spec.rb
+++ b/spec/models/person_spec.rb
@@ -12,6 +12,25 @@ describe Person do
   end
 
   context 'scopes' do
+    describe '.for_json' do
+      it 'does not select public keys' do
+        proc {
+          Person.for_json.first.serialized_public_key
+        }.should raise_error ActiveModel::MissingAttributeError
+      end
+      it 'eager loads profiles' do
+        Person.for_json.first.loaded_profile?.should be_true
+      end
+      it 'selects distinct people' do
+        aspect = bob.aspects.create(:name => 'hilarious people')
+        aspect.contacts << bob.contact_for(eve.person)
+        person_ids = Person.for_json.joins(:contacts => :aspect_memberships).
+          where(:contacts => {:user_id => bob.id},
+               :aspect_memberships => {:aspect_id => bob.aspect_ids}).map{|p| p.id}
+
+        person_ids.uniq.should == person_ids
+      end
+    end
     describe '.local' do
       it 'returns only local people' do
         Person.local =~ [@person]
@@ -28,7 +47,7 @@ describe Person do
       it 'searchs for a person if id is passed' do
         Person.find_from_id_or_username(:id => @person.id).id.should == @person.id
       end
-      
+
       it 'searchs a person from a user if username is passed' do
         Person.find_from_id_or_username(:username => @user.username).id.should == @user.person.id
       end
-- 
GitLab