From ed4692933245ce1c64af60672dc2363e9cc78e29 Mon Sep 17 00:00:00 2001 From: Leena Gupte Date: Wed, 26 Feb 2025 17:01:11 +0000 Subject: [PATCH] Add check for available translations The available translations method looks through the `available_translations` and replaces the English translation with `""`. That means that if there's only an English translation, then the `available_translations` will return `[""]`. `any?` was being used in the view to determine whether or not to display the translations partial, however, `any?` returns true for `[""]`. ``` frontend(dev)> a = [""] => [""] frontend(dev)> a.any? => true frontend(dev)> a.length > 1 => false ``` So rather than not rendering anything when there is only an English translation, an empty `div` was being rendered: `
` Instead of `any?` the original logic from [government-frontend] is being reinstated. This logic has been made into a method in the model so that it can be easily tested. [government-frontend]: https://github.com/alphagov/government-frontend/blob/85f4681feed9ed3b57bb7f2ea0dce10bf6e0338a/app/views/shared/_translations.html.erb#L1 --- app/models/content_item.rb | 4 ++ app/views/shared/_translations.html.erb | 4 +- spec/models/content_item_spec.rb | 49 +++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/app/models/content_item.rb b/app/models/content_item.rb index a3dd585fd7..20514b4454 100644 --- a/app/models/content_item.rb +++ b/app/models/content_item.rb @@ -51,6 +51,10 @@ def method_missing(method_name, *_args, &_block) end end + def available_translations? + available_translations.length > 1 + end + def available_translations translations = content_store_response["links"]["available_translations"] || [] diff --git a/app/views/shared/_translations.html.erb b/app/views/shared/_translations.html.erb index 91fc9d7a70..21124db817 100644 --- a/app/views/shared/_translations.html.erb +++ b/app/views/shared/_translations.html.erb @@ -1,6 +1,6 @@ -<% if @content_item.available_translations.any? %> +<% if content_item.available_translations? %>
<%= render "govuk_publishing_components/components/translation_nav", - translations: translations_for_nav(@content_item.available_translations) %> + translations: translations_for_nav(content_item.available_translations) %>
<% end %> diff --git a/spec/models/content_item_spec.rb b/spec/models/content_item_spec.rb index 2029f33ae8..13cd734a8a 100644 --- a/spec/models/content_item_spec.rb +++ b/spec/models/content_item_spec.rb @@ -82,6 +82,55 @@ end end + describe "#available_translations?" do + subject(:content_item) { described_class.new(links) } + + context "when there are multiple translations" do + let(:links) do + { + "links" => { + "available_translations" => [ + { "locale" => "cy" }, + { "locale" => "en" }, + ], + }, + } + end + + it "returns true" do + expect(content_item.available_translations?).to be true + end + end + + context "when there are only English translations" do + let(:links) do + { + "links" => { + "available_translations" => [ + { "locale" => "en" }, + ], + }, + } + end + + it "returns false" do + expect(content_item.available_translations?).to be false + end + end + + context "when there are no translations" do + let(:links) do + { + "links" => {}, + } + end + + it "returns false" do + expect(content_item.available_translations?).to be false + end + end + end + describe "#available_translations" do subject(:content_item) do described_class.new(