Class: Hyrax::Renderers::AttributeRenderer

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TextHelper, ActionView::Helpers::TranslationHelper, ActionView::Helpers::UrlHelper, ConfiguredMicrodata
Defined in:
app/renderers/hyrax/renderers/attribute_renderer.rb

Overview

Since:

  • 0.14.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConfiguredMicrodata

#microdata?, #microdata_object?, #microdata_object_attributes, #microdata_property, #microdata_type, #microdata_value_attributes

Constructor Details

#initialize(field, values, options = {}) ⇒ AttributeRenderer

Returns a new instance of AttributeRenderer.

Parameters:

  • field (Symbol)
  • values (Array)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :label (String)

    The field label to render

  • :include_empty (String)

    Do we render if if the values are empty?

  • :work_type (String)

    Used for some I18n logic

Since:

  • 0.14.0


20
21
22
23
24
# File 'app/renderers/hyrax/renderers/attribute_renderer.rb', line 20

def initialize(field, values, options = {})
  @field = field
  @values = values
  @options = options
end

Instance Attribute Details

#fieldObject (readonly)

Since:

  • 0.14.0


12
13
14
# File 'app/renderers/hyrax/renderers/attribute_renderer.rb', line 12

def field
  @field
end

#optionsObject (readonly)

Since:

  • 0.14.0


12
13
14
# File 'app/renderers/hyrax/renderers/attribute_renderer.rb', line 12

def options
  @options
end

#valuesObject (readonly)

Since:

  • 0.14.0


12
13
14
# File 'app/renderers/hyrax/renderers/attribute_renderer.rb', line 12

def values
  @values
end

Instance Method Details

#labelObject

Note:

This is a central location for determining the label of a field name. Can be overridden if more complicated logic is needed.

Defaults to the label provided in the options, otherwise, it fallsback to the inner logic of the method.

Returns:

  • The human-readable label for this field.

Since:

  • 0.14.0


65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/renderers/hyrax/renderers/attribute_renderer.rb', line 65

def label
  if options&.key?(:label)
    options.fetch(:label)
  else
    translate(
      :"blacklight.search.fields.#{work_type_label_key}.show.#{field}",
      default: [:"blacklight.search.fields.show.#{field}",
                :"blacklight.search.fields.#{field}",
                field.to_s.humanize]
    )
  end
end

#renderObject

Draw the table row for the attribute

Since:

  • 0.14.0


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/renderers/hyrax/renderers/attribute_renderer.rb', line 27

def render
  return '' if values.blank? && !options[:include_empty]

  markup = %(<tr><th>#{label}</th>\n<td><ul class='tabular'>)

  attributes = microdata_object_attributes(field).merge(class: "attribute attribute-#{field}")

  markup += Array(values).map do |value|
    "<li#{html_attributes(attributes)}>#{attribute_value_to_html(value.to_s)}</li>"
  end.join

  markup += %(</ul></td></tr>)

  markup.html_safe
end

#render_dl_rowObject

Draw the dl row for the attribute

Since:

  • 0.14.0


44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/renderers/hyrax/renderers/attribute_renderer.rb', line 44

def render_dl_row
  return '' if values.blank? && !options[:include_empty]

  markup = %(<dt>#{label}</dt>\n<dd><ul class='tabular'>)

  attributes = microdata_object_attributes(field).merge(class: "attribute attribute-#{field}")

  markup += Array(values).map do |value|
    "<li#{html_attributes(attributes)}>#{attribute_value_to_html(value.to_s)}</li>"
  end.join
  markup += %(</ul></dd>)

  markup.html_safe
end