Skip to content

Commit

Permalink
Merge pull request #994 from alphagov/most_recent_policy_and_engagement
Browse files Browse the repository at this point in the history
Add supergroup sections to taxonomy navigation
  • Loading branch information
sihugh authored Jul 27, 2018
2 parents de4c0a4 + ca92c35 commit efac9eb
Show file tree
Hide file tree
Showing 13 changed files with 423 additions and 3 deletions.
6 changes: 5 additions & 1 deletion app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@ def load_taxonomy_navigation

taxon_ids = taxons.map { |taxon| taxon["content_id"] }
services = Supergroups::Services.new(content_item_path, taxon_ids)
guidance_and_regulation = Supergroups::GuidanceAndRegulation.new(content_item_path, taxon_ids)
policy_and_engagement = Supergroups::PolicyAndEngagement.new(content_item_path, taxon_ids)

@taxonomy_navigation = {
services: (services.all_services if services.any_services?),
services: (services.all_services if services.any_services?),
guidance_and_regulation: guidance_and_regulation.tagged_content,
policy_and_engagement: policy_and_engagement.tagged_content,
}

@tagged_taxons = taxons.map do |taxon|
Expand Down
20 changes: 20 additions & 0 deletions app/presenters/supergroups/guidance_and_regulation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Supergroups
class GuidanceAndRegulation < Supergroup
def initialize(current_path, taxon_ids)
@current_path = current_path
@taxon_ids = taxon_ids
@content = fetch_content
end

def tagged_content
format_document_data(@content, include_timestamp: false)
end

private

def fetch_content
return [] unless @taxon_ids.any?
MostPopularContent.fetch(content_ids: @taxon_ids, current_path: @current_path, filter_content_purpose_supergroup: "guidance_and_regulation")
end
end
end
20 changes: 20 additions & 0 deletions app/presenters/supergroups/policy_and_engagement.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Supergroups
class PolicyAndEngagement < Supergroup
def initialize(current_path, taxon_ids)
@current_path = current_path
@taxon_ids = taxon_ids
@content = fetch_content
end

def tagged_content
format_document_data(@content)
end

private

def fetch_content
return [] unless @taxon_ids.any?
MostRecentContent.fetch(content_ids: @taxon_ids, current_path: @current_path, filter_content_purpose_supergroup: "policy_and_engagement")
end
end
end
45 changes: 45 additions & 0 deletions app/presenters/supergroups/supergroup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Supergroups
class Supergroup
private

def format_document_data(documents, data_category: nil, include_timestamp: true)
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)
},
metadata: {
document_type: document["content_store_document_type"].humanize
}
}

if include_timestamp && document["public_timestamp"]
data[:metadata][:public_updated_at] = Date.parse(document["public_timestamp"])
end

if data_category.present?
data[:link][:data_attributes][:track_category] = data_module_label + data_category
end

data
end
end

def data_attributes(base_path, link_text, index)
{
track_category: data_module_label + "DocumentListClicked",
track_action: index,
track_label: base_path,
track_options: {
dimension29: link_text
}
}
end

def data_module_label
self.class.name.demodulize.camelize(:lower)
end
end
end
38 changes: 38 additions & 0 deletions app/services/most_recent_content.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class MostRecentContent
attr_reader :content_id, :current_path, :filter_taxon, :number_of_links

def initialize(content_ids:, current_path:, filter_content_purpose_supergroup:, number_of_links: 5)
@content_ids = content_ids
@current_path = current_path
@filter_content_purpose_supergroup = filter_content_purpose_supergroup
@number_of_links = number_of_links
end

def self.fetch(content_ids:, current_path:, filter_content_purpose_supergroup:)
new(content_ids: content_ids, current_path: current_path, filter_content_purpose_supergroup: filter_content_purpose_supergroup).fetch
end

def fetch
search_response["results"]
end

private

def search_response
params = {
start: 0,
count: number_of_links,
fields: %w(title
link
description
content_store_document_type
public_timestamp
organisations),
filter_part_of_taxonomy_tree: @content_ids,
order: '-public_timestamp',
reject_link: current_path,
}
params[:filter_content_purpose_supergroup] = @filter_content_purpose_supergroup if @filter_content_purpose_supergroup.present?
Services.rummager.search(params)
end
end
34 changes: 32 additions & 2 deletions app/views/shared/_taxonomy_navigation.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<% if taxonomy_navigation.values().flatten.any? %>
<div class="taxonomy-navigation">
<div class="taxonomy-navigation" data-module="track-click">
<h2>More in
<% tagged_taxons.each do |tagged_taxon| %>
<a class="taxonomy-navigation__title-link" href="<%= tagged_taxon[:taxon_link] %>"><%= tagged_taxon[:taxon_name] %></a>
<% end %>
</h2>

