From fc560f420515c1c94a93857ada7f2fac00cecf35 Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Wed, 8 Aug 2012 05:40:50 +0800 Subject: [PATCH] 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 << "
" + + SocialShareButton.config.allow_sites.each do |name| + link_title = t "social_share_button.share_to", :name => t("social_share_button.#{name.downcase}") + html << link_to("","#", :rel => "nofollow #{rel}", + "data-site" => name, + :class => "social-share-button-#{name}", + :onclick => "return SocialShareButton.share(this);", + :title => h(link_title)) + end + 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"