Class: ModsDisplay::NestedRelatedItem

Inherits:
Field
  • Object
show all
Includes:
RelatedItemConcerns
Defined in:
lib/mods_display/fields/nested_related_item.rb

Overview

This class will hopefully take over for related item support more broadly. Currently there is behavior in RelatedItem and Collection that would need to be accounted for when adding nested metadata support.

Defined Under Namespace

Classes: ValueRenderer

Instance Method Summary collapse

Methods inherited from Field

#label, #render_in

Constructor Details

#initialize(related_item_elements, value_renderer: ValueRenderer) ⇒ NestedRelatedItem

Returns a new instance of NestedRelatedItem.



11
12
13
14
# File 'lib/mods_display/fields/nested_related_item.rb', line 11

def initialize(related_item_elements, value_renderer: ValueRenderer)
  super(related_item_elements)
  @value_renderer = value_renderer
end

Instance Method Details

#fieldsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mods_display/fields/nested_related_item.rb', line 16

def fields
  @fields ||= begin
    return_fields = RelatedItemValue.for_stanford_mods_elements(@stanford_mods_elements).map do |related_item_element|
      next if related_item_element.collection?
      next unless render_nested_related_item?(related_item_element)

      related_item_text = @value_renderer.new(related_item_element).render

      ModsDisplay::Values.new(
        label: related_item_label(related_item_element),
        values: [related_item_text]
      )
    end.compact
    collapse_fields(return_fields)
  end
end

#to_html(view_context = ApplicationController.renderer) ⇒ Object

Used by exhibits



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mods_display/fields/nested_related_item.rb', line 34

def to_html(view_context = ApplicationController.renderer)
  helpers = view_context.respond_to?(:simple_format) ? view_context : ApplicationController.new.view_context

  component = ModsDisplay::ListFieldComponent.with_collection(
    fields,
    value_transformer: ->(value) { helpers.format_mods_html(value.to_s) },
    list_html_attributes: { class: 'mods_display_nested_related_items' },
    list_item_html_attributes: { class: 'mods_display_nested_related_item open' }
  )

  view_context.render component, layout: false
end