class Forma::ComboField

Combo field.

Public Class Methods

new(h={}) click to toggle source
Calls superclass method Forma::SimpleField::new
# File lib/forma/field.rb, line 441
def initialize(h={})
  h = h.symbolize_keys
  @empty = h[:empty]
  @default = h[:default]
  @collection = (h[:collection] || [])
  super(h)
end

Public Instance Methods

edit_element(val) click to toggle source
# File lib/forma/field.rb, line 455
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) click to toggle source
# File lib/forma/field.rb, line 449
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

Private Instance Methods

normalize_data(collection, empty) click to toggle source
# File lib/forma/field.rb, line 467
def normalize_data(collection, empty)
  if collection.is_a?(Hash) then data = collection.to_a
  else data = collection.map { |x| [x.to_s, x.id] } end
  if empty != false then data = ([[empty.to_s, nil]] + data) end
  Hash[data]
end