<% if taxonomy_navigation[:services].any? %>
<% if taxonomy_navigation[:services].present? %>
<div class="taxonomy-navigation__section">
<%= render "govuk_publishing_components/components/heading", {
text: t('supergroups.services'),
Expand All @@ -25,5 +25,35 @@
} %>
</div>
<% end %>

<% if taxonomy_navigation[:guidance_and_regulation].present? %>
<div class="taxonomy-navigation__section">
<%= render "govuk_publishing_components/components/heading", {
text: t('supergroups.guidance_and_regulation'),
heading_level: 3,
font_size: 24,
margin_bottom: 2
} %>

<%= render "govuk_publishing_components/components/document_list", {
items: taxonomy_navigation[:guidance_and_regulation]
} %>
</div>
<% end %>

<% if taxonomy_navigation[:policy_and_engagement].present? %>
<div class="taxonomy-navigation__section">
<%= render "govuk_publishing_components/components/heading", {
text: t('supergroups.policy_and_engagement'),
heading_level: 3,
font_size: 24,
margin_bottom: 2
} %>

<%= render "govuk_publishing_components/components/document_list", {
items: taxonomy_navigation[:policy_and_engagement]
} %>
</div>
<% end %>
</div>
<% end %>
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ en:
see_all_updates: "see all updates"
supergroups:
services: "Services"
guidance_and_regulation: "Guidance and regulation"
policy_and_engagement: "Policy and engagement"
see_more_supergroups: "See more %{supergroup}"
language_names:
ar: Arabic
Expand Down
70 changes: 70 additions & 0 deletions test/integration/content_pages_navigation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class ContentPagesNavigationTest < ActionDispatch::IntegrationTest

test "shows the Services section title and documents with tracking" do
stub_rummager
stub_empty_guidance
stub_empty_policies
setup_variant_b

taxons = SINGLE_TAXON
Expand Down Expand Up @@ -85,6 +87,74 @@ class ContentPagesNavigationTest < ActionDispatch::IntegrationTest
refute page.has_css?('h3', text: "Services")
end

test "shows the Policy section title and documents with tracking" do
stub_rummager
stub_empty_services
stub_empty_guidance
setup_variant_b

taxons = SINGLE_TAXON

setup_and_visit_content_item_with_taxons('guide', taxons)

assert page.has_css?('h3', text: "Policy and engagement")

