From 366734ca5ee8088c672c04cf97d82a0a1a20d14f Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 30 Nov 2010 15:35:32 -0800 Subject: [PATCH] Take queries out of aspect contacts pane on home page --- app/controllers/aspects_controller.rb | 16 +++++++++++- app/views/shared/_aspect_contacts.haml | 12 ++++----- spec/controllers/aspects_controller_spec.rb | 27 +++++++++++++++++++++ 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/app/controllers/aspects_controller.rb b/app/controllers/aspects_controller.rb index a6f79ca28..8ad17ba23 100644 --- a/app/controllers/aspects_controller.rb +++ b/app/controllers/aspects_controller.rb @@ -11,7 +11,10 @@ class AspectsController < ApplicationController def index @posts = current_user.visible_posts(:_type => "StatusMessage").paginate :page => params[:page], :per_page => 15, :order => 'created_at DESC' @post_hashes = hashes_for_posts @posts - + @aspect_hashes = hashes_for_aspects @aspects.all, @contacts + pp @aspect_hashes + pp @aspects.all + pp @contacts @aspect = :all if current_user.getting_started == true @@ -135,6 +138,17 @@ class AspectsController < ApplicationController end private + def hashes_for_aspects aspects, contacts + aspects.map do |a| + hash = {:aspect => a} + aspect_contacts = contacts.select{|c| + c.aspect_ids.include?(a.id)} + hash[:contact_count] = aspect_contacts.count + person_ids = aspect_contacts.map{|c| c.person_id} + hash[:people] = Person.all(:id.in => person_ids, :limit => 8) + hash + end + end def hashes_for_posts posts post_ids = posts.map{|p| p.id} comment_hash = Comment.hash_from_post_ids post_ids diff --git a/app/views/shared/_aspect_contacts.haml b/app/views/shared/_aspect_contacts.haml index 41fa0a29d..da55c5f40 100644 --- a/app/views/shared/_aspect_contacts.haml +++ b/app/views/shared/_aspect_contacts.haml @@ -43,16 +43,16 @@ = link_to t('.add_aspect'), '#add_aspect_pane', :class => "add_aspect_button" %ul - - for user_aspect in @aspects + - for a_hash in @aspect_hashes %li %h4 - = link_to user_aspect, user_aspect + = link_to a_hash[:aspect], a_hash[:aspect] %span - = t('contacts', :count=>user_aspect.contacts.count) + = t('contacts', :count=>a_hash[:contact_count]) - - if user_aspect.contacts.count > 0 - - for aspect_contact in user_aspect.contacts[0..7] - = person_image_link(aspect_contact.person) + - if a_hash[:contact_count] > 0 + - for person in a_hash[:people] + = person_image_link(person) .section %h3= t('.invites') = render "shared/invitations", :invites => @invites diff --git a/spec/controllers/aspects_controller_spec.rb b/spec/controllers/aspects_controller_spec.rb index 572b9247d..66d28365f 100644 --- a/spec/controllers/aspects_controller_spec.rb +++ b/spec/controllers/aspects_controller_spec.rb @@ -153,6 +153,33 @@ describe AspectsController do end end + describe "#hashes_for_aspects" do + before do + @people = [] + 10.times {@people << Factory.create(:person)} + @people.each{|p| @user.reload.activate_contact(p, @user.aspects.first.reload)} + @user.reload + @hashes = @controller.send(:hashes_for_aspects, @user.aspects, @user.contacts) + @hash = @hashes.first + @aspect = @user.aspects.first + end + it 'has aspects' do + @hashes.length.should == 2 + @hash[:aspect].should == @aspect + end + it 'has a contact count' do + @hash[:contact_count].should == 11 + end + it 'has people' do + desired_people = @aspect.contacts.map{|c| c.person.diaspora_handle} + gotten_people = @hash[:people].map{|p| p.diaspora_handle} + gotten_people.each{|p| desired_people.should include p} + end + it 'has at most 8 people' do + @hash[:people].length.should == 8 + end + end + describe "#update" do before do @aspect = @user.aspects.create(:name => "Bruisers")