Class: Katalyst::Tables::CellComponent

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
HtmlAttributes
Defined in:
app/components/katalyst/tables/cell_component.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection:, row:, column:, record:, label:, heading:) ⇒ CellComponent

Returns a new instance of CellComponent.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/components/katalyst/tables/cell_component.rb', line 10

def initialize(collection:, row:, column:, record:, label:, heading:, **)
  super(**)

  @collection = collection
  @row = row
  @column = column
  @record = record
  @heading = heading

  if @row.header?
    @label = Label.new(collection:, column:, label:)
  else
    @data = Data.new(record:, column:)
  end
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



8
9
10
# File 'app/components/katalyst/tables/cell_component.rb', line 8

def collection
  @collection
end

#columnObject (readonly)

Returns the value of attribute column.



8
9
10
# File 'app/components/katalyst/tables/cell_component.rb', line 8

def column
  @column
end

#recordObject (readonly)

Returns the value of attribute record.



8
9
10
# File 'app/components/katalyst/tables/cell_component.rb', line 8

def record
  @record
end

#rowObject (readonly)

Returns the value of attribute row.



8
9
10
# File 'app/components/katalyst/tables/cell_component.rb', line 8

def row
  @row
end

Instance Method Details

#callObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/components/katalyst/tables/cell_component.rb', line 38

def call
  content = if content?
              self.content
            elsif @row.header?
              label
            else
              rendered_value
            end

  content = @content_wrapper.with_content(content.to_s).render_in(view_context) if @content_wrapper

  concat((cell_tag, content, **html_attributes))
end

#heading?Boolean

Returns true if the cell is a heading cell (th).

Returns:

  • (Boolean)

    true if the cell is a heading cell (th).



27
28
29
# File 'app/components/katalyst/tables/cell_component.rb', line 27

def heading?
  @row.header? || @heading
end

#inspectObject



74
75
76
# File 'app/components/katalyst/tables/cell_component.rb', line 74

def inspect
  "#<#{self.class.name} method: #{@method.inspect}>"
end

#labelObject

Return the rendered and sanitized label for the column.



53
54
55
# File 'app/components/katalyst/tables/cell_component.rb', line 53

def label
  @label&.to_s
end

#rendered_valueObject

Return the serialized and sanitised data value for rendering in the cell.



63
64
65
# File 'app/components/katalyst/tables/cell_component.rb', line 63

def rendered_value
  @data&.to_s
end

#to_sObject

Serialize data for use in blocks, i.e.

row.text(:name) { |cell| tag.span(cell) }


69
70
71
72
# File 'app/components/katalyst/tables/cell_component.rb', line 69

def to_s
  # Note, this can't be `content` because the block is evaluated in order to produce content.
  rendered_value
end

#valueObject

Return the raw value of the cell (i.e. the value of the data read from the record)



58
59
60
# File 'app/components/katalyst/tables/cell_component.rb', line 58

def value
  @data&.value
end

#with_content_wrapper(component) ⇒ Object

Adds a component to wrap the content of the cell, similar to a layout in Rails views.



32
33
34
35
36
# File 'app/components/katalyst/tables/cell_component.rb', line 32

def with_content_wrapper(component)
  @content_wrapper = component

  self
end