Commit d8fd6277689d5f91c826e2374ccc968aa8211af8

Authored by Rodrigo Souto
1 parent e7df3c52

checkpoint

app/models/profile_suggestion.rb
... ... @@ -66,8 +66,16 @@ class ProfileSuggestion < ActiveRecord::Base
66 66 # Number of communities in common
67 67 COMMON_COMMUNITIES = 2
68 68  
69   - # Number of friends in common
70   - COMMON_TAGS = 2
  69 + # Number of tags in common
  70 + COMMON_TAGS = 5
  71 +
  72 + def self.connections(rule)
  73 + rule.split(/.*_with_/).last + '_connections'
  74 + end
  75 +
  76 + def self.counter(rule)
  77 + rule.split(/.*_with_/).last + '_count'
  78 + end
71 79  
72 80 def self.register_suggestions(person, suggested_profiles, rule)
73 81 return if suggested_profiles.nil?
... ... @@ -75,10 +83,9 @@ class ProfileSuggestion < ActiveRecord::Base
75 83 suggested_profiles = suggested_profiles.where("profiles.id NOT IN (#{already_suggested_profiles})") if already_suggested_profiles.present?
76 84 suggested_profiles = suggested_profiles.limit(SUGGESTIONS_BY_RULE)
77 85 return if suggested_profiles.blank?
78   - counter = rule.split(/.*_with_/).last
79 86 suggested_profiles.each do |suggested_profile|
80 87 suggestion = person.profile_suggestions.find_or_initialize_by_suggestion_id(suggested_profile.id)
81   - suggestion.send(counter+'=', suggested_profile.common_count.to_i)
  88 + suggestion.send(counter(rule)+'=', suggested_profile.common_count.to_i)
82 89 suggestion.save!
83 90 end
84 91 end
... ... @@ -95,18 +102,43 @@ class ProfileSuggestion < ActiveRecord::Base
95 102 # need here.
96 103  
97 104 def self.people_with_common_friends(person)
98   - person_friends = person.friends.map(&:id)
99   - return if person_friends.blank?
100   - person.environment.people.
101   - select("profiles.*, suggestions.count AS common_count").
102   - joins("
103   - INNER JOIN (SELECT person_id, count(person_id) FROM
  105 + "SELECT person_id as profiles_id, array_agg(friend_id) as common_friends_connections, count(person_id) as common_friends_count FROM
104 106 friendships WHERE friend_id IN (#{person_friends.join(',')}) AND
105 107 person_id NOT IN (#{(person_friends << person.id).join(',')})
106 108 GROUP BY person_id
107   - HAVING count(person_id) >= #{COMMON_FRIENDS}) AS suggestions
108   - ON profiles.id = suggestions.person_id")
109   - end
  109 + HAVING count(person_id) >= #{COMMON_FRIENDS}"
  110 + end
  111 +
  112 + def self.all_suggestions(person)
  113 + select_string = "profiles.*, " + RULES.map { rule| "suggestions.#{counter(rule)} as #{counter(rule)}, suggestions.#{connections(rule)} as #{connections(rule)}" }.join(',')
  114 + suggestions_join = RULES.map.with_index do |rule, i|
  115 + if i == 0
  116 + "(#{self.send(rule, person)}) AS #{rule}"
  117 + else
  118 + previous_rule = RULES[i-1]
  119 + "LEFT OUTER JOIN (#{self.send(rule, person)}) AS #{rule} ON #{previous_rule}.profiles_id = #{rule}.profiles_id"
  120 + end
  121 + end.join(' ')
  122 + join_string = "INNER JOIN (SELECT * FROM #{suggestions_join}) AS suggestions ON profiles.id = suggestions.profiles_id"
  123 + person.environment.profiles.
  124 + select(select_string).
  125 + joins(join_string)
  126 +
  127 + end
  128 +
  129 +# def self.people_with_common_friends(person)
  130 +# person_friends = person.friends.map(&:id)
  131 +# return if person_friends.blank?
  132 +# person.environment.people.
  133 +# select("profiles.*, suggestions.count as common_count, suggestions.array_agg as connections").
  134 +# joins("
  135 +# INNER JOIN (SELECT person_id, array_agg(friend_id), count(person_id) FROM
  136 +# friendships WHERE friend_id IN (#{person_friends.join(',')}) AND
  137 +# person_id NOT IN (#{(person_friends << person.id).join(',')})
  138 +# GROUP BY person_id
  139 +# HAVING count(person_id) >= #{COMMON_FRIENDS}) AS suggestions
  140 +# ON profiles.id = suggestions.person_id")
  141 +# end
110 142  
111 143 def self.people_with_common_communities(person)
112 144 person_communities = person.communities.map(&:id)
... ...
app/views/profile/_upload_image.html.erb
... ... @@ -10,5 +10,5 @@
10 10 </div>
11 11 </div>
12 12 </div>
13   -<div title='<%= activity.target.class.short_description %>' class='profile-activity-icon icon-new icon-newgallery'></div>
  13 +<div title='<%#= activity.target.class.short_description %>' class='profile-activity-icon icon-new icon-newgallery'></div>
14 14 <br/>
... ...
db/migrate/20140720144212_create_profile_suggestions.rb
... ... @@ -6,10 +6,12 @@ class CreateProfileSuggestions &lt; ActiveRecord::Migration
6 6 t.string :suggestion_type
7 7 t.text :categories
8 8 t.boolean :enabled, :default => true
  9 + t.float :score
9 10  
10 11 t.timestamps
11 12 end
12 13 add_index :profile_suggestions, :person_id
13 14 add_index :profile_suggestions, :suggestion_id
  15 + add_index :profile_suggestions, :score
14 16 end
15 17 end
... ...
db/schema.rb
... ... @@ -11,7 +11,7 @@
11 11 #
12 12 # It's strongly recommended to check this file into your version control system.
13 13  
14   -ActiveRecord::Schema.define(:version => 20140720144212) do
  14 +ActiveRecord::Schema.define(:version => 20140805205626) do
15 15  
16 16 create_table "abuse_reports", :force => true do |t|
17 17 t.integer "reporter_id"
... ... @@ -452,11 +452,13 @@ ActiveRecord::Schema.define(:version =&gt; 20140720144212) do
452 452 t.string "suggestion_type"
453 453 t.text "categories"
454 454 t.boolean "enabled", :default => true
  455 + t.float "score"
455 456 t.datetime "created_at", :null => false
456 457 t.datetime "updated_at", :null => false
457 458 end
458 459  
459 460 add_index "profile_suggestions", ["person_id"], :name => "index_profile_suggestions_on_person_id"
  461 + add_index "profile_suggestions", ["score"], :name => "index_profile_suggestions_on_score"
460 462 add_index "profile_suggestions", ["suggestion_id"], :name => "index_profile_suggestions_on_suggestion_id"
461 463  
462 464 create_table "profiles", :force => true do |t|
... ...