Module: MaquinaComponents::TableHelper

Defined in:
app/helpers/maquina_components/table_helper.rb

Overview

Table Component Helper

Provides a simple helper for rendering basic tables from collections. For complex tables, use the partials directly for full control.

Examples:

Basic usage with collection

<%= simple_table @invoices,
      columns: [
        { key: :number, label: "Invoice" },
        { key: :status, label: "Status" },
        { key: :amount, label: "Amount", align: :right }
      ] %>

With caption and bordered variant

<%= simple_table @users,
      columns: [
        { key: :name, label: "Name" },
        { key: :email, label: "Email" },
        { key: :role, label: "Role" }
      ],
      caption: "Active users",
      variant: :bordered %>

Instance Method Summary collapse

Instance Method Details

#simple_table(collection, columns:, caption: nil, variant: nil, table_variant: nil, empty_message: "No data available", row_id: nil, **html_options) ⇒ String

Render a simple table from a collection

Parameters:

  • collection (Array, ActiveRecord::Relation)

    The collection to render

  • columns (Array<Hash>)

    Column definitions with :key, :label, and optional :align

  • caption (String, nil) (defaults to: nil)

    Optional table caption

  • variant (Symbol, nil) (defaults to: nil)

    Container variant (:bordered)

  • table_variant (Symbol, nil) (defaults to: nil)

    Table variant (:striped)

  • empty_message (String) (defaults to: "No data available")

    Message to show when collection is empty

  • row_id (Symbol, nil) (defaults to: nil)

    Method to call for row ID (e.g., :id)

  • html_options (Hash)

    Additional HTML options for the table

Returns:

  • (String)

    Rendered HTML



39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/helpers/maquina_components/table_helper.rb', line 39

def simple_table(collection, columns:, caption: nil, variant: nil, table_variant: nil, empty_message: "No data available", row_id: nil, **html_options)
  render partial: "components/simple_table", locals: {
    collection: collection,
    columns: columns,
    caption: caption,
    variant: variant,
    table_variant: table_variant,
    empty_message: empty_message,
    row_id: row_id,
    html_options: html_options
  }
end

#table_alignment_class(align) ⇒ String?

Convert alignment symbol to CSS class

Parameters:

  • align (Symbol, nil)

    Alignment (:left, :center, :right)

Returns:

  • (String, nil)

    CSS class name



136
137
138
139
140
141
# File 'app/helpers/maquina_components/table_helper.rb', line 136

def table_alignment_class(align)
  case align&.to_sym
  when :right then "text-right"
  when :center then "text-center"
  end
end

#table_body_data_attrsHash

Generate data attributes for table body

Returns:

  • (Hash)

    Data attributes hash



116
117
118
# File 'app/helpers/maquina_components/table_helper.rb', line 116

def table_body_data_attrs
  {data: {table_part: "body"}}
end

#table_caption_data_attrsHash

Generate data attributes for table caption

Returns:

  • (Hash)

    Data attributes hash



128
129
130
# File 'app/helpers/maquina_components/table_helper.rb', line 128

def table_caption_data_attrs
  {data: {table_part: "caption"}}
end

#table_cell_data_attrs(empty: false) ⇒ Hash

Generate data attributes for table cell

Parameters:

  • empty (Boolean) (defaults to: false)

    Whether this is an empty state cell

Returns:

  • (Hash)

    Data attributes hash



108
109
110
111
112
# File 'app/helpers/maquina_components/table_helper.rb', line 108

def table_cell_data_attrs(empty: false)
  attrs = {data: {table_part: "cell"}}
  attrs[:data][:empty] = "true" if empty
  attrs
end

#table_container_data_attrs(variant: nil) ⇒ Hash

Generate data attributes for table container

Parameters:

  • variant (Symbol, nil) (defaults to: nil)

    Container variant (:bordered)

Returns:

  • (Hash)

    Data attributes hash



72
73
74
75
76
# File 'app/helpers/maquina_components/table_helper.rb', line 72

def table_container_data_attrs(variant: nil)
  attrs = {data: {table_part: "container"}}
  attrs[:data][:variant] = variant.to_s if variant
  attrs
end

#table_data_attrs(variant: nil) ⇒ Hash

Generate data attributes for table elements Useful when composing tables with other Rails helpers

Examples:

Using with content_tag

<%= content_tag :table, **table_data_attrs do %>
  ...
<% end %>

Parameters:

  • variant (Symbol, nil) (defaults to: nil)

    Table variant (:striped)

Returns:

  • (Hash)

    Data attributes hash



62
63
64
65
66
# File 'app/helpers/maquina_components/table_helper.rb', line 62

def table_data_attrs(variant: nil)
  attrs = {data: {component: "table"}}
  attrs[:data][:variant] = variant.to_s if variant
  attrs
end

Generate data attributes for table footer

Returns:

  • (Hash)

    Data attributes hash



122
123
124
# File 'app/helpers/maquina_components/table_helper.rb', line 122

def table_footer_data_attrs
  {data: {table_part: "footer"}}
end

#table_head_data_attrsHash

Generate data attributes for table head cell

Returns:

  • (Hash)

    Data attributes hash



100
101
102
# File 'app/helpers/maquina_components/table_helper.rb', line 100

def table_head_data_attrs
  {data: {table_part: "head"}}
end

#table_header_data_attrs(sticky: false) ⇒ Hash

Generate data attributes for table header

Parameters:

  • sticky (Boolean) (defaults to: false)

    Whether the header is sticky

Returns:

  • (Hash)

    Data attributes hash



92
93
94
95
96
# File 'app/helpers/maquina_components/table_helper.rb', line 92

def table_header_data_attrs(sticky: false)
  attrs = {data: {table_part: "header"}}
  attrs[:data][:sticky] = "true" if sticky
  attrs
end

#table_row_data_attrs(selected: false) ⇒ Hash

Generate data attributes for table row

Parameters:

  • selected (Boolean) (defaults to: false)

    Whether the row is selected

Returns:

  • (Hash)

    Data attributes hash



82
83
84
85
86
# File 'app/helpers/maquina_components/table_helper.rb', line 82

def table_row_data_attrs(selected: false)
  attrs = {data: {table_part: "row"}}
  attrs[:data][:state] = "selected" if selected
  attrs
end