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 (or some other gem that adds ‘titleize` to String).

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Erector::Widget

#to_html, #to_s

Methods included from Sass

#sass, #scss

Methods included from JQuery

#jquery, #jquery_load, #jquery_ready

Methods included from Convenience

#css, #dom_id, #javascript, #join, #to_pretty, #to_text, #url

Methods included from Externals

included, #render_externals, #render_with_externals

Methods included from Caching

#cache, included, #should_cache?

Methods included from Needs

included, #initialize

Methods inherited from HTMLWidget

#to_html, #to_s

Methods inherited from XMLWidget

#comment, full_tags, #instruct, #newliney?, self_closing_tags, tag, tag_named

Methods inherited from AbstractWidget

#call_block, #capture_content, #emit, hyphenize_underscores, hyphenize_underscores=, #initialize, inline, prettyprint_default, #prettyprint_default, prettyprint_default=, #to_a, #to_s, #widget

Methods included from AfterInitialize

included, #initialize

Methods included from Text

#character, #h, #nbsp, #raw, #text, #text!

Methods included from Attributes

#format_attributes, #format_sorted, #sort_attributes

Methods included from Element

#_element, #_empty_element, #element, #empty_element

Class Attribute Details

.row_class_listObject (readonly)

Returns the value of attribute row_class_list.



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

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.



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

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:



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

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.



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

def row_classes(*row_classes)
  @row_class_list = row_classes
end

Instance Method Details

#contentObject

The standard erector content method.



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

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