Class: ArToHtmlTable::TableFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/ar_to_html_table/table_formatter.rb

Constant Summary collapse

EXCLUDE_COLUMNS =
[:id, :updated_at, :created_at, :updated_on, :created_on]
CALCULATED_COLUMNS =
/(percent|percentage|difference|diff)_of_(.*)/
DEFAULT_OPTIONS =
{
    :exclude      => EXCLUDE_COLUMNS, 
    :exclude_ids  => true, 
    :odd_row      => "odd", 
    :even_row     => "even", 
    :totals       => true,
    :total_one    => 'tables.total_one',
    :total_many   => 'tables.total_many',
    :unknown_key  => 'tables.unknown',
    :not_set_key  => 'tables.not_set'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(results, options) ⇒ TableFormatter

Initialize a table formatter. Not normally called directly since Array#to_table takes care of this.

results: the value to be formatted
options: formatter options

Options

:include        Array of attributes to include in the table. Default is all attributes excepted :excluded ones.
:exclude        Array of attributes to exclude from the table. Default is [:id, :updated_at, :created_at, :updated_on, :created_on]
:exclude_ids    Exclude attributes with names ending in '_id'. Default is _true_
:sort           A proc invoked to sort the rows before output.  Default is not to sort.
:heading        Table heading places in the first row of a table
:caption        Table caption applied with <caption> markup
:odd_row        CSS Class name of the odd rows in the table.  Default is _odd_
:even_row       CSS Class name of the even rows.  Default is _even_
:totals         Include a total row if _true_.  Default is _true_
:total_one      I18n key for displaying a table footer when there is one row.  Default _tables.total_one_
:total_many     I18n key for displaying a table footer when there are > 1 rows. Default is _tables.total_many_
:unknown_key    I18n key for displaying _Unknown_. Default is _tables.unknown_
:not_set_key    I18n key for displaying _Not Set_.  Default is _tables.no_set_

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ar_to_html_table/table_formatter.rb', line 41

def initialize(results, options)
  raise ArgumentError, "[to_table] First argument must be an array of ActiveRecord rows" \
    unless  results.try(:first).try(:class).try(:descends_from_active_record?) ||
            results.is_a?(ActiveRecord::NamedScope::Scope)
            
  raise ArgumentError, "[to_table] Sort option must be a Proc" \
    if options[:sort] && !options[:sort].is_a?(Proc)
      
  @klass          = results.first.class
  @rows           = results
  @column_order   = 0
  @merged_options = DEFAULT_OPTIONS.merge(options)
  @table_columns  = initialise_columns(rows, klass, merged_options)
  @totals         = initialise_totalling(rows, table_columns)
  results.sort(options[:sort]) if options[:sort]
  @merged_options[:rows] = results
  @html = Builder::XmlMarkup.new(:indent => 2)
  @column_cache   = {}
end

Instance Attribute Details

#column_cacheObject

Returns the value of attribute column_cache.



4
5
6
# File 'lib/ar_to_html_table/table_formatter.rb', line 4

def column_cache
  @column_cache
end

#htmlObject

Returns the value of attribute html.



3
4
5
# File 'lib/ar_to_html_table/table_formatter.rb', line 3

def html
  @html
end

#klassObject

Returns the value of attribute klass.



3
4
5
# File 'lib/ar_to_html_table/table_formatter.rb', line 3

def klass
  @klass
end

#merged_optionsObject

Returns the value of attribute merged_options.



3
4
5
# File 'lib/ar_to_html_table/table_formatter.rb', line 3

def merged_options
  @merged_options
end

#rowsObject

Returns the value of attribute rows.



3
4
5
# File 'lib/ar_to_html_table/table_formatter.rb', line 3

def rows
  @rows
end

#table_columnsObject

Returns the value of attribute table_columns.



3
4
5
# File 'lib/ar_to_html_table/table_formatter.rb', line 3

def table_columns
  @table_columns
end

#totalsObject

Returns the value of attribute totals.



3
4
5
# File 'lib/ar_to_html_table/table_formatter.rb', line 3

def totals
  @totals
end

Instance Method Details

#to_htmlObject

Render the result set to an HTML table using the options set at object instantiation.

Examples

products = Product.all
formatter = ArToHtmlTable::TableFormatter.new(products)
formatter.to_html


69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ar_to_html_table/table_formatter.rb', line 69

def to_html
  options = merged_options
  table_options = {}
  html.table table_options do
    html.caption(options[:caption]) if options[:caption]
    output_table_headings(options)
    output_table_footers(options)
    html.tbody do
      rows.each_with_index do |row, index|
        output_row(row, index, options)
      end
    end
  end 
end