Class: Forma::Field

Inherits:
Object
  • Object
show all
Includes:
Html, Utils
Defined in:
lib/forma/field.rb

Overview

General field interface.

Direct Known Subclasses

ComplexField, MapField, SimpleField

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Html

attr, el

Methods included from Utils

extract_value, number_format, #simple_value, singular_name

Constructor Details

#initialize(h = {}) ⇒ Field

Returns a new instance of Field.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/forma/field.rb', line 17

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]
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



15
16
17
# File 'lib/forma/field.rb', line 15

def actions
  @actions
end

#afterObject (readonly)

Returns the value of attribute after.



11
12
13
# File 'lib/forma/field.rb', line 11

def after
  @after
end

#autofocusObject (readonly)

Returns the value of attribute autofocus.



9
10
11
# File 'lib/forma/field.rb', line 9

def autofocus
  @autofocus
end

#beforeObject (readonly)

Returns the value of attribute before.



11
12
13
# File 'lib/forma/field.rb', line 11

def before
  @before
end

#child_model_nameObject

Returns the value of attribute child_model_name.



13
14
15
# File 'lib/forma/field.rb', line 13

def child_model_name
  @child_model_name
end

#heightObject (readonly)

Returns the value of attribute height.



10
11
12
# File 'lib/forma/field.rb', line 10

def height
  @height
end

#hintObject (readonly)

Returns the value of attribute hint.



8
9
10
# File 'lib/forma/field.rb', line 8

def hint
  @hint
end

#i18nObject (readonly)

Returns the value of attribute i18n.



8
9
10
# File 'lib/forma/field.rb', line 8

def i18n
  @i18n
end

#iconObject (readonly)

Returns the value of attribute icon.



12
13
14
# File 'lib/forma/field.rb', line 12

def icon
  @icon
end

#inline_hintObject (readonly)

Returns the value of attribute inline_hint.



8
9
10
# File 'lib/forma/field.rb', line 8

def inline_hint
  @inline_hint
end

#labelObject (readonly)

Returns the value of attribute label.



8
9
10
# File 'lib/forma/field.rb', line 8

def label
  @label
end

#modelObject

Returns the value of attribute model.



13
14
15
# File 'lib/forma/field.rb', line 13

def model
  @model
end

#model_nameObject

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



79
80
81
82
83
84
# File 'lib/forma/field.rb', line 79

def model_name
  if @model_name then @model_name
  elsif @parent then @parent.child_model_name
  else singular_name(self.model)
  end
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/forma/field.rb', line 8

def name
  @name
end

#parentObject

Returns the value of attribute parent.



13
14
15
# File 'lib/forma/field.rb', line 13

def parent
  @parent
end

#readonlyObject (readonly)

Returns the value of attribute readonly.



9
10
11
# File 'lib/forma/field.rb', line 9

def readonly
  @readonly
end

#requiredObject (readonly)

Returns the value of attribute required.



9
10
11
# File 'lib/forma/field.rb', line 9

def required
  @required
end

#tagObject (readonly)

Returns the value of attribute tag.



8
9
10
# File 'lib/forma/field.rb', line 8

def tag
  @tag
end

#urlObject (readonly)

Returns the value of attribute url.



12
13
14
# File 'lib/forma/field.rb', line 12

def url
  @url
end

#valueObject

Returns the value of attribute value.



13
14
15
# File 'lib/forma/field.rb', line 13

def value
  @value
end

#widthObject (readonly)

Returns the value of attribute width.



10
11
12
# File 'lib/forma/field.rb', line 10

def width
  @width
end

Instance Method Details

#action(url, h = {}) ⇒ Object



35
36
37
38
# File 'lib/forma/field.rb', line 35

def action(url, h={})
  h[:url] = url
  @actions << Action.new(h)
end

#idObject



50
51
52
53
# 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_keyObject



86
87
88
89
90
91
92
# File 'lib/forma/field.rb', line 86

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_hintObject



100
101
102
# File 'lib/forma/field.rb', line 100

def localized_hint
  self.hint.present? ? self.hint : I18n.t("#{localization_key}_hint", default: '')
end

#localized_labelObject



94
95
96
97
98
# File 'lib/forma/field.rb', line 94

def localized_label
  unless self.label == false
    self.label.present? ? self.label : I18n.t(localization_key, default: self.name)
  end
end

#name_as_chainObject



40
41
42
43
44
45
46
47
48
# 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_nameObject



55
56
57
# File 'lib/forma/field.rb', line 55

def parameter_name
  parameter_name_from_chain(name_as_chain)
end

#to_html(edit) ⇒ Object

Convert this element into HTML.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/forma/field.rb', line 60

def to_html(edit)
  val = self.value
  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)
      view = el('a', attrs: { href: eval_url }, children: [ view ]) if @url
      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