Class: AgileFormFields::NumberField

Inherits:
AgileFormField show all
Defined in:
app/models/agile_form_fields/number_field.rb

Overview

Implementation of number_field AgileRails form field. Number fields can be formated for display with thousands delimiters and decimal separators and can have currency symbol.

Form options:

  • type: number_field (required)

  • name: Field name (required)

  • format: Format options

  • decimals: No of decimal places

  • separator: decimal separator (yes no , .) Default yes if decimals > 0

  • delimiter: Thousands delimiter (yes no , .) Default defind by locals

  • currency: Currency sign (yes no sign) Default no. If yes defined by locals

  • html: html options which apply to text_field field (optional)

Form example:

10:
  name: title
  type: number_field
  size: 10
  format:
    decimals: 2
    delimiter: false

Instance Attribute Summary

Attributes inherited from AgileFormField

#css, #js

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AgileFormField

#hash_to_options, #html, #initialize, #options_to_hash, #record_text_for, #ro_standard, #set_css_code, #set_default_value, #set_initial_value, #set_style, #t

Constructor Details

This class inherits a constructor from AgileFormFields::AgileFormField

Class Method Details

.get_data(params, name) ⇒ Object

Return value. Return nil if input field is empty



85
86
87
88
89
# File 'app/models/agile_form_fields/number_field.rb', line 85

def self.get_data(params, name)
  return 0 if params['record'][name].blank?

  params['record'][name].match('.') ? params['record'][name].to_f : params['record'][name].to_i
end

Instance Method Details

#renderObject

Render text_field AgileRails form field



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/agile_form_fields/number_field.rb', line 54

def render
  set_initial_value

  record = record_text_for(@yaml['name'])
  @yaml['html'] ||= {}
  @yaml['html']['class'] = 'ar-number'
  if @yaml['format'].class == String
    format = @yaml['format']
    @yaml['format'] = {}
    @yaml['format']['decimal']   = format[1].blank? ? 2 : format[1].to_i
    @yaml['format']['separator'] = format[2].blank? ? I18n.t('number.currency.format.separator') : format[2]
    @yaml['format']['delimiter'] = format[3].blank? ? I18n.t('number.currency.format.delimiter') : format[3]
  end
  @yaml['html']['data-decimal']   = @yaml.dig('format', 'decimal') || 2
  @yaml['html']['data-delimiter'] = @yaml.dig('format', 'delimiter') || I18n.t('number.currency.format.delimiter')
  @yaml['html']['data-separator'] = @yaml.dig('format', 'separator') || I18n.t('number.currency.format.separator')

  value = @yaml['html']['value'] || @record.send(@yaml['name']) || 0
  @yaml['html']['value'] = AgileHelper.format_number(value, @yaml['html']['data-decimal'], @yaml['html']['data-separator'], @yaml['html']['data-delimiter'] )
  
  return ro_standard(@yaml['html']['value']) if @readonly

  @yaml['html']['autocomplete'] ||= 'off'
  @html += @env.hidden_field( record, @yaml['name'], value: value )
  @html += @env.text_field( nil, "record_#{@yaml['name']}_", @yaml['html'])
  self
end