Class: Forma::ComboField
- Inherits:
-
SimpleField
- Object
- Field
- SimpleField
- Forma::ComboField
- 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, #turbolink, #url, #value, #width
Instance Method Summary collapse
- #edit_element(val) ⇒ Object
-
#initialize(h = {}) ⇒ ComboField
constructor
A new instance of ComboField.
- #view_element(val) ⇒ Object
Methods inherited from SimpleField
Methods inherited from Field
#action, #id, #localization_key, #localized_hint, #localized_label, #name_as_chain, #parameter_name, #to_html
Methods included from Html
Methods included from Utils
extract_value, number_format, #simple_value, singular_name
Constructor Details
#initialize(h = {}) ⇒ ComboField
Returns a new instance of ComboField.
442 443 444 445 446 447 448 |
# File 'lib/forma/field.rb', line 442 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
456 457 458 459 460 461 462 463 464 |
# File 'lib/forma/field.rb', line 456 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
450 451 452 453 454 |
# File 'lib/forma/field.rb', line 450 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 |