Skip to content

Commit

Permalink
Merge pull request #482 from alphagov/add-about-section-to-article-sc…
Browse files Browse the repository at this point in the history
…hema

Add 'about' to schema.org article schema
  • Loading branch information
oscarwyatt committed Aug 15, 2018
2 parents 98e807f + 03b0143 commit ceec08a
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,10 @@
- Don't include changes that are purely internal. The CHANGELOG should be a
useful summary for people upgrading their application, not a replication
of the commit log.

# Unreleased

* Add the 'about' property for the schema.org schema for an Article with live taxons (PR #482)

## Unreleased

Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
@@ -1,5 +1,5 @@
GovukPublishingComponents::Engine.routes.draw do
root :to => 'component_guide#index', as: :component_guide
root to: 'component_guide#index', as: :component_guide
get ':component/preview' => 'component_guide#preview', as: :component_preview_all
get ':component/:example/preview' => 'component_guide#preview', as: :component_preview
get ':component' => 'component_guide#show', as: :component_doc
Expand Down
Expand Up @@ -29,7 +29,7 @@ def structured_data
"url" => page.logo_url,
}
}
}.merge(image_schema).merge(author_schema).merge(body).merge(is_part_of)
}.merge(image_schema).merge(author_schema).merge(body).merge(is_part_of).merge(about)
end

private
Expand Down Expand Up @@ -98,6 +98,29 @@ def linked_page(step_by_step)
image_placeholders: page.image_placeholders
)
end

def about
return {} unless live_taxons.any?
{
"about" => linked_taxons
}
end

def live_taxons
taxons = page.content_item.dig("links", "taxons")
return [] unless taxons
taxons.select { |taxon| taxon["phase"] == "live" }
end

def linked_taxons
live_taxons.map do |taxon|
{
"@context" => "http://schema.org",
"@type" => "CreativeWork",
"sameAs" => taxon["web_url"]
}
end
end
end
end
end
96 changes: 96 additions & 0 deletions spec/lib/presenters/schema_org_spec.rb
Expand Up @@ -174,6 +174,102 @@
expect(structured_data['image']).to eql([1, 2])
end

it "adds about schema if there are live taxons" do
content_item = GovukSchemas::RandomExample.for_schema(frontend_schema: "answer") do |random_item|
random_item.merge(live_taxons_links)
end

structured_data = generate_structured_data(
content_item: content_item,
schema: :article
).structured_data

expect(structured_data['about']).to eql([
{
"@context" => "http://schema.org",
"@type" => "CreativeWork",
"sameAs" => "https://www.gov.uk/education/becoming-an-apprentice"
},
{
"@context" => "http://schema.org",
"@type" => "CreativeWork",
"sameAs" => "https://www.gov.uk/employment/finding-job"
}
])
end

it "adds about schema but not not include non live taxon" do
one_live_one_alpha_taxon = live_taxons_links
one_live_one_alpha_taxon["links"]["taxons"][1]["phase"] = "alpha"
content_item = GovukSchemas::RandomExample.for_schema(frontend_schema: "answer") do |random_item|
random_item.merge(one_live_one_alpha_taxon)
end

structured_data = generate_structured_data(
content_item: content_item,
schema: :article
).structured_data

expect(structured_data['about']).to eql([
{
"@context" => "http://schema.org",
"@type" => "CreativeWork",
"sameAs" => "https://www.gov.uk/education/becoming-an-apprentice"
}
])
end

it "does not include about if no live taxons" do
no_live_taxons = live_taxons_links
no_live_taxons["links"]["taxons"][0]["phase"] = "alpha"
no_live_taxons["links"]["taxons"][1]["phase"] = "draft"
content_item = GovukSchemas::RandomExample.for_schema(frontend_schema: "answer") do |random_item|
random_item.merge(no_live_taxons)
end

structured_data = generate_structured_data(
content_item: content_item,
schema: :article
).structured_data

expect(structured_data['about']).to eql(nil)
end

def live_taxons_links
{
"links" => {
"taxons" => [
{
"api_path" => "/api/content/education/becoming-an-apprentice",
"base_path" => "/education/becoming-an-apprentice",
"content_id" => "ff0e8e1f-4dea-42ff-b1d5-f1ae37807af2",
"document_type" => "taxon",
"locale" => "en",
"schema_name" => "taxon",
"title" => "Becoming an apprentice",
"withdrawn" => false,
"phase" => "live",
"api_url" => "https://www.gov.uk/api/content/education/becoming-an-apprentice",
"web_url" => "https://www.gov.uk/education/becoming-an-apprentice"
},
{
"api_path" => "/api/content/employment/finding-job",
"base_path" => "/employment/finding-job",
"content_id" => "21bfd8f6-3360-43f9-be42-b00002982d70",
"document_type" => "taxon",
"locale" => "en",
"schema_name" => "taxon",
"title" => "Finding a job",
"withdrawn" => false,
"phase" => "live",
"api_url" => "https://www.gov.uk/api/content/employment/finding-job",
"web_url" => "https://www.gov.uk/employment/finding-job"
}
]
}
}
end

def generate_structured_data(args)
GovukPublishingComponents::Presenters::SchemaOrg.new(GovukPublishingComponents::Presenters::Page.new(args))
end
Expand Down

0 comments on commit ceec08a

Please sign in to comment.