Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 80 lines (70 sloc) 2.018 kb
f879795 Add README
Joe Fiorini authored
1 h1. Totally Tabular!
2
3 Assuming I have 5 people with:
4
5 * first_name
6 * last_name
7 * registration_status
8
9 My pointy-haired boss wants to see a list of these people on our
10 administrative dashboard. Sure, I could write some ugly ERB like so:
11
12 <pre>
13 <code>
14 <table class="people">
15 <thead>
16 <tr>
17 <th class="header">First Name</th>
18 <th>Last Name</th>
19 <th>Registration Status</th>
20 </tr>
21 </thead>
22 <tbody>
23 <% @people.each do |person| %>
24 <tr class="<%= person.registration_status %>">
25 <td>Michael Bluth</td>
26 <td>Bluth</td>
27 <td>
28 <% if person.registered? %>
29 <strong>Registered</strong>
30 <% else %>
31 <em>Unregistered</em>
32 <% end %>
33 </tr>
34 <% end %>
35 </tbody>
36 </table>
37 </code>
38 </pre>
39
40 But why? I mean, we're using Ruby for Pete's sake! How about a cleaner way to do the same thing?
41
42 <pre>
43 <code>
44 <%= person_table(people_presenter.people, :class => 'people') %>
45 </code>
46 </pre>
47
48 Okay, so that's just a custom Rails helper method. Nothing special. But let's look at the definition of the helper method! That's where the special sauce is.
49
50 <pre>
51 <code>
52 def person_table(people, attributes={})
53 TableView.new(@people, :class => 'people') do
54 define_column("First Name") do |person|
55 header_attributes!(:class => 'header')
56 row_attributes!(:class => person.registration_status)
57 template! do
58 person.first_name
59 end
60 end
61 define_column("Last Name") do |person|
62 template! do
63 person.last_name
64 end
65 end
66 define_column("Registration Status") do |person|
67 template! do
68 if person.registered?
69 content_tag(:strong, "Registered")
70 else
71 content_tag(:em, "Unregistered")
72 end
73 end
74 end
75 end
76 end
77 </code>
78 </pre>
79
Something went wrong with that request. Please try again.