Skip to content

Instantly share code, notes, and snippets.

@lucasts
Created March 16, 2011 06:37
Show Gist options
  • Save lucasts/872103 to your computer and use it in GitHub Desktop.
Save lucasts/872103 to your computer and use it in GitHub Desktop.
just a challenge trying to make devise, rails3, fbgraph working together
# (...) some requires not important to our context
module FBGraph
@config = {}
class << self
def load_config(yaml_file)
return false unless File.exist?(yaml_file)
cfg = YAML::load(File.open(yaml_file))
if defined? Rails
cfg = cfg[Rails.env]
end
cfg
end
def config
return @config if @config.any?
load_config(File.join(Rails.root , 'config' , 'facebook.yml')).freeze
end
end
end

Everything starts from some devise extensions not working with rails 3.

I tried one(I didn't remember exactly which one) that claims to work with Rails 3, but end up not working with Ruby 1.9x :(

After some research, I found https://github.com/mooktakim/devise_oauth2_facebook that seems simple and a great start

As I just wrote, it was simple to install,... routes working...everything fine until I click to test de facebook login.

I fell into this: NoMethodError (undefined method []' for false:FalseClass): fbgraph (1.7.1) lib/fbgraph/authorization.rb:10:in authorize_url' devise_oauth2_facebook (0.1.50) app/controllers/devise/facebook_consumer_controller.rb:7:in auth' actionpack (3.0.5) lib/action_controller/metal/implicit_render.rb:4:in send_action'

Rails, thanks for the backtrace, lets see what happens.

Looking into facebook_consumer_controller.rb, auth method is simple, just:

def auth
  url = send("#{resource_name}_fb_callback_url".to_sym)
  redirect_to facebook_client.authorization.authorize_url(:redirect_uri => url , :scope => Devise.facebook_permissions)
end

Clean, isn't? So, what the hell is happening here.

Just to be clear, I have to read fbgraph source code, that is a dependency for devise_oauth2_facebook gem, will be not a problem at all, but since one is based in another, I supposed that it works. That is not true.

Let's see fbgraph/authorization.rb and it's authorize_url method

def authorize_url(params = {})
  params = { :redirect_uri => FBGraph.config[:canvas_url] }.merge(params)
  @client.oauth_client.web_server.authorize_url(params)
end

just another short method, thanks god. ok, we do a merge params here, usually stuff ... setting redirect url, with a fancy config call. ahhh... config call, bitch.

As the error message told us, the problem is that line, line 10.

Sad things... sad things...

Open another file: fbgraph.rb

the file is attached to this gist, take a look, I need to paste it fully.

I'll not be funny to say that, but even the file looking simple to read, I read it two times:

  • first: hum... load config, ok, the problem must be in another file...
  • 1min later, second read: oww...wait...I MUST have a config/facebook.yml in my Rails project, sh**

Ok, I made a complaint here and there, but it was very fun to dig a little :)

TODO: find better option to fix that in projects(and send a patch):

  • do a little patch to devise_oauth2_facebook, avoiding that
  • do a little patch to fbgraph, making config not so attached to a config file
  • monkey patch? NOOOO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment