lib/administrate/field/number.rb in administrate-0.14.0 vs lib/administrate/field/number.rb in administrate-0.15.0
- old
+ new
@@ -1,18 +1,21 @@
require_relative "base"
+require "active_support/number_helper"
module Administrate
module Field
class Number < Field::Base
def to_s
- data.nil? ? "-" : format_string % value
+ result = data.nil? ? "-" : format_string % value
+ result = format(result) if options[:format]
+ prefix + result + suffix
end
private
def format_string
- prefix + "%.#{decimals}f" + suffix
+ "%.#{decimals}f"
end
def prefix
options[:prefix].to_s
end
@@ -27,9 +30,23 @@
options.fetch(:decimals, default)
end
def value
data * options.fetch(:multiplier, 1)
+ end
+
+ def format(result)
+ formatter = options[:format][:formatter]
+ formatter_options = options[:format][:formatter_options].to_h
+
+ case formatter
+ when :number_to_delimited
+ ActiveSupport::NumberHelper.number_to_delimited(
+ result, **formatter_options
+ )
+ else
+ result
+ end
end
end
end
end