govuk_navigation_helpers 0.0.1 → 1.0.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: 68f75d61c3b660e079b9c64a30e3a99cc04850b3
4
- data.tar.gz: f018b0023a25f3dba8ec58742c4abcce083a3d8c
3
+ metadata.gz: f4176c900f1ecc9f326253506091426c84a04185
4
+ data.tar.gz: 1382b484bfbdf27857da344c9eab90ea44f6cecb
5
5
  SHA512:
6
- metadata.gz: 781df8adc2fa3e43335f89da904a7ffc389943fe6906756f6ba5e933322e70cb85515c2ce0cbaf21d78d10ab8c298bd24efc2bc2bb26223df344e3386c1af0f2
7
- data.tar.gz: caadaeab8b6cb296803094498591c2e0ed7780dd03f70a6dac5fff41c0fd35f8d5ae1fcd8c2143c2fd70ecbfc96386f1df242d3a2f9602c987e86beb7d967efc
6
+ metadata.gz: 8382fe5f7de451bdba0ca038f1e0a481ede4a19d9e05231178e5c7e2fa166783e11896755ae7c999330760f73c3b5ac0bf6a18b56391f054803d88c467f103a7
7
+ data.tar.gz: f8862c700764b6e0fea0cc61a6307b5f2661d255ef99ec350620f4233913b0dde4d83d2f996c53ee814c85825d300fb088bc4c95aa8ad365f78766792098d078
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 1.0.0
2
+
3
+ * Functionality to generate data for the breadcrumb component
data/LICENCE.txt ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+ Copyright (C) 2016 Crown copyright (Government Digital Service)
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+ this software and associated documentation files (the "Software"), to deal in
6
+ the Software without restriction, including without limitation the rights to
7
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8
+ of the Software, and to permit persons to whom the Software is furnished to do
9
+ so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
data/README.md CHANGED
@@ -2,7 +2,15 @@
2
2
 
3
3
  This is a gem to share code between GOV.UK frontends.
4
4
 
