class Forma::Field

General field interface.

Attributes

actions[R]
after[R]
autofocus[R]
before[R]
child_model_name[RW]
height[R]
hint[R]
i18n[R]
icon[R]
inline_hint[R]
label[R]
model[RW]
model_name[W]
name[R]
parent[RW]
readonly[R]
required[R]
tag[R]
url[R]
value[RW]
width[R]

Public Class Methods

new(h = {}) click to toggle source
# File lib/forma/field.rb, line 16
def initialize(h = {})
  h = h.symbolize_keys
  @id = h[:id]; @label = h[:label]; @hint = h[:hint]; @i18n = h[:i18n]
  @inline_hint = h[:inline_hint]
  @required = h[:required]; @autofocus = h[:autofocus]; @readonly = (not not h[:readonly])
  @width = h[:width]; @height = h[:height]
  @before = h[:before]; @after = h[:after]
  @name = h[:name]; @value = h[:value]
  @url = h[:url]; @icon = h[:icon]
  @model = h[:model]; @parent = h[:parent]
  @model_name = h[:model_name]; @child_model_name = h[:child_model_name]
  @actions = h[:actions] || []
  @tag = h[:tag]
  @empty = h[:empty]
  @force_nonempty = h[:force_nonempty]
  @class = h[:class]
  @turbolink = h[:turbolink]
end

Public Instance Methods

action(url, h={}) click to toggle source
# File lib/forma/field.rb, line 35
def action(url, h={})
  h[:url] = url
  @actions << Action.new(h)
end
id() click to toggle source
# File lib/forma/field.rb, line 50
def id
  if @id then @id
  else name_as_chain.select{ |x| x.present? }.join('_') end
end
localization_key() click to toggle source
# File lib/forma/field.rb, line 91
def localization_key
  if @i18n.present?
    ["models", self.model_name, @i18n].compact.join('.')
  elsif self.respond_to?(:name)
    ["models", self.model_name, self.name].compact.join('.')
  end
end
localized_hint() click to toggle source
# File lib/forma/field.rb, line 105
def localized_hint
  self.hint.present? ? self.hint : I18n.t("#{localization_key}_hint", default: '')
end
localized_label() click to toggle source
# File lib/forma/field.rb, line 99
def localized_label
  unless self.label == false
    self.label.present? ? self.label : I18n.t(localization_key, default: self.name)
  end
end
model_name() click to toggle source

Returns model name. Model name can be defined by user or determined automatically, based on model class.

# File lib/forma/field.rb, line 84
def model_name
  if @model_name then @model_name
  elsif @parent then @parent.child_model_name
  else singular_name(self.model)
  end
end
name_as_chain() click to toggle source
# File lib/forma/field.rb, line 40
def name_as_chain
  if self.parent and self.parent.respond_to?(:name_as_chain)
    chain = self.parent.name_as_chain
    chain << self.name
  else
    chain = [ self.model_name, self.name ]
  end
  chain.map { |x| x.to_s.gsub '.', '_' }
end
parameter_name() click to toggle source
# File lib/forma/field.rb, line 55
def parameter_name
  parameter_name_from_chain(name_as_chain)
end
to_html(edit) click to toggle source

Convert this element into HTML.

# File lib/forma/field.rb, line 60
def to_html(edit)
  val = self.value
  val = val.call(self.model) if val.is_a?(Proc)
  if edit and not readonly
    edit = edit_element(val)
    el('div', children: [ before_element, icon_element, edit, after_element, actions_element, inline_hint_element ])
  else
    if val.present? or val == false or @force_nonempty
      view = view_element(val)
      if @url
        attrs = { href: eval_url }
        attrs['data-no-turbolink']=true if @turbolink==false
        view = el('a', attrs: attrs, children: [ view ])
      end
      el('div', attrs: { class: (@class ? eval_with_model(@class) : nil) },
        children: [ before_element, icon_element, view, after_element, actions_element, inline_hint_element ])
    else
      el('div', children: [ empty_element, actions_element ])
    end
  end
end

Protected Instance Methods

actions_element() click to toggle source
# File lib/forma/field.rb, line 126
def actions_element; el('div', attrs: { class: 'ff-field-actions' }, children: @actions.map { |action| action.to_html(@model) }) if @actions.any? end
after_element() click to toggle source
# File lib/forma/field.rb, line 124
def after_element; el('span', text: eval_with_model(after), attrs: { class: 'ff-field-after' }) if after.present? end
before_element() click to toggle source
# File lib/forma/field.rb, line 123
def before_element; el('span', text: eval_with_model(before), attrs: { class: 'ff-field-before' }) if before.present? end
empty_element() click to toggle source
# File lib/forma/field.rb, line 125
def empty_element; el('span', attrs: { class: 'ff-empty' }, text: Forma.config.texts.empty) unless @empty == false end
eval_icon() click to toggle source
# File lib/forma/field.rb, line 129
def eval_icon; eval_with_model(@icon) end
eval_url() click to toggle source
# File lib/forma/field.rb, line 128
def eval_url; eval_with_model(@url) end
icon_element() click to toggle source
# File lib/forma/field.rb, line 122
def icon_element; el('img', attrs: { src: eval_icon, style: { 'margin-right' => '4px' } }) if @icon.present? end
inline_hint_element() click to toggle source
# File lib/forma/field.rb, line 127
def inline_hint_element; el('div', attrs: { class: 'ff-inline-hint' }, text: @inline_hint) if @inline_hint.present? end
parameter_name_from_chain(chain) click to toggle source
# File lib/forma/field.rb, line 111
def parameter_name_from_chain(chain)
  length = chain.length
  p_name = ''
  chain.select{ |x| x.present? }.reverse.each_with_index do |n, i|
    if i == 0 then p_name = n
    elsif i == length - 1 then p_name = "#{n}[#{p_name}]"
    else p_name = "#{n}_attributes[#{p_name}]" end
  end
  p_name
end

Private Instance Methods

eval_with_model(val, h={}) click to toggle source
# File lib/forma/field.rb, line 132
def eval_with_model(val, h={}); val.is_a?(Proc) ? val.call(h[:model] || self.model) : val.to_s end