Class: TableHelper::Table

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(template, records, options = {}) ⇒ Table

Returns a new instance of Table.



7
8
9
10
11
12
13
14
15
16
# File 'lib/table_for/table.rb', line 7

def initialize(template, records, options = {})
  @template, @records, @columns = template, records, []
  # table's html options
  @table_html_options = options.delete(:html) || {}
  # trs' html options
  @tr_html_options = @table_html_options.delete(:tr) || {}
  # stripes (cycling)
  @stripes = options.delete(:stripes) || []
  @stripes.extend CoreEx::ArrayIterator
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &proc) ⇒ Object (protected)



55
56
57
# File 'lib/table_for/table.rb', line 55

def method_missing(method, *args, &proc)
  @template.send(method, *args, &proc)
end

Instance Method Details

#column(*args, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/table_for/table.rb', line 30

def column(*args, &block)
  col_options = args.extract_options!
  res = nil

  attr = args.shift or nil

  if block_given?
    col_options[:callback] = block
    @columns << (res = CallbackColumn.new(@template, @records, attr, col_options))
  elsif attr
    @columns << (res = SimpleColumn.new(@template, @records, attr, col_options))
  else
    raise ArgumentError, "Attribute name or block should be given"
  end
  res
end

#columns(*args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/table_for/table.rb', line 18

def columns(*args)
  res = []
  if args.present?
    args.each do |arg|
      res << self.column(arg)
    end
  else
    raise ArgumentError, "At least one attribute name should be given"
  end
  res
end

#drawObject



47
48
49
50
51
# File 'lib/table_for/table.rb', line 47

def draw
   :table, @table_html_options do
    head + body
  end
end