by jgdavey

jgdavey /tabletastic

A smart table builder for Rails collections

131 Stars 33 Forks Last release: Not found MIT License 114 Commits 14 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

= Tabletastic

== 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| :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://

Optionally, you can create an initializer at config/initializers/tabletastic.rb with the following: Tabletastic.defaulttableblock = lambda {|table| :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| 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| :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| :actions => :all do t.cell(:title, :cellhtml => {:class => "titlestring"}) t.cell(:body, :heading => "Content") {|p| truncate(p.body, 30)} t.cell(:author) {|p| && link_to(, } end end %>

will product html like:

Title Content Author
Something Lorem ipsum dolor sit amet,... Jim Bean
Second Post This is the second post Jack Daniels
Something else Blah!

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|}, the following:

<%= table_for(@posts) %>

will be the same as

<%= table_for(@posts) do |t| 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| :actions => :all }

will make the following equivalent:

<%= table_for(@posts) %>

<%= table_for(@posts) do |t| :actions => :all end %>

And to omit those action links, you can just use +table_for+ the old-fashioned way:

<%= table_for(@posts) do |t| end %>

== Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or changelog. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

== Copyright

Copyright (c) 2011 Joshua Davey. See LICENSE for details.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.