Class: StompBase::Models::DataTableComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/stomp_base/models/data_table_component.rb

Constant Summary collapse

TYPE_COLORS =
{
  string: "bg-blue-100 text-blue-700",
  text: "bg-blue-100 text-blue-700",
  integer: "bg-green-100 text-green-700",
  bigint: "bg-green-100 text-green-700",
  decimal: "bg-purple-100 text-purple-700",
  float: "bg-purple-100 text-purple-700",
  boolean: "bg-yellow-100 text-yellow-700",
  datetime: "bg-pink-100 text-pink-700",
  timestamp: "bg-pink-100 text-pink-700",
  date: "bg-indigo-100 text-indigo-700",
  default: "bg-gray-100 text-gray-600"
}.freeze

Instance Method Summary collapse

Methods inherited from BaseComponent

#call, #render?

Constructor Details

#initialize(columns:, records:, model_class:, current_page: nil, total_pages: nil) ⇒ DataTableComponent

Returns a new instance of DataTableComponent.



20
21
22
23
24
25
26
27
# File 'app/components/stomp_base/models/data_table_component.rb', line 20

def initialize(columns:, records:, model_class:, current_page: nil, total_pages: nil)
  super()
  @columns = columns
  @records = records
  @model_class = model_class
  @current_page = current_page
  @total_pages = total_pages
end

Instance Method Details

#column_type(column_name) ⇒ Object



50
51
52
53
# File 'app/components/stomp_base/models/data_table_component.rb', line 50

def column_type(column_name)
  column = find_column(column_name)
  column&.type ? column.type.to_s.upcase : "UNKNOWN"
end

#column_type_badge_color(column_name) ⇒ Object



43
44
45
46
47
48
# File 'app/components/stomp_base/models/data_table_component.rb', line 43

def column_type_badge_color(column_name)
  column = find_column(column_name)
  return TYPE_COLORS[:default] unless column

  TYPE_COLORS[column.type] || TYPE_COLORS[:default]
end

#format_cell_value(value) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'app/components/stomp_base/models/data_table_component.rb', line 33

def format_cell_value(value)
  return (:span, "NULL", style: "color: #6c757d; font-style: italic;") if value.nil?

  if value.is_a?(String) && value.length > 50
    (:span, "#{value[0..50]}...", title: value)
  else
    value.to_s
  end
end

#pagination?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'app/components/stomp_base/models/data_table_component.rb', line 29

def pagination?
  @total_pages && @total_pages > 1
end