Class: Tableview::ViewHandler::Table

Inherits:
TablePiece show all
Defined in:
lib/tableview/view_handler.rb

Instance Attribute Summary collapse

Attributes inherited from TablePiece

#options

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Table

Returns a new instance of Table.



26
27
28
29
30
31
32
# File 'lib/tableview/view_handler.rb', line 26

def initialize(*args)
  super *args
  self.parts = []
  @current_part = Body.new
  @added = false
  @headers, @procs, @subtables = [], [], []
end

Instance Attribute Details

#partsObject

Returns the value of attribute parts.



25
26
27
# File 'lib/tableview/view_handler.rb', line 25

def parts
  @parts
end

#subtablesObject

Returns the value of attribute subtables.



25
26
27
# File 'lib/tableview/view_handler.rb', line 25

def subtables
  @subtables
end

#titleObject

Returns the value of attribute title.



25
26
27
# File 'lib/tableview/view_handler.rb', line 25

def title
  @title
end

Instance Method Details

#body(opts = {}, &block) ⇒ Object



73
74
75
# File 'lib/tableview/view_handler.rb', line 73

def body(opts = {}, &block)
  self.part Body, opts, &block
end

#column(title, opts = {}, header_opts = {}, row_opts = {}, &block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tableview/view_handler.rb', line 85

def column(title, opts = {}, header_opts = {}, row_opts = {}, &block)
  if title.is_a? Symbol
    method = title unless block_given?
    title = I18n.translate("activerecord.attributes.#{@collection.klass.name.downcase}.#{title}")
  end
  if block_given?
    proc = block#lambda { |val| block.call(val) }
  else
    proc = lambda { |val| val.send(method) }
  end
  
  @headers << Cell.new(title, opts.merge(header_opts))
  @procs << proc
  @column_based = true
end

#columns(*titles) ⇒ Object



101
102
103
104
105
# File 'lib/tableview/view_handler.rb', line 101

def columns(*titles)
  titles.each do |title|
    self.column title
  end
end

#config(opts) ⇒ Object



38
39
40
# File 'lib/tableview/view_handler.rb', line 38

def config(opts)
  self.options = opts
end

#create_table!Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/tableview/view_handler.rb', line 126

def create_table!
  return unless @column_based
  return if @table_generated
  header_row do |row|
    row.cells = @headers
  end
  @collection.each do |el|
    row do |r|
      @procs.each do |proc|
        r.cell proc.call(el)
      end
    end
  end
  @subtables << self
end


63
64
65
# File 'lib/tableview/view_handler.rb', line 63

def footer(opts = {}, &block)
  self.part Footer, opts, &block
end


67
68
69
70
71
# File 'lib/tableview/view_handler.rb', line 67

def footer_row(opts = {}, &block)
  self.footer do |t|
    t.row opts, &block
  end
end

#generate_subtable_for(subcollection, title = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/tableview/view_handler.rb', line 107

def generate_subtable_for(subcollection, title = nil)
  t = Table.new
  t.title = title
  t.header_row do |row|
    row.cells = @headers
  end
  t.body do |b|
    subcollection.each do |el|
      b.row do |r|
        @procs.each do |proc|
          r.cell proc.call(el)
        end
      end
    end
  end # body
  @subtables << t
  @table_generated = true
end

#header(opts = {}, &block) ⇒ Object



53
54
55
# File 'lib/tableview/view_handler.rb', line 53

def header(opts = {}, &block)
  self.part Header, opts, &block
end

#header_row(opts = {}, &block) ⇒ Object



57
58
59
60
61
# File 'lib/tableview/view_handler.rb', line 57

def header_row(opts = {}, &block)
  self.header do |table|
    table.row opts, &block
  end
end

#part(obj, opts) {|@current_part| ... } ⇒ Object

Yields:

  • (@current_part)


42
43
44
45
46
47
48
49
50
51
# File 'lib/tableview/view_handler.rb', line 42

def part(obj, opts, &block)
  prev = @current_part
  added = @added
  @added = true
  @current_part = obj.new opts
  yield @current_part
  self.parts << @current_part
  @current_part = prev
  @added = added
end

#row(opts = {}, &block) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/tableview/view_handler.rb', line 77

def row(opts = {}, &block)
  unless @added
    @parts << @current_part
    @added = true
  end
  @current_part.send :row, opts, &block
end

#table_for(s) ⇒ Object



34
35
36
# File 'lib/tableview/view_handler.rb', line 34

def table_for(s)
  @collection = s
end