Class: ModsDisplay::RowFieldComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/mods_display/row_field_component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field:, delimiter: nil, label_html_attributes: {}, value_html_attributes: {}, value_transformer: nil) ⇒ RowFieldComponent

Returns a new instance of RowFieldComponent.



7
8
9
10
11
12
13
14
15
# File 'app/components/mods_display/row_field_component.rb', line 7

def initialize(field:, delimiter: nil, label_html_attributes: {}, value_html_attributes: {}, value_transformer: nil)
  super

  @field = field
  @delimiter = delimiter
  @value_transformer = value_transformer
  @label_html_attributes = label_html_attributes
  @value_html_attributes = value_html_attributes
end

Instance Attribute Details

#delimiterObject (readonly)

Returns the value of attribute delimiter.



17
18
19
# File 'app/components/mods_display/row_field_component.rb', line 17

def delimiter
  @delimiter
end

#fieldObject (readonly)

Returns the value of attribute field.



17
18
19
# File 'app/components/mods_display/row_field_component.rb', line 17

def field
  @field
end

#label_html_attributesObject (readonly)

Returns the value of attribute label_html_attributes.



17
18
19
# File 'app/components/mods_display/row_field_component.rb', line 17

def label_html_attributes
  @label_html_attributes
end

#value_html_attributesObject (readonly)

Returns the value of attribute value_html_attributes.



17
18
19
# File 'app/components/mods_display/row_field_component.rb', line 17

def value_html_attributes
  @value_html_attributes
end

Instance Method Details

#format_value(value) ⇒ Object



23
24
25
26
27
28
29
# File 'app/components/mods_display/row_field_component.rb', line 23

def format_value(value)
  if @value_transformer
    @value_transformer.call(value)
  else
    helpers.format_mods_html(value, field: field)
  end
end

#labelObject



39
40
41
# File 'app/components/mods_display/row_field_component.rb', line 39

def label
  field.label&.sub(/:$/, '')
end

#render?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'app/components/mods_display/row_field_component.rb', line 19

def render?
  field.values.any?(&:present?)
end

#valuesObject



31
32
33
34
35
36
37
# File 'app/components/mods_display/row_field_component.rb', line 31

def values
  if delimiter
    [safe_join(field.values.select(&:present?).map { |value| format_value(value) }, delimiter)]
  else
    field.values.select(&:present?).map { |value| format_value(value) }
  end
end