Class: ExpressTemplates::Components::TableFor

Inherits:
Base
  • Object
show all
Includes:
Capabilities::Building, Capabilities::Configurable
Defined in:
lib/express_templates/components/table_for.rb

Overview

Create an html table from a collection of data.

Typically this will be a collection of models of the same type. Each member of the collection must respond to the column names provided.

Example:

“‘ruby table_for(:people) do |t|

t.column :name
t.column :email
t.column :phone

end “‘

This assumes that a @people variable will exist in the view and that it will be a collection whose members respond to :name, :email, and :phone

This will result in markup like the following:

<table id="people">
  <thead>
    <tr>
      <th class="name">Name</th>
      <th class="email">Email</th>
      <th class="phone">Phone</th>
    </tr>
  </thead>
  <tbody>
    <tr id="person-1">
      <td class="name">Steven Talcott Smith</td>
      <td class="email">[email protected]</td>
      <td class="phone">415-555-1212</td>
    </tr>
  </tbody>
</table>

Defined Under Namespace

Classes: Column

Instance Attribute Summary collapse

Attributes inherited from Expander

#handlers, #locals, #stack

Instance Method Summary collapse

Methods included from Capabilities::Configurable

included

Methods inherited from Base

inherited

Methods included from Capabilities::Iterating

included

Methods included from Capabilities::Wrapping

included

Methods included from Capabilities::Rendering

included

Methods included from Capabilities::Templating

included

Methods included from Macro

included

Methods inherited from Expander

#expand, #initialize_expander, #method_missing, #process_children!, register_macros_for

Constructor Details

#initialize(*args) {|_self| ... } ⇒ TableFor

Returns a new instance of TableFor.

Yields:

  • (_self)

Yield Parameters:



46
47
48
49
50
# File 'lib/express_templates/components/table_for.rb', line 46

def initialize(*args)
  super(*args)
  _process_args!(args) # from Configurable
  yield(self) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ExpressTemplates::Expander

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



52
53
54
# File 'lib/express_templates/components/table_for.rb', line 52

def columns
  @columns
end

Instance Method Details

#column(name, options = {}) ⇒ Object



54
55
56
57
# File 'lib/express_templates/components/table_for.rb', line 54

def column(name, options = {})
  @columns ||= []
  @columns << Column.new(name, options)
end

#compileObject



91
92
93
# File 'lib/express_templates/components/table_for.rb', line 91

def compile
  wrap_for_stack_trace(lookup(:markup))
end

#wrap_for_stack_trace(body) ⇒ Object



87
88
89
# File 'lib/express_templates/components/table_for.rb', line 87

def wrap_for_stack_trace(body)
  "ExpressTemplates::Components::TableFor.render_in(self) {\n#{body}\n}"
end