facebook_share 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- facebook_share (0.0.4)
4
+ facebook_share (0.0.5)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/README.md CHANGED
@@ -6,10 +6,14 @@ Any public method will return just JavaScript code and nothing else.
6
6
 
7
7
  ## How to install
8
8
 
9
- 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.
10
-
11
9
  gem install facebook_share
12
10
 
11
+ This gem supports jQuery and Dojo, be sure to have it installed in your project.
12
+
13
+ In case of jQuery, the gem does not depend on jquery-rails, because some projects use jQuery without it or expose jQuery function to another global variable or use [jQuery.noConflict](http://api.jquery.com/jQuery.noConflict).
14
+
15
+ Further information about choosing framework are described in next section.
16
+
13
17
  ## Code changes
14
18
 
15
19
  If you don't have a Facebook Application for your project yet, [create one](http://www.facebook.com/developers/createapp.php).
@@ -20,10 +24,13 @@ Then add this to your ApplicationHelper
20
24
  include FacebookShare
21
25
 
22
26
  FacebookShare.default_facebook_share_options = {
27
+ :framework => :jquery,
28
+ :jquery_function => "$",
29
+
23
30
  :app_id => "YOUR_APP_ID",
24
- :status => false,
25
- :cookie => false,
26
- :xfbml => false,
31
+ :status => "false",
32
+ :cookie => "false",
33
+ :xfbml => "false",
27
34
 
28
35
  :selector => '.fb_share',
29
36
  :locale => "en_US"
@@ -34,10 +41,12 @@ You can ommit *app_id* parameter, if you already have a Facebook Application ini
34
41
 
35
42
  Be sure you have <div id="fb-root"></div> in your application layout before you load the Facebook Connect JS
36
43
 
37
- Default facebook Share options can be changed with the above code snippet
44
+ Default facebook Share options can be changed with the above code snippet. The options can be also passed to any public method, so you don't have to rely on defaults at any given time.
38
45
 
39
- * *appid* - your Facebook application ID that will connect your site to Facebook
40
- * *status*. *cookie* and *xfbml* - as described at [FB.init JS SDK](http://developers.facebook.com/docs/reference/javascript/fb.init/)
46
+ * *framework* - choose a JavaScript framework to work with. For now just **:dojo** and **:jquery** are supported.
47
+ ** *jquery_function* - If you are using jQuery and mapped the jQuery function to a different variable, you can use that to pass a correct jQuery variable name, for example **$j**, **jQuery**, etc.
48
+ * *app_id* - your Facebook application ID that will connect your site to Facebook - as described at [FB.init JS SDK](http://developers.facebook.com/docs/reference/javascript/fb.init/)
49
+ * *status*, *cookie* and *xfbml* - as described at [FB.init JS SDK](http://developers.facebook.com/docs/reference/javascript/fb.init/)
41
50
  * *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). If your locale has both parts of the string the same, for example "de_DE", "pl_PL", since version 0.0.4 you can put just "de" or "pl", etc. The script **does not** check for validity of given locale.
42
51
  * *selector* - a selector to target Facebook share binding, ".fb_share" by default
43
52
  * any other parameter will be passed to Facebook's **[FB.ui](http://developers.facebook.com/docs/reference/javascript/fb.ui/)** function, so you can use whichever parameters you need, except for *method*, which defaults always to *publish.stream*
@@ -49,10 +58,14 @@ The simplest usage (given you specified your project's Facebook Application ID)
49
58
  <%= link_to 'Share on Facebook', '#', :class => "fb_share" %>
50
59
  <%= facebook_share_once %>
51
60
 
52
- That will produce a link "Share on Facebook" with a class of "fb_share" and a corresponding JavaScript script tags initializing Facebook app and sharing method bind to click on that link. By default gem passes ".fb_share" selector to jQuery.
61
+ That will produce a link "Share on Facebook" with a class of "fb_share" and a corresponding JavaScript script tags initializing Facebook app and sharing method bind to click on that link. By default gem passes ".fb_share" selector to the Javascript framework of your choice.
53
62
 
54
63
  ## Changelog
55
64
 
65
+ v0.0.5
66
+
67
+ * Added support for [Dojo framework](http://dojotoolkit.org/)
68
+
56
69
  v0.0.4
57
70
 
58
71
  * Locale guessing, if necessary
@@ -1,3 +1,3 @@
1
1
  module FacebookShare
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  module FacebookShare
2
2
  INIT_PARAMS = %w(status cookie xfbml)
3
- REMOVE_PARAMS = %w(app_id selector status cookie xfbml locale)
4
-
3
+ REMOVE_PARAMS = %w(app_id selector status cookie xfbml locale framework jquery_function)
4
+
5
5
  class << self
6
6
  attr_accessor :default_facebook_share_options
7
7
  end
@@ -15,6 +15,10 @@ module FacebookShare
15
15
  end
16
16
  {
17
17
  :app_id => "0",
18
+
19
+ :framework => :jquery,
20
+ :jquery_function => "$",
21
+
18
22
  :selector => ".fb_share",
19
23
  :link => link,
20
24
  :locale => "en_US",
@@ -26,14 +30,31 @@ module FacebookShare
26
30
  facebook_script_tags options, facebook_share( options )
27
31
  end
28
32
 
29
- def facebook_share(options = {})
33
+ def facebook_share_code(options = {})
30
34
  options = default_facebook_share_options.merge(options)
31
- script = <<-JS
32
- $("#{options[:selector]}").unbind("click.facebook_share").bind("click.facebook_share",function () {
33
- FB.ui({method: \'stream.publish\'#{build_params(options)}});
35
+ <<-JS
36
+ FB.ui({method: 'stream.publish'#{build_params(options)}});
34
37
  return false;
38
+ JS
39
+ end
40
+
41
+ def facebook_share(options = {})
42
+ options = default_facebook_share_options.merge(options)
43
+ script = ""
44
+ case options[:framework]
45
+ when :dojo
46
+ script << <<-JS
47
+ dojo.query("#{options[:selector]}").onclick(function(evt){
48
+ #{facebook_share_code(options)}
49
+ });
50
+ JS
51
+ else # default to jquery
52
+ script << <<-JS
53
+ #{options[:jquery_function]}("#{options[:selector]}").unbind("click.facebook_share").bind("click.facebook_share",function () {
54
+ #{facebook_share_code(options)}
35
55
  });
36
56
  JS
57
+ end
37
58
  script
38
59
  end
39
60
 
@@ -63,23 +84,24 @@ JS
63
84
  html_safe_string("FB.init({appId:\"#{options[:app_id]}\"#{params}});")
64
85
  end
65
86
 
66
- def build_params(options, for_init = false)
67
- script = ""
68
- options.each do |key, value|
69
- # if it's for init script, include only status, cookie and xfbml
70
- # if it's for stream.publish, include all except for initial
71
- param_check = ( for_init ) ? FacebookShare::INIT_PARAMS.include?(key.to_s) : !(FacebookShare::REMOVE_PARAMS.include?(key.to_s))
87
+ private
88
+ def build_params(options, for_init = false)
89
+ script = ""
90
+ options.each do |key, value|
91
+ # if it's for init script, include only status, cookie and xfbml
92
+ # if it's for stream.publish, include all except for initial
93
+ param_check = ( for_init ) ? FacebookShare::INIT_PARAMS.include?(key.to_s) : !(FacebookShare::REMOVE_PARAMS.include?(key.to_s))
72
94
 
73
- if value && param_check
74
- value_sanitized = value.to_s.gsub(/"/, '\"')
75
- script << ", #{key}: \"#{value_sanitized}\""
95
+ if value && param_check
96
+ value_sanitized = value.to_s.gsub(/"/, '\"')
97
+ script << ", #{key}: \"#{value_sanitized}\""
98
+ end
76
99
  end
100
+ script
77
101
  end
78
- script
79
- end
80
102
 
81
- def html_safe_string(str)
82
- @use_html_safe ||= "".respond_to?(:html_safe)
83
- @use_html_safe ? str.html_safe : str
84
- end
103
+ def html_safe_string(str)
104
+ @use_html_safe ||= "".respond_to?(:html_safe)
105
+ @use_html_safe ? str.html_safe : str
106
+ end
85
107
  end
@@ -50,23 +50,47 @@ describe FacebookShare do
50
50
  describe "creating init script" do
51
51
  context "when vanilla locale" do
52
52
  subject { facebook_connect_js_tag( :locale => "en_US" ) }
53
-
53
+
54
54
  it { should match(/\/en_US\/all\.js/) }
55
55
  it { should_not match(/\/en\/all\.js/) }
56
56
  end
57
57
 
58
58
  context "when shortened locale" do
59
59
  subject { facebook_connect_js_tag( :locale => "en" ) }
60
-
60
+
61
61
  it { should match(/\/en_EN\/all\.js/) }
62
62
  it { should_not match(/\/en\/all\.js/) }
63
63
  end
64
64
 
65
65
  context "when shortened locale 2" do
66
66
  subject { facebook_connect_js_tag( :locale => "pl" ) }
67
-
67
+
68
68
  it { should match(/\/pl_PL\/all\.js/) }
69
69
  it { should_not match(/\/pl\/all\.js/) }
70
70
  end
71
71
  end
72
+
73
+ describe "using this JavaScript framework: " do
74
+ context "Dojo" do
75
+ subject { facebook_share_once( :selector => ".fb_share", :framework => :dojo ) }
76
+
77
+ it { should match(/dojo\.query\(".fb_share"\)/) }
78
+ it { should_not match(/\$\(".fb_share"\)/) }
79
+ end
80
+
81
+ context "jQuery" do
82
+ subject { facebook_share_once( :selector => ".fb_share", :framework => :jquery ) }
83
+
84
+ it { should match(/\$\(".fb_share"\)/) }
85
+ it { should_not match(/dojo\.query\(".fb_share"\)/) }
86
+ end
87
+
88
+ context "jQuery with a defined function" do
89
+ subject { facebook_share_once( :selector => ".fb_share", :framework => :jquery, :jquery_function => "$j" ) } # $j is used on Wikimedia projects
90
+
91
+ it { should match(/\$j\(".fb_share"\)/) }
92
+ it { should_not match(/\$\(".fb_share"\)/) }
93
+ it { should_not match(/dojo\.query\(".fb_share"\)/) }
94
+ end
95
+ end
72
96
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: facebook_share
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Mike Po\xC5\x82tyn"