5
- ## Installation
5
+ ## Nomenclature
6
+
7
+ - **content item**: An object returned by the content store
8
+
9
+ ## Technical documentation
10
+
11
+ These helpers format data from the [content store](https://github.com/alphagov/content-store) for use with [GOV.UK Components](http://govuk-component-guide.herokuapp.com/about).
12
+
13
+ ### Installation
6
14
 
7
15
  Add this line to your application's Gemfile:
8
16
 
@@ -14,6 +22,35 @@ And then execute:
14
22
 
15
23
  $ bundle
16
24
 
17
- ## Usage
25
+ ### Usage
26
+
27
+ Get the JSON representation of a page and initialise the helper:
28
+
29
+ ```ruby
30
+ def some_controller_method
31
+ content_item = Services.content_store.content_item("/register-to-vote")
32
+ @navigation = GovukNavigationHelpers::NavigationHelper.new(content_item)
33
+ end
34
+ ```
35
+
36
+ Render the component:
37
+
38
+ ```ruby
39
+ <%= render partial: 'govuk_component/breadcrumbs', locals: { breadcrumbs: @navigation.breadcrumbs }
40
+ ```
41
+
42
+ ### Running the test suite
43
+
44
+ `bundle exec rake`
45
+
46
+ ### Documentation
47
+
48
+ See [RubyDoc](http://www.rubydoc.info/gems/govuk_navigation_helpers) for some limited documentation.
49
+
50
+ To run a Yard server locally to preview documentation, run:
51
+
52
+ $ bundle exec yard server --reload
53
+
54
+ ## Licence
18
55
 
19
- TBD.
56
+ [MIT License](LICENCE.txt)
data/Rakefile CHANGED
@@ -1,17 +1,18 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require "gem_publisher"
2
4
 
3
- begin
4
- require 'rspec/core/rake_task'
5
- RSpec::Core::RakeTask.new(:spec)
6
- rescue LoadError
7
- end
8
-
9
- task :default => [:spec]
5
+ RSpec::Core::RakeTask.new(:spec)
10
6
 
11
- require "gem_publisher"
7
+ task default: [:spec, :lint]
12
8
 
13
9
  desc "Publish gem to RubyGems"
14
- task :publish_gem do |t|
10
+ task :publish_gem do |_t|
15
11
  published_gem = GemPublisher.publish_if_updated("govuk_navigation_helpers.gemspec", :rubygems)
16
12
  puts "Published #{published_gem}" if published_gem
17
13
  end
14
+
15
+ desc "Run govuk-lint with similar params to CI"
16
+ task "lint" do
17
+ sh "bundle exec govuk-lint-ruby --format clang"
18
+ end
@@ -22,6 +22,10 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
23
  spec.add_development_dependency "rspec", "~> 3.5"
24
24
  spec.add_development_dependency "gem_publisher", "~> 1.5.0"
25
+ spec.add_development_dependency "govuk-lint", "~> 1.2.1"
26
+ spec.add_development_dependency "pry-byebug", "~> 3.4"
27
+ spec.add_development_dependency "yard", "~> 0.8"
28
+ spec.add_development_dependency "govuk_schemas", "~> 0.1"
25
29
 
26
30
  spec.required_ruby_version = ">= 2.3.1"
27
31
  end
data/jenkins.sh CHANGED
@@ -36,6 +36,21 @@ git clean -fdx
36
36
  # is master.
37
37
  git merge --no-commit origin/master || git merge --abort
38
38
 
39
+ echo "Running ruby linter"
40
+ bundle install --path "${HOME}/bundles/${JOB_NAME}"
41
+ bundle exec govuk-lint-ruby \
42
+ --format html --out rubocop-${GIT_COMMIT}.html \
43
+ --format clang
44
+
45
+ # Clone govuk-content-schemas depedency for contract tests
46
+ rm -rf /tmp/govuk-content-schemas
47
+ git clone git@github.com:alphagov/govuk-content-schemas.git /tmp/govuk-content-schemas
48
+ (
49
+ cd /tmp/govuk-content-schemas
50
+ git checkout ${SCHEMA_GIT_COMMIT:-"master"}
51
+ )
52
+ export GOVUK_CONTENT_SCHEMAS_PATH=/tmp/govuk-content-schemas
53
+
39
54
  # Bundle and run tests against multiple ruby versions
40
55
  for version in 2.3.1; do
41
56
  rm -f Gemfile.lock
@@ -1,5 +1,21 @@
1
1
  require "govuk_navigation_helpers/version"
2
+ require "govuk_navigation_helpers/breadcrumbs"
2
3
 
3
4
  module GovukNavigationHelpers
4
- # Your code goes here...
5
+ class NavigationHelper
6
+ def initialize(content_item)
7
+ @content_item = content_item
8
+ end
9
+
10
+ # Generate a breacrumb trail
11
+ #
12
+ # @return [Array<Hash>] Each item is a hash containing `:title` and `:url` for one link in the breadcrumb
13
+ def breadcrumbs
14
+ Breadcrumbs.new(content_item).breadcrumbs
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :content_item
20
+ end
5
21
  end
@@ -0,0 +1,30 @@
1
+ module GovukNavigationHelpers
2
+ class Breadcrumbs
3
+ def initialize(content_item)
4
+ @content_item = content_item
5
+ end
6
+
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)
19
+ end
20
+
21
+ ordered_parents << { title: "Home", url: "/" }
22
+
23
+ ordered_parents.reverse
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :content_item
29
+ end
30
+ end
@@ -1,3 +1,3 @@
1
1
  module GovukNavigationHelpers
2
- VERSION = "0.0.1"
2
+ VERSION = "1.0.0".freeze
3
3
  end
@@ -0,0 +1,103 @@
1
+ require 'spec_helper'
2
+ require 'govuk_navigation_helpers'
3
+ require 'govuk_schemas'
4
+
5
+ RSpec.describe GovukNavigationHelpers::Breadcrumbs do
6
+ describe "#breadcrumbs" do
7
+ it "can handle any valid content item" do
8
+ generator = GovukSchemas::RandomExample.for_schema("placeholder", schema_type: "frontend")
9
+
10
+ 50.times do
11
+ expect {
12
+ described_class.new(generator.payload).breadcrumbs
13
+ }.to_not raise_error
14
+ end
15
+ end
16
+
17
+ it "returns the root when parent is not specified" do
18
+ content_item = { "links" => {} }
19
+ breadcrumbs = breadcrumbs_for(content_item)
20
+
21
+ expect(breadcrumbs).to eq(
22
+ [
23
+ { title: "Home", url: "/" },
24
+ ]
25
+ )
26
+ end
27
+
28
+ it "returns the root when parent is empty" do
29
+ content_item = content_item_with_parents([])
30
+ breadcrumbs = breadcrumbs_for(content_item)
31
+
32
+ expect(breadcrumbs).to eq(
33
+ [
34
+ { title: "Home", url: "/" },
35
+ ]
36
+ )
37
+ end
38
+
39
+ it "places parent under the root when there is a parent" do
40
+ parent = {
41
+ "content_id" => "30c1b93d-2553-47c9-bc3c-fc5b513ecc32",
42
+ "locale" => "en",
43
+ "title" => "A-parent",
44
+ "base_path" => "/a-parent",
45
+ }
46
+
47
+ content_item = content_item_with_parents([parent])
48
+ breadcrumbs = breadcrumbs_for(content_item)
49
+
50
+ expect(breadcrumbs).to eq(
51
+ [
52
+ { title: "Home", url: "/" },
53
+ { title: "A-parent", url: "/a-parent" }
54
+ ]
55
+ )
56
+ end
57
+
58
+ it "includes grandparent when available" do
59
+ grandparent = {
60
+ "title" => "Another-parent",
61
+ "base_path" => "/another-parent",
62
+ "content_id" => "30c1b93d-2553-47c9-bc3c-fc5b513ecc32",
63
+ "locale" => "en",
64
+ }
65
+
66
+ parent = {
67
+ "content_id" => "30c1b93d-2553-47c9-bc3c-fc5b513ecc32",
68
+ "locale" => "en",
69
+ "title" => "A-parent",
70
+ "base_path" => "/a-parent",
71
+ "links" => {
72
+ "parent" => [grandparent]
73
+ }
74
+ }
75
+
76
+ content_item = content_item_with_parents([parent])
77
+ breadcrumbs = breadcrumbs_for(content_item)
78
+
79
+ expect(breadcrumbs).to eq(
80
+ [
81
+ { title: "Home", url: "/" },
82
+ { title: "Another-parent", url: "/another-parent" },
83
+ { title: "A-parent", url: "/a-parent" }
84
+ ]
85
+ )
86
+ end
87
+ end
88
+
89
+ def content_item_with_parents(parents)
90
+ {
91
+ "links" => { "parent" => parents }
92
+ }
93
+ end
94
+
95
+ def breadcrumbs_for(content_item)
96
+ generator = GovukSchemas::RandomExample.for_schema("placeholder", schema_type: "frontend")
97
+ fully_valid_content_item = generator.merge_and_validate(content_item)
98
+
99
+ # Use the main class instead of GovukNavigationHelpers::Breadcrumbs, so that
100
+ # we're testing both at the same time.
101
+ GovukNavigationHelpers::NavigationHelper.new(fully_valid_content_item).breadcrumbs
102
+ end
103
+ end
data/spec/spec_helper.rb CHANGED
@@ -16,6 +16,9 @@
16
16
  # users commonly want.
17
17
  #
18
18
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+
20
+ require 'pry-byebug'
21
+
19
22
  RSpec.configure do |config|
20
23
  # rspec-expectations config goes here. You can use an alternate
21
24
  # assertion/expectation library such as wrong or the stdlib/minitest
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: 0.0.1
4
+ version: 1.0.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-09-14 00:00:00.000000000 Z
11
+ date: 2016-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,62 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 1.5.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: govuk-lint
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.2.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.2.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.4'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.4'
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.8'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.8'
111
+ - !ruby/object:Gem::Dependency
112
+ name: govuk_schemas
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.1'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.1'
69
125
  description: Gem to transform items from the content-store into payloads for GOV.UK
70
126
  components
71
127
  email:
@@ -77,14 +133,17 @@ files:
77
133
  - ".gitignore"
78
134
  - ".rspec"
79
135
  - ".ruby-version"
136
+ - CHANGELOG.md
80
137
  - Gemfile
81
- - LICENSE.txt
138
+ - LICENCE.txt
82
139
  - README.md
83
140
  - Rakefile
84
141
  - govuk_navigation_helpers.gemspec
85
142
  - jenkins.sh
86
143
  - lib/govuk_navigation_helpers.rb
144
+ - lib/govuk_navigation_helpers/breadcrumbs.rb
87
145
  - lib/govuk_navigation_helpers/version.rb
146
+ - spec/breadcrumbs_spec.rb
88
147
  - spec/spec_helper.rb
89
148
  homepage: https://github.com/alphagov/govuk_navigation_helpers
90
149
  licenses:
@@ -111,4 +170,5 @@ signing_key:
111
170
  specification_version: 4
112
171
  summary: Gem to transform items from the content-store into payloads for GOV.UK components
113
172
  test_files:
173
+ - spec/breadcrumbs_spec.rb
114
174
  - spec/spec_helper.rb
data/LICENSE.txt DELETED
@@ -1,22 +0,0 @@
1
- Copyright (c) 2016 Tijmen Brommet
2
-
3
- MIT License
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.