Class: BetterUi::Table::ColumnComponent

Inherits:
ApplicationComponent show all
Defined in:
app/components/better_ui/table/column_component.rb

Overview

A data-holder component for defining table columns in collection mode.

ColumnComponent stores column configuration (key, label, alignment, formatter) and produces no rendered output. The parent TableComponent reads these configurations to build the table structure.

Examples:

Column with key (auto-extracts value from collection items)

<% t.with_column(key: :name, label: "Name") %>

Column with custom formatter

<% t.with_column(key: :role, label: "Role") { |user| user.role.humanize } %>

Column with no key (header-only, relies on formatter block)

<% t.with_column(label: "Actions", align: :right) { |user| link_to("Edit", user) } %>

Constant Summary collapse

ALIGNMENTS =
%i[left center right].freeze
SORT_DIRECTIONS =
%i[asc desc].freeze

Constants inherited from ApplicationComponent

ApplicationComponent::SHADOWS, ApplicationComponent::VARIANTS, ApplicationComponent::VARIANT_BODY_DIVIDE, ApplicationComponent::VARIANT_DIVIDE, ApplicationComponent::VARIANT_HEADER_BG, ApplicationComponent::VARIANT_HEADER_TEXT, ApplicationComponent::VARIANT_HIGHLIGHTED, ApplicationComponent::VARIANT_HOVERABLE, ApplicationComponent::VARIANT_RING, ApplicationComponent::VARIANT_SORT_ICON, ApplicationComponent::VARIANT_STRIPED

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key: nil, label: nil, align: :left, header_classes: nil, cell_classes: nil, sortable: false, sorted: false, sort_direction: :asc, sort_url: nil, sort_html: {}, &formatter) ⇒ ColumnComponent

Returns a new instance of ColumnComponent.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/components/better_ui/table/column_component.rb', line 26

def initialize(key: nil, label: nil, align: :left, header_classes: nil, cell_classes: nil,
               sortable: false, sorted: false, sort_direction: :asc,
               sort_url: nil, sort_html: {}, &formatter)
  @key = key
  @label = label
  @align = validate_align(align)
  @header_classes = header_classes
  @cell_classes = cell_classes
  @sortable = sortable
  @sorted = sorted
  @sort_direction = validate_sort_direction(sort_direction)
  @sort_url = sort_url
  @sort_html = sort_html || {}
  @formatter = formatter
end

Instance Attribute Details

#alignObject (readonly)

Returns the value of attribute align.



23
24
25
# File 'app/components/better_ui/table/column_component.rb', line 23

def align
  @align
end

#cell_classesObject (readonly)

Returns the value of attribute cell_classes.



23
24
25
# File 'app/components/better_ui/table/column_component.rb', line 23

def cell_classes
  @cell_classes
end

#formatterObject (readonly)

Returns the value of attribute formatter.



23
24
25
# File 'app/components/better_ui/table/column_component.rb', line 23

def formatter
  @formatter
end

#header_classesObject (readonly)

Returns the value of attribute header_classes.



23
24
25
# File 'app/components/better_ui/table/column_component.rb', line 23

def header_classes
  @header_classes
end

#keyObject (readonly)

Returns the value of attribute key.



23
24
25
# File 'app/components/better_ui/table/column_component.rb', line 23

def key
  @key
end

#labelObject (readonly)

Returns the value of attribute label.



23
24
25
# File 'app/components/better_ui/table/column_component.rb', line 23

def label
  @label
end

#sort_directionObject (readonly)

Returns the value of attribute sort_direction.



23
24
25
# File 'app/components/better_ui/table/column_component.rb', line 23

def sort_direction
  @sort_direction
end

#sort_htmlObject (readonly)

Returns the value of attribute sort_html.



23
24
25
# File 'app/components/better_ui/table/column_component.rb', line 23

def sort_html
  @sort_html
end

#sort_urlObject (readonly)

Returns the value of attribute sort_url.



23
24
25
# File 'app/components/better_ui/table/column_component.rb', line 23

def sort_url
  @sort_url
end

#sortableObject (readonly)

Returns the value of attribute sortable.



23
24
25
# File 'app/components/better_ui/table/column_component.rb', line 23

def sortable
  @sortable
end

#sortedObject (readonly)

Returns the value of attribute sorted.



23
24
25
# File 'app/components/better_ui/table/column_component.rb', line 23

def sorted
  @sorted
end

Instance Method Details

#callObject



42
43
44
# File 'app/components/better_ui/table/column_component.rb', line 42

def call
  ""
end

#display_labelObject



46
47
48
# File 'app/components/better_ui/table/column_component.rb', line 46

def display_label
  @label || @key&.to_s&.humanize || ""
end

#value_for(item) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'app/components/better_ui/table/column_component.rb', line 50

def value_for(item)
  if @formatter
    @formatter.call(item)
  elsif @key
    item.respond_to?(@key) ? item.public_send(@key) : item[@key]
  else
    ""
  end
end