Skip to content

Commit

Permalink
Made addthis_share_button no merely an alias, it now creates a bookma…
Browse files Browse the repository at this point in the history
…rk button with a different graphic.
  • Loading branch information
jaap3 committed Apr 30, 2009
1 parent 0029b73 commit 099e436
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 28 deletions.
29 changes: 24 additions & 5 deletions README.rdoc
Expand Up @@ -24,9 +24,21 @@ Or use it as a plain plugin:

== Quick Start

The most basic use is simply providing no arguments:
This plugin adds 4 new methods to be used in your html views:

addthis_bookmark_button
addthis_share_button
addthis_email_button
addthis_feed_button(url)

The bookmark_button and share_button are identical except for the default
graphic. The names of email_button and feed_button are self explanitory.

The most basic use is simply providing no arguments, this is possible for the
following methods:

addthis_bookmark_button
addthis_share_button
addthis_email_button

The code generated by Addthis will then try to figure out the url and title by
Expand All @@ -35,17 +47,20 @@ itself. This is not possible for addthis_feed_button.
You can set a custom url, a required argument for addthis_feed_button:

addthis_bookmark_button("http://www.example.com/")
addthis_share_button("http://www.example.com/")
addthis_email_button("http://www.example.com/")
addthis_feed_button("http://www.example.com/")

Setting a title is also possible when adding a bookmark or email button:
Setting a title is also possible when adding a bookmark, share or email button:

addthis_bookmark_button("http://www.example.com/", "Example website")
addthis_share_button("http://www.example.com/", "Example website")
addthis_email_button("http://www.example.com/", "Example website")

To only set a custom title you can use to options hash:

addthis_bookmark_button(:page_title => "Example title")
addthis_share_button(:page_title => "Example title")
addthis_email_button(:page_title => "Example title")

The options hash can be used to customize the widget to a great extent. Each of
Expand All @@ -71,6 +86,7 @@ the source to see what they are.
Jaap3::Addthis::CONFIG
Jaap3::Addthis::DEFAULT_OPTIONS
Jaap3::Addthis::BOOKMARK_BUTTON_DEFAULTS
Jaap3::Addthis::SHARE_BUTTON_DEFAULTS
Jaap3::Addthis::FEED_BUTTON_DEFAULTS
Jaap3::Addthis::EMAIL_BUTTON_DEFAULTS

Expand Down Expand Up @@ -104,12 +120,15 @@ but withouth the image.
You can also use the block to call the Rails image_tag helper to embed your
own images.

addthis_bookmark_button { "#{image_tag("bookmark.gif") Bookmark...}" }

You can permanently change the default image of each button by modifying the
following constants in you initializer:

Jaap3::Addthis::BOOKMARK_BUTTON_DEFAULTS[:button_html] = "Click here to AddThis"
Jaap3::Addthis::FEED_BUTTON_DEFAULTS[:button_html] = "Click here to AddThis"
Jaap3::Addthis::EMAIL_BUTTON_DEFAULTS[:button_html] = "Click here to AddThis"
Jaap3::Addthis::BOOKMARK_BUTTON_DEFAULTS[:button_html] = "Click here to bookmark this"
Jaap3::Addthis::SHARE_BUTTON_DEFAULTS[:button_html] = "Click here to share this"
Jaap3::Addthis::FEED_BUTTON_DEFAULTS[:button_html] = "Click here to subscribe"
Jaap3::Addthis::EMAIL_BUTTON_DEFAULTS[:button_html] = "Click here to email this"

=== Script options

