Class: DrgcmsFormFields::NumberField

Inherits:
DrgcmsField show all
Defined in:
app/models/drgcms_form_fields.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

#js

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DrgcmsField

#hash_to_options, #html, #initialize, #record_text_for, #ro_standard, #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



1384
1385
1386
1387
# File 'app/models/drgcms_form_fields.rb', line 1384

def self.get_data(params, name)
  return 0 if params['record'][name].blank?
  params['record'][name].match('.') ? BigDecimal.new(params['record'][name]) : Integer.new(params['record'][name])
end

Instance Method Details

#renderObject

Render text_field field html code



1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
# File 'app/models/drgcms_form_fields.rb', line 1361

def render
  return ro_standard if @readonly
  set_initial_value
#
  record = record_text_for(@yaml['name'])
  @yaml['html'] ||= {}
  @yaml['html']['class'] = 'dc-number'
  @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 = @record[@yaml['name']] || 0 
    
  @html << @parent.hidden_field( record, @yaml['name'], value: value )
  
  @yaml['html']['value'] = @parent.dc_format_number(value, @yaml['html']['data-decimal'], @yaml['html']['data-separator'], @yaml['html']['data-delimiter'] )
  @html << @parent.text_field( nil,"record_#{@yaml['name']}1", @yaml['html']) 
  self
end