Class: YiffSpace::Utils::TableBuilder
- Inherits:
-
Object
- Object
- YiffSpace::Utils::TableBuilder
- Defined in:
- lib/yiffspace/utils/table_builder.rb
Overview
A helper class for building HTML tables. Used in views.
Defined Under Namespace
Classes: Column
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#row_attributes ⇒ Object
readonly
Returns the value of attribute row_attributes.
-
#table_attributes ⇒ Object
readonly
Returns the value of attribute table_attributes.
Instance Method Summary collapse
-
#all_row_attributes(item, _row) ⇒ Hash
Return the HTML attributes for each <tr> tag.
-
#caption ⇒ Object
Set the table caption.
-
#column(**options) ⇒ Object
Add a column to the table.
-
#initialize(items, tr: {}, **table_attributes) {|table| ... } ⇒ TableBuilder
constructor
Build a table for an array of objects, one object per row.
Constructor Details
#initialize(items, tr: {}, **table_attributes) {|table| ... } ⇒ TableBuilder
Build a table for an array of objects, one object per row.
The <table> tag is automatically given an HTML id of the form ‘name-table`. For example, `posts-table`, `tags-table`.
The <tr> tag is automatically given an HTML id of the form ‘name-id`. For example, `post-1234`, `tag-4567`, etc. Each <tr> tag also gets a set of data attributes for each model; see #html_data_attributes in app/policies.
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/yiffspace/utils/table_builder.rb', line 94 def initialize(items, tr: {}, **table_attributes) @items = items @columns = [] @table_attributes = { class: "striped", **table_attributes } @row_attributes = tr @table_attributes[:id] ||= "#{items.model_name.plural.dasherize}-table" if items.respond_to?(:model_name) yield(self) if block_given? end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
78 79 80 |
# File 'lib/yiffspace/utils/table_builder.rb', line 78 def columns @columns end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
78 79 80 |
# File 'lib/yiffspace/utils/table_builder.rb', line 78 def items @items end |
#row_attributes ⇒ Object (readonly)
Returns the value of attribute row_attributes.
78 79 80 |
# File 'lib/yiffspace/utils/table_builder.rb', line 78 def row_attributes @row_attributes end |
#table_attributes ⇒ Object (readonly)
Returns the value of attribute table_attributes.
78 79 80 |
# File 'lib/yiffspace/utils/table_builder.rb', line 78 def table_attributes @table_attributes end |
Instance Method Details
#all_row_attributes(item, _row) ⇒ Hash
Return the HTML attributes for each <tr> tag.
125 126 127 128 129 130 131 132 133 |
# File 'lib/yiffspace/utils/table_builder.rb', line 125 def all_row_attributes(item, _row) return {} unless item.is_a?(ApplicationRecord) { id: "#{item.model_name.singular.dasherize}-#{item.id}", **row_attributes, **ApplicationController.helpers.data_attributes_for(item), } end |
#caption ⇒ Object
Set the table caption
106 107 108 109 |
# File 'lib/yiffspace/utils/table_builder.rb', line 106 def caption @caption = yield if block_given? @caption end |
#column(**options) ⇒ Object
Add a column to the table.
114 115 116 117 118 119 |
# File 'lib/yiffspace/utils/table_builder.rb', line 114 def column(*, **, &) opt = .extract!(:if, :unless) return if (opt.key?(:if) && !opt[:if]) || (opt.key?(:unless) && opt[:unless]) @columns << Column.new(*, **, &) end |