Skip to content

Instantly share code, notes, and snippets.

@mrdanadams
Created March 28, 2012 20:55
Show Gist options
  • Select an option

  • Save mrdanadams/2230436 to your computer and use it in GitHub Desktop.

Select an option

Save mrdanadams/2230436 to your computer and use it in GitHub Desktop.

Revisions

  1. mrdanadams created this gist Mar 28, 2012.
    17 changes: 17 additions & 0 deletions controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    ids = current_user.facebook_friends if current_user
    @search = Profile.solr_search do
    keywords params[:q] do
    boost_fields name: 10.0, user: 4.0
    end

    unless ids.blank?
    adjust_solr_params do |params|
    # See "bq (boost query)" in http://wiki.apache.org/solr/DisMaxQParserPlugin
    params[:bq] = "facebook_id_s:(#{ids.join(' OR ')})^10"
    end
    end

    paginate page: params[:page], per_page: page_size
    end

    @results = @search.results
    9 changes: 9 additions & 0 deletions model.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    searchable do
    # some normal fields to search
    text :name, :description

    # index the ids as an array of strings (not text) as we want exact match, no stemming, etc
    string :facebook_id do
    user.facebook_id
    end
    end
    22 changes: 22 additions & 0 deletions user.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    # Returns a user associated with a facebook account.
    def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
    data = access_token.extra.raw_info
    if user = User.where(:email => data.email).first
    user
    else # Create a user with a stub password.
    user = User.new email: data.email, first_name: data.first_name, last_name: data.last_name, password: Devise.friendly_token[0,20]

    facebook_id = data.id
    token = access_token.credentials.token

    user.facebook_info = { 'id' => facebook_id, 'token' => token }

    # in the future we could save the user and get the friends in a background worker if it's too slow
    fb_user = FbGraph::User.fetch facebook_id, access_token: token
    user.facebook_friends = fb_user.friends.map &:identifier

    user.save!
    user.confirm!
    user
    end
    end