Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tabletastic

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tabletastic

  • 0.2.3
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

= Tabletastic

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 %>
TitleBodyAuthor Id
<%=h post.title %><%=h post.body %><%=h post.author_id %><%= link_to "Show", post %><%= link_to "Edit", edit_post_path(post) %><%= link_to "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.

== NOTICE: Rails 3 only!

As of version 0.2.0, this project is no longer compatible with Rails 2. To use, make sure you're using the latest release of Rails 3.

Also, the new Rails 3 idiom is for blocks in erb templates to be use <%= %>, so if you're upgrading, you MUST now use the <%= %> erb helpers when you call +table_for+.

For example:

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

The following would also be valid:

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

Or in HAML:

= table_for(@people) do |t| = t.data

But, following the block helper conventions, the following would NOT be okay

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

Or, in HAML:

-# Don't do this! = table_for(@people) do |t| - t.data

This is because the +data+ method has output, and can optionally be called with a block.

For more information about Rails 3 and Block helpers and the new conventions, check out {the Railscasts episode about ERB blocks}[http://railscasts.com/episodes/208-erb-blocks-in-rails-3]

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

TitleBodyAuthor
SomethingLorem ipsum dolor sit amet consequat. Duis aute irure dolor.Jim Beam
Second PostThis is the second postJack Daniels
Something elseBlah!

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:

AuthorTitleCreated at
Jim BeamSomething2009-11-15 02:42:48 UTC
Jack DanielsSecond Post2009-11-16 00:11:00 UTC
Something else2009-11-16 00:11:30 UTC

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:

TitleContentAuthor
SomethingLorem ipsum dolor sit amet,... Jim Bean Show Edit Destroy
Second PostThis is the second post Jack Daniels Show Edit Destroy
Something elseBlah! Show Edit Destroy

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:

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

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

FAQs

Package last updated on 10 Aug 2011

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc