Permalink
Browse files

Do not accept invalid tags

  • Loading branch information...
1 parent 04f6c6d commit 896a53bf573687714b0fd85666be175111fcfa8c Joe Fiorini committed Jan 15, 2010
Showing with 56 additions and 10 deletions.
  1. +8 −10 lib/totally_tabular/html_helper.rb
  2. +48 −0 lib/totally_tabular/table_view.rb
@@ -2,17 +2,15 @@ module TotallyTabular
class HtmlHelper
def tag(tag, content="")
- if tag.to_s.empty?
- ""
+ case tag.to_s
+ when "br"
+ "<br>"
+ when "input"
+ "<input>"
+ when /^\w+$/
+ "<%s>%s</%s>" % [tag, content, tag]
else
- case tag
- when :br
- "<br>"
- when :input
- "<input>"
- else
- "<%s>%s</%s>" % [tag, content, tag]
- end
+ ""
end
end
end
@@ -1,4 +1,52 @@
+require File.dirname(__FILE__) + '/table'
+require File.dirname(__FILE__) + '/column'
+require File.dirname(__FILE__) + '/row'
+
module TotallyTabular
class TableView
+
+ def initialize(collection, options={}, &block)
+ @collection = collection
+ @options = options
+ @column_definition = block
+ end
+
+ def render
+ table = Table.new
+ table.instance_eval(&@column_definition)
+ headers = if @collection.empty?
+ ""
+ else
+ build_headers(table.columns)
+ end
+ rows = build_rows(@collection, table.columns)
+ tbody = content_tag(:tbody, rows.join)
+ thead = content_tag(:thead, headers)
+ tfoot = content_tag(:tfoot)
+ content_tag(:table, [thead, tbody, tfoot], options)
+ end
+
+ def options
+ @options
+ end
+
+ private
+
+ def build_rows(collection, columns)
+ collection.map do |item|
+ row = Row.new
+ row.render(columns.map do |column|
+ content_tag(:td, column.template.call(item, row))
+ end.join)
+ end
+ end
+
+ def build_headers(columns)
+ headers = columns.map do |column|
+ content_tag(:th, column.name, column.header_attributes)
+ end
+ Row.new.render(headers)
+ end
+
end
end

0 comments on commit 896a53b

Please sign in to comment.