• Stars
    star
    128
  • Rank 281,044 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 15 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A smart table builder for Rails collections

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):

<table>
  <tr>
    <th>Title</th>
    <th>Body</th>
    <th>Author Id</th>
  </tr>
  <% for post in @posts %>
    <tr>
      <td><%=h post.title %></td>
      <td><%=h post.body %></td>
      <td><%=h post.author_id %></td>
      <td><%= link_to "Show", post %></td>
      <td><%= link_to "Edit", edit_post_path(post) %></td>
      <td><%= link_to "Destroy", post, :confirm => 'Are you sure?', :method => :delete %></td>
      </tr>
  <% end %>
</table>

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.default_table_block = 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:

<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>

For even greater flexibility, you can pass data a block:

<%= table_for(@posts) do |t|
  t.data :actions => :all do
    t.cell(:title, :cell_html => {: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:

<table id="posts">
  <thead>
    <tr>
      <th>Title</th>
      <th>Content</th>
      <th>Author</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr class="post odd" id="post_1">
      <td class="titlestring">Something</td>
      <td>Lorem ipsum dolor sit amet,...</td>
      <td>
        <a href="/authors/1">Jim Bean</a>
      </td>
      <td class="actions show_link">
        <a href="/posts/1">Show</a>
      </td>
      <td class="actions edit_link">
        <a href="/posts/1/edit">Edit</a>
      </td>
      <td class="actions destroy_link">
        <a href="/posts/1/edit">Destroy</a> <!-- inline javascript omitted -->
      </td>
    </tr>
    <tr class="post even" id="post_2">
      <td class="titlestring">Second Post</td>
      <td>This is the second post</td>
      <td>
        <a href="/authors/2">Jack Daniels</a>
      </td>
      <td class="actions show_link">
        <a href="/posts/2">Show</a>
      </td>
      <td class="actions edit_link">
        <a href="/posts/2/edit">Edit</a>
      </td>
      <td class="actions destroy_link">
        <a href="/posts/2/edit">Destroy</a> <!-- inline javascript omitted -->
      </td>
    </tr>
    <tr class="post odd" id="post_3">
      <td class="titlestring">Something else</td>
      <td>Blah!</td>
      <td></td>
      <td class="actions show_link">
        <a href="/posts/3">Show</a>
      </td>
      <td class="actions edit_link">
        <a href="/posts/3/edit">Edit</a>
      </td>
      <td class="actions destroy_link">
        <a href="/posts/3/edit">Destroy</a> <!-- inline javascript omitted -->
      </td>
    </tr>
  </tbody>
</table>

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.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 %>

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 © 2011 Joshua Davey. See LICENSE for details.

More Repositories

1

vim-turbux

Turbo Ruby testing with tmux
Vim Script
180
star
2

vim-blockle

Brace yourself, it's time to toggle your ruby blocks!
Vim Script
61
star
3

kevin

IMDB, Clojure, Datomic, and Kevin Bacon
Clojure
52
star
4

unicornleap

Make unicorns leap across your screen!
Swift
50
star
5

transit-rails

Use transit format in your Rails application
Ruby
37
star
6

boot-middleman

Middleman task for boot
Clojure
20
star
7

zxing-cpp

Mirror of the C++ ZXing library
C++
19
star
8

clj-pgcopy

Import data into postgres quickly from Clojure
Clojure
19
star
9

prose

Reformat text pleasantly
Rust
18
star
10

tree-sql-example

Example of using Postgres for Recursive Tree SQL in a Rails app
Ruby
18
star
11

hammock

A partial implementation of Clojure, in pure Ruby
Clojure
16
star
12

minesweeper

HTML
12
star
13

qrdecoder

A ruby wrapper for the C++ port of ZXing
Ruby
11
star
14

sql_views

Example application demonstrating the "has_one view" technique
Ruby
5
star
15

crockery

Clojure printing for human-readable tables
Clojure
4
star
16

movable_erb

A tool to convert CSV documents to MTImport, useful to convert legacy data to blog-standard format for importing.
Ruby
3
star
17

windowing-example

Ruby
3
star
18

vim-hearth

Test running stuff for fireplace.vim
Vim Script
3
star
19

wordbots

Bots for slack. Always random, sometimes funny.
Clojure
3
star
20

boot-shadow-cljs

Clojure
3
star
21

salon_space

Rails app for managing Salon clients
Ruby
2
star
22

jgdavey.github.com

HTML
2
star
23

sqltricks

SQL Tricks presentation for Hashrocket MiniConf Winter 2012
Ruby
2
star
24

ballpark

Fuzzy string matching and ranking for Clojure(script)
Clojure
2
star
25

vim-twiddle

Swap argument order
Ruby
1
star
26

boot-haml

Clojure
1
star
27

vim-weefactor

Micro Ruby refactorings
Vim Script
1
star
28

harvestime

Plain text harvest time-tracking
Ruby
1
star
29

uniqueness_constraints_example

Ruby
1
star
30

rook

A card game
Clojure
1
star
31

dot-files

deprecated. I now use dotmatricks
Perl
1
star
32

fake

Rust
1
star