Remove n-query problem from contact_list

This commit is contained in:
Raphael 2011-01-19 18:06:35 -08:00
parent e97081e8cd
commit 2c68bb0305
8 changed files with 31 additions and 14 deletions

View file

@ -78,22 +78,24 @@ class AspectsController < ApplicationController
end end
def edit def edit
@aspect = current_user.aspects.where(:id => params[:id]).first @aspect = current_user.aspects.where(:id => params[:id]).includes(:contacts => {:person => :profile}).first
@contacts = current_user.contacts.includes(:person => :profile).where(:pending => false) @contacts = current_user.contacts.includes(:person => :profile).where(:pending => false)
unless @aspect unless @aspect
render :file => "#{Rails.root}/public/404.html", :layout => false, :status => 404 render :file => "#{Rails.root}/public/404.html", :layout => false, :status => 404
else else
@aspect_ids = [@aspect.id] @aspect_ids = [@aspect.id]
@aspect_contacts_count = @aspect.contacts.count @aspect_contacts_count = @aspect.contacts.length
render :layout => false render :layout => false
end end
end end
def manage def manage
Rails.logger.info("Controller time")
@aspect = :manage @aspect = :manage
@contacts = current_user.contacts.includes(:person => :profile).where(:pending => false) @contacts = current_user.contacts.includes(:person => :profile).where(:pending => false)
@remote_requests = Request.where(:recipient_id => current_user.person.id).includes(:sender => :profile) @remote_requests = Request.where(:recipient_id => current_user.person.id).includes(:sender => :profile)
@aspects = @all_aspects.includes(:contacts => {:person => :profile}) @aspects = @all_aspects.includes(:contacts => {:person => :profile})
Rails.logger.info("VIEW TIME!!!!!!")
end end
def update def update

View file

@ -38,11 +38,11 @@ module AspectsHelper
:class => 'added button' :class => 'added button'
end end
def aspect_membership_button(aspect_id, contact, person) def aspect_membership_button(aspect, contact, person)
if contact.nil? || !contact.aspect_ids.include?(aspect_id) if contact.nil? || !aspect.contacts.include?(contact)
add_to_aspect_button(aspect_id, person.id) add_to_aspect_button(aspect.id, person.id)
else else
remove_from_aspect_button(aspect_id, person.id) remove_from_aspect_button(aspect.id, person.id)
end end
end end

View file

@ -26,7 +26,7 @@
= button_to t('.remove_aspect'), @aspect, :method => "delete", :confirm => t('.confirm_remove_aspect'), :class => 'button' = button_to t('.remove_aspect'), @aspect, :method => "delete", :confirm => t('.confirm_remove_aspect'), :class => 'button'
- if @contacts.count > 0 - if @contacts.count > 0
= render 'shared/contact_list', :aspect_id => @aspect.id, :contacts => @contacts = render 'shared/contact_list', :aspect => @aspect, :contacts => @contacts
#aspect_edit_controls #aspect_edit_controls
= link_to t('.rename'), '#' = link_to t('.rename'), '#'

View file

@ -46,7 +46,7 @@
- for aspect in @all_aspects - for aspect in @all_aspects
%li{:data=>{:guid=>aspect.id}, :class => ("selected" if @object_aspect_ids.include?(aspect.id))} %li{:data=>{:guid=>aspect.id}, :class => ("selected" if @object_aspect_ids.include?(aspect.id))}
= link_for_aspect(aspect, :class => 'aspect_selector', :title => "#{aspect.contacts.count} contacts") = link_for_aspect(aspect, :class => 'aspect_selector', :title => "#{aspect.aspect_memberships.length} contacts")
%li %li
= link_to '+', '#add_aspect_pane', :class => "add_aspect_button", :title => t('aspects.manage.add_a_new_aspect'), :rel => 'facebox' = link_to '+', '#add_aspect_pane', :class => "add_aspect_button", :title => t('aspects.manage.add_a_new_aspect'), :rel => 'facebox'

View file

@ -9,12 +9,12 @@
%span.name %span.name
= link_to aspect.name, aspect = link_to aspect.name, aspect
.right .right
= aspect_membership_button(aspect.id, contact, person) = aspect_membership_button(aspect, contact, person)
- for aspect in aspects_without_person - for aspect in aspects_without_person
%li{:data=>{:guid=>aspect.id}} %li{:data=>{:guid=>aspect.id}}
%span.name %span.name
= link_to aspect.name, aspect = link_to aspect.name, aspect
.right .right
= aspect_membership_button(aspect.id, contact, person) = aspect_membership_button(aspect, contact, person)

View file

@ -9,7 +9,7 @@
%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 => aspect, :contacts => contacts, :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

@ -14,5 +14,5 @@
.description .description
= contact.person.diaspora_handle = contact.person.diaspora_handle
.right .right
= aspect_membership_button(aspect_id, contact, contact.person) = aspect_membership_button(aspect, contact, contact.person)

View file

@ -88,14 +88,16 @@ describe AspectsController do
connect_users(@user, @aspect0, user, aspect) connect_users(@user, @aspect0, user, aspect)
post = @user.post(:status_message, :message => "hello#{n}", :to => @aspect1.id) post = @user.post(:status_message, :message => "hello#{n}", :to => @aspect1.id)
@posts << post @posts << post
user.comment "yo#{post.message}", :on => post 8.times do |n|
user.comment "yo#{post.message}", :on => post
end
end end
end end
it 'takes time' do it 'takes time' do
Benchmark.realtime{ Benchmark.realtime{
get :index get :index
}.should < 1.5 }.should < 0.8
end end
end end
end end
@ -141,6 +143,19 @@ describe AspectsController do
get :manage get :manage
response.should be_success response.should be_success
end end
it "performs reasonably" do
require 'benchmark'
8.times do |n|
aspect = @user.aspects.create(:name => "aspect#{n}")
8.times do |o|
person = Factory(:person)
@user.activate_contact(person, aspect)
end
end
Benchmark.realtime{
get :manage
}.should < 2
end
it "assigns aspect to manage" do it "assigns aspect to manage" do
get :manage get :manage
assigns(:aspect).should == :manage assigns(:aspect).should == :manage