From 1c62c1a79986fcf66a4a1015cc3e827718a7eeda Mon Sep 17 00:00:00 2001 From: "Matthew K. Fu JuYuan" Date: Mon, 6 Aug 2012 16:12:49 +0800 Subject: [PATCH 1/6] first version of fb_share --- Gemfile | 2 ++ Gemfile.lock | 2 ++ app/assets/javascripts/application.js | 3 ++- app/assets/stylesheets/application.css | 2 +- app/helpers/application_helper.rb | 2 ++ config/initializers/social_share_button.rb | 3 +++ config/locales/social_share_button.en.yml | 14 ++++++++++++++ config/locales/social_share_button.zh-tw.yml | 14 ++++++++++++++ .../news/front_end/news_bulletins/show.html.erb | 3 ++- 9 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 config/initializers/social_share_button.rb create mode 100644 config/locales/social_share_button.en.yml create mode 100644 config/locales/social_share_button.zh-tw.yml diff --git a/Gemfile b/Gemfile index 3146d5bc4..3eb4c0607 100644 --- a/Gemfile +++ b/Gemfile @@ -39,6 +39,8 @@ gem 'progress_bar' gem 'sinatra' gem 'sprockets' +gem 'social-share-button' + gem 'tinymce-rails' gem 'therubyracer' if RUBY_PLATFORM.downcase.include?("linux") gem 'rb-readline' if RUBY_PLATFORM.downcase.include?("linux") diff --git a/Gemfile.lock b/Gemfile.lock index 31bdf08a4..96f07221a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -250,6 +250,7 @@ GEM rack (~> 1.3, >= 1.3.6) rack-protection (~> 1.2) tilt (~> 1.3, >= 1.3.3) + social-share-button (0.0.6) spork (0.9.0) spork (0.9.0-x86-mingw32) win32-process @@ -343,6 +344,7 @@ DEPENDENCIES shoulda-matchers simplecov sinatra + social-share-button spork sprockets sunspot-rails-tester diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 13d92690e..f3c799cf2 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -6,4 +6,5 @@ // //= require jquery //= require jquery_ujs -//= require_tree . \ No newline at end of file +//= require_tree . +//= social-share-button \ No newline at end of file diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 9833ffda0..c0c227fe5 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -3,5 +3,5 @@ *and any sub-directories. You're free to add application-wide styles to this file and they'll appear at *the top of the compiled file, but it's generally better to create a new file per style scope. *= require_self - + *= require social-share-button */ diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 0c6229b3c..16b0ff098 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -140,6 +140,7 @@ module ApplicationHelper stylesheets << "\n" stylesheets << "\n" end + stylesheets << "\n" stylesheets << "\n" if page.design.reset_css stylesheets << "\n" stylesheets << "\n" if page.design.default_css @@ -161,6 +162,7 @@ module ApplicationHelper end javascripts << "\n" javascripts << "\n" + javascripts << "\n" javascripts << "\n" page.design.javascripts.each do |js| # javascripts << "" diff --git a/config/initializers/social_share_button.rb b/config/initializers/social_share_button.rb new file mode 100644 index 000000000..8b9335778 --- /dev/null +++ b/config/initializers/social_share_button.rb @@ -0,0 +1,3 @@ +SocialShareButton.configure do |config| + config.allow_sites = %w(twitter facebook google_plus) +end \ No newline at end of file diff --git a/config/locales/social_share_button.en.yml b/config/locales/social_share_button.en.yml new file mode 100644 index 000000000..7e3f1fb0e --- /dev/null +++ b/config/locales/social_share_button.en.yml @@ -0,0 +1,14 @@ +en: + social_share_button: + share_to: Share to %{name} + weibo: Sina Weibo + twitter: Twitter + facebook: Facebook + douban: Douban + qq: Qzone + tqq: Tqq + delicious: Delicious + baidu: Baidu.com + kaixin001: Kaixin001.com + renren: Renren.com + google_plus: Google+ diff --git a/config/locales/social_share_button.zh-tw.yml b/config/locales/social_share_button.zh-tw.yml new file mode 100644 index 000000000..b0321eb5a --- /dev/null +++ b/config/locales/social_share_button.zh-tw.yml @@ -0,0 +1,14 @@ +zh_tw: + social_share_button: + share_to: 分享到%{name} + weibo: 新浪微博 + twitter: Twitter + facebook: Facebook + douban: 豆瓣 + qq: QQ空間 + tqq: 腾讯微博 + delicious: Delicious + baidu: 百度收藏 + kaixin001: 開心網 + renren: 人人網 + google_plus: Google+ diff --git a/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/show.html.erb b/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/show.html.erb index edd8a9e1a..c6c912c8f 100644 --- a/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/show.html.erb +++ b/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/show.html.erb @@ -40,5 +40,6 @@
- <%= render "shared/addthis_toolbox"%> + <%= social_share_button_tag(@news_bulletin.title) %> + <%#= render "shared/addthis_toolbox"%>
From 3cb182ff8ed5c5e2f7909942d2cc166a782f1734 Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Wed, 8 Aug 2012 03:10:26 +0800 Subject: [PATCH 2/6] Add meta to head for fb share --- app/helpers/application_helper.rb | 15 +++++++++++++-- .../front_end/bulletins/show.html.erb | 5 ++--- .../news/front_end/news_bulletins/show.html.erb | 5 +---- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 16b0ff098..8fb553151 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -140,7 +140,7 @@ module ApplicationHelper stylesheets << "\n" stylesheets << "\n" end - stylesheets << "\n" + stylesheets << "\n" stylesheets << "\n" if page.design.reset_css stylesheets << "\n" stylesheets << "\n" if page.design.default_css @@ -162,7 +162,7 @@ module ApplicationHelper end javascripts << "\n" javascripts << "\n" - javascripts << "\n" + javascripts << "\n" javascripts << "\n" page.design.javascripts.each do |js| # javascripts << "" @@ -214,4 +214,15 @@ module ApplicationHelper object.strftime("%Y-%m-%d") end + def share_links(object) + js = '' + js << "\n" rescue '' + js << "\n" rescue '' + js << "\n" rescue '' + content_tag :div, :class => 'fb' do + concat social_share_button_tag + concat javascript_tag "$('head').append('#{j js}');" + end + end + end diff --git a/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/show.html.erb b/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/show.html.erb index af1ca12c3..2671d2e11 100644 --- a/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/show.html.erb +++ b/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/show.html.erb @@ -36,6 +36,5 @@ <% end %> -
- <%= render "shared/addthis_toolbox"%> -
+ +<%= share_links(@news_bulletin) %> diff --git a/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/show.html.erb b/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/show.html.erb index c6c912c8f..38a628285 100644 --- a/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/show.html.erb +++ b/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/show.html.erb @@ -39,7 +39,4 @@ <% end %> -
- <%= social_share_button_tag(@news_bulletin.title) %> - <%#= render "shared/addthis_toolbox"%> -
+<%= share_links(@news_bulletin) %> From fc560f420515c1c94a93857ada7f2fac00cecf35 Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Wed, 8 Aug 2012 05:40:50 +0800 Subject: [PATCH 3/6] Try fb from override and generated page --- .../lib/social-share-button.coffee | 34 +++++++++++++++++++ app/controllers/application_controller.rb | 7 ++++ app/helpers/application_helper.rb | 7 ++-- app/views/shared/render_share.html.erb | 11 ++++++ .../override_and_new_class_methods.rb | 21 ++++++++++++ config/routes.rb | 2 ++ 6 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 app/assets/javascripts/lib/social-share-button.coffee create mode 100644 app/views/shared/render_share.html.erb diff --git a/app/assets/javascripts/lib/social-share-button.coffee b/app/assets/javascripts/lib/social-share-button.coffee new file mode 100644 index 000000000..8e97d0eff --- /dev/null +++ b/app/assets/javascripts/lib/social-share-button.coffee @@ -0,0 +1,34 @@ +window.SocialShareButton = + openUrl : (url) -> + window.open(url) + false + + share : (el) -> + site = $(el).data('site') + title = encodeURIComponent($(el).parent().data('title')) + img = encodeURIComponent($(el).parent().data("img")) + fb_url = encodeURIComponent($(el).parent().data("fb_url")) + url = encodeURIComponent(location.href) + fb_full_url = encodeURIComponent("#{fb_url}?orig_url=#{location.href}") + switch site + when "weibo" + SocialShareButton.openUrl("http://v.t.sina.com.cn/share/share.php?url=#{url}&pic=#{img}&title=#{title}&content=utf-8") + when "twitter" + SocialShareButton.openUrl("https://twitter.com/home?status=#{title}: #{url}") + when "douban" + SocialShareButton.openUrl("http://www.douban.com/recommend/?url=#{url}&title=#{title}&image=#{img}") + when "facebook" + SocialShareButton.openUrl("http://www.facebook.com/sharer.php?t=#{title}&u=#{fb_full_url}") + when "qq" + SocialShareButton.openUrl("http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=#{url}&title=#{title}&pics=#{img}") + when "tqq" + SocialShareButton.openUrl("http://share.v.t.qq.com/index.php?c=share&a=index&url=#{url}&title=#{title}&pic=#{img}") + when "baidu" + SocialShareButton.openUrl("http://apps.hi.baidu.com/share/?url=#{url}&title=#{title}&content=") + when "kaixin001" + SocialShareButton.openUrl("http://www.kaixin001.com/rest/records.php?url=#{url}&content=#{title}&style=11&pic=#{img}") + when "renren" + SocialShareButton.openUrl("http://widget.renren.com/dialog/share?resourceUrl=#{url}&title=#{title}&description=") + when "google_plus" + SocialShareButton.openUrl("https://plus.google.com/share?url=#{url}&t=#{title}") + false \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5fd9de5c9..bf9da2f71 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -106,6 +106,13 @@ class ApplicationController < ActionController::Base render :text => '404 Not Found' end end + + def render_share + object_class = params[:model].classify.constantize + @object = object_class.find(params[:id]) + @orig_url = params[:orig_url] + render 'shared/render_share', :layout => false + end protected diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 8fb553151..be2ab2706 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -162,7 +162,8 @@ module ApplicationHelper end javascripts << "\n" javascripts << "\n" - javascripts << "\n" + # javascripts << "\n" + javascripts << "\n" javascripts << "\n" page.design.javascripts.each do |js| # javascripts << "" @@ -220,8 +221,8 @@ module ApplicationHelper js << "\n" rescue '' js << "\n" rescue '' content_tag :div, :class => 'fb' do - concat social_share_button_tag - concat javascript_tag "$('head').append('#{j js}');" + concat social_share_button_tag(object.title, :fb_url => "http://#{request.env['HTTP_HOST']}/share/#{object.class.to_s.underscore}/#{object.id}", :image => "http://#{request.env['HTTP_HOST']}#{object.image.url}") + # concat javascript_tag "$('head').append('#{j js}');" end end diff --git a/app/views/shared/render_share.html.erb b/app/views/shared/render_share.html.erb new file mode 100644 index 000000000..110d30f57 --- /dev/null +++ b/app/views/shared/render_share.html.erb @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/config/initializers/override_and_new_class_methods.rb b/config/initializers/override_and_new_class_methods.rb index 8170d674f..075158042 100644 --- a/config/initializers/override_and_new_class_methods.rb +++ b/config/initializers/override_and_new_class_methods.rb @@ -3,3 +3,24 @@ Integer.class_eval do !self.zero? end end + +module SocialShareButton + module Helper + def social_share_button_tag(title = "", opts = {}) + rel = opts[:rel] + html = [] + html << "" + raw html.join("\n") + end + end +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index b281dd33b..91a28d60e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -140,6 +140,8 @@ Orbit::Application.routes.draw do match '/panel/:app_name/front_end/:app_action' => 'pages#index_from_link', :constraints => lambda { |request| !request.query_string.include?("inner=true") } + + match '/share/:model/:id' => 'application#render_share' # routes for gridfs files match "/gridfs/*path" => "gridfs#serve" From f7e6703d5f8e19977ab09c4bc362092b161adea8 Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Wed, 8 Aug 2012 06:50:28 +0800 Subject: [PATCH 4/6] Change the way the orig_url is passed to fb --- app/assets/javascripts/lib/social-share-button.coffee | 2 +- app/controllers/application_controller.rb | 2 +- app/views/shared/render_share.html.erb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/lib/social-share-button.coffee b/app/assets/javascripts/lib/social-share-button.coffee index 8e97d0eff..8a72a2ac9 100644 --- a/app/assets/javascripts/lib/social-share-button.coffee +++ b/app/assets/javascripts/lib/social-share-button.coffee @@ -9,7 +9,7 @@ window.SocialShareButton = img = encodeURIComponent($(el).parent().data("img")) fb_url = encodeURIComponent($(el).parent().data("fb_url")) url = encodeURIComponent(location.href) - fb_full_url = encodeURIComponent("#{fb_url}?orig_url=#{location.href}") + fb_full_url = encodeURIComponent("#{fb_url}?orig_host=#{location.host}&orig_path=#{location.pathname}") switch site when "weibo" SocialShareButton.openUrl("http://v.t.sina.com.cn/share/share.php?url=#{url}&pic=#{img}&title=#{title}&content=utf-8") diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index bf9da2f71..7601f91a9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -110,7 +110,7 @@ class ApplicationController < ActionController::Base def render_share object_class = params[:model].classify.constantize @object = object_class.find(params[:id]) - @orig_url = params[:orig_url] + @orig_url = "http://#{params[:orig_host]}/#{params[:orig_path]}" render 'shared/render_share', :layout => false end diff --git a/app/views/shared/render_share.html.erb b/app/views/shared/render_share.html.erb index 110d30f57..33cc4b00a 100644 --- a/app/views/shared/render_share.html.erb +++ b/app/views/shared/render_share.html.erb @@ -2,7 +2,7 @@ - + From db16fdc7b5e12a5ad6373777849c9cf5b8afc0f2 Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Wed, 8 Aug 2012 07:46:10 +0800 Subject: [PATCH 5/6] Change for getting url from key --- app/assets/javascripts/lib/social-share-button.coffee | 3 +-- app/controllers/application_controller.rb | 4 +++- app/helpers/application_helper.rb | 4 ++-- .../panel/announcement/front_end/bulletins/show.html.erb | 2 +- .../views/panel/news/front_end/news_bulletins/show.html.erb | 2 +- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/lib/social-share-button.coffee b/app/assets/javascripts/lib/social-share-button.coffee index 8a72a2ac9..cec9fcbc4 100644 --- a/app/assets/javascripts/lib/social-share-button.coffee +++ b/app/assets/javascripts/lib/social-share-button.coffee @@ -9,7 +9,6 @@ window.SocialShareButton = img = encodeURIComponent($(el).parent().data("img")) fb_url = encodeURIComponent($(el).parent().data("fb_url")) url = encodeURIComponent(location.href) - fb_full_url = encodeURIComponent("#{fb_url}?orig_host=#{location.host}&orig_path=#{location.pathname}") switch site when "weibo" SocialShareButton.openUrl("http://v.t.sina.com.cn/share/share.php?url=#{url}&pic=#{img}&title=#{title}&content=utf-8") @@ -18,7 +17,7 @@ window.SocialShareButton = when "douban" SocialShareButton.openUrl("http://www.douban.com/recommend/?url=#{url}&title=#{title}&image=#{img}") when "facebook" - SocialShareButton.openUrl("http://www.facebook.com/sharer.php?t=#{title}&u=#{fb_full_url}") + SocialShareButton.openUrl("http://www.facebook.com/sharer.php?t=#{title}&u=#{fb_url}") when "qq" SocialShareButton.openUrl("http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=#{url}&title=#{title}&pics=#{img}") when "tqq" diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7601f91a9..88c14bfad 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -110,7 +110,9 @@ class ApplicationController < ActionController::Base def render_share object_class = params[:model].classify.constantize @object = object_class.find(params[:id]) - @orig_url = "http://#{params[:orig_host]}/#{params[:orig_path]}" + module_app = ModuleApp.first(:conditions => {:key => params[:key]}) + @item = Item.where(module_app_id: module_app.id).all_of("tag" => {"$in" => [nil,'']},"category" => { "$in" => [nil,'']}).first + @orig_url = "http://#{request.host}/#{@item.path}?id=#{@object.id}" render 'shared/render_share', :layout => false end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index be2ab2706..e9211c766 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -215,13 +215,13 @@ module ApplicationHelper object.strftime("%Y-%m-%d") end - def share_links(object) + def share_links(object, key) js = '' js << "\n" rescue '' js << "\n" rescue '' js << "\n" rescue '' content_tag :div, :class => 'fb' do - concat social_share_button_tag(object.title, :fb_url => "http://#{request.env['HTTP_HOST']}/share/#{object.class.to_s.underscore}/#{object.id}", :image => "http://#{request.env['HTTP_HOST']}#{object.image.url}") + concat social_share_button_tag(object.title, :fb_url => "http://#{request.env['HTTP_HOST']}/share/#{object.class.to_s.underscore}/#{object.id}?key=#{key}", :image => "http://#{request.env['HTTP_HOST']}#{object.image.url}") # concat javascript_tag "$('head').append('#{j js}');" end end diff --git a/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/show.html.erb b/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/show.html.erb index 2671d2e11..c8cc6bea0 100644 --- a/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/show.html.erb +++ b/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/show.html.erb @@ -37,4 +37,4 @@ <% end %> -<%= share_links(@news_bulletin) %> +<%= share_links(@bulletin, 'announcement') %> diff --git a/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/show.html.erb b/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/show.html.erb index 38a628285..a1d4097b7 100644 --- a/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/show.html.erb +++ b/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/show.html.erb @@ -39,4 +39,4 @@ <% end %> -<%= share_links(@news_bulletin) %> +<%= share_links(@news_bulletin, 'news') %> From e9de0e24b4c1558ecb759329016e8c50ff23cef4 Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Wed, 8 Aug 2012 10:12:21 +0800 Subject: [PATCH 6/6] Fix fb share, redirect with js when the page is not requested by fb but by user --- app/controllers/application_controller.rb | 2 +- app/helpers/application_helper.rb | 2 +- app/views/shared/render_share.html.erb | 8 ++++++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 88c14bfad..109d4742a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -112,7 +112,7 @@ class ApplicationController < ActionController::Base @object = object_class.find(params[:id]) module_app = ModuleApp.first(:conditions => {:key => params[:key]}) @item = Item.where(module_app_id: module_app.id).all_of("tag" => {"$in" => [nil,'']},"category" => { "$in" => [nil,'']}).first - @orig_url = "http://#{request.host}/#{@item.path}?id=#{@object.id}" + @orig_url = "http://#{request.host_with_port}/#{@item.path}?id=#{@object.id}" render 'shared/render_share', :layout => false end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index e9211c766..63526229f 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -163,7 +163,7 @@ module ApplicationHelper javascripts << "\n" javascripts << "\n" # javascripts << "\n" - javascripts << "\n" + javascripts << "\n" javascripts << "\n" page.design.javascripts.each do |js| # javascripts << "" diff --git a/app/views/shared/render_share.html.erb b/app/views/shared/render_share.html.erb index 33cc4b00a..f8d3c9302 100644 --- a/app/views/shared/render_share.html.erb +++ b/app/views/shared/render_share.html.erb @@ -3,8 +3,12 @@ - - + +