Skip to content

Commit

Permalink
v.0.0.2-alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
Holek committed Feb 15, 2011
1 parent 8935900 commit f3b5861
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 35 deletions.
42 changes: 34 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
# Facebook Share

This gem will add an easy-to-use Facebook Share button feature to your Rails project.
This gem will add an easy-to-use Facebook Share button feature to your Rails project. This gem does not take care of authentication or authorization. It's only purpose is to bind Facebook Share button to anything you want.

Any public method will return just JavaScript code and nothing else.

## How To Install

This gem relies on jQuery, be sure to have it installed in your project. This gem does not depend on jquery-rails gem, because some projects use jQuery without it.
This gem relies on jQuery, be sure to have it installed in your project. It does not depend on jquery-rails gem, because some projects use jQuery without it.

gem install facebook_share

then add
## Code changes

If you don't have a Facebook Application for your project yet, [create one](http://www.facebook.com/developers/createapp.php).

Then add this to your ApplicationHelper

module ApplicationHelper
include FacebookShare

FacebookShare.default_facebook_share_options = {
:app_id => "YOUR_APP_ID",
:status => false,
:cookie => false,
:xfbml => false,

:selector => '.fb_share',
:locale => "en_US"
}
end

You can ommit *app_id* parameter, if you already have a Facebook Application initialized in your project.

Be sure you have <div id="fb-root"></div> in your application layout before you load the Facebook Connect JS

Default facebook Share options can be changed with the above code snippet
* *appid* - your Facebook application ID that will connect your site to Facebook.
* *status*. *cookie* and *xfbml* - as described at [FB.init JS SDK](http://developers.facebook.com/docs/reference/javascript/fb.init/)

include FacebookShare
* *locale* - Facebook locale code representations, ie. en_US, de_DE, pl_PL, etc. The full list of Facebook supported languages is available in http://www.facebook.com/translations/FacebookLocales.xml or at [Facebook Developer Wiki](http://fbdevwiki.com/wiki/Locales)

at the top of your ApplicationHelper

## Examples
## Usage

To be documented

## Note on Patches/Pull Requests

Expand All @@ -29,7 +55,7 @@ To be documented

## Copyright

Copyright (c) 2011 Mike Połtyn.
Copyright (c) 2011 Mike Połtyn. Originally build as part of work at [Railslove](http://railslove.com).

## License

Expand Down
76 changes: 50 additions & 26 deletions lib/facebook_share.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,73 @@
module FacebookShare

def facebook_share_once(app_id, params = {})
facebook_script_tags app_id, facebook_share('.fb_share', params)
INIT_PARAMS = %w(status cookie xfbml)
REMOVE_PARAMS = %w(app_id selector status cookie xfbml locale)

class << self
attr_accessor :default_facebook_share_options
end

def default_facebook_share_options
{:app_id => "0", :selector => ".fb_share", :url => request.url, :locale => "en_US", :display => "popup"}
#.merge(FacebookShare.default_facebook_share_options || {})
end

def facebook_share(selector, params = {})
available_params = {
:display => "popup"
}.merge(params)
def facebook_share_once(options = {})
facebook_script_tags options, facebook_share( options )
end

def facebook_share(options = {})
options = default_facebook_share_options.merge(options)
script = <<-JS
$("#{selector}").unbind("click.facebook_share").bind("click.facebook_share",function () {
FB.ui(
{
method: \'stream.publish\'
#{build_params(available_params)}
});
return false;
});
FB.ui({method: \'stream.publish\'#{build_params(options)}});
return false;
});
JS
script
end

def facebook_script_tags(app_id, initial_script = "")
def facebook_script_tags(options = {}, initial_script = "")
options = default_facebook_share_options.merge(options)
script = <<-JS
<script type="text/javascript" src="https://connect.facebook.net/de_DE/all.js"></script>
#{facebook_connect_js_tag(options)}
<script type="text/javascript">
/* <![CDATA[ */
FB.init({appId:"#{app_id}", xfbml : true});
#{facebook_init_script(options)}
#{initial_script}
/* ]]> */
</script>
JS
script
html_safe_string(script)
end

def facebook_connect_js_tag(options = {})
options = default_facebook_share_options.merge(options)
html_safe_string("<script type=\"text/javascript\" src=\"https://connect.facebook.net/#{default_facebook_share_options.merge(options)[:locale]}/all.js\"></script>")
end

private
def build_params(available_params)
script = ""
available_params.each do |key, value|
if value
value_sanitized = value.gsub(/"/, '\"')
script << ", #{key}: \"#{value_sanitized}\""
end
def facebook_init_script(options = {})
options = default_facebook_share_options.merge(options)
params = build_params options, true
html_safe_string("FB.init({appId:\"#{options[:app_id]}\"#{params}});")
end

def build_params(options, for_init = false)
script = ""
options.each do |key, value|
param_check = ( for_init ) ? INIT_PARAMS.include?(key) : !(REMOVE_PARAMS.include?(key))
# if it's for init script, include only status, cookie and xfbml
# if it's for stream.publish, include all except for initial
if value && param_check
value_sanitized = value.gsub(/"/, '\"')
script << ", #{key}: \"#{value_sanitized}\""
end
script
end
script
end

def html_safe_string(str)
@use_html_safe ||= "".respond_to?(:html_safe)
@use_html_safe ? str.html_safe : str
end
end
2 changes: 1 addition & 1 deletion lib/facebook_share/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module FacebookShare
VERSION = "0.0.1"
VERSION = "0.0.2"
end

0 comments on commit f3b5861

Please sign in to comment.