Module: TableHelper

Defined in:
app/helpers/table_helper.rb

Overview

Defines tables to display a list of entries. The helper methods come in different granularities:

  • #plain_table - A basic table for the given entries and attributes using the Crud::TableBuilder.

  • #list_table - A sortable #plain_table for the current entries, with the given attributes or default.

  • #crud_table - A sortable #plain_table for the current entries, with the given attributes or default and the standard crud action links.

Instance Method Summary collapse

Instance Method Details

#crud_table(*attrs, &block) ⇒ Object

Create a table of the current entries with the default or the passed attributes in its columns. Edit and destroy actions are added to each row. If attrs are present, the first column will link to the show action. Edit and destroy actions are appended to the end of each row. If a block is given, the column defined there will be inserted between the given attributes and the actions. An options hash for the table builder may be given as the last argument.


55
56
57
58
59
60
61
62
63
64
# File 'app/helpers/table_helper.rb', line 55

def crud_table(*attrs, &block)
  attrs, options = explode_attrs_with_options(attrs, &block)
  first = attrs.shift
  plain_table_or_message(entries, options) do |t|
    t.attr_with_show_link(first) if first
    t.sortable_attrs(*attrs)
    yield t if block_given?
    standard_table_actions(t)
  end
end

#list_table(*attrs, &block) ⇒ Object

Create a table of the entries with the default or the passed attributes in its columns. An options hash may be given as the last argument.


40
41
42
43
44
45
46
# File 'app/helpers/table_helper.rb', line 40

def list_table(*attrs, &block)
  attrs, options = explode_attrs_with_options(attrs, &block)
  plain_table_or_message(entries, options) do |t|
    t.sortable_attrs(*attrs)
    yield t if block_given?
  end
end

#plain_table(entries, *attrs) ⇒ Object

Renders a table for the given entries. One column is rendered for each attribute passed. If a block is given, the columns defined therein are appended to the attribute columns. If entries is empty, an appropriate message is rendered. An options hash may be given as the last argument.


16
17
18
19
20
21
22
23
24
# File 'app/helpers/table_helper.rb', line 16

def plain_table(entries, *attrs)
  options = attrs.extract_options!
  add_css_class(options, 'table table-striped table-hover')
  builder = options.delete(:builder) || DryCrud::Table::Builder
  builder.table(entries, self, options) do |t|
    t.attrs(*attrs)
    yield t if block_given?
  end
end

#plain_table_or_message(entries, *attrs, &block) ⇒ Object

Renders a #plain_table for the given entries. If entries is empty, an appropriate message is rendered.


28
29
30
31
32
33
34
35
# File 'app/helpers/table_helper.rb', line 28

def plain_table_or_message(entries, *attrs, &block)
  entries.to_a # force evaluation of relations
  if entries.present?
    plain_table(entries, *attrs, &block)
  else
    tag.div(ti(:no_list_entries), class: 'table')
  end
end

#standard_table_actions(table) ⇒ Object

Adds standard action link columns (edit, destroy) to the given table.


67
68
69
70
# File 'app/helpers/table_helper.rb', line 67

def standard_table_actions(table)
  table.edit_action_col
  table.destroy_action_col
end