Module: ArToHtmlTable::ColumnFormats::ClassMethods

Includes:
ActionView::Helpers::DateHelper, ActionView::Helpers::NumberHelper, ArToHtmlTable::ColumnFormatter
Defined in:
lib/ar_to_html_table/column_formats.rb

Constant Summary

Constants included from ArToHtmlTable::ColumnFormatter

ArToHtmlTable::ColumnFormatter::MIN_PERCENT_BAR_VALUE, ArToHtmlTable::ColumnFormatter::REDUCTION_FACTOR

Instance Method Summary collapse

Methods included from ArToHtmlTable::ColumnFormatter

#bar_and_percentage, #currency_without_sign, #float_with_precision, #group_not_set_on_blank, #hours_to_time, included, #integer_with_delimiter, #long_date, #long_day_name, #long_month_name, #not_set_on_blank, #ordinalize, #percentage, #seconds_to_time, #short_date, #short_day_name, #short_month_name, #unknown_on_blank

Instance Method Details

#column_format(method, options = {}) ⇒ Object Also known as: table_format

Define a column format.

Options

:order      Defines the column output order relative to other columns
:total      Column totaling method
:class      CSS Class to be added to the table cell
:formatter  Formatter to be applied.  Default is #to_s.  A symbol or lambda.  Symbol can
            represent any method that accepts a value and options including methods in
            ActionView::Helpers::NumberHelper

See HtmlTable::ColumnFormatter for formatter options.

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


83
84
85
86
# File 'lib/ar_to_html_table/column_formats.rb', line 83

def column_format(method, options = {})
  options[:formatter] = procify(options[:formatter]) if options[:formatter] && options[:formatter].is_a?(Symbol)
  @attr_formats = (@attr_formats || default_formats).deep_merge({method.to_s => options})
end

#format_column(column_name, value, 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

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

Parameters

column_name:  A column (attribute) on the the model instance
value: The value to be formatted
options Formatter options


130
131
132
133
134
135
136
137
138
139
# File 'lib/ar_to_html_table/column_formats.rb', line 130

def format_column(column_name, value, options = {})
  format = format_of(column_name)
  if format && (formatter = format[:formatter])
    formatter_options = options.merge(:options => format[:options])
    formatter.call(value, formatter_options)
  else
    Rails.logger.debug "[table_formatter] Column #{column_name} has no configured formatter"
    value.to_s
  end
end

#format_of(name) ⇒ Object

Retrieve a column format.

Examples

# Given the following class definition
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

Product.format_of(:name)
=> { :order => 1 }

Product.format_of(:revenue)
=> { :total => :sum, :order => 5, :class => 'right' }


106
107
108
109
# File 'lib/ar_to_html_table/column_formats.rb', line 106

def format_of(name)
  @attr_formats ||= default_formats
  @attr_formats[name.to_s] || {}
end