class Forma::SelectField

Selection field.

Public Class Methods

new(h={}) click to toggle source
Calls superclass method Forma::SimpleField::new
# File lib/forma/field.rb, line 530
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

Public Instance Methods

edit_element(val) click to toggle source
# File lib/forma/field.rb, line 553
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_name() click to toggle source
# File lib/forma/field.rb, line 539
def id_field_name
  chain = name_as_chain
  chain[chain.length - 1] = "#{chain.last}_id"
  parameter_name_from_chain(chain)
end
type_field_name() click to toggle source
# File lib/forma/field.rb, line 545
def type_field_name
  chain = name_as_chain
  chain[chain.length - 1] = "#{chain.last}_type"
  parameter_name_from_chain(chain)
end
view_element(val) click to toggle source
# File lib/forma/field.rb, line 551
def view_element(val); el(@tag || 'span', text: val.to_s) end