Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module TweetButton | ||
TWITTER_SHARE_URL = "http://twitter.com/share" | ||
|
||
def tweet_button(options = {}) | ||
# Merge user specified overrides into defaults, then convert those to data-* attrs | ||
params = options_to_data_params(default_tweet_button_options.merge(options)) | ||
|
||
html = ''.html_safe | ||
|
||
unless @widgetized | ||
html << tweet_widgets_js_tag | ||
end | ||
|
||
html << link_to("Tweet", TWITTER_SHARE_URL, params) | ||
end | ||
|
||
class <<self | ||
attr_accessor :default_tweet_button_options | ||
end | ||
|
||
def default_tweet_button_options | ||
{ | ||
:url => request.url, | ||
:via => "tweetbutton", | ||
:text => "", | ||
:related => "", | ||
:count => "vertical", | ||
:lang => "en" | ||
}.merge(TweetButton.default_tweet_button_options || {}) | ||
end | ||
|
||
def tweet_widgets_js_tag | ||
@widgetized = true | ||
'<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>'.html_safe | ||
end | ||
|
||
def custom_tweet_button(text = 'Tweet', options = {}, html_options = {}) | ||
# This line is really long. And it makes me sad. | ||
link_to(text, "#{TWITTER_SHARE_URL}?#{options_to_query_string(default_tweet_button_options.merge(options))}", html_options) | ||
end | ||
|
||
def options_to_data_params(opts) | ||
params = {} | ||
opts.each {|k, v| params["data-#{k}"] = v} | ||
|
||
# Make sure the CSS class is there | ||
params['class'] = 'twitter-share-button' | ||
params | ||
end | ||
|
||
# I'm pretty sure this is in the stdlib or Rails somewhere. Too lazy to figure it out now. | ||
def options_to_query_string(opts) | ||
opts.map{|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v)}"}.join("&") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Generated by jeweler | ||
# DO NOT EDIT THIS FILE DIRECTLY | ||
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command | ||
# -*- encoding: utf-8 -*- | ||
|
||
Gem::Specification.new do |s| | ||
s.name = %q{tweet-button} | ||
s.version = "0.1.0" | ||
|
||
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= | ||
s.authors = ["Jeremy McAnally"] | ||
s.date = %q{2010-08-12} | ||
s.description = %q{Generate Tweet buttons for your Rails apps} | ||
s.email = %q{jeremymcanally@gmail.com} | ||
s.extra_rdoc_files = [ | ||
"LICENSE", | ||
"README.rdoc" | ||
] | ||
s.files = [ | ||
".document", | ||
".gitignore", | ||
"LICENSE", | ||
"README.rdoc", | ||
"Rakefile", | ||
"VERSION", | ||
"lib/tweet-button.rb", | ||
"test/helper.rb", | ||
"test/test_tweet-button.rb" | ||
] | ||
s.homepage = %q{http://github.com/jm/tweet-button} | ||
s.rdoc_options = ["--charset=UTF-8"] | ||
s.require_paths = ["lib"] | ||
s.rubygems_version = %q{1.3.6} | ||
s.summary = %q{Generate new Twitter 'Tweet buttons'} | ||
s.test_files = [ | ||
"test/helper.rb", | ||
"test/test_tweet-button.rb" | ||
] | ||
|
||
if s.respond_to? :specification_version then | ||
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION | ||
s.specification_version = 3 | ||
|
||
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then | ||
else | ||
end | ||
else | ||
end | ||
end | ||
|