Skip to content

Commit

Permalink
Merge pull request #154 from alphagov/mvp-document-collections
Browse files Browse the repository at this point in the history
Add document collections format
  • Loading branch information
boffbowsh committed May 25, 2016
2 parents 9b9f3b7 + 7efbba1 commit 7613945
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@
@import "views/detailed-guide";
@import "views/publication";
@import "views/fatality-notice";
@import "views/document-collection";
35 changes: 35 additions & 0 deletions app/assets/stylesheets/views/_document-collection.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.document-collection {
@include description;
@include sidebar-with-body;
@include history-notice;
@include withdrawal-notice;

.group-title {
@include bold-27;
margin-top: $gutter;
margin-bottom: $gutter-half;

@include media(desktop) {
margin-top: $gutter * 1.5;
margin-bottom: $gutter-two-thirds;
}

&:first-child {
margin-top: 0;
}
}

.group-document-list-item {
display: block;
list-style: none;
margin-bottom: $gutter-half;

@include media(desktop) {
margin-bottom: $gutter-two-thirds;
}
}

.collection-document-title {
@include bold-19;
}
}
47 changes: 47 additions & 0 deletions app/presenters/document_collection_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class DocumentCollectionPresenter < ContentItemPresenter
include Political
include Linkable
include Updatable
include Withdrawable
include ActionView::Helpers::UrlHelper

def body
content_item["details"]["body"]
end

def contents
groups.map do |group|
title = group["title"]
link_to(title, "##{group_title_id(title)}")
end
end

def groups
content_item["details"]["collection_groups"]
end

def group_document_links(group)
group_documents(group).map do |link|
link_to(link["title"], link["base_path"])
end
end

def group_heading(group)
title = group["title"]
content_tag :h3, title, class: "group-title", id: group_title_id(title)
end

private

def group_documents(group)
group["documents"].map { |id| documents_hash[id] }
end

def group_title_id(title)
title.tr(' ', '-').downcase
end

def documents_hash
@documents_hash ||= content_item["links"]["documents"].map { |d| [d["content_id"], d] }.to_h
end
end
65 changes: 65 additions & 0 deletions app/views/content_items/document_collection.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<%= content_for :page_class, @content_item.format.dasherize %>
<%= content_for :title, @content_item.page_title %>

<div class="grid-row">
<div class="column-two-thirds">
<%= render 'govuk_component/title',
context: t("content_item.format.#{@content_item.document_type}", count: 1),
title: @content_item.title,
average_title_length: "long" %>
</div>
</div>

<%= render 'shared/withdrawal_notice', content_item: @content_item %>

<div class="grid-row">
<div class="column-two-thirds">
<%= render 'govuk_component/metadata',
from: @content_item.from,
part_of: @content_item.part_of,
first_published: @content_item.published,
last_updated: @content_item.updated,
see_updates_link: true
%>
</div>
</div>
<%= render 'shared/history_notice', content_item: @content_item %>
<%= render 'shared/description', description: @content_item.description %>

<div class="grid-row sidebar-with-body">
<div class="column-third">
<%= render 'shared/sidebar_contents', contents: @content_item.contents %>
</div>
<div class="column-two-thirds">
<% if @content_item.body.present? %>
<%= render 'govuk_component/govspeak',
content: @content_item.body,
direction: page_text_direction %>
<% end %>
<% @content_item.groups.each do |group| %>
<%= @content_item.group_heading(group) %>
<% if group["body"].present? %>
<%= render 'govuk_component/govspeak',
content: group["body"],
direction: page_text_direction %>
<% end %>
<ol class="group-document-list">
<% @content_item.group_document_links(group).each do |link| %>
<li class="group-document-list-item">
<h3 class="collection-document-title"><%= link %></h3>
<%# TODO: Include document date and type when available %>
</li>
<% end %>
</ol>
<% end %>
</div>
</div>

<%= render 'govuk_component/document_footer',
from: @content_item.from,
updated: @content_item.updated,
history: @content_item.history,
published: @content_item.published,
part_of: @content_item.part_of,
direction: page_text_direction
%>
75 changes: 75 additions & 0 deletions test/integration/document_collection_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require 'test_helper'

