Permalink
Browse files

updated FB connect API to use latest

  • Loading branch information...
1 parent 1c8dccd commit eb6d92c9c2677e11976fad487c9d01e6a264d1c3 @kristianmandrup committed Apr 3, 2012
Showing with 45 additions and 15 deletions.
  1. +1 −1 VERSION
  2. +38 −8 lib/facebook-social_plugins/script_helper.rb
  3. +6 −6 spec/facebook-social_plugins/script_helper_spec.rb
View
2 VERSION
@@ -1 +1 @@
-0.1.2
+0.1.3
View
46 lib/facebook-social_plugins/script_helper.rb
@@ -2,20 +2,38 @@ module FacebookSocialPlugins
module ScriptHelper
# can be used inside a js.erb file or similar
- def fb_login_and_reload options = {:ready => false, :selector => '#fb_login_and_reload'}
- selector = options[:selector] || '#fb_login_and_reload'
+ def fb_login_and_react options = {:ready => false, :selector => '#fb_login', :success => 'reload();', :failure => ''}
+ selector = options[:selector] || '#fb_login'
+ success = options[:success] || '// on success'
+ failure = options[:failure] || '// on failure'
script = %Q{$('#{selector}').click(function() {
- FB.Connect.requireSession(function() { reload(); }); return false;
- }
+ FB.login(function(response) {
+ if (response.authResponse) {
+ #{success}
+ } else {
+ #{failure}
+ }
+ });
+ return false;
+ }#{scope_permissions options[:scope]}
}
options[:ready] ? wrap_ready(script) : script
end
- def fb_logout_and_reload options = {:ready => false, :selector => '#fb_logout_and_reload'}
- selector = options[:selector] || '#fb_logout_and_reload'
+ def fb_logout_and_react options = {:ready => false, :selector => '#fb_logout', :success => 'reload();', :failure => ''}
+ selector = options[:selector] || '#fb_logout'
+ success = options[:success] || '// on success'
+ failure = options[:failure] || '// on failure'
script = %Q{$('#{selector}').click(function() {
- FB.Connect.logout(function() { reload(); }); return false;
- }
+ FB.login(function(response) {
+ if (response.authResponse) {
+ #{success}
+ } else {
+ #{failure}
+ }
+ });
+ return false;
+ }
}
options[:ready] ? wrap_ready(script) : script
end
@@ -62,6 +80,18 @@ def fb_channel_script locale = :en_US
protected
+ def scope_permissions scope
+ scopes = case scope
+ when Array
+ scope.join(',')
+ when String
+ scope
+ else
+ nil
+ end
+ scopes ? ", {scope: '#{scopes}'}" : ''
+ end
+
def wrap_ready script
%Q{$(function() {
#{script}
View
12 spec/facebook-social_plugins/script_helper_spec.rb
@@ -57,17 +57,17 @@
end
end
- describe '#fb_login_and_reload' do
+ describe '#fb_login_and_react' do
it 'should work' do
- output = fb_login_and_reload :ready => true, :selector => '#fb_login'
- output.should == "$(function() {\n\t\t$('#fb_login').click(function() { \n\t\tFB.Connect.requireSession(function() { reload(); }); return false;\n }\n\n\t}\n"
+ output = fb_login_and_react :ready => true, :selector => '#fb_login', :scope => 'email,user_likes'
+ output.should == "$(function() {\n\t\t$('#fb_login').click(function() { \n\t\tFB.login(function(response) { \n\t\t\tif (response.authResponse) {\n\t\t\t\t// on success\n\t\t\t} else {\n\t\t\t\t// on failure\n\t\t\t}\t\t\t\n\t\t}); \n\t\treturn false;\n }, {scope: 'email,user_likes'}\n\n\t}\n"
end
end
- describe '#fb_logout_and_reload' do
+ describe '#fb_logout_and_react' do
it 'should work' do
- output = fb_logout_and_reload(:ready => true)
- output.should == "$(function() {\n\t\t$('#fb_logout_and_reload').click(function() { \n\t\tFB.Connect.logout(function() { reload(); }); return false;\n }\n\n\t}\n"
+ output = fb_logout_and_react :ready => true, :failure => "console.log('failed FB logout');"
+ output.should == "$(function() {\n\t\t$('#fb_logout').click(function() { \n\t\tFB.login(function(response) { \n\t\t\tif (response.authResponse) {\n\t\t\t\t// on success\n\t\t\t} else {\n\t\t\t\tconsole.log('failed FB logout');\n\t\t\t}\t\t\t\n\t\t}); \n\t\treturn false;\n \t}\n\n\t}\n"
end
end

0 comments on commit eb6d92c

Please sign in to comment.