assert page.has_css?('.gem-c-document-list__item a[data-track-category="policyAndEngagementDocumentListClicked"]', 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

test "does not show the Policy section if there is no tagged content" do
stub_empty_rummager
setup_variant_b

taxons = SINGLE_TAXON

setup_and_visit_content_item_with_taxons('guide', taxons)

refute page.has_css?('h3', text: "Policy and engagement")
end

test "shows the Guidance section title and documents with tracking" do
stub_rummager
stub_empty_services
stub_empty_policies
setup_variant_b

taxons = SINGLE_TAXON

setup_and_visit_content_item_with_taxons('guide', taxons)

assert page.has_css?('h3', text: "Guidance and regulation")

assert page.has_css?('.gem-c-document-list__item a[data-track-category="guidanceAndRegulationDocumentListClicked"]', 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

test "does not show the Guidance section if there is no tagged content" do
stub_empty_rummager
setup_variant_b

taxons = SINGLE_TAXON

setup_and_visit_content_item_with_taxons('guide', taxons)

refute page.has_css?('h3', text: "Guidance and regulation")
end

def stub_empty_services
Supergroups::Services.any_instance.stubs(:all_services).returns({})
end

def stub_empty_guidance
Supergroups::GuidanceAndRegulation.any_instance.stubs(:tagged_content).returns([])
end

def stub_empty_policies
Supergroups::PolicyAndEngagement.any_instance.stubs(:tagged_content).returns([])
end

def setup_variant_a
ContentItemsController.any_instance.stubs(:show_new_navigation?).returns(false)
end
Expand Down
26 changes: 26 additions & 0 deletions test/presenters/supergroups/guidance_and_regulation_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'test_helper'

class GuidanceAndRegulationTest < ActiveSupport::TestCase
include RummagerHelpers

def taxon_content_ids
['c3c860fc-a271-4114-b512-1c48c0f82564', 'ff0e8e1f-4dea-42ff-b1d5-f1ae37807af2']
end

test "tagged_content returns empty array if taxon ids is a blank array" do
guidance_and_regulation = Supergroups::GuidanceAndRegulation.new("/no-particular-page", [])
assert_equal [], guidance_and_regulation.tagged_content
end

test "tagged_content returns empty array if there are taxon ids but no results" do
stub_most_popular_content("/no-particular-page", [], 0, "guidance_and_regulation")
guidance_and_regulation = Supergroups::GuidanceAndRegulation.new("/no-particular-page", [])
assert_equal [], guidance_and_regulation.tagged_content
end

test "tagged_content returns array with 2 items if there are 2 results" do
stub_most_popular_content("/no-particular-page", taxon_content_ids, 2, "guidance_and_regulation")
guidance_and_regulation = Supergroups::GuidanceAndRegulation.new("/no-particular-page", taxon_content_ids)
assert_equal 2, guidance_and_regulation.tagged_content.count
end
end
26 changes: 26 additions & 0 deletions test/presenters/supergroups/policy_and_engagement_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'test_helper'

class PolicyAndEngagementTest < ActiveSupport::TestCase
include RummagerHelpers

def taxon_content_ids
['c3c860fc-a271-4114-b512-1c48c0f82564', 'ff0e8e1f-4dea-42ff-b1d5-f1ae37807af2']
end

test "tagged_content returns empty array if taxon ids is a blank array" do
policy_and_engagement = Supergroups::PolicyAndEngagement.new("/a-random-path", [])
assert_equal [], policy_and_engagement.tagged_content
end

test "tagged_content returns empty array if there are taxon ids but no results" do
stub_most_recent_content("/a-random-path", [], 0, "policy_and_engagement")
policy_and_engagement = Supergroups::PolicyAndEngagement.new("/a-random-path", [])
assert_equal [], policy_and_engagement.tagged_content
end

test "tagged_content returns array with 2 items if there are 2 results" do
stub_most_recent_content("/a-random-path", taxon_content_ids, 2, "policy_and_engagement")
policy_and_engagement = Supergroups::PolicyAndEngagement.new("/a-random-path", taxon_content_ids)
assert_equal 2, policy_and_engagement.tagged_content.count
end
end
43 changes: 43 additions & 0 deletions test/services/most_recent_content_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "test_helper"
require './test/support/custom_assertions.rb'

class MostRecentContentTest < ActiveSupport::TestCase
include RummagerHelpers

def most_recent_content
@most_recent_content ||= MostRecentContent.new(
content_ids: taxon_content_ids,
current_path: "/some-path",
filter_content_purpose_supergroup: "guidance_and_regulation",
number_of_links: 6
)
end

def taxon_content_ids
['c3c860fc-a271-4114-b512-1c48c0f82564', 'ff0e8e1f-4dea-42ff-b1d5-f1ae37807af2']
end

test "orders the results by popularity in descending order" do
assert_includes_params(order: '-public_timestamp') do
most_recent_content.fetch
end
end

test "includes taxon ids" do
assert_includes_params(filter_part_of_taxonomy_tree: taxon_content_ids) do
most_recent_content.fetch
end
end

test "returns number of links" do
assert_includes_params(count: 6) do
most_recent_content.fetch
end
end

test "excludes current page" do
assert_includes_params(reject_link: "/some-path") do
most_recent_content.fetch
end
end
end
15 changes: 15 additions & 0 deletions test/support/custom_assertions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module MiniTest::Assertions
# Behaves a bit like `hash_including`.
# Given a slice of the original hash, it verifies that the original hash
# includes the sub-hash given. Useful when testing partial arguments/params to
# methods.
def assert_includes_subhash(expected_sub_hash, hash)
expected_sub_hash.each do |key, value|
assert_equal(
value,
hash[key],
"Expected #{hash} to include #{key}=#{value}"
)
end
end
end
Loading

0 comments on commit efac9eb

Please sign in to comment.