require 'table_helper/collection_table' # Provides a set of methods for turning a collection into a table module TableHelper # Generates a new table for the given collection. # # == 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 |t| # t.header :title # t.header :category # t.header :author # t.header :publish_date, 'Date
Published' # t.header :num_comments, '# Comments' # t.header :num_trackbacks, '# Trackbacks' # # t.rows.alternate = :odd # t.rows.each do |row, post, index| # row.category post.category.name # row.author post.author.name # row.publish_date time_ago_in_words(post.published_at) # 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): # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
TitleDate
Published
# Comments# Trackbacks
Open-source projects: The good, the bad, and the ugly23 days--
5 reasons you should care about RailsJohn Q. Public21 days--
Deprecation: Stop digging yourself a hole17 days--
Jumpstart your Rails career at RailsConf 20074 days--
Getting some RESTabout 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) do |t| # t.header :title # t.header :category # t.header :author # t.header :publish_date, 'Date
Published' # t.header :num_comments, '# Comments' # t.header :num_trackbacks, '# Trackbacks' # # t.rows.alternate = :odd # t.rows.each do |row, post, index| # row.category post.category.name # row.author post.author.name # row.publish_date time_ago_in_words(post.published_at) # row.num_comments post.comments.empty? ? '-' : post.comments.size # row.num_trackbacks post.trackbacks.empty? ? '-' : post.trackbacks.size # end # # t.footer :num_comments, @posts.inject(0) {|sum, post| sum += post.comments.size} # t.footer :num_trackbacks, @posts.inject(0) {|sum, post| sum += post.trackbacks.size} # end # %> # # ...is compiled to: # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
TitleDate
Published
# Comments# Trackbacks
Open-source projects: The good, the bad, and the ugly23 days--
5 reasons you should care about RailsJohn Q. Public21 days--
00
def collection_table(collection, klass = nil, html_options = {}, &block) CollectionTable.new(collection, klass, html_options, &block).html end end ActionController::Base.class_eval do helper TableHelper end