Created
September 10, 2008 14:12
-
-
Save augustl/9893 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% 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