Refactor CSS to use standard Tailwind-style utility classes instead of ad-hoc rules. This eliminates a lot of single-purpose rules for specific UI elements and standardizes margins to be more consistent throughout the site. Utility classes are defined manually on an as-needed basis instead of importing Tailwind as a whole. Naming conventions mostly follow Tailwind's conventions, otherwise they follow Bootstrap. * https://tailwindcss.com/docs/ * https://getbootstrap.com/docs/5.0/utilities/spacing/
67 lines
1.8 KiB
Ruby
67 lines
1.8 KiB
Ruby
class TableBuilder
|
|
class Column
|
|
attr_reader :attribute, :name, :block, :header_attributes, :body_attributes
|
|
|
|
def initialize(attribute = nil, column: nil, th: {}, td: {}, width: nil, name: nil, &block)
|
|
@attribute = attribute
|
|
@column = column
|
|
@header_attributes = { width: width, **th }
|
|
@body_attributes = td
|
|
@block = block
|
|
|
|
@name = name || attribute
|
|
@name = @name.to_s.titleize unless @name.is_a?(String)
|
|
|
|
if @name.present? || @column.present?
|
|
if @column.present?
|
|
column_class = "#{@column}-column"
|
|
else
|
|
column_class = "#{@name.parameterize.dasherize}-column"
|
|
end
|
|
@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)
|
|
if block.present?
|
|
block.call(item, i, j, self)
|
|
nil
|
|
elsif attribute.is_a?(Symbol)
|
|
item.send(attribute)
|
|
else
|
|
""
|
|
end
|
|
end
|
|
end
|
|
|
|
attr_reader :columns, :table_attributes, :row_attributes, :items
|
|
|
|
def initialize(items, tr: {}, **table_attributes)
|
|
@items = items
|
|
@columns = []
|
|
@table_attributes = { class: "striped", **table_attributes }
|
|
@row_attributes = tr
|
|
|
|
if items.respond_to?(:model_name)
|
|
@table_attributes[:id] ||= "#{items.model_name.plural.dasherize}-table"
|
|
end
|
|
|
|
yield self if block_given?
|
|
end
|
|
|
|
def column(...)
|
|
@columns << Column.new(...)
|
|
end
|
|
|
|
def all_row_attributes(item, i)
|
|
return {} if !item.is_a?(ApplicationRecord)
|
|
|
|
{
|
|
id: "#{item.model_name.singular.dasherize}-#{item.id}",
|
|
**row_attributes,
|
|
**ApplicationController.helpers.data_attributes_for(item, "data", item.html_data_attributes)
|
|
}
|
|
end
|
|
end
|