Skip to content

Instantly share code, notes, and snippets.

@augustl
Created September 10, 2008 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save augustl/9893 to your computer and use it in GitHub Desktop.
Save augustl/9893 to your computer and use it in GitHub Desktop.
def table_listing(collection, &block)
row = RowHandler.new
capture(row, &block)
headers = "<tr>" + row.columns.map {|name, proc| "<th>#{name}</th>" }.join("\n") + "</tr>"
if collection.empty?
rows = "<tr><td colspan=\"#{row.columns.size}\">Ingen treff</td></tr>"
else
rows = collection.map do |item|
output = "<tr>"
row.columns.each do |name, proc|
output << "<td>"
#output << proc.call(item) - this failz
output << "this is a row"
output << "</td>"
end
output << "</tr>"
end.join("\n")
end
concat(content_tag(:table, headers + rows), block.binding)
end
class RowHandler
attr_reader :columns
def initialize
@columns = []
end
def add(name = nil, &block)
@columns << [name, block]
end
end
<% table_listing @users do |c| %>
<%= c.add('Username') {|u| link_to(u.username, user_path(u))} %>
<%= c.add('E-mail')(&:email) %>
<%= c.add('Full name')(&:full_name) %>
<% c.add do %>
<%= button_to 'Destroy', user_path(u), :method => :delete %>
<% end %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment