Code Monkey home page Code Monkey logo

tabletastic's Introduction

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.

tabletastic's People

Contributors

balinterdi avatar hashrocketeer avatar jgdavey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

tabletastic's Issues

specify the html options of the header

i wanna some columns of the table will be ignored when some one exports or prints the table. so i need to add a class to the head as it does to the cell. i hacked the head method in TableBuilder and add field.cell_html as the third parameter of content_tag.

I18n is not working

Hi,

I have tried to change all my "Destroy" links to "Delete", followed instructions in the README, but with no luck.

HAML blocks and Rails 3

I'm using Rails 3.0 RC and tabletastic from master branch.

As you document, blocks in erb templates have changed for Rails 3. So the following code works:

<%= table_for @posts do |t|
  t.data :title
end %>

But the following code generates the table headers and body twice:

<%= table_for @posts do |t| %>
  <%= t.dada :title %>
<% end %>

I think it should work this way too, but no big deal anyway.

The problem comes when using HAML:

= table_for @posts do |t|
  = t.data :title

This generates the following output:

<thead><tr><th>Title/th></tr></thead>
<tbody>
<tr class="post odd" id="post_1"><td>My title</td></tr>
</tbody>
<table id="posts">0</table>

Using the block in one line works fine:
= table_for(@posts) {|table| table.data(:title)}

But it's a bit awkward to use this sintax for more complex tables.

I've looked at HAML documentation and I think I'm using it right. I'm also using simple form this way:

= simple_form_for @post do |form|
  = form.input :title  
  = form.button :submit

And it works fine.

So I guess there's a bit of a problem with the way tabletastic handles blocks and the output buffer.

Thanks.

access variable

table_for(@posts) do |t|
@category = category.where(:id => @posts)
t.column do |j|
@category.id
end
t.column do |j|
@category.name
end
.......
end

not working pls help

Undefined method error using simple example

Hi-

In my show partial, I get:

undefined method `empty?' for #<MyModel:0x00000003003a18>

Stack trace:

activemodel (3.2.13) lib/active_model/attribute_methods.rb:407:in `method_missing'
activerecord (3.2.13) lib/active_record/attribute_methods.rb:149:in `method_missing'
tabletastic (0.2.3) lib/tabletastic/helper.rb:18:in `default_class_for'
tabletastic (0.2.3) lib/tabletastic/helper.rb:6:in `table_for'

Using your simple example:

<%= table_for(@bundle) do |t|
t.data
end %>

Ruby 1.9.3 on Rails 3.2.13

Any ideas?

Thanks!

undefined method `table_for' for ...

Tabletastic seems to be broken:

In my Gemfile:

gem 'tabletastic'

-> bundle install was ok.

$ bundle show tabletastic

/home/my_user/.bundle/ruby/1.8/gems/tabletastic-0.1.3

In my view:

= table_for(@entries) do |e|

Gives me:

undefined method `table_for' for #<#Class:0xb5301a28:0xb52f460c>

Rails version:

rails-3.0.0.beta4

Specify row class

I'd like to be able to specify the row's class.
A possible use case would to differentiate user's that are not active.

Example:
Say I have 3 users, and Bob's account has not been activated.
Then I would like the output to be:

<table id="users">
  <thead>
    <tr>
      <th>Name</th>
      <th>Email</th>
    </tr>
  </thead>
  <tbody>
    <tr class="post odd" id="post_1">
      <td>Alice</td>
      <td>[email protected]</td>
    </tr>
    <tr class="post even inactive" id="post_2">   <-------- Note the extra "inactive" class added to this row.
      <td>Bob</td>
      <td>[email protected]</td>
    </tr>
    <tr class="post odd" id="post_3">
      <td>Carol</td>
      <td>[email protected]</td>
    </tr>
  </tbody>
</table>

Add possibility to not output thead at all

Sometimes it's good to have a small table without any thead. Right now I can do only something like

table_for connections do |t|
  t.data do
    t.cell(:created_at, :heading => nil
    t.cell(:status, :heading => nil
  end
end

I would prefer to have some option at table_for that would disable tbody generation at all.

Dynamic class for row

Im trying to pass the value of the variable as the class for the row. For example:

= table_for(@fonts) do |t|
= t.data :actions => :all, :action_prefix => "admin" do
    = t.cell :name, :cell_html => {:class => ??}
    = t.cell :family

As I am displaying a list of fonts and need the class for each font to be the value of the name of that font.

Any thoughts?

Pass URLs or paths to actions

Let's say I've got articles, and in the articles table I want an action to view all the comments in that article, where comments is a nested resource.

Right now I don't think it's possible, since only symbols are allowed for the action. It would be nice to have the ability to pass a URL to the action, as follows:

table.data :actions => [:show, :edit, article_comments_path(@article)]

Just to give an example.

Thanks.

Custom action

Hi!
I really like your gem, but I need some feature, that I cannot find.
I need custom action for row, how can I add it?

Add a span to an action anchor link?

How can I add a span to an action anchor link? In this case all actions 'show', 'edit' and 'destroy'

      = table_for(@post) do |table|
        = table.data :actions => [:show, :edit, :destroy], :action_prefix=>"mycontext" do
          - table.cell :subdomain

Desired output:

<td class="actions show_link"><span><a href="/mycontext/post/1">View</a><span></td>

Thanks

establizing general settings

Is there any way to determine that all tables that i'm going to create have the same html properties

For example I wish that all were created with this form:

<% table_for(@users, :html => {:cellspacing => "0", :cellpadding => "0"}) do |t| %>

I don't want to set in al my tables the :html hash. It is posible?

how to...

Hi,
I love this plugins but, how to make cell with a complex content, like that :
e.g. :
<% Liste.secteur.where(:id => dossier.secteur).each do |t| %>
<%= t.nom %>
<%end%>

or anything with a block ".each do" ....

It's possible ?
thx for your answer

Handling namespaces?

I have an admin namespace and a bunch of controllers within, such as "categories." When I try to use tabletastic to generate the actions it fails because the routes are

edit_admin_category_path(@category) and not edit_category_path(@category)

I tried the typical rails convension of table_for([:admin, @category]) do which works in formtastic but it failed in tabletastic.

Any idea how to get actions => :all to work with namespaces?

Thanks,

Josh

Datamapper support

Are there a plans to release Tabletastic with Rails 3 / Datamapper support?

Destroy action doesn't add confirm

The normal destroy action has a "confirm" option which does a javascript alert to make sure before deleting. That is missing in the :actions => :destroy rendering

Standard CRUD Action links

Need to add an easier way to add standard action links (show/edit/destroy) from the table. Perhaps something like:

table_for @posts, :actions => [:show, :edit] do |t|
   t.data
end

or

table_for @posts do |t|
   t.data :column, :title, :links => [:show, :edit]
end

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.