Skip to content

Commit

Permalink
Add collections to publisher metadata if B variant
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgarner committed Aug 15, 2018
1 parent a3a949f commit 4e4b894
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 18 deletions.
1 change: 1 addition & 0 deletions app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def set_guide_draft_access_token
def load_content_item
content_item = Services.content_store.content_item(content_item_path)
@content_item = PresenterBuilder.new(content_item, content_item_path).presenter
@content_item.include_collections_in_other_publisher_metadata = show_new_navigation?
end

def load_taxonomy_navigation
Expand Down
18 changes: 11 additions & 7 deletions app/presenters/content_item/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ def important_metadata
end

def publisher_metadata
{
published: published,
last_updated: updated,
link_to_history: !!updated,
other: {
'From': from
{}.tap do |publisher_metadata|
publisher_metadata[:published] = published
publisher_metadata[:last_updated] = updated
publisher_metadata[:link_to_history] = !!updated
publisher_metadata[:other] = {
from: from,
}
}

if include_collections_in_other_publisher_metadata
publisher_metadata[:other][:collections] = links('document_collections')
end
end
end
end
end
2 changes: 2 additions & 0 deletions app/presenters/content_item_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class ContentItemPresenter
:document_type,
:taxons

attr_accessor :include_collections_in_other_publisher_metadata

def initialize(content_item, requested_content_item_path = nil)
@content_item = content_item
@requested_content_item_path = requested_content_item_path
Expand Down
33 changes: 31 additions & 2 deletions app/views/components/_publisher-metadata.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,39 @@
<%
values ||= []
values = Array(values)
toggle_id = "app-c-publisher-metadata__definition--#{title.to_s.parameterize(separator: '-')}"
%>
<% if values.any? %>
<dt class="app-c-publisher-metadata__term"><%= title %>: </dt>
<dd class="app-c-publisher-metadata__definition"><%= values.to_sentence.html_safe %></dd>
<dt class="app-c-publisher-metadata__term">
<%= t(title.to_s.parameterize(separator: '_'),
scope: 'components.publisher_metadata',
default: title.to_s.humanize) %>:
</dt>
<dd class="app-c-publisher-metadata__definition" data-module="gem-toggle">
<% if title.to_sym == :collections %>
<% if values.size <= 2 %>
<span class="app-c-publisher-metadata__definition_sentence">
<%= values.take(2).to_sentence.html_safe %>
</span>
<% else %>
<% featured_value, *other_values = values %>
<span class="app-c-publisher-metadata__definition_sentence">
<%= featured_value.html_safe %> and <%= pluralize(other_values.size, 'other') %>
</span>
<a href="#"
data-controls="<%= toggle_id %>"
data-expanded="false"
data-toggled-text="&minus; <%= t('components.publisher_metadata.hide_all') %>">
+ <%= t('components.publisher_metadata.show_all') %>
</a>
<span id="<%= toggle_id %>" class="js-hidden"><%= other_values.to_sentence.html_safe %></span>
<% end %>
<% else %>
<span class="app-c-publisher-metadata__definition_sentence">
<%= values.to_sentence.html_safe %>
</span>
<% end %>
</dd>
<% end %>
<% end %>
</dl>
Expand Down
26 changes: 22 additions & 4 deletions app/views/components/docs/publisher-metadata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ examples:
published: 31 July 2017
last_updated: 20 September 2017
other:
From:
from:
- <a href='/government/organisations/ministry-of-defence'>Ministry of Defence</a>
with_history_link:
data:
published: 31 July 2017
last_updated: 20 September 2017
link_to_history: true
other:
From:
from:
- <a href='/government/organisations/ministry-of-defence'>Ministry of Defence</a>
no_last_updated:
data:
Expand All @@ -37,18 +37,36 @@ examples:
published: 31 July 2017
last_updated: 20 September 2017
other:
From:
from:
- <a href='/government/organisations/department-for-education'>Department for Education</a>
- <a href='/government/organisations/education-funding-agency'>Education Funding Agency</a>
multiple_publishers_and_no_link_to_history:
data:
published: 31 July 2017
last_updated: 20 September 2017
other:
From:
from:
- <a href='/government/organisations/department-for-education'>Department for Education</a>
- <a href='/government/organisations/education-funding-agency'>Education Funding Agency</a>
- <a href='/government/organisations/department-for-work-pensions'>Department for Work and Pensions</a>
one_collection:
data:
other:
collections:
- <a href='/government/collections/tribunals-statistics'>Tribunals statistics</a>
two_collections:
data:
other:
collections:
- <a href='/government/collections/tribunals-statistics'>Tribunals statistics</a>
- <a href='/government/collections/civil-justice-statistics-quarterly'>Civil justice statistics quarterly</a>
more_than_two_collections:
data:
other:
collections:
- <a href='/government/collections/tribunals-statistics'>Tribunals statistics</a>
- <a href='/government/collections/civil-justice-statistics-quarterly'>Civil justice statistics quarterly</a>
- <a href='/government/collections/offender-management-statistics-quarterly'>Offender management statistics quarterly</a>
nothing:
description: |
If an `other` object is provided with keys that have no values the component does not render.
Expand Down
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ en:
common:
last_updated: "Last updated"
components:
publisher_metadata:
from: "From"
collections: "Collections"
show_all: "show all"
hide_all: "hide all"
related_navigation:
collections: "Collection"
external_links: "Elsewhere on the web"
Expand Down
27 changes: 22 additions & 5 deletions test/components/publisher_metadata_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def component_name
end

test "does not render when an 'other' object is provided without any values" do
assert_empty render_component(other: { From: [] })
assert_empty render_component(other: { from: [] })
assert_empty render_component(other: { a: false, b: "", c: [], d: {}, e: nil })
end

Expand All @@ -23,13 +23,13 @@ def component_name


test "renders two from links when two publishers are given" do
render_component(other: { From: ["<a href='/government/organisations/ministry-of-defence'>Ministry of Defence</a>", "<a href='/government/organisations/education-funding-agency'>Education Funding Agency</a>"] })
render_component(other: { from: ["<a href='/government/organisations/ministry-of-defence'>Ministry of Defence</a>", "<a href='/government/organisations/education-funding-agency'>Education Funding Agency</a>"] })
assert_select ".app-c-publisher-metadata__other a[href='/government/organisations/ministry-of-defence']", text: 'Ministry of Defence'
assert_select ".app-c-publisher-metadata__other a[href='/government/organisations/education-funding-agency']", text: 'Education Funding Agency'
end

test "renders a sentence when multiple publishers are given" do
render_component(other: { From: ["<a href='/government/organisations/department-for-education'>Department for Education</a>", "<a href='/government/organisations/education-funding-agency'>Education Funding Agency</a>"] })
render_component(other: { from: ["<a href='/government/organisations/department-for-education'>Department for Education</a>", "<a href='/government/organisations/education-funding-agency'>Education Funding Agency</a>"] })
assert_select ".app-c-publisher-metadata__other dt", text: 'From:'
assert_select ".app-c-publisher-metadata__other dd", text: 'Department for Education and Education Funding Agency'
end
Expand All @@ -45,15 +45,32 @@ def component_name
end

test "renders full metadata component when all parameters are given" do
render_component(other: { From: ["<a href='/government/organisations/ministry-of-defence'>Ministry of Defence</a>"] }, published: "31 July 2017", last_updated: "20 September 2017", link_to_history: true)
render_component(other: { from: ["<a href='/government/organisations/ministry-of-defence'>Ministry of Defence</a>"] }, published: "31 July 2017", last_updated: "20 September 2017", link_to_history: true)
assert_select ".app-c-published-dates"
assert_select ".app-c-publisher-metadata__other a[href='/government/organisations/ministry-of-defence']", text: 'Ministry of Defence'
assert_select ".app-c-publisher-metadata__other dt", text: 'From:'
assert_select ".app-c-publisher-metadata__other dd", text: 'Ministry of Defence'
end

test "link tracking is enabled" do
render_component(other: { From: ["<a href='/government/organisations/ministry-of-defence'>Ministry of Defence</a>"] })
render_component(other: { from: ["<a href='/government/organisations/ministry-of-defence'>Ministry of Defence</a>"] })
assert_select ".app-c-publisher-metadata__other dl[data-module='track-click']"
end

test "renders two collection links when two collections are given" do
render_component(other: { collections: ["<a href='/government/collections/tribunals-statistics'>Tribunals statistics</a>", "<a href='/government/collections/civil-justice-statistics-quarterly'>Civil justice statistics quarterly</a>"] })
assert_select ".app-c-publisher-metadata__other dt", text: 'Collections:'
assert_select ".app-c-publisher-metadata__other dd a[href='/government/collections/tribunals-statistics']", text: 'Tribunals statistics'
assert_select ".app-c-publisher-metadata__other dd a[href='/government/collections/civil-justice-statistics-quarterly']", text: 'Civil justice statistics quarterly'
end

test "renders the first collection and a toggle link when more than two collections are given" do
render_component(other: { collections: ["<a href='/government/collections/tribunals-statistics'>Tribunals statistics</a>", "<a href='/government/collections/civil-justice-statistics-quarterly'>Civil justice statistics quarterly</a>", "<a href='/government/collections/offender-management-statistics-quarterly'>Offender management statistics quarterly</a>"] })
assert_select ".app-c-publisher-metadata__other dt", text: 'Collections:'
assert_select ".app-c-publisher-metadata__other .app-c-publisher-metadata__definition_sentence", text: 'Tribunals statistics and 2 others'
assert_select ".app-c-publisher-metadata__other a", text: '+ show all'
assert_select ".app-c-publisher-metadata__other a[href='/government/collections/tribunals-statistics']", text: 'Tribunals statistics'
assert_select ".app-c-publisher-metadata__other a[href='/government/collections/civil-justice-statistics-quarterly']", text: 'Civil justice statistics quarterly'
assert_select ".app-c-publisher-metadata__other a[href='/government/collections/offender-management-statistics-quarterly']", text: 'Offender management statistics quarterly'
end
end
23 changes: 23 additions & 0 deletions test/integration/content_pages_related_navigation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,29 @@ def setup
refute page.has_css?('.gem-c-related-navigation__sub-heading', text: 'Collection')
end

test "ContentPagesNav variant A does not show collections as publisher metadata" do
stub_rummager
setup_variant_a

setup_and_visit_content_item_with_taxons('case_study', SINGLE_TAXON)

within '.app-c-publisher-metadata' do
refute page.has_css?('.app-c-publisher-metadata__term', text: 'Collections')
end
end

test "ContentPagesNav variant B shows collections as publisher metadata" do
stub_rummager
setup_variant_b

setup_and_visit_content_item_with_taxons('case_study', SINGLE_TAXON)

within '.app-c-publisher-metadata' do
assert page.has_css?('.app-c-publisher-metadata__term', text: 'Collections')
assert page.has_css?('.app-c-publisher-metadata__definition a', text: 'Work Programme real life stories')
end
end

def setup_variant_a
ContentItemsController.any_instance.stubs(:show_new_navigation?).returns(false)
end
Expand Down

0 comments on commit 4e4b894

Please sign in to comment.