ccs-frontend_helpers 0.1.0.rc.7 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (157) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -5
  3. data/CHANGELOG.md +2 -4
  4. data/Gemfile +1 -15
  5. data/Gemfile.lock +80 -81
  6. data/ccs-frontend_helpers.gemspec +10 -0
  7. data/lib/ccs/frontend_helpers/ccs_frontend/dashboard_panels.rb +79 -0
  8. data/lib/ccs/frontend_helpers/ccs_frontend/footer.rb +119 -6
  9. data/lib/ccs/frontend_helpers/ccs_frontend/header.rb +183 -6
  10. data/lib/ccs/{components/ccs → frontend_helpers/ccs_frontend}/logo.rb +12 -10
  11. data/lib/ccs/frontend_helpers/ccs_frontend.rb +4 -2
  12. data/lib/ccs/frontend_helpers/govuk_frontend/accordion.rb +95 -8
  13. data/lib/ccs/frontend_helpers/govuk_frontend/back_link.rb +17 -6
  14. data/lib/ccs/frontend_helpers/govuk_frontend/breadcrumbs.rb +54 -6
  15. data/lib/ccs/frontend_helpers/govuk_frontend/button.rb +105 -6
  16. data/lib/ccs/frontend_helpers/govuk_frontend/cookie_banner.rb +114 -6
  17. data/lib/ccs/frontend_helpers/govuk_frontend/details.rb +23 -7
  18. data/lib/ccs/frontend_helpers/govuk_frontend/error_message.rb +32 -11
  19. data/lib/ccs/frontend_helpers/govuk_frontend/error_summary.rb +65 -13
  20. data/lib/ccs/frontend_helpers/govuk_frontend/field/character_count.rb +165 -0
  21. data/lib/ccs/frontend_helpers/govuk_frontend/field/checkboxes.rb +200 -0
  22. data/lib/ccs/frontend_helpers/govuk_frontend/field/date_input.rb +153 -0
  23. data/lib/ccs/frontend_helpers/govuk_frontend/field/file_upload.rb +83 -0
  24. data/lib/ccs/frontend_helpers/govuk_frontend/field/input.rb +153 -0
  25. data/lib/ccs/frontend_helpers/govuk_frontend/field/radios.rb +201 -0
  26. data/lib/ccs/frontend_helpers/govuk_frontend/field/select.rb +124 -0
  27. data/lib/ccs/frontend_helpers/govuk_frontend/field/textarea.rb +106 -0
  28. data/lib/ccs/frontend_helpers/govuk_frontend/field.rb +213 -0
  29. data/lib/ccs/frontend_helpers/govuk_frontend/fieldset.rb +48 -7
  30. data/lib/ccs/frontend_helpers/govuk_frontend/footer.rb +161 -6
  31. data/lib/ccs/frontend_helpers/govuk_frontend/form_group.rb +24 -13
  32. data/lib/ccs/frontend_helpers/govuk_frontend/header.rb +139 -6
  33. data/lib/ccs/frontend_helpers/govuk_frontend/hint.rb +17 -8
  34. data/lib/ccs/frontend_helpers/govuk_frontend/inset_text.rb +21 -7
  35. data/lib/ccs/frontend_helpers/govuk_frontend/label.rb +70 -7
  36. data/lib/ccs/frontend_helpers/govuk_frontend/notification_banner.rb +113 -7
  37. data/lib/ccs/frontend_helpers/govuk_frontend/pagination.rb +314 -6
  38. data/lib/ccs/frontend_helpers/govuk_frontend/panel.rb +28 -7
  39. data/lib/ccs/frontend_helpers/govuk_frontend/phase_banner.rb +27 -8
  40. data/lib/ccs/frontend_helpers/govuk_frontend/skip_link.rb +18 -6
  41. data/lib/ccs/frontend_helpers/govuk_frontend/step_by_step_navigation.rb +186 -7
  42. data/lib/ccs/frontend_helpers/govuk_frontend/summary_list.rb +204 -6
  43. data/lib/ccs/frontend_helpers/govuk_frontend/table.rb +100 -11
  44. data/lib/ccs/frontend_helpers/govuk_frontend/tabs.rb +73 -6
  45. data/lib/ccs/frontend_helpers/govuk_frontend/tag.rb +21 -7
  46. data/lib/ccs/frontend_helpers/govuk_frontend/warning_text.rb +30 -7
  47. data/lib/ccs/frontend_helpers/govuk_frontend.rb +19 -17
  48. data/lib/ccs/frontend_helpers/shared_methods.rb +27 -0
  49. data/lib/ccs/frontend_helpers/version.rb +1 -1
  50. metadata +142 -112
  51. data/.ruby-version +0 -1
  52. data/lib/ccs/components/base.rb +0 -64
  53. data/lib/ccs/components/ccs/dashboard_section/panel.rb +0 -57
  54. data/lib/ccs/components/ccs/dashboard_section.rb +0 -71
  55. data/lib/ccs/components/ccs/footer/link.rb +0 -55
  56. data/lib/ccs/components/ccs/footer/meta.rb +0 -59
  57. data/lib/ccs/components/ccs/footer/navigation.rb +0 -60
  58. data/lib/ccs/components/ccs/footer.rb +0 -95
  59. data/lib/ccs/components/ccs/header/link.rb +0 -60
  60. data/lib/ccs/components/ccs/header/navigation.rb +0 -98
  61. data/lib/ccs/components/ccs/header/service_authentication.rb +0 -53
  62. data/lib/ccs/components/ccs/header.rb +0 -110
  63. data/lib/ccs/components/govuk/accordion/section/content.rb +0 -53
  64. data/lib/ccs/components/govuk/accordion/section/header.rb +0 -57
  65. data/lib/ccs/components/govuk/accordion/section.rb +0 -61
  66. data/lib/ccs/components/govuk/accordion.rb +0 -58
  67. data/lib/ccs/components/govuk/back_link.rb +0 -51
  68. data/lib/ccs/components/govuk/breadcrumbs/link.rb +0 -57
  69. data/lib/ccs/components/govuk/breadcrumbs.rb +0 -55
  70. data/lib/ccs/components/govuk/button.rb +0 -107
  71. data/lib/ccs/components/govuk/cookie_banner/action.rb +0 -60
  72. data/lib/ccs/components/govuk/cookie_banner/message.rb +0 -80
  73. data/lib/ccs/components/govuk/cookie_banner.rb +0 -55
  74. data/lib/ccs/components/govuk/details.rb +0 -53
  75. data/lib/ccs/components/govuk/error_message.rb +0 -60
  76. data/lib/ccs/components/govuk/error_summary/item.rb +0 -54
  77. data/lib/ccs/components/govuk/error_summary.rb +0 -70
  78. data/lib/ccs/components/govuk/field/input/character_count.rb +0 -129
  79. data/lib/ccs/components/govuk/field/input/file_upload.rb +0 -44
  80. data/lib/ccs/components/govuk/field/input/select.rb +0 -82
  81. data/lib/ccs/components/govuk/field/input/text_input/fix.rb +0 -55
  82. data/lib/ccs/components/govuk/field/input/text_input.rb +0 -97
  83. data/lib/ccs/components/govuk/field/input/textarea.rb +0 -59
  84. data/lib/ccs/components/govuk/field/input.rb +0 -62
  85. data/lib/ccs/components/govuk/field/inputs/checkboxes.rb +0 -69
  86. data/lib/ccs/components/govuk/field/inputs/date_input/item.rb +0 -65
  87. data/lib/ccs/components/govuk/field/inputs/date_input.rb +0 -89
  88. data/lib/ccs/components/govuk/field/inputs/item/checkbox/form.rb +0 -47
  89. data/lib/ccs/components/govuk/field/inputs/item/checkbox/tag.rb +0 -43
  90. data/lib/ccs/components/govuk/field/inputs/item/checkbox.rb +0 -50
  91. data/lib/ccs/components/govuk/field/inputs/item/divider.rb +0 -50
  92. data/lib/ccs/components/govuk/field/inputs/item/radio/form.rb +0 -44
  93. data/lib/ccs/components/govuk/field/inputs/item/radio/tag.rb +0 -43
  94. data/lib/ccs/components/govuk/field/inputs/item/radio.rb +0 -50
  95. data/lib/ccs/components/govuk/field/inputs/item.rb +0 -111
  96. data/lib/ccs/components/govuk/field/inputs/radios.rb +0 -69
  97. data/lib/ccs/components/govuk/field/inputs.rb +0 -57
  98. data/lib/ccs/components/govuk/field.rb +0 -108
  99. data/lib/ccs/components/govuk/fieldset/legend.rb +0 -65
  100. data/lib/ccs/components/govuk/fieldset.rb +0 -54
  101. data/lib/ccs/components/govuk/footer/link.rb +0 -55
  102. data/lib/ccs/components/govuk/footer/meta.rb +0 -59
  103. data/lib/ccs/components/govuk/footer/navigation.rb +0 -60
  104. data/lib/ccs/components/govuk/footer.rb +0 -131
  105. data/lib/ccs/components/govuk/form_group.rb +0 -60
  106. data/lib/ccs/components/govuk/header/link.rb +0 -56
  107. data/lib/ccs/components/govuk/header/navigation.rb +0 -71
  108. data/lib/ccs/components/govuk/header.rb +0 -107
  109. data/lib/ccs/components/govuk/hint.rb +0 -49
  110. data/lib/ccs/components/govuk/inset_text.rb +0 -52
  111. data/lib/ccs/components/govuk/label.rb +0 -85
  112. data/lib/ccs/components/govuk/notification_banner.rb +0 -107
  113. data/lib/ccs/components/govuk/pagination/increment/next.rb +0 -44
  114. data/lib/ccs/components/govuk/pagination/increment/previous.rb +0 -43
  115. data/lib/ccs/components/govuk/pagination/increment.rb +0 -98
  116. data/lib/ccs/components/govuk/pagination/item/ellipsis.rb +0 -28
  117. data/lib/ccs/components/govuk/pagination/item/form.rb +0 -49
  118. data/lib/ccs/components/govuk/pagination/item/tag.rb +0 -47
  119. data/lib/ccs/components/govuk/pagination/item.rb +0 -64
  120. data/lib/ccs/components/govuk/pagination.rb +0 -96
  121. data/lib/ccs/components/govuk/panel.rb +0 -62
  122. data/lib/ccs/components/govuk/phase_banner.rb +0 -69
  123. data/lib/ccs/components/govuk/skip_link.rb +0 -51
  124. data/lib/ccs/components/govuk/step_by_step_navigation/section/content/list/item.rb +0 -53
  125. data/lib/ccs/components/govuk/step_by_step_navigation/section/content/list.rb +0 -50
  126. data/lib/ccs/components/govuk/step_by_step_navigation/section/content/paragraph.rb +0 -45
  127. data/lib/ccs/components/govuk/step_by_step_navigation/section/content.rb +0 -63
  128. data/lib/ccs/components/govuk/step_by_step_navigation/section/heading.rb +0 -69
  129. data/lib/ccs/components/govuk/step_by_step_navigation/section.rb +0 -56
  130. data/lib/ccs/components/govuk/step_by_step_navigation.rb +0 -59
  131. data/lib/ccs/components/govuk/summary_list/action/link.rb +0 -59
  132. data/lib/ccs/components/govuk/summary_list/card/actions.rb +0 -59
  133. data/lib/ccs/components/govuk/summary_list/card/title.rb +0 -51
  134. data/lib/ccs/components/govuk/summary_list/card.rb +0 -63
  135. data/lib/ccs/components/govuk/summary_list/row/actions.rb +0 -59
  136. data/lib/ccs/components/govuk/summary_list/row/key.rb +0 -47
  137. data/lib/ccs/components/govuk/summary_list/row/value.rb +0 -47
  138. data/lib/ccs/components/govuk/summary_list/row.rb +0 -67
  139. data/lib/ccs/components/govuk/summary_list.rb +0 -74
  140. data/lib/ccs/components/govuk/table/body/data_cell.rb +0 -53
  141. data/lib/ccs/components/govuk/table/body/head_cell.rb +0 -52
  142. data/lib/ccs/components/govuk/table/header/head_cell.rb +0 -54
  143. data/lib/ccs/components/govuk/table.rb +0 -111
  144. data/lib/ccs/components/govuk/tabs/panel.rb +0 -58
  145. data/lib/ccs/components/govuk/tabs/tab.rb +0 -56
  146. data/lib/ccs/components/govuk/tabs.rb +0 -66
  147. data/lib/ccs/components/govuk/tag.rb +0 -51
  148. data/lib/ccs/components/govuk/warning_text.rb +0 -61
  149. data/lib/ccs/frontend_helpers/ccs_frontend/dashboard_section.rb +0 -27
  150. data/lib/ccs/frontend_helpers/govuk_frontend/character_count.rb +0 -28
  151. data/lib/ccs/frontend_helpers/govuk_frontend/checkboxes.rb +0 -28
  152. data/lib/ccs/frontend_helpers/govuk_frontend/date_input.rb +0 -28
  153. data/lib/ccs/frontend_helpers/govuk_frontend/file_upload.rb +0 -28
  154. data/lib/ccs/frontend_helpers/govuk_frontend/input.rb +0 -28
  155. data/lib/ccs/frontend_helpers/govuk_frontend/radios.rb +0 -28
  156. data/lib/ccs/frontend_helpers/govuk_frontend/select.rb +0 -28
  157. data/lib/ccs/frontend_helpers/govuk_frontend/textarea.rb +0 -28
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../../components/govuk/cookie_banner'
3
+ require_relative 'button'
4
4
 
