Class: AutoForme::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/autoforme/table.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, objs, opts = {}) ⇒ Table

Returns a new instance of Table.



25
26
27
28
29
30
31
32
33
# File 'lib/autoforme/table.rb', line 25

def initialize(action, objs, opts={})
  @action = action
  @request = action.request
  @model = action.model
  @type = action.normalized_type
  @columns = model.columns_for(type, request)
  @objs = objs
  @opts = opts
end

Instance Attribute Details

#actionObject (readonly)

The AutoForme::Action for the current table



5
6
7
# File 'lib/autoforme/table.rb', line 5

def action
  @action
end

#columnsObject (readonly)

The data columns for the current table



17
18
19
# File 'lib/autoforme/table.rb', line 17

def columns
  @columns
end

#modelObject (readonly)

The AutoForme::Model for the current table



8
9
10
# File 'lib/autoforme/table.rb', line 8

def model
  @model
end

#objsObject (readonly)

An array of objects to show in the table



20
21
22
# File 'lib/autoforme/table.rb', line 20

def objs
  @objs
end

#optsObject (readonly)

Any options for the table



23
24
25
# File 'lib/autoforme/table.rb', line 23

def opts
  @opts
end

#requestObject (readonly)

The AutoForme::Request for the current table



11
12
13
# File 'lib/autoforme/table.rb', line 11

def request
  @request
end

#typeObject (readonly)

The action type for the current table



14
15
16
# File 'lib/autoforme/table.rb', line 14

def type
  @type
end

Instance Method Details

#h(s) ⇒ Object



35
36
37
# File 'lib/autoforme/table.rb', line 35

def h(s)
  action.h(s)
end

#to_sObject

Return an HTML string for the table.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/autoforme/table.rb', line 40

def to_s
  html = "<table class=\"#{model.table_class_for(type, request)}\">"
  if caption = opts[:caption]
    html << "<caption>#{h caption}</caption>"
  end

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