eager loading aspect memberships

This commit is contained in:
danielgrippi 2011-05-13 18:22:42 -07:00
parent 30796de400
commit a02b9c01c8
5 changed files with 12 additions and 6 deletions

View file

@ -112,12 +112,12 @@ 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 }
@contacts_in_aspect = @aspect.contacts.includes(:aspect_memberships, :person => :profile).all.sort! { |x, y| x.person.name <=> y.person.name }
c = Contact.arel_table
if @contacts_in_aspect.empty?
@contacts_not_in_aspect = current_user.contacts.receiving.includes(:person => :profile).all.sort! { |x, y| x.person.name <=> y.person.name }
@contacts_not_in_aspect = current_user.contacts.receiving.includes(:aspect_memberships, :person => :profile).all.sort! { |x, y| x.person.name <=> y.person.name }
else
@contacts_not_in_aspect = current_user.contacts.receiving.where(c[:id].not_in(@contacts_in_aspect.map(&:id))).includes(:person => :profile).all.sort! { |x, y| x.person.name <=> y.person.name }
@contacts_not_in_aspect = current_user.contacts.receiving.where(c[:id].not_in(@contacts_in_aspect.map(&:id))).includes(:aspect_memberships, :person => :profile).all.sort! { |x, y| x.person.name <=> y.person.name }
end
@contacts = @contacts_in_aspect + @contacts_not_in_aspect

View file

@ -37,7 +37,7 @@ class ContactsController < ApplicationController
end
def sharing
@contacts = current_user.contacts.sharing.includes(:aspects)
@contacts = current_user.contacts.sharing.includes(:aspect_memberships)
render :layout => false
end
end

View file

@ -83,7 +83,7 @@ module AspectGlobalHelper
end
def aspect_dropdown_list_item(aspect, contact, person)
checked = contact.persisted? && contact.aspects.include?(aspect) ? "checked=\"checked\"" : ""
checked = (contact.persisted? && contact.aspect_memberships.detect{ |am| am.aspect_id == aspect.id}) ? "checked=\"checked\"" : ""
str = "<li data-aspect_id=#{aspect.id}>"
str << "<input #{checked} id=\"in_aspect\" name=\"in_aspect\" type=\"checkbox\" value=\"in_aspect\" />"
str << aspect.name

View file

@ -39,7 +39,7 @@ module AspectsHelper
end
def aspect_membership_button(aspect, contact, person)
if contact.nil? || !aspect.contacts.include?(contact)
if contact.nil? || !contact.aspect_memberships.detect{ |am| am.aspect_id == aspect.id}
add_to_aspect_button(aspect.id, person.id)
else
remove_from_aspect_button(aspect.id, person.id)

View file

@ -352,6 +352,12 @@ describe AspectsController do
get :edit, :id => alices_aspect_3.id
assigns[:contacts].map(&:id).should == [alice.contact_for(bob.person), alice.contact_for(eve.person), alice.contact_for(@katz.person), alice.contact_for(@zed.person)].map(&:id)
end
it 'eager loads the aspect memberships for all the contacts' do
get :edit, :id => @alices_aspect_2.id
pp assigns[:contacts].count
assigns[:contacts].each{ |c| pp @alices_aspect_2.contacts.include?(c); c.aspect_memberships.loaded?.should be_true}
end
end
describe "#toggle_contact_visibility" do