Class: Autotable::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/autotable/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(collection, template, options) ⇒ Builder

Returns a new instance of Builder.



5
6
7
8
9
# File 'lib/autotable/builder.rb', line 5

def initialize(collection, template, options)
  @collection, @template = collection, template
  @options = DEFAULT_OPTIONS.merge(options)
  @columns, @actions = [], []
end

Instance Method Details

#action(title, options = {}) ⇒ Object



26
27
28
29
# File 'lib/autotable/builder.rb', line 26

def action(title, options = {})
  options[:title] = title
  @actions << options
end

#column(method, title = nil, &formatter) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/autotable/builder.rb', line 11

def column(method, title = nil, &formatter)
  if method.is_a?(String)
    title = method
    method = nil
  end
  
  @columns << {
    :method     => method,
    :title      => title || method.to_s.humanize,
    :formatter  => formatter
  }

  nil
end

#to_htmlObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/autotable/builder.rb', line 31

def to_html
  html  = "<table class='#{table_class}'>\n"
  html << header_html
  html << "<tbody>\n"
  
  if @collection.empty?
    html << "<tr class='warning'>\n"
    html << "<td colspan='#{@columns.size + 1}'>No items found</td>\n"
    html << "</tr>\n"
  else
    @collection.each { |c| html << row_html(c) }
  end
  
  html << "</tbody>\n"
  html << "</table>\n"
  html.html_safe
end