Class: FieldTable

Inherits:
Erector::Widget show all
Includes:
Erector::Inline
Defined in:
lib/erector/widgets/field_table.rb

Overview

A simple HTML table with three columns: label, contents, and (optionally) note. Each row is called a field. The last row can optionally contain “buttons” (actually any widget will do) that are all shown in the 2nd column. It’s all surrounded with a fieldset element for a title.

In ASCII art, it looks something like this:

/-Meals—————————————-\ | Breakfast | eggs | | | Lunch | sandwich | | | Dinner | lo mein | with shrimp! | | | [Save] [Cancel] | | -———————————————/

There are two ways to create a FieldTable.

  1. Pass a block in to the constructor.

  2. Make a subclass.

In both cases you’ll want to call the “field” and “button” methods on the table. This sets up the contents which will be rendered later during FieldTable#content. If you make a subclass (#2) you can do this either in the constructor or in the content method before you call super.

The FieldTable is surrounded by a fieldset element whose legend is the “title” instance variable. Inside this fieldset is a table element. Each field (row of the table) has a label (th), a content cell (td), and an optional note (td).

If you call “button” you can pass in a block that’ll get rendered inside the 2nd column of the last row. The idea here is that you might want to make an HTML form that has a bunch of buttons at the bottom (Save, Cancel, Clear) and these all go in the same cell, with no label for the row.

TODO: error messages?

Author:

  • Alex Chaffee

Defined Under Namespace

Classes: Field

Instance Method Summary collapse

Methods included from Erector::Inline

#call_block

Methods inherited from Erector::Widget

#to_html, #to_s

Methods included from Erector::Sass

#sass, #scss

Methods included from Erector::JQuery

#jquery, #jquery_load, #jquery_ready

Methods included from Erector::Convenience

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

Methods included from Erector::Externals

included, #render_externals, #render_with_externals

Methods included from Erector::Caching

#cache, included, #should_cache?

Methods included from Erector::Needs

included

Methods inherited from Erector::HTMLWidget

#to_html, #to_s

Methods inherited from Erector::XMLWidget

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

Methods inherited from Erector::AbstractWidget

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

Methods included from Erector::AfterInitialize

included

Methods included from Erector::Text

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

Methods included from Erector::Attributes

#format_attributes, #format_sorted, #sort_attributes

Methods included from Erector::Element

#_element, #_empty_element, #element, #empty_element

Constructor Details

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

Pass in a block and it’ll get called with a pointer to this table, so you can call ‘field’ and ‘button’ to configure it

Yields:

  • (_self)

Yield Parameters:

  • _self (FieldTable)

    the object that the method was called on



77
78
79
80
81
82
# File 'lib/erector/widgets/field_table.rb', line 77

def initialize(*args)
  super
  @fields = []
  @buttons = []
  yield self if block_given? # invoke the configuration block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Erector::Inline

Instance Method Details

#button(&button_proc) ⇒ Object

If you call “button” you can pass in a block that’ll get rendered inside the 2nd column of the last row. The idea here is that you might want to make an HTML form that has a bunch of buttons at the bottom (Save, Cancel, Clear) and these all go in the same cell, with no label for the row.

TODO: allow passing in a widget instead of a block



69
70
71
# File 'lib/erector/widgets/field_table.rb', line 69

def button(&button_proc)
  @buttons << button_proc
end

#contentObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/erector/widgets/field_table.rb', line 84

def content
  fieldset :class => "field_table" do
    legend @title
    table :width => '100%' do
      @fields.each do |f|
        widget f
      end
      unless @buttons.empty?
        tr :class => "field_table_buttons" do
          td :colspan => 2, :align => "right" do          
            table :class => 'layout' do
              tr do
                @buttons.each do |button|
                  td :class => "field_table_button" do
                    button.call
                  end
                end
              end
            end
          end
        end
      end
    end
  end
end

#field(label = nil, note = nil, &contents) ⇒ Object

Define a field, containing a label, optional note, and block for the contents

TODO: allow passing in a widget instead of a block



60
61
62
# File 'lib/erector/widgets/field_table.rb', line 60

def field(label = nil, note = nil, &contents)
  @fields << Field.new(:label => label, :note => note, &contents)
end