A smart table builder for Rails collections
= Tabletastic
http://travis-ci.org/jgdavey/tabletastic.png
== NOTICE: No longer maintained
I haven't used this gem in years, and am no longer actively contributing to it. I am more than willing to give commit rights to anyone that wants to maintain it.
== Introduction
Inspired by the projects table_builder and formtastic, I realized how often I created tables for my active record collections. This is my attempt to simplify this (the default scaffold):
<% for post in @posts %> <% end %>Title | Body | Author Id | |||
---|---|---|---|---|---|
<%=h post.title %> | <%=h post.body %> | <%=h post.authorid %> | <%= linkto "Show", post %> | <%= linkto "Edit", editpostpath(post) %> | <%= linkto "Destroy", post, :confirm => 'Are you sure?', :method => :delete %> |
into this:
<%= table_for(@posts) do |t| t.data :actions => :all end %>
and still output the same effective results, but with all the semantic goodness that tabular data should have, i.e. a +thead+ and +tbody+ element.
== Installation
In your Rails project, Gemfile: gem "tabletastic"
Or, for if you're behind the times, as a plugin: script/plugin install git://github.com/jgdavey/tabletastic.git
Optionally, you can create an initializer at config/initializers/tabletastic.rb with the following: Tabletastic.defaulttableblock = lambda {|table| table.data :actions => :all }
== Usage
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:
Title | Body | Author |
---|---|---|
Something | Lorem ipsum dolor sit amet consequat. Duis aute irure dolor. | Jim Beam |
Second Post | This is the second post | Jack Daniels |
Something else | Blah! |
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:
<%= tablefor(@posts) do |t| t.data :author, :title, :createdat end %>
will produce html like:
Author | Title | Created at |
---|---|---|
Jim Beam | Something | 2009-11-15 02:42:48 UTC |
Jack Daniels | Second Post | 2009-11-16 00:11:00 UTC |
Something else | 2009-11-16 00:11:30 UTC |
For even greater flexibility, you can pass +data+ a block:
<%= tablefor(@posts) do |t| t.data :actions => :all do t.cell(:title, :cellhtml => {:class => "titlestring"}) t.cell(:body, :heading => "Content") {|p| truncate(p.body, 30)} t.cell(:author) {|p| p.author && link_to(p.author.name, p.author) } end end %>
will product html like:
Title | Content | Author | |||
---|---|---|---|---|---|
Something | Lorem ipsum dolor sit amet,... | Jim Bean | Show | Edit | Destroy <!-- inline javascript omitted --> |
Second Post | This is the second post | Jack Daniels | Show | Edit | Destroy <!-- inline javascript omitted --> |
Something else | Blah! | Show | Edit | Destroy <!-- inline javascript omitted --> |
If it still isn't flexible enough for your needs, it might be time to return to static html/erb.
== Internationalization (I18n)
Tabletastic has some i18n-features enabled by default.
Here is an example of locales:
en: tabletastic: actions: show: "See" edit: "Edit" destroy: "Remove" confirmation: "Are you sure?" models: comment: title: "Title"
== Default Options and Tabletastic initializer
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.defaulttablehtml Tabletastic.defaulttableblock
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, +defaulttablehtml+ 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.defaulttablehtml
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:
Similarly, all of those calls to +tablefor+ can get boring since you might be using the same block every time.
The option +defaulttableblock+ lets you set a default lambda so that you can omit the block with +tablefor+.
Since by default +defaulttableblock+ 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 +defaulttablehtml+ , you can set a different block in the initializer file for your block to default to.
For example:
Tabletastic.defaulttableblock = 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 %>
== Note on Patches/Pull Requests
== Copyright
Copyright (c) 2011 Joshua Davey. See LICENSE for details.