Class: Forma::Field
Overview
General field interface.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#actions ⇒ Object
readonly
Returns the value of attribute actions.
-
#after ⇒ Object
readonly
Returns the value of attribute after.
-
#autofocus ⇒ Object
readonly
Returns the value of attribute autofocus.
-
#before ⇒ Object
readonly
Returns the value of attribute before.
-
#child_model_name ⇒ Object
Returns the value of attribute child_model_name.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#hint ⇒ Object
readonly
Returns the value of attribute hint.
-
#i18n ⇒ Object
readonly
Returns the value of attribute i18n.
-
#icon ⇒ Object
readonly
Returns the value of attribute icon.
-
#inline_hint ⇒ Object
readonly
Returns the value of attribute inline_hint.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#model ⇒ Object
Returns the value of attribute model.
-
#model_name ⇒ Object
Returns model name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#readonly ⇒ Object
readonly
Returns the value of attribute readonly.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
-
#turbolink ⇒ Object
readonly
Returns the value of attribute turbolink.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#value ⇒ Object
Returns the value of attribute value.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #action(url, h = {}) ⇒ Object
- #id ⇒ Object
-
#initialize(h = {}) ⇒ Field
constructor
A new instance of Field.
- #localization_key ⇒ Object
- #localized_hint ⇒ Object
- #localized_label ⇒ Object
- #name_as_chain ⇒ Object
- #parameter_name ⇒ Object
-
#to_html(edit) ⇒ Object
Convert this element into HTML.
Methods included from Html
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 34 |
# 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] @turbolink = h[:turbolink] end |
Instance Attribute Details
#actions ⇒ Object (readonly)
Returns the value of attribute actions.
15 16 17 |
# File 'lib/forma/field.rb', line 15 def actions @actions end |
#after ⇒ Object (readonly)
Returns the value of attribute after.
11 12 13 |
# File 'lib/forma/field.rb', line 11 def after @after end |
#autofocus ⇒ Object (readonly)
Returns the value of attribute autofocus.
9 10 11 |
# File 'lib/forma/field.rb', line 9 def autofocus @autofocus end |
#before ⇒ Object (readonly)
Returns the value of attribute before.
11 12 13 |
# File 'lib/forma/field.rb', line 11 def before @before end |
#child_model_name ⇒ Object
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 |
#height ⇒ Object (readonly)
Returns the value of attribute height.
10 11 12 |
# File 'lib/forma/field.rb', line 10 def height @height end |
#hint ⇒ Object (readonly)
Returns the value of attribute hint.
8 9 10 |
# File 'lib/forma/field.rb', line 8 def hint @hint end |
#i18n ⇒ Object (readonly)
Returns the value of attribute i18n.
8 9 10 |
# File 'lib/forma/field.rb', line 8 def i18n @i18n end |
#icon ⇒ Object (readonly)
Returns the value of attribute icon.
12 13 14 |
# File 'lib/forma/field.rb', line 12 def icon @icon end |
#inline_hint ⇒ Object (readonly)
Returns the value of attribute inline_hint.
8 9 10 |
# File 'lib/forma/field.rb', line 8 def inline_hint @inline_hint end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
8 9 10 |
# File 'lib/forma/field.rb', line 8 def label @label end |
#model ⇒ Object
Returns the value of attribute model.
13 14 15 |
# File 'lib/forma/field.rb', line 13 def model @model end |
#model_name ⇒ Object
Returns model name. Model name can be defined by user or determined automatically, based on model class.
85 86 87 88 89 90 |
# File 'lib/forma/field.rb', line 85 def model_name if @model_name then @model_name elsif @parent then @parent.child_model_name else singular_name(self.model) end end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/forma/field.rb', line 8 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
13 14 15 |
# File 'lib/forma/field.rb', line 13 def parent @parent end |
#readonly ⇒ Object (readonly)
Returns the value of attribute readonly.
9 10 11 |
# File 'lib/forma/field.rb', line 9 def readonly @readonly end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
9 10 11 |
# File 'lib/forma/field.rb', line 9 def required @required end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
8 9 10 |
# File 'lib/forma/field.rb', line 8 def tag @tag end |
#turbolink ⇒ Object (readonly)
Returns the value of attribute turbolink.
12 13 14 |
# File 'lib/forma/field.rb', line 12 def turbolink @turbolink end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
12 13 14 |
# File 'lib/forma/field.rb', line 12 def url @url end |
#value ⇒ Object
Returns the value of attribute value.
13 14 15 |
# File 'lib/forma/field.rb', line 13 def value @value end |
#width ⇒ Object (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
36 37 38 39 |
# File 'lib/forma/field.rb', line 36 def action(url, h={}) h[:url] = url @actions << Action.new(h) end |
#id ⇒ Object
51 52 53 54 |
# File 'lib/forma/field.rb', line 51 def id if @id then @id else name_as_chain.select{ |x| x.present? }.join('_') end end |
#localization_key ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/forma/field.rb', line 92 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 ⇒ Object
106 107 108 |
# File 'lib/forma/field.rb', line 106 def localized_hint self.hint.present? ? self.hint : I18n.t("#{localization_key}_hint", default: '') end |
#localized_label ⇒ Object
100 101 102 103 104 |
# File 'lib/forma/field.rb', line 100 def localized_label unless self.label == false self.label.present? ? self.label : I18n.t(localization_key, default: self.name) end end |
#name_as_chain ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/forma/field.rb', line 41 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 ⇒ Object
56 57 58 |
# File 'lib/forma/field.rb', line 56 def parameter_name parameter_name_from_chain(name_as_chain) end |
#to_html(edit) ⇒ Object
Convert this element into HTML.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/forma/field.rb', line 61 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 |