5
5
  module CCS
6
6
  module FrontendHelpers
@@ -11,16 +11,124 @@ module CCS
11
11
  # {https://design-system.service.gov.uk/components/cookie-banner GDS - Components - Cookie banner}
12
12
 
13
13
  module CookieBanner
14
+ include ActionView::Context
15
+ include ActionView::Helpers::TagHelper
16
+ include ActionView::Helpers::TextHelper
17
+ include Button
18
+
14
19
  # Generates the HTML for the GOV.UK Cookie banner component
15
20
  #
16
- # @param (see CCS::Components::GovUK::CookieBanner#initialize)
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
17
77
  #
18
- # @option (see CCS::Components::GovUK::CookieBanner#initialize)
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
19
84
  #
20
- # @return (see CCS::Components::GovUK::CookieBanner#render)
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')
21
128
 
22
- def govuk_cookie_banner(messages, **options)
23
- Components::GovUK::CookieBanner.new(context: self, messages: messages, **options).render
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'
24
132
  end
25
133
  end
26
134
  end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../../components/govuk/details'
3
+ require 'action_view'
4
+
5
+ require_relative '../shared_methods'
4
6
 
5
7
  module CCS
6
8
  module FrontendHelpers
@@ -11,18 +13,32 @@ module CCS
11
13
  # {https://design-system.service.gov.uk/components/details GDS - Components - Details}
