Skip to content

Instantly share code, notes, and snippets.

@jakl
Last active January 4, 2018 14:34
Show Gist options
  • Save jakl/6612826 to your computer and use it in GitHub Desktop.
Save jakl/6612826 to your computer and use it in GitHub Desktop.
Rails Table Partial
<td width="<%= width %>">
<% if cell %>
<%= render partial: cell_partial, object: cell %>
<% end %>
</td>
<tr>
<%= render partial: 'cell', collection: row,
locals: {
cell_partial: cell_partial,
width: "#{100/row.length}%",
} %>
</tr>
<% # Nil elements in the rows will be rendered as empty cells %>
<table class="table table-no-hover table-bordered full-width table-show-empty">
<tr><td colspan="100%">
<%= render partial: table[:header_partial], object: table[:header],
as: 'header' %>
</td></tr>
<%= render partial: 'row', collection: table[:rows],
locals: { cell_partial: table[:cell_partial] } %>
</table>
<div><%= thing.name %></div>
<div><%= thing.description %></div>
<div>We have <%= count %> things</div>
things = Thing.all # My self descriptive model
@table = {
header_partial: 'thing_header',
header: {
count: things.length,
},
cell_partial: 'thing',
rows: groups_of(2, things),
}
def groups_of(length, arr)
arr.reduce([[]]) do |group, elem|
if group.last.length != length
group.last.push(elem)
else
group.push([elem])
end
group
end.tap do |groups|
# Fill in gaps in last group with nils
(groups.last.length...length).each do
groups.last.push(nil)
end
end
end
.table-show-empty {
empty-cells: show;
}
.full-width {
width: 100%;
}
.table-no-hover {
tbody tr:hover td,
tbody tr:hover th {
background-color: transparent;
}
}
@jakl
Copy link
Author

jakl commented Sep 18, 2013

For CSS:
table-bordered is from boostrap
table-no-hover was just my personal bug, might do nothing in isolation like this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment