require 'table_helper/collection_table' # Provides a set of methods for turning a collection into a table. # # == Basic Example # # This example shows the most basic usage of +collection_table+ which takes # information about a collection, the objects in them, the columns defined # for the class, and generates a table based on that. # # Suppose you have a table generated by a migration like so: # # class CreatePeople < ActiveRecord::Base # def self.up # create_table do |t| # t.string :first_name # t.string :last_name # t.integer :company_id # t.string :role # end # end # end # # ...then invoking the helper within a view: # # <%= collection_table Person.find(:all) %> # # ...is compiled to (formatted here for the sake of sanity): # # # # # # # # # # # # # # # # # # # # # # # # #
First NameLast NameCompanyRole
JohnDoe1President
JaneDoe1Vice-President
# # == Advanced Example # # This example below shows how +collection_table+ can be customized to show # specific headers, content, and footers. # # <%= # collection_table(@posts, {}, :id => 'posts', :class => 'summary') do |header, body| # header.column :title # header.column :category # header.column :author # header.column :publish_date, 'Date
Published' # header.column :num_comments, '# Comments' # header.column :num_trackbacks, '# Trackbacks' # # body.alternate = true # body.build do |row, post, index| # row.category post.category.name # row.author post.author.name # row.publish_date time_ago_in_words(post.published_on) # row.num_comments post.comments.empty? ? '-' : post.comments.size # row.num_trackbacks post.trackbacks.empty? ? '-' : post.trackbacks.size # end # end # %> # # ...is compiled to (formatted here for the sake of sanity): # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
TitleCategoryAuthorDate
Published
# Comments# Trackbacks
Open-source projects: The good, the bad, and the uglyGeneralJohn Doe23 days--
5 reasons you should care about RailsRailsJohn Q. Public21 days--
Deprecation: Stop digging yourself a holeRailsJane Doe17 days--
Jumpstart your Rails career at RailsConf 2007ConferencesJane Doe4 days--
Getting some RESTRailsJohn Doeabout 18 hours--
# # == Creating footers # # Footers allow you to show some sort of summary information based on the # data displayed in the body of the table. Below is an example: # # <% # collection_table(@posts, :footer => true) do |header, body, footer| # header.column :title # header.column :category # header.column :author # header.column :publish_date, 'Date
Published' # header.column :num_comments, '# Comments' # header.column :num_trackbacks, '# Trackbacks' # # body.alternate = true # body.build do |row, post, index| # row.category post.category.name # row.author post.author.name # row.publish_date time_ago_in_words(post.published_on) # row.num_comments post.comments.empty? ? '-' : post.comments.size # row.num_trackbacks post.trackbacks.empty? ? '-' : post.trackbacks.size # end # # footer.cell :num_comments, @posts.inject(0) {|sum, post| sum += post.comments.size} # footer.cell :num_trackbacks, @posts.inject(0) {|sum, post| sum += post.trackbacks.size} # end # %> module TableHelper # Creates a new table based on the objects in the given collection # # Configuration options: # # * +class+ - Specify the type of objects expected in the collection if it can't be guessed from its contents. # * +header+ - Specify if a header (thead) should be built into the table. Default is true. # * +footer+ - Specify if a footer (tfoot) should be built into the table. Default is false. def collection_table(collection, options = {}, html_options = {}, &block) table = CollectionTable.new(collection, options, html_options) table.build(&block) table.html end end ActionController::Base.class_eval do helper TableHelper end