Class: Mack::Utils::Html

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/html.rb

Overview

Useful utilities for dealing with HTML.

Class Method Summary collapse

Class Method Details

.content_tag(tag, content, options = {}) ⇒ Object

Builds an HTML tag.

Examples:

(:b, "hello") # => <b>hello</b>
("div", "hello world!", :class => :foo) # => <div class="foo">hello world!</div>


88
89
90
# File 'lib/utils/html.rb', line 88

def (tag, content, options = {})
  html = "<#{tag} #{options.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, html_options = {})
  if html_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', '#{html_options[:method]}'); f.appendChild(s);f.submit()}
    html_options.delete(:method)

    if html_options[:confirm]
      confirm = %{if (confirm('#{html_options[:confirm]}'))}
      html_options.delete(:confirm)
    end

    html_options[:onclick] = (confirm ? (confirm + " { ") : "") << meth << (confirm ? (" } ") : "") << ";return false;"
  end

  html = "<a href=" << '"' << url
  html << '"'
  html << " " << html_options.join("%s=\"%s\"", " ") unless html_options.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, options = {})
  html = "<img src=\"#{image_src}\""
  html << " " << options.join("%s=\"%s\"", " ") unless options.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
  options = {}
  
  if ags.is_a?(Array)
    content = ags[0] if ags[0].is_a?(String)
    options = ags[1] if ags[1].is_a?(Hash)
  elsif ags.is_a?(String)
    content = ags
  elsif ags.is_a?(Hash)
    options = ags
  end
  
  content = yield if block_given?
  
  (tag, content, options)
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