Skip to content

Commit

Permalink
Simplify news and services
Browse files Browse the repository at this point in the history
We're now only retrieving 3 items per section so we can delete a lot of code
from the news_and_communications and services presenters and get them to return
an array of results rather than hashes.

This allows us to get them to behave a bit more like the other presenters.
  • Loading branch information
sihugh committed Aug 14, 2018
1 parent 0c02ad2 commit c3554a4
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 114 deletions.
27 changes: 4 additions & 23 deletions app/presenters/supergroups/news_and_communications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,19 @@ def initialize(current_path, taxon_ids, filters)
end

def tagged_content
return unless @content.any?
{
documents: documents,
promoted_content: promoted_content,
}
format_document_data(@content)
end

private

def documents
items = @content.drop(promoted_content_count)
format_document_data(items)
end

def promoted_content
items = @content.take(promoted_content_count)
format_document_data(items, data_category: "ImageCardClicked")
end

private

def format_document_data(documents, data_category: nil)
def format_document_data(documents)
# Start with_index at 1 to help align analytics
documents.each.with_index(1).map do |document, index|
data = {
link: {
text: document["title"],
path: document["link"],
data_attributes: data_attributes(document["link"], document["title"], index, data_category)
data_attributes: data_attributes(document["link"], document["title"], index, "ImageCardClicked")
},
metadata: {
document_type: document_type(document),
Expand All @@ -52,10 +37,6 @@ def format_document_data(documents, data_category: nil)
end
end

def promoted_content_count
3
end

def image_url(document)
document["image_url"] || PLACEHOLDER_IMAGE
end
Expand Down
36 changes: 1 addition & 35 deletions app/presenters/supergroups/services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,7 @@ def initialize(current_path, taxon_ids, filters)
end

def tagged_content
return unless @content.any?
{
documents: documents,
promoted_content: promoted_content,
}
end

private

def documents
items = @content.drop(promoted_content_count)
format_document_data(items)
end

def promoted_content
items = @content.take(promoted_content_count)
format_document_data(items, "HighlightBoxClicked")
end

def promoted_content_count
3
end

def format_document_data(documents, data_category = "DocumentListClicked")
documents.each.with_index(1)&.map do |document, index|
data = {
link: {
text: document["title"],
path: document["link"],
data_attributes: data_attributes(document["link"], document["title"], index, data_category),
}
}

data
end
format_document_data(@content, data_category: "HighlightBoxClicked")
end
end
end
1 change: 1 addition & 0 deletions app/presenters/supergroups/supergroup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def tagged_content
private

def format_document_data(documents, data_category: nil, include_timestamp: true)
# Start with_index at 1 to help align analytics
documents.each.with_index(1).map do |document, index|
data = {
link: {
Expand Down
13 changes: 4 additions & 9 deletions app/views/shared/_taxonomy_navigation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

<%= render "govuk_publishing_components/components/highlight_boxes", {
inverse: true,
items: taxonomy_navigation[:services][:promoted_content]
items: taxonomy_navigation[:services]
} %>
</div>
<% end %>
Expand Down Expand Up @@ -72,9 +72,8 @@
margin_bottom: 2
} %>

<% taxonomy_navigation[:news_and_communications][:promoted_content].in_groups_of(3, false) do |promo_group| %>
<div class="grid-row">
<% promo_group.each do |promo| %>
<div class="grid-row">
<% taxonomy_navigation[:news_and_communications].each do |promo| %>
<div class="column-one-third">
<%= render "govuk_publishing_components/components/image_card", {
context: promo[:image][:context],
Expand All @@ -86,11 +85,7 @@
} %>
</div>
<% end %>
</div>
<% end %>
<%= render "govuk_publishing_components/components/document_list", {
items: taxonomy_navigation[:news_and_communications][:documents]
} %>
</div>
</div>
<% end %>

Expand Down
5 changes: 0 additions & 5 deletions test/integration/content_pages_navigation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,6 @@ def setup

setup_and_visit_content_item_with_taxons('guide', taxons)

assert page.has_css?('h3', text: "News and communications")
assert page.has_css?('.gem-c-image-card__title', text: 'Free school meals form')
assert page.has_css?('.gem-c-image-card__title-link[data-track-category="newsAndCommunicationsImageCardClicked"]', text: 'Free school meals form')
assert page.has_css?('.gem-c-image-card__title-link[data-track-action="1"]', text: 'Free school meals form')
assert page.has_css?('.gem-c-image-card__title-link[data-track-label="/government/publications/meals"]', text: 'Free school meals form')
assert_has_news_and_communications_section
end

Expand Down
39 changes: 8 additions & 31 deletions test/presenters/supergroups/news_and_communications_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,32 @@ class NewsAndCommunicationsTest < ActiveSupport::TestCase

test "finds no results if taxon ids is a blank array" do
news_and_comms = Supergroups::NewsAndCommunications.new("/a-random-path", [], {})
assert_nil news_and_comms.tagged_content
assert_equal [], news_and_comms.tagged_content
end

test "finds no results if there are taxon ids but no results" do
taxon_content_ids = ['any-old-taxon', 'some-other-taxon-id']

stub_most_recent_content("/a-random-path", taxon_content_ids, 0, "news_and_communications")
news_and_comms = Supergroups::NewsAndCommunications.new("/a-random-path", taxon_content_ids, {})
assert_nil news_and_comms.tagged_content
assert_equal [], news_and_comms.tagged_content
end

test "finds 2 featured items and 0 normal items with 2 results" do
test "Presents 2 items with 2 results" do
taxon_content_ids = ['any-old-taxon', 'some-other-taxon-id']
stub_most_recent_content("/a-random-path", taxon_content_ids, 2, "news_and_communications")
stub_content_store_items(2)

news_and_comms = Supergroups::NewsAndCommunications.new("/a-random-path", taxon_content_ids, {})

assert_equal 0, news_and_comms.tagged_content[:documents].count
assert_equal 2, news_and_comms.tagged_content[:promoted_content].count
end

test "finds 3 promoted items and 2 normal items if there are enough results" do
taxon_content_ids = ['any-old-taxon', 'some-other-taxon-id']
stub_most_recent_content("/a-random-path", taxon_content_ids, 5, "news_and_communications")
stub_content_store_items(3)

news_and_comms = Supergroups::NewsAndCommunications.new("/a-random-path", taxon_content_ids, {})

assert_equal 2, news_and_comms.tagged_content[:documents].count
assert_equal 3, news_and_comms.tagged_content[:promoted_content].count
assert_equal 2, news_and_comms.tagged_content.count
end

test "promoted content includes placeholder images if the content doesn't have one" do
placeholder_image = "https://assets.publishing.service.gov.uk/government/assets/placeholder.jpg"

taxon_content_ids = ['any-old-taxon']
stub_most_recent_content("/a-random-path", taxon_content_ids, 1, "news_and_communications")
placeholder_image = "http://static.test.gov.uk/government/assets/placeholder.jpg"
stub_rummager_document_without_image_url

news_and_comms = Supergroups::NewsAndCommunications.new("/a-random-path", taxon_content_ids, {})

news_item = news_and_comms.tagged_content[:promoted_content].first
news_and_comms = Supergroups::NewsAndCommunications.new("/a-random-path", ['any-old-taxon'], {})
news_item = news_and_comms.tagged_content.first

assert_equal news_item[:image][:url], placeholder_image
end
Expand All @@ -57,14 +40,8 @@ class NewsAndCommunicationsTest < ActiveSupport::TestCase
stub_most_recent_content("/a-random-path", taxon_content_ids, 1, "news_and_communications")

news_and_comms = Supergroups::NewsAndCommunications.new("/a-random-path", taxon_content_ids, {})
news_item = news_and_comms.tagged_content[:promoted_content].first
news_item = news_and_comms.tagged_content.first

assert_equal news_item[:image][:url], "https://assets.testing.gov.uk/awesome-pic.jpg"
end

def stub_content_store_items(count)
count.times.map do |i|
content_store_has_item("/content-item-#{i}")
end
end
end
12 changes: 5 additions & 7 deletions test/presenters/supergroups/services_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ class ServicesTest < ActiveSupport::TestCase

test "services returns no results if taxon ids is a blank array" do
services = Supergroups::Services.new("/a-random-path", [], {})
assert_nil services.tagged_content
assert_equal [], services.tagged_content
end

test "services returns no results if there are taxon ids but no results" do
taxon_content_ids = ['any-old-taxon', 'some-other-taxon-id']

stub_most_popular_content("/a-random-path", taxon_content_ids, 0, "services")
services = Supergroups::Services.new("/a-random-path", taxon_content_ids, {})
assert_nil services.tagged_content
assert_equal [], services.tagged_content
end

test "tagged_content returns hash with with 2 featured items and 0 normal items with 2 results" do
Expand All @@ -23,18 +23,16 @@ class ServicesTest < ActiveSupport::TestCase

services = Supergroups::Services.new("/a-random-path", taxon_content_ids, {})

assert_equal 0, services.tagged_content[:documents].count
assert_equal 2, services.tagged_content[:promoted_content].count
assert_equal 2, services.tagged_content.count
end

test "tagged_content returns hash with with 3 featured items and 2 normal items if there are enough results" do
taxon_content_ids = ['any-old-taxon', 'some-other-taxon-id']

stub_most_popular_content("/a-random-path", taxon_content_ids, 5, "services")
stub_most_popular_content("/a-random-path", taxon_content_ids, 3, "services")

services = Supergroups::Services.new("/a-random-path", taxon_content_ids, {})

assert_equal 2, services.tagged_content[:documents].count
assert_equal 3, services.tagged_content[:promoted_content].count
assert_equal 3, services.tagged_content.count
end
end
4 changes: 0 additions & 4 deletions test/support/content_pages_nav_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,5 @@ def assert_has_news_and_communications_section
assert page.has_css?('.gem-c-image-card__title-link[data-track-category="newsAndCommunicationsImageCardClicked"]', text: 'Free school meals form')
assert page.has_css?('.gem-c-image-card__title-link[data-track-action="1"]', text: 'Free school meals form')
assert page.has_css?('.gem-c-image-card__title-link[data-track-label="/government/publications/meals"]', text: 'Free school meals form')

assert page.has_css?('.gem-c-document-list__item a[data-track-category="newsAndCommunicationsDocumentListClicked"]', text: 'Free school meals form')
assert page.has_css?('.gem-c-document-list__item a[data-track-action="1"]', text: 'Free school meals form')
assert page.has_css?('.gem-c-document-list__item a[data-track-label="/government/publications/meals"]', text: 'Free school meals form')
end
end

0 comments on commit c3554a4

Please sign in to comment.