Class: DryCrud::Table::Builder

Inherits:
Object
  • Object
show all
Includes:
Actions, Sorting
Defined in:
app/helpers/dry_crud/table/builder.rb

Overview

A simple helper to easily define tables listing several rows of the same data type.

Example Usage:

DryCrud::Table::Builder.table(entries, template) do |t|
  t.col('My Header', class: 'css') {|e| link_to 'Show', e }
  t.attrs :name, :city
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Actions

#action_col, #attr_with_show_link, #destroy_action_col, #edit_action_col, #show_action_col, #table_action_link

Methods included from Sorting

#sort_header, #sortable_attr, #sortable_attrs

Constructor Details

#initialize(entries, template, options = {}) ⇒ Builder

Returns a new instance of Builder.


23
24
25
26
27
28
# File 'app/helpers/dry_crud/table/builder.rb', line 23

def initialize(entries, template, options = {})
  @entries = entries
  @template = template
  @options = options
  @cols = []
end

Instance Attribute Details

#colsObject (readonly)

Returns the value of attribute cols


17
18
19
# File 'app/helpers/dry_crud/table/builder.rb', line 17

def cols
  @cols
end

#entriesObject (readonly)

Returns the value of attribute entries


17
18
19
# File 'app/helpers/dry_crud/table/builder.rb', line 17

def entries
  @entries
end

#optionsObject (readonly)

Returns the value of attribute options


17
18
19
# File 'app/helpers/dry_crud/table/builder.rb', line 17

def options
  @options
end

#templateObject (readonly)

Returns the value of attribute template


17
18
19
# File 'app/helpers/dry_crud/table/builder.rb', line 17

def template
  @template
end

Class Method Details

.table(entries, template, options = {}) {|t| ... } ⇒ Object

Convenience method to directly generate a table. Renders a row for each entry in entries. Takes a block that gets the table object as parameter for configuration. Returns the generated html for the table.

Yields:

  • (t)

33
34
35
36
37
# File 'app/helpers/dry_crud/table/builder.rb', line 33

def self.table(entries, template, options = {})
  t = new(entries, template, options)
  yield t
  t.to_html
end

Instance Method Details

#align_class(attr) ⇒ Object

Returns css classes used for alignment of the cell data. Based on the column type of the attribute.


75
76
77
78
79
80
81
82
83
# File 'app/helpers/dry_crud/table/builder.rb', line 75

def align_class(attr)
  entry = entries.present? ? entry_class.new : nil
  case column_type(entry, attr)
  when :integer, :float, :decimal
    'right' unless association(entry, attr, :belongs_to)
  when :boolean
    'center'
  end
end

#attr(attr, header = nil, html_options = {}, &block) ⇒ Object

Define a column for the given attribute and an optional header. If no header is given, the attribute name is used. The cell will contain the formatted attribute value for the current entry.


58
59
60
61
62
63
# File 'app/helpers/dry_crud/table/builder.rb', line 58

def attr(attr, header = nil, html_options = {}, &block)
  header ||= attr_header(attr)
  block ||= ->(e) { format_attr(e, attr) }
  add_css_class(html_options, align_class(attr))
  col(header, html_options, &block)
end

#attr_header(attr) ⇒ Object

Creates a header string for the given attr.


86
87
88
# File 'app/helpers/dry_crud/table/builder.rb', line 86

def attr_header(attr)
  captionize(attr, entry_class)
end

#attrs(*attrs) ⇒ Object

Convenience method to add one or more attribute columns. The attribute name will become the header, the cells will contain the formatted attribute value for the current entry.


49
50
51
52
53
# File 'app/helpers/dry_crud/table/builder.rb', line 49

def attrs(*attrs)
  attrs.each do |a|
    attr(a)
  end
end

#col(header = '', html_options = {}, &block) ⇒ Object

Define a column for the table with the given header, the html_options used for each td and a block rendering the contents of a cell for the current entry. The columns appear in the order they are defined.


42
43
44
# File 'app/helpers/dry_crud/table/builder.rb', line 42

def col(header = '', html_options = {}, &block)
  @cols << Col.new(header, html_options, @template, block)
end

#to_htmlObject

Renders the table as HTML.


66
67
68
69
70
71
# File 'app/helpers/dry_crud/table/builder.rb', line 66

def to_html
  tag.table(options) do
    tag.thead(html_header) +
      (:tbody, entries) { |e| html_row(e) }
  end
end