Some optimizations to ServicesController#finder. Apparently setting an association on an unsaved record to an empty array hits the database.

This commit is contained in:
Raphael Sofaer 2011-08-13 09:40:31 -07:00
parent b85df7303b
commit f0583de36c
6 changed files with 36 additions and 45 deletions

View file

@ -66,7 +66,7 @@ module AspectGlobalHelper
end
def aspect_dropdown_list_item(aspect, contact, person)
checked = (contact.persisted? && contact.aspect_memberships.detect{ |am| am.aspect_id == aspect.id})
checked = (contact.persisted? && contact.in_aspect?(aspect))
klass = checked ? "selected" : ""
str = <<LISTITEM

View file

@ -1,5 +1,6 @@
module ServicesHelper
@@contact_proxy = Contact.new(:aspects => [])
def contact_proxy(friend)
friend.contact || Contact.new(:person => friend.person, :aspects => [])
friend.contact || @@contact_proxy.dup.tap{|c| c.person = friend.person}
end
end

View file

@ -73,6 +73,16 @@ class Contact < ActiveRecord::Base
self.sharing && self.receiving
end
def in_aspect? aspect
if aspect_memberships.loaded?
aspect_memberships.detect{ |am| am.aspect_id == aspect.id }
elsif aspects.loaded?
aspects.detect{ |a| a.id == aspect.id }
else
AspectMembership.exists?(:contact_id => self.id, :aspect_id => aspect.id)
end
end
private
def not_contact_for_self
if person_id && person.owner == user

View file

@ -29,7 +29,7 @@ class Services::Facebook < Service
else
self.service_users
end
result.order('service_users.person_id DESC, service_users.name')
result.includes(:contact => :aspects, :person => :profile).order('service_users.person_id DESC, service_users.name')
end
def save_friends

View file

@ -1,20 +1,28 @@
%li.remote_friend{:id => "uid_" + friend.uid, :uid => friend.uid}
.stream_element.contact{:id => friend.id}
.right
- if friend.already_invited?
= link_to t('services.index.resend'), service_inviter_path(:uid => friend.uid, :provider => 'facebook', :invitation_id => friend.invitation_id)
- elsif friend.on_diaspora?
= render 'aspect_memberships/aspect_dropdown', :contact => contact_proxy(friend), :person => friend.person, :hang => 'left'
- else
= render 'aspect_memberships/aspect_dropdown', :contact => contact_proxy(friend), :person => friend.person, :hang => 'left', :extra_class => 'inviter', :service_uid => friend.uid
- if friend.on_diaspora?
= person_image_link(friend.person, :size => :thumb_small)
- else
= image_tag(friend.photo_url, :class => 'avatar')
%h4.name
- if friend.on_diaspora?
= link_to friend.name, person_path(friend.person)
- else
= friend.name
- if friend.already_invited?
= link_to t('.resend'), service_inviter_path(:uid => friend.uid, :provider => 'facebook', :invitation_id => friend.invitation_id)
- elsif friend.on_diaspora?
= render 'aspect_memberships/aspect_dropdown', :contact => contact_proxy(friend), :person => friend.person, :hang => 'left'
- else
= facebook_invite_form(friend)
.content
%span.from.name
- if friend.on_diaspora?
= link_to friend.name, person_path(friend.person)
- else
= friend.name
.info
- if friend.person
= friend.person.diaspora_handle
- else
not yet on diaspora

View file

@ -17,35 +17,7 @@
= t('.service_friends', :service => params[:provider].titleize)
#people_stream.stream.contacts
- if @friends.size > 0
- for friend in @friends
.stream_element.contact{:id => friend.id}
.right
- if friend.already_invited?
= link_to t('.resend'), service_inviter_path(:uid => friend.uid, :provider => 'facebook', :invitation_id => friend.invitation_id)
- elsif friend.on_diaspora?
= render 'aspect_memberships/aspect_dropdown', :contact => contact_proxy(friend), :person => friend.person, :hang => 'left'
- else
= render 'aspect_memberships/aspect_dropdown', :contact => contact_proxy(friend), :person => friend.person, :hang => 'left', :extra_class => 'inviter', :service_uid => friend.uid
- if friend.on_diaspora?
= person_image_link(friend.person, :size => :thumb_small)
- else
= image_tag(friend.photo_url, :class => 'avatar')
.content
%span.from.name
- if friend.on_diaspora?
= link_to friend.name, person_path(friend.person)
- else
= friend.name
.info
- if friend.person
= friend.person.diaspora_handle
- else
not yet on diaspora
= render :partial => 'remote_friend', :collection => @friends, :as => :friend
/= will_paginate @friends
- else