Here's a trick I use when I need to create parameterized links to external sites that service my site. For example, on a church site I maintain, we sometimes link bible references to the actual verses they cite. When we recommend certain books for study, we like to provide links for our users to purchase those books. By using a link adapter we can conform the parameters into the format expected by the provider. Using the adapter rather than dozens of hardcoded links sitewide makes it a cinch to fix links that are broken when a provider discontinues or modifies his service.
Looking up a bible verse:
tag 'link_to_verse' do |tag|
passage = tag.attr['passage']
search_for = passage.gsub('.', ' ').squeeze(" ")
%{<a class='ext-link' href="http://bible.gospelcom.net/passage/?search=#{search_for};&version=31;" title="Lookup #{passage}"/>#{passage}</a>}
end
Usage:
<r:link_to_verse passage="John 3:16" />
Result: John 3:16
Linking to a recommended book:
tag 'link_to_book' do |tag|
isbn = tag.attr['isbn']
store_link = ["http://www.allbookstores.com/book/#{isbn}", "All Book Stores"]
#Alternative providers, each having different link formats:
#store_link = ["http://www.bestwebbuys.com/books/compare/isbn/#{isbn}", "Best Book Buys"]
#store_link = ["http://www.shopping.com/xFS?KW=#{isbn}&FN=Books", "Shopping.<b>com</b>pare books"]
#store_link = ["http://www.pricegrabber.com/search_getprod.php?isbn=#{isbn}", "Price Grabber"]
#store_link = ["http://www.pricescan.com/books/BookDetail.asp?isbn=#{isbn}", "Price Scan"]
#store_link = ["http://isbn.nu/#{isbn}", "ISBN NU"]
#store_link = ["http://www.bookfinder4u.com/IsbnSearch.aspx?isbn=#{isbn}&mode=direct", "Book Finder 4U"]
#store_link = ["http://www.bestbookdeal.com/book/compare/#{isbn}", "Best Book Deal"]
#store_link = ["http://www.aaabooksearch.com/Compare/Prices/US/#{isbn}.html", "AAA Book Search"]
#store_link = ["http://www.fetchbook.info/compare.do?search=#{isbn}&searchBy=ISBN&Submit=Search", "Fetch Book"]
#store_link = ["http://www.bookhq.com/compare/#{isbn}.html", "Book H.Q."]
#store_link = ["http://www3.addall.com/New/submitNew.cgi?query=#{isbn}&type=ISBN&location=&state=&dispCurr=USD", "Add All Books"]
#store_link = ["http://www.cheapestbookprice.com/?searchby=ISBN&search_all=1&searchfortext=#{isbn}&search.x=0&search.y=0", "Cheapest Book Price"]
#store_link = ["http://dogbert.abebooks.com/servlet/SearchResults?imagefield.x=0&cm_re=A*Search+Box*Form&kn=#{isbn}&imagefield.y=0", "Abe Books"]
#store_link = ["http://ebs.allbookstores.com/book/compare/#{isbn}", "Every Book Store"]
url, store_name = store_link
%{<a class='ext-link' href="#{url}" title="Go to #{store_name}"/>#{tag.single? ? 'buy book' : tag.expand}</a>}
end
Usage:
<r:link_to_book isbn="0842332588" /> <r:link_to_book isbn="0842332588">The Daily Walk Bible</r:link_to_book>
Result: buy book The Daily Walk Bible
This approach is a natural fit for creating a "link_to_map" adapter that accesses Google Maps, Yahoo Maps, or MapQuest.
Consider what we can accomplish when we track our users' preferences in our database. We could allow some users to read verses in King James and others in the New International Version. Some users might prefer to buy from certain book stores or use certain Map services. We'd simply have to modify the tag code to lookup and make use of the current user's settings. The same sort of possibilities exist by making use of the "config" table. (e.g. extension.link_to_map.default = MapQuest)
