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
29
30
31
# 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] || []
  # row class
  @row_class = h[:row_class]
  @checkboxes = h[:checkboxes]
  # context
  @context = h[:context]
end

Instance Attribute Details

#checkboxes(val = true) ⇒ Object (readonly)

Returns the value of attribute checkboxes.



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

def checkboxes
  @checkboxes
end

#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

#row_class(clazz) ⇒ Object (readonly)

Returns the value of attribute row_class.



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

def row_class
  @row_class
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



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

def add_field(f)
  @fields << f
end

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



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

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

#paginate(h = {}) ⇒ Object



58
59
60
61
# File 'lib/forma/table.rb', line 58

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

#table_body_elementObject



121
122
123
# File 'lib/forma/table.rb', line 121

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

#table_header_elementObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/forma/table.rb', line 79

def table_header_element
  children = []
  if @checkboxes
    children << el('th', attrs: { style: {width: '10px'} }, children: [
      el('input', attrs: { id: "all-models", type: 'checkbox' })
    ])
  end
  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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/forma/table.rb', line 100

def table_row(model)
  children = []
  if @checkboxes
    children << el('td', children: [
      el('input', attrs: { id: "model-#{model.id}", type: 'checkbox' })
    ])
  end
  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
  ############################################################################################
  options = { children: children }
  options[:attrs] = { class: eval_with_model(@row_class, model: model) } if @row_class
  ############################################################################################
  html = el('tr', options)
end

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



44
45
46
47
# File 'lib/forma/table.rb', line 44

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

#to_htmlObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/forma/table.rb', line 33

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