Class: SuperLookingList

Inherits:
Object show all
Defined in:
lib/smklib/super_looking_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, list, columns, speed, table_width, options = {}) ⇒ SuperLookingList

Returns a new instance of SuperLookingList.



7
8
9
# File 'lib/smklib/super_looking_list.rb', line 7

def initialize(model, list, columns, speed, table_width, options = {})
  @model, @list, @columns, @speed, @table_width, @options = model, list, columns, speed, table_width, options
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



5
6
7
# File 'lib/smklib/super_looking_list.rb', line 5

def columns
  @columns
end

#listObject

Returns the value of attribute list.



5
6
7
# File 'lib/smklib/super_looking_list.rb', line 5

def list
  @list
end

#modelObject

Returns the value of attribute model.



5
6
7
# File 'lib/smklib/super_looking_list.rb', line 5

def model
  @model
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/smklib/super_looking_list.rb', line 5

def options
  @options
end

#speedObject

Returns the value of attribute speed.



5
6
7
# File 'lib/smklib/super_looking_list.rb', line 5

def speed
  @speed
end

#table_widthObject

Returns the value of attribute table_width.



5
6
7
# File 'lib/smklib/super_looking_list.rb', line 5

def table_width
  @table_width
end

Instance Method Details

#format_column(item, column) ⇒ Object



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

def format_column(item, column)
  c = column[:field]
  val = item.send(c)
  if column[:format]
    val = column[:format].call(item, c)
  else
    if item.respond_to? :format_for_display
      STDERR.puts "Warning: {model}::format_for_display is deprecated (#{item.to_s})"
      val = item.format_for_display(c)
    end
  end
  val = "n/a" if val.to_s.empty?
  return val.to_s
end

#render(xml = nil, &block) ⇒ Object



26
27
28
29
30
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
57
58
59
60
# File 'lib/smklib/super_looking_list.rb', line 26

def render(xml = nil, &block)
  xml ||= Builder::XmlMarkup.new(:indent => 2)
  xml.div do
    xml.tight_table("border" => "1", "width" => table_width.to_s, "cellpadding" => "2", "class" => "SuperTableList #{@model.capitalize}") do
      xml.tr do
        columns.each { |c|
          xml.th {
            if options[:header_format]
              options[:header_format].call(xml, c)
            else
              xml << c[:label]
            end
          }
        }
      end
      idx = 0
      for item in list
        style = ((idx % 2 == 0) ? "even" : "odd") + " type_#{item.class}"
        xml.tr("id" => "#{model}_row_#{idx}", "onclick" => "proceed_on_click(this, #{speed})", "onmouseover" => "show_details(this);", "class" => style, "onmouseout" => "hide_last_shown(this);") do
          columns.each do |col|
            xml.td("class" => "field_#{col[:field]}") { xml << format_column(item, col) }
          end
        end
        xml.tr("class" => "current", "id" => "#{model}_row_#{idx}_details", "style" => "display: none;") do
          xml.td("colspan" => columns.size) do
            xml.div("style" => "overflow: auto;", "id" => "#{model}_row_#{idx}_block_details") do
              yield(xml, item)
            end
          end
        end
        idx += 1
      end
    end
  end
end