Module: TagHelper
Instance Method Summary collapse
- #attributes(hash) ⇒ Object
- #content_tag(tag, value, attrs = {}) ⇒ Object
- #hidden_field_tag(name, value, html_options = {}) ⇒ Object
- #image_tag(src, html_options = {}) ⇒ Object
- #label_tag(label_for, label = nil, html_options = {}) ⇒ Object
- #tag_and_attributes(tag, attributes) ⇒ Object
- #text_field_tag(name, value, html_options = {}) ⇒ Object
- #unary_tag(tag, attrs = {}) ⇒ Object
Instance Method Details
#attributes(hash) ⇒ Object
33 34 35 36 37 |
# File 'lib/tag_helper.rb', line 33 def attributes(hash) hash.to_a. reject { |k,v| v.nil? }. map { |k, v| %{#{k}="#{v}"} }.join(' ') end |
#content_tag(tag, value, attrs = {}) ⇒ Object
23 24 25 26 27 |
# File 'lib/tag_helper.rb', line 23 def content_tag(tag, value, attrs = {}) start_tag = "<#{tag_and_attributes(tag, attributes(attrs))}>" end_tag = "</#{tag}>" [start_tag, value, end_tag].join end |
#hidden_field_tag(name, value, html_options = {}) ⇒ Object
15 16 17 |
# File 'lib/tag_helper.rb', line 15 def hidden_field_tag(name, value, = {}) unary_tag(:input, .merge(:id => name, :value => value, :type => 'hidden', :name => name)) end |
#image_tag(src, html_options = {}) ⇒ Object
3 4 5 |
# File 'lib/tag_helper.rb', line 3 def image_tag(src, = {}) unary_tag(:img, .merge(:src => src)) end |
#label_tag(label_for, label = nil, html_options = {}) ⇒ Object
7 8 9 |
# File 'lib/tag_helper.rb', line 7 def label_tag(label_for, label=nil, = {}) content_tag(:label, label || label_for, .merge(:for => label_for)) end |
#tag_and_attributes(tag, attributes) ⇒ Object
29 30 31 |
# File 'lib/tag_helper.rb', line 29 def tag_and_attributes(tag, attributes) attributes.empty?? tag : "#{tag} #{attributes}" end |
#text_field_tag(name, value, html_options = {}) ⇒ Object
11 12 13 |
# File 'lib/tag_helper.rb', line 11 def text_field_tag(name, value, = {}) unary_tag(:input, .merge(:id => name, :value => value, :type => 'text', :name => name)) end |
#unary_tag(tag, attrs = {}) ⇒ Object
19 20 21 |
# File 'lib/tag_helper.rb', line 19 def unary_tag(tag, attrs = {}) "<#{tag_and_attributes(tag, attributes(attrs))} />" end |