Module: HTML

Defined in:
lib/carat/html-helpers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.CGI_include!Object



26
27
28
29
# File 'lib/carat/html-helpers.rb', line 26

def self.CGI_include!
  require 'cgi'
  CGI.class_eval { include XHTML }
end

.FCGI_include!Object



31
32
33
34
# File 'lib/carat/html-helpers.rb', line 31

def self.FCGI_include!
  require 'fcgi'
  FCGI.class_eval { include XHTML }
end

Instance Method Details

#esc(str) ⇒ Object

Return an html “safe” version of the string, where every &, < and > are replaced with appropriate entities.



57
58
59
# File 'lib/carat/html-helpers.rb', line 57

def esc(str)
  str.gsub(/&/,'&amp;').gsub(/</,'&lt;').gsub(/>/,'&gt;')
end

#escformat(str) ⇒ Object

Calls #esc, and then further replaces carriage returns and quote characters with entities.



62
63
64
# File 'lib/carat/html-helpers.rb', line 62

def escformat(str)
  xmlsafe(str).gsub(/[\r\n]+/,'&#13;&#10;').gsub(%r|"|,'&quot;').gsub(%r|'|,'&#39;')
end

#marshal_from_html(name) ⇒ Object

Create an hidden input field throught which an object can can be marshalled. This makes it very easy to pass from data betwenn requests.



49
50
51
# File 'lib/carat/html-helpers.rb', line 49

def marshal_from_html(name)
  return Marshal.load(CGI.unescape(self["__#{name}__"][0])) if self.params.has_key?("__#{name}__")
end

#marshal_to_html(name, iobj) ⇒ Object

Create an hidden input field through which an object can can be marshalled. This makes it very easy to pass from data betwenn requests.



42
43
44
45
# File 'lib/carat/html-helpers.rb', line 42

def marshal_to_html(name, iobj)
  data = CGI.escape(Marshal.dump(iobj))
  return %Q{<input type="hidden" name="__#{name}__" value="#{data}"/>\n}
end

#render_object(o, collection, edit) ⇒ Object

Renders an object with annotations: tinyurl.com/6xjnj Creates a table rendering o‘s attributes.



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/carat/html-helpers.rb', line 68

def render_object(o, collection, edit)
  r = "<table>\n"
  o.instance_variables.each do |attr_name| 
  attr_value = o.instance_variable_get(attr_name)
  attr_anns = o.class.send(attr_name[1..-1])
    r << "  <tr>\n"
    r << "    <td>" << attr_anns[:description] << "</td>\n"
    r << "    <td>" << text_or_input(edit, :name => "#{collection}[#{o.class.name}][#{attr_name}]", :value => attr_value) << "</td>\n"
    r << "  </tr>\n"
  end
  r << "</table>"
  r
end