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 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_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
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 = 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
58 59 60 |
# File 'lib/table_helper/header.rb', line 58 def column_names row.cell_names end |
#columns ⇒ Object
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 |
#html ⇒ Object
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.dup [:style] = 'display: none;' if table.empty? && hide_when_empty content_tag(tag_name, content, ) end |