Class: DataTable::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/data-table/column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description = '', opts = {}, &renderer) ⇒ Column

Returns a new instance of Column.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/data-table/column.rb', line 7

def initialize(name, description = '', opts = {}, &renderer)
  @name = name
  @description = description
  @data_type = opts[:data_type]
  @help_text = opts[:help_text]
  @css_class = opts[:css_class]
  @attributes = opts[:attributes] || {}
  @width = opts[:width]
  @options = opts
  @display = true
  @index = 0
  @renderer = renderer
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



5
6
7
# File 'lib/data-table/column.rb', line 5

def attributes
  @attributes
end

#css_classObject

Returns the value of attribute css_class.



5
6
7
# File 'lib/data-table/column.rb', line 5

def css_class
  @css_class
end

#displayObject

Returns the value of attribute display.



5
6
7
# File 'lib/data-table/column.rb', line 5

def display
  @display
end

#indexObject

Returns the value of attribute index.



5
6
7
# File 'lib/data-table/column.rb', line 5

def index
  @index
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/data-table/column.rb', line 4

def name
  @name
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/data-table/column.rb', line 5

def options
  @options
end

Instance Method Details

#css_class_namesObject



54
55
56
57
58
59
60
# File 'lib/data-table/column.rb', line 54

def css_class_names
  class_names = []
  class_names << @name.to_s
  class_names << @data_type.to_s
  class_names << @css_class
  class_names.compact.join(' ')
end

#custom_attributesObject



50
51
52
# File 'lib/data-table/column.rb', line 50

def custom_attributes
  @attributes.map { |k, v| "#{k}='#{v}'" }.join ' '
end

#render_cell(cell_data, row = nil, row_index = 0, col_index = 0) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/data-table/column.rb', line 21

def render_cell(cell_data, row = nil, row_index = 0, col_index = 0)
  @data_type ||= type(cell_data)

  html = []
  html << if @renderer && row
            case @renderer.arity
            when 1 then @renderer.call(cell_data).to_s
            when 2 then @renderer.call(cell_data, row).to_s
            when 3 then @renderer.call(cell_data, row, row_index).to_s
            when 4 then @renderer.call(cell_data, row, row_index, self).to_s
            when 5 then @renderer.call(cell_data, row, row_index, self, col_index).to_s
            end
          else
            cell_data.to_s
          end

  html << '</td>'
  # Doing this here b/c you can't change @css_class if this is done before the renderer is called
  "<td class='#{css_class_names}' #{custom_attributes}>" + html.join
end

#render_column_headerObject



42
43
44
45
46
47
48
# File 'lib/data-table/column.rb', line 42

def render_column_header
  header = ["<th class='#{css_class_names}' #{custom_attributes}"]
  header << "title='#{@help_text}' " if @help_text
  header << "style='width: #{@width}'" if @width
  header << ">#{@description}</th>"
  header.join
end

#type(data) ⇒ Object

Set a CSS class name based on data type For backward compatability, ‘string’ is renamed to ‘text’ For convenience, all Numerics (e.g. Integer, BigDecimal, etc.) just return ‘numeric’



65
66
67
68
69
70
71
72
73
# File 'lib/data-table/column.rb', line 65

def type(data)
  if data.is_a? Numeric
    'numeric'
  elsif data.is_a? String
    'text'
  else
    data.class.to_s.downcase
  end
end