Class: Tablemaker::ViewHelpers::ActionView::TableHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/tablemaker/view_helpers.rb

Instance Method Summary collapse

Constructor Details

#initialize(context, &block) ⇒ TableHelper

Returns a new instance of TableHelper.



7
8
9
10
11
12
13
14
15
# File 'lib/tablemaker/view_helpers.rb', line 7

def initialize(context, &block)
  @context = context
  @stack = []
  @root = Tablemaker.column do |r|
    stack(r) do
      yield self
    end
  end
end

Instance Method Details

#column(&blk) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tablemaker/view_helpers.rb', line 25

def column(&blk)
  if @stack.size == 1
    row do
      column(&blk)
    end
  else
    current.column do |c|
      stack(c, &blk)
    end
  end
end

#rowObject



17
18
19
20
21
22
23
# File 'lib/tablemaker/view_helpers.rb', line 17

def row
  current.row do |r|
    stack(r) do
      yield
    end
  end
end

#td(*args, &blk) ⇒ Object



37
38
39
# File 'lib/tablemaker/view_helpers.rb', line 37

def td(*args, &blk)
  cell("td", *args, &blk)
end

#th(*args, &blk) ⇒ Object



41
42
43
# File 'lib/tablemaker/view_helpers.rb', line 41

def th(*args, &blk)
  cell("th", *args, &blk)
end

#to_html(attrs = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tablemaker/view_helpers.rb', line 45

def to_html(attrs = {})
  context.("table", attrs) do
    @root.each_row do |r|
      s = context.("tr") do
        r.each do |c|
          attrs = {}
          attrs[:rowspan] = c.real_rows if c.real_rows > 1
          attrs[:colspan] = c.real_cols if c.real_cols > 1
          s2 = context.(c.data[:name], c.data[:opts].merge(attrs)) do
            c.data[:text]
          end

          context.concat(s2)
        end
      end
      context.concat(s)
    end
  end
end