Module: ModelTableizer

Defined in:
lib/model_tableizer.rb,
lib/model_tableizer/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Instance Method Details

#column(name, params = {}, &blk) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/model_tableizer.rb', line 11

def column(name, params={}, &blk)
  content = ''
  if block_given?
    content = blk.call
  else
    if @item.respond_to?(name)
      content = @item.send(name) 
    end
  end

  tmp="<td name = '#{name}'"
  tmp += params.attr_str
  tmp += ">#{content}</td>" 
  @columns << tmp
  unless @header.include?(name)
    @header << name 
    @header_params[@header.length-1] = params.attr_str
  end
end

#table(items = [], params = {}, &blk) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/model_tableizer.rb', line 31

def table(items=[], params={}, &blk)
  params[:id] ||= Time.now
  row_html = ""
  if block_given?
    @header = []
    @header_params = []
    html = "<table id='#{params[:id]}'>"
    items.each_with_index { |item, ct|
      row_html += "<tr>"
      @columns = []
      @item = item
      instance_variable_set("@#{item.class.to_s.downcase}", item)
      define_me(item.class.to_s.downcase) { item }
      blk.call
      @columns.each { |col| row_html += col }
      row_html += "</tr>"
    }
    head = "<thead>"
    @header.each_with_index { |col, ct| 
      head += "<th class='#{col}' #{@header_params[ct]} >#{col.to_s.to_camel}</th>" }
      head += "</thead>"

    "#{html}#{head}#{row_html}</table>"

  end
end