Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/grouped-related-links'
Browse files Browse the repository at this point in the history
  • Loading branch information
alext committed Jul 23, 2013
2 parents eeaa24f + 05014a8 commit 6a24dfb
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 27 deletions.
12 changes: 7 additions & 5 deletions app/assets/stylesheets/core.scss
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ h4 {
}

.inner {
background-color: $white;;
border-bottom: 10px solid $grey-8;
background-color: $white;
padding: 0.5em 1em 0 0;
width: auto;

Expand All @@ -168,7 +167,7 @@ h4 {
}

nav {
margin-bottom: 2.5em;
margin-bottom: 1.25em;
}

h2 {
Expand Down Expand Up @@ -201,8 +200,11 @@ h4 {
list-style: none outside none;

a {
padding: 0;
font-weight: bold;
padding: 0 0.5em;
display: inline;
text-decoration: none;
background-color: $light-blue-25;
font-weight: normal;
}
}

Expand Down
49 changes: 31 additions & 18 deletions app/views/root/related.raw.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,38 @@
<div class="related-container">

<div class="related" id="related">
<div class="inner group">
<% artefact.related_artefacts.group_by(&:group).each do |group, related_artefacts| %>
<%
section = case group
when 'subsection' then artefact.primary_section
when 'section' then artefact.primary_root_section
when 'other' then { "title" => "Elsewhere on GOV.UK" }
end
section = section['parent'] if artefact.format == "travel-advice" and group == "subsection"
%>
<div class="inner group related-<%= group %>">
<% if section %>
<h2 id="parent-<%= group %>"><%= h section["title"] %></h2>
<% end %>

<% if root_section = artefact.primary_root_section %>
<h2 id="parent-section"><%= h root_section["title"] %></h2>
<% end %>
<nav role="navigation" aria-labelledby="parent-section">
<ul>
<% artefact.related_artefacts.each do |item| %>
<li>
<a href="<%= h item.web_url %>"><%= h item.title %></a>
</li>
<% end %>
<% if root_section = artefact.primary_root_section %>
<li class="related-topic"><a href="<%= h root_section["content_with_tag"]["web_url"] %>"
>More</a></li>
<% end %>
</ul>
</nav>
<a class="return-to-top" href="#content">Return to top &uarr;</a>
<nav role="navigation" aria-labelledby="parent-<%= group %>">
<ul>
<% related_artefacts.each do |item| %>
<li>
<a href="<%= h item.web_url %>"><%= h item.title %></a>
</li>
<% end %>
<% if section and section["content_with_tag"] and section["content_with_tag"]["web_url"].present? %>
<li class="related-topic"><a href="<%= h section["content_with_tag"]["web_url"] %>"
>view all</a></li>
<% end %>
</ul>
</nav>
</div>
<% end %>

<div class="inner group">
<a class="return-to-top" href="#content">Return to top &uarr;</a>
</div>

<div id="legacy-sources">
Expand Down
56 changes: 52 additions & 4 deletions test/integration/related_template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ def get_template

context "with related artefacts" do
setup do
@related1 = stub("Artefact", :web_url => "http://www.example.com/foo", :title => "Foo")
@related2 = stub("Artefact", :web_url => "http://www.example.com/bar", :title => "Bar")
@artefact = stub("Artefact", :related_artefacts => [@related1, @related2], :primary_root_section => { "content_with_tag" => { "web_url" => "/browse/section" }})
@related1 = stub("Artefact", :web_url => "http://www.example.com/foo", :title => "Foo", :group => "subsection")
@related2 = stub("Artefact", :web_url => "http://www.example.com/bar", :title => "Bar", :group => "section")
@related3 = stub("Artefact", :web_url => "http://www.example.com/baz", :title => "Baz", :group => "other")
@artefact = stub("Artefact",
:related_artefacts => [@related1, @related2, @related3],
:primary_root_section => { "title" => "Section", "content_with_tag" => { "web_url" => "/browse/section" }},
:primary_section => { "title" => "Sub-section", "content_with_tag" => { "web_url" => "/browse/section/subsection" } },
:format => "guide"
)
end

should "add an item for each related item" do
Expand All @@ -39,6 +45,20 @@ def get_template
assert_match /foo\?bar=baz&amp;id=2/, result
end

should "add the subsection links if present" do
@artefact.stubs(:primary_section).returns({
"title" => "Subsection",
"content_with_tag" => {"web_url" => "http://www.example.com/browse/section/subsection"},
})
template = get_template
artefact = @artefact
result = ERB.new(template).result(binding)
doc = Nokogiri::HTML.parse(result)

assert doc.at_css("ul li.related-topic a[href='http://www.example.com/browse/section/subsection']")
assert_equal "view all", doc.at_css("ul li a[href='http://www.example.com/browse/section/subsection']").text
end

should "add the section link if present" do
@artefact.stubs(:primary_root_section).returns({
"title" => "Something",
Expand All @@ -50,7 +70,35 @@ def get_template
doc = Nokogiri::HTML.parse(result)

assert doc.at_css("ul li.related-topic a[href='http://www.example.com/browse/something']")
assert_equal "More", doc.at_css("ul li a[href='http://www.example.com/browse/something']").text
assert_equal "view all", doc.at_css("ul li a[href='http://www.example.com/browse/something']").text
end

should "add the internal links elsewhere if present" do
template = get_template
artefact = @artefact
result = ERB.new(template).result(binding)
doc = Nokogiri::HTML.parse(result)

assert_equal "Elsewhere on GOV.UK", doc.at_css("h2#parent-other").text
end

should "use the second-level section name for travel advice formats" do
@artefact.stubs(:format).returns("travel-advice")
@artefact.stubs(:primary_section).returns({
"title" => "Foreign travel advice",
"content_with_tag" => {"web_url" => "/foreign-travel-advice"},
"parent" => {
"title" => "Foo",
"content_with_tag" => {"web_url" => "/browse/abc/foo"}
}
})

template = get_template
artefact = @artefact
result = ERB.new(template).result(binding)
doc = Nokogiri::HTML.parse(result)

assert_equal "Foo", doc.at_css("h2#parent-subsection").text
end
end

Expand Down

0 comments on commit 6a24dfb

Please sign in to comment.