Module: TagHelper

Extended by:
TagHelper
Included in:
TagHelper
Defined in:
lib/tag_helper.rb

Instance Method Summary collapse

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 (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, html_options = {})
  unary_tag(:input, html_options.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, html_options = {})
  unary_tag(:img, html_options.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, html_options = {})
  (:label, label || label_for, html_options.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, html_options = {})
  unary_tag(:input, html_options.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