Skip to content

Commit

Permalink
Merge pull request #5010 from samvera/renderer-sort
Browse files Browse the repository at this point in the history
handle optional `:sort` option for attribute renderers
  • Loading branch information
jeremyf authored Jul 15, 2021
2 parents e5fdb2b + 1c4ae5b commit 4023fcf
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions app/renderers/hyrax/renderers/attribute_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ class AttributeRenderer

attr_reader :field, :values, :options

##
# @param [Symbol] field
# @param [Array] values
# @param [Hash] options
# @option options [String] :label The field label to render
# @option options [String] :include_empty Do we render if if the values are empty?
# @option options [String] :work_type Used for some I18n logic
# @option options [Boolean] :sort sort the values with +Array#sort+ if truthy
def initialize(field, values, options = {})
@field = field
@values = values
Expand All @@ -31,7 +33,10 @@ def render

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

markup += Array(values).map do |value|
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

Expand All @@ -48,7 +53,10 @@ def render_dl_row

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

markup += Array(values).map do |value|
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>)
Expand Down

0 comments on commit 4023fcf

Please sign in to comment.