Class: Forma::SelectField

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

Overview

Selection 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 = {}) ⇒ SelectField

Returns a new instance of SelectField.



525
526
527
528
529
530
531
532
# File 'lib/forma/field.rb', line 525

def initialize(h={})
  h = h.symbolize_keys
  @search_url = h[:search_url]
  @search_width = h[:search_width] || 500
  @search_height = h[:search_height] || 600
  @polymorphic = h[:polymorphic]
  super(h)
end

Instance Method Details

#edit_element(val) ⇒ Object



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/forma/field.rb', line 548

def edit_element(val)
  inputs = [ el('input', attrs: { id: "#{self.id}_id_value", type: 'hidden', value: "#{val and val.id}", name: id_field_name }) ]
  if @polymorphic
    inputs <<  el('input',
      attrs: { id: "#{self.id}_type_value", type: 'hidden', value: "#{val and val.class.to_s}", name: type_field_name }
    )
  end
  text_element = el(
    'span',
    attrs: { id: "#{self.id}_text", class: ['ff-select-label', ('ff-empty' if val.blank?)] },
    text: (val.present? ? val.to_s : Forma.config.texts.empty)
  )
  buttons = el('div', attrs: { class: 'btn-group' }, children: [
    el('a', attrs: { class: 'ff-select-link btn btn-mini btn-default btn-xs', 'data-id' => self.id, 'data-url' => @search_url, 'data-width' => @search_width, 'data-height' => @search_height }, children: [
      el('i', attrs: { class: 'icon icon-search fa fa-search' })
    ]),
    el('a', attrs: { class: 'ff-clear-selection-action btn btn-mini btn-default btn-xs', 'data-id' => self.id }, children: [
      el('i', attrs: { class: 'icon icon-trash fa fa-trash-o' })
    ])
  ])
  children = inputs + [ text_element, buttons ]
  el('div', attrs: { id: self.id, class: 'ff-select-field' }, children: children)
end

#id_field_nameObject



534
535
536
537
538
# File 'lib/forma/field.rb', line 534

def id_field_name
  chain = name_as_chain
  chain[chain.length - 1] = "#{chain.last}_id"
  parameter_name_from_chain(chain)
end

#type_field_nameObject



540
541
542
543
544
# File 'lib/forma/field.rb', line 540

def type_field_name
  chain = name_as_chain
  chain[chain.length - 1] = "#{chain.last}_type"
  parameter_name_from_chain(chain)
end

#view_element(val) ⇒ Object



546
# File 'lib/forma/field.rb', line 546

def view_element(val); el(@tag || 'span', text: val.to_s) end