pluginaweek / table_helper

Adds a helper method for generating HTML tables from collections

This URL has Read+Write access

obrie (author)
Sun Jun 01 20:04:03 -0700 2008
commit  112e5a6fdb36e08e5ffc17288d04fc1cd619828e
tree    37d068acb3aa926ca8bdecc14c5268fe3885e965
parent  1023d2511a6ea807522ff1d0b3ce4fb69dc73c49
table_helper / README
100644 161 lines (130 sloc) 5.014 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
= table_helper
 
+table_helper+ adds a helper method for generating HTML tables from collections.
 
== Resources
 
Wiki
 
* http://wiki.pluginaweek.org/Table_helper
 
API
 
* http://api.pluginaweek.org/table_helper
 
Development
 
* http://dev.pluginaweek.org/browser/trunk/table_helper
 
Source
 
* http://svn.pluginaweek.org/trunk/table_helper
 
== Description
 
Tables of summary data for ActiveRecord models are often formatted in the same
way by creating a header indicating the attribute and a body containing the
data from each record in separate rows. table_helper makes it easier to create
these types of tables by DRYing much of the html being generated.
 
== Usage
 
=== Basic Example
 
  <%= collection_table Person.find(:all) %>
 
...is compiled to (formatted here for the sake of sanity):
 
  <table cellpadding="0" cellspacing="0">
  <thead>
    <tr>
      <th class="first_name" scope="col">First Name</th>
      <th class="last_name" scope="col">Last Name</th>
      <th class="company_id" scope="col">Company</th>
      <th class="role" scope="col">Role</th>
    </tr>
  </thead>
  <tbody>
    <tr class="row">
      <td class="first_name">John</td>
      <td class="last_name">Doe</td>
      <td class="company_id">1</td>
      <td class="role">President</td>
    </tr>
    <tr class="row">
      <td class="first_name">Jane</td>
      <td class="last_name">Doe</td>
      <td class="company_id">1</td>
      <td class="role">Vice-President</td>
    </tr>
  </tbody>
  <table>
 
=== Advanced Example
 
  <%=
    collection_table(@posts, {}, :id => 'posts', :class => 'summary') do |header, body|
      header.column :title
      header.column :category
      header.column :author
      header.column :publish_date, 'Date<br \>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):
 
  <table cellpadding="0" cellspacing="0" class="summary" id="posts">
  <thead>
    <tr>
      <th class="title" scope="col">Title</th>
      <th class="category" scope="col">Category</th>
      <th class="author" scope="col">Author</th>
      <th class="publish_date" scope="col">Date<br \>Published</th>
      <th class="num_comments" scope="col"># Comments</th>
      <th class="num_trackbacks" scope="col"># Trackbacks</th>
    </tr>
  </thead>
  <tbody class="alternate">
    <tr class="row">
      <td class="title">Open-source projects: The good, the bad, and the ugly</td>
      <td class="category">General</td>
      <td class="author">John Doe</td>
      <td class="publish_date">23 days</td>
      <td class="num_comments">-</td>
      <td class="num_trackbacks">-</td>
    </tr>
    <tr class="row alternate">
      <td class="title">5 reasons you should care about Rails</td>
      <td class="category">Rails</td><td class="author">John Q. Public</td>
      <td class="publish_date">21 days</td>
      <td class="num_comments">-</td>
      <td class="num_trackbacks">-</td>
    </tr>
    <tr class="row">
      <td class="title">Deprecation: Stop digging yourself a hole</td>
      <td class="category">Rails</td>
      <td class="author">Jane Doe</td>
      <td class="publish_date">17 days</td>
      <td class="num_comments">-</td>
      <td class="num_trackbacks">-</td>
    </tr>
    <tr class="row alternate">
      <td class="title">Jumpstart your Rails career at RailsConf 2007</td>
      <td class="category">Conferences</td>
      <td class="author">Jane Doe</td>
      <td class="publish_date">4 days</td>
      <td class="num_comments">-</td>
      <td class="num_trackbacks">-</td>
    </tr>
    <tr class="row">
      <td class="title">Getting some REST</td>
      <td class="category">Rails</td>
      <td class="author">John Doe</td>
      <td class="publish_date">about 18 hours</td>
      <td class="num_comments">-</td>
      <td class="num_trackbacks">-</td>
    </tr>
  </tbody>
  </table>
 
=== Caveat Emptor
 
See the API for more information on syntax and options. You should only use
table_helper if it fits the needs of your application. Remember one of the key
principles of Rails, KISS (Keep It Simple Stupid). table_helper works really
well when you need to quickly output several of these types of summary tables.
If this is not the case, you may want to stick to using actual html.
 
== Testing
 
Before you can run any tests, the following gem must be installed:
* plugin_test_helper[http://wiki.pluginaweek.org/Plugin_test_helper]
 
To run against a specific version of Rails:
 
  rake test RAILS_FRAMEWORK_ROOT=/path/to/rails
 
== Dependencies
 
* Rails 2.0 or later