Take queries out of aspect contacts pane on home page

This commit is contained in:
Raphael 2010-11-30 15:35:32 -08:00
parent 57485e7093
commit 366734ca5e
3 changed files with 48 additions and 7 deletions

View file

@ -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

View file

@ -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

View file

@ -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")