Class: Mack::Utils::Html
- Inherits:
-
Object
- Object
- Mack::Utils::Html
- Defined in:
- lib/utils/html.rb
Overview
Useful utilities for dealing with HTML.
Class Method Summary collapse
-
.content_tag(tag, content, options = {}) ⇒ Object
Builds an HTML tag.
-
.href(link_text, url = link_text, html_options = {}) ⇒ Object
(also: a)
Used in views to create href links.
-
.image_tag(image_src, options = {}) ⇒ Object
Builds a HTML image tag.
-
.method_missing(sym, *args) ⇒ Object
Wraps the content_tag method.
-
.rss(url) ⇒ Object
A wrapper to generate an auto discovery tag so browsers no the page contains an RSS feed.
Class Method Details
.content_tag(tag, content, options = {}) ⇒ Object
Builds an HTML tag.
Examples:
content_tag(:b, "hello") # => <b>hello</b>
content_tag("div", "hello world!", :class => :foo) # => <div class="foo">hello world!</div>
88 89 90 |
# File 'lib/utils/html.rb', line 88 def content_tag(tag, content, = {}) html = "<#{tag} #{.join("%s=\"%s\"", " ")}>#{content}</#{tag}>" end |
.href(link_text, url = link_text, html_options = {}) ⇒ Object Also known as: a
Used in views to create href links. It takes link_text, url, and a Hash that gets added to the href as options.
Examples:
Mack::Utils::Html.href("http://www.mackframework.com") # => <a href="http://www.mackframework.com">http://www.mackframework.com</a>
Mack::Utils::Html.href("Mack", "http://www.mackframework.com") # => <a href="http://www.mackframework.com">Mack</a>
Mack::Utils::Html.href("Mack", "http://www.mackframework.com", :target => "_blank") # => <a href="http://www.mackframework.com" target="_blank">Mack</a>
Mack::Utils::Html.href("Mack", "http://www.mackframework.com", :target => "_blank", :rel => :nofollow) # => <a href="http://www.mackframework.com" target="_blank" rel="nofollow">Mack</a>
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.
Mack::Utils::Html.href("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.
Mack::Utils::Html.href("Mack", "http://www.mackframework.com", :method => :delete, :confirm => "Are you sure?")
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/utils/html.rb', line 23 def href(link_text, url = link_text, = {}) if [: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', '#{[:method]}'); f.appendChild(s);f.submit()} .delete(:method) if [:confirm] confirm = %{if (confirm('#{[:confirm]}'))} .delete(:confirm) end [:onclick] = (confirm ? (confirm + " { ") : "") << meth << (confirm ? (" } ") : "") << ";return false;" end html = "<a href=" << '"' << url html << '"' html << " " << .join("%s=\"%s\"", " ") unless .empty? html << ">" << link_text html << "</a>" html end |
.image_tag(image_src, options = {}) ⇒ Object
Builds a HTML image tag.
94 95 96 97 98 |
# File 'lib/utils/html.rb', line 94 def image_tag(image_src, = {}) html = "<img src=\"#{image_src}\"" html << " " << .join("%s=\"%s\"", " ") unless .empty? html << ">" end |
.method_missing(sym, *args) ⇒ Object
Wraps the content_tag method.
Examples:
Mack::Utils::Html.b("hello") # => <b>hello</b>
Mack::Utils::Html.div("hello world!", :class => :foo)) # => <div class="foo">hello world!</div>
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/utils/html.rb', line 62 def method_missing(sym, *args) ags = args.parse_splat_args tag = sym content = nil = {} if ags.is_a?(Array) content = ags[0] if ags[0].is_a?(String) = ags[1] if ags[1].is_a?(Hash) elsif ags.is_a?(String) content = ags elsif ags.is_a?(Hash) = ags end content = yield if block_given? content_tag(tag, content, ) end |
.rss(url) ⇒ Object
A wrapper to generate an auto discovery tag so browsers no the page contains an RSS feed.
Example:
<%= Mack::Utils::Html.rss(posts_index_url(:format => :xml)) %>
53 54 55 |
# File 'lib/utils/html.rb', line 53 def rss(url) "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"RSS\" href=\"#{url}\">" end |