module Mack module ViewHelpers module HtmlHelpers # This is just an alias to # # 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 # Builds an HTML tag. # # Examples: # content_tag(:b) {"hello"} # => hello # content_tag("div", :class => :foo) {"hello world!"} # =>