module FacebookSocialPlugins::Helper::Script
Public Instance Methods
fb_async_init_script(app_id, domain, options = {})
click to toggle source
app_id - facebook app id, a number/string, fx ‘753632322’ domain - fx www.example.com options - status, cookie, xfbml (true|false)
-
:channel => ‘channel.html’
# File lib/facebook-social_plugins/helper/script.rb, line 100 def fb_async_init_script app_id, domain, options = {} raise ArgumentError, "Not a valid Facebook App ID. It should be a 15 digit number, was: #{app_id}, length=#{app_id.size}" unless valid_facebook_id?(app_id) raise ArgumentError, "No domain name specified" if domain.blank? if options[:channel] || options[:channel_url] locale = options[:locale] || I18n.locale channel = options[:channel_url] || "assets/facebook_channel_#{locale}" channelAttrib = "channelUrl : '//#{domain}/#{channel}.html', // Channel File" else channelAttrib = '// No Facebook channel defined (use fx #fb_channel_script)' end %Q{ window.fbAsyncInit = function() { FB.init({ appId : '#{app_id}', // App ID #{channelAttrib} status : #{options[:status] || true}, // check login status cookie : #{options[:cookie] || true}, // enable cookies to allow the server to access the session xfbml : #{options[:xfbml] || true } // parse XFBML }); // Additional initialization code here }; } end
fb_channel_script(locale = :en_US)
click to toggle source
# File lib/facebook-social_plugins/helper/script.rb, line 131 def fb_channel_script locale = :en_US %Q{ (function(d){ var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;} js = d.createElement('script'); js.id = id; js.async = true; js.src = '#{all_script(locale)}'; d.getElementsByTagName('head')[0].appendChild(js); }(document)); } end
fb_login_click_react(options = {:selector => ' { || ... }
click to toggle source
can be used inside a js.erb file or similar
# File lib/facebook-social_plugins/helper/script.rb, line 5 def fb_login_click_react options = {:selector => '#fb_login'}, &block selector = options[:selector] || '#fb_login' block_content = yield if block on_success = options[:on_success] || block_content || '// on success' on_fail = options[:on_fail] || '// on failure' script = %Q{$('#{selector}').click(function() { #{fb_login_react(options, &block)} return false; } } options[:ready] ? wrap_ready(script) : script end
fb_login_react(options = {}) { || ... }
click to toggle source
# File lib/facebook-social_plugins/helper/script.rb, line 19 def fb_login_react options = {}, &block block_content = yield if block on_success = options[:on_success] || block_content || '// on success' on_fail = options[:on_fail] || '// on failure' script = %Q{FB.login(function(response) { if (response.authResponse) { #{on_success} } else { #{on_fail} } }#{scope_permissions options[:scope]}); } options[:ready] ? wrap_ready(script) : script end
fb_logout_click_react(options = {:selector => ' { || ... }
click to toggle source
# File lib/facebook-social_plugins/helper/script.rb, line 36 def fb_logout_click_react options = {:selector => '#fb_logout'}, &block selector = options[:selector] || '#fb_logout' block_content = yield if block on_done = options[:on_done] || block_content || '// on done' script = %Q{$('#{selector}').click(function() { #{fb_logout_react(:on_done => on_done)} return false; } } options[:ready] ? wrap_ready(script) : script end
fb_logout_react(options = {}) { || ... }
click to toggle source
# File lib/facebook-social_plugins/helper/script.rb, line 48 def fb_logout_react options = {}, &block block_content = yield if block on_done = options[:on_done] || block_content || '// on done' script = %Q{FB.logout(function(response) { #{on_done} }); } options[:ready] ? wrap_ready(script) : script end
fb_onlogin_react(options = {}) { || ... }
click to toggle source
# File lib/facebook-social_plugins/helper/script.rb, line 85 def fb_onlogin_react options = {}, &block block_content = yield if block reaction = options[:reaction] || block_content || ' // on login' script = %Q{FB.Event.subscribe("auth.login", function() { #{reaction} }); } options[:ready] ? wrap_ready(script) : script end
fb_onlogin_redirect_to(path, options = {})
click to toggle source
# File lib/facebook-social_plugins/helper/script.rb, line 77 def fb_onlogin_redirect_to path, options = {} script = %Q{FB.Event.subscribe("auth.login", function() { window.location = '#{path}' }); } options[:ready] ? wrap_ready(script) : script end
fb_onlogout_react(options = {}) { || ... }
click to toggle source
# File lib/facebook-social_plugins/helper/script.rb, line 67 def fb_onlogout_react options = {}, &block block_content = yield if block reaction = options[:reaction] || block_content || ' // on logout' script = %Q{FB.Event.subscribe("auth.logout", function() { #{reaction} }); } options[:ready] ? wrap_ready(script) : script end
fb_onlogout_redirect_to(path, options = {})
click to toggle source
# File lib/facebook-social_plugins/helper/script.rb, line 59 def fb_onlogout_redirect_to path, options = {} script = %Q{FB.Event.subscribe("auth.logout", function() { window.location = '#{path}' }); } options[:ready] ? wrap_ready(script) : script end
valid_facebook_id?(id)
click to toggle source
# File lib/facebook-social_plugins/helper/script.rb, line 126 def valid_facebook_id? id raise ArgumentError, "No Facebook app id specified" if id.blank? id = /\d{15}/ end
Protected Instance Methods
all_script(locale = :en_US)
click to toggle source
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 supported locale code when loading the SDK. For example, if your site is in Spanish use es_LA
# File lib/facebook-social_plugins/helper/script.rb, line 172 def all_script locale = :en_US "//connect.facebook.net/#{locale}/all.js" end
scope_permissions(scope)
click to toggle source
# File lib/facebook-social_plugins/helper/script.rb, line 146 def scope_permissions scope scopes = case scope when Array scope.join(',') when String scope else nil end scopes ? ", {scope: '#{scopes}'}" : '' end
wrap_ready(script)
click to toggle source
# File lib/facebook-social_plugins/helper/script.rb, line 158 def wrap_ready script %Q{$(function() { #{script} } } end