Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle optional :sort option for attribute renderers #5010

Merged
merged 1 commit into from
Jul 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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