Files
danbooru/app/logical/table_builder.rb
evazion 5c913d8ad1 table builder: fix various incorrect css classes.
Change calling convention to explicitly indicate whether the attributes
are for the <th> element or the <td> element. Fixes various cases where
the two were mixed up.

* Fix .col-expand classes not being set correctly on the /post_versions,
  /pool_versions, and /notes pages.

* Fix .updater and .updated-at classes not being set correctly on the
  /forum_topics page.

* Fix the name param being ignored (noticeable in the post count field
  on the /tags page).

* Don't pass empty string when column has no name.
2020-01-07 02:40:59 -06:00

49 lines
1.2 KiB
Ruby

class TableBuilder
class Column
attr_reader :attribute, :name, :block, :header_attributes, :body_attributes
def initialize(attribute = nil, th: {}, td: {}, width: nil, name: nil, &block)
@attribute = attribute
@header_attributes = { width: width, **th }
@body_attributes = td
@block = block
@name = name || attribute
@name = @name.to_s.titleize unless @name.kind_of?(String)
end
def value(item, i, j)
if block.present?
block.call(item, i, j, self)
nil
elsif attribute.kind_of?(Symbol)
item.send(attribute)
else
""
end
end
end
attr_reader :columns, :table_attributes, :items
def initialize(items, **table_attributes)
@items = items
@columns = []
@table_attributes = { class: "striped", **table_attributes }
yield self if block_given?
end
def column(*options, &block)
@columns << Column.new(*options, &block)
end
def all_row_attributes(item, i)
return {} if !item.is_a?(ApplicationRecord)
{
id: "#{item.model_name.singular.dasherize}-#{item.id}",
**ApplicationController.helpers.data_attributes_for(item, "data", item.html_data_attributes)
}
end
end