Class: ArToHtmlTable::TableFormatter
- Inherits:
-
Object
- Object
- ArToHtmlTable::TableFormatter
- 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
-
#column_cache ⇒ Object
Returns the value of attribute column_cache.
-
#html ⇒ Object
Returns the value of attribute html.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#merged_options ⇒ Object
Returns the value of attribute merged_options.
-
#rows ⇒ Object
Returns the value of attribute rows.
-
#table_columns ⇒ Object
Returns the value of attribute table_columns.
-
#totals ⇒ Object
Returns the value of attribute totals.
Instance Method Summary collapse
-
#initialize(results, options) ⇒ TableFormatter
constructor
Initialize a table formatter.
-
#to_html ⇒ Object
Render the result set to an HTML table using the options set at object instantiation.
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_
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, ) 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 [:sort] && ![:sort].is_a?(Proc) @klass = results.first.class @rows = results @column_order = 0 @merged_options = DEFAULT_OPTIONS.merge() @table_columns = initialise_columns(rows, klass, ) @totals = initialise_totalling(rows, table_columns) results.sort([:sort]) if [:sort] @merged_options[:rows] = results @html = Builder::XmlMarkup.new(:indent => 2) @column_cache = {} end |
Instance Attribute Details
#column_cache ⇒ Object
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 |
#html ⇒ Object
Returns the value of attribute html.
3 4 5 |
# File 'lib/ar_to_html_table/table_formatter.rb', line 3 def html @html end |
#klass ⇒ Object
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_options ⇒ Object
Returns the value of attribute merged_options.
3 4 5 |
# File 'lib/ar_to_html_table/table_formatter.rb', line 3 def @merged_options end |
#rows ⇒ Object
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_columns ⇒ Object
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 |
#totals ⇒ Object
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_html ⇒ Object
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 = = {} html.table do html.([:caption]) if [:caption] output_table_headings() () html.tbody do rows.each_with_index do |row, index| output_row(row, index, ) end end end end |