Class: TableHelper::Body

Inherits:
HtmlElement show all
Defined in:
lib/table_helper/body.rb

Overview

Represents the body of the table. In HTML, you can think of this as the <tbody> tag of the table.

Constant Summary collapse

@@empty_caption_class =
'ui-collection-empty'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from HtmlElement

#html

Constructor Details

#initialize(table) ⇒ Body

:nodoc:



21
22
23
24
25
26
# File 'lib/table_helper/body.rb', line 21

def initialize(table) #:nodoc:
  super()

  @table = table
  @empty_caption = 'No matches found.'
end

Instance Attribute Details

#alternateObject

If set to :odd or :even, every odd or even-numbered row will have the alternate class appended to its html attributes. Default is nil.



16
17
18
# File 'lib/table_helper/body.rb', line 16

def alternate
  @alternate
end

#empty_captionObject

The caption to display in the collection is empty



19
20
21
# File 'lib/table_helper/body.rb', line 19

def empty_caption
  @empty_caption
end

#tableObject (readonly)

The table this body is a part of



12
13
14
# File 'lib/table_helper/body.rb', line 12

def table
  @table
end

Instance Method Details

#build_row(object, index = table.collection.index(object)) ⇒ Object

Builds a row for an object in the table.

The provided block should set the values for each cell in the row.



79
80
81
82
83
84
# File 'lib/table_helper/body.rb', line 79

def build_row(object, index = table.collection.index(object))
  row = BodyRow.new(object, self)
  row.alternate = alternate ? index.send("#{alternate}?") : false
  @builder.call(row.builder, object, index) if @builder
  row.html
end

#each(&block) ⇒ Object

Builds the body of the table. This includes the actual data that is generated for each object in the collection.

each expects a block that defines the data in each cell. Each iteration of the block will provide the row within the table, the object being rendered, and the index of the object. For example,

t.rows.each do |row, post, index|
  row.title     "<div class=\"wrapped\">#{post.title}</div>"
  row.category  post.category.name
end

In addition, to specifying the data, you can also modify the html options of the row. For more information on doing this, see the BodyRow class.

If the collection is empty and empty_caption is set on the body, then the actual body will be replaced by a single row containing the html that was stored in empty_caption.

Default Values

Whenever possible, the default value of a cell will be set to the object’s attribute with the same name as the cell. For example, if a Post consists of the attribute title, then the cell for the title will be prepopulated with that attribute’s value:

t.rows.each do |row, post index|
  row.title post.title
end

row.title is already set to post.category so there’s no need to manually set the value of that cell. However, it is always possible to override the default value like so:

t.rows.each do |row, post, index|
  row.title     link_to(post.title, post_url(post))
  row.category  post.category.name
end


72
73
74
# File 'lib/table_helper/body.rb', line 72

def each(&block)
  @builder = block
end