Commit 159f970193d94ea5cc1cb63a15aea59ea35a9b41
1 parent
40df430a
Exists in
master
and in
29 other branches
Update of suggestions when the relation change
When the number of a category increases or a new category is verified, the suggestion is updated with the new values (ActionItem3234)
Showing
3 changed files
with
109 additions
and
36 deletions
Show diff stats
app/models/profile_suggestion.rb
... | ... | @@ -31,7 +31,7 @@ class ProfileSuggestion < ActiveRecord::Base |
31 | 31 | end |
32 | 32 | |
33 | 33 | RULES = %w[ |
34 | - friends_of_friends_with_common_friends | |
34 | + friends_of_friends | |
35 | 35 | people_with_common_communities |
36 | 36 | people_with_common_tags |
37 | 37 | communities_with_common_friends |
... | ... | @@ -53,7 +53,7 @@ class ProfileSuggestion < ActiveRecord::Base |
53 | 53 | # Number of friends in common |
54 | 54 | COMMON_TAGS = 2 |
55 | 55 | |
56 | - def self.friends_of_friends_with_common_friends(person) | |
56 | + def self.friends_of_friends(person) | |
57 | 57 | person_attempts = 0 |
58 | 58 | person_friends = person.friends |
59 | 59 | person_friends.each do |friend| |
... | ... | @@ -63,7 +63,9 @@ class ProfileSuggestion < ActiveRecord::Base |
63 | 63 | unless friend_of_friend == person || friend_of_friend.is_a_friend?(person) || person.already_request_friendship?(friend_of_friend) |
64 | 64 | common_friends = friend_of_friend.friends & person_friends |
65 | 65 | if common_friends.size >= COMMON_FRIENDS |
66 | - person.profile_suggestions.create(:suggestion => friend_of_friend, :common_friends => common_friends.size) | |
66 | + suggestion = person.profile_suggestions.find_or_initialize_by_suggestion_id(friend_of_friend.id) | |
67 | + suggestion.common_friends = common_friends.size | |
68 | + suggestion.save | |
67 | 69 | end |
68 | 70 | end |
69 | 71 | end |
... | ... | @@ -80,7 +82,9 @@ class ProfileSuggestion < ActiveRecord::Base |
80 | 82 | unless member == person || member.is_a_friend?(person) || person.already_request_friendship?(member) |
81 | 83 | common_communities = person_communities & member.communities |
82 | 84 | if common_communities.size >= COMMON_COMMUNITIES |
83 | - person.profile_suggestions.create(:suggestion => member, :common_communities => common_communities.size) | |
85 | + suggestion = person.profile_suggestions.find_or_initialize_by_suggestion_id(member.id) | |
86 | + suggestion.common_communities = common_communities.size | |
87 | + suggestion.save | |
84 | 88 | end |
85 | 89 | end |
86 | 90 | end |
... | ... | @@ -99,7 +103,9 @@ class ProfileSuggestion < ActiveRecord::Base |
99 | 103 | unless author.nil? || author == person || author.is_a_friend?(person) || person.already_request_friendship?(author) |
100 | 104 | common_tags = tags & author.article_tags.keys |
101 | 105 | if common_tags.size >= COMMON_TAGS |
102 | - person.profile_suggestions.create(:suggestion => author, :common_tags => common_tags.size) | |
106 | + suggestion = person.profile_suggestions.find_or_initialize_by_suggestion_id(author.id) | |
107 | + suggestion.common_tags = common_tags.size | |
108 | + suggestion.save | |
103 | 109 | end |
104 | 110 | end |
105 | 111 | end |
... | ... | @@ -116,7 +122,9 @@ class ProfileSuggestion < ActiveRecord::Base |
116 | 122 | unless person.is_member_of?(community) || community.already_request_membership?(person) |
117 | 123 | common_friends = community.members & person.friends |
118 | 124 | if common_friends.size >= COMMON_FRIENDS |
119 | - person.profile_suggestions.create(:suggestion => community, :common_friends => common_friends.size) | |
125 | + suggestion = person.profile_suggestions.find_or_initialize_by_suggestion_id(community.id) | |
126 | + suggestion.common_friends = common_friends.size | |
127 | + suggestion.save | |
120 | 128 | end |
121 | 129 | end |
122 | 130 | end |
... | ... | @@ -135,7 +143,9 @@ class ProfileSuggestion < ActiveRecord::Base |
135 | 143 | unless !profile.community? || person.is_member_of?(profile) || profile.already_request_membership?(person) |
136 | 144 | common_tags = tags & profile.article_tags.keys |
137 | 145 | if common_tags.size >= COMMON_TAGS |
138 | - person.profile_suggestions.create(:suggestion => profile, :common_tags => common_tags.size) | |
146 | + suggestion = person.profile_suggestions.find_or_initialize_by_suggestion_id(profile.id) | |
147 | + suggestion.common_tags = common_tags.size | |
148 | + suggestion.save | |
139 | 149 | end |
140 | 150 | end |
141 | 151 | end | ... | ... |
test/unit/profile_suggestion_test.rb
... | ... | @@ -3,19 +3,19 @@ require File.dirname(__FILE__) + '/../test_helper' |
3 | 3 | |
4 | 4 | class ProfileSuggestionTest < ActiveSupport::TestCase |
5 | 5 | |
6 | - should 'save the profile class' do | |
7 | - person = create_user('person').person | |
8 | - suggested_community = fast_create(Community) | |
6 | + def setup | |
7 | + @person = create_user('test_user').person | |
8 | + @community = fast_create(Community) | |
9 | + end | |
10 | + attr_reader :person, :community | |
9 | 11 | |
10 | - suggestion = ProfileSuggestion.create(:person => person, :suggestion => suggested_community) | |
12 | + should 'save the profile class' do | |
13 | + suggestion = ProfileSuggestion.create(:person => person, :suggestion => community) | |
11 | 14 | assert_equal 'Community', suggestion.suggestion_type |
12 | 15 | end |
13 | 16 | |
14 | 17 | should 'only accept pre-defined categories' do |
15 | - person = create_user('person').person | |
16 | - suggested_community = fast_create(Community) | |
17 | - | |
18 | - suggestion = ProfileSuggestion.new(:person => person, :suggestion => suggested_community) | |
18 | + suggestion = ProfileSuggestion.new(:person => person, :suggestion => community) | |
19 | 19 | |
20 | 20 | suggestion.categories = {:unexistent => 1} |
21 | 21 | suggestion.valid? |
... | ... | @@ -23,25 +23,99 @@ class ProfileSuggestionTest < ActiveSupport::TestCase |
23 | 23 | end |
24 | 24 | |
25 | 25 | should 'disable a suggestion' do |
26 | - person = create_user('person').person | |
27 | - suggested_community = fast_create(Community) | |
28 | - | |
29 | - suggestion = ProfileSuggestion.create(:person => person, :suggestion => suggested_community) | |
26 | + suggestion = ProfileSuggestion.create(:person => person, :suggestion => community) | |
30 | 27 | |
31 | 28 | suggestion.disable |
32 | 29 | assert_equal false, ProfileSuggestion.find(suggestion.id).enabled? |
33 | 30 | end |
34 | 31 | |
35 | 32 | should 'not suggest the same community twice' do |
36 | - person = create_user('person').person | |
37 | - suggested_community = fast_create(Community) | |
38 | - | |
39 | - ProfileSuggestion.create(:person => person, :suggestion => suggested_community) | |
33 | + ProfileSuggestion.create(:person => person, :suggestion => community) | |
40 | 34 | |
41 | - repeated_suggestion = ProfileSuggestion.new(:person => person, :suggestion => suggested_community) | |
35 | + repeated_suggestion = ProfileSuggestion.new(:person => person, :suggestion => community) | |
42 | 36 | |
43 | 37 | repeated_suggestion.valid? |
44 | 38 | assert_equal true, repeated_suggestion.errors[:suggestion_id.to_s].present? |
45 | 39 | end |
46 | 40 | |
41 | + should 'update existing person suggestion when the number of common friends increase' do | |
42 | + suggested_person = create_user('test_user').person | |
43 | + suggestion = ProfileSuggestion.create(:person => person, :suggestion => suggested_person, :common_friends => 2) | |
44 | + | |
45 | + friend = create_user('friend').person | |
46 | + friend2 = create_user('friend2').person | |
47 | + friend3 = create_user('friend2').person | |
48 | + person.add_friend friend | |
49 | + person.add_friend friend2 | |
50 | + person.add_friend friend3 | |
51 | + | |
52 | + friend.add_friend suggested_person | |
53 | + | |
54 | + suggested_person.add_friend friend | |
55 | + suggested_person.add_friend friend2 | |
56 | + suggested_person.add_friend friend3 | |
57 | + | |
58 | + assert_difference 'ProfileSuggestion.find(suggestion.id).common_friends', 1 do | |
59 | + ProfileSuggestion.friends_of_friends(person) | |
60 | + end | |
61 | + end | |
62 | + | |
63 | + should 'update existing person suggestion when the number of common communities increase' do | |
64 | + suggested_person = create_user('test_user').person | |
65 | + suggestion = ProfileSuggestion.create(:person => person, :suggestion => suggested_person, :common_communities => 1) | |
66 | + | |
67 | + community.add_member person | |
68 | + community.add_member suggested_person | |
69 | + | |
70 | + community2 = fast_create(Community) | |
71 | + community2.add_member person | |
72 | + community2.add_member suggested_person | |
73 | + | |
74 | + assert_difference 'ProfileSuggestion.find(suggestion.id).common_communities', 1 do | |
75 | + ProfileSuggestion.people_with_common_communities(person) | |
76 | + end | |
77 | + end | |
78 | + | |
79 | + should 'update existing person suggestion when the number of common tags increase' do | |
80 | + suggested_person = create_user('test_user').person | |
81 | + suggestion = ProfileSuggestion.create(:person => person, :suggestion => suggested_person, :common_tags => 1) | |
82 | + | |
83 | + create(Article, :created_by => person, :profile => person, :tag_list => 'first-tag, second-tag, third-tag, fourth-tag') | |
84 | + create(Article, :created_by => suggested_person, :profile => suggested_person, :tag_list => 'first-tag, second-tag, third-tag') | |
85 | + | |
86 | + assert_difference 'ProfileSuggestion.find(suggestion.id).common_tags', 2 do | |
87 | + ProfileSuggestion.people_with_common_tags(person) | |
88 | + end | |
89 | + end | |
90 | + | |
91 | + should 'update existing community suggestion when the number of common friends increase' do | |
92 | + suggestion = ProfileSuggestion.create(:person => person, :suggestion => community, :common_friends => 1) | |
93 | + | |
94 | + member1 = create_user('member1').person | |
95 | + member2 = create_user('member2').person | |
96 | + | |
97 | + person.add_friend member1 | |
98 | + person.add_friend member2 | |
99 | + | |
100 | + community.add_member member1 | |
101 | + community.add_member member2 | |
102 | + | |
103 | + assert_difference 'ProfileSuggestion.find(suggestion.id).common_friends', 1 do | |
104 | + ProfileSuggestion.communities_with_common_friends(person) | |
105 | + end | |
106 | + | |
107 | + end | |
108 | + | |
109 | + should 'update existing community suggestion when the number of common tags increase' do | |
110 | + other_person = create_user('test_user').person | |
111 | + | |
112 | + suggestion = ProfileSuggestion.create(:person => person, :suggestion => community, :common_tags => 1) | |
113 | + | |
114 | + create(Article, :created_by => person, :profile => person, :tag_list => 'first-tag, second-tag, third-tag, fourth-tag') | |
115 | + create(Article, :created_by => other_person, :profile => community, :tag_list => 'first-tag, second-tag, third-tag') | |
116 | + | |
117 | + assert_difference 'ProfileSuggestion.find(suggestion.id).common_tags', 2 do | |
118 | + ProfileSuggestion.communities_with_common_tags(person) | |
119 | + end | |
120 | + end | |
47 | 121 | end | ... | ... |
test/unit/profile_suggestions_job_test.rb
... | ... | @@ -6,26 +6,15 @@ class ProfileSuggestionsJobTest < ActiveSupport::TestCase |
6 | 6 | person = create_user('person').person |
7 | 7 | friend = create_user('friend').person |
8 | 8 | friend2 = create_user('friend2').person |
9 | - friend3 = create_user('friend3').person | |
10 | 9 | |
11 | 10 | person.add_friend friend |
12 | - friend.add_friend person | |
13 | - | |
14 | 11 | person.add_friend friend2 |
15 | - friend2.add_friend person | |
16 | - | |
17 | - person.add_friend friend3 | |
18 | - friend3.add_friend person | |
19 | 12 | |
20 | 13 | friend_of_friend = create_user('friend_of_friend').person |
21 | 14 | friend.add_friend friend_of_friend |
22 | - friend_of_friend.add_friend friend | |
23 | 15 | |
16 | + friend_of_friend.add_friend friend | |
24 | 17 | friend_of_friend.add_friend friend2 |
25 | - friend2.add_friend friend_of_friend | |
26 | - | |
27 | - friend_of_friend.add_friend friend3 | |
28 | - friend3.add_friend friend_of_friend | |
29 | 18 | |
30 | 19 | job = ProfileSuggestionsJob.new(person.id) |
31 | 20 | assert_difference 'ProfileSuggestion.count', 1 do | ... | ... |