>= rails 3.0.0.rc
Joshua Davey (josh@joshuadavey.com)
You can download this project in either zip or tar formats.
You can also clone the project with Git by running:
$ git clone git://github.com/jgdavey/tabletastic
NOTICE: Rails 3 only!
In your Rails project, Gemfile:
gem "tabletastic", '~> 0.2.0.pre5'
Optionally, you can create an initializer at config/initializers/tabletastic.rb
By default, you can just use the table_for method to build up your table. Assuming you have a Post model with title and body, that belongs to an Author model with a name, you can just use the helper. It will try to detect all content fields and belongs to associations.
In your view, simply calling:
<%= table_for(@posts) do |t| t.data end %>
will produce html like this:
<table id="posts"> <thead> <tr> <th>Title</th> <th>Body</th> <th>Author</th> </tr> </thead> <tbody> <tr class="post odd" id="post_1"> <td>Something</td> <td>Lorem ipsum dolor sit amet consequat. Duis aute irure dolor.</td> <td>Jim Beam</td> </tr> <tr class="post even" id="post_2"> <td>Second Post</td> <td>This is the second post</td> <td>Jack Daniels</td> </tr> <tr class="post odd" id="post_3"> <td>Something else</td> <td>Blah!</td> <td></td> </tr> </tbody> </table>
To limit the fields, change the order, or to include fields that are excluded by default (such as created_at), You can list methods to call on each resource:
<%= table_for(@posts) do |t| t.data :author, :title, :created_at end %>
will produce html like:
<table id="posts"> <thead> <tr> <th>Author</th> <th>Title</th> <th>Created at</th> </tr> </thead> <tbody> <tr id="post_1" class="post odd"> <td>Jim Beam</td> <td>Something</td> <td>2009-11-15 02:42:48 UTC</td> </tr> <tr id="post_2" class="post even"> <td>Jack Daniels</td> <td>Second Post</td> <td>2009-11-16 00:11:00 UTC</td> </tr> <tr id="post_3" class="post odd"> <td></td> <td>Something else</td> <td>2009-11-16 00:11:30 UTC</td> </tr> </tbody> </table>
As of version 0.2.0, you can now setup some of your own defaults in an initializer file.
For example, create a file called tabletastic.rb in your config/initializers folder. Within that file, there are several defaults that you can setup for yourself, including:
Tabletastic.default_table_html
Tabletastic.default_table_block
Both of these options are setup up so that inline options will still override whatever they are set to, but providing them here will save you some code if you commonly reuse options.
By default, default_table_html is simple an empty hash {}. By providing your own defaults, you can ensure that all of your tables have a specific html class or cellspacing, or whatever.
If Tabletastic.default_table_html is set to {:cellspacing => 0, :class => 'yowza'}, then all tabletastic tables will have this html by default, unless overridden:
table_for(@posts) do |t| # body of block removed for brevity
will produce:
<table class="yowza" cellspacing="0" id="posts">
Similarly, all of those calls to table_for can get boring since you might be using the same block every time. The option default_table_block lets you set a default lambda so that you can omit the block with table_for. Since by default default_table_block is set to lambda{|table| table.data}, the following:
<%= table_for(@posts) %>
will be the same as
<%= table_for(@posts) do |t| t.data end %>
However, just like default_table_html , you can set a different block in the initializer file for your block to default to.
For example:
Tabletastic.default_table_block = lambda {|table| table.data :actions => :all }
will make the following equivalent:
<%= table_for(@posts) %>
<%= table_for(@posts) do |t| t.data :actions => :all end %>
And to omit those action links, you can just use table_for the old-fashioned way:
<%= table_for(@posts) do |t| t.data end %>