Take contact.person query out of the views, only query people once in hashes_for_aspects

This commit is contained in:
Raphael 2010-12-14 12:11:15 -08:00
parent fd9d1332fc
commit e63a8a4bfa
9 changed files with 34 additions and 34 deletions

View file

@ -166,13 +166,17 @@ class AspectsController < ApplicationController
end end
def hashes_for_aspects aspects, contacts, opts = {} def hashes_for_aspects aspects, contacts, opts = {}
contact_hashes = hashes_for_contacts contacts
aspects.map do |a| aspects.map do |a|
hash = {:aspect => a} hash = {:aspect => a}
aspect_contacts = contacts.select{|c| aspect_contact_hashes = contact_hashes.select{|c|
c.aspect_ids.include?(a.id)} c[:contact].aspect_ids.include?(a.id)}
hash[:contact_count] = aspect_contacts.count hash[:contact_count] = aspect_contact_hashes.count
person_ids = aspect_contacts.map{|c| c.person_id} if opts[:limit]
hash[:people] = Person.all({:id.in => person_ids, :fields => [:profile]}.merge(opts)) hash[:contacts] = aspect_contact_hashes.slice(0,opts[:limit])
else
hash[:contacts] = aspect_contact_hashes
end
hash hash
end end
end end

View file

@ -30,10 +30,5 @@ module AspectsHelper
remove_from_aspect_button(aspect_id, contact.person.id) remove_from_aspect_button(aspect_id, contact.person.id)
end end
end end
def contact_link(contact)
person = contact.person
link_to person.name, person
end
end end

View file

@ -4,6 +4,6 @@
%span %span
= t('contacts', :count => contact_count) = t('contacts', :count => contact_count)
- if contact_count > 0 - if contact_count > 0
- for person in people - for hash in contacts
= person_image_link(person) = person_image_link(hash[:person])

View file

@ -6,9 +6,9 @@
= include_javascripts :aspects = include_javascripts :aspects
#edit_aspect_pane #edit_aspect_pane
- if @contacts.count > 0 - if @contacts.count > 0
%h4= t('.add_existing') %h4= t('.add_existing')
= render 'shared/contact_list', :aspect_id => aspect.id, :contacts => contacts, :manage => defined?(manage) = render 'shared/contact_list', :aspect_id => aspect.id, :contact_hashes => contacts, :manage => defined?(manage)
= render 'shared/add_contact', :aspect_id => aspect.id = render 'shared/add_contact', :aspect_id => aspect.id

View file

@ -51,17 +51,17 @@
%li!= remove_link(hash[:aspect]) %li!= remove_link(hash[:aspect])
%ul.dropzone{:data=>{:aspect_id=>hash[:aspect].id}} %ul.dropzone{:data=>{:aspect_id=>hash[:aspect].id}}
-for person in hash[:people] -for contact_hash in hash[:contacts]
%li.person{:data=>{:guid=>person.id, :aspect_id=>hash[:aspect].id}} %li.person{:data=>{:guid=>contact_hash[:person].id, :aspect_id=>hash[:aspect].id}}
.delete .delete
.x .x
X X
.circle .circle
= link_to person_image_tag(person), person = link_to person_image_tag(contact_hash[:person]), contact_hash[:person]
.draggable_info .draggable_info
=t('.drag_to_add') =t('.drag_to_add')
.fancybox_content .fancybox_content
%div{:id => "manage_aspect_contacts_pane_#{hash[:aspect].id}"} %div{:id => "manage_aspect_contacts_pane_#{hash[:aspect].id}"}
= render "requests/manage_aspect_contacts", :aspect_name => hash[:aspect].name, :aspect_id => hash[:aspect].id, :manage => true = render "requests/manage_aspect_contacts", :aspect => hash[:aspect], :manage => true, :contact_hashes => hash[:contacts]

View file

@ -15,7 +15,7 @@
.span-8.append-1 .span-8.append-1
= render 'aspects/aspect_contacts', :contacts => @aspect_contacts, :aspect => @aspect = render 'aspects/aspect_contacts', :contacts => @aspect_contacts, :aspect => @aspect
= render 'aspects/edit_aspect_pane', :contacts => @contacts, :aspect => @aspect = render 'aspects/edit_aspect_pane', :contacts => @aspect_contacts, :aspect => @aspect
.span-15.last .span-15.last

View file

@ -6,10 +6,10 @@
.modal_title_bar .modal_title_bar
%h4 %h4
= t('.manage_within') = t('.manage_within')
%i= aspect_name %i= aspect.name
.span-6.append-1.last .span-6.append-1.last
%h3= t('.existing') %h3= t('.existing')
= render 'shared/contact_list', :aspect_id => aspect_id, :contacts => @contacts, :manage => defined?(manage) = render 'shared/contact_list', :aspect_id => aspect.id, :contact_hashes => contact_hashes, :manage => defined?(manage)
.span-7.last .span-7.last
= render 'shared/add_contact', :aspect_id => aspect_id = render 'shared/add_contact', :aspect_id => aspect.id

View file

@ -6,10 +6,10 @@
.contact_list .contact_list
= search_field_tag :contact_search, "", :class => 'contact_list_search', :results => 5, :placeholder => t('.all_contacts') = search_field_tag :contact_search, "", :class => 'contact_list_search', :results => 5, :placeholder => t('.all_contacts')
%ul %ul
- for contact in contacts - for hash in contact_hashes
%li %li
%span.name %span.name
= contact_link contact = link_to hash[:person].name, hash[:person]
.right .right
= aspect_membership_button(aspect_id, contact) = aspect_membership_button(aspect_id, hash[:contact])

View file

@ -190,19 +190,20 @@ describe AspectsController do
@hashes.length.should == 2 @hashes.length.should == 2
@hash[:aspect].should == @aspect @hash[:aspect].should == @aspect
end end
it 'has a contact count' do it 'has a contact_count' do
@hash[:contact_count].should == @aspect.contacts.count @hash[:contact_count].should == @aspect.contacts.count
end end
it 'has people' do it 'takes a limit on contacts returned' do
desired_people = @aspect.contacts.map{|c| c.person.id} @hash[:contacts].count.should == 9
gotten_people = @hash[:people].map{|p| p.id}
gotten_people.each{|p| desired_people.should include p}
end end
it 'takes a limit on people returned' do it 'has a person in each hash' do
@hash[:people].length.should == 9 @aspect.contacts.map{|c| c.person}.include?(@hash[:contacts].first[:person]).should be_true
end end
it "does not return the rsa key" do it "does not return the rsa key" do
@hash[:people].first.serialized_public_key.should be_nil @hash[:contacts].first[:person].serialized_public_key.should be_nil
end
it 'has a contact in each hash' do
@aspect.contacts.include?(@hash[:contacts].first[:contact]).should be_true
end end
end end