Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

improved script helpers for login and logout and did specs

  • Loading branch information...
commit cf2d00cfaf7350b538d7f27a1333635668da240b 1 parent 83b0f5f
@kristianmandrup authored
View
21 README.rdoc
@@ -92,6 +92,17 @@ The JavaScript SDK is available in all locales that are supported by Facebook. T
}(document));
</script>
+=== Configuration scripts*
+
+* fb_async_init_script(app_id, domain)
+* fb_channel_script(locale = :en_US)
+
+=== Login and Logout
+
+* fb_logout_and_reload(options = {:ready => false, :selector => '#fb_logout_and_reload'})
+* fb_login_and_reload(options = {:ready => false, :selector => '#fb_login_and_reload'})
+* fb_logout_and_redirect_to(path, options = {:ready => false})
+
== Open Graph Meta helper
Convenience method to generate all header/meta tags for Facebook Open Graph
@@ -150,7 +161,15 @@ View methods exposed:
* fb_send_button (options = {})
* fb_subscribe_button (options = {})
-Note: You don't have to worry about dashed or underscored properties. Conversions will be handled automatically. The Facebook Social plugins API is (sadly) not very consistent when it comes to attribute names.
+Note: You don't have to worry about dashed or underscored properties. Conversions will be handled automatically. The Facebook Social plugins API is (sadly) not very consistent when it comes to attribute names.
+
+Extras:
+
+* fb_logout_button(options = {}) # renders logout button (english only)
+
+Logout button options:
+html: (html options for anchor tag)
+size: 'small' or 'large' (size of logout button img)
=== Facebook root
View
39 lib/facebook-social_plugins/script_helper.rb
@@ -1,11 +1,39 @@
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'
+ script = %Q{$('#{selector}').click(function() {
+ FB.Connect.requireSession(function() { reload(); }); return false;
+ }
+}
+ 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'
+ script = %Q{$('#{selector}').click(function() {
+ FB.Connect.logout(function() { reload(); }); return false;
+ }
+}
+ options[:ready] ? wrap_ready(script) : script
+ end
+
+ def fb_logout_and_redirect_to path, options = {:ready => false}
+ script = %Q{FB.Event.subscribe("auth.logout", function() {
+ window.location = '#{path}'
+ });
+}
+ options[:ready] ? wrap_ready(script) : script
+ end
+
+
# app_id - facebook app id, a number/string, fx '753632322'
# domain - fx www.example.com
# options - status, cookie, xfbml (true|false)
# - :channel => 'channel.html'
- def async_init_script app_id, domain, options = {}
+ def fb_async_init_script app_id, domain, options = {}
%Q{
window.fbAsyncInit = function() {
FB.init({
@@ -21,7 +49,7 @@ def async_init_script app_id, domain, options = {}
}
end
- def facebook_script locale = :en_US
+ def fb_channel_script locale = :en_US
%Q{
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
@@ -34,6 +62,13 @@ def facebook_script locale = :en_US
protected
+ def wrap_ready script
+ %Q{$(function() {
+ #{script}
+ }
+}
+ end
+
# The JavaScript SDK is available in all locales that are supported by Facebook.
# This list of supported locales is available as an XML file.
# To change the locale of the SDK to match the locale of your site, change en_US to a
View
2  lib/facebook-social_plugins/social_plugin.rb
@@ -67,7 +67,7 @@ def plugin_class
end
def attributes
- @attributes ||= {}
+ @attributes ||= {:id => :string, :style => :string}
end
def valid? value, valid_type
View
10 lib/facebook-social_plugins/view_helper.rb
@@ -44,12 +44,14 @@ def fb_login_button options = {}
FacebookSocialPlugins::LoginButton.new(options).render
end
- def fb_logout &block
- content_tag :script, %Q{FB.Event.subscribe("auth.logout", function() { #{yield} });}
+ def fb_logout_button options = {}
+ content_tag :a, '', {:id => 'fb_logout_and_reload', :href => '#'}.merge(options[:html] || {}) do
+ content_tag :img, :id => 'fb_logout_image', :src => "http://static.ak.fbcdn.net/images/fbconnect/logout-buttons/logout_#{options[:size] || :small}.gif", :alt => "Facebook Logout"
+ end
end
- def fb_logout_and_redirect_to path
- content_tag :script, %Q{FB.Event.subscribe("auth.logout", function() {window.location = #{path} });}
+ def fb_logout &block
+ content_tag :script, %Q{FB.Event.subscribe("auth.logout", function() { #{yield} });}
end
def fb_recommendations_box options = {}
View
43 spec/facebook-social_plugins/script_helper_spec.rb
@@ -38,18 +38,43 @@
include ControllerTestHelpers,
FacebookSocialPlugins::ScriptHelper
- it "should create async init script" do
- output = async_init_script '123', 'www.example.com'
- output.should == async_script
+ describe '#async_init_script' do
+ it "should create async init script" do
+ output = fb_async_init_script '123', 'www.example.com'
+ output.should == async_script
+ end
end
- it "should create facebook script" do
- output = facebook_script
- output.should == fb_script
+ describe '#facebook_script' do
+ it "should create facebook script" do
+ output = fb_channel_script
+ output.should == fb_script
+ end
+
+ it "should create localized facebook script" do
+ output = fb_channel_script :es_LA
+ output.should == fb_script_es
+ end
+ end
+
+ describe '#fb_login_and_reload' 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"
+ end
+ end
+
+ describe '#fb_logout_and_reload' 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"
+ end
end
- it "should create localized facebook script" do
- output = facebook_script :es_LA
- output.should == fb_script_es
+ describe '#fb_logout_and_redirect_to' do
+ it 'should work' do
+ output = fb_logout_and_redirect_to('facebook/logout', :ready => true)
+ output.should == "$(function() {\n\t\tFB.Event.subscribe(\"auth.logout\", function() { \n\twindow.location = 'facebook/logout' \n\t}); \n\n\t}\n"
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.