<% table.define :table do |options| %> <%= content_tag :table, table_for_table_html(options) do %> <%= table.render :header %> <%= table.render :body %> <%# Purposely no default implementation for the tfoot block given, since most tables do not have footers. Block provided to for easy hook to add a footer %> <%= table.render :footer %> <% end %> <% end %> <% table.define :header do |options| %> <%= content_tag :thead, options[:thead_html] do %> <%= table.render :header_row %> <% end %> <% end %> <% table.define :header_row do |options| %> <%= content_tag :tr, options[:header_row_html] do %> <%= table.render :header_column, :collection => table.columns %> <% end %> <% end %> <% table.define :header_column do |column, options| %> <%= content_tag :th, table_for_header_html(column, options) do %> <%= table.render "#{column.name}_header", column, column.options %> <% end %> <% end %> <%# Define header blocks for the edit, show, and delete columns (i.e. we want those columns to display with a blank header) %> <% [:edit, :show, :delete].each do |action| %> <% table.define "#{action}_header", :label => false, :order => :id do |column, options| %> <%= table_for_header_cell_data(column, options) %> <% end %> <% end %> <%# Define a header block for each column, named using that column's name with the word "_header" appended to it %> <% table.define lambda {|column| "#{column.name}_header" }, :collection => table.columns do |column, options| %> <%= table_for_header_cell_data(column, options) %> <% end %> <% table.define :body do |options| %> <%= content_tag :tbody, options[:tbody_html] do %> <%= table.render :row, :collection => records %> <% end %> <% end %> <% table.define :row do |record, options| %> <% table.set_current_record(record) %> <%= content_tag :tr, call_each_hash_value_if_proc(options[:row_html], record) do %> <%= table.render :column, record, :collection => table.columns %> <% end %> <% end %> <% table.define :column do |column, record, options| %> <%= content_tag :td, call_each_hash_value_if_proc(options[:column_html], record, column) do %> <%= table.render column.name, record, column, column.options %> <% end %> <% end %> <% table.define :edit, :action => :edit, :link_label => "Edit" do |record, column, options| %> <% url = options[:url] ? call_if_proc(options[:url], record) : [options[:action], options[:scope], record].flatten %> <% link_name = options[:link_field] ? record.send(options[:link_field]) : options[:link_label] %> <%= link_to call_if_proc(link_name, record), url, options[:link_html] %> <% end %> <% table.define :show, :action => nil, :link_label => "Show" do |record, column, options| %> <% url = options[:url] ? call_if_proc(options[:url], record) : [options[:action], options[:scope], record].flatten %> <% link_name = options[:link_field] ? record.send(options[:link_field]) : options[:link_label] %> <%= link_to call_if_proc(link_name, record), url, options[:link_html] %> <% end %> <% table.define :delete, :action => nil, :link_html => {}, :link_label => "Delete" do |record, column, options| %> <% url = options[:url] ? call_if_proc(options[:url], record) : [options[:action], options[:scope], record].flatten %> <% confirm = options[:confirm] ? options[:confirm] : "Are you sure you want to delete this #{record.class.to_s.titleize}?" %> <% method = options[:method] ? options[:method] : "delete" %> <% link_name = options[:link_field] ? record.send(options[:link_field]) : options[:link_label] %> <%= link_to call_if_proc(link_name, record), url, {:method => method, :confirm => confirm}.merge(options[:link_html]) %> <% end %> <%# Define a block for each column, named using that column's name %> <% table.define lambda {|column| column.name }, :collection => table.columns do |record, column, options| %> <%= table_for_cell_data(record, column, options) %> <% end %> <%= table.render :table %>