Module: AmountField::Helpers::FormHelper

Includes:
ActionView::Helpers::NumberHelper
Defined in:
lib/amount_field/form_helper.rb

Overview

TODO/2010-04-23/tb remove redundant code using @template.amount_field(object, method, options) !?

Instance Method Summary collapse

Instance Method Details

#amount_field(object_name, method, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/amount_field/form_helper.rb', line 11

def amount_field(object_name, method, options = {})
  format_options = I18n.t(:'number.amount_field.format', :raise => true) rescue {}
  format_options = format_options.merge(AmountField::ActiveRecord::Validations.configuration)
  format_options.merge!(options.delete(:format) || {})
  
  object = options.delete(:object) || instance_variable_get("@#{object_name}")

  # if no explicit value is given, we set a formatted one. In case of an error we take the
  # original value inserted by the user.
  if object.errors[method].blank?
    options[:value] ||= number_with_precision(object.send(method), format_options)
  else
    options[:value] ||= object.send("#{AmountField::Configuration.prefix}_#{method}") || object.send("#{method}_before_type_cast")
  end

  options[:name]  ||= "#{object_name}[#{AmountField::Configuration.prefix}_#{method}]"
  options[:class] = "#{options[:class]} #{AmountField::Configuration.css_class}"
  options[:id]    ||= "#{object_name}_#{method}"

  text_field(object_name, method, options)
end