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) ⇒ Table

Returns a new instance of Table.



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

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

Instance Attribute Details

#actionObject (readonly)

The AutoForme::Action for the current table



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

def action
  @action
end

#columnsObject (readonly)

The data columns for the current table



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

def columns
  @columns
end

#modelObject (readonly)

The AutoForme::Model for the current table



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

def model
  @model
end

#objsObject (readonly)

An array of objects to show in the table



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

def objs
  @objs
end

#requestObject (readonly)

The AutoForme::Request for the current table



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

def request
  @request
end

#typeObject (readonly)

The action type for the current table



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

def type
  @type
end

Instance Method Details

#h(s) ⇒ Object



33
34
35
# File 'lib/autoforme/table.rb', line 33

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

#to_sObject

Return an HTML string for the table.



38
39
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
# File 'lib/autoforme/table.rb', line 38

def to_s
  html = String.new
  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>"
  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|
      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)
      end
      html << "<td>#{val}</td>"
    end
    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>"
  end
  html << "</tbody></table>"
  html
end