diff --git a/app/models/profile_suggestion.rb b/app/models/profile_suggestion.rb index 8964097..dfd504c 100644 --- a/app/models/profile_suggestion.rb +++ b/app/models/profile_suggestion.rb @@ -31,7 +31,7 @@ class ProfileSuggestion < ActiveRecord::Base end RULES = %w[ - friends_of_friends_with_common_friends + friends_of_friends people_with_common_communities people_with_common_tags communities_with_common_friends @@ -53,7 +53,7 @@ class ProfileSuggestion < ActiveRecord::Base # Number of friends in common COMMON_TAGS = 2 - def self.friends_of_friends_with_common_friends(person) + def self.friends_of_friends(person) person_attempts = 0 person_friends = person.friends person_friends.each do |friend| @@ -63,7 +63,9 @@ class ProfileSuggestion < ActiveRecord::Base unless friend_of_friend == person || friend_of_friend.is_a_friend?(person) || person.already_request_friendship?(friend_of_friend) common_friends = friend_of_friend.friends & person_friends if common_friends.size >= COMMON_FRIENDS - person.profile_suggestions.create(:suggestion => friend_of_friend, :common_friends => common_friends.size) + suggestion = person.profile_suggestions.find_or_initialize_by_suggestion_id(friend_of_friend.id) + suggestion.common_friends = common_friends.size + suggestion.save end end end @@ -80,7 +82,9 @@ class ProfileSuggestion < ActiveRecord::Base unless member == person || member.is_a_friend?(person) || person.already_request_friendship?(member) common_communities = person_communities & member.communities if common_communities.size >= COMMON_COMMUNITIES - person.profile_suggestions.create(:suggestion => member, :common_communities => common_communities.size) + suggestion = person.profile_suggestions.find_or_initialize_by_suggestion_id(member.id) + suggestion.common_communities = common_communities.size + suggestion.save end end end @@ -99,7 +103,9 @@ class ProfileSuggestion < ActiveRecord::Base unless author.nil? || author == person || author.is_a_friend?(person) || person.already_request_friendship?(author) common_tags = tags & author.article_tags.keys if common_tags.size >= COMMON_TAGS - person.profile_suggestions.create(:suggestion => author, :common_tags => common_tags.size) + suggestion = person.profile_suggestions.find_or_initialize_by_suggestion_id(author.id) + suggestion.common_tags = common_tags.size + suggestion.save end end end @@ -116,7 +122,9 @@ class ProfileSuggestion < ActiveRecord::Base unless person.is_member_of?(community) || community.already_request_membership?(person) common_friends = community.members & person.friends if common_friends.size >= COMMON_FRIENDS - person.profile_suggestions.create(:suggestion => community, :common_friends => common_friends.size) + suggestion = person.profile_suggestions.find_or_initialize_by_suggestion_id(community.id) + suggestion.common_friends = common_friends.size + suggestion.save end end end @@ -135,7 +143,9 @@ class ProfileSuggestion < ActiveRecord::Base unless !profile.community? || person.is_member_of?(profile) || profile.already_request_membership?(person) common_tags = tags & profile.article_tags.keys if common_tags.size >= COMMON_TAGS - person.profile_suggestions.create(:suggestion => profile, :common_tags => common_tags.size) + suggestion = person.profile_suggestions.find_or_initialize_by_suggestion_id(profile.id) + suggestion.common_tags = common_tags.size + suggestion.save end end end diff --git a/test/unit/profile_suggestion_test.rb b/test/unit/profile_suggestion_test.rb index fbe2d8f..14aee8d 100644 --- a/test/unit/profile_suggestion_test.rb +++ b/test/unit/profile_suggestion_test.rb @@ -3,19 +3,19 @@ require File.dirname(__FILE__) + '/../test_helper' class ProfileSuggestionTest < ActiveSupport::TestCase - should 'save the profile class' do - person = create_user('person').person - suggested_community = fast_create(Community) + def setup + @person = create_user('test_user').person + @community = fast_create(Community) + end + attr_reader :person, :community - suggestion = ProfileSuggestion.create(:person => person, :suggestion => suggested_community) + should 'save the profile class' do + suggestion = ProfileSuggestion.create(:person => person, :suggestion => community) assert_equal 'Community', suggestion.suggestion_type end should 'only accept pre-defined categories' do - person = create_user('person').person - suggested_community = fast_create(Community) - - suggestion = ProfileSuggestion.new(:person => person, :suggestion => suggested_community) + suggestion = ProfileSuggestion.new(:person => person, :suggestion => community) suggestion.categories = {:unexistent => 1} suggestion.valid? @@ -23,25 +23,99 @@ class ProfileSuggestionTest < ActiveSupport::TestCase end should 'disable a suggestion' do - person = create_user('person').person - suggested_community = fast_create(Community) - - suggestion = ProfileSuggestion.create(:person => person, :suggestion => suggested_community) + suggestion = ProfileSuggestion.create(:person => person, :suggestion => community) suggestion.disable assert_equal false, ProfileSuggestion.find(suggestion.id).enabled? end should 'not suggest the same community twice' do - person = create_user('person').person - suggested_community = fast_create(Community) - - ProfileSuggestion.create(:person => person, :suggestion => suggested_community) + ProfileSuggestion.create(:person => person, :suggestion => community) - repeated_suggestion = ProfileSuggestion.new(:person => person, :suggestion => suggested_community) + repeated_suggestion = ProfileSuggestion.new(:person => person, :suggestion => community) repeated_suggestion.valid? assert_equal true, repeated_suggestion.errors[:suggestion_id.to_s].present? end + should 'update existing person suggestion when the number of common friends increase' do + suggested_person = create_user('test_user').person + suggestion = ProfileSuggestion.create(:person => person, :suggestion => suggested_person, :common_friends => 2) + + friend = create_user('friend').person + friend2 = create_user('friend2').person + friend3 = create_user('friend2').person + person.add_friend friend + person.add_friend friend2 + person.add_friend friend3 + + friend.add_friend suggested_person + + suggested_person.add_friend friend + suggested_person.add_friend friend2 + suggested_person.add_friend friend3 + + assert_difference 'ProfileSuggestion.find(suggestion.id).common_friends', 1 do + ProfileSuggestion.friends_of_friends(person) + end + end + + should 'update existing person suggestion when the number of common communities increase' do + suggested_person = create_user('test_user').person + suggestion = ProfileSuggestion.create(:person => person, :suggestion => suggested_person, :common_communities => 1) + + community.add_member person + community.add_member suggested_person + + community2 = fast_create(Community) + community2.add_member person + community2.add_member suggested_person + + assert_difference 'ProfileSuggestion.find(suggestion.id).common_communities', 1 do + ProfileSuggestion.people_with_common_communities(person) + end + end + + should 'update existing person suggestion when the number of common tags increase' do + suggested_person = create_user('test_user').person + suggestion = ProfileSuggestion.create(:person => person, :suggestion => suggested_person, :common_tags => 1) + + create(Article, :created_by => person, :profile => person, :tag_list => 'first-tag, second-tag, third-tag, fourth-tag') + create(Article, :created_by => suggested_person, :profile => suggested_person, :tag_list => 'first-tag, second-tag, third-tag') + + assert_difference 'ProfileSuggestion.find(suggestion.id).common_tags', 2 do + ProfileSuggestion.people_with_common_tags(person) + end + end + + should 'update existing community suggestion when the number of common friends increase' do + suggestion = ProfileSuggestion.create(:person => person, :suggestion => community, :common_friends => 1) + + member1 = create_user('member1').person + member2 = create_user('member2').person + + person.add_friend member1 + person.add_friend member2 + + community.add_member member1 + community.add_member member2 + + assert_difference 'ProfileSuggestion.find(suggestion.id).common_friends', 1 do + ProfileSuggestion.communities_with_common_friends(person) + end + + end + + should 'update existing community suggestion when the number of common tags increase' do + other_person = create_user('test_user').person + + suggestion = ProfileSuggestion.create(:person => person, :suggestion => community, :common_tags => 1) + + create(Article, :created_by => person, :profile => person, :tag_list => 'first-tag, second-tag, third-tag, fourth-tag') + create(Article, :created_by => other_person, :profile => community, :tag_list => 'first-tag, second-tag, third-tag') + + assert_difference 'ProfileSuggestion.find(suggestion.id).common_tags', 2 do + ProfileSuggestion.communities_with_common_tags(person) + end + end end diff --git a/test/unit/profile_suggestions_job_test.rb b/test/unit/profile_suggestions_job_test.rb index cacc992..44d7dd2 100644 --- a/test/unit/profile_suggestions_job_test.rb +++ b/test/unit/profile_suggestions_job_test.rb @@ -6,26 +6,15 @@ class ProfileSuggestionsJobTest < ActiveSupport::TestCase person = create_user('person').person friend = create_user('friend').person friend2 = create_user('friend2').person - friend3 = create_user('friend3').person person.add_friend friend - friend.add_friend person - person.add_friend friend2 - friend2.add_friend person - - person.add_friend friend3 - friend3.add_friend person friend_of_friend = create_user('friend_of_friend').person friend.add_friend friend_of_friend - friend_of_friend.add_friend friend + friend_of_friend.add_friend friend friend_of_friend.add_friend friend2 - friend2.add_friend friend_of_friend - - friend_of_friend.add_friend friend3 - friend3.add_friend friend_of_friend job = ProfileSuggestionsJob.new(person.id) assert_difference 'ProfileSuggestion.count', 1 do -- libgit2 0.21.2