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
t.column :hourly_rate, header: "Rate",
                       formatter: -> (amount) {'$%0.2f' % amount rescue 'N/A'}

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, :phone, :hourly_rate

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>
      <th class="hourly_rate">Rate</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>
      <td class="hourly_rate">$250.00</td>
    </tr>
  </tbody>
</table>

Note that column options include :formatter and :header.

:formatter may be a stabby lambda which is passed the value to be formatted.

:header may be either a string

Defined Under Namespace

Classes: Column

Instance Attribute Summary collapse

Attributes inherited from Expander

#handlers, #locals, #stack, #template

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:



56
57
58
59
60
# File 'lib/express_templates/components/table_for.rb', line 56

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.



62
63
64
# File 'lib/express_templates/components/table_for.rb', line 62

def columns
  @columns
end

Instance Method Details

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



64
65
66
67
# File 'lib/express_templates/components/table_for.rb', line 64

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

#compileObject



107
108
109
# File 'lib/express_templates/components/table_for.rb', line 107

def compile
  wrap_for_stack_trace(lookup(:markup))
end

#wrap_for_stack_trace(body) ⇒ Object



103
104
105
# File 'lib/express_templates/components/table_for.rb', line 103

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