govuk_navigation_helpers 7.0.0 → 7.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ec1764a33f8094c760cd5a83e47ae8a51277b119
4
- data.tar.gz: f28cae4e16260780344f7f77c5d0043f569d628e
3
+ metadata.gz: 3017d40e8156702b7e7af8aa4872cf2e34257772
4
+ data.tar.gz: 1ecf2847c2c9b062423c3edd50ab2eec59483d8e
5
5
  SHA512:
6
- metadata.gz: 91bade4008ffcbbaf9c51dbd2a86a93ab474b0b5fc8c9038113016e7ffc6d9de9b59e227629d000e1ded82944779534f58c3aefe72b6acd7c746fda3ce00e4d6
7
- data.tar.gz: 865b85c47eeed422b1960e5874b391f8f298d8a01a686ddbb8699fec6ab35246ecfd89750a9534f9f227a13e9661ca6c4143d0483946406fd8093de665724592
6
+ metadata.gz: faad2926bd20b3c53af19202469eecc0af10088292226e2bfb9dffc9b9620fe1fb0b36f6c9fcfcef93e6b9aa3c0f3885de823e8cc3abb3e9afcc09782275497f
7
+ data.tar.gz: cdf11771a20dc7711c69414bd4a88cf3a8a736ffa16fcbb3c3e6196fd13fde836591569c3432e281570f747f570a90c31d2aace8fe648f4389ebbf3fedf50a90
@@ -1,3 +1,7 @@
1
+ ## 7.1.0
2
+
3
+ * Add helper for rendering related navigation sidebar on content pages with universal layout
4
+
1
5
  ## 7.0.0
2
6
 
3
7
  * Root taxons are now whitelisted, so that only taxons descendant from one of
@@ -3,6 +3,7 @@ require "govuk_navigation_helpers/breadcrumbs"
3
3
  require "govuk_navigation_helpers/related_items"
4
4
  require "govuk_navigation_helpers/taxon_breadcrumbs"
5
5
  require "govuk_navigation_helpers/taxonomy_sidebar"
6
+ require "govuk_navigation_helpers/related_navigation_sidebar"
6
7
  require "govuk_navigation_helpers/rummager_taxonomy_sidebar_links"
7
8
  require "govuk_navigation_helpers/curated_taxonomy_sidebar_links"
8
9
 
@@ -45,6 +46,15 @@ module GovukNavigationHelpers
45
46
  RelatedItems.new(content_item).related_items
46
47
  end
47
48
 
49
+ # Generate a payload containing related navigation sidebar data. Intended for use with
50
+ # the related navigation sidebar component
51
+ #
52
+ # @return [Hash] Payload for the GOV.UK related navigation sidebar component
53
+ # @see https://government-frontend.herokuapp.com/component-guide/related-navigation-sidebar
54
+ def related_navigation_sidebar
55
+ RelatedNavigationSidebar.new(content_item).related_navigation_sidebar
56
+ end
57
+
48
58
  private
49
59
 
50
60
  attr_reader :content_item
@@ -77,6 +77,34 @@ module GovukNavigationHelpers
77
77
  end
78
78
  end
79
79
 
80
+ def related_ordered_items
81
+ content_store_response.dig("links", "ordered_related_items").to_a
82
+ end
83
+
84
+ def quick_links
85
+ content_store_response.dig("details", "quick_links").to_a
86
+ end
87
+
88
+ def related_collections
89
+ filter_link_type(content_store_response.dig("links", "document_collections").to_a, "document_collection")
90
+ end
91
+
92
+ def related_other_contacts
93
+ filter_link_type(content_store_response.dig("links", "related").to_a, "contact")
94
+ end
95
+
96
+ def related_organisations
97
+ filter_link_type(content_store_response.dig("links", "organisations").to_a, "organisation")
98
+ end
99
+
100
+ def related_policies
101
+ filter_link_type(content_store_response.dig("links", "related_policies").to_a, "policy")
102
+ end
103
+
104
+ def related_topics
105
+ filter_link_type(content_store_response.dig("links", "topics").to_a, "topic")
106
+ end
107
+
80
108
  def external_links
81
109
  content_store_response.dig("details", "external_related_links").to_a
82
110
  end
@@ -123,5 +151,11 @@ module GovukNavigationHelpers
123
151
  get_root_taxon(parent_taxons.first)
124
152
  end
125
153
  end
154
+
155
+ def filter_link_type(links, type)
156
+ links.select do |link|
157
+ link["document_type"] == type
158
+ end
159
+ end
126
160
  end
