Commit 5fba93dd04f1840c1834130276f24c6cb6002579

Authored by Rodrigo Souto
1 parent 1df3b980

person: register and use friends_count to fetch most_popular people

(ActionItem3039)
app/helpers/cache_counter_helper.rb 0 → 100644
@@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
  1 +module CacheCounterHelper
  2 + def update_cache_counter(name, object, value)
  3 + if object.present?
  4 + object.send(name.to_s+'=', object.send(name) + value)
  5 + object.save!
  6 + end
  7 + end
  8 +end
app/models/friendship.rb
1 class Friendship < ActiveRecord::Base 1 class Friendship < ActiveRecord::Base
2 track_actions :new_friendship, :after_create, :keep_params => ["friend.name", "friend.url", "friend.profile_custom_icon"], :custom_user => :person 2 track_actions :new_friendship, :after_create, :keep_params => ["friend.name", "friend.url", "friend.profile_custom_icon"], :custom_user => :person
3 - 3 +
  4 + extend CacheCounterHelper
  5 +
4 belongs_to :person, :foreign_key => :person_id 6 belongs_to :person, :foreign_key => :person_id
5 belongs_to :friend, :class_name => 'Person', :foreign_key => 'friend_id' 7 belongs_to :friend, :class_name => 'Person', :foreign_key => 'friend_id'
  8 +
  9 + after_create do |friendship|
  10 + update_cache_counter(:friends_count, friendship.person, 1)
  11 + update_cache_counter(:friends_count, friendship.friend, 1)
  12 + end
  13 +
  14 + after_destroy do |friendship|
  15 + update_cache_counter(:friends_count, friendship.person, -1)
  16 + update_cache_counter(:friends_count, friendship.friend, -1)
  17 + end
6 end 18 end
app/models/person.rb
@@ -66,11 +66,7 @@ class Person &lt; Profile @@ -66,11 +66,7 @@ class Person &lt; Profile
66 has_and_belongs_to_many :acepted_forums, :class_name => 'Forum', :join_table => 'terms_forum_people' 66 has_and_belongs_to_many :acepted_forums, :class_name => 'Forum', :join_table => 'terms_forum_people'
67 has_and_belongs_to_many :articles_with_access, :class_name => 'Article', :join_table => 'article_privacy_exceptions' 67 has_and_belongs_to_many :articles_with_access, :class_name => 'Article', :join_table => 'article_privacy_exceptions'
68 68
69 - named_scope :more_popular,  
70 - :select => "#{Profile.qualified_column_names}, count(friend_id) as total",  
71 - :group => Profile.qualified_column_names,  
72 - :joins => "LEFT OUTER JOIN friendships on profiles.id = friendships.person_id",  
73 - :order => "total DESC" 69 + named_scope :more_popular, :order => 'friends_count DESC'
74 70
75 named_scope :more_active, 71 named_scope :more_active,
76 :select => "#{Profile.qualified_column_names}, count(action_tracker.id) as total", 72 :select => "#{Profile.qualified_column_names}, count(action_tracker.id) as total",
db/migrate/20140312144156_define_initial_value_for_profiles_friends_count.rb 0 → 100644
@@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
  1 +class DefineInitialValueForProfilesFriendsCount < ActiveRecord::Migration
  2 + def self.up
  3 + friends_counts = execute("SELECT profiles.id, count(profiles.id) FROM profiles INNER JOIN friendships ON ( profiles.id = friendships.friend_id AND profiles.type = E'Person') GROUP BY profiles.id;")
  4 + friends_counts.each do |count|
  5 + execute("UPDATE profiles SET friends_count=#{count['count'].to_i} WHERE profiles.id=#{count['id']};")
  6 + end
  7 + end
  8 +
  9 + def self.down
  10 + execute("UPDATE profiles SET friends_count=0;")
  11 + end
  12 +end
test/unit/person_test.rb
@@ -647,11 +647,14 @@ class PersonTest &lt; ActiveSupport::TestCase @@ -647,11 +647,14 @@ class PersonTest &lt; ActiveSupport::TestCase
647 p2 = fast_create(Person) 647 p2 = fast_create(Person)
648 p3 = fast_create(Person) 648 p3 = fast_create(Person)
649 649
650 - p1.add_friend(p2)  
651 - p2.add_friend(p1)  
652 - p2.add_friend(p3)  
653 - assert_equal p2, Person.more_popular[0]  
654 - assert_equal p1, Person.more_popular[1] 650 + p1.friends_count = 1
  651 + p2.friends_count = 2
  652 + p3.friends_count = 3
  653 + p1.save!
  654 + p2.save!
  655 + p3.save!
  656 +
  657 + assert_order [p3, p2, p1], Person.more_popular
655 end 658 end
656 659
657 should 'list people that have no friends in more popular list' do 660 should 'list people that have no friends in more popular list' do
@@ -1363,7 +1366,7 @@ class PersonTest &lt; ActiveSupport::TestCase @@ -1363,7 +1366,7 @@ class PersonTest &lt; ActiveSupport::TestCase
1363 u = create_user('user'+i.to_s) 1366 u = create_user('user'+i.to_s)
1364 u.deactivate 1367 u.deactivate
1365 } 1368 }
1366 - assert_equal activated, Person.activated 1369 + assert_equivalent activated, Person.activated
1367 end 1370 end
1368 1371
1369 should 'deactivated named_scope return persons who are deactivated users' do 1372 should 'deactivated named_scope return persons who are deactivated users' do
@@ -1413,4 +1416,31 @@ class PersonTest &lt; ActiveSupport::TestCase @@ -1413,4 +1416,31 @@ class PersonTest &lt; ActiveSupport::TestCase
1413 person.reload 1416 person.reload
1414 assert_equal person.activities, [] 1417 assert_equal person.activities, []
1415 end 1418 end
  1419 +
  1420 + should 'increase friends_count on new friendship' do
  1421 + person = create_user('person').person
  1422 + friend = create_user('friend').person
  1423 + assert_difference person, :friends_count, 1 do
  1424 + assert_difference friend, :friends_count, 1 do
  1425 + person.add_friend(friend)
  1426 + friend.reload
  1427 + end
  1428 + person.reload
  1429 + end
  1430 + end
  1431 +
  1432 + should 'decrease friends_count on new friendship' do
  1433 + person = create_user('person').person
  1434 + friend = create_user('friend').person
  1435 + person.add_friend(friend)
  1436 + friend.reload
  1437 + person.reload
  1438 + assert_difference person, :friends_count, -1 do
  1439 + assert_difference friend, :friends_count, -1 do
  1440 + person.remove_friend(friend)
  1441 + friend.reload
  1442 + end
  1443 + person.reload
  1444 + end
  1445 + end
1416 end 1446 end