govuk_navigation_helpers 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ae03499e445926565f940d61ac2ca8a9e6566f40
4
- data.tar.gz: e4b3be57785db7c30e2846cb9b2d410d1b3f4309
3
+ metadata.gz: 5fba2dad8d4cf628e28fa8f21861c424c87ab444
4
+ data.tar.gz: f9c522d2424160b6f34fc675d107f9b5f7aec215
5
5
  SHA512:
6
- metadata.gz: 08da4b0086ef29b400da2c05c5b87bbc5219656cefa84fbdf139beefc73e8e150a95ac81c7d4ee08aa412ead50289bff2df652149aa3d8cacb1091d61ce0d5a3
7
- data.tar.gz: 3dd946e21c66b799ad1119e2a97a8db3d1cefb3d022e7fc04e6b9542d7d05a409f44d2d429815eaeaab70775738ea8e322fa09a713cda3c927aed34be8916a85
6
+ metadata.gz: dbdcff0ab5f3f3cf62c24a449d7ca0a6e413edf5762338d0550defeedcab378acbc61f1d851488d3785dd6080dd0d57c5b818972e1bd2c61b8ecf133dd464cf2
7
+ data.tar.gz: 9d162ed86cdc9a002c01b37140f1ac1da95bb8dce64cbf547be960c4fd616cd166710426eb3ec8f424715b0b66e6a520ca6458beb28bcb210165dd0029a56110
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 2.1.0
2
+
3
+ * Add helper to render taxon breadcrumbs
4
+
1
5
  ## 2.0.0
2
6
 
3
7
  * Functionality to generate data for the related items component
@@ -1,21 +1,12 @@
1
1
  module GovukNavigationHelpers
2
2
  class Breadcrumbs
3
3
  def initialize(content_item)
4
- @content_item = content_item
4
+ @content_item = ContentItem.new(content_item)
5
5
  end
6
6
 
7
7
  def breadcrumbs
8
- direct_parent = content_item.dig("links", "parent", 0)
9
-
10
- ordered_parents = []
11
-
12
- while direct_parent
13
- ordered_parents << {
14
- title: direct_parent["title"],
15
- url: direct_parent["base_path"],
16
- }
17
-
18
- direct_parent = direct_parent.dig("links", "parent", 0)
8
+ ordered_parents = all_parents.map do |parent|
9
+ { title: parent.title, url: parent.base_path }
19
10
  end
20
11
 
21
12
  ordered_parents << { title: "Home", url: "/" }
@@ -28,5 +19,18 @@ module GovukNavigationHelpers
28
19
  private
29
20
 
30
21
  attr_reader :content_item
22
+
23
+ def all_parents
24
+ parents = []
25
+
26
+ direct_parent = content_item.parent
27
+ while direct_parent
28
+ parents << direct_parent
29
+
30
+ direct_parent = direct_parent.parent
31
+ end
32
+
33
+ parents
34
+ end
31
35
  end
32
36
  end
@@ -16,6 +16,13 @@ module GovukNavigationHelpers
16
16
  ContentItem.new(parent_item)
17
17
  end
18
18
 
19
+ def parent_taxon
20
+ # TODO: Determine what to do when there are multiple parents. For now just display the first
21
+ parent_taxon = content_store_response.dig("links", "parent_taxons", 0)
22
+ return unless parent_taxon
23
+ ContentItem.new(parent_taxon)
24
+ end
25
+
19
26
  def mainstream_browse_pages
20
27
  content_store_response.dig("links", "mainstream_browse_pages").to_a.map do |link|
21
28
  ContentItem.new(link)
@@ -0,0 +1,35 @@
1
+ module GovukNavigationHelpers
2
+ class TaxonBreadcrumbs
3
+ def initialize(content_item)
4
+ @content_item = ContentItem.new(content_item)
5
+ end
6
+
7
+ def breadcrumbs
8
+ ordered_parents = all_parents.map do |parent|
9
+ { title: parent.title, url: parent.base_path }
10
+ end
11
+
12
+ ordered_parents << { title: "Home", url: "/" }
13
+
14
+ {
15
+ breadcrumbs: ordered_parents.reverse
16
+ }
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :content_item
22
+
23
+ def all_parents
24
+ parents = []
25
+
26
+ direct_parent = content_item.parent_taxon
27
+ while direct_parent
28
+ parents << direct_parent
29
+ direct_parent = direct_parent.parent_taxon
30
+ end
31
+
32
+ parents
33
+ end
34
+ end
35
+ end
@@ -1,3 +1,3 @@
1
1
  module GovukNavigationHelpers
2
- VERSION = "2.0.0".freeze
2
+ VERSION = "2.1.0".freeze
3
3
  end
@@ -1,6 +1,7 @@
1
1
  require "govuk_navigation_helpers/version"
2
2
  require "govuk_navigation_helpers/breadcrumbs"
3
3
  require "govuk_navigation_helpers/related_items"
4
+ require "govuk_navigation_helpers/taxon_breadcrumbs"
4
5
 
5
6
  module GovukNavigationHelpers
6
7
  class NavigationHelper
@@ -8,7 +9,7 @@ module GovukNavigationHelpers
8
9
  @content_item = content_item
9
10
  end
10
11
 
11
- # Generate a breacrumb trail
12
+ # Generate a breadcrumb trail
12
13
  #
13
14
  # @return [Hash] Payload for the GOV.UK breadcrumbs component
14
15
  # @see http://govuk-component-guide.herokuapp.com/components/breadcrumbs
