-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #994 from alphagov/most_recent_policy_and_engagement
Add supergroup sections to taxonomy navigation
- Loading branch information
Showing
13 changed files
with
423 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
test/presenters/supergroups/guidance_and_regulation_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.