Expand Down
21 changes: 14 additions & 7 deletions lib/addthis.rb
Expand Up @@ -8,6 +8,10 @@ module Addthis
:options => nil, :language => nil, :secure => false
}
BOOKMARK_BUTTON_DEFAULTS = {
:title => "",
:button_html => '<img src="http://s7.addthis.com/static/btn/lg-bookmark-en.gif" width="125" height="16" border="0" alt="Bookmark and Share" />'
}
SHARE_BUTTON_DEFAULTS = {
:title => "",
:button_html => '<img src="http://s7.addthis.com/static/btn/lg-share-en.gif" width="125" height="16" border="0" alt="Bookmark and Share" />'
}
Expand All @@ -21,14 +25,17 @@ module Addthis
}

module Helper
def addthis_bookmark_button(*args)
url, options = extract_addthis_url_and_options(args)
options[:button_html] = yield if block_given?
options = BOOKMARK_BUTTON_DEFAULTS.merge(options)
s = %Q{<a href="http://www.addthis.com/bookmark.php?v=20" onmouseover="#{addthis_open("", url, options[:page_title])}" onmouseout="addthis_close()" onclick="return addthis_sendto()" title="#{options[:title]}">}
addthis_tag(s, options)
%w(bookmark share).each do |func|
module_eval <<-EOS
def addthis_#{func}_button(*args)
url, options = extract_addthis_url_and_options(args)
options[:button_html] = yield if block_given?
options = Jaap3::Addthis::#{func.upcase}_BUTTON_DEFAULTS.merge(options)
s = %Q(<a href="http://www.addthis.com/bookmark.php?v=20" onmouseover="\#{addthis_open("", url, options[:page_title])}" onmouseout="addthis_close()" onclick="return addthis_sendto()" title="\#{options[:title]}">)
addthis_tag(s, options)
end
EOS
end
alias addthis_share_button addthis_bookmark_button

def addthis_email_button(*args)
url, options = extract_addthis_url_and_options(args)
Expand Down
94 changes: 78 additions & 16 deletions test/addthis_test.rb
Expand Up @@ -19,7 +19,7 @@ class AddthisTest < Test::Unit::TestCase
assert respond_to?(:addthis_email_button)
end

[:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
[:addthis_bookmark_button, :addthis_share_button, :addthis_feed_button, :addthis_email_button].each do |m|
context "the output of #{m}" do
setup do
@output = method(m).call("http://example.com")
Expand All @@ -43,7 +43,7 @@ class AddthisTest < Test::Unit::TestCase
end
end

[:addthis_bookmark_button, :addthis_email_button].each do |m|
[:addthis_bookmark_button, :addthis_share_button, :addthis_email_button].each do |m|
context "the output of #{m} with url and title set" do
setup { @output = method(m).call("http://example.com", "It's an \"example\"") }

Expand All @@ -53,7 +53,7 @@ class AddthisTest < Test::Unit::TestCase
end
end

context "a bookmark/share button" do
context "a bookmark button" do
setup { @output = addthis_bookmark_button }

should_set_title_to Jaap3::Addthis::BOOKMARK_BUTTON_DEFAULTS[:title]
Expand All @@ -66,6 +66,29 @@ class AddthisTest < Test::Unit::TestCase
should "set title to [TITLE]" do
assert_match "'[TITLE]'", @output
end

should "set button html to default" do
assert_match Jaap3::Addthis::BOOKMARK_BUTTON_DEFAULTS[:button_html], @output
end
end

context "a share button" do
setup { @output = addthis_share_button }

should_set_title_to Jaap3::Addthis::SHARE_BUTTON_DEFAULTS[:title]
should_set_href_to "http://www.addthis.com/bookmark.php?v=20"

should "set url to [URL]" do
assert_match "'[URL]'", @output
end

should "set title to [TITLE]" do
assert_match "'[TITLE]'", @output
end

should "set button html to default" do
assert_match Jaap3::Addthis::SHARE_BUTTON_DEFAULTS[:button_html], @output
end
end

context "a feed button" do
Expand All @@ -77,19 +100,32 @@ class AddthisTest < Test::Unit::TestCase
should "set url to example.com" do
assert_match "'http://example.com')", @output
end

should "set button html to default" do
assert_match Jaap3::Addthis::FEED_BUTTON_DEFAULTS[:button_html], @output
end
end

context "an email button" do
setup { @output = addthis_email_button }

should_set_title_to Jaap3::Addthis::EMAIL_BUTTON_DEFAULTS[:title]
should_set_href_to "http://www.addthis.com/bookmark.php"

should "set button html to default" do
assert_match Jaap3::Addthis::EMAIL_BUTTON_DEFAULTS[:button_html], @output
end
end

context "with publisher configured" do
setup { Jaap3::Addthis::CONFIG[:publisher] = "test_publisher" }
setup do
@config = Jaap3::Addthis::CONFIG.clone
Jaap3::Addthis::CONFIG[:publisher] = "test_publisher"
end

teardown { Jaap3::Addthis::CONFIG.clear.merge! @config }

[:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
[:addthis_bookmark_button, :addthis_share_button, :addthis_feed_button, :addthis_email_button].each do |m|
context "the output of #{m}" do
setup { @output = method(m).call("http://example.com") }

Expand All @@ -104,7 +140,7 @@ class AddthisTest < Test::Unit::TestCase
end

context "in turn overwritten by options hash" do
[:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
[:addthis_bookmark_button, :addthis_share_button, :addthis_feed_button, :addthis_email_button].each do |m|
context "the output of #{m}" do
setup { @output = method(m).call("http://example.com", :publisher => "another_publisher") }

Expand All @@ -121,9 +157,14 @@ class AddthisTest < Test::Unit::TestCase
end

context "with altered script_src" do
setup { Jaap3::Addthis::DEFAULT_OPTIONS[:script_src] = "http://example.com/example.js" }
setup do
@defaults = Jaap3::Addthis::DEFAULT_OPTIONS.clone
Jaap3::Addthis::DEFAULT_OPTIONS[:script_src] = "http://example.com/example.js"
end

teardown { Jaap3::Addthis::DEFAULT_OPTIONS.clear.merge! @defaults }

[:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
[:addthis_bookmark_button, :addthis_share_button, :addthis_feed_button, :addthis_email_button].each do |m|
context "the output of #{m}" do
setup { @output = method(m).call("http://example.com") }

Expand All @@ -132,7 +173,7 @@ class AddthisTest < Test::Unit::TestCase
end

context "in turn overwritten by options hash" do
[:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
[:addthis_bookmark_button, :addthis_share_button, :addthis_feed_button, :addthis_email_button].each do |m|
context "the output of #{m}" do
setup { @output = method(m).call("http://example.com", :script_src => "http://www.example.com/example.js") }

Expand All @@ -143,7 +184,7 @@ class AddthisTest < Test::Unit::TestCase
end

context "when overwriting title" do
[:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
[:addthis_bookmark_button, :addthis_share_button, :addthis_feed_button, :addthis_email_button].each do |m|
context "the output of #{m}" do
setup { @output = method(m).call("http://example.com", :title => "Example title") }

Expand All @@ -153,28 +194,31 @@ class AddthisTest < Test::Unit::TestCase
end

context "when setting secure to true" do
[:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
[:addthis_bookmark_button, :addthis_share_button, :addthis_feed_button, :addthis_email_button].each do |m|
context "by using the options hash the output of #{m}" do
setup { @output = method(m).call("http://example.com", :secure => true) }

should_set_script_src_to "https://secure.addthis.com/js/200/addthis_widget.js"
end
end

[:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
[:addthis_bookmark_button, :addthis_share_button, :addthis_feed_button, :addthis_email_button].each do |m|
context "by altering the defaults the output of #{m}" do
setup do
@defaults = Jaap3::Addthis::DEFAULT_OPTIONS.clone
Jaap3::Addthis::DEFAULT_OPTIONS[:secure] = true
@output = method(m).call("http://example.com")
end

teardown { Jaap3::Addthis::DEFAULT_OPTIONS.clear.merge! @defaults }

should_set_script_src_to "https://secure.addthis.com/js/200/addthis_widget.js"
end
end
end

context "with a block" do
[:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
[:addthis_bookmark_button, :addthis_share_button, :addthis_feed_button, :addthis_email_button].each do |m|
context "the output of #{m}" do
setup do
@output = method(m).call("http://example.com") do
Expand All @@ -191,12 +235,24 @@ class AddthisTest < Test::Unit::TestCase

context "with changed default html" do
setup do
@bookmark_defaults = Jaap3::Addthis::BOOKMARK_BUTTON_DEFAULTS.clone
Jaap3::Addthis::BOOKMARK_BUTTON_DEFAULTS[:button_html] = "Click here to AddThis"
@share_defaults = Jaap3::Addthis::SHARE_BUTTON_DEFAULTS.clone
Jaap3::Addthis::SHARE_BUTTON_DEFAULTS[:button_html] = "Click here to AddThis"
@feed_defaults = Jaap3::Addthis::FEED_BUTTON_DEFAULTS.clone
Jaap3::Addthis::FEED_BUTTON_DEFAULTS[:button_html] = "Click here to AddThis"
@email_defaults = Jaap3::Addthis::FEED_BUTTON_DEFAULTS.clone
Jaap3::Addthis::EMAIL_BUTTON_DEFAULTS[:button_html] = "Click here to AddThis"
end

[:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
teardown do
Jaap3::Addthis::BOOKMARK_BUTTON_DEFAULTS.clear.merge! @bookmark_defaults
Jaap3::Addthis::SHARE_BUTTON_DEFAULTS.clear.merge! @share_defaults
Jaap3::Addthis::FEED_BUTTON_DEFAULTS.clear.merge! @feed_defaults
Jaap3::Addthis::EMAIL_BUTTON_DEFAULTS.clear.merge! @email_defaults
end

[:addthis_bookmark_button, :addthis_share_button, :addthis_feed_button, :addthis_email_button].each do |m|
context "the output of #{m}" do
setup { @output = method(m).call("http://example.com") }

Expand All @@ -216,7 +272,7 @@ class AddthisTest < Test::Unit::TestCase
:offset_left => 60,
:hover_delay => 200
}
[:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
[:addthis_bookmark_button, :addthis_share_button, :addthis_feed_button, :addthis_email_button].each do |m|
context "the output of #{m}" do
setup { @output = method(m).call("http://example.com", options) }

Expand All @@ -226,15 +282,18 @@ class AddthisTest < Test::Unit::TestCase
end
end

[:addthis_bookmark_button, :addthis_feed_button, :addthis_email_button].each do |m|
[:addthis_bookmark_button, :addthis_share_button, :addthis_feed_button, :addthis_email_button].each do |m|
context "by changing the defaults the output of #{m}" do
setup do
options.each_pair do |attribute, value|
@defaults = Jaap3::Addthis::DEFAULT_OPTIONS.clone
Jaap3::Addthis::DEFAULT_OPTIONS[attribute] = value
end
@output = method(m).call("http://example.com")
end

teardown { Jaap3::Addthis::DEFAULT_OPTIONS.clear.merge! @defaults }

options.each_pair do |attribute, value|
should_customize attribute, value
end
Expand All @@ -251,10 +310,13 @@ class AddthisTest < Test::Unit::TestCase

context "by changing the defaults the output of addthis_bookmark_button" do
setup do
@defaults = Jaap3::Addthis::DEFAULT_OPTIONS.clone
Jaap3::Addthis::DEFAULT_OPTIONS[:options] = "facebook, email, twitter, more"
@output = addthis_bookmark_button
end

teardown { Jaap3::Addthis::DEFAULT_OPTIONS.clear.merge! @defaults }

should_customize :options, "facebook, email, twitter, more"
end
end
Expand Down

0 comments on commit 099e436

Please sign in to comment.