govuk_web_banners 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: da77b3f454b15e59feb67dd5e9bf18fef636b3bda1bf874d049c8df3b6399ac1
4
+ data.tar.gz: 7c2ccf8b6bf5cdfba1a0aea1580f31c8ed24b9f9d2238dddf3c020ddbe418250
5
+ SHA512:
6
+ metadata.gz: 20fe2fe0e6c23f98145a36ce5e5139ff3c8f7d58f1e6d53e13731ddfafa13b712917fc13b1aadccaf76fe2d291254377e3691ed3d4292e293d681bd0f2167102
7
+ data.tar.gz: 30cd8b18cb423dd66517abb9ff6a58cdcba830ea76bc0d81376564b1680865ad751b7853eaa09c5157afe837273cf31e1a64a5b0b3bfc575343b1806de2720e5
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (C) 2024 Crown Copyright (Government Digital Service)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # GovukWebBanners
2
+ Proof of Concept for centralising handling of Recruitment, Global, and Emergency banners (currently spread across apps)
3
+
4
+ ## Usage
5
+ Currently, supports recruitment banners
6
+
7
+ ## Adding the gem to your application
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "govuk_web_banners"
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install govuk_web_banners
22
+ ```
23
+
24
+ Add the JS dependencies to your existing asset dependencies file:
25
+
26
+ ```
27
+ //= require govuk_web_banners/dependencies
28
+ ```
29
+
30
+ Add a call to the partial in the layout or view that you want banners to appear in:
31
+
32
+ ```
33
+ <%= render "govuk_web_banners/recruitment_banner" %>
34
+ ```
35
+
36
+ You should make sure this line is above the call to render_component_stylesheets call if your
37
+ app is using individual component stylesheets.
38
+
39
+ ## Updating banner information in the gem
40
+
41
+ Data for the current set of live banners can be found at `config/govuk_web_banners/recruitment_banners.yml`. To
42
+ add a banner to the config, add an entry under the banners: array. Note that this array must always be valid,
43
+ so if there are no banners in the file, it must contain at least `banners: []`
44
+
45
+ ### Example banner entry
46
+
47
+ ```
48
+ banners:
49
+ - name: Banner 1
50
+ suggestion_text: "Help improve GOV.UK"
51
+ suggestion_link_text: "Sign up to take part in user research (opens in a new tab)"
52
+ survey_url: https://google.com
53
+ page_paths:
54
+ - /
55
+ - /foreign-travel-advice
56
+ start_date: 21/10/2024
57
+ end_date: 18/11/2024
58
+ ```
59
+
60
+ The required keys are `suggestion_text`, `suggestion_link_text`, and `survey_url` (the values to appear in the
61
+ banner), and `page_paths` (an array of paths on which the banner should be shown).
62
+
63
+ Optional keys are `name` (an identifying name for this banner, not rendered anywhere), and `start_date` / `end_date`
64
+ (the banner becomes active at the start of the day specified as `start_date`, and stops at the *start* of the day
65
+ specified as `end_date`). Start and end dates must be in the DD/MM/YYYY format parsable as a YAML -> Date.
66
+
67
+ ### Keeping the config file valid and tidy
68
+
69
+ The config file will be checked during CI, so an invalid file can't be released as a gem and we are forced
70
+ to make sure it's kept tidy. These checks include:
71
+
72
+ * the banners array must be a valid YAML array
73
+ * all banners have a suggestion_text, suggestion_link_text, survey_url and page_paths
74
+ * the same page_path is not present on two banners that are active at the same time
75
+ * paths must start with a forward-slash (/)
76
+
77
+ It will also display warnings (but not fail CI)
78
+
79
+ * if there are banners that have expired - you are encouraged to remove obsolete config, but it will not
80
+ prevent you merging changes.
81
+ * if page_paths point to pages that are not currently live on GOV.UK - this may be intentional (if the banner
82
+ is for a page that isn't yet published), or it may indicate a typo in the path.
83
+
84
+ Note that some of this validation code is in the lib/govuk_web_banners/validators path, which
85
+ should be tested to ensure the checking is valid, but will not be bundled into the released gem.
86
+
87
+ ## License
88
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ require "bundler/setup"
2
+ require "rubocop/rake_task"
3
+ require "rspec/core/rake_task"
4
+
5
+ APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__)
6
+ load "rails/tasks/engine.rake"
7
+
8
+ load "rails/tasks/statistics.rake"
9
+
10
+ require "bundler/gem_tasks"
11
+
12
+ RuboCop::RakeTask.new
13
+ RSpec::Core::RakeTask.new
14
+
15
+ require "govuk_web_banners/validators/recruitment_banner"
16
+ require "rainbow"
17
+
18
+ desc "show errors in the live config"
19
+ task :check_config do
20
+ validator = GovukWebBanners::Validators::RecruitmentBanner.new(GovukWebBanners::RecruitmentBanner.all_banners)
21
+
22
+ if !validator.valid?
23
+ puts Rainbow("\nLive config contains errors!").red
24
+ validator.errors.each_key do |key|
25
+ puts(key)
26
+ validator.errors[key].each { |error| puts(" - #{error}") }
27
+ end
28
+ puts
29
+ exit(1)
30
+ elsif validator.warnings?
31
+ puts Rainbow("\nLive config is valid, but with warnings").yellow
32
+ validator.warnings.each_key do |key|
33
+ puts(key)
34
+ validator.warnings[key].each { |warnings| puts(" - #{warnings}") }
35
+ end
36
+ puts
37
+ else
38
+ puts Rainbow("\nLive config is valid!\n").green
39
+ end
40
+ rescue StandardError => e
41
+ puts(e)
42
+ puts("Live config could not be read (if there are no banners, check banner key is marked as an empty array - banners: [])")
43
+ exit(1)
44
+ end
45
+
46
+ task default: %i[check_config rubocop spec]
@@ -0,0 +1 @@
1
+ // Nothing to include here.
@@ -0,0 +1 @@
1
+ //= require govuk_publishing_components/components/intervention
@@ -0,0 +1,11 @@
1
+ <% recruitment_banner = GovukWebBanners::RecruitmentBanner.for_path(request.path) %>
2
+ <% if recruitment_banner.present? %>
3
+ <div class="govuk-width-container govuk-!-margin-top-4">
4
+ <%= render "govuk_publishing_components/components/intervention", {
5
+ new_tab: true,
6
+ suggestion_text: recruitment_banner.suggestion_text,
7
+ suggestion_link_text: recruitment_banner.suggestion_link_text,
8
+ suggestion_link_url: recruitment_banner.survey_url,
9
+ } %>
10
+ </div>
11
+ <% end %>
@@ -0,0 +1,17 @@
1
+ # Example usage of adding a banner to the banners list
2
+
3
+ # - name: Banner 1
4
+ # suggestion_text: "Help improve GOV.UK"
5
+ # suggestion_link_text: "Sign up to take part in user research (opens in a new tab)"
6
+ # survey_url: https://google.com
7
+ # page_paths:
8
+ # - /
9
+ # - /foreign-travel-advice
10
+ # start_date: 21/10/2024
11
+ # end_date: 18/11/2024
12
+
13
+ # start_date and end_date are optional, everything else is mandatory.
14
+ #
15
+ # Note that this file must contain a valid banners array, so if there are no banners
16
+ # currently included, the file should at least contain banners: []
17
+ banners: []
@@ -0,0 +1,5 @@
1
+ module GovukWebBanners
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace GovukWebBanners
4
+ end
5
+ end
@@ -0,0 +1,39 @@
1
+ module GovukWebBanners
2
+ class RecruitmentBanner
3
+ BANNER_CONFIG_FILE = "../../config/govuk_web_banners/recruitment_banners.yml".freeze
4
+
5
+ def self.for_path(path)
6
+ active_banners.find do |banner|
7
+ return banner if banner.page_paths.include?(path)
8
+ end
9
+ end
10
+
11
+ def self.active_banners
12
+ all_banners.select(&:active?)
13
+ end
14
+
15
+ def self.all_banners
16
+ recruitment_banners_urls_file_path = Rails.root.join(__dir__, BANNER_CONFIG_FILE)
17
+ recruitment_banners_data = YAML.load_file(recruitment_banners_urls_file_path)
18
+ recruitment_banners_data["banners"].map { |attributes| RecruitmentBanner.new(attributes:) }
19
+ end
20
+
21
+ attr_reader :name, :suggestion_text, :suggestion_link_text, :survey_url, :page_paths, :start_date, :end_date
22
+
23
+ def initialize(attributes:)
24
+ @name = attributes["name"]
25
+ @suggestion_text = attributes["suggestion_text"]
26
+ @suggestion_link_text = attributes["suggestion_link_text"]
27
+ @survey_url = attributes["survey_url"]
28
+ @page_paths = attributes["page_paths"]
29
+ @start_date = attributes["start_date"] ? Time.parse(attributes["start_date"]) : Time.at(0)
30
+ @end_date = attributes["end_date"] ? Time.parse(attributes["end_date"]) : Time.now + 10.years
31
+ end
32
+
33
+ # NB: .between? is inclusive. To make it exclude the end date, we set the end range as
34
+ # 1 second earlier.
35
+ def active?
36
+ Time.zone.now.between?(start_date, end_date - 1.second)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module GovukWebBanners
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,8 @@
1
+ require "govuk_publishing_components"
2
+
3
+ require "govuk_web_banners/engine"
4
+ require "govuk_web_banners/recruitment_banner"
5
+ require "govuk_web_banners/version"
6
+
7
+ module GovukWebBanners
8
+ end
metadata ADDED
@@ -0,0 +1,183 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: govuk_web_banners
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - GOV.UK Dev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-11-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: govuk_publishing_components
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: govuk_test
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rainbow
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-govuk
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: webmock
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: A gem to support banners on GOV.UK frontend applications
140
+ email:
141
+ - govuk-dev@digital.cabinet-office.gov.uk
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - MIT-LICENSE
147
+ - README.md
148
+ - Rakefile
149
+ - app/assets/config/govuk_web_banners_manifest.js
150
+ - app/assets/javascripts/govuk_web_banners/dependencies.js
151
+ - app/views/govuk_web_banners/_recruitment_banner.html.erb
152
+ - config/govuk_web_banners/recruitment_banners.yml
153
+ - lib/govuk_web_banners.rb
154
+ - lib/govuk_web_banners/engine.rb
155
+ - lib/govuk_web_banners/recruitment_banner.rb
156
+ - lib/govuk_web_banners/version.rb
157
+ homepage: https://github.com/alphagov/govuk_web_banners
158
+ licenses:
159
+ - MIT
160
+ metadata:
161
+ homepage_uri: https://github.com/alphagov/govuk_web_banners
162
+ source_code_uri: https://www.github.com/alphagov/govuk_web_banners
163
+ changelog_uri: https://www.github.com/alphagov/govuk_web_banners/CHANGELOG.md
164
+ post_install_message:
165
+ rdoc_options: []
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '3.1'
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubygems_version: 3.5.23
180
+ signing_key:
181
+ specification_version: 4
182
+ summary: A gem to support banners on GOV.UK frontend applications
183
+ test_files: []