class DocumentCollectionTest < ActionDispatch::IntegrationTest
test "document collection" do
setup_and_visit_content_item('document_collection')
assert_has_component_title(@content_item["title"])
assert page.has_text?(@content_item["description"])
end

test "renders metadata and document footer" do
setup_and_visit_content_item('document_collection')
assert_has_component_metadata_pair("first_published", "29 February 2016")

from = ["<a href=\"/government/organisations/driver-and-vehicle-standards-agency\">Driver and Vehicle Standards Agency</a>"]
assert_has_component_metadata_pair("from", from)
assert_has_component_document_footer_pair("from", from)
end

test "renders body when provided" do
setup_and_visit_content_item('document_collection_with_body')
assert_has_component_govspeak(@content_item["details"]["body"])
end

test "renders contents with link to each collection group" do
setup_and_visit_content_item('document_collection')
@content_item["details"]["collection_groups"].each do |group|
assert page.has_css?('nav a', text: group["title"])
end
end

test "renders each collection group" do
setup_and_visit_content_item('document_collection')
groups = @content_item["details"]["collection_groups"]
group_count = groups.count

groups.each do |group|
assert page.has_css?('.group-title', text: group["title"])
end

within ".sidebar-with-body" do
assert page.has_css?(shared_component_selector("govspeak"), count: group_count)
assert page.has_css?('.group-document-list', count: group_count)
end
end

test "renders all collection documents" do
setup_and_visit_content_item('document_collection')
documents = @content_item["links"]["documents"]

documents.each do |doc|
assert page.has_css?('.collection-document-title a', text: doc["title"])
end

assert page.has_css?('.group-document-list .group-document-list-item', count: documents.count)
end

test "withdrawn collection" do
setup_and_visit_content_item('document_collection')
assert page.has_css?('title', text: "[Withdrawn]", visible: false)

within ".withdrawal-notice" do
assert page.has_text?('This collection was withdrawn'), "is withdrawn"
assert_has_component_govspeak(@content_item["details"]["withdrawn_notice"]["explanation"])
assert page.has_css?("time[datetime='#{@content_item['details']['withdrawn_notice']['withdrawn_at']}']")
end
end

test "historically political collection" do
setup_and_visit_content_item('document_collection')

within ".history-notice" do
assert page.has_text?('This collection was published under the 2010 to 2015 Conservative and Liberal Democrat coalition government')
end
end
end
42 changes: 42 additions & 0 deletions test/presenters/document_collection_presenter_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'presenter_test_helper'

class DocumentCollectionPresenterTest < PresenterTest
def format_name
"document_collection"
end

test 'presents the basic details of a content item' do
assert_equal schema_item['description'], presented_item.description
assert_equal schema_item['format'], presented_item.format
assert_equal schema_item['title'], presented_item.title
assert_equal schema_item('document_collection_with_body')['details']['body'], presented_item('document_collection_with_body').body
end

test 'presents a contents list based on collection groups' do
contents = [
"<a href=\"#car-and-light-van\">Car and light van</a>",
"<a href=\"#moped-and-motorcycle\">Moped and motorcycle</a>",
"<a href=\"#lorry\">Lorry</a>",
"<a href=\"#bus-and-coach\">Bus and coach</a>",
"<a href=\"#driver-and-rider-trainer\">Driver and rider trainer</a>",
"<a href=\"#developed-driving-competence\">Developed driving competence</a>"
]

assert_equal contents, presented_item.contents
end

test 'presents a group heading with generated ID' do
heading = '<h3 class="group-title" id="heading-with-spaces">Heading with Spaces</h3>'
assert_equal heading, presented_item.group_heading("title" => "Heading with Spaces")
end

test 'presents an ordered list of group documents' do
documents = [
"<a href=\"/government/publications/national-standard-for-driving-cars-and-light-vans\">National standard for driving cars and light vans</a>",
"<a href=\"/government/publications/car-and-small-van-driving-syllabus\">Car and light van driving syllabus</a>",
"<a href=\"/government/publications/car-and-light-van-driver-competence-framework\">Car and light van driver competence framework</a>",
]
document_ids = schema_item["details"]["collection_groups"].first["documents"]
assert_equal documents, presented_item.group_document_links("documents" => document_ids)
end
end

0 comments on commit 7613945

Please sign in to comment.