Module: TagHelper

Defined in:
lib/tag_helper.rb

Overview

XHTML tags builder

Class Method Summary collapse

Class Method Details

.attributes(hash) ⇒ Object



56
57
58
59
60
# File 'lib/tag_helper.rb', line 56

def attributes(hash)
  hash.to_a
    .reject { |_k, v| v.nil? }
    .map { |k, v| %(#{k}="#{v}") }.join(' ')
end

.content(tag, value, attrs = {}) ⇒ Object



46
47
48
49
50
# File 'lib/tag_helper.rb', line 46

def content(tag, value, attrs = {})
  start_tag = "<#{tag_and_attributes(tag, attributes(attrs))}>"
  end_tag   = "</#{tag}>"
  [start_tag, value, end_tag].join
end

.hidden_field(name, value, html_options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/tag_helper.rb', line 28

def hidden_field(name, value, html_options = {})
  unary(
    :input,
    html_options.merge(
      id: name,
      value: value,
      type: 'hidden',
      name: name))
end

.iframe(html_options = {}) ⇒ Object



38
39
40
# File 'lib/tag_helper.rb', line 38

def iframe(html_options = {})
  unary(:iframe, html_options)
end

.image(src, html_options = {}) ⇒ Object



5
6
7
8
9
# File 'lib/tag_helper.rb', line 5

def image(src, html_options = {})
  unary(
    :img,
    html_options.merge(src: src))
end

.label(label_for, label = nil, html_options = {}) ⇒ Object



11
12
13
14
15
16
# File 'lib/tag_helper.rb', line 11

def label(label_for, label = nil, html_options = {})
  content(
    :label,
    label || label_for,
    html_options.merge(for: label_for))
end

.tag_and_attributes(tag, attributes) ⇒ Object



52
53
54
# File 'lib/tag_helper.rb', line 52

def tag_and_attributes(tag, attributes)
  attributes.empty? ? tag : "#{tag} #{attributes}"
end

.text_field(name, value, html_options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/tag_helper.rb', line 18

def text_field(name, value, html_options = {})
  unary(
    :input,
    html_options.merge(
      id: name,
      value: value,
      type: 'text',
      name: name))
end

.unary(tag, attrs = {}) ⇒ Object



42
43
44
# File 'lib/tag_helper.rb', line 42

def unary(tag, attrs = {})
  "<#{tag_and_attributes(tag, attributes(attrs))} />"
end