Class: Forma::ComboField

Inherits:
SimpleField show all
Defined in:
lib/forma/field.rb

Overview

Combo field.

Instance Attribute Summary

Attributes inherited from Field

#actions, #after, #autofocus, #before, #child_model_name, #height, #hint, #i18n, #icon, #inline_hint, #label, #model, #model_name, #name, #parent, #readonly, #required, #tag, #url, #value, #width

Instance Method Summary collapse

Methods inherited from SimpleField

#errors, #has_errors?, #value

Methods inherited from Field

#action, #id, #localization_key, #localized_hint, #localized_label, #name_as_chain, #parameter_name, #to_html

Methods included from Html

attr, el

Methods included from Utils

extract_value, number_format, #simple_value, singular_name

Constructor Details

#initialize(h = {}) ⇒ ComboField

Returns a new instance of ComboField.



436
437
438
439
440
441
442
# File 'lib/forma/field.rb', line 436

def initialize(h={})
  h = h.symbolize_keys
  @empty = h[:empty]
  @default = h[:default]
  @collection = (h[:collection] || [])
  super(h)
end

Instance Method Details

#edit_element(val) ⇒ Object



450
451
452
453
454
455
456
457
458
# File 'lib/forma/field.rb', line 450

def edit_element(val)
  data = normalize_data(@collection, @empty)
  selection = val.present? ? val : @default
  el('select', attrs: { name: parameter_name }, children: data.map { |text, value|
    if value.nil? then el('option', attrs: { selected: selection.blank? , value: ""}, text: text)
    else el('option', attrs: { selected: (true if selection.to_s == value.to_s), value: value }, text: text)
    end
  })
end

#view_element(val) ⇒ Object



444
445
446
447
448
# File 'lib/forma/field.rb', line 444

def view_element(val)
  data = normalize_data(@collection, false)
  text = data.find{|text, value| val == value }[0].to_s rescue nil
  el('span', text: text)
end