class AutoForme::Table

  1. lib/autoforme/table.rb
Superclass: Object

Helper class for formating HTML tables used for the browse/search results pages.


Public Class

  1. new

Public Instance

  1. action
  2. columns
  3. h
  4. model
  5. objs
  6. request
  7. to_s
  8. type


action [R]

The AutoForme::Action for the current table

columns [R]

The data columns for the current table

model [R]

The AutoForme::Model for the current table

objs [R]

An array of objects to show in the table

request [R]

The AutoForme::Request for the current table

type [R]

The action type for the current table

Public Class methods

new (action, objs)
[show source]
# File lib/autoforme/table.rb, line 23
def initialize(action, objs)
  @action = action
  @request = action.request
  @model = action.model
  @type = action.normalized_type
  @columns = model.columns_for(type, request)
  @objs = objs

Public Instance methods

h (s)
[show source]
# File lib/autoforme/table.rb, line 32
def h(s)
to_s ()

Return an HTML string for the table.

[show source]
# File lib/autoforme/table.rb, line 37
def to_s
  html =
  html << "<table id=\"autoforme_table\" class=\"#{model.table_class_for(type, request)}\">"

  html << "<thead><tr>"
  columns.each do |column|
    html << "<th>#{h action.column_label_for(type, request, model, column)}</th>"
  html << "<th>Show</th>" if show = model.supported_action?(:show, request)
  html << "<th>Edit</th>" if edit = model.supported_action?(:edit, request)
  html << "<th>Delete</th>" if delete = model.supported_action?(:delete, request)
  html << "</tr></thead>"

  html << "<tbody>"
  objs.each do |obj|
    html << "<tr>"
    columns.each do |column|
      unless val = model.show_html_for(obj, column, type, request)
        val = model.column_value(type, request, obj, column)
        val = val.to_s('F') if defined?(BigDecimal) && val.is_a?(BigDecimal)
        val = h(val)
      html << "<td>#{val}</td>"
    html << "<td><a href=\"#{action.url_for("show/#{model.primary_key_value(obj)}")}\" class=\"btn btn-xs btn-info\">Show</a></td>" if show
    html << "<td><a href=\"#{action.url_for("edit/#{model.primary_key_value(obj)}")}\" class=\"btn btn-xs btn-primary\">Edit</a></td>" if edit
    html << "<td><a href=\"#{action.url_for("delete/#{model.primary_key_value(obj)}")}\" class=\"btn btn-xs btn-danger\">Delete</a></td>" if delete
    html << "</tr>"
  html << "</tbody></table>"