Class: Terrazzo::Field::Number

Inherits:
Base
  • Object
show all
Defined in:
lib/terrazzo/field/number.rb

Constant Summary

Constants inherited from Base

Base::ABSTRACT_FIELD_CLASSES

Instance Attribute Summary

Attributes inherited from Base

#attribute, #data, #options, #page, #resource

Instance Method Summary collapse

Methods inherited from Base

associative?, default_options, eager_load?, #field_type, field_type, #form_input_attributes, #initialize, permitted_attribute, #required?, searchable?, sortable?, transform_param, with_options

Constructor Details

This class inherits a constructor from Terrazzo::Field::Base

Instance Method Details

#serializable_options(page = nil) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/terrazzo/field/number.rb', line 20

def serializable_options(page = nil)
  opts = {}
  opts[:prefix] = options[:prefix] if options.key?(:prefix)
  opts[:suffix] = options[:suffix] if options.key?(:suffix)
  opts[:decimals] = options[:decimals] if options.key?(:decimals)
  opts
end

#serialize_value(mode) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/terrazzo/field/number.rb', line 6

def serialize_value(mode)
  return data if data.nil? || mode == :form

  value = options.key?(:multiplier) ? data * options[:multiplier] : data

  if options[:format]
    formatter = options[:format][:formatter]
    formatter_options = options[:format][:formatter_options].to_h
    ActiveSupport::NumberHelper.try(formatter, value, **formatter_options) || value
  else
    value
  end
end