From 3cb182ff8ed5c5e2f7909942d2cc166a782f1734 Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Wed, 8 Aug 2012 03:10:26 +0800 Subject: [PATCH 1/5] 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 16b0ff09..8fb55315 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 af1ca12c..2671d2e1 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 c6c912c8..38a62828 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 2/5] 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 00000000..8e97d0ef --- /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 5fd9de5c..bf9da2f7 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 8fb55315..be2ab270 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 00000000..110d30f5 --- /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 8170d674..07515804 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 b281dd33..91a28d60 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 3/5] 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 8e97d0ef..8a72a2ac 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 bf9da2f7..7601f91a 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 110d30f5..33cc4b00 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 4/5] 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 8a72a2ac..cec9fcbc 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 7601f91a..88c14bfa 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 be2ab270..e9211c76 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 2671d2e1..c8cc6bea 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 38a62828..a1d4097b 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 5/5] 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 88c14bfa..109d4742 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 e9211c76..63526229 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 33cc4b00..f8d3c930 100644 --- a/app/views/shared/render_share.html.erb +++ b/app/views/shared/render_share.html.erb @@ -3,8 +3,12 @@ - - + +