Class: DrgcmsFormFields::HashField

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

Overview

Implementation of text_field DRG CMS form field.

Form options:

  • type: text_field (required)

  • name: Field name (required)

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

Form example:

10:
  name: title
  type: text_field
  size: 30
  html:
    required: yes

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, #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



74
75
76
77
78
79
80
81
82
83
# File 'app/models/drgcms_form_fields/hash_field.rb', line 74

def self.get_data(params, name)
  return nil if params['record'][name].blank?
#
  result = params['record'][name].split("\n").select {|e| !e.blank? }
  return nil if result.size == 0
# convert to Hash
  ret = {}
  result.map { |e| key,value = e.chomp.split(':'); ret[key.strip] = value.strip unless value.blank? }
  ret
end

Instance Method Details

#renderObject

Render text_field field html code



58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/drgcms_form_fields/hash_field.rb', line 58

def render
  return ro_standard if @readonly
  set_initial_value
#
  record = record_text_for(@yaml['name'])
# Convert Hash to values separated by colon  
  if @record[@yaml['name']]
    @yaml['html']['value'] = @record[@yaml['name']].to_a.inject('') {|r, e| r << "#{e.first}:#{e.last}\n"}
  end
  @html << @parent.text_area( record, @yaml['name'], @yaml['html']) 
  self
end

#ro_standardObject

Returns value for readonly field



46
47
48
49
50
51
52
53
# File 'app/models/drgcms_form_fields/hash_field.rb', line 46

def ro_standard()
  return self if @record[@yaml['name']].nil?
  html = ''
  @record[@yaml['name']].each do |key, value|
    html << "#{key}:#{value}<br>"
  end
  super(html)
end