Created
May 6, 2010 20:03
-
-
Save jqr/392632 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Returns a link to url with the specified content, automatically adds | |
# rel="nofollow" and the external class to the link. | |
def link_to_external(content, url, options = {}) | |
url = httpify_url(url) | |
link_to content, url, options.merge(:rel => :nofollow, :class => "#{options[:class].to_s} external") | |
end | |
# Just like link_to_external, but uses the dehttpified url as the content. | |
def link_to_external_url(url, options = {}) | |
link_to_external(h(dehttpify_url(url)), url, options) | |
end | |
# Adds http:// to a URL if missing. | |
def httpify_url(url) | |
if url.match(/^https?\:\/\//i) | |
url | |
else | |
"http://#{url}" | |
end | |
end | |
# Removes http:// from a URL if present. | |
def dehttpify_url(url) | |
if url.match(/^https?\:\/\//i) | |
url.gsub(/^https?\:\/\//i, '') | |
else | |
url | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment