Class TidyTable
In: lib/tidy_table.rb
Parent: Object

This is yet another library to convert an ActiveRecord array (or any struct) to an HTML table.

You get a table with a bunch of CSS classes automatically applied (or customize it with your own classes and ids). For example, first_row (is also a th tag), first_column, last_column, even & odd (for rows).

Other tags like thead and tbody may be added in the future.

Simple example with an ActiveRecord object:

  <%= TidyTable.new(@records).to_html(%w(title description created_at)) %>

You also format the row values with a block:

  <%= TidyTable.new(@records, :table_class => "revenue_report").to_html(%w(resource visits min max)) do |row|
    [
      row.resource,
      number_with_delimiter(row.visit_count),
      row.min,
      row.max
    ]
  end %>

Or in HAML:

  = TidyTable.new(@records, :table_class => "revenue_report").to_html(%w(resource visits min max)) do |row|
    - [ row.resource, number_with_delimiter(row.visit_count), row.min, row.max ]

Methods

new   to_html  

Constants

VERSION = '0.0.4'

Public Class methods

Make a new TidyTable with a data array and CSS options.

  • :table_class — Defaults to ‘tidy_table‘
  • :first_row_class — Defaults to ‘first_row‘
  • :first_column_class — Defaults to ‘first_column‘
  • :last_column_class — Defaults to ‘last_column‘

You also get ‘even’ and ‘odd’ for free (rows only).

Public Instance methods

First argument is an array of column names. Will also be called as methods on each row object if no block is provided.

If given, a block will be called for each row of the array. You should return an Array with the values formatted in the format that you want to see in the resulting cells.

Or, return a Hash where

  :data  => ['contents', 'for', 'cells'],
  :id    => 'id_for_this_row',
  :class => 'extra_classes for_this row'

[Validate]