From 1c4ae5b1dc5d7b6c1d93e95f3ab28cf72cf6e26d Mon Sep 17 00:00:00 2001 From: tamsin johnson Date: Wed, 30 Jun 2021 16:28:01 -0700 Subject: [PATCH] handle optional `:sort` option for attribute renderers following a suggestion in #395, introduce a `:sort` option for renderers and a startlingly naive implementation. --- app/renderers/hyrax/renderers/attribute_renderer.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/renderers/hyrax/renderers/attribute_renderer.rb b/app/renderers/hyrax/renderers/attribute_renderer.rb index 91b28fbefc..2058552913 100644 --- a/app/renderers/hyrax/renderers/attribute_renderer.rb +++ b/app/renderers/hyrax/renderers/attribute_renderer.rb @@ -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 @@ -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| "#{attribute_value_to_html(value.to_s)}" end.join @@ -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| "#{attribute_value_to_html(value.to_s)}" end.join markup += %()