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

  • :sort (Boolean)

    sort the values with Array#sort if truthy

Since:

  • 0.14.0



22
23
24
25
26
# File 'app/renderers/hyrax/renderers/attribute_renderer.rb', line 22

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



73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/renderers/hyrax/renderers/attribute_renderer.rb', line 73

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/renderers/hyrax/renderers/attribute_renderer.rb', line 29

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}")

  values_array = Array(values)
  values_array = values_array.sort if options[:sort]

  markup += values_array.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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/renderers/hyrax/renderers/attribute_renderer.rb', line 49

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}")

  values_array = Array(values)
  values_array.sort! if options[:sort]

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

  markup.html_safe
end