Class: FormCandy::Field::Money

Inherits:
Base
  • Object
show all
Defined in:
lib/caisson/helpers/form/field/money.rb

Instance Method Summary collapse

Instance Method Details

#build(name, value, options = {}) ⇒ Object

************************************************************************************* PUBLIC INSTANCE METHODS *************************************************************************************



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/caisson/helpers/form/field/money.rb', line 9

def build(name, value, options={})
  options = parse_text_options options

  decimal = options[:decimal] ? options.delete(:decimal).to_i : 2
  options[:class].gsub!('text-field', 'money-field')

  options["data-validations"] = [options["data-validations"], 'numeric'].compact.join(' ')
  options["data-decimal"] = decimal

  content = text_field_tag('formatted-' + name, format_value(value, decimal), options)
  content += (:span, '$', class: 'symbol currency')
  content += hidden_field_tag(name, value, id: nil, class: 'dbdata')

  return content.html_safe
end

#format_value(value, decimal) ⇒ Object



25
26
27
# File 'lib/caisson/helpers/form/field/money.rb', line 25

def format_value(value, decimal)
  value.blank? ? '' : sprintf("%0.0#{decimal}f", (value.to_f / (10 ** decimal)).round(decimal)).gsub('.', ',')
end