Skip to content

Instantly share code, notes, and snippets.

@sbellware
Created November 16, 2010 07:24
Show Gist options
  • Select an option

  • Save sbellware/701556 to your computer and use it in GitHub Desktop.

Select an option

Save sbellware/701556 to your computer and use it in GitHub Desktop.
Table helpers for HAML
module LayoutHelper
def layout(options={}, &block)
options.prepend! 'layout', :class
capture_haml do
haml_tag(:table, options) do
haml_concat capture_haml(&block)
end
end
end
def row(options={}, &block)
options.prepend! 'layout', :class
html = to_html(&block)
capture_haml do
haml_tag(:tr, options) do
if html.contains_td?
haml_concat html
else
wrap_with_column &block
end
end
end
end
def column(options={}, &block)
capture_haml do
wrap_with_column options, &block
end
end
def wrap_with_column(options={}, &block)
options.prepend! 'layout', :class
haml_tag(:td, options) do
haml_concat capture_haml(&block)
end
end
private
def to_html(&block)
html = capture_haml(&block)
def html.contains_td?
not (self =~ /<td.+<\/td>/mi).nil?
end
html
end
end
class Hash
def prepend!(value, key)
return replace_value(key, value) do |value, current_value|
"#{value} #{current_value}"
end
end
def append!(value, key)
return replace_value(key, value) do |value, current_value|
"#{current_value} #{value}"
end
end
private
def replace_value(key, value)
if not key? key
new_value = "#{value}"
else
current_value = delete(key).split.reject { |w| w == value }.join(' ')
new_value = yield(value, current_value).strip
end
merge! key => new_value
return new_value
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment