govuk_navigation_helpers 6.2.0 → 6.3.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: c3d18101e3d40e2584d480f3aa5690142e1114c2
4
- data.tar.gz: bb33a4f50beebe2d1b20fcd0546032f2360da0fe
3
+ metadata.gz: c461bdaf7894d043e544c90c0a55847e2ef71aeb
4
+ data.tar.gz: b6faea824ddea2a36d627eec6a1f2f60d05fc009
5
5
  SHA512:
6
- metadata.gz: f6b9748dec2db2e5206ee6d99a43da48ec8e0defdca905d792bc6e2e38170b2009468ca92603552f8093a0edb1c305ee0f7745ad05effcb6ccf822cdfc68fc5e
7
- data.tar.gz: 92fe160a5c1860e2299b9760c1eb35cf248292fd449384ab345429f3b0edb65864604cbcb55ecc9bf0e54779674e3940af8e9f535a04e8b7836cc634f5c81a7b
6
+ metadata.gz: 729b456c6c589ea4cbed175c5fd5798a1e1c28f0be3c80f4a329235bf37af534cfa226b755ada6b4fd9a18462362ca1ae7ed65d58bc043290f1130f34f71f2e2
7
+ data.tar.gz: f96e665046a7619631bc8c1295c21385067ce5de8dcf06d0e66250f103f1207ae143a7fa651026358298fddd68f7fdfc766413f329ffdbd3260d61ef1abe18be
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## 6.3.0
2
+
3
+ * Allow curated related links to be shown on the new navigation sidebar. If the
4
+ link type `ordered_related_items_overrides` contains links to content items,
5
+ we will use those instead of the "more like this" links from search. These
6
+ links can be added in `content-tagger`.
7
+
1
8
  ## 6.2.0
2
9
 
3
10
  * Remove "Register to vote" section from related links data as deadline of
@@ -3,6 +3,8 @@ 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/rummager_taxonomy_sidebar_links"
7
+ require "govuk_navigation_helpers/curated_taxonomy_sidebar_links"
6
8
 
7
9
  module GovukNavigationHelpers
8
10
  class NavigationHelper
@@ -62,8 +62,33 @@ module GovukNavigationHelpers
62
62
  end
63
63
  end
64
64
 
65
+ def curated_taxonomy_sidebar_links
66
+ content_store_response.dig("links", "ordered_related_items_overrides").to_a.map do |link|
67
+ ContentItem.new(link)
68
+ end
69
+ end
70
+
65
71
  def external_links
66
72
  content_store_response.dig("details", "external_related_links").to_a
67
73
  end
74
+
75
+ def as_taxonomy_sidebar_link
76
+ {
77
+ title: title,
78
+ link: base_path,
79
+ }
80
+ end
81
+
82
+ def ==(other)
83
+ content_id == other.content_id
84
+ end
85
+
86
+ def hash
87
+ content_id.hash
88
+ end
89
+
90
+ def eql?(other)
91
+ self == other
92
+ end
68
93
  end
69
94
  end
