Skip to content

Commit

Permalink
Merge pull request #2143 from alphagov/amend_hub_page
Browse files Browse the repository at this point in the history
Amend tracking and anchor links on brexit child taxon pages
  • Loading branch information
hannako authored Jun 14, 2021
2 parents d7fe417 + 029ea0c commit 6bc6e31
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
27 changes: 18 additions & 9 deletions app/lib/body_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,31 @@ def raw_title_and_link_sections
end

def parsed_links(section)
raw_links = section.present? ? section.css("a") : []
raw_links = section.css("a") if section.present?
if raw_links.any?
raw_links.map do |link|
{
path: format_path(link["href"]),
text: link.content,
}
end
raw_links.map { |link| parse_link(link) }
else
[]
end
end

def parse_link(raw_link)
{
path: format_path(raw_link["href"]),
text: raw_link.content,
}
end

def parsed_title(section)
raw_title = section.present? ? section.at_css("h2") : ""
raw_title.present? ? raw_title.content : ""
parsed = { text: "", id: "" }

raw_header = section.at_css("h2") if section.present?
if raw_header.present?
id = raw_header.attribute("id")
parsed[:text] = raw_header.content
parsed[:id] = id.value if id.present?
end
parsed
end

def format_path(raw_url)
Expand Down
2 changes: 1 addition & 1 deletion app/views/content_items/detailed_guide.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
data: {
track_action: brexit_link[:path],
track_category: brexit_link[:track_category],
track_label: brexit_link[:text],
track_label: "Guidance nav link",
module: 'gem-track-click',
}) %>.
</p>
Expand Down
7 changes: 4 additions & 3 deletions app/views/content_items/detailed_guide/_brexit_links.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<% @content_item.title_and_link_sections.each do |section| %>
<% if section[:title].present? %>
<% if section[:title][:text].present? %>
<%= render "govuk_publishing_components/components/heading", {
text: section[:title],
text: section[:title][:text],
heading_level: 2,
font_size: "m",
margin_bottom: 6,
id: section[:title][:id],
} %>
<% end %>
<% if section[:links].present? %>
Expand All @@ -13,7 +14,7 @@
link_to(link[:text], link[:path], class: "govuk-link", data: {
track_action: link[:path],
track_category: track_category,
track_label: section[:title] || "",
track_label: section[:title][:text] || "",
module: 'gem-track-click',
})
end
Expand Down
2 changes: 1 addition & 1 deletion test/integration/detailed_guide_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,6 @@ class DetailedGuideTest < ActionDispatch::IntegrationTest

assert_equal ContentItem::BrexitHubPage::BREXIT_BUSINESS_PAGE_PATH, track_action
assert_equal "brexit-citizen-page", track_category
assert_equal "Brexit guidance for businesses", track_label
assert_equal "Guidance nav link", track_label
end
end
15 changes: 12 additions & 3 deletions test/unit/body_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ def subject
test "#title_and_link_sections" do
expected = [
{
title: "Travel to the EU",
title: {
text: "Travel to the EU",
id: "travel-to-the-eu",
},
links: [
{
path: "/foreign-travel-advice",
Expand All @@ -57,7 +60,10 @@ def subject
],
},
{
title: "Travel to the UK",
title: {
text: "Travel to the UK",
id: "travel-to-the-uk",
},
links: [
{
path: "/local-travel-advice",
Expand All @@ -78,7 +84,10 @@ def subject
subject = BodyParser.new(html_missing_section_headings)
expected = [
{
title: "",
title: {
text: "",
id: "",
},
links: [
{
path: "/foreign-travel-advice",
Expand Down

0 comments on commit 6bc6e31

Please sign in to comment.