Module: HappyHelpers::Helpers::Html

Included in:
HappyHelpers::Helpers
Defined in:
lib/happy-helpers/helpers/html.rb

Instance Method Summary collapse

Instance Method Details

#capture(*args, &block) ⇒ Object



43
44
45
46
# File 'lib/happy-helpers/helpers/html.rb', line 43

def capture(*args, &block)
  # TODO: support more than just HAML. :P~
  capture_haml(*args, &block)
end

#escape_html(t) ⇒ Object



16
17
18
19
# File 'lib/happy-helpers/helpers/html.rb', line 16

def escape_html(t)
  #Rack::Utils.escape_html(t.to_s)
  CGI::escape_html(t.to_s)
end

#html_tag(name, options = nil, escape = true, &block) ⇒ Object



4
5
6
# File 'lib/happy-helpers/helpers/html.rb', line 4

def html_tag(name, options = nil, escape = true, &block)
  "<#{name} #{html_tag_attributes(options, escape) if options}#{block_given? ? ">#{yield if block_given?}</#{name}>" : " />"}"
end

#html_tag_attributes(options, escape = true) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/happy-helpers/helpers/html.rb', line 8

def html_tag_attributes(options, escape = true)
  options.map do |k,v|
    if v
      v == true ? "#{k}" : "#{k}=\"#{ escape_html(v) }\""
    end
  end.compact.join(" ")
end


25
26
27
28
# File 'lib/happy-helpers/helpers/html.rb', line 25

def link_to(name, *target)
  options = target.last.is_a?(Hash) ? target.pop : {}
  html_tag(:a, options.merge(href: url_for(*target))) { name }
end

#preserve(t) ⇒ Object



21
22
23
# File 'lib/happy-helpers/helpers/html.rb', line 21

def preserve(t)
  t.chomp("\n").gsub(/\n/, '&#x000A;').gsub(/\r/, '')
end

#url_for(*what) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/happy-helpers/helpers/html.rb', line 30

def url_for(*what)
  return what.first if what.size == 1 && what.first.is_a?(String)

  what.flatten.inject('') do |url, item|
    url << "/%s" % case item
      when String, Symbol then item.to_s
      else "%s/%s" % [item.class.to_s.tableize.pluralize, item.try(:to_param) || item.try(:to_id) || item.try(:id)]
    end

    url
  end
end