<% table.define :table do |options| %> <%= content_tag :table, options[:table_html] 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(table, 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) %> <% table.define(:edit_header) {} %> <% table.define(:show_header) {} %> <% table.define(:delete_header) {} %> <%# 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| %> <% if options[:sortable] %> <%= table_for_sort_link(column, options) %> <% else %> <%= options[:label] ? options[:label] : column.name.to_s.titleize %> <% end %> <% 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, table.evaluated_procs(options[:row_html], record) do %> <%= table.render :column, record, :collection => table.columns %> <% end %> <% end %> <% table.define :column do |column, record, options| %> <%= content_tag :td, table.evaluated_procs(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] ? table.evaluated_proc(options[:url], record) : [options[:action], options[:scope], record].flatten %> <%= link_to options[:link_label], url, options[:link_html] %> <% end %> <% table.define :show, :action => nil, :link_label => "Show" do |record, column, options| %> <% url = options[:url] ? table.evaluated_proc(options[:url], record) : [options[:action], options[:scope], record].flatten %> <%= link_to options[:link_label], url, options[:link_html] %> <% end %> <% table.define :delete, :action => nil, :link_html => {}, :link_label => "Delete" do |record, column, options| %> <% url = options[:url] ? table.evaluated_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_to options[:link_label], 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| %> <%= record.send(column.name) %> <% end %> <%= table.render :table %>