Module: TagHelper
- Defined in:
- lib/tag_helper.rb
Overview
XHTML tags builder
Class Method Summary collapse
- .attributes(hash) ⇒ Object
- .content(tag, value, attrs = {}) ⇒ Object
- .hidden_field(name, value, html_options = {}) ⇒ Object
- .iframe(html_options = {}) ⇒ Object
- .image(src, html_options = {}) ⇒ Object
- .label(label_for, label = nil, html_options = {}) ⇒ Object
- .tag_and_attributes(tag, attributes) ⇒ Object
- .text_field(name, value, html_options = {}) ⇒ Object
- .unary(tag, attrs = {}) ⇒ Object
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, = {}) unary( :input, .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( = {}) unary(:iframe, ) end |
.image(src, html_options = {}) ⇒ Object
5 6 7 8 9 |
# File 'lib/tag_helper.rb', line 5 def image(src, = {}) unary( :img, .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, = {}) content( :label, label || label_for, .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, = {}) unary( :input, .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 |