table builder: add default html ids and css classes.

* Add default IDs for <table> tags (ex: <table id="artists-table">).
* Add default CSS classes for <th> and <td> tags (ex: <td class="name-column">).
This commit is contained in:
evazion
2020-01-09 17:29:10 -06:00
parent d0d3da08e5
commit 2cf0e7dcb5

View File

@@ -10,6 +10,12 @@ class TableBuilder
@name = name || attribute
@name = @name.to_s.titleize unless @name.kind_of?(String)
if @name.present?
column_class = "#{@name.parameterize.dasherize}-column"
@header_attributes[:class] = "#{column_class} #{@header_attributes[:class]}".strip
@body_attributes[:class] = "#{column_class} #{@body_attributes[:class]}".strip
end
end
def value(item, i, j)
@@ -30,6 +36,11 @@ class TableBuilder
@items = items
@columns = []
@table_attributes = { class: "striped", **table_attributes }
if items.respond_to?(:model_name)
@table_attributes[:id] ||= "#{items.model_name.plural.dasherize}-table"
end
yield self if block_given?
end