@@ -0,0 +1,96 @@
1
+ require 'set'
2
+
3
+ module GovukNavigationHelpers
4
+ class CuratedTaxonomySidebarLinks
5
+ def initialize(content_item)
6
+ @content_item = content_item
7
+ end
8
+
9
+ def related_items
10
+ @related_items ||=
11
+ taxon_links +
12
+ elsewhere_on_gov_uk_links +
13
+ elsewhere_on_the_web_links
14
+ end
15
+
16
+ private
17
+
18
+ def taxon_links
19
+ @content_item.parent_taxons.map do |taxon|
20
+ {
21
+ title: taxon.title,
22
+ url: taxon.base_path,
23
+ description: taxon.description,
24
+ related_content: format_for_sidebar(related_content_by_taxon[taxon]),
25
+ }
26
+ end
27
+ end
28
+
29
+ def elsewhere_on_gov_uk_links
30
+ elsewhere_items = related_content_elsewhere_on_govuk
31
+ return [] if elsewhere_items.empty?
32
+
33
+ [
34
+ {
35
+ title: 'Elsewhere on GOV.UK',
36
+ related_content: format_for_sidebar(elsewhere_items),
37
+ },
38
+ ]
39
+ end
40
+
41
+ def elsewhere_on_the_web_links
42
+ return [] if @content_item.external_links.empty?
43
+
44
+ external_links = @content_item.external_links.map do |link|
45
+ {
46
+ title: link['title'],
47
+ link: link['url'],
48
+ }
49
+ end
50
+
51
+ [
52
+ {
53
+ title: 'Elsewhere on the web',
54
+ related_content: external_links,
55
+ },
56
+ ]
57
+ end
58
+
59
+ def related_content_by_taxon
60
+ @related_items_by_taxon ||= begin
61
+ curated_related_items = @content_item.curated_taxonomy_sidebar_links.to_set
62
+
63
+ @content_item.parent_taxons.each_with_object({}) do |taxon, items_by_taxon|
64
+ items_related_to_taxon = filter_items_by_taxon(curated_related_items, taxon)
65
+ items_by_taxon[taxon] = items_related_to_taxon
66
+ curated_related_items = undisplayed_items(curated_related_items, items_related_to_taxon)
67
+ end
68
+ end
69
+ end
70
+
71
+ def related_content_elsewhere_on_govuk
72
+ @related_content_elsewhere_on_govuk ||= begin
73
+ related_content = @content_item.curated_taxonomy_sidebar_links.to_set
74
+ related_taxon_content = related_content_by_taxon.values.flatten.to_set
75
+ related_content - related_taxon_content
76
+ end
77
+ end
78
+
79
+ def filter_items_by_taxon(items, taxon)
80
+ items.select do |item|
81
+ item.parent_taxons.include?(taxon)
82
+ end
83
+ end
84
+
85
+ def undisplayed_items(all_items, displayed_items)
86
+ all_items - displayed_items
87
+ end
88
+
89
+ def format_for_sidebar(collection)
90
+ collection
91
+ .to_a
92
+ .sort_by(&:title)
93
+ .map(&:as_taxonomy_sidebar_link)
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,61 @@
1
+ module GovukNavigationHelpers
2
+ class RummagerTaxonomySidebarLinks
3
+ def initialize(content_item)
4
+ @content_item = content_item
5
+ end
6
+
7
+ def related_items
8
+ parent_taxons = @content_item.parent_taxons
9
+ used_related_links = Set.new
10
+
11
+ parent_taxons.each_with_index.map do |parent_taxon, index|
12
+ related_content = index < 2 ? content_related_to(parent_taxon, used_related_links) : []
13
+
14
+ used_related_links.merge(
15
+ related_content.map { |content| content[:link] }
16
+ )
17
+
18
+ {
19
+ title: parent_taxon.title,
20
+ url: parent_taxon.base_path,
21
+ description: parent_taxon.description,
22
+ related_content: related_content,
23
+ }
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ # This method will fetch content related to content_item, and tagged to taxon. This is a
30
+ # temporary method that uses search to achieve this. This behaviour is to be moved into
31
+ # the content store
32
+ def content_related_to(taxon, used_related_links)
33
+ statsd.time(:taxonomy_sidebar_search_time) do
34
+ begin
35
+ results = Services.rummager.search(
36
+ similar_to: @content_item.base_path,
37
+ start: 0,
38
+ count: 3,
39
+ filter_taxons: [taxon.content_id],
40
+ filter_navigation_document_supertype: 'guidance',
41
+ reject_link: used_related_links.to_a,
42
+ fields: %w[title link],
43
+ )['results']
44
+
45
+ statsd.increment(:taxonomy_sidebar_searches)
46
+
47
+ results
48
+ .map { |result| { title: result['title'], link: result['link'], } }
49
+ .sort_by { |result| result[:title] }
50
+ rescue StandardError => e
51
+ GovukNavigationHelpers.configuration.error_handler.notify(e)
52
+ []
53
+ end
54
+ end
55
+ end
56
+
57
+ def statsd
58
+ GovukNavigationHelpers.configuration.statsd
59
+ end
60
+ end
61
+ end
@@ -9,61 +9,27 @@ module GovukNavigationHelpers
9
9
 
