From c9d58d930866bd965d6a028b526b67d982a0b4b8 Mon Sep 17 00:00:00 2001
From: Raphael Sofaer <raphael@joindiaspora.com>
Date: Wed, 30 Mar 2011 14:47:17 -0700
Subject: [PATCH] Revert "Don't instantiate all contacts on aspects index,
 temporary querying in views for aspect listings"

This reverts commit 3b7063d44a35e8c74aea5a146e8746247928d823.
---
 app/controllers/aspects_controller.rb   | 14 +++-----------
 app/models/profile.rb                   | 16 ++++++----------
 app/views/aspects/_aspect_listings.haml |  2 +-
 app/views/shared/_publisher.html.haml   |  4 ++--
 4 files changed, 12 insertions(+), 24 deletions(-)

diff --git a/app/controllers/aspects_controller.rb b/app/controllers/aspects_controller.rb
index a73cd15ea5..947762b205 100644
--- a/app/controllers/aspects_controller.rb
+++ b/app/controllers/aspects_controller.rb
@@ -16,6 +16,7 @@ class AspectsController < ApplicationController
     else
       @aspects = current_user.aspects
     end
+    @aspects = @aspects.includes(:contacts => {:person => :profile})
 
     # redirect to signup
     if (current_user.getting_started == true || @aspects.blank?) && !request.format.mobile? && !request.format.js?
@@ -23,16 +24,7 @@ class AspectsController < ApplicationController
       return
     end
 
-    @selected_contacts_hashes = Contact.connection.execute(
-      current_user.contacts.joins(:person => :profile).select("people.id, people.guid, profiles.first_name, profiles.last_name, people.diaspora_handle, profiles.image_url_small, profiles.image_url_medium, profiles.image_url").to_sql
-    ).map do |r|
-      {:id => r[1],
-       :name => Person.name_from_attrs(r[2], r[3], r[4]),
-       :avatar => Profile.image_url_from_attrs(:thumb_small, r[5], r[6], r[7]),
-       :handle => r[4],
-       :url => "/people/#{r[0]}"}
-    end
-
+    @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', :order => session[:sort_order] + ' DESC', :page => params[:page]).includes(
       :comments, :mentions, :likes, :dislikes).paginate(:page => params[:page], :per_page => 15, :order => session[:sort_order] + ' DESC')
@@ -106,7 +98,7 @@ class AspectsController < ApplicationController
 
   def edit
     @aspect = current_user.aspects.where(:id => params[:id]).includes(:contacts => {:person => :profile}).first
-
+    
     @contacts_in_aspect = @aspect.contacts.includes(:person => :profile).all.sort! { |x, y| x.person.name <=> y.person.name }
     c = Contact.arel_table
     if @contacts_in_aspect.empty?
diff --git a/app/models/profile.rb b/app/models/profile.rb
index 6882a5f0b3..d45c29fbb4 100644
--- a/app/models/profile.rb
+++ b/app/models/profile.rb
@@ -63,17 +63,13 @@ class Profile < ActiveRecord::Base
     (self.person) ? self.person.diaspora_handle : self[:diaspora_handle]
   end
 
-  def image_url(size_wanted = :thumb_large)
-    self.class.image_url_from_attrs(size_wanted, self[:image_url_small], self[:image_url_medium], self[:image_url])
-  end
-
-  def self.image_url_from_attrs(size_wanted, small, medium, large)
-    result = if size_wanted == :thumb_medium && medium
-               medium
-             elsif size_wanted == :thumb_small && small
-               small
+  def image_url(size = :thumb_large)
+    result = if size == :thumb_medium && self[:image_url_medium]
+               self[:image_url_medium]
+             elsif size == :thumb_small && self[:image_url_small]
+               self[:image_url_small]
              else
-               large
+               self[:image_url]
              end
     result || '/images/user/default.png'
   end
diff --git a/app/views/aspects/_aspect_listings.haml b/app/views/aspects/_aspect_listings.haml
index 0054c8ec39..33d410d93b 100644
--- a/app/views/aspects/_aspect_listings.haml
+++ b/app/views/aspects/_aspect_listings.haml
@@ -10,4 +10,4 @@
 
 %ul
   - for aspect in aspects
-    = render 'aspects/aspect', :aspect => aspect, :contacts => aspect.contacts.where(:pending => false).limit(16).includes(:person => :profile)
+    = render 'aspects/aspect', :aspect => aspect, :contacts => aspect.contacts.reject{|x| x.pending == true}
diff --git a/app/views/shared/_publisher.html.haml b/app/views/shared/_publisher.html.haml
index 794fa618db..3bab72762d 100644
--- a/app/views/shared/_publisher.html.haml
+++ b/app/views/shared/_publisher.html.haml
@@ -20,8 +20,8 @@
 
   .content_creation
     = form_for(StatusMessage.new, :remote => true, :html => {"data-type" => "json"}) do |status|
-      - if @selected_contacts_hashes
-        = hidden_field_tag :contact_json, @selected_contacts_hashes.to_json
+      - if @selected_contacts
+        = hidden_field_tag :contact_json, @selected_contacts.map{|contact| contact.person}.to_json
       = status.error_messages
       #file-upload
         = image_tag 'icons/photos.png'
-- 
GitLab