127
161
  end
@@ -0,0 +1,70 @@
1
+ require 'govuk_navigation_helpers/services'
2
+ require 'govuk_navigation_helpers/configuration'
3
+
4
+ module GovukNavigationHelpers
5
+ class RelatedNavigationSidebar
6
+ def initialize(content_item)
7
+ @content_item = ContentItem.new content_item
8
+ end
9
+
10
+ def related_navigation_sidebar
11
+ {
12
+ related_items: related_items,
13
+ collections: related_collections,
14
+ topics: related_topics,
15
+ policies: related_policies,
16
+ publishers: related_organisations,
17
+ other: [related_external_links, related_contacts]
18
+ }
19
+ end
20
+
21
+ private
22
+
23
+ def build_links_for_sidebar(collection, path_key = "base_path", additional_attr = {})
24
+ collection.map do |link|
25
+ {
26
+ text: link["title"],
27
+ path: link[path_key]
28
+ }.merge(additional_attr)
29
+ end
30
+ end
31
+
32
+ def related_items
33
+ quick_links = build_links_for_sidebar(@content_item.quick_links, "url")
34
+
35
+ quick_links.any? ? quick_links : build_links_for_sidebar(@content_item.related_ordered_items)
36
+ end
37
+
38
+ def related_organisations
39
+ build_links_for_sidebar(@content_item.related_organisations)
40
+ end
41
+
42
+ def related_collections
43
+ build_links_for_sidebar(@content_item.related_collections)
44
+ end
45
+
46
+ def related_policies
47
+ build_links_for_sidebar(@content_item.related_policies)
48
+ end
49
+
50
+ def related_topics
51
+ build_links_for_sidebar(@content_item.related_topics)
52
+ end
53
+
54
+ def related_contacts
55
+ return [] unless @content_item.related_other_contacts.any?
56
+ [
57
+ title: "Other contacts",
58
+ links: build_links_for_sidebar(@content_item.related_other_contacts).map
59
+ ]
60
+ end
61
+
62
+ def related_external_links
63
+ return [] unless @content_item.external_links.any?
64
+ [
65
+ title: "Elsewhere on the web",
66
+ links: build_links_for_sidebar(@content_item.external_links, "url", rel: 'external')
67
+ ]
68
+ end
69
+ end
70
+ end
@@ -1,3 +1,3 @@
1
1
  module GovukNavigationHelpers
2
- VERSION = "7.0.0".freeze
2
+ VERSION = "7.1.0".freeze
3
3
  end
