Class: TableHelper::CollectionTable
- Inherits:
-
HtmlElement
- Object
- HtmlElement
- TableHelper::CollectionTable
- Defined in:
- lib/table_helper/collection_table.rb
Overview
Represents a table that displays data for multiple objects within a collection.
Constant Summary collapse
- @@collection_class =
'ui-collection'
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
The actual collection of objects being rendered in the table’s body.
-
#klass ⇒ Object
readonly
The class of objects that will be rendered in the table.
-
#object_name ⇒ Object
The name of the objects in the collection.
-
#rows ⇒ Object
readonly
The body of rows that will be generated for each object in the collection.
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Will any rows be generated in the table’s body?.
-
#footer(*args) ⇒ Object
Creates a new footer cell with the given name.
-
#header(*args) ⇒ Object
Creates a new header cell with the given name.
-
#initialize(collection, klass = nil, html_options = {}) {|_self| ... } ⇒ CollectionTable
constructor
Creates a new table based on the objects within the given collection.
Methods inherited from HtmlElement
Constructor Details
#initialize(collection, klass = nil, html_options = {}) {|_self| ... } ⇒ CollectionTable
Creates a new table based on the objects within the given collection
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/table_helper/collection_table.rb', line 51 def initialize(collection, klass = nil, = {}) #:nodoc: , klass = klass, nil if klass.is_a?(Hash) super() @collection = collection @klass = klass || default_class @object_name = @klass ? @klass.name.split('::').last.underscore : nil @html_options.reverse_merge!(:cellspacing => '0', :cellpadding => '0') self[:class] = "#{self[:class]} #{@object_name.pluralize}".strip if @object_name self[:class] = "#{self[:class]} #{collection_class}".strip # Build actual content @header = Header.new(self) @rows = Body.new(self) @footer = Footer.new(self) yield self if block_given? end |
Instance Attribute Details
#collection ⇒ Object (readonly)
The actual collection of objects being rendered in the table’s body. This collection will also be used to help automatically determine what the class is.
28 29 30 |
# File 'lib/table_helper/collection_table.rb', line 28 def collection @collection end |
#klass ⇒ Object (readonly)
The class of objects that will be rendered in the table
15 16 17 |
# File 'lib/table_helper/collection_table.rb', line 15 def klass @klass end |
#object_name ⇒ Object
The name of the objects in the collection. By default, this is the underscored name of the class of objects being rendered. This value is used for generates parts of the css classes in the table, such as rows and cells.
For example, if the class is Post, then the object name will be “post”.
23 24 25 |
# File 'lib/table_helper/collection_table.rb', line 23 def object_name @object_name end |
#rows ⇒ Object (readonly)
The body of rows that will be generated for each object in the collection. The actual content for each row and the html options can be configured as well.
See TableHelper::Body for more information.
Examples
# Defining rows
t.rows.each do |row, post, index|
row.category post.category.name
row. post..name
...
row[:style] = 'margin-top: 5px;'
end
# Customizing html options
t.rows[:class] = 'pretty'
48 49 50 |
# File 'lib/table_helper/collection_table.rb', line 48 def rows @rows end |
Instance Method Details
#empty? ⇒ Boolean
Will any rows be generated in the table’s body?
72 73 74 |
# File 'lib/table_helper/collection_table.rb', line 72 def empty? collection.empty? end |
#footer(*args) ⇒ Object
Creates a new footer cell with the given name. Footers must be defined in the order in which they will be rendered.
The actual caption and html options for the footer can be configured as well. The caption determines what will be displayed in each cell.
Examples
# Adding footers
t. :total # => <td class="post-total"></th>
t. :total, 5 # => <td class="post-total">5</th>
t. :total, :class => 'pretty' # => <td class="post-total pretty"></th>
t. :total, 5, :class => 'pretty' # => <td class="post-total pretty">5</th>
# Customizing html options
t.[:class] = 'pretty'
112 113 114 |
# File 'lib/table_helper/collection_table.rb', line 112 def (*args) args.empty? ? @footer : @footer.cell(*args) end |
#header(*args) ⇒ Object
Creates a new header cell with the given name. Headers must be defined in the order in which they will be rendered.
The actual caption and html options for the header can be configured as well. The caption determines what will be displayed in each cell.
Examples
# Adding headers
t.header :title # => <th class="post-title">Title</th>
t.header :title, 'The Title' # => <th class="post-title">The Title</th>
t.header :title, :class => 'pretty' # => <th class="post-title pretty">Title</th>
t.header :title, 'The Title', :class => 'pretty' # => <th class="post-title pretty">The Title</th>
# Customizing html options
t.header[:class] = 'pretty'
92 93 94 |
# File 'lib/table_helper/collection_table.rb', line 92 def header(*args) args.empty? ? @header : @header.column(*args) end |