From be4ed0ff117ffac6c92804aa42cb5d061dd2f7b8 Mon Sep 17 00:00:00 2001 From: Tijmen Brommet Date: Thu, 17 May 2018 12:37:00 +0100 Subject: [PATCH] Add structured data via the new component This uses the new component built in alphagov/govuk_publishing_components#318. https://trello.com/c/RLQI1IUV --- app/presenters/news_article_presenter.rb | 4 -- app/presenters/news_article_structured.rb | 64 ------------------- app/views/content_items/answer.html.erb | 6 ++ app/views/content_items/guide.html.erb | 7 ++ app/views/content_items/news_article.html.erb | 8 ++- .../content_items/travel_advice.html.erb | 14 +++- .../presenters/news_article_presenter_test.rb | 16 ----- 7 files changed, 29 insertions(+), 90 deletions(-) delete mode 100644 app/presenters/news_article_structured.rb diff --git a/app/presenters/news_article_presenter.rb b/app/presenters/news_article_presenter.rb index 72e2316f1..b9507ed38 100644 --- a/app/presenters/news_article_presenter.rb +++ b/app/presenters/news_article_presenter.rb @@ -10,8 +10,4 @@ class NewsArticlePresenter < ContentItemPresenter def image content_item["details"]["image"] end - - def structured_data - NewsArticleStructured.new(self).structured_data - end end diff --git a/app/presenters/news_article_structured.rb b/app/presenters/news_article_structured.rb deleted file mode 100644 index d3c9c61e4..000000000 --- a/app/presenters/news_article_structured.rb +++ /dev/null @@ -1,64 +0,0 @@ -class NewsArticleStructured - def initialize(presenter) - @presenter = presenter - end - - def structured_data - return {} unless enough_structured_data? - - # http://schema.org/NewsArticle - { - "@context" => "http://schema.org", - "@type" => "NewsArticle", - "mainEntityOfPage" => { - "@type" => "WebPage", - "@id" => page_url, - }, - "headline" => presenter.title, - "datePublished" => presenter.first_public_at, - "dateModified" => presenter.public_updated_at, - "description" => presenter.description, - "publisher" => { - "@type" => "Organization", - "name" => "GOV.UK", - "url" => "https://www.gov.uk", - "logo" => { - "@type" => "ImageObject", - # TODO: change this to a better image, without the URL hard coded. - "url" => "https://assets.publishing.service.gov.uk/static/opengraph-image-a1f7d89ffd0782738b1aeb0da37842d8bd0addbd724b8e58c3edbc7287cc11de.png", - }, - }, - "image" => [ - image["url"], - ], - "author" => { - "@type" => "Organization", - "name" => publishing_organisation["title"], - "url" => Plek.current.website_root + publishing_organisation["base_path"], - }, - } - end - -private - - attr_reader :presenter - - def enough_structured_data? - # The author (for which we use the publishing org) and image are required - # fields. If the news article doesn't have them, don't use structured data - # at all. - publishing_organisation && image - end - - def publishing_organisation - presenter.content_item.dig("links", "primary_publishing_organisation").to_a.first - end - - def page_url - Plek.current.website_root + presenter.content_item["base_path"] - end - - def image - presenter.image - end -end diff --git a/app/views/content_items/answer.html.erb b/app/views/content_items/answer.html.erb index 24ee37dea..cf9e7615d 100644 --- a/app/views/content_items/answer.html.erb +++ b/app/views/content_items/answer.html.erb @@ -1 +1,7 @@ +<% content_for :extra_head_content do %> + <%= render 'govuk_publishing_components/components/machine_readable_metadata', + schema: :article, + content_item: @content_item.content_item %> +<% end %> + <%= render 'content_items/body_with_related_links' %> diff --git a/app/views/content_items/guide.html.erb b/app/views/content_items/guide.html.erb index d0c4f5d34..2a74b31fe 100644 --- a/app/views/content_items/guide.html.erb +++ b/app/views/content_items/guide.html.erb @@ -1,3 +1,10 @@ +<% content_for :extra_head_content do %> + <%= render 'govuk_publishing_components/components/machine_readable_metadata', + schema: :article, + content_item: @content_item.content_item, + canonical_url: @content_item.canonical_url %> +<% end %> + <% content_for :simple_header, true %>
diff --git a/app/views/content_items/news_article.html.erb b/app/views/content_items/news_article.html.erb index 018a00e4d..494c9618c 100644 --- a/app/views/content_items/news_article.html.erb +++ b/app/views/content_items/news_article.html.erb @@ -1,6 +1,8 @@ - +<% content_for :extra_head_content do %> + <%= render 'govuk_publishing_components/components/machine_readable_metadata', + schema: :news_article, + content_item: @content_item.content_item %> +<% end %>
diff --git a/app/views/content_items/travel_advice.html.erb b/app/views/content_items/travel_advice.html.erb index d4742c527..c2cea10f3 100644 --- a/app/views/content_items/travel_advice.html.erb +++ b/app/views/content_items/travel_advice.html.erb @@ -1,9 +1,17 @@ -<% - content_for :simple_header, true - content_for :extra_head_content do %> +<% content_for :simple_header, true %> + +<% content_for :extra_head_content do %> <%= auto_discovery_link_tag :atom, @content_item.feed_link, title: "Recent updates for #{@content_item.country_name}" %> + + <%= render 'govuk_publishing_components/components/machine_readable_metadata', + schema: :article, + content_item: @content_item.content_item, + canonical_url: @content_item.canonical_url, + title: @content_item.page_title, + body: @content_item.current_part_body %> <% end %> +
<%= render 'govuk_component/title', @content_item.title_and_context %> diff --git a/test/presenters/news_article_presenter_test.rb b/test/presenters/news_article_presenter_test.rb index 989420411..7dd09d040 100644 --- a/test/presenters/news_article_presenter_test.rb +++ b/test/presenters/news_article_presenter_test.rb @@ -45,22 +45,6 @@ class PresentedNewsArticleTest < NewsArticlePresenterTestCase test 'presents the locale' do assert_equal schema_item['locale'], presented_item.locale end - - test 'has no structured data if no publishing org' do - expected = {} - assert_equal expected, presented_item.structured_data - end - - test 'has structured data' do - item = { "links" => { "primary_publishing_organisation" => [{ "title" => "Ministry of Magic", "base_path" => "/government/organisations/magic" }] } } - - structured_data = presented_item("news_article", item).structured_data - - assert_equal "https://www.test.gov.uk/government/news/christmas-2016-prime-ministers-message", structured_data["mainEntityOfPage"]["@id"] - assert_equal "2016-12-25T00:15:02.000+00:00", structured_data["datePublished"] - assert_equal "Ministry of Magic", structured_data["author"]["name"] - assert_equal "https://www.test.gov.uk/government/organisations/magic", structured_data["author"]["url"] - end end class HistoryModePresentedNewsArticle < NewsArticlePresenterTestCase