@@ -0,0 +1,139 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe GovukNavigationHelpers::RelatedNavigationSidebar do
4
+ def payload_for(schema, content_item)
5
+ generator = GovukSchemas::RandomExample.for_schema(schema, schema_type: "frontend")
6
+ fully_valid_content_item = generator.merge_and_validate(content_item)
7
+ GovukNavigationHelpers::NavigationHelper.new(fully_valid_content_item).related_navigation_sidebar
8
+ end
9
+
10
+ describe "#related-navigation-sidebar" do
11
+ it "can handle randomly generated content" do
12
+ generator = GovukSchemas::RandomExample.for_schema("placeholder", schema_type: "frontend")
13
+
14
+ expect { payload_for("placeholder", generator.payload) }.to_not raise_error
15
+ end
16
+
17
+ it "returns empty arrays if there are no related navigation sidebar links" do
18
+ nothing = payload_for("placeholder",
19
+ "details" => {
20
+ "external_related_links" => []
21
+ },
22
+ "links" => {
23
+ }
24
+ )
25
+
26
+ expected = {
27
+ related_items: [],
28
+ collections: [],
29
+ topics: [],
30
+ policies: [],
31
+ publishers: [],
32
+ other: [[], []]
33
+ }
34
+
35
+ expect(nothing).to eql(expected)
36
+ end
37
+
38
+ it "extracts and returns the appropriate related links" do
39
+ payload = payload_for("speech",
40
+ "details" => {
41
+ "body" => "body",
42
+ "government" => {
43
+ "title" => "government",
44
+ "slug" => "government",
45
+ "current" => true
46
+ },
47
+ "political" => true,
48
+ "delivered_on" => "2017-09-22T14:30:00+01:00"
49
+ },
50
+ "links" => {
51
+ "ordered_related_items" => [
52
+ {
53
+ "content_id" => "32c1b93d-2553-47c9-bc3c-fc5b513ecc32",
54
+ "locale" => "en",
55
+ "base_path" => "/related-item",
56
+ "title" => "related item"
57
+ }
58
+ ],
59
+ "document_collections" => [
60
+ {
61
+ "content_id" => "32c1b93d-2553-47c9-bc3c-fc5b513ecc32",
62
+ "locale" => "en",
63
+ "base_path" => "/related-collection",
64
+ "title" => "related collection",
65
+ "document_type" => "document_collection"
66
+ }
67
+ ],
68
+ "topics" => [
69
+ {
70
+ "content_id" => "32c1b93d-2553-47c9-bc3c-fc5b513ecc32",
71
+ "locale" => "en",
72
+ "base_path" => "/related-topic",
73
+ "title" => "related topic",
74
+ "document_type" => "topic"
75
+ }
76
+ ],
77
+ "organisations" => [
78
+ {
79
+ "content_id" => "32c1b93d-2553-47c9-bc3c-fc5b513ecc32",
80
+ "locale" => "en",
81
+ "base_path" => "/related-organisation",
82
+ "title" => "related organisation",
83
+ "document_type" => "organisation"
84
+ }
85
+ ],
86
+ "related_policies" => [
87
+ {
88
+ "content_id" => "32c1b93d-2553-47c9-bc3c-fc5b513ecc32",
89
+ "locale" => "en",
90
+ "base_path" => "/related-policy",
91
+ "title" => "related policy",
92
+ "document_type" => "policy"
93
+ }
94
+ ]
95
+ }
96
+ )
97
+
98
+ expected = {
99
+ related_items: [{ path: "/related-item", text: "related item" }],
100
+ collections: [{ path: "/related-collection", text: "related collection" }],
101
+ topics: [{ path: "/related-topic", text: "related topic" }],
102
+ policies: [{ path: "/related-policy", text: "related policy" }],
103
+ publishers: [{ path: "/related-organisation", text: "related organisation" }],
104
+ other: [[], []]
105
+ }
106
+
107
+ expect(payload).to eql(expected)
108
+ end
109
+
110
+ it "returns an Elsewhere on the web section for external related links" do
111
+ payload = payload_for("placeholder",
112
+ "details" => {
113
+ "external_related_links" => [
114
+ {
115
+ "title" => "external-link",
116
+ "url" => "https://external"
117
+ }
118
+ ]
119
+ },
120
+ )
121
+
122
+ expected = [
123
+ [
124
+ {
125
+ title: "Elsewhere on the web",
126
+ links: [{
127
+ text: "external-link",
128
+ path: "https://external",
129
+ rel: "external"
130
+ }]
131
+ }
132
+ ],
133
+ []
134
+ ]
135
+
136
+ expect(payload[:other]).to eql(expected)
137
+ end
138
+ end
139
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk_navigation_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.0.0
4
+ version: 7.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GOV.UK Dev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-17 00:00:00.000000000 Z
11
+ date: 2017-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gds-api-adapters
@@ -176,6 +176,7 @@ files:
176
176
  - lib/govuk_navigation_helpers/curated_taxonomy_sidebar_links.rb
177
177
  - lib/govuk_navigation_helpers/grouped_related_links.rb
178
178
  - lib/govuk_navigation_helpers/related_items.rb
179
+ - lib/govuk_navigation_helpers/related_navigation_sidebar.rb
179
180
  - lib/govuk_navigation_helpers/rummager_taxonomy_sidebar_links.rb
180
181
  - lib/govuk_navigation_helpers/services.rb
181
182
  - lib/govuk_navigation_helpers/taxon_breadcrumbs.rb
@@ -183,6 +184,7 @@ files:
183
184
  - lib/govuk_navigation_helpers/version.rb
184
185
  - spec/breadcrumbs_spec.rb
185
186
  - spec/content_item_spec.rb
187
+ - spec/navigation_sidebar_spec.rb
186
188
  - spec/related_items_spec.rb
187
189
  - spec/spec_helper.rb
188
190
  - spec/taxon_breadcrumbs_spec.rb
@@ -214,6 +216,7 @@ summary: Gem to transform items from the content-store into payloads for GOV.UK
214
216
  test_files:
215
217
  - spec/breadcrumbs_spec.rb
216
218
  - spec/content_item_spec.rb
219
+ - spec/navigation_sidebar_spec.rb
217
220
  - spec/related_items_spec.rb
218
221
  - spec/spec_helper.rb
219
222
  - spec/taxon_breadcrumbs_spec.rb