Class: TableHelper::Header

Inherits:
HtmlElement show all
Defined in:
lib/table_helper/header.rb

Overview

Represents the header of the table. In HTML, you can think of this as the <thead> tag of the table.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ Header

Creates a new header for the given table.

If the class is known, then the header will be pre-filled with the columns defined in that class (assuming it’s an ActiveRecord class).



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/table_helper/header.rb', line 24

def initialize(table)
  super()
  
  @table = table
  @row = Row.new(self)
  @hide_when_empty = true
  
  # If we know what class the objects in the collection are and we can
  # figure out what columns are defined in that class, then we can
  # pre-fill the header with those columns so that the user doesn't
  # have to
  klass = table.klass
  if klass && klass.respond_to?(:column_names)
    if !table.empty? && klass < ActiveRecord::Base
      # Make sure only the attributes that have been loaded are used
      column_names = table.collection.first.attributes.keys
    else
      # Assume all attributes are loaded
      column_names = klass.column_names
    end
    
    column(*column_names.map(&:to_sym))
  end
  
  @customized = false
end

Instance Attribute Details

#hide_when_emptyObject

Whether or not the header should be hidden when the collection is empty. Default is true.



15
16
17
# File 'lib/table_helper/header.rb', line 15

def hide_when_empty
  @hide_when_empty
end

#rowObject (readonly)

The actual header row



11
12
13
# File 'lib/table_helper/header.rb', line 11

def row
  @row
end

#tableObject (readonly)

The table this header is a part of



8
9
10
# File 'lib/table_helper/header.rb', line 8

def table
  @table
end

Instance Method Details

#clearObject

Clears all of the current columns from the header



63
64
65
# File 'lib/table_helper/header.rb', line 63

def clear
  row.clear
end

#column(*names) ⇒ Object

Creates one or more to columns in the header. This will clear any pre-existing columns if it is being customized for the first time after it was initially created.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/table_helper/header.rb', line 70

def column(*names)
  # Clear the header row if this is being customized by the user
  unless @customized
    @customized = true
    clear
  end
  
  # Extract configuration
  options = names.last.is_a?(Hash) ? names.pop : {}
  content = names.last.is_a?(String) ? names.pop : nil
  args = [content, options].compact
  
  names.collect! do |name|
    column = row.cell(name, *args)
    column.content_type = :header
    column[:scope] ||= 'col'
    column
  end
  
  names.length == 1 ? names.first : names
end

#column_namesObject

Gets the names of all of the columns being displayed in the table



58
59
60
# File 'lib/table_helper/header.rb', line 58

def column_names
  row.cell_names
end

#columnsObject

The current columns in this header, in the order in which they will be built



53
54
55
# File 'lib/table_helper/header.rb', line 53

def columns
  row.cells
end

#htmlObject

Creates and returns the generated html for the header



93
94
95
96
97
98
# File 'lib/table_helper/header.rb', line 93

def html
  html_options = @html_options.dup
  html_options[:style] = 'display: none;' if table.empty? && hide_when_empty
  
  (tag_name, content, html_options)
end