12
14
 
13
15
  module Details
16
+ include SharedMethods
17
+ include ActionView::Context
18
+ include ActionView::Helpers::TagHelper
19
+ include ActionView::Helpers::TextHelper
20
+
14
21
  # Generates the HTML for the GOV.UK Details component
15
22
  #
16
- # @param (see CCS::Components::GovUK::Details#initialize)
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
17
25
  #
18
- # @option (see CCS::Components::GovUK::Details#initialize)
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
19
28
  #
20
- # @yield (see CCS::Components::GovUK::Details#render)
29
+ # @yield HTML that will be contained within the 'govuk-details__text' div
21
30
  #
22
- # @return (see CCS::Components::GovUK::Details#render)
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')
23
37
 
24
- def govuk_details(summary_text, **options, &block)
25
- Components::GovUK::Details.new(context: self, summary_text: summary_text, **options).render(&block)
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
26
42
  end
27
43
  end
28
44
  end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../../components/govuk/back_link'
3
+ require 'action_view'
4
+
5
+ require_relative '../shared_methods'
4
6
 
5
7
  module CCS
6
8
  module FrontendHelpers
@@ -11,34 +13,53 @@ module CCS
11
13
  # {https://design-system.service.gov.uk/components/error-message GDS - Components - Error message}
12
14
 
13
15
  module ErrorMessage
16
+ include SharedMethods
17
+ include ActionView::Context
18
+ include ActionView::Helpers::TagHelper
19
+ include ActionView::Helpers::TextHelper
20
+
14
21
  # Generates the HTML for the GOV.UK Error message component
15
22
  #
16
- # @param (see CCS::Components::GovUK::ErrorMessage#initialize)
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
17
26
  #
18
- # @option (see CCS::Components::GovUK::ErrorMessage#initialize)
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
19
30
  #
20
- # @return (see CCS::Components::GovUK::ErrorMessage#render)
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'
21
39
 
22
- def govuk_error_message(error_message, attribute, **options)
23
- Components::GovUK::ErrorMessage.new(context: self, message: error_message, attribute: attribute, **options).render
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
24
44
  end
25
45
 
26
46
  # Generates the HTML for the GOV.UK Error message component using the error messages in an ActiveModel
27
47
  #
28
48
  # @param model [ActiveModel] model that will be used to find the error message
29
49
  # @param attribute [String, Symbol] the attribute that has an error
30
- # @param options [Hash] options that will be used in customising the HTML
50
+ # @param govuk_error_message_options [Hash] options that will be used in customising the HTML
31
51
  #
32
- # @option (see CCS::Components::GovUK::ErrorMessage#initialize)
52
+ # @option (see #govuk_error_message)
33
53
  #
