module Mack
module ViewHelpers # :nodoc:
module LinkHelpers
# This is just an alias to the a method
#
# Examples:
# <%= link_to("http://www.mackframework.com") %> # => http://www.mackframework.com
# <%= link_to("Mack", "http://www.mackframework.com") %> # => Mack
# <%= link_to("Mack", "http://www.mackframework.com", :target => "_blank") %> # => Mack
# <%= link_to("Mack", "http://www.mackframework.com", :target => "_blank", :rel => :nofollow) %> # => Mack
# If you pass in :method as an option it will be a JavaScript form that will post to the specified link with the
# methd specified.
# <%= link_to("Mack", "http://www.mackframework.com", :method => :delete) %>
# If you use the :method option you can also pass in a :confirm option. The :confirm option will generate a
# javascript confirmation window. If 'OK' is selected the the form will submit. If 'cancel' is selected, then
# nothing will happen. This is extremely useful for 'delete' type of links.
# <%= link_to("Mack", "http://www.mackframework.com", :method => :delete, :confirm => "Are you sure?") %>
def link_to(link_text, url = link_text, html_options = {})
options = {:href => url}.merge(html_options)
a(link_text, options)
end
# Used in views to create href links. It takes link_text, url, and a Hash that gets added
# to the href as options.
#
# Examples:
# a("http://www.mackframework.com") # => http://www.mackframework.com
# a("Mack", :href => "http://www.mackframework.com") # => Mack
# a("Mack", :href => "http://www.mackframework.com", :target => "_blank") # => Mack
# a("Mack", :href => "http://www.mackframework.com", :target => "_blank", :rel => :nofollow) # => Mack
# If you pass in :method as an option it will be a JavaScript form that will post to the specified link with the
# methd specified.
# a("Mack", :href => "http://www.mackframework.com", :method => :delete)
# If you use the :method option you can also pass in a :confirm option. The :confirm option will generate a
# javascript confirmation window. If 'OK' is selected the the form will submit. If 'cancel' is selected, then
# nothing will happen. This is extremely useful for 'delete' type of links.
# a("Mack", :href => "http://www.mackframework.com", :method => :delete, :confirm => "Are you sure?")
def a(link_text, options = {})
options = {:href => link_text}.merge(options)
if options[:method]
meth = nil
confirm = nil
meth = %{var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var s = document.createElement('input'); s.setAttribute('type', 'hidden'); s.setAttribute('name', '_method'); s.setAttribute('value', '#{options[:method]}'); f.appendChild(s);f.submit()}
options.delete(:method)
if options[:confirm]
confirm = %{if (confirm('#{options[:confirm]}'))}
options.delete(:confirm)
end
options[:onclick] = (confirm ? (confirm + " { ") : "") << meth << (confirm ? (" } ") : "") << ";return false;"
end
content_tag(:a, options, link_text)
end
# Wraps an image tag with a link tag.
#
# Examples:
# <%= link_image_to("/images/foo.jpg", "#" %> # =>
def link_image_to(image_url, url, image_options = {}, html_options = {})
link_to(img(image_url, image_options), url, html_options)
end
# Builds a mailto href. By default it will generate
# JavaScript to help prevent phishing. To turn this off
# pass in the option :format => :plain
#
# mail_to("Saul Frami", "frami.saul@klocko.ca") # =>
#
def mail_to(text, email_address = nil, options = {})
email_address = text if email_address.blank?
options = {:format => :js}.merge(options)
format = options[:format]
options - [:format]
link = link_to(text, "mailto:#{email_address}", options)
if format == :js
y = ''
link.size.times {y << 'a'}
js_link = ""
return js_link
else
return link
end
end
end # LinkHelpers
end # ViewHelpers
end # Mack