Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
= 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 %>Title | Body | Author 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:
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:
<%= table_for(@posts) do |t| t.data :author, :title, :created_at 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:
<%= 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:
Title | Content | Author | |||
---|---|---|---|---|---|
Something | Lorem ipsum dolor sit amet,... | Jim Bean | Show | Edit | Destroy |
Second Post | This is the second post | Jack Daniels | Show | Edit | Destroy |
Something else | Blah! | 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
== Copyright
Copyright (c) 2010 Joshua Davey. See LICENSE for details.
FAQs
Unknown package
We found that tabletastic demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.