34
54
  # @return [NilClass, ActiveSupport::SafeBuffer] if the error message is not on the model it will return nil,
35
- # otherwise it returns the error message HTML
55
+ # otherwise the HTML for the GOV.UK Error message
56
+ # which can then be rendered on the page is returned
36
57
 
37
- def govuk_error_message_with_model(model, attribute, **options)
58
+ def govuk_error_message_with_model(model, attribute, **govuk_error_message_options)
38
59
  error_message = model.errors[attribute].first
39
60
  return unless error_message
40
61
 
41
- Components::GovUK::ErrorMessage.new(context: self, message: error_message, attribute: attribute, **options).render
62
+ govuk_error_message(error_message, attribute, **govuk_error_message_options)
42
63
  end
43
64
  end
44
65
  end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../../components/govuk/error_summary'
3
+ require 'action_view'
4
+
5
+ require_relative '../shared_methods'
4
6
 
5
7
  module CCS
6
8
  module FrontendHelpers
@@ -11,16 +13,38 @@ module CCS
11
13
  # {https://design-system.service.gov.uk/components/error-summary GDS - Components - Error summary}
12
14
 
13
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
+
14
22
  # Generates the HTML for the GOV.UK Error summary component
15
23
  #
16
- # @param (see CCS::Components::GovUK::ErrorSummary#initialize)
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
17
28
  #
18
- # @option (see CCS::Components::GovUK::ErrorSummary#initialize)
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
19
31
  #
20
- # @return (see CCS::Components::GovUK::ErrorSummary#render)
32
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Error summary
33
+ # which can then be rendered on the page
21
34
 
22
- def govuk_error_summary(title, error_summary_items, description = nil, **options)
23
- Components::GovUK::ErrorSummary.new(context: self, title: title, error_summary_items: error_summary_items, description: description, **options).render
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
24
48
  end
25
49
 
26
50
  # Generates the HTML for the GOV.UK Error summary component using the error messages in an ActiveModel
@@ -28,19 +52,47 @@ module CCS
28
52
  # @param model [ActiveModel] model that will be used to find the error messages to list
29
53
  # @param title [String] text to use for the heading of the error summary block
30
54
  # @param description [String] (nil) optional text to use for the description of the errors
31
- # @param options [Hash] options that will be used in customising the HTML
55
+ # @param govuk_error_summary_options [Hash] options that will be used in customising the HTML
32
56
  #
33
- # @option (see CCS::Components::GovUK::ErrorSummary#initialize)
57
+ # @option (see #govuk_error_summary)
34
58
  #
35
- # @return [NilClass, ActiveSupport::SafeBuffer] if error messages are not on the model it will return nil,
36
- # otherwise it returns the error summary HTML.
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
37
62
 
38
- def govuk_error_summary_with_model(model, title, description = nil, **options)
63
+ def govuk_error_summary_with_model(model, title, description = nil, **govuk_error_summary_options)
39
64
  return if model.errors.blank?
40
65
 
41
- error_summary_items = model.errors.map { |error| { text: error.message, href: "##{error.attribute}-error" } }
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}
42
83
 
43
- Components::GovUK::ErrorSummary.new(context: self, title: title, error_summary_items: error_summary_items, description: description, **options).render
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
44
96
  end
45
97
  end
46
98
  end
