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
-
#column_format(method, options) ⇒ Object
(also: #table_format)
Define a column format.
-
#format_column(column_name, value) ⇒ Object
(also: #format_attribute)
Invoke the formatter on a column.
-
#format_of(name) ⇒ Object
Retrieve a column format.
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
65 66 67 68 |
# File 'lib/ar_to_html_table/column_formats.rb', line 65 def column_format(method, ) [:formatter] = procify([:formatter]) if [:formatter] && [:formatter].is_a?(Symbol) @attr_formats = (@attr_formats || default_formats).deep_merge({method.to_s => }) end |
#format_column(column_name, value) ⇒ 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
111 112 113 114 115 |
# File 'lib/ar_to_html_table/column_formats.rb', line 111 def format_column(column_name, value) formatter = format_of(column_name)[:formatter] raise "Column #{column_name} has no configured formatter" unless formatter && formatter.is_a?(Proc) formatter.call(value, {}) 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' }
88 89 90 91 |
# File 'lib/ar_to_html_table/column_formats.rb', line 88 def format_of(name) @attr_formats ||= default_formats @attr_formats[name.to_s] || {} end |