Skip to content

Commit

Permalink
Add ability to render sub facets as metadata
Browse files Browse the repository at this point in the history
Sub-facets are introduced as part of Nested Facet work on Specialist Publisher. The new structure introduced allows users to define one level of nesting for facet values. These also need to be surfaced as part of metadata.
  • Loading branch information
minhngocd committed Feb 19, 2025
1 parent ad92c1c commit d3ae6f4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 24 deletions.
65 changes: 41 additions & 24 deletions app/presenters/specialist_document_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def important_metadata
super.tap do |m|
facets_with_friendly_values.each do |facet|
m.merge!(facet["name"] => value_or_array_of_values(facet["values"]))
m.merge!(facet["sub_facet_name"] => value_or_array_of_values(facet["sub_facet_values"])) if facet["sub_facet_values"].present?
end
end
end
Expand Down Expand Up @@ -120,18 +121,13 @@ def facet_values

def facets_with_friendly_values
facets_with_values.map do |facet|
facet_key = facet["key"]
# Cast all values into an array
values = [facet_values[facet_key]].flatten

facet["values"] = case facet["type"]
when "date"
friendly_facet_date(values)
when "text"
friendly_facet_text(facet, values)
else
values
end
values = [facet_values[facet["key"]]].compact.flatten
facet["values"] = friendly_facet_values(facet, values)

if facet["nested_facet"]
sub_facet_values = [facet_values[facet["sub_facet_key"]]].compact.flatten
facet["sub_facet_values"] = friendly_sub_facet_text(facet, sub_facet_values)
end

facet
end
Expand All @@ -146,42 +142,63 @@ def facets_with_values
.reject { |f| f["key"] == internal_notes_facet_key }
end

def friendly_facet_values(facet, values)
return friendly_facet_date(values) if facet["type"] == "date"
return friendly_facet_text(facet, values) if facet["type"] == "text"

values
end

def friendly_facet_date(dates)
dates.map { |date| display_date(date) }
end

def friendly_facet_text(facet, values)
if facet["allowed_values"] && facet["allowed_values"].any?
facet_blocks(facet, values)
else
values
end
return values unless facet["allowed_values"].present?

Check failure on line 157 in app/presenters/specialist_document_presenter.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Rails/Blank: Use `if facet["allowed_values"].blank?` instead of `unless facet["allowed_values"].present?`.

facet_blocks(facet["name"],
facet["key"],
facet["allowed_values"],
values,
facet["filterable"])
end

def friendly_sub_facet_text(facet, sub_facet_values)
return sub_facet_values unless facet["allowed_values"].present?

Check failure on line 167 in app/presenters/specialist_document_presenter.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Rails/Blank: Use `if facet["allowed_values"].blank?` instead of `unless facet["allowed_values"].present?`.

sub_facet_allowed_values = facet["allowed_values"].pluck("sub_facets").compact.flatten

facet_blocks(facet["sub_facet_name"],
facet["sub_facet_key"],
sub_facet_allowed_values,
sub_facet_values,
facet["filterable"])
end

# The facet value is hyphenated, map this to the
# friendly readable version provided in `allowed_values`
def facet_blocks(facet, values)
def facet_blocks(facet_name, facet_key, allowed_values, values, filterable)
values.map do |value|
allowed_value = facet["allowed_values"].detect { |av| av["value"] == value }
allowed_value = allowed_values.detect { |av| av["value"] == value }

if allowed_value
facet_block(facet, allowed_value)
facet_block(allowed_value, facet_key, filterable)
else
GovukError.notify(
"Facet value not in list of allowed values",
extra: { error_message: "Facet value '#{value}' not an allowed value for facet '#{facet['name']}' on #{base_path} content item" },
extra: { error_message: "Facet value '#{value}' not an allowed value for facet '#{facet_name}' on #{base_path} content item" },
)
value
end
end
end

def facet_block(facet, allowed_value)
def facet_block(allowed_value, facet_key, filterable)
friendly_value = allowed_value["label"]

return friendly_value unless facet["filterable"]
return friendly_value unless filterable

facet_link(friendly_value, allowed_value["value"], facet["key"])
facet_link(friendly_value, allowed_value["value"], facet_key)
end

def facet_link(label, value, key)
Expand Down
27 changes: 27 additions & 0 deletions test/presenters/specialist_document_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,5 +329,32 @@ def example_facet(overrides = {})
presented = present_example(example)
assert Time.zone.parse(presented.published) == Time.zone.parse("2010-01-01")
end

test "includes nested facets as text in metadata" do
values = { "facet-key" => "main-facet-1-value", "sub-facet-key" => "sub-facet-1-value" }
facet = example_facet({
"sub_facet_name" => "Sub Facet name",

Check failure on line 336 in test/presenters/specialist_document_presenter_test.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Layout/FirstHashElementIndentation: Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.
"sub_facet_key" => "sub-facet-key",
"nested_facet" => true,
"allowed_values" => [
{
"label" => "Main Facet Value",
"value" => "main-facet-1-value",
"sub_facets" => [
{
"label" => "Sub Facet Value",
"value" => "sub-facet-1-value"

Check failure on line 346 in test/presenters/specialist_document_presenter_test.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Style/TrailingCommaInHashLiteral: Put a comma after the last item of a multiline hash.
}

Check failure on line 347 in test/presenters/specialist_document_presenter_test.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Style/TrailingCommaInArrayLiteral: Put a comma after the last item of a multiline array. (https://rubystyle.guide#no-trailing-array-commas)
]

Check failure on line 348 in test/presenters/specialist_document_presenter_test.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Style/TrailingCommaInHashLiteral: Put a comma after the last item of a multiline hash.
},
],
})

Check failure on line 351 in test/presenters/specialist_document_presenter_test.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Layout/FirstHashElementIndentation: Indent the right brace the same as the start of the line where the left brace is.
example = example_with_finder_facets([facet], values)

presented = present_example(example)

assert_equal "Main Facet Value", presented.important_metadata["Facet name"]
assert_equal "Sub Facet Value", presented.important_metadata["Sub Facet name"]
end
end
end

0 comments on commit d3ae6f4

Please sign in to comment.