@@ -16,6 +17,14 @@ module GovukNavigationHelpers
16
17
  Breadcrumbs.new(content_item).breadcrumbs
17
18
  end
18
19
 
20
+ # Generate a breadcrumb trail for a taxon, using the taxon_parent link field
21
+ #
22
+ # @return [Hash] Payload for the GOV.UK breadcrumbs component
23
+ # @see http://govuk-component-guide.herokuapp.com/components/breadcrumbs
24
+ def taxon_breadcrumbs
25
+ TaxonBreadcrumbs.new(content_item).breadcrumbs
26
+ end
27
+
19
28
  # Generate a related items payload
20
29
  #
21
30
  # @return [Hash] Payload for the GOV.UK Component
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe GovukNavigationHelpers::TaxonBreadcrumbs do
4
+ describe "Taxon breadcrumbs" do
5
+ it "can handle any valid content item" do
6
+ generator = GovukSchemas::RandomExample.for_schema("taxon", schema_type: "frontend")
7
+
8
+ expect { GovukNavigationHelpers::TaxonBreadcrumbs.new(generator.payload).breadcrumbs }.to_not raise_error
9
+ end
10
+
11
+ it "returns the root when parent is not specified" do
12
+ content_item = { "links" => {} }
13
+ breadcrumbs = breadcrumbs_for(content_item)
14
+
15
+ expect(breadcrumbs).to eq(
16
+ breadcrumbs: [
17
+ { title: "Home", url: "/" },
18
+ ]
19
+ )
20
+ end
21
+
22
+ it "returns the root when parent is empty" do
23
+ content_item = content_item_with_parents([])
24
+ breadcrumbs = breadcrumbs_for(content_item)
25
+
26
+ expect(breadcrumbs).to eq(
27
+ breadcrumbs: [
28
+ { title: "Home", url: "/" },
29
+ ]
30
+ )
31
+ end
32
+
33
+ it "places parent under the root when there is a parent" do
34
+ parent = {
35
+ "content_id" => "30c1b93d-2553-47c9-bc3c-fc5b513ecc32",
36
+ "locale" => "en",
37
+ "title" => "A-parent",
38
+ "base_path" => "/a-parent",
39
+ }
40
+
41
+ content_item = content_item_with_parents([parent])
42
+ breadcrumbs = breadcrumbs_for(content_item)
43
+
44
+ expect(breadcrumbs).to eq(
45
+ breadcrumbs: [
46
+ { title: "Home", url: "/" },
47
+ { title: "A-parent", url: "/a-parent" }
48
+ ]
49
+ )
50
+ end
51
+
52
+ it "includes grandparent when available" do
53
+ grandparent = {
54
+ "title" => "Another-parent",
55
+ "base_path" => "/another-parent",
56
+ "content_id" => "30c1b93d-2553-47c9-bc3c-fc5b513ecc32",
57
+ "locale" => "en",
58
+ }
59
+
60
+ parent = {
61
+ "content_id" => "30c1b93d-2553-47c9-bc3c-fc5b513ecc32",
62
+ "locale" => "en",
63
+ "title" => "A-parent",
64
+ "base_path" => "/a-parent",
65
+ "links" => {
66
+ "parent_taxons" => [grandparent]
67
+ }
68
+ }
69
+
70
+ content_item = content_item_with_parents([parent])
71
+ breadcrumbs = breadcrumbs_for(content_item)
72
+
73
+ expect(breadcrumbs).to eq(
74
+ breadcrumbs: [
75
+ { title: "Home", url: "/" },
76
+ { title: "Another-parent", url: "/another-parent" },
77
+ { title: "A-parent", url: "/a-parent" }
78
+ ]
79
+ )
80
+ end
81
+ end
82
+
83
+ def breadcrumbs_for(content_item)
84
+ generator = GovukSchemas::RandomExample.for_schema("taxon", schema_type: "frontend")
85
+ fully_valid_content_item = generator.merge_and_validate(content_item)
86
+
87
+ # Use the main class instead of GovukNavigationHelpers::Breadcrumbs, so that
88
+ # we're testing both at the same time.
89
+ GovukNavigationHelpers::NavigationHelper.new(fully_valid_content_item).taxon_breadcrumbs
90
+ end
91
+
92
+ def content_item_with_parents(parents)
93
+ {
94
+ "links" => { "parent_taxons" => parents }
95
+ }
96
+ end
97
+ 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: 2.0.0
4
+ version: 2.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: 2016-10-25 00:00:00.000000000 Z
11
+ date: 2017-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -145,10 +145,12 @@ files:
145
145
  - lib/govuk_navigation_helpers/content_item.rb
146
146
  - lib/govuk_navigation_helpers/grouped_related_links.rb
147
147
  - lib/govuk_navigation_helpers/related_items.rb
148
+ - lib/govuk_navigation_helpers/taxon_breadcrumbs.rb
148
149
  - lib/govuk_navigation_helpers/version.rb
149
150
  - spec/breadcrumbs_spec.rb
150
151
  - spec/related_items_spec.rb
151
152
  - spec/spec_helper.rb
153
+ - spec/taxon_breadcrumbs_spec.rb
152
154
  homepage: https://github.com/alphagov/govuk_navigation_helpers
153
155
  licenses:
154
156
  - MIT
@@ -177,3 +179,4 @@ test_files:
177
179
  - spec/breadcrumbs_spec.rb
178
180
  - spec/related_items_spec.rb
179
181
  - spec/spec_helper.rb
182
+ - spec/taxon_breadcrumbs_spec.rb