Skip to content

Instantly share code, notes, and snippets.

@phinze
Forked from butzopower/gist:122320
Created June 2, 2009 15:57
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 phinze/122321 to your computer and use it in GitHub Desktop.
Save phinze/122321 to your computer and use it in GitHub Desktop.
# What is worse?
<%= f.table "Bill Dates", @bill_dates do |t|
t.date
t.billed?
end %>
# Or
<% f.table "Bill Dates", @bill_dates do |t| %>
<%= t.add_field(:date) %>
<%= t.billed? %>
<% end %>
# Why can't we
<% f.table "Bill Dates", @bill_dates do |t| %>
<%= t.date %>
<%= t.billed? %>
<% end %>
def table(title, collection, &block)
rowcatcher = Class.new()
rowcatcher.class_eval do
attr_accessor :fields
def initialize
@fields = []
end
collection.first.class.columns do |c|
define_method(c.name) { @fields << c.name }
end
end
rc = rowcatcher.new
yield rc
xhtml = ctag(:tr) do
collection.each do |c|
rc.fields.each do |f|
ctag(:td c.send(f)))
end
end
end
concat(xhtml)
end
<% uiris_form_for :animal_orders, :no_form_tag => true do |f| %>
<% f.table(@collection) do |t| %>
<%= t.column(:bar) { |row, _| number_to_crazy_format(val) } %>
<%= t.column(:baz, :header => "Baz Baz") %>
<%= t.column(:qux_id, :sort => true) { |_, val| f.object.orders.find(val).title } %>
<%= t.column(:quux) %>
<% end %>
<%#
def table(method_or_collection, options, &block)
collection = method_or_collection.is_a?(Symbol) ? try_to_call(method_or_collection) \
: method_or_collection
rowcatcher = Class.new()
rowcatcher.class_eval do
attr_accessor :columns
def initialize
@columns = []
end
def column(method, options, &block)
col = {
:method => method,
:options => options.dup,
:lambda => block
}
@columns << col
end
end
rc = rowcatcher.new
yield rc
# Headers
xhtml = ctag(:tr) do
end
# Rows
xhtml = ctag(:tr) do
collection.each do |c|
rc.fields.each do |f|
ctag(:td c.send(f)))
end
end
end
concat(ctag(:table, xhtml))
end
%>
<% f.table(nil, list_orders, :paginate_prefix => prefix) do |table| %>
<% table.show(:header => false, :send => false) do |c| %>
<%= link_to 'Show', c %>
<% end %>
<% table.edit(:header => false, :send => false) do |c| %>
<%= link_to 'Edit', edit_animal_order_path(c) %>
<% end %>
<% if local_assigns.has_key?(:include_destroy_link) && include_destroy_link == true %>
<% table.delete(:header => false, :send => false) do |order| %>
<%= link_to 'Delete', order, :method => :delete, :confirm => "Are you sure you want to delete this order?" %>
<% end %>
<% end %>
<% if local_assigns.has_key?(:include_template_link) && include_template_link == true %>
<% table.new_from_template(:header => false, :send => false) do |order| %>
<%= link_to 'New from Template', new_animal_order_path(:template_id => order.id) %>
<% end %>
<% end %>
<% table.account_number :send => false, :sort => false do |order| %>
<%=h order.account.account_number unless order.account.nil? %>
<% end %>
<% table.pi_species_specs :send => false, :sort => false do |order| %>
<%=h order.pi.display_name unless order.pi.nil? %><br />
<%=h order.species.accounting_code unless order.species.nil? %> <%=h order.requested_sex %> <%=h order.requested_age %> <%=h order.requested_weight %>
<% end %>
<% table.arrival_date :send => false, :sort => false do |order| %>
<%=h order.asap? ? "ASAP" : order.arrival_date.strftime("%m/%d/%y") %>
<% end %>
<% table.quantity %>
<% table.vendor_name :send => false, :sort => false do |order| %>
<%=h order.vendor.name unless order.vendor.nil? %>
<% end %>
<% table.ordering_user :send => false, :sort => false do |order| %>
<%=h order.ordering_user.display_name unless order.ordering_user.nil? %>
<% end %>
<% table.location :send => false, :sort => false do |order| %>
<%=h order.location.building unless order.location.nil? %> <%=h order.location.room unless order.location.nil? %>
<% end %>
<% end %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment