don't select duplicate people

This commit is contained in:
Raphael 2011-01-26 14:05:06 -08:00 committed by zhitomirskiyi
parent 31e4683183
commit a8306f9f37
2 changed files with 55 additions and 41 deletions

View file

@ -27,14 +27,14 @@ class Contact < ActiveRecord::Base
end end
def contacts def contacts
t_p = Person.arel_table people = Person.arel_table
incoming_aspects = Aspect.joins(:contacts).where( incoming_aspects = Aspect.joins(:contacts).where(
:user_id => self.person.owner_id, :user_id => self.person.owner_id,
:contacts_visible => true, :contacts_visible => true,
:contacts => {:person_id => self.user.person.id}) :contacts => {:person_id => self.user.person.id}).select('`aspects`.id')
incoming_aspect_ids = incoming_aspects.map{|a| a.id} incoming_aspect_ids = incoming_aspects.map{|a| a.id}
similar_contacts = Person.joins(:contacts => :aspect_memberships).where( similar_contacts = Person.joins(:contacts => :aspect_memberships).where(
:aspect_memberships => {:aspect_id => incoming_aspect_ids}).where(t_p[:id].not_eq(self.user.person.id)) :aspect_memberships => {:aspect_id => incoming_aspect_ids}).where(people[:id].not_eq(self.user.person.id)).select('DISTINCT `people`.*')
end end
private private
def not_contact_for_self def not_contact_for_self

View file

@ -47,17 +47,6 @@ describe Contact do
end end
end end
context 'requesting' do
before do
@contact = Contact.new
@user = Factory.create(:user)
@person = Factory(:person)
@contact.user = @user
@contact.person = @person
end
describe '#contacts' do describe '#contacts' do
before do before do
@alice = alice @alice = alice
@ -92,8 +81,33 @@ describe Contact do
asp.save asp.save
@contact.contacts.should == [] @contact.contacts.should == []
end end
it 'returns no duplicate contacts' do
[@alice, @eve].each {|c| @bob.add_contact_to_aspect(@bob.contact_for(c.person), @bob.aspects.last)}
contact_ids = @contact.contacts.map{|p| p.id}
contact_ids.uniq.should == contact_ids
end
end end
context 'on a contact for a remote user' do
before do
@contact = @bob.contact_for @people1.first
end
it 'returns an empty array' do
@contact.contacts.should == []
end
end
end
context 'requesting' do
before do
@contact = Contact.new
@user = Factory.create(:user)
@person = Factory(:person)
@contact.user = @user
@contact.person = @person
end end
describe '#generate_request' do describe '#generate_request' do