Class: Panda::Core::Admin::TableComponent

Inherits:
Base
  • Object
show all
Defined in:
app/components/panda/core/admin/table_component.rb

Constant Summary

Constants inherited from Base

Base::TAILWIND_MERGER

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(term: "", rows: [], icon: "", responsive: true, sort: nil, sort_direction: nil, **attrs, &block) ⇒ TableComponent

Returns a new instance of TableComponent.



9
10
11
12
13
14
15
16
17
18
19
# File 'app/components/panda/core/admin/table_component.rb', line 9

def initialize(term: "", rows: [], icon: "", responsive: true, sort: nil, sort_direction: nil, **attrs, &block)
  @term = term
  @rows = rows
  @icon = icon
  @responsive = responsive
  @sort = sort&.to_s
  @sort_direction = (sort_direction&.to_s == "desc") ? "desc" : "asc"
  @columns = []
  @setup_block = block
  super(**attrs)
end

Instance Attribute Details

#iconObject (readonly)

Returns the value of attribute icon.



7
8
9
# File 'app/components/panda/core/admin/table_component.rb', line 7

def icon
  @icon
end

#rowsObject (readonly)

Returns the value of attribute rows.



7
8
9
# File 'app/components/panda/core/admin/table_component.rb', line 7

def rows
  @rows
end

#sortObject (readonly)

Returns the value of attribute sort.



7
8
9
# File 'app/components/panda/core/admin/table_component.rb', line 7

def sort
  @sort
end

#sort_directionObject (readonly)

Returns the value of attribute sort_direction.



7
8
9
# File 'app/components/panda/core/admin/table_component.rb', line 7

def sort_direction
  @sort_direction
end

#termObject (readonly)

Returns the value of attribute term.



7
8
9
# File 'app/components/panda/core/admin/table_component.rb', line 7

def term
  @term
end

Instance Method Details

#column(label, width: nil, sortable: false, sort_key: nil, &cell_block) ⇒ Object



25
26
27
28
# File 'app/components/panda/core/admin/table_component.rb', line 25

def column(label, width: nil, sortable: false, sort_key: nil, &cell_block)
  @columns << Column.new(label, width, sortable: sortable, sort_key: sort_key, &cell_block)
  self  # Allow chaining
end

#columnsObject

Lazy accessor that ensures columns are registered before returning them. Supports two patterns:

  1. Block passed to new() - executed here (for tests)
  2. Block passed to render() - executed via content (for ERB templates)


56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/components/panda/core/admin/table_component.rb', line 56

def columns
  unless @columns_registered
    if @setup_block
      # Block was passed to new() - execute it
      @setup_block.call(self)
    else
      # Block was passed to render() - execute via content
      content
    end
    @columns_registered = true
  end
  @columns
end

#render_cell_content(row, cell_block) ⇒ Object



30
31
32
33
34
35
# File 'app/components/panda/core/admin/table_component.rb', line 30

def render_cell_content(row, cell_block)
  # Use capture to properly handle ERB blocks that output to the template buffer.
  # Directly calling cell_block.call(row) causes double-rendering because ERB blocks
  # both output to the buffer AND return their last expression.
  helpers.capture(row, &cell_block)
end

#responsive?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'app/components/panda/core/admin/table_component.rb', line 21

def responsive?
  @responsive
end

#sort_indicator_for(column) ⇒ Object



46
47
48
49
50
# File 'app/components/panda/core/admin/table_component.rb', line 46

def sort_indicator_for(column)
  return unless sort == column.sort_key

  (sort_direction == "asc") ? "" : ""
end

#sort_url_for(column) ⇒ Object



37
38
39
40
41
42
43
44
# File 'app/components/panda/core/admin/table_component.rb', line 37

def sort_url_for(column)
  return unless column.sortable?

  new_direction = (sort == column.sort_key && sort_direction == "asc") ? "desc" : "asc"
  request = helpers.request
  query_params = request.query_parameters.merge("sort" => column.sort_key, "direction" => new_direction)
  "#{request.path}?#{query_params.to_query}"
end