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
# 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
  column(*klass.column_names.map(&:to_sym)) if klass && klass.respond_to?(:column_names)
  
  @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



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

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.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/table_helper/header.rb', line 60

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



48
49
50
# File 'lib/table_helper/header.rb', line 48

def column_names
  row.cell_names
end

#columnsObject

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



43
44
45
# File 'lib/table_helper/header.rb', line 43

def columns
  row.cells
end

#htmlObject

Creates and returns the generated html for the header



83
84
85
86
87
88
# File 'lib/table_helper/header.rb', line 83

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