10
10
  def sidebar
11
11
  {
12
- items: taxons
12
+ items: related_items,
13
13
  }
14
14
  end
15
15
 
16
16
  private
17
17
 
18
- def statsd
19
- GovukNavigationHelpers.configuration.statsd
18
+ def there_are_related_item_overrides?
19
+ # TODO: We should check for any external links when we have "new"
20
+ # external links being curated in Content Tagger
21
+ @content_item.curated_taxonomy_sidebar_links.any?
20
22
  end
21
23
 
22
- def taxons
23
- parent_taxons = @content_item.parent_taxons
24
- used_related_links = Set.new
25
-
26
- parent_taxons.each_with_index.map do |parent_taxon, index|
27
- related_content = index < 2 ? content_related_to(parent_taxon, used_related_links) : []
28
-
29
- used_related_links.merge(
30
- related_content.map { |content| content[:link] }
31
- )
32
-
33
- {
34
- title: parent_taxon.title,
35
- url: parent_taxon.base_path,
36
- description: parent_taxon.description,
37
- related_content: related_content,
38
- }
39
- end
24
+ def related_items
25
+ related_items_factory.new(@content_item).related_items
40
26
  end
41
27
 
42
- # This method will fetch content related to @content_item, and tagged to taxon. This is a
43
- # temporary method that uses search to achieve this. This behaviour is to be moved into
44
- # the content store
45
- def content_related_to(taxon, used_related_links)
46
- statsd.time(:taxonomy_sidebar_search_time) do
47
- begin
48
- results = Services.rummager.search(
49
- similar_to: @content_item.base_path,
50
- start: 0,
51
- count: 3,
52
- filter_taxons: [taxon.content_id],
53
- filter_navigation_document_supertype: 'guidance',
54
- reject_link: used_related_links.to_a,
55
- fields: %w[title link],
56
- )['results']
57
-
58
- statsd.increment(:taxonomy_sidebar_searches)
59
-
60
- results
61
- .map { |result| { title: result['title'], link: result['link'], } }
62
- .sort_by { |result| result[:title] }
63
- rescue StandardError => e
64
- GovukNavigationHelpers.configuration.error_handler.notify(e)
65
- []
66
- end
28
+ def related_items_factory
29
+ if there_are_related_item_overrides?
30
+ CuratedTaxonomySidebarLinks
31
+ else
32
+ RummagerTaxonomySidebarLinks
67
33
  end
68
34
  end
69
35
  end
@@ -1,3 +1,3 @@
1
1
  module GovukNavigationHelpers
2
- VERSION = "6.2.0".freeze
2
+ VERSION = "6.3.0".freeze
3
3
  end
@@ -186,6 +186,273 @@ RSpec.describe GovukNavigationHelpers::TaxonomySidebar do
186
186
  end
187
187
  end
188
188
 
