ccs-frontend_helpers 0.1.0.rc.1

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.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +2 -0
  3. data/.rubocop.yml +127 -0
  4. data/CHANGELOG.md +44 -0
  5. data/Gemfile +10 -0
  6. data/Gemfile.lock +241 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +102 -0
  9. data/Rakefile +10 -0
  10. data/ccs-frontend_helpers.gemspec +47 -0
  11. data/lib/ccs/frontend_helpers/ccs_frontend/dashboard_panels.rb +79 -0
  12. data/lib/ccs/frontend_helpers/ccs_frontend/footer.rb +141 -0
  13. data/lib/ccs/frontend_helpers/ccs_frontend/header.rb +205 -0
  14. data/lib/ccs/frontend_helpers/ccs_frontend/logo.rb +49 -0
  15. data/lib/ccs/frontend_helpers/ccs_frontend.rb +20 -0
  16. data/lib/ccs/frontend_helpers/govuk_frontend/accordion.rb +115 -0
  17. data/lib/ccs/frontend_helpers/govuk_frontend/back_link.rb +39 -0
  18. data/lib/ccs/frontend_helpers/govuk_frontend/breadcrumbs.rb +76 -0
  19. data/lib/ccs/frontend_helpers/govuk_frontend/button.rb +127 -0
  20. data/lib/ccs/frontend_helpers/govuk_frontend/cookie_banner.rb +136 -0
  21. data/lib/ccs/frontend_helpers/govuk_frontend/details.rb +46 -0
  22. data/lib/ccs/frontend_helpers/govuk_frontend/error_message.rb +67 -0
  23. data/lib/ccs/frontend_helpers/govuk_frontend/error_summary.rb +100 -0
  24. data/lib/ccs/frontend_helpers/govuk_frontend/field/character_count.rb +165 -0
  25. data/lib/ccs/frontend_helpers/govuk_frontend/field/checkboxes.rb +200 -0
  26. data/lib/ccs/frontend_helpers/govuk_frontend/field/date_input.rb +153 -0
  27. data/lib/ccs/frontend_helpers/govuk_frontend/field/file_upload.rb +83 -0
  28. data/lib/ccs/frontend_helpers/govuk_frontend/field/input.rb +153 -0
  29. data/lib/ccs/frontend_helpers/govuk_frontend/field/radios.rb +201 -0
  30. data/lib/ccs/frontend_helpers/govuk_frontend/field/select.rb +124 -0
  31. data/lib/ccs/frontend_helpers/govuk_frontend/field/textarea.rb +106 -0
  32. data/lib/ccs/frontend_helpers/govuk_frontend/field.rb +213 -0
  33. data/lib/ccs/frontend_helpers/govuk_frontend/fieldset.rb +71 -0
  34. data/lib/ccs/frontend_helpers/govuk_frontend/footer.rb +183 -0
  35. data/lib/ccs/frontend_helpers/govuk_frontend/form_group.rb +50 -0
  36. data/lib/ccs/frontend_helpers/govuk_frontend/header.rb +161 -0
  37. data/lib/ccs/frontend_helpers/govuk_frontend/hint.rb +38 -0
  38. data/lib/ccs/frontend_helpers/govuk_frontend/inset_text.rb +44 -0
  39. data/lib/ccs/frontend_helpers/govuk_frontend/label.rb +92 -0
  40. data/lib/ccs/frontend_helpers/govuk_frontend/notification_banner.rb +136 -0
  41. data/lib/ccs/frontend_helpers/govuk_frontend/pagination.rb +336 -0
  42. data/lib/ccs/frontend_helpers/govuk_frontend/panel.rb +51 -0
  43. data/lib/ccs/frontend_helpers/govuk_frontend/phase_banner.rb +49 -0
  44. data/lib/ccs/frontend_helpers/govuk_frontend/skip_link.rb +40 -0
  45. data/lib/ccs/frontend_helpers/govuk_frontend/step_by_step_navigation.rb +215 -0
  46. data/lib/ccs/frontend_helpers/govuk_frontend/summary_list.rb +226 -0
  47. data/lib/ccs/frontend_helpers/govuk_frontend/table.rb +124 -0
  48. data/lib/ccs/frontend_helpers/govuk_frontend/tabs.rb +95 -0
  49. data/lib/ccs/frontend_helpers/govuk_frontend/tag.rb +42 -0
  50. data/lib/ccs/frontend_helpers/govuk_frontend/warning_text.rb +53 -0
  51. data/lib/ccs/frontend_helpers/govuk_frontend.rb +82 -0
  52. data/lib/ccs/frontend_helpers/shared_methods.rb +27 -0
  53. data/lib/ccs/frontend_helpers/version.rb +7 -0
  54. data/lib/ccs/frontend_helpers.rb +20 -0
  55. data/sig/ccs/frontend_helpers.rbs +6 -0
  56. metadata +241 -0
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_view'
4
+
5
+ require_relative '../shared_methods'
6
+
7
+ module CCS
8
+ module FrontendHelpers
9
+ module GovUKFrontend
10
+ # = GOV.UK Breadcrumbs
11
+ #
12
+ # This helper is used for generating the breadcrumbs component from the
13
+ # {https://design-system.service.gov.uk/components/breadcrumbs GDS - Components - Breadcrumbs}
14
+
15
+ module Breadcrumbs
16
+ include SharedMethods
17
+ include ActionView::Context
18
+ include ActionView::Helpers::TagHelper
19
+ include ActionView::Helpers::TextHelper
20
+ include ActionView::Helpers::UrlHelper
21
+
22
+ # Generates the HTML for the GOV.UK breadcrumbs component
23
+ #
24
+ # @param govuk_breadcrumb_items [Array<Hash>] An array of links for the breadcrumbs list. See {#govuk_breadcrumb_link} for details of the items in the array.
25
+ # @param govuk_breadcrumbs_options [Hash] options that will be used in customising the HTML
26
+ #
27
+ # @option govuk_breadcrumbs_options [String] :classes additional CSS classes for the breadcrumbs HTML
28
+ # @option govuk_breadcrumbs_options [Boolean] :collapse_on_mobile indicates if it is to colapse breadcrumbs on mobile
29
+ # @option govuk_breadcrumbs_options [Hash] :attributes any additional attributes that will added as part of the HTML
30
+ #
31
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Breadcrumbs
32
+ # which can then be rendered on the page
33
+
34
+ def govuk_breadcrumbs(govuk_breadcrumb_items, **govuk_breadcrumbs_options)
35
+ initialise_attributes_and_set_classes(govuk_breadcrumbs_options, 'govuk-breadcrumbs')
36
+
37
+ govuk_breadcrumbs_options[:attributes][:class] << ' govuk-breadcrumbs--collapse-on-mobile' if govuk_breadcrumbs_options[:collapse_on_mobile]
38
+
39
+ tag.div(**govuk_breadcrumbs_options[:attributes]) do
40
+ tag.ol(class: 'govuk-breadcrumbs__list') do
41
+ govuk_breadcrumb_items.each { |govuk_breadcrumb_item| concat(govuk_breadcrumb_link(govuk_breadcrumb_item)) }
42
+ end
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ # Generates the HTML for each link in the breadcrumbs.
49
+ # It is called by {#govuk_breadcrumbs} which will pass in the breadcrum item.
50
+ #
51
+ # @param govuk_breadcrumb_item [Hash] a hash containg options for the breadcrumb item
52
+ #
53
+ # @option govuk_breadcrumb_item [String] :text the text for the link
54
+ # @option govuk_breadcrumb_item [String] :href the URI for the link. If blank it is assumed that this item relates to current page
55
+ # @option govuk_breadcrumb_item [Hash] :attributes any additional attributes that will added as part of the HTML.
56
+ # If the link is blank then it defaults to +{ aria: { current: 'page' } }+
57
+ #
58
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Breadcrumb list item
59
+
60
+ def govuk_breadcrumb_link(govuk_breadcrumb_item)
61
+ if govuk_breadcrumb_item[:href].present?
62
+ (govuk_breadcrumb_item[:attributes] ||= {})[:class] = 'govuk-breadcrumbs__link'
63
+
64
+ tag.li(class: 'govuk-breadcrumbs__list-item') do
65
+ link_to(govuk_breadcrumb_item[:text], govuk_breadcrumb_item[:href], **govuk_breadcrumb_item[:attributes])
66
+ end
67
+ else
68
+ tag.li(class: 'govuk-breadcrumbs__list-item', aria: { current: 'page' }) do
69
+ govuk_breadcrumb_item[:text]
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_view'
4
+
5
+ require_relative '../shared_methods'
6
+
7
+ module CCS
8
+ module FrontendHelpers
9
+ module GovUKFrontend
10
+ # = GOV.UK Button
11
+ #
12
+ # This helper is used for generating the button component from the
13
+ # {https://design-system.service.gov.uk/components/button GDS - Components - Button}
14
+
15
+ module Button
16
+ include SharedMethods
17
+ include ActionView::Context
18
+ include ActionView::Helpers::TagHelper
19
+ include ActionView::Helpers::TextHelper
20
+ include ActionView::Helpers::UrlHelper
21
+ include ActionView::Helpers::FormTagHelper
22
+
23
+ # Generates the HTML for the GOV.UK button component
24
+ #
25
+ # @param text [String] the text that will be shown in the button
26
+ # @param govuk_button_options [Hash] options that will be used in customising the HTML
27
+ #
28
+ # @option govuk_button_options [String] :classes additional CSS classes for the button HTML
29
+ # @option govuk_button_options [Boolean] :is_start_button indicates if it is a start button
30
+ # @option govuk_button_options [String] :href the URI that will be used in anchor tag
31
+ # @option govuk_button_options [ActionView::Helpers::FormBuilder] :form the form builder used to create the submit button
32
+ # @option govuk_button_options [Hash] :attributes ({ data: { module: 'govuk-button' } }) any additional attributes that will added as part of the HTML
33
+ #
34
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Button
35
+ # which can then be rendered on the page
36
+
37
+ def govuk_button(text, **govuk_button_options)
38
+ initialise_attributes_and_set_classes(govuk_button_options, 'govuk-button')
39
+ set_data_module(govuk_button_options, 'govuk-button')
40
+
41
+ govuk_button_options[:attributes][:class] << ' govuk-button--disabled' if govuk_button_options[:attributes][:disabled]
42
+ govuk_button_options[:attributes][:class] << ' govuk-button--start' if govuk_button_options[:is_start_button]
43
+
44
+ button_method = if govuk_button_options[:href]
45
+ :govuk_button_link
46
+ elsif govuk_button_options[:form]
47
+ :govuk_button_submit
48
+ else
49
+ :govuk_button_button
50
+ end
51
+
52
+ send(button_method, text, **govuk_button_options)
53
+ end
54
+
55
+ private
56
+
57
+ # Generates the HTML for the GOV.UK button component as an anchor tag.
58
+ # It is called by {#govuk_button} which will pass in the parameters, including any defaults.
59
+ #
60
+ # @param text [String] the text that will be shown in the button
61
+ # @param govuk_button_options [Hash] options that will be used in customising the HTML
62
+ #
63
+ # @option govuk_button_options [Boolean] :is_start_button indicates if it is a start button
64
+ # @option govuk_button_options [String] :href the URI that will be used in anchor tag
65
+ # @option govuk_button_options [Hash] :attributes any additional attributes that will added as part of the HTML
66
+ #
67
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Button as an anchor element
68
+ # which can then be rendered on the page
69
+
70
+ def govuk_button_link(text, **govuk_button_options)
71
+ govuk_button_options[:attributes][:role] = :button
72
+ govuk_button_options[:attributes][:draggable] = false
73
+
74
+ link_to(govuk_button_options[:href], **govuk_button_options[:attributes]) do
75
+ concat(text)
76
+ concat(govuk_start_button_icon) if govuk_button_options[:is_start_button]
77
+ end
78
+ end
79
+
80
+ # Generates the HTML for the GOV.UK button component as a button.
81
+ # It is called by {#govuk_button} which will pass in the parameters, including any defaults.
82
+ #
83
+ # @param text [String] the text that will be shown in the button
84
+ # @param govuk_button_options [Hash] options that will be used in customising the HTML
85
+ #
86
+ # @option govuk_button_options [Boolean] :is_start_button indicates if it is a start button
87
+ # @option govuk_button_options [Hash] :attributes any additional attributes that will added as part of the HTML
88
+ #
89
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Button as a button element
90
+ # which can then be rendered on the page
91
+
92
+ def govuk_button_button(text, **govuk_button_options)
93
+ button_tag(**govuk_button_options[:attributes]) do
94
+ concat(text)
95
+ concat(govuk_start_button_icon) if govuk_button_options[:is_start_button]
96
+ end
97
+ end
98
+
99
+ # Generates the HTML for the GOV.UK button component as an input.
100
+ # It is called by {#govuk_button} which will pass in the parameters, including any defaults.
101
+ #
102
+ # @param text [String] the text that will be shown in the input
103
+ # @param govuk_button_options [Hash] options that will be used in customising the HTML
104
+ #
105
+ # @option govuk_button_options [ActionView::Helpers::FormBuilder] :form the form builder used to create the submit button
106
+ # @option govuk_button_options [Hash] :attributes any additional attributes that will added as part of the HTML
107
+ #
108
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Button as an input element
109
+ # which can then be rendered on the page
110
+
111
+ def govuk_button_submit(text, **govuk_button_options)
112
+ govuk_button_options[:form].submit(text, **govuk_button_options[:attributes])
113
+ end
114
+
115
+ # Generates the arrow for the start button option as part of {#govuk_button}
116
+ #
117
+ # @return [ActiveSupport::SafeBuffer] the HTML for the arrow for the start button
118
+
119
+ def govuk_start_button_icon
120
+ tag.svg(class: 'govuk-button__start-icon', xmlns: 'http://www.w3.org/2000/svg', width: 17.5, height: 19, viewBox: '0 0 33 40', aria: { hidden: true }, focusable: false) do
121
+ tag.path(fill: 'currentColor', d: 'M0 0h13l20 20-20 20H0l20-20z')
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'button'
4
+
5
+ module CCS
6
+ module FrontendHelpers
7
+ module GovUKFrontend
8
+ # = GOV.UK Cookie Banner
9
+ #
10
+ # This helper is used for generating the cookie banner component from the
11
+ # {https://design-system.service.gov.uk/components/cookie-banner GDS - Components - Cookie banner}
12
+
13
+ module CookieBanner
14
+ include ActionView::Context
15
+ include ActionView::Helpers::TagHelper
16
+ include ActionView::Helpers::TextHelper
17
+ include Button
18
+
19
+ # Generates the HTML for the GOV.UK Cookie banner component
20
+ #
21
+ # @param messages [Array] the different messages you can pass into the cookie banner. See {govuk_cookie_banner_message_content}
22
+ # @param govuk_cookie_banner_options [Hash] options that will be used in customising the HTML
23
+ #
24
+ # @option govuk_cookie_banner_options [String] :classes additional CSS classes for the cookie banner HTML
25
+ # @option govuk_cookie_banner_options [Hash] :attributes any additional attributes that will added as part of the HTML
26
+ #
27
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Cookie banner
28
+ # which can then be rendered on the page
29
+
30
+ def govuk_cookie_banner(messages, **govuk_cookie_banner_options)
31
+ determine_govuk_cookie_banner_attributes(govuk_cookie_banner_options)
32
+
33
+ tag.div(**govuk_cookie_banner_options[:attributes]) do
34
+ messages.each do |message|
35
+ initialise_attributes_and_set_classes(message, 'govuk-cookie-banner__message govuk-width-container')
36
+
37
+ concat(tag.div(**message[:attributes]) do
38
+ concat(govuk_cookie_banner_message_content(message))
39
+ concat(govuk_cookie_banner_message_actions(message[:actions])) if message[:actions]
40
+ end)
41
+ end
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ # Generates the HTML for a cookie banner message content used in {govuk_cookie_banner}
48
+ #
49
+ # @param message [Hash] the options for the cookie banner message
50
+ #
51
+ # @option message [String] :heading_text the heading text that displays in the message
52
+ # @option message [ActiveSupport::SafeBuffer] :content HTML to use as content for the message
53
+ # @option message [String] :text if +:content+ is blank then this is the text used for the message
54
+ # @option message [Array] :actions the buttons and links that you want to display in the message. See {govuk_cookie_banner_message_actions}
55
+ # @option message [String] :classes additional CSS classes for the cookie message HTML
56
+ # @option message [Hash] :attributes any additional attributes that will added as part of the HTML
57
+ #
58
+ # @return [ActiveSupport::SafeBuffer] the HTML for a cookie banner message content used in {govuk_cookie_banner}
59
+
60
+ def govuk_cookie_banner_message_content(message)
61
+ tag.div(class: 'govuk-grid-row') do
62
+ tag.div(class: 'govuk-grid-column-two-thirds') do
63
+ concat(tag.h2(message[:heading_text], class: 'govuk-cookie-banner__heading govuk-heading-m')) if message[:heading_text]
64
+ concat(tag.div(class: 'govuk-cookie-banner__content') do
65
+ message[:content] || tag.p(message[:text], class: 'govuk-body')
66
+ end)
67
+ end
68
+ end
69
+ end
70
+
71
+ # rubocop:disable Metrics/AbcSize
72
+
73
+ # Generates the HTML for the cookie banner message actions used in {govuk_cookie_banner}
74
+ # It defaults to creating button unless a +href+ is set which will create a link
75
+ #
76
+ # @param message_actions [Hash] the options for the cookie banner message actions
77
+ #
78
+ # @option message_actions [String] :text the button or link text
79
+ # @option message_actions [String] :href the href for a link
80
+ # @option message_actions [String] :classes additional CSS classes for the cookie action button or link
81
+ # @option message_actions [Hash] :attributes any additional attributes that will added as part of the HTML.
82
+ # If the +:type+ key is present with a value of +:button+,
83
+ # the action will be rendered as a button
84
+ #
85
+ # @return [ActiveSupport::SafeBuffer] the HTML for the cookie banner message actions used in {govuk_cookie_banner}
86
+
87
+ def govuk_cookie_banner_message_actions(message_actions)
88
+ tag.div(class: 'govuk-button-group') do
89
+ message_actions.each do |message_action|
90
+ message_action[:attributes] ||= {}
91
+
92
+ concat(
93
+ if message_action[:href]
94
+ if message_action[:attributes][:type] == :button
95
+ govuk_button(
96
+ message_action[:text],
97
+ href: message_action[:href],
98
+ classes: message_action[:classes],
99
+ attributes: message_action[:attributes]
100
+ )
101
+ else
102
+ message_action[:attributes][:class] = "govuk-link #{message_action[:classes]}".rstrip
103
+
104
+ link_to(message_action[:text], message_action[:href], **message_action[:attributes])
105
+ end
106
+ else
107
+ govuk_button(
108
+ message_action[:text],
109
+ classes: message_action[:classes],
110
+ attributes: message_action[:attributes]
111
+ )
112
+ end
113
+ )
114
+ end
115
+ end
116
+ end
117
+
118
+ # rubocop:enable Metrics/AbcSize
119
+
120
+ # Sets the default attributes for {govuk_cookie_banner}
121
+ #
122
+ # @param govuk_cookie_banner_options [Hash] options that will be used in customising the HTML
123
+ #
124
+ # @option (see govuk_cookie_banner)
125
+
126
+ def determine_govuk_cookie_banner_attributes(govuk_cookie_banner_options)
127
+ initialise_attributes_and_set_classes(govuk_cookie_banner_options, 'govuk-cookie-banner')
128
+
129
+ (govuk_cookie_banner_options[:attributes][:data] ||= {})[:nosnippet] = 'true'
130
+ govuk_cookie_banner_options[:attributes][:role] = 'region'
131
+ (govuk_cookie_banner_options[:attributes][:aria] ||= {})[:label] ||= 'Cookie banner'
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_view'
4
+
5
+ require_relative '../shared_methods'
6
+
7
+ module CCS
8
+ module FrontendHelpers
9
+ module GovUKFrontend
10
+ # = GOV.UK Details
11
+ #
12
+ # This helper is used for generating the details component from the
13
+ # {https://design-system.service.gov.uk/components/details GDS - Components - Details}
14
+
15
+ module Details
16
+ include SharedMethods
17
+ include ActionView::Context
18
+ include ActionView::Helpers::TagHelper
19
+ include ActionView::Helpers::TextHelper
20
+
21
+ # Generates the HTML for the GOV.UK Details component
22
+ #
23
+ # @param summary_text [String] the summary text for the details element
24
+ # @param govuk_details_options [Hash] options that will be used in customising the HTML
25
+ #
26
+ # @option govuk_details_options [String] :classes additional CSS classes for the details HTML
27
+ # @option govuk_details_options [Hash] :attributes ({ data: { module: 'govuk-details' } }) any additional attributes that will added as part of the HTML
28
+ #
29
+ # @yield HTML that will be contained within the 'govuk-details__text' div
30
+ #
31
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Details
32
+ # which can then be rendered on the page
33
+
34
+ def govuk_details(summary_text, **govuk_details_options, &block)
35
+ initialise_attributes_and_set_classes(govuk_details_options, 'govuk-details')
36
+ set_data_module(govuk_details_options, 'govuk-details')
37
+
38
+ tag.details(**govuk_details_options[:attributes]) do
39
+ concat(tag.summary(tag.span(summary_text, class: 'govuk-details__summary-text'), class: 'govuk-details__summary'))
40
+ concat(tag.div(class: 'govuk-details__text', &block))
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_view'
4
+
5
+ require_relative '../shared_methods'
6
+
7
+ module CCS
8
+ module FrontendHelpers
9
+ module GovUKFrontend
10
+ # = GOV.UK Error Message
11
+ #
12
+ # This helper is used for generating the error message component from the
13
+ # {https://design-system.service.gov.uk/components/error-message GDS - Components - Error message}
14
+
15
+ module ErrorMessage
16
+ include SharedMethods
17
+ include ActionView::Context
18
+ include ActionView::Helpers::TagHelper
19
+ include ActionView::Helpers::TextHelper
20
+
21
+ # Generates the HTML for the GOV.UK Error message component
22
+ #
23
+ # @param message [String] the message to be displayed
24
+ # @param attribute [String, Symbol] the attribute that has an error
25
+ # @param govuk_error_message_options [Hash] options that will be used in customising the HTML
26
+ #
27
+ # @option govuk_error_message_options [String] :classes additional CSS classes for the error message HTML
28
+ # @option govuk_error_message_options [String] :visually_hidden_text ('Error') visualy hidden text before the error message
29
+ # @option govuk_error_message_options [Hash] :attributes ({}) any additional attributes that will be added as part of the HTML
30
+ #
31
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Error message
32
+ # which can then be rendered on the page
33
+
34
+ def govuk_error_message(message, attribute, **govuk_error_message_options)
35
+ initialise_attributes_and_set_classes(govuk_error_message_options, 'govuk-error-message')
36
+
37
+ govuk_error_message_options[:attributes][:id] ||= "#{attribute}-error"
38
+ visually_hidden_text = govuk_error_message_options[:visually_hidden_text] || 'Error'
39
+
40
+ tag.p(**govuk_error_message_options[:attributes]) do
41
+ concat(tag.span("#{visually_hidden_text}: ", class: 'govuk-visually-hidden')) if visually_hidden_text.present?
42
+ concat(message)
43
+ end
44
+ end
45
+
46
+ # Generates the HTML for the GOV.UK Error message component using the error messages in an ActiveModel
47
+ #
48
+ # @param model [ActiveModel] model that will be used to find the error message
49
+ # @param attribute [String, Symbol] the attribute that has an error
50
+ # @param govuk_error_message_options [Hash] options that will be used in customising the HTML
51
+ #
52
+ # @option (see #govuk_error_message)
53
+ #
54
+ # @return [NilClass, ActiveSupport::SafeBuffer] if the error message is not on the model it will return nil,
55
+ # otherwise the HTML for the GOV.UK Error message
56
+ # which can then be rendered on the page is returned
57
+
58
+ def govuk_error_message_with_model(model, attribute, **govuk_error_message_options)
59
+ error_message = model.errors[attribute].first
60
+ return unless error_message
61
+
62
+ govuk_error_message(error_message, attribute, **govuk_error_message_options)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_view'
4
+
5
+ require_relative '../shared_methods'
6
+
7
+ module CCS
8
+ module FrontendHelpers
9
+ module GovUKFrontend
10
+ # = GOV.UK Error Summary
11
+ #
12
+ # This helper is used for generating the error summary component from the
13
+ # {https://design-system.service.gov.uk/components/error-summary GDS - Components - Error summary}
14
+
15
+ module ErrorSummary
16
+ include SharedMethods
17
+ include ActionView::Context
18
+ include ActionView::Helpers::TagHelper
19
+ include ActionView::Helpers::TextHelper
20
+ include ActionView::Helpers::UrlHelper
21
+
22
+ # Generates the HTML for the GOV.UK Error summary component
23
+ #
24
+ # @param title [String] text to use for the heading of the error summary block
25
+ # @param error_list [Array] the list of errors to include in the summary. See {govuk_error_summary_list}
26
+ # @param description [String] optional text to use for the description of the errors
27
+ # @param govuk_error_summary_options [Hash] options that will be used in customising the HTML
28
+ #
29
+ # @option govuk_error_summary_options [String] :classes additional CSS classes for the error summary HTML
30
+ # @option govuk_error_summary_options [Hash] :attributes ({}) any additional attributes that will be added as part of the HTML
31
+ #
32
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Error summary
33
+ # which can then be rendered on the page
34
+
35
+ def govuk_error_summary(title, error_list, description = nil, **govuk_error_summary_options)
36
+ initialise_attributes_and_set_classes(govuk_error_summary_options, 'govuk-error-summary')
37
+ set_data_module(govuk_error_summary_options, 'govuk-error-summary')
38
+
39
+ tag.div(**govuk_error_summary_options[:attributes]) do
40
+ tag.div(role: 'alert') do
41
+ concat(tag.h2(title, class: 'govuk-error-summary__title'))
42
+ concat(tag.div(class: 'govuk-error-summary__body') do
43
+ concat(tag.p(description)) if description
44
+ concat(govuk_error_summary_list(error_list))
45
+ end)
46
+ end
47
+ end
48
+ end
49
+
50
+ # Generates the HTML for the GOV.UK Error summary component using the error messages in an ActiveModel
51
+ #
52
+ # @param model [ActiveModel] model that will be used to find the error messages to list
53
+ # @param title [String] text to use for the heading of the error summary block
54
+ # @param description [String] (nil) optional text to use for the description of the errors
55
+ # @param govuk_error_summary_options [Hash] options that will be used in customising the HTML
56
+ #
57
+ # @option (see #govuk_error_summary)
58
+ #
59
+ # @return [NilClass, ActiveSupport::SafeBuffer] if there are no errors on the model it will return nil,
60
+ # otherwise the HTML for the GOV.UK Error summary
61
+ # which can then be rendered on the page is returned
62
+
63
+ def govuk_error_summary_with_model(model, title, description = nil, **govuk_error_summary_options)
64
+ return if model.errors.blank?
65
+
66
+ error_list = model.errors.map { |error| { text: error.message, href: "##{error.attribute}-error" } }
67
+
68
+ govuk_error_summary(title, error_list, description, **govuk_error_summary_options)
69
+ end
70
+
71
+ private
72
+
73
+ # Generates the HTML for the error list in {govuk_error_summary}
74
+ #
75
+ # @param error_list [Array] The list of errors to include in the summary. See {#govuk_error_summary_list}
76
+ #
77
+ # @option error_list [String] :href href for the error link item. If provided item will be a link
78
+ # @option error_list [String] :text Text for the error link item
79
+ # @option error_list [Hash] :attributes ({}) any additional attributes that will be added as part of the link item
80
+ #
81
+ # @return [ActiveSupport::SafeBuffer] the HTML for the error list
82
+ # which is used in {govuk_error_summary}
83
+
84
+ def govuk_error_summary_list(error_list)
85
+ tag.ul(class: 'govuk-list govuk-error-summary__list') do
86
+ error_list.each do |error|
87
+ concat(tag.li do
88
+ if error[:href]
89
+ link_to(error[:text], error[:href], **(error[:attributes] || {}))
90
+ else
91
+ error[:text]
92
+ end
93
+ end)
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end