Class: DrgcmsFormFields::NumberField

Inherits:
DrgcmsField show all
Defined in:
app/models/drgcms_form_fields/number_field.rb

Overview

Implementation of number_field DRG CMS 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 DrgcmsField

#css, #js

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DrgcmsField

#__css_code, #hash_to_options, #html, #initialize, #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 DrgcmsFormFields::DrgcmsField

Class Method Details

.get_data(params, name) ⇒ Object

Return value. Return nil if input field is empty



86
87
88
89
90
# File 'app/models/drgcms_form_fields/number_field.rb', line 86

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 field html code



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
81
# File 'app/models/drgcms_form_fields/number_field.rb', line 54

def render
  #return ro_standard if @readonly
  set_initial_value
#
  record = record_text_for(@yaml['name'])
  @yaml['html'] ||= {}
  @yaml['html']['class'] = 'dc-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')
 # @yaml['html']['data-currency']  = @yaml.dig('format','currency') == 'yes' ? I18n.t('number.currency.format.currency') : @yaml.dig('format','currency')
  value = @yaml['html']['value'] || @record[@yaml['name']] || 0
  @yaml['html']['value'] = @parent.dc_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 << @parent.hidden_field( record, @yaml['name'], value: value )
  @html << @parent.text_field( nil, "record_#{@yaml['name']}_", @yaml['html'])
  self
end