Class: Forma::Form

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

Overview

Form.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

extract_value, number_format, #simple_value, singular_name

Methods included from WithTitleElement

#active_title, #title_element

Methods included from FieldHelper

#array_field, #boolean_field, #combo_field, #complex_field, #date_field, #email_field, #file_field, #image_field, #map_field, #number_field, #password_field, #select_field, #subform, #table_field, #text_field

Methods included from Html

attr, el

Constructor Details

#initialize(h = {}) ⇒ Form



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/forma/form.rb', line 14

def initialize(h = {})
  h = h.symbolize_keys
  # general
  @id = h[:id]
  # @theme = h[:theme] || 'blue'
  # title properties
  @title = h[:title]
  @icon = h[:icon]
  @collapsible = h[:collapsible]
  @collapsed = h[:collapsed]
  # submit options
  @url = h[:url]
  @submit = h[:submit] || 'Save'
  @wait_on_submit = h[:wait_on_submit].blank? ? true : (not not h[:wait_on_submit])
  @method = h[:method]
  @auth_token = h[:auth_token]
  # tabs
  @tabs = h[:tabs] || []
  @selected_tab = h[:selected_tab] || 0
  # model, errors and editing options
  @model = h[:model]
  @errors = h[:errors]
  @edit = h[:edit]
  @model_name = h[:model_name]
  # actions
  @title_actions = h[:title_actions] || []
  @bottom_actions = h[:bottom_actions] || []
  @multipart = (not not h[:multipart])
end

Instance Attribute Details

#collapsedObject

Returns the value of attribute collapsed.



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

def collapsed
  @collapsed
end

#collapsibleObject

Returns the value of attribute collapsible.



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

def collapsible
  @collapsible
end

#editObject

Returns the value of attribute edit.



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

def edit
  @edit
end

#iconObject

Returns the value of attribute icon.



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

def icon
  @icon
end

#modelObject

Returns the value of attribute model.



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

def model
  @model
end

#model_nameObject

Returns the value of attribute model_name.



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

def model_name
  @model_name
end

#parent_fieldObject

Returns the value of attribute parent_field.



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

def parent_field
  @parent_field
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#title_actionsObject

Returns the value of attribute title_actions.



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

def title_actions
  @title_actions
end

Instance Method Details

#add_field(f) ⇒ Object

Adding a new field to this form.



80
81
82
83
# File 'lib/forma/form.rb', line 80

def add_field(f)
  @tabs = [ Tab.new ] if @tabs.empty?
  @tabs[0].col1.add_field(f)
end

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



65
66
67
68
69
# File 'lib/forma/form.rb', line 65

def bottom_action(url, h={})
  h[:url] = url
  h[:as] = 'button' unless h[:as].present?
  @bottom_actions << Action.new(h)
end

#column_element(col, hasSecondCol) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/forma/form.rb', line 155

def column_element(col, hasSecondCol)
  if col
    el('div', attrs: { class: ['ff-col', (hasSecondCol ? 'ff-col-50' : 'ff-col-100')]}, children: [
      el('div', attrs: { class: 'ff-col-inner' }, children: col.fields.map { |fld| field_element(fld) })
    ])
  end
end

#field_error_element(errors) ⇒ Object



120
121
122
123
124
# File 'lib/forma/form.rb', line 120

def field_error_element(errors)
  many = errors.length > 1
  children = (many ? errors.map { |e| el('li', text: e.to_s)  } : [el('div', text: errors[0].to_s)])
  el('div', attrs: { class: 'ff-field-errors' }, children: children)
end

#submit(txt = nil) ⇒ Object



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

def submit(txt = nil)
  @submit = txt if txt.present?
  @submit
end

#tab(opts = {}) {|tab| ... } ⇒ Object

Adds a new tab and ibject body content.

Yields:



72
73
74
75
76
77
# File 'lib/forma/form.rb', line 72

def tab(opts={})
  tab = Tab.new(opts)
  @tabs << tab
  yield tab if block_given?
  tab
end

#tab_actions(tab) ⇒ Object



162
163
164
165
166
# File 'lib/forma/form.rb', line 162

def tab_actions(tab)
  if tab.actions.any?
    el('div', attrs: { class: 'ff-tab-actions' }, children: tab.actions.map { |a| a.to_html(@model) })
  end
end

#tab_element(tab) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/forma/form.rb', line 167

def tab_element(tab)
  hasSecondCol = (not tab.col2.fields.empty?)
  col1 = column_element(tab.col1, hasSecondCol)
  col2 = column_element(tab.col2, hasSecondCol)
  el('div', attrs: { class: 'ff-tab-content',style: ({ display: 'none' } if @tabs.index(tab) != @selected_tab) },
    children: [
      tab_actions(tab),
      el('div', attrs: { class: 'ff-cols'}, children: [col1, col2])
    ]
  )
end

#tabs_headerObject



178
179
180
181
182
183
184
185
186
187
# File 'lib/forma/form.rb', line 178

def tabs_header
  if @tabs.length > 1
    el('ul', attrs: { class: 'ff-tabs-header' }, children: @tabs.map { |tab|
      el('li', attrs: { class: ('ff-selected' if @tabs.index(tab) == @selected_tab) }, children: [
        (el('img', attrs: { src: tab.icon }) if tab.icon),
        (el('span', text: tab.title || 'No Title'))
      ])
    })
  end
end

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



60
61
62
63
# File 'lib/forma/form.rb', line 60

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

#to_htmlObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/forma/form.rb', line 44

def to_html
  el(
    'div',
    attrs: { id: @id, class: [ 'ff-form' ] },
    children: [
      title_element,
      body_element,
    ]
  )
end