Class: TableHelper::Header
- Inherits:
-
HtmlElement
- Object
- HtmlElement
- TableHelper::Header
- 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
-
#hide_when_empty ⇒ Object
Whether or not the header should be hidden when the collection is empty.
-
#row ⇒ Object
readonly
The actual header row.
-
#table ⇒ Object
readonly
The table this header is a part of.
Instance Method Summary collapse
-
#clear ⇒ Object
Clears all of the current columns from the header.
-
#column(*names) ⇒ Object
Creates one or more to columns in the header.
-
#column_names ⇒ Object
Gets the names of all of the columns being displayed in the table.
-
#columns ⇒ Object
The current columns in this header, in the order in which they will be built.
-
#html ⇒ Object
Creates and returns the generated html for the header.
-
#initialize(table) ⇒ Header
constructor
Creates a new header for the given table.
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_empty ⇒ Object
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 |
#row ⇒ Object (readonly)
The actual header row
11 12 13 |
# File 'lib/table_helper/header.rb', line 11 def row @row end |
#table ⇒ Object (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
#clear ⇒ Object
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 = names.last.is_a?(Hash) ? names.pop : {} content = names.last.is_a?(String) ? names.pop : nil args = [content, ].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_names ⇒ Object
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 |
#columns ⇒ Object
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 |
#html ⇒ Object
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 = .dup [:style] = 'display: none;' if table.empty? && hide_when_empty content_tag(tag_name, content, ) end |