Class: Forma::Table

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = {}) ⇒ Table

Returns a new instance of Table.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/forma/table.rb', line 11

def initialize(h = {})
  h = h.symbolize_keys
  @id = h[:id]
  # title properties
  @title = h[:title]
  @icon = h[:icon]
  @collapsible = h[:collapsible]
  @collapsed = h[:collapsed]
  # values and fields
  @models = h[:models]
  @fields = h[:fields] || []
  @paginate = h[:paginate]
  # actions
  @title_actions = h[:title_actions] || []
  @item_actions = h[:item_actions] || []
  # context
  @context = h[:context]
end

Instance Attribute Details

#collapsedObject (readonly)

Returns the value of attribute collapsed.



7
8
9
# File 'lib/forma/table.rb', line 7

def collapsed
  @collapsed
end

#collapsibleObject (readonly)

Returns the value of attribute collapsible.



7
8
9
# File 'lib/forma/table.rb', line 7

def collapsible
  @collapsible
end

#iconObject (readonly)

Returns the value of attribute icon.



7
8
9
# File 'lib/forma/table.rb', line 7

def icon
  @icon
end

#modelsObject

Returns the value of attribute models.



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

def models
  @models
end

#titleObject (readonly)

Returns the value of attribute title.



7
8
9
# File 'lib/forma/table.rb', line 7

def title
  @title
end

#title_actionsObject (readonly)

Returns the value of attribute title_actions.



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

def title_actions
  @title_actions
end

Instance Method Details

#add_field(f) ⇒ Object



51
52
53
# File 'lib/forma/table.rb', line 51

def add_field(f)
  @fields << f
end

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



46
47
48
49
# File 'lib/forma/table.rb', line 46

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

#paginate(h = {}) ⇒ Object



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

def paginate(h={})
  @paginate = true
  @paginate_options = h
end

#table_body_elementObject



97
98
99
100
# File 'lib/forma/table.rb', line 97

def table_body_element
  children = 
  el('tbody', children: @models.map { |model| table_row(model) })
end

#table_header_elementObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/forma/table.rb', line 73

def table_header_element
  children = @fields.map { |f|
    f.model = @models.first
    label_text = f.localized_label
    label_hint = f.localized_hint
    el('th', attrs: { class: 'ff-field' }, text: label_text, children: [
      (el('i', attrs: { class: 'ff-field-hint', 'data-toggle' => 'tooltip', title: label_hint }) if label_hint.present?)
    ])
  }
  children << el('th', attrs: { style: {width: '100px'} }) if @item_actions.any?
  el('thead', children: [
    el('tr', children: children)
  ])
end

#table_row(model) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/forma/table.rb', line 87

def table_row(model)
  children = @fields.map { |fld|
    fld.model = model
    el('td', children: [ fld.to_html(false) ])
  }
  if @item_actions.any?
    children << el('td', children: @item_actions.map { |act| act.to_html(model) })
  end
  el('tr', children: children)
end

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



41
42
43
44
# File 'lib/forma/table.rb', line 41

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

#to_htmlObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/forma/table.rb', line 30

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