Class: BootWheel::Table

Inherits:
Internals::Component show all
Defined in:
lib/boot_wheel/table.rb

Constant Summary collapse

TEMPLATE =
t("table.erb")

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Internals::Component

#helpers, #renderer, t, #to_html, #x

Constructor Details

#initialize(items:, style_props: [], fields: items.first.keys, **params) ⇒ Table

Returns a new instance of Table.



7
8
9
10
11
12
# File 'lib/boot_wheel/table.rb', line 7

def initialize(items:, style_props: [], fields: items.first.keys, **params)
  @items       = items
  @style_props = style_props
  @fields      = normalize_fields(fields)
  @params      = params
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



14
15
16
# File 'lib/boot_wheel/table.rb', line 14

def fields
  @fields
end

#itemsObject (readonly)

Returns the value of attribute items.



14
15
16
# File 'lib/boot_wheel/table.rb', line 14

def items
  @items
end

#paramsObject (readonly)

Returns the value of attribute params.



14
15
16
# File 'lib/boot_wheel/table.rb', line 14

def params
  @params
end

Instance Method Details

#applied_classesObject



16
17
18
# File 'lib/boot_wheel/table.rb', line 16

def applied_classes
  helpers.class_names(["table"] + @style_props.map { |e| "table-#{e}"} + (params[:table_class] || []))
end

#field_with_key(key) ⇒ Object



28
29
30
# File 'lib/boot_wheel/table.rb', line 28

def field_with_key(key)
  @fields.find { |e| e[:key] == key }
end

#normalize_fields(fields) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/boot_wheel/table.rb', line 20

def normalize_fields(fields)
  item_keys = @items.first.keys

  fields.map.with_index do |e,i|
    Hash === e ? e : { key: item_keys[i], label: e }
  end
end