@@ -0,0 +1,165 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'textarea'
4
+
5
+ module CCS
6
+ module FrontendHelpers
7
+ module GovUKFrontend
8
+ module Field
9
+ # = GOV.UK Character count
10
+ #
11
+ # This helper is used for generating the character count component from the
12
+ # {https://design-system.service.gov.uk/components/character-count GDS - Components - Character count}
13
+ #
14
+ # This is a wrapper around a Textarea module and so makes use of the methods in {Textarea}
15
+
16
+ module CharacterCount
17
+ include Textarea
18
+
19
+ # Generates the HTML for the GOV.UK character count component.
20
+ # It works by warpping the govuk_textarea in HTML which will trigger the JavaScript to do the character count.
21
+ #
22
+ # @param attribute [String, Symbol] the attribute of the character count text area
23
+ # @param govuk_character_count_options [Hash] options that will be used for the parts of the form group, label, hint, textarea and the character count
24
+ #
25
+ # @option govuk_character_count_options [String] :error_message (nil) the error message to be displayed
26
+ # @option govuk_character_count_options [ActiveModel] :model (nil) optional model that can be used to find an error message
27
+ # @option govuk_character_count_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create
28
+ # the textarea tag and find the error message
29
+ # @option govuk_character_count_options [Hash] :form_group see {govuk_field}
30
+ # @option govuk_character_count_options [Hash] :label see {govuk_field}
31
+ # @option govuk_character_count_options [Hash] :hint see {govuk_field}
32
+ # @option govuk_character_count_options [Hash] :textarea see {govuk_textarea}
33
+ # @option govuk_character_count_options [Hash] :character_count ({}) the options that will be used when rendering the textarea.
34
+ # See {_govuk_character_count} for more details.
35
+ #
36
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK chracter count
37
+ # which can then be rendered on the page
38
+
39
+ def govuk_character_count(attribute, **govuk_character_count_options)
40
+ character_count_attribute = govuk_character_count_options[:form] ? "#{form.object_name}_#{attribute}" : attribute
41
+
42
+ _govuk_character_count(character_count_attribute, **govuk_character_count_options) do |govuk_textarea_options|
43
+ govuk_textarea(
44
+ attribute,
45
+ error_message: govuk_character_count_options[:error_message],
46
+ model: govuk_character_count_options[:model],
47
+ form: govuk_character_count_options[:form],
48
+ **govuk_textarea_options
49
+ )
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ # Wrapper method used by {govuk_character_count} to generate the character count HTML
56
+ #
57
+ # @param attribute [String, Symbol] the attribute of the character count
58
+ # @param govuk_character_count_options [Hash] options that will be used in customising the HTML.
59
+ # This includes everything described in {govuk_character_count}
60
+ # with the addition of the +:character_count+ which are described below
61
+ #
62
+ # @option govuk_character_count_options [String] :maxlength (required) - if +maxwords+ is set, this is not required.
63
+ # The maximum number of characters.
64
+ # If +maxwords+ is provided, the +maxlength+ option will be ignored.
65
+ # @option govuk_character_count_options [String] :maxwords (required) - if maxlength is set, this is not required.
66
+ # The maximum number of words.
67
+ # If +maxwords+ is provided, the +maxlength+ option will be ignored.
68
+ # @option govuk_character_count_options [String] :threshold the percentage value of the limit at which point the count message is displayed.
69
+ # If this attribute is set, the count message will be hidden by default.
70
+ # @option govuk_textarea_options [Hash] :fallback_hint ({}) additional parameters that will be used to create the hint containing the character count text.
71
+ # This includes all the options in {govuk_hint} plus +:count_message+.
72
+ # This will replace the default text for the count message.
73
+ # If you want the count number to appear, put %<count>s in the string and it will be replaced with the number
74
+ #
75
+ # @yield the textarea HTML generated by {govuk_character_count}
76
+ #
77
+ # @yieldparam govuk_textarea_options [Hash] the options used in the textarea called by {govuk_character_count}
78
+ #
79
+ # @return [ActiveSupport::SafeBuffer] the HTML for the chracter count which wrpas arround {govuk_character_count}
80
+
81
+ def _govuk_character_count(attribute, **govuk_character_count_options)
82
+ deep_init_hash(govuk_character_count_options, :textarea, :attributes, :aria)
83
+ govuk_character_count_options[:textarea][:attributes][:aria][:describedby] = [govuk_character_count_options.dig(:textarea, :attributes, :aria, :describedby), "#{attribute}-info"].compact.join(' ')
84
+
85
+ govuk_character_count_options[:textarea][:classes] = "#{govuk_character_count_options[:textarea][:classes]} govuk-js-character-count".lstrip
86
+
87
+ tag.div(**get_character_count_attributes(**govuk_character_count_options)) do
88
+ concat(yield(govuk_character_count_options))
89
+ concat(character_count_hint(attribute, **govuk_character_count_options))
90
+ end
91
+ end
92
+
93
+ # Generates the fallback hint HTML for {_govuk_character_count}
94
+ #
95
+ # @param (see _govuk_character_count)
96
+ #
97
+ # @option (see _govuk_character_count)
98
+ #
99
+ # @return [ActiveSupport::SafeBuffer] the HTML for the fullback hint used in {_govuk_character_count}
100
+
101
+ def character_count_hint(attribute, **govuk_character_count_options)
102
+ fallback_hint_length = govuk_character_count_options[:character_count][:maxwords] || govuk_character_count_options[:character_count][:maxlength]
103
+ fallback_hint_default = "You can enter up to %<count>s #{govuk_character_count_options[:character_count][:maxwords] ? 'words' : 'characters'}"
104
+
105
+ deep_init_hash(govuk_character_count_options, :character_count, :fallback_hint, :attributes)
106
+
107
+ fallback_hint_text = format(govuk_character_count_options[:character_count][:fallback_hint][:count_message] || fallback_hint_default, count: fallback_hint_length)
108
+
109
+ govuk_character_count_options[:character_count][:fallback_hint][:classes] = "#{govuk_character_count_options.dig(:character_count, :fallback_hint, :classes)} govuk-character-count__message".lstrip
110
+ govuk_character_count_options[:character_count][:fallback_hint][:attributes].merge!(id: "#{attribute}-info")
111
+
112
+ govuk_hint(fallback_hint_text, **govuk_character_count_options[:character_count][:fallback_hint])
113
+ end
114
+
115
+ # Generates a hash with the character count attributes used in {_govuk_character_count}
116
+ #
117
+ # @param govuk_character_count_options [Hash] options that will be used in customising the HTML.
118
+ # This includes everything described in {govuk_character_count}
119
+ # with the addition of the +:character_count+ which are described below
120
+ #
121
+ # @option (see _govuk_character_count)
122
+ #
123
+ # @return [Hash] contains the HTMl attributes used in {_govuk_character_count}
124
+
125
+ def get_character_count_attributes(**govuk_character_count_options)
126
+ govuk_character_count_attributes = { class: 'govuk-character-count', data: { module: 'govuk-character-count' } }
127
+
128
+ %i[maxlength threshold maxwords].each do |data_attribute|
129
+ govuk_character_count_attributes[:data][data_attribute] = govuk_character_count_options[:character_count][data_attribute].to_s if govuk_character_count_options[:character_count][data_attribute]
130
+ end
131
+
132
+ govuk_character_count_attributes
133
+ end
134
+
135
+ # Method to initialise hashes within hashes if it is not already initialised
136
+ #
137
+ # @param hash [Hash] the hash that is going to be initialised
138
+ # @param keys [Array] the keys that are going to be initialised in the hash
139
+ #
140
+ # @example When the hash is completely empty
141
+ # hash = { }
142
+ # keys = [:a, :b, :c]
143
+ #
144
+ # deep_init_hash(hash, *keys)
145
+ #
146
+ # hash = { a: { b: { c: { } } } }
147
+ #
148
+ # @example When the hash has already been initialised
149
+ # hash = { a: { b: { d: 'hello' } } }
150
+ # keys = [:a, :b, :c]
151
+ #
152
+ # deep_init_hash(hash, *keys)
153
+ #
154
+ # hash = { a: { b: { d: 'hello', c: { } } } }
155
+
156
+ def deep_init_hash(hash, *keys)
157
+ current_hash = hash
158
+
159
+ keys.each { |key| current_hash = (current_hash[key] ||= {}) }
160
+ end
161
+ end
162
+ end
163
+ end
164
+ end
165
+ end