Class: Erector::Widgets::Table

Inherits:
Erector::Widget show all
Defined in:
lib/erector/widgets/table.rb

Overview

The Table widget provides the ability to render a table from a list of objects (one for each row).

Because the default for the column titles utilizes the ActiveSupport Inflector#titleize method, this widget requires active_support to be loaded.

class UsersTable < Erector::Widgets::Table
  column :first_name
  column :last_name
  column :email
  row_classes :even, :odd
end

widget UsersTable, :row_objects => [user_1, user_2, user_3]

Defined Under Namespace

Classes: ColumnDefinition

Constant Summary

Constants inherited from Erector::Widget

Erector::Widget::NON_NEWLINEY, Erector::Widget::RESERVED_INSTANCE_VARS, Erector::Widget::SPACES_PER_INDENT

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Erector::Widget

#_render, after_initialize, all_tags, #assign_local, #assign_locals, #capture, #character, #close_tag, #css, #element, #empty_element, empty_tags, full_tags, #h, #html_escape, #initialize, #instruct, #javascript, #join, #nbsp, needs, #newliney, #open_tag, #prettyprint_default, prettyprint_default=, #raw, #rawtext, #text, #to_a, #to_pretty, #to_s, #url, #widget, #write_via

Constructor Details

This class inherits a constructor from Erector::Widget

Class Attribute Details

.row_class_listObject (readonly)

Returns the value of attribute row_class_list.



42
43
44
# File 'lib/erector/widgets/table.rb', line 42

def row_class_list
  @row_class_list
end

Class Method Details

.column(id, name = id.to_s.titleize, &cell_proc) ⇒ Object

Define a column, optionally specifying the name (the heading that the user sees) and a block which renders the cell given a row object. If the block is not specified, the cell contains the result of calling a method whose name is id.

The name can be a string or a proc.



26
27
28
29
# File 'lib/erector/widgets/table.rb', line 26

def column(id, name=id.to_s.titleize, &cell_proc)
  cell_proc ||= proc {|object| text object.__send__(id)}
  column_definitions << ColumnDefinition.new(id, name, cell_proc)
end

.column_definitionsObject

:nodoc:



31
32
33
# File 'lib/erector/widgets/table.rb', line 31

def column_definitions #:nodoc:
  @column_definitions ||= []
end

.row_classes(*row_classes) ⇒ Object

A list of HTML classes to apply to the rows in turn. After the list is exhausted, start again at the start. The most common use for this is to specify one class for odd rows and a different class for even rows.



39
40
41
# File 'lib/erector/widgets/table.rb', line 39

def row_classes(*row_classes)
  @row_class_list = row_classes
end

Instance Method Details

#contentObject

The standard erector content method.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/erector/widgets/table.rb', line 46

def content
  table do
    thead do
      tr do
        column_definitions.each do |column_def|
          th do
            if column_def.name.is_a?(Proc)
              self.instance_exec(column_def.id, &column_def.name)
            else
              text column_def.name
            end
          end
        end
      end
    end
    tbody do
      @row_objects.each_with_index do |object, index|
        row object, index
      end
    end
  end
end