Skip to content

Instantly share code, notes, and snippets.

@rjungemann
Created January 20, 2011 23:49
Show Gist options
  • Save rjungemann/788963 to your computer and use it in GitHub Desktop.
Save rjungemann/788963 to your computer and use it in GitHub Desktop.
<!-- an example "friend label" -->
<div class='friend_container'>
<div class='friend_image'>
<div class='image'></div>
</div>
<div class='friend_name'>@adorableLizard</div>
<div class='friend_info'>Twitter User</div>
</div>
[
{
:is_dm => true,
:kind => "Twitter User",
:label => "<div class='friend_container'><div class='friend_image'><div class='image'></div></div><div class='friend_name'>@adorableLizard</div><div class='friend_info'>Twitter User</div></div>",
:value => "@adorableLizard"
}
]
class UiController < ApplicationController
def fb_autocomplete_friends
# phrase the user is searching for
term = params[:term]
raise "Term must be provided." unless term
# a sample list of friends
friends = [
{ :label => nil, :kind => "Facebook User", :value => "Karina Sasse" },
{ :label => nil, :kind => "Facebook User", :value => "Nannie Nell" },
{ :label => nil, :kind => "Facebook User", :value => "Tabatha Liss" },
{ :label => nil, :kind => "Facebook User", :value => "Althea Deeds" },
{ :label => nil, :kind => "Facebook User", :value => "Eve Howton" },
{ :label => nil, :kind => "Facebook User", :value => "Amie Rieves" },
{ :label => nil, :kind => "Facebook User", :value => "Lonnie Marrone" },
{ :label => nil, :kind => "Facebook User", :value => "Nelson Fitzhenry" },
{ :label => nil, :kind => "Facebook User", :value => "Max Pawlowicz" },
{ :label => nil, :kind => "Facebook User", :value => "Tameka Rutten" },
{ :label => nil, :kind => "Twitter User", :value => "@adorableLizard" },
{ :label => nil, :kind => "Twitter User", :value => "@bunny_cheese" },
{ :label => nil, :kind => "Twitter User", :value => "@combatIron" },
{ :label => nil, :kind => "Twitter User", :value => "@singinggurl" },
{ :label => nil, :kind => "Twitter User", :value => "@MoonDespair" },
{ :label => nil, :kind => "Twitter User", :value => "@EvilLover" },
{ :label => nil, :kind => "Twitter User", :value => "@LovelyPrincess" },
{ :label => nil, :kind => "Twitter User", :value => "@Grimlyadorable" },
{ :label => nil, :kind => "Twitter User", :value => "@JoyfulIronOptimist" },
{ :label => nil, :kind => "Twitter User", :value => "@WeaselSpring" }
]
# a list of matching friends, and a list of friends with their
# labels rendered
found_friends, rendered_friends = [], []
# a version of term which has "dm " stripped off the front if
# present
t = nil
# if the user is attempting a direct message, this will be true
is_dm = false
# set is_dm and t, if necessary
if(term.match(/^dm\s+\@/))
is_dm = true
t = term.gsub!(/^dm\s+/, '')
end
# filter for found friends, and render the dynamic fields for them
# using the "fb_autocomplete_friend" helper as contents of label
if(t != "@" && t.length > 0)
found_friends = friends.find_all do |f|
f[:value].downcase.match(/^#{t}/i)
end
rendered_friends = found_friends.map do |f|
f[:is_dm] = is_dm
f[:label] = render_to_string :partial => 'fb_autocomplete_friend',
:locals => { :name => f[:value], :term => term, :kind => f[:kind] }
f
end
end
render :json => rendered_friends
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment