class Forma::Table

Attributes

checkboxes[R]
collapsed[R]
collapsible[R]
icon[R]
models[RW]
row_class[R]
title[R]
title_actions[R]

Public Class Methods

new(h = {}) click to toggle source
# File lib/forma/table.rb, line 10
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

Public Instance Methods

add_field(f) click to toggle source
# File lib/forma/table.rb, line 53
def add_field(f)
  @fields << f
end
item_action(url, h={}) click to toggle source
# File lib/forma/table.rb, line 48
def item_action(url, h={})
  h[:url] = url
  @item_actions << Action.new(h)
end
paginate(h={}) click to toggle source
# File lib/forma/table.rb, line 57
def paginate(h={})
  @paginate = true
  @paginate_options = h
end
title_action(url, h={}) click to toggle source
# File lib/forma/table.rb, line 43
def title_action(url, h={})
  h[:url] = url
  @title_actions << Action.new(h)
end
to_html() click to toggle source
# File lib/forma/table.rb, line 32
def to_html
  el(
    'div',
    attrs: { id: @id, class: ['ff-table'] },
    children: [
      title_element,
      body_element,
    ]
  )
end

Private Instance Methods

body_element() click to toggle source
# File lib/forma/table.rb, line 67
def body_element
  el(
    'div', attrs: {
      class: ['ff-table-body', 'ff-collapsible-body'],
      style: ( {display: 'none'} if @collapsible && @collapsed ),
    },
    children: [ table_element, pagination_element ]
  )
end
eval_with_model(val, h={}) click to toggle source
# File lib/forma/table.rb, line 147
def eval_with_model(val, h={}); val.is_a?(Proc) ? val.call(h[:model] || self.model) : val.to_s end
pagination_element() click to toggle source
# File lib/forma/table.rb, line 131
def pagination_element
  if @paginate and @context
    self_from_block = eval("self", @context.binding)
    s = self_from_block.send(:will_paginate, @models, @paginate_options)
    paginate = el('div', html: s.to_s)
    el('div', attrs: { class: 'ff-paginate' }, children: [
      paginate,
      (el('div', attrs: { class: 'ff-totals' }, children: [
        el('code', text: "#{@models.total_entries}"),
        el('span', text: @paginate_options[:records] || 'records')
      ]) if @models.total_entries > 0)
    ])
  end
end
table_body_element() click to toggle source
# File lib/forma/table.rb, line 120
def table_body_element
  children = el('tbody', children: @models.map { |model| table_row(model) })
end
table_element() click to toggle source
# File lib/forma/table.rb, line 77
def table_element
  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

  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

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

  if @models and @models.any?
    el('table', attrs: { class: 'ff-common-table' }, children: [ table_header_element, table_body_element ])
  else
    el('div', attrs: { class: ['ff-empty', 'ff-table-empty'] }, text: Forma.config.texts.table_empty)
  end
end
table_header_element() click to toggle source
# File lib/forma/table.rb, line 78
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) click to toggle source
# File lib/forma/table.rb, line 99
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