189
+ context 'when there are related link overrides' do
190
+ context 'belonging to the same taxon' do
191
+ it 'displays the related link overrides under a single taxon' do
192
+ content_item = content_item_tagged_to_taxon
193
+
194
+ content_item['links']['ordered_related_items_overrides'] = [
195
+ {
196
+ 'title' => 'Related link override B',
197
+ 'base_path' => '/override-b',
198
+ 'content_id' => 'override-b',
199
+ 'links' => {
200
+ 'taxons' => [
201
+ content_item['links']['taxons'][0],
202
+ ],
203
+ },
204
+ },
205
+ ]
206
+
207
+ expect(sidebar_for(content_item)).to eq(
208
+ items: [
209
+ {
210
+ title: "Taxon A",
211
+ url: "/taxon-a",
212
+ description: "The A taxon.",
213
+ related_content: [],
214
+ },
215
+ {
216
+ title: "Taxon B",
217
+ url: "/taxon-b",
218
+ description: "The B taxon.",
219
+ related_content: [
220
+ { title: 'Related link override B', link: '/override-b' },
221
+ ],
222
+ }
223
+ ]
224
+ )
225
+ end
226
+
227
+ it 'displays the related link overrides under a multiple taxons' do
228
+ content_item = content_item_tagged_to_taxon
229
+
230
+ content_item['links']['ordered_related_items_overrides'] = [
231
+ {
232
+ 'title' => 'Related link override B',
233
+ 'base_path' => '/override-b',
234
+ 'content_id' => 'override-b',
235
+ 'links' => {
236
+ 'taxons' => [
237
+ content_item['links']['taxons'][0],
238
+ ],
239
+ },
240
+ },
241
+ {
242
+ 'title' => 'Related link override A-2',
243
+ 'base_path' => '/override-a-2',
244
+ 'content_id' => 'override-a-2',
245
+ 'links' => {
246
+ 'taxons' => [
247
+ content_item['links']['taxons'][1],
248
+ ],
249
+ },
250
+ },
251
+ {
252
+ 'title' => 'Related link override A-1',
253
+ 'base_path' => '/override-a-1',
254
+ 'content_id' => 'override-a-1',
255
+ 'links' => {
256
+ 'taxons' => [
257
+ content_item['links']['taxons'][1],
258
+ ],
259
+ },
260
+ },
261
+ ]
262
+
263
+ expect(sidebar_for(content_item)).to eq(
264
+ items: [
265
+ {
266
+ title: "Taxon A",
267
+ url: "/taxon-a",
268
+ description: "The A taxon.",
269
+ related_content: [
270
+ { 'title': 'Related link override A-1', 'link': '/override-a-1' },
271
+ { 'title': 'Related link override A-2', 'link': '/override-a-2' },
272
+ ],
273
+ },
274
+ {
275
+ title: "Taxon B",
276
+ url: "/taxon-b",
277
+ description: "The B taxon.",
278
+ related_content: [
279
+ { 'title': 'Related link override B', 'link': '/override-b' },
280
+ ],
281
+ }
282
+ ]
283
+ )
284
+ end
285
+
286
+ it 'displays a related link tagged to multiple taxons under a single taxon' do
287
+ content_item = content_item_tagged_to_taxon
288
+
289
+ content_item['links']['ordered_related_items_overrides'] = [
290
+ {
291
+ 'title' => 'Related link override',
292
+ 'base_path' => '/override',
293
+ 'content_id' => 'override',
294
+ 'links' => {
295
+ 'taxons' => [
296
+ content_item['links']['taxons'][0],
297
+ content_item['links']['taxons'][1],
298
+ ],
299
+ },
300
+ },
301
+ ]
302
+
303
+ expect(sidebar_for(content_item)).to eq(
304
+ items: [
305
+ {
306
+ title: "Taxon A",
307
+ url: "/taxon-a",
308
+ description: "The A taxon.",
309
+ related_content: [
310
+ { 'title': 'Related link override', 'link': '/override' },
311
+ ],
312
+ },
313
+ {
314
+ title: "Taxon B",
315
+ url: "/taxon-b",
316
+ description: "The B taxon.",
317
+ related_content: [],
318
+ }
319
+ ]
320
+ )
321
+ end
322
+
323
+ it 'displays a related link tagged to another taxon under "Elsewhere"' do
324
+ content_item = content_item_tagged_to_taxon
325
+
326
+ content_item['links']['ordered_related_items_overrides'] = [
327
+ {
328
+ 'title' => 'Related link override',
329
+ 'base_path' => '/override',
330
+ 'content_id' => 'override',
331
+ 'links' => {
332
+ 'taxons' => [
333
+ {
334
+ 'title' => 'Some other taxon',
335
+ 'content_id' => 'some-other-taxon',
336
+ 'base_path' => '/some-other-taxon',
337
+ }
338
+ ],
339
+ },
340
+ },
341
+ ]
342
+
343
+ expect(sidebar_for(content_item)).to eq(
344
+ items: [
345
+ {
346
+ title: "Taxon A",
347
+ url: "/taxon-a",
348
+ description: "The A taxon.",
349
+ related_content: [],
350
+ },
351
+ {
352
+ title: "Taxon B",
353
+ url: "/taxon-b",
354
+ description: "The B taxon.",
355
+ related_content: [],
356
+ },
357
+ {
358
+ title: "Elsewhere on GOV.UK",
359
+ related_content: [
360
+ { title: 'Related link override', link: '/override' }
361
+ ],
362
+ },
363
+ ]
364
+ )
365
+ end
366
+
367
+ it 'displays a related link not tagged to any taxons under "Elsewhere"' do
368
+ content_item = content_item_tagged_to_taxon
369
+
370
+ content_item['links']['ordered_related_items_overrides'] = [
371
+ {
372
+ 'title' => 'Related link override',
373
+ 'base_path' => '/override',
374
+ 'content_id' => 'override',
375
+ 'links' => {},
376
+ },
377
+ ]
378
+
379
+ expect(sidebar_for(content_item)).to eq(
380
+ items: [
381
+ {
382
+ title: "Taxon A",
383
+ url: "/taxon-a",
384
+ description: "The A taxon.",
385
+ related_content: [],
386
+ },
387
+ {
388
+ title: "Taxon B",
389
+ url: "/taxon-b",
390
+ description: "The B taxon.",
391
+ related_content: [],
392
+ },
393
+ {
394
+ title: "Elsewhere on GOV.UK",
395
+ related_content: [
396
+ { title: 'Related link override', link: '/override' }
397
+ ],
398
+ },
399
+ ]
400
+ )
401
+ end
402
+
403
+ it 'displays an external related link under "Elsewhere"' do
404
+ content_item = content_item_tagged_to_taxon
405
+
406
+ content_item['links']['ordered_related_items_overrides'] = [
407
+ {
408
+ 'title' => 'Related link override',
409
+ 'base_path' => '/override',
410
+ 'content_id' => 'override',
411
+ 'links' => {},
412
+ },
413
+ ]
414
+
415
+ content_item['details'] = {
416
+ 'external_related_links' => [
417
+ {
418
+ 'title' => 'External related link',
419
+ 'url' => 'http://example.com',
420
+ },
421
+ ]
422
+ }
423
+
424
+ expect(sidebar_for(content_item)).to eq(
425
+ items: [
426
+ {
427
+ title: "Taxon A",
428
+ url: "/taxon-a",
429
+ description: "The A taxon.",
430
+ related_content: [],
431
+ },
432
+ {
433
+ title: "Taxon B",
434
+ url: "/taxon-b",
435
+ description: "The B taxon.",
436
+ related_content: [],
437
+ },
438
+ {
439
+ title: "Elsewhere on GOV.UK",
440
+ related_content: [
441
+ { title: 'Related link override', link: '/override' }
442
+ ],
443
+ },
444
+ {
445
+ title: "Elsewhere on the web",
446
+ related_content: [
447
+ { title: 'External related link', link: 'http://example.com' }
448
+ ],
449
+ },
450
+ ]
451
+ )
452
+ end
453
+ end
454
+ end
455
+
189
456
  context 'when Rummager raises an exception' do
190
457
  error_handler = nil
191
458
 
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: 6.2.0
4
+ version: 6.3.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-05-23 00:00:00.000000000 Z
11
+ date: 2017-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gds-api-adapters
@@ -173,8 +173,10 @@ files:
173
173
  - lib/govuk_navigation_helpers/breadcrumbs.rb
174
174
  - lib/govuk_navigation_helpers/configuration.rb
175
175
  - lib/govuk_navigation_helpers/content_item.rb
176
+ - lib/govuk_navigation_helpers/curated_taxonomy_sidebar_links.rb
176
177
  - lib/govuk_navigation_helpers/grouped_related_links.rb
177
178
  - lib/govuk_navigation_helpers/related_items.rb
179
+ - lib/govuk_navigation_helpers/rummager_taxonomy_sidebar_links.rb
178
180
  - lib/govuk_navigation_helpers/services.rb
179
181
  - lib/govuk_navigation_helpers/taxon_breadcrumbs.rb
180
182
  - lib/govuk_navigation_helpers/taxonomy_sidebar.rb