Skip to content

Instantly share code, notes, and snippets.

@iMagesh
Created February 27, 2012 06:38
Show Gist options
  • Save iMagesh/1921949 to your computer and use it in GitHub Desktop.
Save iMagesh/1921949 to your computer and use it in GitHub Desktop.
A rough who to connect suggestion based on mutual interests, code needs to be optimized
def who_to_connect
@sent_invitations = current_user.sent_inv.where(:is_pending => 1).collect(&:friend_id)
@friends = friends_list
@friends = @friends + @sent_invitations
options = %w(majors careers)
@sug_list = []
options.each do |option|
@my_majors = eval("current_user.#{option}")
count = 1
hash = {}
@my_majors.each do |major|
hash[count] = major.users.collect(&:id)
count = count + 1
hash.count
end
# puts hash.keys
user_ids = hash.values.flatten.uniq
user_ids = user_ids - @friends.push(current_user.id)
user_ids.each do |user|
his_interests = eval("User.find(user).#{option}.collect {|m| m.id if @my_majors.include?(m)}")
his_interests = his_interests.uniq.compact
@user = User.find(user)
# user_role = User.find(user).role.name
if @user.role.name == ("College Student" || "Counselor")
role = "mentor"
else
role = "student"
end
@sug_list << {:user => user, :interests => his_interests.count, :user_type => role, :thumb => (@user.attachment.image.url(:thumb) if @user.attachment), :avatar => (@user.attachment.image.url(:thumb) if @user.attachment )}
end
end
@final_list = []
@sug_list.group_by {|row| row[:user] }.collect do |k,v|
keys = v.collect {|rec| rec.keys}.flatten.uniq
group = {}
keys.each do |key|
vals = v.collect { |rec| rec[key] }.uniq.compact
group[key] = vals.size > 1 ? vals.inject{|sum,x| sum + x } : vals.first
end
@final_list << group
end
@sug_list = @final_list.sort_by { |my_item| my_item[:interests] }.reverse
return @sug_list
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment