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

Fix related navigation component bugs #205

Merged
merged 2 commits into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def related_navigation
{ "topical_events" => related_topical_events },
{ "world_locations" => related_world_locations },
{ "statistical_data_sets" => related_statistical_data_sets },
{ "worldwide_organisations" => related_worldwide_organisations },
]

other = [related_external_links, related_contacts] || []
Expand Down Expand Up @@ -100,11 +99,6 @@ def related_world_locations
build_links_for_sidebar(locations)
end

def related_worldwide_organisations
organisations = filter_link_type("worldwide_organisations", "worldwide_organisation")
build_links_for_sidebar(organisations)
end

def related_collections
collections = filter_link_type("document_collections", "document_collection")
build_links_for_sidebar(collections)
Expand Down Expand Up @@ -172,7 +166,7 @@ def parent
end

def grandparent
parent.dig("parent", 0)
parent.dig("links", "parent", 0)
end

# This method post-processes the topics collated by the helper.
Expand Down Expand Up @@ -214,12 +208,14 @@ def tagged_to_same_mainstream_browse_page

def parents_tagged_to_same_mainstream_browse_page
return [] unless parent && grandparent

common_parent_content_ids = tagged_to_same_mainstream_browse_page.map(&:content_id)
common_parent_content_ids = tagged_to_same_mainstream_browse_page.map { |item| item["content_id"] }

@parents_tagged_to_same_mainstream_browse_page ||= related_links.select do |related_item|
next if common_parent_content_ids.include?(related_item["content_id"])
related_item.dig("links", "mainstream_browse_pages").map(&:parent).map(&:content_id).include?(grandparent["content_id"])
mainstream_browse_pages = related_item.dig("links", "mainstream_browse_pages")
parents = mainstream_browse_pages.map { |page| page["links"]["parent"][0] }
content_ids = parents.map { |parent| parent["content_id"] }
content_ids.include?(grandparent["content_id"])
end
end

Expand Down
127 changes: 86 additions & 41 deletions spec/lib/related_navigation_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def payload_for(schema, content_item)
{ "topical_events" => [] },
{ "world_locations" => [] },
{ "statistical_data_sets" => [] },
{ "worldwide_organisations" => [] },
]

expect(nothing).to eq(expected)
Expand Down Expand Up @@ -138,49 +137,11 @@ def payload_for(schema, content_item)
{ "topical_events" => [{ path: "/related-topical-event", text: "related topical event" }] },
{ "world_locations" => [{ path: "/world/world-location/news", text: "World, ~ (@Location)" }] },
{ "statistical_data_sets" => [] },
{ "worldwide_organisations" => [] },
]

expect(payload).to eql(expected)
end

it "returns worldwide organisations" do
payload = payload_for("world_location_news_article",
"details" => {
"body" => "body",
"government" => {
"title" => "government",
"slug" => "government",
"current" => true
},
"political" => true,
"first_public_at" => "2016-01-01T19:00:00Z",
},
"links" => {
"worldwide_organisations" => [
{
"content_id" => "32c1b93d-2553-47c9-bc3c-fc5b513ecc32",
"title" => "related worldwide organisation",
"base_path" => "/related-worldwide-organisation",
"document_type" => "worldwide_organisation",
"locale" => "en"
}
],
})
expected = [
{ "related_items" => [] },
{ "related_guides" => [] },
{ "collections" => [] },
{ "topics" => [] },
{ "policies" => [] },
{ "topical_events" => [] },
{ "world_locations" => [] },
{ "statistical_data_sets" => [] },
{ "worldwide_organisations" => [{ path: "/related-worldwide-organisation", text: "related worldwide organisation" }] },
]
expect(payload).to eql(expected)
end

it "returns statistical data sets" do
payload = payload_for("publication",
"details" => {
Expand Down Expand Up @@ -214,7 +175,6 @@ def payload_for(schema, content_item)
{ "topical_events" => [] },
{ "world_locations" => [] },
{ "statistical_data_sets" => [{ path: "/related-statistical-data-set", text: "related statistical data set" }] },
{ "worldwide_organisations" => [] },
]
expect(payload).to eql(expected)
end
Expand Down Expand Up @@ -263,12 +223,97 @@ def payload_for(schema, content_item)
{ "topical_events" => [] },
{ "world_locations" => [] },
{ "statistical_data_sets" => [] },
{ "worldwide_organisations" => [] },
]

expect(payload).to eql(expected)
end

it "returns parent and grandparent topics" do
payload = payload_for("answer",
"links" => {
"parent" => [
{
"base_path" => "/browse/working/tax-minimum-wage",
"content_id" => "a3e4b233-e2f3-4709-b531-8da7f21a3f6d",
"locale" => "en",
"title" => "Your pay, tax and the National Minimum Wage",
"links" => {
"parent" => [
{
"base_path" => "/browse/working",
"content_id" => "a24f0662-bbfe-4b77-9ec0-e73240e88b9a",
"locale" => "en",
"title" => "Working, jobs and pensions",
}
]
},
}
],
"ordered_related_items" => [
{
"base_path" => "/apprenticeships-guide",
"content_id" => "b95f3e83-a8d1-40fa-843c-cb712861707d",
"locale" => "en",
"title" => "Become an apprentice",
"links" => {
"mainstream_browse_pages" => [
{
"base_path" => "/browse/working/finding-job",
"content_id" => "eceb3053-43d1-4a49-873d-85ff9ade7066",
"locale" => "en",
"title" => "Finding a job",
"links" => {
"parent" => [
{
"base_path" => "/browse/working",
"content_id" => "a24f0662-bbfe-4b77-9ec0-e73240e88b9a",
"locale" => "en",
"title" => "Working, jobs and pensions",
}
]
},
}
]
},
},
{
"base_path" => "/national-insurance",
"content_id" => "c995522c-4020-4d54-ae5d-271d4b3a8777",
"locale" => "en",
"title" => "National Insurance",
"links" => {
"mainstream_browse_pages" => [
{
"base_path" => "/browse/working/tax-minimum-wage",
"content_id" => "a3e4b233-e2f3-4709-b531-8da7f21a3f6d",
"locale" => "en",
"title" => "Your pay, tax and the National Minimum Wage",
"links" => {
"parent" => [
{
"base_path" => "/browse/working",
"content_id" => "a24f0662-bbfe-4b77-9ec0-e73240e88b9a",
"locale" => "en",
"title" => "Working, jobs and pensions",
}
]
},
}
]
},
}
]
})

expected = {
"topics" => [
{ text: "Your pay, tax and the National Minimum Wage", path: "/browse/working/tax-minimum-wage" },
{ text: "Working, jobs and pensions", path: "/browse/working" },
]
}
expect(payload).to include(expected)
end

it "returns an Elsewhere on the web section for external related links" do
payload = payload_for("placeholder",
"details" => {
Expand Down