Permalink
Browse files

Fixed indentation

  • Loading branch information...
1 parent 5a73eba commit e04229624b4176b7bb75a5656ab670c5b4ca2e79 Iain Hecker committed Nov 5, 2008
Showing with 57 additions and 30 deletions.
  1. +20 −6 lib/model_based_html/base.rb
  2. +37 −24 lib/model_based_html/table.rb
@@ -12,9 +12,9 @@ def default(type, object, template, &block)
object = object.to_s.camelize.constantize.new if object.is_a?(Symbol)
@object = object
@template = template
- concat(start_tag(type, :object_html => @object))
+ open_tag(start_tag(type, :object_html => @object))
yield self
- concat("</#{type}>")
+ close_tag("</#{type}>")
end
def start_tag(type, options = {})
@@ -27,17 +27,17 @@ def h(string)
end
def value_tag(tag, method_or_value, options, &block)
- concat(start_tag(tag, attributes(method_or_value, options)))
+ open_tag(start_tag(tag, attributes(method_or_value, options)))
concat_or_yield(value(method_or_value), "</#{tag}>", &block)
end
def value_tag_h(tag, method_or_value, options, &block)
- concat(start_tag(tag, attributes(method_or_value, options)))
+ open_tag(start_tag(tag, attributes(method_or_value, options)))
concat_or_yield(h(value(method_or_value)), "</#{tag}>", &block)
end
def name_tag(tag, method_or_value, options, &block)
- concat(start_tag(tag, attributes(method_or_value, options)))
+ open_tag(start_tag(tag, attributes(method_or_value, options)))
concat_or_yield(name(method_or_value), "</#{tag}>", &block)
end
@@ -79,7 +79,7 @@ def concat_or_yield(*args, &block)
else
concat(str)
end
- concat(args.join) unless args.empty?
+ close_tag(args.join) unless args.empty?
end
def value(method_or_value)
@@ -96,11 +96,25 @@ def name(method_or_value)
# echo it in your views.
# <% dl.dd :name %> will be the same as <%= dl.dd :name %>
def concat(*args)
+ @tags_opened ||= 0
@template.concat("\n")
+ @template.concat(" " * (@tags_opened))
@template.concat(*args)
""
end
+ def open_tag(*args)
+ @tags_opened ||= 0
+ concat(*args)
+ @tags_opened += 1
+ end
+
+ def close_tag(*args)
+ @tags_opened ||= 1
+ @tags_opened = (@tags_opened - 1).abs
+ concat(*args)
+ end
+
end
end
@@ -23,41 +23,40 @@ def initialize(type, collection, template, options = {}, &block)
def thead(options = {}, &block)
return "" if @collection.empty? and not @force
reset_cell_count
- concat(start_tag(:thead, options))
+ open_tag(start_tag(:thead, options))
@inside_thead = true
yield
@inside_thead = false
- concat("</thead>")
+ close_tag("</thead>")
end
def tbody(options = {}, &block)
- return "" if @collection.empty? and not @force
- reset_cell_count
- sanitize = options.delete(:sanitize) or false
- concat(start_tag(:tbody, options))
-
- if @collection.empty? and @force
- tr(@object) do
- td(empty_collection_message, :colspan => table_width)
- end
- else
+ when_not_empty do
+ reset_cell_count
+ sanitize = options.delete(:sanitize) or false
+ open_tag(start_tag(:tbody, options))
+
if block_given?
@collection.each do |object|
@object = object
yield object
end
else
- raise ArgumentError, "No columns defined for automatic table making" if @columns.nil? or @columns.empty?
+ raise ArgumentError, "No columns defined. Make a thead with at least 1 th." if @columns.nil? or @columns.empty?
@collection.each do |o|
tr(o) do
@columns.each do |column|
- sanitize ? td_h(o.send(column)) : td(o.send(column))
+ if columns.is_a?(Symbol)
+ sanitize ? td_h(o.send(column)) : td(o.send(column))
+ else
+ td('')
+ end
end
end
end
end
+ close_tag("</tbody>")
end
- concat("</tbody>")
end
def td(method_or_value = nil, options = {}, &block)
@@ -77,33 +76,47 @@ def th(method_or_value = nil, options = {}, &block)
name_tag(:th, method_or_value, options, &block)
end
- def tr(object = @object, &block)
+ def tr(options = {}, &block)
reset_cell_count
- concat(start_tag(:tr, :class => odd_or_even, :object_html => object))
+ options = {:object_html => @object}.merge(options)
+ options.update(:class => odd_or_even) unless options[:class]
+ open_tag(start_tag(:tr, options))
yield
- concat("</tr>")
+ close_tag("</tr>")
end
def odd_or_even
- @template.cycle("odd", "even", :name => "table_#{@object.object_id}")
+ @template.cycle("odd", "even", :name => "table_#{self.object_id}")
end
- def empty(&block)
+ def when_empty(message = nil, &block)
if @collection.empty?
if @force
- tr do
- td(:colspan => table_width) do
- yield empty_collection_message
+ tr(:class => "empty_collection_message", :object_html => nil) do
+ td('', :colspan => @table_width) do
+ concat(yield_or_return_empty_collection_message(message, &block))
end
end
else
- yield empty_collection_message
+ concat(yield_or_return_empty_collection_message(message, &block))
end
end
end
+ def when_not_empty(&block)
+ yield unless @collection.empty?
+ end
+
private
+ def yield_or_return_empty_collection_message(message = nil, &block)
+ if block_given?
+ yield empty_collection_message
+ else
+ return (message or empty_collection_message)
+ end
+ end
+
def empty_collection_message
translate_options = {
:scope => :tables,

0 comments on commit e042296

Please sign in to comment.