Sha256: f81f8a2f72be125dec6b1a05b9fdff1b14c5d9af62829d49b9854c33f349f667
Contents?: true
Size: 1.75 KB
Versions: 1
Compression:
Stored size: 1.75 KB
Contents
module TableGo class Formatter extend ActionView::Helpers::NumberHelper class MissingFormatterError < StandardError; end class_attribute :formatters self.formatters = {} formatters.store(:date, lambda do |value, record, column| value.present? ? I18n.l(value, :format => (column.as_options[:format] || :default)) : value end ) formatters.store(:datetime, lambda do |value, record, column| value.present? ? I18n.l(Time.parse(value.to_s).localtime, :format => (column.as_options[:format] || :default)) : value end ) formatters.store(:boolean, lambda do |value, record, column| if value == true '✔' elsif value == false '✘' end end ) formatters.store(:percent, lambda do |value, record, column| number_to_percentage(value, column.as_options) end ) formatters.store(:currency, lambda do |value, record, column| number_to_currency(value, column.as_options) end ) formatters.store(:number, lambda do |value, record, column| number_with_delimiter(value, column.as_options) end ) def self.apply(formatter, record, column, value) formatter_proc = case formatter when Symbol; formatters[formatter.to_sym] when Proc; formatter end raise MissingFormatterError.new('formatter "%s" not found' % formatter) if formatter_proc.blank? formatter_proc.call(value, record, column).to_s.html_safe end def self.apply_send(formatter, record, column, value) value.send(formatter).to_s.html_safe end end end # when Time # I18n.l(value.localtime, :format => :compact)
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
table_go-0.1.9 | lib/table_go/formatter.rb |