We got nominated! Help us out and vote for GitHub as Best Bootstrapped Startup of 2008. (You can vote once a day.) [ hide ]

public
Description: The open source social networking platform in Ruby on Rails from the author of RailsSpace
Homepage: http://insoshi.com
Clone URL: git://github.com/insoshi/insoshi.git
Fixed horribly broken common_contacts_with method
Michael Hartl (author)
Wed Oct 15 17:43:55 -0700 2008
commit  a6fc61fe1f709ef86514e0d5d5227ae09dfa7d97
tree    a64737388b327e47e681a8956f1e29fe4a065f16
parent  aabcf1f482e7045e4091b71a9a7685997725193b
...
22
23
24
25
 
 
 
26
27
28
...
139
140
141
142
 
143
144
145
...
22
23
24
 
25
26
27
28
29
30
...
141
142
143
 
144
145
146
147
0
@@ -22,7 +22,9 @@ class PeopleController < ApplicationController
0
     end
0
     if logged_in?
0
       @some_contacts = @person.some_contacts
0
- @common_contacts = current_person.common_contacts_with(@person)
0
+ @common_contacts = current_person.common_contacts_with(@person,
0
+ :page =>
0
+ params[:page])
0
       # Use the same max number as in basic contacts list.
0
       num_contacts = Person::MAX_DEFAULT_CONTACTS
0
       @some_common_contacts = @common_contacts[0...num_contacts]
0
@@ -139,7 +141,7 @@ class PeopleController < ApplicationController
0
   def common_contacts
0
     @person = Person.find(params[:id])
0
     @common_contacts = @person.common_contacts_with(current_person,
0
- params[:page])
0
+ :page => params[:page])
0
     respond_to do |format|
0
       format.html
0
     end
...
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
 
 
 
 
393
394
395
...
373
374
375
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376
377
378
379
380
381
382
0
@@ -373,23 +373,10 @@ class Person < ActiveRecord::Base
0
   end
0
 
0
   # Return the common connections with the given person.
0
- def common_contacts_with(contact, page = 1)
0
- # AND NOT (contact_id = :person OR contact_id = :contact)
0
- sql = %(SELECT DISTINCT contact_id FROM connections
0
- INNER JOIN people contact ON connections.contact_id = contact.id
0
- WHERE ((person_id = :person OR person_id = :contact)
0
- AND NOT (contact_id = :person OR contact_id = :contact)
0
- AND status = :accepted
0
- AND contact.deactivated = :false
0
- AND (contact.email_verified IS NULL
0
- OR contact.email_verified = :true)))
0
- conditions = [sql, { :person => id, :contact => contact.id,
0
- :accepted => Connection::ACCEPTED,
0
- :false => false, :true => true }]
0
- opts = { :page => page, :per_page => RASTER_PER_PAGE }
0
- @common_contacts ||= Person.find(Connection.
0
- paginate_by_sql(conditions, opts).
0
- map(&:contact_id)).paginate
0
+ def common_contacts_with(contact, options = {})
0
+ # I tried to do this in SQL for efficiency, but failed miserably.
0
+ # Horrifyingly, MySQL lacks support for the INTERSECT keyword.
0
+ (contacts & contact.contacts).paginate(options)
0
   end
0
   
0
   protected
...
1
2
 
 
 
 
 
 
 
 
3
4
5
6
7
8
9
10
11
12
13
14
15
16
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
 
 
 
 
 
16
17
18
0
@@ -1,15 +1,17 @@
0
 <%- unless current_person?(@person) or @common_contacts.empty? -%>
0
     <h2>Common Contacts</h2>
0
+
0
+
0
+ <%- if @common_contacts.length > Person::MAX_DEFAULT_CONTACTS -%>
0
+ <div class="button see_all_link">
0
+ <%= link_to "See All",
0
+ common_contacts_person_path(@person) %>
0
+ </div>
0
+ <%- end -%>
0
     <ul class="grid contacts medium">
0
       <%- @some_common_contacts.each do |contact| -%>
0
         <%= render :partial => 'shared/contact_medium',
0
                    :locals => { :contact => contact } %>
0
       <%- end -%>
0
- <%- if @common_contacts.length > Person::MAX_DEFAULT_CONTACTS -%>
0
- <li class="more">
0
- <%= link_to "See all &raquo;",
0
- common_contacts_person_path(@person) %>
0
- </li>
0
- <%- end -%>
0
     </ul>
0
 <%- end -%>
0
\ No newline at end of file
...
187
188
189
 
 
 
 
 
 
190
191
192
...
187
188
189
190
191
192
193
194
195
196
197
198
0
@@ -187,6 +187,12 @@ describe Person do
0
         common_contacts.should == [@contact]
0
       end
0
 
0
+ it "should not include non-common contacts" do
0
+ admin = people(:admin)
0
+ Connection.connect(@person, admin)
0
+ @person.common_contacts_with(@kelly).should_not contain(admin)
0
+ end
0
+
0
       it "should exclude deactivated people from common contacts" do
0
         @contact.toggle!(:deactivated)
0
         common_contacts = @person.common_contacts_with(@kelly)

Comments

    No one has commented yet.