Module: ArToHtmlTable::ColumnFormats::InstanceMethods

Defined in:
lib/ar_to_html_table/column_formats.rb

Instance Method Summary collapse

Instance Method Details

#format_column(column_name, options = {}) ⇒ Object Also known as: format_attribute

Invoke the formatter on a column.

Examples

class Product < ActiveRecord::Base
  column_format :name,      :order => 1
  column_format :orders,    :total => :sum
  column_format :revenue,   :total => :sum, :order => 5, :class => 'right'
  column_format :age, 	    :total => :avg, :order => 20, :class => 'right', :formatter => :number_with_delimiter
end

# p = Product.first
# p.age
=> 4346

# p.format_column(:age)
=> "4,346"

Parameters

column_name:  A column (attribute) on the the model instance 
options:  Formatter options (passed to the formatter)

Options and where they come from

Options provided to a formatter are merged from several sources
Depending on who is calling the formatter.

table_formatter calls with options :cell_type and :column.  :cell_type
is either :td or :th depending on the type of cell the table formatter i
populating. :column is the internal column definition used by the table_formatter
which includes some attributes it uses.

instance#format_column (this method) merges in the :row options which is the
model instance.

class#format_column (the class method) merges in the formatter options (defined
as :options on the column_format definition)


51
52
53
54
# File 'lib/ar_to_html_table/column_formats.rb', line 51

def format_column(column_name, options = {})
  formatter_options = options.merge(:row => self)
  self.class.format_column(column_name, self[column_name], formatter_options)
end