ruralocity
4/17/2012 - 11:20 AM

incomplete, untested at all spaghetti helpers for social media sharing in rails

incomplete, untested at all spaghetti helpers for social media sharing in rails

def facebook_button(url)
  content_for :head do
    content_tag :script,
      (function(d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s); js.id = id;
        js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
        fjs.parentNode.insertBefore(js, fjs);
      }(document, 'script', 'facebook-jssdk'));
  end
  
  content_tag :div, '',
    { 'data-href' => url,
      'data-send' => 'false',
      'data-width' => '450',
      'data-show-faces' => 'false' }
end

def twitter_button(url)
  content_for :head do
    content_tag :script, '!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");'
  end
  
  content_tag :a, 'Tweet',
    { :href => 'https://twitter.com/share',
      :class => 'twitter-share-button',
      'data-url' => url }
end

def linkedin_button(url)
  javascript_include_tag "//platform.linkedin.com/in.js"
  content_tag :script, '',
    { 'type' => 'IN/Share',
      'data-url' => url,
      'data-counter' => 'right' }
end

def pinterest_button(url,image_url,description)
  content_for :head do
    javascript_include_tag "//assets.pinterest.com/js/pinit.js"
  end
  
  content_tag :a, 
    image_tag('//assets.pinterest.com/images/PinExt.png', 
      {:border => 0, :title => 'Pin It'}),
      "http://pinterest.com/pin/create/button/?url=#{url}&media=#{image_url}&description=#{description}",
      :class => 'pin-it-button'
end